blob: 79a854e8c4c603a0148cbe246e22d49a4d8e7731 [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;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700792 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700793 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700794
Eric Laurente552edb2014-03-10 17:42:56 -0700795#ifdef AUDIO_POLICY_TEST
796 if (mCurOutput != 0) {
797 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
798 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
799
800 if (mTestOutputs[mCurOutput] == 0) {
801 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700802 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
803 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700804 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700805 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700806 outputDesc->mFlags =
807 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700808 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700809 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
810 config.sample_rate = mTestSamplingRate;
811 config.channel_mask = mTestChannels;
812 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700813 if (offloadInfo != NULL) {
814 config.offload_info = *offloadInfo;
815 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700816 status = mpClientInterface->openOutput(0,
817 &mTestOutputs[mCurOutput],
818 &config,
819 &outputDesc->mDevice,
820 String8(""),
821 &outputDesc->mLatency,
822 outputDesc->mFlags);
823 if (status == NO_ERROR) {
824 outputDesc->mSamplingRate = config.sample_rate;
825 outputDesc->mFormat = config.format;
826 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700827 AudioParameter outputCmd = AudioParameter();
828 outputCmd.addInt(String8("set_id"),mCurOutput);
829 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
830 addOutput(mTestOutputs[mCurOutput], outputDesc);
831 }
832 }
833 return mTestOutputs[mCurOutput];
834 }
835#endif //AUDIO_POLICY_TEST
836
837 // open a direct output if required by specified parameters
838 //force direct flag if offload flag is set: offloading implies a direct output stream
839 // and all common behaviors are driven by checking only the direct flag
840 // this should normally be set appropriately in the policy configuration file
841 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700842 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700843 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700844 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
845 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
846 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800847 // only allow deep buffering for music stream type
848 if (stream != AUDIO_STREAM_MUSIC) {
849 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700850 } else if (/* stream == AUDIO_STREAM_MUSIC && */
851 flags == AUDIO_OUTPUT_FLAG_NONE &&
852 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
853 // use DEEP_BUFFER as default output for music stream type
854 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800855 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700856 if (stream == AUDIO_STREAM_TTS) {
857 flags = AUDIO_OUTPUT_FLAG_TTS;
858 }
Eric Laurente552edb2014-03-10 17:42:56 -0700859
Eric Laurentb732cf52014-09-24 19:08:21 -0700860 sp<IOProfile> profile;
861
862 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700863 // and not explicitly requested
864 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800865 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700866 audio_channel_count_from_out_mask(channelMask) <= 2) {
867 goto non_direct_output;
868 }
869
Andy Hung2ddee192015-12-18 17:34:44 -0800870 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
871 // This prevents creating an offloaded track and tearing it down immediately after start
872 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700873 // FIXME: We should check the audio session here but we do not have it in this context.
874 // This may prevent offloading in rare situations where effects are left active by apps
875 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700876
Eric Laurente552edb2014-03-10 17:42:56 -0700877 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800878 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700879 profile = getProfileForDirectOutput(device,
880 samplingRate,
881 format,
882 channelMask,
883 (audio_output_flags_t)flags);
884 }
885
Eric Laurent1c333e22014-05-20 10:48:17 -0700886 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700887 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700888
889 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700890 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700891 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
892 outputDesc = desc;
893 // reuse direct output if currently open and configured with same parameters
894 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800895 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700896 (channelMask == outputDesc->mChannelMask)) {
897 outputDesc->mDirectOpenCount++;
898 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
899 return mOutputs.keyAt(i);
900 }
901 }
902 }
903 // close direct output if currently open and configured with different parameters
904 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700905 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700906 }
Eric Laurent861a6282015-05-18 15:40:16 -0700907
908 // if the selected profile is offloaded and no offload info was specified,
909 // create a default one
910 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100911 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700912 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
913 defaultOffloadInfo.sample_rate = samplingRate;
914 defaultOffloadInfo.channel_mask = channelMask;
915 defaultOffloadInfo.format = format;
916 defaultOffloadInfo.stream_type = stream;
917 defaultOffloadInfo.bit_rate = 0;
918 defaultOffloadInfo.duration_us = -1;
919 defaultOffloadInfo.has_video = true; // conservative
920 defaultOffloadInfo.is_streaming = true; // likely
921 offloadInfo = &defaultOffloadInfo;
922 }
923
Eric Laurentc75307b2015-03-17 15:29:32 -0700924 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700925 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700926 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700927 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700928 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
929 config.sample_rate = samplingRate;
930 config.channel_mask = channelMask;
931 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700932 if (offloadInfo != NULL) {
933 config.offload_info = *offloadInfo;
934 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700935 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700936 &output,
937 &config,
938 &outputDesc->mDevice,
939 String8(""),
940 &outputDesc->mLatency,
941 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700942
943 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700944 if (status != NO_ERROR ||
945 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800946 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700947 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700948 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
949 "format %d %d, channelMask %04x %04x", output, samplingRate,
950 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
951 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700952 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700953 mpClientInterface->closeOutput(output);
954 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800955 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800956 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800957 goto non_direct_output;
958 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700959 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700960 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700961 outputDesc->mSamplingRate = config.sample_rate;
962 outputDesc->mChannelMask = config.channel_mask;
963 outputDesc->mFormat = config.format;
964 outputDesc->mRefCount[stream] = 0;
965 outputDesc->mStopTime[stream] = 0;
966 outputDesc->mDirectOpenCount = 1;
967
Eric Laurente552edb2014-03-10 17:42:56 -0700968 audio_io_handle_t srcOutput = getOutputForEffect();
969 addOutput(output, outputDesc);
970 audio_io_handle_t dstOutput = getOutputForEffect();
971 if (dstOutput == output) {
972 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
973 }
974 mPreviousOutputs = mOutputs;
975 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700976 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700977 return output;
978 }
979
Eric Laurentb732cf52014-09-24 19:08:21 -0700980non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700981
982 // A request for HW A/V sync cannot fallback to a mixed output because time
983 // stamps are embedded in audio data
984 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
985 return AUDIO_IO_HANDLE_NONE;
986 }
987
Eric Laurente552edb2014-03-10 17:42:56 -0700988 // ignoring channel mask due to downmix capability in mixer
989
990 // open a non direct output
991
992 // for non direct outputs, only PCM is supported
993 if (audio_is_linear_pcm(format)) {
994 // get which output is suitable for the specified stream. The actual
995 // routing change will happen when startOutput() will be called
996 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
997
Eric Laurent8838a382014-09-08 16:44:28 -0700998 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
999 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1000 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001001 }
1002 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1003 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1004
Paul McLeanaa981192015-03-21 09:55:15 -07001005 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001006
1007 return output;
1008}
1009
Eric Laurente0720872014-03-11 09:30:41 -07001010audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001011 audio_output_flags_t flags,
1012 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001013{
1014 // select one output among several that provide a path to a particular device or set of
1015 // devices (the list was previously build by getOutputsForDevice()).
1016 // The priority is as follows:
1017 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001018 // 2: the output with the bit depth the closest to the requested one
1019 // 3: the primary output
1020 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001021
1022 if (outputs.size() == 0) {
1023 return 0;
1024 }
1025 if (outputs.size() == 1) {
1026 return outputs[0];
1027 }
1028
1029 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001030 audio_io_handle_t outputForFlags = 0;
1031 audio_io_handle_t outputForPrimary = 0;
1032 audio_io_handle_t outputForFormat = 0;
1033 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1034 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001035
1036 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001037 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001038 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001039 // if a valid format is specified, skip output if not compatible
1040 if (format != AUDIO_FORMAT_INVALID) {
1041 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001042 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001043 continue;
1044 }
1045 } else if (!audio_is_linear_pcm(format)) {
1046 continue;
1047 }
Eric Laurente6930022016-02-11 10:20:40 -08001048 if (AudioPort::isBetterFormatMatch(
1049 outputDesc->mFormat, bestFormat, format)) {
1050 outputForFormat = outputs[i];
1051 bestFormat = outputDesc->mFormat;
1052 }
Eric Laurent8838a382014-09-08 16:44:28 -07001053 }
1054
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001055 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001056 if (commonFlags >= maxCommonFlags) {
1057 if (commonFlags == maxCommonFlags) {
1058 if (AudioPort::isBetterFormatMatch(
1059 outputDesc->mFormat, bestFormatForFlags, format)) {
1060 outputForFlags = outputs[i];
1061 bestFormatForFlags = outputDesc->mFormat;
1062 }
1063 } else {
1064 outputForFlags = outputs[i];
1065 maxCommonFlags = commonFlags;
1066 bestFormatForFlags = outputDesc->mFormat;
1067 }
Eric Laurente552edb2014-03-10 17:42:56 -07001068 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1069 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001070 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001071 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001072 }
1073 }
1074 }
1075
Eric Laurente6930022016-02-11 10:20:40 -08001076 if (outputForFlags != 0) {
1077 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001078 }
Eric Laurente6930022016-02-11 10:20:40 -08001079 if (outputForFormat != 0) {
1080 return outputForFormat;
1081 }
1082 if (outputForPrimary != 0) {
1083 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001084 }
1085
1086 return outputs[0];
1087}
1088
Eric Laurente0720872014-03-11 09:30:41 -07001089status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001090 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001091 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001092{
Paul McLeanaa981192015-03-21 09:55:15 -07001093 ALOGV("startOutput() output %d, stream %d, session %d",
1094 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001095 ssize_t index = mOutputs.indexOfKey(output);
1096 if (index < 0) {
1097 ALOGW("startOutput() unknown output %d", output);
1098 return BAD_VALUE;
1099 }
1100
Eric Laurentc75307b2015-03-17 15:29:32 -07001101 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1102
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001103 // Routing?
1104 mOutputRoutes.incRouteActivity(session);
1105
Eric Laurentc75307b2015-03-17 15:29:32 -07001106 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001107 AudioMix *policyMix = NULL;
1108 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001109 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001110 policyMix = outputDesc->mPolicyMix;
1111 address = policyMix->mDeviceAddress.string();
1112 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1113 newDevice = policyMix->mDeviceType;
1114 } else {
1115 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1116 }
Eric Laurent493404d2015-04-21 15:07:36 -07001117 } else if (mOutputRoutes.hasRouteChanged(session)) {
1118 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001119 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001120 } else {
1121 newDevice = AUDIO_DEVICE_NONE;
1122 }
1123
1124 uint32_t delayMs = 0;
1125
Eric Laurentc40d9692016-04-13 19:14:13 -07001126 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001127
1128 if (status != NO_ERROR) {
1129 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001130 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001131 }
1132 // Automatically enable the remote submix input when output is started on a re routing mix
1133 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001134 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1135 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001136 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1137 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001138 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001139 "remote-submix");
1140 }
1141
1142 if (delayMs != 0) {
1143 usleep(delayMs * 1000);
1144 }
1145
1146 return status;
1147}
1148
1149status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1150 audio_stream_type_t stream,
1151 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001152 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001153 uint32_t *delayMs)
1154{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001155 // cannot start playback of STREAM_TTS if any other output is being used
1156 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001157
1158 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001159 if (stream == AUDIO_STREAM_TTS) {
1160 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001161 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001162 return INVALID_OPERATION;
1163 } else {
1164 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1165 }
1166 } else {
1167 // some playback other than beacon starts
1168 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1169 }
1170
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001171 // check active before incrementing usage count
1172 bool force = !outputDesc->isActive();
1173
Eric Laurente552edb2014-03-10 17:42:56 -07001174 // increment usage count for this stream on the requested output:
1175 // NOTE that the usage count is the same for duplicated output and hardware output which is
1176 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1177 outputDesc->changeRefCount(stream, 1);
1178
Eric Laurent493404d2015-04-21 15:07:36 -07001179 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001180 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001181 if (device == AUDIO_DEVICE_NONE) {
1182 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001183 }
Eric Laurente552edb2014-03-10 17:42:56 -07001184 routing_strategy strategy = getStrategy(stream);
1185 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001186 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1187 (beaconMuteLatency > 0);
1188 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001189 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001190 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001191 if (desc != outputDesc) {
1192 // force a device change if any other output is managed by the same hw
1193 // module and has a current device selection that differs from selected device.
1194 // In this case, the audio HAL must receive the new device selection so that it can
1195 // change the device currently selected by the other active output.
1196 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001197 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001198 force = true;
1199 }
1200 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001201 // a notification so that audio focus effect can propagate, or that a mute/unmute
1202 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001203 uint32_t latency = desc->latency();
1204 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1205 waitMs = latency;
1206 }
1207 }
1208 }
Eric Laurentc40d9692016-04-13 19:14:13 -07001209 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001210
1211 // handle special case for sonification while in call
1212 if (isInCall()) {
1213 handleIncallSonification(stream, true, false);
1214 }
1215
1216 // apply volume rules for current stream and device if necessary
1217 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001218 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001219 outputDesc,
1220 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001221
1222 // update the outputs if starting an output with a stream that can affect notification
1223 // routing
1224 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001225
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001226 // force reevaluating accessibility routing when ringtone or alarm starts
1227 if (strategy == STRATEGY_SONIFICATION) {
1228 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1229 }
Eric Laurente552edb2014-03-10 17:42:56 -07001230 }
1231 return NO_ERROR;
1232}
1233
1234
Eric Laurente0720872014-03-11 09:30:41 -07001235status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001236 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001237 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001238{
1239 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1240 ssize_t index = mOutputs.indexOfKey(output);
1241 if (index < 0) {
1242 ALOGW("stopOutput() unknown output %d", output);
1243 return BAD_VALUE;
1244 }
1245
Eric Laurentc75307b2015-03-17 15:29:32 -07001246 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001247
Eric Laurentc75307b2015-03-17 15:29:32 -07001248 if (outputDesc->mRefCount[stream] == 1) {
1249 // Automatically disable the remote submix input when output is stopped on a
1250 // re routing mix of type MIX_TYPE_RECORDERS
1251 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1252 outputDesc->mPolicyMix != NULL &&
1253 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1254 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1255 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001256 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001257 "remote-submix");
1258 }
1259 }
1260
1261 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001262 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001263 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001264 int activityCount = mOutputRoutes.decRouteActivity(session);
1265 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1266
1267 if (forceDeviceUpdate) {
1268 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1269 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001270 }
1271
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001272 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001273}
1274
1275status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001276 audio_stream_type_t stream,
1277 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001278{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001279 // always handle stream stop, check which stream type is stopping
1280 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1281
Eric Laurente552edb2014-03-10 17:42:56 -07001282 // handle special case for sonification while in call
1283 if (isInCall()) {
1284 handleIncallSonification(stream, false, false);
1285 }
1286
1287 if (outputDesc->mRefCount[stream] > 0) {
1288 // decrement usage count of this stream on the output
1289 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001290
Eric Laurente552edb2014-03-10 17:42:56 -07001291 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001292 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001293 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001294 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001295 // delay the device switch by twice the latency because stopOutput() is executed when
1296 // the track stop() command is received and at that time the audio track buffer can
1297 // still contain data that needs to be drained. The latency only covers the audio HAL
1298 // and kernel buffers. Also the latency does not always include additional delay in the
1299 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001300 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001301
1302 // force restoring the device selection on other active outputs if it differs from the
1303 // one being selected for this output
1304 for (size_t i = 0; i < mOutputs.size(); i++) {
1305 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001306 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001307 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001308 desc->isActive() &&
1309 outputDesc->sharesHwModuleWith(desc) &&
1310 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001311 setOutputDevice(desc,
1312 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001313 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001314 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001315 }
1316 }
1317 // update the outputs if stopping one with a stream that can affect notification routing
1318 handleNotificationRoutingForStream(stream);
1319 }
1320 return NO_ERROR;
1321 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001322 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001323 return INVALID_OPERATION;
1324 }
1325}
1326
Eric Laurente83b55d2014-11-14 10:06:21 -08001327void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001328 audio_stream_type_t stream __unused,
1329 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001330{
1331 ALOGV("releaseOutput() %d", output);
1332 ssize_t index = mOutputs.indexOfKey(output);
1333 if (index < 0) {
1334 ALOGW("releaseOutput() releasing unknown output %d", output);
1335 return;
1336 }
1337
1338#ifdef AUDIO_POLICY_TEST
1339 int testIndex = testOutputIndex(output);
1340 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001341 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001342 if (outputDesc->isActive()) {
1343 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001344 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001345 mTestOutputs[testIndex] = 0;
1346 }
1347 return;
1348 }
1349#endif //AUDIO_POLICY_TEST
1350
Paul McLeanaa981192015-03-21 09:55:15 -07001351 // Routing
1352 mOutputRoutes.removeRoute(session);
1353
Eric Laurentc75307b2015-03-17 15:29:32 -07001354 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001355 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001356 if (desc->mDirectOpenCount <= 0) {
1357 ALOGW("releaseOutput() invalid open count %d for output %d",
1358 desc->mDirectOpenCount, output);
1359 return;
1360 }
1361 if (--desc->mDirectOpenCount == 0) {
1362 closeOutput(output);
1363 // If effects where present on the output, audioflinger moved them to the primary
1364 // output by default: move them back to the appropriate output.
1365 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001366 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001367 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1368 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001369 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001370 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001371 }
1372 }
1373}
1374
1375
Eric Laurentcaf7f482014-11-25 17:50:47 -08001376status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1377 audio_io_handle_t *input,
1378 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001379 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001380 uint32_t samplingRate,
1381 audio_format_t format,
1382 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001383 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001384 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001385 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001386{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001387 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1388 "session %d, flags %#x",
1389 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001390
Eric Laurentcaf7f482014-11-25 17:50:47 -08001391 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001392 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001393
Eric Laurent275e8e92014-11-30 15:14:47 -08001394 audio_devices_t device;
1395 // handle legacy remote submix case where the address was not always specified
1396 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001397 audio_source_t inputSource = attr->source;
1398 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001399 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001400
Eric Laurentc447ded2015-01-06 08:47:05 -08001401 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1402 inputSource = AUDIO_SOURCE_MIC;
1403 }
1404 halInputSource = inputSource;
1405
Paul McLean466dc8e2015-04-17 13:15:36 -06001406 // Explicit routing?
1407 sp<DeviceDescriptor> deviceDesc;
1408 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1409 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1410 deviceDesc = mAvailableInputDevices[i];
1411 break;
1412 }
1413 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001414 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001415
Eric Laurentc447ded2015-01-06 08:47:05 -08001416 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001417 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001418 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001419 if (ret != NO_ERROR) {
1420 return ret;
1421 }
1422 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001423 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1424 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001425 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001426 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001427 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001428 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001429 return BAD_VALUE;
1430 }
Eric Laurentc722f302014-12-10 11:21:49 -08001431 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001432 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001433 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1434 // there is an external policy, but this input is attached to a mix of recorders,
1435 // meaning it receives audio injected into the framework, so the recorder doesn't
1436 // know about it and is therefore considered "legacy"
1437 *inputType = API_INPUT_LEGACY;
1438 } else {
1439 // recording a mix of players defined by an external policy, we're rerouting for
1440 // an external policy
1441 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1442 }
Eric Laurentc722f302014-12-10 11:21:49 -08001443 } else if (audio_is_remote_submix_device(device)) {
1444 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001445 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001446 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1447 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001448 } else {
1449 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001450 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001451
Eric Laurent599c7582015-12-07 18:05:55 -08001452 }
1453
1454 *input = getInputForDevice(device, address, session, uid, inputSource,
1455 samplingRate, format, channelMask, flags,
1456 policyMix);
1457 if (*input == AUDIO_IO_HANDLE_NONE) {
1458 mInputRoutes.removeRoute(session);
1459 return INVALID_OPERATION;
1460 }
1461 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1462 return NO_ERROR;
1463}
1464
1465
1466audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1467 String8 address,
1468 audio_session_t session,
1469 uid_t uid,
1470 audio_source_t inputSource,
1471 uint32_t samplingRate,
1472 audio_format_t format,
1473 audio_channel_mask_t channelMask,
1474 audio_input_flags_t flags,
1475 AudioMix *policyMix)
1476{
1477 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1478 audio_source_t halInputSource = inputSource;
1479 bool isSoundTrigger = false;
1480
1481 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1482 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1483 if (index >= 0) {
1484 input = mSoundTriggerSessions.valueFor(session);
1485 isSoundTrigger = true;
1486 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1487 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1488 } else {
1489 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001490 }
1491 }
1492
Andy Hungf129b032015-04-07 13:45:50 -07001493 // find a compatible input profile (not necessarily identical in parameters)
1494 sp<IOProfile> profile;
1495 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001496 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001497 audio_format_t profileFormat = format;
1498 audio_channel_mask_t profileChannelMask = channelMask;
1499 audio_input_flags_t profileFlags = flags;
1500 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001501 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001502 profileSamplingRate, profileFormat, profileChannelMask,
1503 profileFlags);
1504 if (profile != 0) {
1505 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001506 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1507 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001508 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1509 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1510 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001511 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1512 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001513 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001514 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001515 }
Eric Laurente552edb2014-03-10 17:42:56 -07001516 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001517 // Pick input sampling rate if not specified by client
1518 if (samplingRate == 0) {
1519 samplingRate = profileSamplingRate;
1520 }
Eric Laurente552edb2014-03-10 17:42:56 -07001521
Eric Laurent322b4d22015-04-03 15:57:54 -07001522 if (profile->getModuleHandle() == 0) {
1523 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001524 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001525 }
1526
Eric Laurent599c7582015-12-07 18:05:55 -08001527 sp<AudioSession> audioSession = new AudioSession(session,
1528 inputSource,
1529 format,
1530 samplingRate,
1531 channelMask,
1532 flags,
1533 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001534 isSoundTrigger,
1535 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001536
Eric Laurentfb66dd92016-01-28 18:32:03 -08001537
Eric Laurent599c7582015-12-07 18:05:55 -08001538 // reuse an open input if possible
1539 for (size_t i = 0; i < mInputs.size(); i++) {
1540 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001541 // reuse input if:
1542 // - it shares the same profile
1543 // AND
1544 // - it is not a reroute submix input
1545 // AND
1546 // - it is: not used for sound trigger
1547 // OR
1548 // used for sound trigger and all clients use the same session ID
1549 //
1550 if ((profile == desc->mProfile) &&
1551 (isSoundTrigger == desc->isSoundTrigger()) &&
1552 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001553
1554 sp<AudioSession> as = desc->getAudioSession(session);
1555 if (as != 0) {
1556 // do not allow unmatching properties on same session
1557 if (as->matches(audioSession)) {
1558 as->changeOpenCount(1);
1559 } else {
1560 ALOGW("getInputForDevice() record with different attributes"
1561 " exists for session %d", session);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001562 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001563 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001564 } else if (isSoundTrigger) {
1565 break;
1566 }
1567 // force close input if current source is now the highest priority request on this input
1568 // and current input properties are not exactly as requested.
1569 if ((desc->mSamplingRate != samplingRate ||
1570 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001571 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001572 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
1573 source_priority(inputSource))) {
1574 ALOGV("%s: ", __FUNCTION__);
1575 AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/);
1576 for (size_t j = 0; j < sessions.size(); j++) {
1577 audio_session_t currentSession = sessions.keyAt(j);
1578 stopInput(desc->mIoHandle, currentSession);
1579 releaseInput(desc->mIoHandle, currentSession);
1580 }
1581 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001582 } else {
1583 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001584 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1585 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001586 }
Eric Laurent599c7582015-12-07 18:05:55 -08001587 }
1588 }
Eric Laurent599c7582015-12-07 18:05:55 -08001589
Eric Laurentcf2c0212014-07-25 16:20:43 -07001590 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001591 config.sample_rate = profileSamplingRate;
1592 config.channel_mask = profileChannelMask;
1593 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001594
Eric Laurent322b4d22015-04-03 15:57:54 -07001595 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001596 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001597 &config,
1598 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001599 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001600 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001601 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001602
1603 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001604 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001605 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001606 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001607 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001608 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1609 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001610 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001611 if (input != AUDIO_IO_HANDLE_NONE) {
1612 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001613 }
Eric Laurent599c7582015-12-07 18:05:55 -08001614 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001615 }
1616
Eric Laurent1f2f2232014-06-02 12:01:23 -07001617 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001618 inputDesc->mSamplingRate = profileSamplingRate;
1619 inputDesc->mFormat = profileFormat;
1620 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001621 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001622 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001623 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001624
Eric Laurent599c7582015-12-07 18:05:55 -08001625 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001626 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001627
Eric Laurent599c7582015-12-07 18:05:55 -08001628 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001629}
1630
Eric Laurentfb66dd92016-01-28 18:32:03 -08001631bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1632 const sp<AudioSession>& audioSession)
1633{
1634 // Do not allow capture if an active voice call is using a software patch and
1635 // the call TX source device is on the same HW module.
1636 // FIXME: would be better to refine to only inputs whose profile connects to the
1637 // call TX device but this information is not in the audio patch
1638 if (mCallTxPatch != 0 &&
1639 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1640 return false;
1641 }
1642
1643 // starting concurrent capture is enabled if:
1644 // 1) capturing for re-routing
1645 // 2) capturing for HOTWORD source
1646 // 3) capturing for FM TUNER source
1647 // 3) All other active captures are either for re-routing or HOTWORD
1648
1649 if (is_virtual_input_device(inputDesc->mDevice) ||
1650 audioSession->inputSource() == AUDIO_SOURCE_HOTWORD ||
1651 audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) {
1652 return true;
1653 }
1654
1655 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1656 for (size_t i = 0; i < activeInputs.size(); i++) {
1657 sp<AudioInputDescriptor> activeInput = activeInputs[i];
1658 if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) &&
1659 (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) &&
1660 !is_virtual_input_device(activeInput->mDevice)) {
1661 return false;
1662 }
1663 }
1664
1665 return true;
1666}
1667
1668
Eric Laurent4dc68062014-07-28 17:26:49 -07001669status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001670 audio_session_t session,
1671 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001672{
1673 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001674 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001675 ssize_t index = mInputs.indexOfKey(input);
1676 if (index < 0) {
1677 ALOGW("startInput() unknown input %d", input);
1678 return BAD_VALUE;
1679 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001680 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001681
Eric Laurent599c7582015-12-07 18:05:55 -08001682 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1683 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001684 ALOGW("startInput() unknown session %d on input %d", session, input);
1685 return BAD_VALUE;
1686 }
1687
Eric Laurentfb66dd92016-01-28 18:32:03 -08001688 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1689 ALOGW("startInput(%d) failed: other input already started", input);
1690 return INVALID_OPERATION;
1691 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001692
Eric Laurentfb66dd92016-01-28 18:32:03 -08001693 if (isInCall()) {
1694 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1695 }
1696 if (mInputs.activeInputsCount() != 0) {
1697 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001698 }
1699
Eric Laurent313d1e72016-01-29 09:56:57 -08001700 // increment activity count before calling getNewInputDevice() below as only active sessions
1701 // are considered for device selection
1702 audioSession->changeActiveCount(1);
1703
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001704 // Routing?
1705 mInputRoutes.incRouteActivity(session);
1706
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001707 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001708
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001709 setInputDevice(input, getNewInputDevice(inputDesc), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001710
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001711 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1712 // if input maps to a dynamic policy with an activity listener, notify of state change
1713 if ((inputDesc->mPolicyMix != NULL)
1714 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1715 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1716 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001717 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001718
1719 if (mInputs.activeInputsCount() == 0) {
1720 SoundTrigger::setCaptureState(true);
1721 }
1722
1723 // automatically enable the remote submix output when input is started if not
1724 // used by a policy mix of type MIX_TYPE_RECORDERS
1725 // For remote submix (a virtual device), we open only one input per capture request.
1726 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1727 String8 address = String8("");
1728 if (inputDesc->mPolicyMix == NULL) {
1729 address = String8("0");
1730 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1731 address = inputDesc->mPolicyMix->mDeviceAddress;
1732 }
1733 if (address != "") {
1734 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1735 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1736 address, "remote-submix");
1737 }
Eric Laurentc722f302014-12-10 11:21:49 -08001738 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001739 }
Eric Laurente552edb2014-03-10 17:42:56 -07001740 }
1741
Eric Laurent599c7582015-12-07 18:05:55 -08001742 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001743
Eric Laurente552edb2014-03-10 17:42:56 -07001744 return NO_ERROR;
1745}
1746
Eric Laurent4dc68062014-07-28 17:26:49 -07001747status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1748 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001749{
1750 ALOGV("stopInput() input %d", input);
1751 ssize_t index = mInputs.indexOfKey(input);
1752 if (index < 0) {
1753 ALOGW("stopInput() unknown input %d", input);
1754 return BAD_VALUE;
1755 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001756 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001757
Eric Laurent599c7582015-12-07 18:05:55 -08001758 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001759 if (index < 0) {
1760 ALOGW("stopInput() unknown session %d on input %d", session, input);
1761 return BAD_VALUE;
1762 }
1763
Eric Laurent599c7582015-12-07 18:05:55 -08001764 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001765 ALOGW("stopInput() input %d already stopped", input);
1766 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001767 }
1768
Eric Laurent599c7582015-12-07 18:05:55 -08001769 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001770
1771 // Routing?
1772 mInputRoutes.decRouteActivity(session);
1773
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001774 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001775
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001776 if (inputDesc->isActive()) {
1777 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1778 } else {
1779 // if input maps to a dynamic policy with an activity listener, notify of state change
1780 if ((inputDesc->mPolicyMix != NULL)
1781 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1782 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1783 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001784 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001785
1786 // automatically disable the remote submix output when input is stopped if not
1787 // used by a policy mix of type MIX_TYPE_RECORDERS
1788 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1789 String8 address = String8("");
1790 if (inputDesc->mPolicyMix == NULL) {
1791 address = String8("0");
1792 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1793 address = inputDesc->mPolicyMix->mDeviceAddress;
1794 }
1795 if (address != "") {
1796 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1797 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1798 address, "remote-submix");
1799 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001800 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001801
Eric Laurentfb66dd92016-01-28 18:32:03 -08001802 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001803
Eric Laurentfb66dd92016-01-28 18:32:03 -08001804 if (mInputs.activeInputsCount() == 0) {
1805 SoundTrigger::setCaptureState(false);
1806 }
1807 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00001808 }
Eric Laurente552edb2014-03-10 17:42:56 -07001809 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001810 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001811}
1812
Eric Laurent4dc68062014-07-28 17:26:49 -07001813void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1814 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001815{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001816
Eric Laurente552edb2014-03-10 17:42:56 -07001817 ALOGV("releaseInput() %d", input);
1818 ssize_t index = mInputs.indexOfKey(input);
1819 if (index < 0) {
1820 ALOGW("releaseInput() releasing unknown input %d", input);
1821 return;
1822 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001823
1824 // Routing
1825 mInputRoutes.removeRoute(session);
1826
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001827 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1828 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001829
Eric Laurent599c7582015-12-07 18:05:55 -08001830 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001831 if (index < 0) {
1832 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1833 return;
1834 }
Eric Laurent599c7582015-12-07 18:05:55 -08001835
1836 if (audioSession->openCount() == 0) {
1837 ALOGW("releaseInput() invalid open count %d on session %d",
1838 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001839 return;
1840 }
Eric Laurent599c7582015-12-07 18:05:55 -08001841
1842 if (audioSession->changeOpenCount(-1) == 0) {
1843 inputDesc->removeAudioSession(session);
1844 }
1845
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001846 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001847 ALOGV("releaseInput() exit > 0");
1848 return;
1849 }
1850
Eric Laurent05b90f82014-08-27 15:32:29 -07001851 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001852 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001853 ALOGV("releaseInput() exit");
1854}
1855
Eric Laurentd4692962014-05-05 18:13:44 -07001856void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001857 bool patchRemoved = false;
1858
Eric Laurentd4692962014-05-05 18:13:44 -07001859 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001860 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001861 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001862 if (patch_index >= 0) {
1863 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1864 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1865 mAudioPatches.removeItemsAt(patch_index);
1866 patchRemoved = true;
1867 }
Eric Laurentd4692962014-05-05 18:13:44 -07001868 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1869 }
1870 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001871 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001872 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001873
1874 if (patchRemoved) {
1875 mpClientInterface->onAudioPatchListUpdate();
1876 }
Eric Laurentd4692962014-05-05 18:13:44 -07001877}
1878
Eric Laurente0720872014-03-11 09:30:41 -07001879void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001880 int indexMin,
1881 int indexMax)
1882{
1883 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001884 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001885
1886 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001887 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1888 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001889 continue;
1890 }
1891 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001892 }
Eric Laurente552edb2014-03-10 17:42:56 -07001893}
1894
Eric Laurente0720872014-03-11 09:30:41 -07001895status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001896 int index,
1897 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001898{
1899
François Gaffied1ab2bd2015-12-02 18:20:06 +01001900 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1901 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001902 return BAD_VALUE;
1903 }
1904 if (!audio_is_output_device(device)) {
1905 return BAD_VALUE;
1906 }
1907
1908 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001909 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001910
Eric Laurent1fd372e2016-04-06 14:23:53 -07001911 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001912 stream, device, index);
1913
Eric Laurent28d09f02016-03-08 10:43:05 -08001914 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001915 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1916 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001917 continue;
1918 }
1919 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1920 }
Eric Laurente552edb2014-03-10 17:42:56 -07001921
Eric Laurent1fd372e2016-04-06 14:23:53 -07001922 // update volume on all outputs and streams matching the following:
1923 // - The requested stream (or a stream matching for volume control) is active on the output
1924 // - The device (or devices) selected by the strategy corresponding to this stream includes
1925 // the requested device
1926 // - For non default requested device, currently selected device on the output is either the
1927 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001928 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1929 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001930 status_t status = NO_ERROR;
1931 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001932 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1933 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001934 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1935 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001936 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001937 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001938 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1939 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001940 continue;
1941 }
Eric Laurent794fde22016-03-11 09:50:45 -08001942 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001943 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001944 if ((curStreamDevice & device) == 0) {
1945 continue;
1946 }
1947 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001948 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001949 curStreamDevice |= device;
1950 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1951 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1952 applyDefault = true;
1953 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001954
Eric Laurent1fd372e2016-04-06 14:23:53 -07001955 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001956 status_t volStatus =
1957 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1958 if (volStatus != NO_ERROR) {
1959 status = volStatus;
1960 }
1961 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001962 }
Eric Laurente552edb2014-03-10 17:42:56 -07001963 }
1964 return status;
1965}
1966
Eric Laurente0720872014-03-11 09:30:41 -07001967status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001968 int *index,
1969 audio_devices_t device)
1970{
1971 if (index == NULL) {
1972 return BAD_VALUE;
1973 }
1974 if (!audio_is_output_device(device)) {
1975 return BAD_VALUE;
1976 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001977 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001978 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001979 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001980 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1981 }
François Gaffiedfd74092015-03-19 12:10:59 +01001982 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001983
François Gaffied1ab2bd2015-12-02 18:20:06 +01001984 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001985 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1986 return NO_ERROR;
1987}
1988
Eric Laurente0720872014-03-11 09:30:41 -07001989audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001990 const SortedVector<audio_io_handle_t>& outputs)
1991{
1992 // select one output among several suitable for global effects.
1993 // The priority is as follows:
1994 // 1: An offloaded output. If the effect ends up not being offloadable,
1995 // AudioFlinger will invalidate the track and the offloaded output
1996 // will be closed causing the effect to be moved to a PCM output.
1997 // 2: A deep buffer output
1998 // 3: the first output in the list
1999
2000 if (outputs.size() == 0) {
2001 return 0;
2002 }
2003
2004 audio_io_handle_t outputOffloaded = 0;
2005 audio_io_handle_t outputDeepBuffer = 0;
2006
2007 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002008 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07002009 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07002010 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2011 outputOffloaded = outputs[i];
2012 }
2013 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
2014 outputDeepBuffer = outputs[i];
2015 }
2016 }
2017
2018 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
2019 outputOffloaded, outputDeepBuffer);
2020 if (outputOffloaded != 0) {
2021 return outputOffloaded;
2022 }
2023 if (outputDeepBuffer != 0) {
2024 return outputDeepBuffer;
2025 }
2026
2027 return outputs[0];
2028}
2029
Eric Laurente0720872014-03-11 09:30:41 -07002030audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002031{
2032 // apply simple rule where global effects are attached to the same output as MUSIC streams
2033
Eric Laurent3b73df72014-03-11 09:06:29 -07002034 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002035 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2036 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2037
2038 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2039 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2040 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2041
2042 return output;
2043}
2044
Eric Laurente0720872014-03-11 09:30:41 -07002045status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002046 audio_io_handle_t io,
2047 uint32_t strategy,
2048 int session,
2049 int id)
2050{
2051 ssize_t index = mOutputs.indexOfKey(io);
2052 if (index < 0) {
2053 index = mInputs.indexOfKey(io);
2054 if (index < 0) {
2055 ALOGW("registerEffect() unknown io %d", io);
2056 return INVALID_OPERATION;
2057 }
2058 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002059 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002060}
2061
Eric Laurentc75307b2015-03-17 15:29:32 -07002062bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2063{
Eric Laurent28d09f02016-03-08 10:43:05 -08002064 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002065 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2066 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002067 continue;
2068 }
2069 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2070 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002071 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002072}
2073
2074bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2075{
2076 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2077}
2078
Eric Laurente0720872014-03-11 09:30:41 -07002079bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002080{
2081 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002082 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002083 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002084 return true;
2085 }
2086 }
2087 return false;
2088}
2089
Eric Laurent275e8e92014-11-30 15:14:47 -08002090// Register a list of custom mixes with their attributes and format.
2091// When a mix is registered, corresponding input and output profiles are
2092// added to the remote submix hw module. The profile contains only the
2093// parameters (sampling rate, format...) specified by the mix.
2094// The corresponding input remote submix device is also connected.
2095//
2096// When a remote submix device is connected, the address is checked to select the
2097// appropriate profile and the corresponding input or output stream is opened.
2098//
2099// When capture starts, getInputForAttr() will:
2100// - 1 look for a mix matching the address passed in attribtutes tags if any
2101// - 2 if none found, getDeviceForInputSource() will:
2102// - 2.1 look for a mix matching the attributes source
2103// - 2.2 if none found, default to device selection by policy rules
2104// At this time, the corresponding output remote submix device is also connected
2105// and active playback use cases can be transferred to this mix if needed when reconnecting
2106// after AudioTracks are invalidated
2107//
2108// When playback starts, getOutputForAttr() will:
2109// - 1 look for a mix matching the address passed in attribtutes tags if any
2110// - 2 if none found, look for a mix matching the attributes usage
2111// - 3 if none found, default to device and output selection by policy rules.
2112
2113status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2114{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002115 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2116 status_t res = NO_ERROR;
2117
2118 sp<HwModule> rSubmixModule;
2119 // examine each mix's route type
2120 for (size_t i = 0; i < mixes.size(); i++) {
2121 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2122 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2123 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002124 break;
2125 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002126 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2127 // Loop back through "remote submix"
2128 if (rSubmixModule == 0) {
2129 for (size_t j = 0; i < mHwModules.size(); j++) {
2130 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2131 && mHwModules[j]->mHandle != 0) {
2132 rSubmixModule = mHwModules[j];
2133 break;
2134 }
2135 }
2136 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002137
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002138 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002139
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002140 if (rSubmixModule == 0) {
2141 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2142 res = INVALID_OPERATION;
2143 break;
2144 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002145
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002146 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002147
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002148 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002149 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002150 res = INVALID_OPERATION;
2151 break;
2152 }
2153 audio_config_t outputConfig = mixes[i].mFormat;
2154 audio_config_t inputConfig = mixes[i].mFormat;
2155 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2156 // stereo and let audio flinger do the channel conversion if needed.
2157 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2158 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2159 rSubmixModule->addOutputProfile(address, &outputConfig,
2160 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2161 rSubmixModule->addInputProfile(address, &inputConfig,
2162 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002163
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002164 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2165 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2166 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2167 address.string(), "remote-submix");
2168 } else {
2169 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2170 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2171 address.string(), "remote-submix");
2172 }
2173 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002174 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002175 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002176 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2177 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002178
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002179 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002180 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2181 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2182 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2183 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2184 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2185 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002186 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2187 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002188 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2189 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002190 } else {
2191 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002192 }
2193 break;
2194 }
2195 }
2196
2197 if (res != NO_ERROR) {
2198 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002199 i, device, address.string());
2200 res = INVALID_OPERATION;
2201 break;
2202 } else if (!foundOutput) {
2203 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2204 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002205 res = INVALID_OPERATION;
2206 break;
2207 }
Eric Laurentc722f302014-12-10 11:21:49 -08002208 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002209 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002210 if (res != NO_ERROR) {
2211 unregisterPolicyMixes(mixes);
2212 }
2213 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002214}
2215
2216status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2217{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002218 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002219 status_t res = NO_ERROR;
2220 sp<HwModule> rSubmixModule;
2221 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002222 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002223 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002224
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002225 if (rSubmixModule == 0) {
2226 for (size_t j = 0; i < mHwModules.size(); j++) {
2227 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2228 && mHwModules[j]->mHandle != 0) {
2229 rSubmixModule = mHwModules[j];
2230 break;
2231 }
2232 }
2233 }
2234 if (rSubmixModule == 0) {
2235 res = INVALID_OPERATION;
2236 continue;
2237 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002238
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002239 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002240
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002241 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2242 res = INVALID_OPERATION;
2243 continue;
2244 }
2245
2246 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2247 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2248 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2249 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2250 address.string(), "remote-submix");
2251 }
2252 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2253 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2254 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2255 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2256 address.string(), "remote-submix");
2257 }
2258 rSubmixModule->removeOutputProfile(address);
2259 rSubmixModule->removeInputProfile(address);
2260
2261 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2262 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2263 res = INVALID_OPERATION;
2264 continue;
2265 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002266 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002267 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002268 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002269}
2270
Eric Laurente552edb2014-03-10 17:42:56 -07002271
Eric Laurente0720872014-03-11 09:30:41 -07002272status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002273{
2274 const size_t SIZE = 256;
2275 char buffer[SIZE];
2276 String8 result;
2277
2278 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2279 result.append(buffer);
2280
Eric Laurent87ffa392015-05-22 10:32:38 -07002281 snprintf(buffer, SIZE, " Primary Output: %d\n",
2282 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002283 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002284 std::string stateLiteral;
2285 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2286 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002287 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002288 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002289 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
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 media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
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 dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002296 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002297 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002298 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002299 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002300 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002301 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002302 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2303 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2304 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002305 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2306 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002307 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2308 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002309
François Gaffie2110e042015-03-24 08:41:51 +01002310 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002311
François Gaffie112b0af2015-11-19 16:13:25 +01002312 mAvailableOutputDevices.dump(fd, String8("Available output"));
2313 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002314 mHwModules.dump(fd);
2315 mOutputs.dump(fd);
2316 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002317 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002318 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002319 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002320
2321 return NO_ERROR;
2322}
2323
2324// This function checks for the parameters which can be offloaded.
2325// This can be enhanced depending on the capability of the DSP and policy
2326// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002327bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002328{
2329 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002330 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002331 offloadInfo.sample_rate, offloadInfo.channel_mask,
2332 offloadInfo.format,
2333 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2334 offloadInfo.has_video);
2335
Andy Hung2ddee192015-12-18 17:34:44 -08002336 if (mMasterMono) {
2337 return false; // no offloading if mono is set.
2338 }
2339
Eric Laurente552edb2014-03-10 17:42:56 -07002340 // Check if offload has been disabled
2341 char propValue[PROPERTY_VALUE_MAX];
2342 if (property_get("audio.offload.disable", propValue, "0")) {
2343 if (atoi(propValue) != 0) {
2344 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2345 return false;
2346 }
2347 }
2348
2349 // Check if stream type is music, then only allow offload as of now.
2350 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2351 {
2352 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2353 return false;
2354 }
2355
2356 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002357 const bool allowOffloadWithVideo =
2358 property_get_bool("audio.offload.video", false /* default_value */);
2359 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002360 ALOGV("isOffloadSupported: has_video == true, returning false");
2361 return false;
2362 }
2363
2364 //If duration is less than minimum value defined in property, return false
2365 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2366 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2367 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2368 return false;
2369 }
2370 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2371 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2372 return false;
2373 }
2374
2375 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2376 // creating an offloaded track and tearing it down immediately after start when audioflinger
2377 // detects there is an active non offloadable effect.
2378 // FIXME: We should check the audio session here but we do not have it in this context.
2379 // This may prevent offloading in rare situations where effects are left active by apps
2380 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002381 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002382 return false;
2383 }
2384
2385 // See if there is a profile to support this.
2386 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002387 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002388 offloadInfo.sample_rate,
2389 offloadInfo.format,
2390 offloadInfo.channel_mask,
2391 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002392 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2393 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002394}
2395
Eric Laurent6a94d692014-05-20 11:18:06 -07002396status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2397 audio_port_type_t type,
2398 unsigned int *num_ports,
2399 struct audio_port *ports,
2400 unsigned int *generation)
2401{
2402 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2403 generation == NULL) {
2404 return BAD_VALUE;
2405 }
2406 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2407 if (ports == NULL) {
2408 *num_ports = 0;
2409 }
2410
2411 size_t portsWritten = 0;
2412 size_t portsMax = *num_ports;
2413 *num_ports = 0;
2414 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002415 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2416 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002417 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002418 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2419 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2420 continue;
2421 }
2422 if (portsWritten < portsMax) {
2423 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2424 }
2425 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002426 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002427 }
2428 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002429 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2430 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2431 continue;
2432 }
2433 if (portsWritten < portsMax) {
2434 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2435 }
2436 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002437 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002438 }
2439 }
2440 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2441 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2442 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2443 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2444 }
2445 *num_ports += mInputs.size();
2446 }
2447 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002448 size_t numOutputs = 0;
2449 for (size_t i = 0; i < mOutputs.size(); i++) {
2450 if (!mOutputs[i]->isDuplicated()) {
2451 numOutputs++;
2452 if (portsWritten < portsMax) {
2453 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2454 }
2455 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002456 }
Eric Laurent84c70242014-06-23 08:46:27 -07002457 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002458 }
2459 }
2460 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002461 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002462 return NO_ERROR;
2463}
2464
2465status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2466{
2467 return NO_ERROR;
2468}
2469
Eric Laurent6a94d692014-05-20 11:18:06 -07002470status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2471 audio_patch_handle_t *handle,
2472 uid_t uid)
2473{
2474 ALOGV("createAudioPatch()");
2475
2476 if (handle == NULL || patch == NULL) {
2477 return BAD_VALUE;
2478 }
2479 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2480
Eric Laurent874c42872014-08-08 15:13:39 -07002481 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2482 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2483 return BAD_VALUE;
2484 }
2485 // only one source per audio patch supported for now
2486 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002487 return INVALID_OPERATION;
2488 }
Eric Laurent874c42872014-08-08 15:13:39 -07002489
2490 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002491 return INVALID_OPERATION;
2492 }
Eric Laurent874c42872014-08-08 15:13:39 -07002493 for (size_t i = 0; i < patch->num_sinks; i++) {
2494 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2495 return INVALID_OPERATION;
2496 }
2497 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002498
2499 sp<AudioPatch> patchDesc;
2500 ssize_t index = mAudioPatches.indexOfKey(*handle);
2501
Eric Laurent6a94d692014-05-20 11:18:06 -07002502 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2503 patch->sources[0].role,
2504 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002505#if LOG_NDEBUG == 0
2506 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002507 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002508 patch->sinks[i].role,
2509 patch->sinks[i].type);
2510 }
2511#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002512
2513 if (index >= 0) {
2514 patchDesc = mAudioPatches.valueAt(index);
2515 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2516 mUidCached, patchDesc->mUid, uid);
2517 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2518 return INVALID_OPERATION;
2519 }
2520 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002521 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002522 }
2523
2524 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002525 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002526 if (outputDesc == NULL) {
2527 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2528 return BAD_VALUE;
2529 }
Eric Laurent84c70242014-06-23 08:46:27 -07002530 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2531 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002532 if (patchDesc != 0) {
2533 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2534 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2535 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2536 return BAD_VALUE;
2537 }
2538 }
Eric Laurent874c42872014-08-08 15:13:39 -07002539 DeviceVector devices;
2540 for (size_t i = 0; i < patch->num_sinks; i++) {
2541 // Only support mix to devices connection
2542 // TODO add support for mix to mix connection
2543 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2544 ALOGV("createAudioPatch() source mix but sink is not a device");
2545 return INVALID_OPERATION;
2546 }
2547 sp<DeviceDescriptor> devDesc =
2548 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2549 if (devDesc == 0) {
2550 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2551 return BAD_VALUE;
2552 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002553
François Gaffie53615e22015-03-19 09:24:12 +01002554 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002555 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002556 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002557 NULL, // updatedSamplingRate
2558 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002559 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002560 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002561 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002562 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002563 ALOGV("createAudioPatch() profile not supported for device %08x",
2564 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002565 return INVALID_OPERATION;
2566 }
2567 devices.add(devDesc);
2568 }
2569 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002570 return INVALID_OPERATION;
2571 }
Eric Laurent874c42872014-08-08 15:13:39 -07002572
Eric Laurent6a94d692014-05-20 11:18:06 -07002573 // TODO: reconfigure output format and channels here
2574 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002575 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002576 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002577 index = mAudioPatches.indexOfKey(*handle);
2578 if (index >= 0) {
2579 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2580 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2581 }
2582 patchDesc = mAudioPatches.valueAt(index);
2583 patchDesc->mUid = uid;
2584 ALOGV("createAudioPatch() success");
2585 } else {
2586 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2587 return INVALID_OPERATION;
2588 }
2589 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2590 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2591 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002592 // only one sink supported when connecting an input device to a mix
2593 if (patch->num_sinks > 1) {
2594 return INVALID_OPERATION;
2595 }
François Gaffie53615e22015-03-19 09:24:12 +01002596 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002597 if (inputDesc == NULL) {
2598 return BAD_VALUE;
2599 }
2600 if (patchDesc != 0) {
2601 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2602 return BAD_VALUE;
2603 }
2604 }
2605 sp<DeviceDescriptor> devDesc =
2606 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2607 if (devDesc == 0) {
2608 return BAD_VALUE;
2609 }
2610
François Gaffie53615e22015-03-19 09:24:12 +01002611 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002612 devDesc->mAddress,
2613 patch->sinks[0].sample_rate,
2614 NULL, /*updatedSampleRate*/
2615 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002616 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002617 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002618 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002619 // FIXME for the parameter type,
2620 // and the NONE
2621 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002622 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002623 return INVALID_OPERATION;
2624 }
2625 // TODO: reconfigure output format and channels here
2626 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002627 devDesc->type(), inputDesc->mIoHandle);
2628 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002629 index = mAudioPatches.indexOfKey(*handle);
2630 if (index >= 0) {
2631 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2632 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2633 }
2634 patchDesc = mAudioPatches.valueAt(index);
2635 patchDesc->mUid = uid;
2636 ALOGV("createAudioPatch() success");
2637 } else {
2638 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2639 return INVALID_OPERATION;
2640 }
2641 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2642 // device to device connection
2643 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002644 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002645 return BAD_VALUE;
2646 }
2647 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002648 sp<DeviceDescriptor> srcDeviceDesc =
2649 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002650 if (srcDeviceDesc == 0) {
2651 return BAD_VALUE;
2652 }
Eric Laurent874c42872014-08-08 15:13:39 -07002653
Eric Laurent6a94d692014-05-20 11:18:06 -07002654 //update source and sink with our own data as the data passed in the patch may
2655 // be incomplete.
2656 struct audio_patch newPatch = *patch;
2657 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002658
Eric Laurent874c42872014-08-08 15:13:39 -07002659 for (size_t i = 0; i < patch->num_sinks; i++) {
2660 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2661 ALOGV("createAudioPatch() source device but one sink is not a device");
2662 return INVALID_OPERATION;
2663 }
2664
2665 sp<DeviceDescriptor> sinkDeviceDesc =
2666 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2667 if (sinkDeviceDesc == 0) {
2668 return BAD_VALUE;
2669 }
2670 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2671
Eric Laurent3bcf8592015-04-03 12:13:24 -07002672 // create a software bridge in PatchPanel if:
2673 // - source and sink devices are on differnt HW modules OR
2674 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002675 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002676 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002677 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002678 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002679 return INVALID_OPERATION;
2680 }
Eric Laurent874c42872014-08-08 15:13:39 -07002681 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002682 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002683 // if the sink device is reachable via an opened output stream, request to go via
2684 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002685 audio_io_handle_t output = selectOutput(outputs,
2686 AUDIO_OUTPUT_FLAG_NONE,
2687 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002688 if (output != AUDIO_IO_HANDLE_NONE) {
2689 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2690 if (outputDesc->isDuplicated()) {
2691 return INVALID_OPERATION;
2692 }
2693 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002694 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002695 newPatch.num_sources = 2;
2696 }
Eric Laurent83b88082014-06-20 18:31:16 -07002697 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002698 }
2699 // TODO: check from routing capabilities in config file and other conflicting patches
2700
2701 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2702 if (index >= 0) {
2703 afPatchHandle = patchDesc->mAfPatchHandle;
2704 }
2705
2706 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2707 &afPatchHandle,
2708 0);
2709 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2710 status, afPatchHandle);
2711 if (status == NO_ERROR) {
2712 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002713 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002714 addAudioPatch(patchDesc->mHandle, patchDesc);
2715 } else {
2716 patchDesc->mPatch = newPatch;
2717 }
2718 patchDesc->mAfPatchHandle = afPatchHandle;
2719 *handle = patchDesc->mHandle;
2720 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002721 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002722 } else {
2723 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2724 status);
2725 return INVALID_OPERATION;
2726 }
2727 } else {
2728 return BAD_VALUE;
2729 }
2730 } else {
2731 return BAD_VALUE;
2732 }
2733 return NO_ERROR;
2734}
2735
2736status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2737 uid_t uid)
2738{
2739 ALOGV("releaseAudioPatch() patch %d", handle);
2740
2741 ssize_t index = mAudioPatches.indexOfKey(handle);
2742
2743 if (index < 0) {
2744 return BAD_VALUE;
2745 }
2746 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2747 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2748 mUidCached, patchDesc->mUid, uid);
2749 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2750 return INVALID_OPERATION;
2751 }
2752
2753 struct audio_patch *patch = &patchDesc->mPatch;
2754 patchDesc->mUid = mUidCached;
2755 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002756 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002757 if (outputDesc == NULL) {
2758 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2759 return BAD_VALUE;
2760 }
2761
Eric Laurentc75307b2015-03-17 15:29:32 -07002762 setOutputDevice(outputDesc,
2763 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002764 true,
2765 0,
2766 NULL);
2767 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2768 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002769 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002770 if (inputDesc == NULL) {
2771 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2772 return BAD_VALUE;
2773 }
2774 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002775 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002776 true,
2777 NULL);
2778 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2779 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2780 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2781 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2782 status, patchDesc->mAfPatchHandle);
2783 removeAudioPatch(patchDesc->mHandle);
2784 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002785 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002786 } else {
2787 return BAD_VALUE;
2788 }
2789 } else {
2790 return BAD_VALUE;
2791 }
2792 return NO_ERROR;
2793}
2794
2795status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2796 struct audio_patch *patches,
2797 unsigned int *generation)
2798{
François Gaffie53615e22015-03-19 09:24:12 +01002799 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002800 return BAD_VALUE;
2801 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002802 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002803 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002804}
2805
Eric Laurente1715a42014-05-20 11:30:42 -07002806status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002807{
Eric Laurente1715a42014-05-20 11:30:42 -07002808 ALOGV("setAudioPortConfig()");
2809
2810 if (config == NULL) {
2811 return BAD_VALUE;
2812 }
2813 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2814 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002815 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2816 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002817 }
2818
Eric Laurenta121f902014-06-03 13:32:54 -07002819 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002820 if (config->type == AUDIO_PORT_TYPE_MIX) {
2821 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002822 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002823 if (outputDesc == NULL) {
2824 return BAD_VALUE;
2825 }
Eric Laurent84c70242014-06-23 08:46:27 -07002826 ALOG_ASSERT(!outputDesc->isDuplicated(),
2827 "setAudioPortConfig() called on duplicated output %d",
2828 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002829 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002830 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002831 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002832 if (inputDesc == NULL) {
2833 return BAD_VALUE;
2834 }
Eric Laurenta121f902014-06-03 13:32:54 -07002835 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002836 } else {
2837 return BAD_VALUE;
2838 }
2839 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2840 sp<DeviceDescriptor> deviceDesc;
2841 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2842 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2843 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2844 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2845 } else {
2846 return BAD_VALUE;
2847 }
2848 if (deviceDesc == NULL) {
2849 return BAD_VALUE;
2850 }
Eric Laurenta121f902014-06-03 13:32:54 -07002851 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002852 } else {
2853 return BAD_VALUE;
2854 }
2855
Eric Laurenta121f902014-06-03 13:32:54 -07002856 struct audio_port_config backupConfig;
2857 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2858 if (status == NO_ERROR) {
2859 struct audio_port_config newConfig;
2860 audioPortConfig->toAudioPortConfig(&newConfig, config);
2861 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002862 }
Eric Laurenta121f902014-06-03 13:32:54 -07002863 if (status != NO_ERROR) {
2864 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002865 }
Eric Laurente1715a42014-05-20 11:30:42 -07002866
2867 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002868}
2869
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002870void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2871{
Eric Laurentd60560a2015-04-10 11:31:20 -07002872 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002873 clearAudioPatches(uid);
2874 clearSessionRoutes(uid);
2875}
2876
Eric Laurent6a94d692014-05-20 11:18:06 -07002877void AudioPolicyManager::clearAudioPatches(uid_t uid)
2878{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002879 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002880 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2881 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002882 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002883 }
2884 }
2885}
2886
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002887void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2888 audio_io_handle_t ouptutToSkip)
2889{
2890 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2891 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2892 for (size_t j = 0; j < mOutputs.size(); j++) {
2893 if (mOutputs.keyAt(j) == ouptutToSkip) {
2894 continue;
2895 }
2896 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2897 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2898 continue;
2899 }
2900 // If the default device for this strategy is on another output mix,
2901 // invalidate all tracks in this strategy to force re connection.
2902 // Otherwise select new device on the output mix.
2903 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002904 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002905 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2906 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2907 }
2908 }
2909 } else {
2910 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2911 setOutputDevice(outputDesc, newDevice, false);
2912 }
2913 }
2914}
2915
2916void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2917{
2918 // remove output routes associated with this uid
2919 SortedVector<routing_strategy> affectedStrategies;
2920 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2921 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2922 if (route->mUid == uid) {
2923 mOutputRoutes.removeItemsAt(i);
2924 if (route->mDeviceDescriptor != 0) {
2925 affectedStrategies.add(getStrategy(route->mStreamType));
2926 }
2927 }
2928 }
2929 // reroute outputs if necessary
2930 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2931 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2932 }
2933
2934 // remove input routes associated with this uid
2935 SortedVector<audio_source_t> affectedSources;
2936 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2937 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2938 if (route->mUid == uid) {
2939 mInputRoutes.removeItemsAt(i);
2940 if (route->mDeviceDescriptor != 0) {
2941 affectedSources.add(route->mSource);
2942 }
2943 }
2944 }
2945 // reroute inputs if necessary
2946 SortedVector<audio_io_handle_t> inputsToClose;
2947 for (size_t i = 0; i < mInputs.size(); i++) {
2948 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002949 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002950 inputsToClose.add(inputDesc->mIoHandle);
2951 }
2952 }
2953 for (size_t i = 0; i < inputsToClose.size(); i++) {
2954 closeInput(inputsToClose[i]);
2955 }
2956}
2957
Eric Laurentd60560a2015-04-10 11:31:20 -07002958void AudioPolicyManager::clearAudioSources(uid_t uid)
2959{
2960 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2961 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2962 if (sourceDesc->mUid == uid) {
2963 stopAudioSource(mAudioSources.keyAt(i));
2964 }
2965 }
2966}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002967
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002968status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2969 audio_io_handle_t *ioHandle,
2970 audio_devices_t *device)
2971{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08002972 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2973 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002974 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002975
François Gaffiedf372692015-03-19 10:43:27 +01002976 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002977}
2978
Eric Laurentd60560a2015-04-10 11:31:20 -07002979status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2980 const audio_attributes_t *attributes,
2981 audio_io_handle_t *handle,
2982 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002983{
Eric Laurentd60560a2015-04-10 11:31:20 -07002984 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2985 if (source == NULL || attributes == NULL || handle == NULL) {
2986 return BAD_VALUE;
2987 }
2988
2989 *handle = AUDIO_IO_HANDLE_NONE;
2990
2991 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2992 source->type != AUDIO_PORT_TYPE_DEVICE) {
2993 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2994 return INVALID_OPERATION;
2995 }
2996
2997 sp<DeviceDescriptor> srcDeviceDesc =
2998 mAvailableInputDevices.getDevice(source->ext.device.type,
2999 String8(source->ext.device.address));
3000 if (srcDeviceDesc == 0) {
3001 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3002 return BAD_VALUE;
3003 }
3004 sp<AudioSourceDescriptor> sourceDesc =
3005 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3006
3007 struct audio_patch dummyPatch;
3008 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3009 sourceDesc->mPatchDesc = patchDesc;
3010
3011 status_t status = connectAudioSource(sourceDesc);
3012 if (status == NO_ERROR) {
3013 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3014 *handle = sourceDesc->getHandle();
3015 }
3016 return status;
3017}
3018
3019status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3020{
3021 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3022
3023 // make sure we only have one patch per source.
3024 disconnectAudioSource(sourceDesc);
3025
3026 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003027 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003028 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3029
3030 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3031 sp<DeviceDescriptor> sinkDeviceDesc =
3032 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3033
3034 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3035 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3036
3037 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3038 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003039 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003040 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3041 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3042 // create patch between src device and output device
3043 // create Hwoutput and add to mHwOutputs
3044 } else {
3045 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3046 audio_io_handle_t output =
3047 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3048 if (output == AUDIO_IO_HANDLE_NONE) {
3049 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3050 return INVALID_OPERATION;
3051 }
3052 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3053 if (outputDesc->isDuplicated()) {
3054 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3055 return INVALID_OPERATION;
3056 }
3057 // create a special patch with no sink and two sources:
3058 // - the second source indicates to PatchPanel through which output mix this patch should
3059 // be connected as well as the stream type for volume control
3060 // - the sink is defined by whatever output device is currently selected for the output
3061 // though which this patch is routed.
3062 patch->num_sinks = 0;
3063 patch->num_sources = 2;
3064 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3065 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3066 patch->sources[1].ext.mix.usecase.stream = stream;
3067 status_t status = mpClientInterface->createAudioPatch(patch,
3068 &afPatchHandle,
3069 0);
3070 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3071 status, afPatchHandle);
3072 if (status != NO_ERROR) {
3073 ALOGW("%s patch panel could not connect device patch, error %d",
3074 __FUNCTION__, status);
3075 return INVALID_OPERATION;
3076 }
3077 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003078 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003079
3080 if (status != NO_ERROR) {
3081 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3082 return status;
3083 }
3084 sourceDesc->mSwOutput = outputDesc;
3085 if (delayMs != 0) {
3086 usleep(delayMs * 1000);
3087 }
3088 }
3089
3090 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3091 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3092
3093 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003094}
3095
Eric Laurent493404d2015-04-21 15:07:36 -07003096status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003097{
Eric Laurentd60560a2015-04-10 11:31:20 -07003098 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3099 ALOGV("%s handle %d", __FUNCTION__, handle);
3100 if (sourceDesc == 0) {
3101 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3102 return BAD_VALUE;
3103 }
3104 status_t status = disconnectAudioSource(sourceDesc);
3105
3106 mAudioSources.removeItem(handle);
3107 return status;
3108}
3109
Andy Hung2ddee192015-12-18 17:34:44 -08003110status_t AudioPolicyManager::setMasterMono(bool mono)
3111{
3112 if (mMasterMono == mono) {
3113 return NO_ERROR;
3114 }
3115 mMasterMono = mono;
3116 // if enabling mono we close all offloaded devices, which will invalidate the
3117 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3118 // for recreating the new AudioTrack as non-offloaded PCM.
3119 //
3120 // If disabling mono, we leave all tracks as is: we don't know which clients
3121 // and tracks are able to be recreated as offloaded. The next "song" should
3122 // play back offloaded.
3123 if (mMasterMono) {
3124 Vector<audio_io_handle_t> offloaded;
3125 for (size_t i = 0; i < mOutputs.size(); ++i) {
3126 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3127 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3128 offloaded.push(desc->mIoHandle);
3129 }
3130 }
3131 for (size_t i = 0; i < offloaded.size(); ++i) {
3132 closeOutput(offloaded[i]);
3133 }
3134 }
3135 // update master mono for all remaining outputs
3136 for (size_t i = 0; i < mOutputs.size(); ++i) {
3137 updateMono(mOutputs.keyAt(i));
3138 }
3139 return NO_ERROR;
3140}
3141
3142status_t AudioPolicyManager::getMasterMono(bool *mono)
3143{
3144 *mono = mMasterMono;
3145 return NO_ERROR;
3146}
3147
Eric Laurentd60560a2015-04-10 11:31:20 -07003148status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3149{
3150 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3151
3152 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3153 if (patchDesc == 0) {
3154 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3155 sourceDesc->mPatchDesc->mHandle);
3156 return BAD_VALUE;
3157 }
3158 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3159
Eric Laurent28d09f02016-03-08 10:43:05 -08003160 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003161 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3162 if (swOutputDesc != 0) {
3163 stopSource(swOutputDesc, stream, false);
3164 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3165 } else {
3166 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3167 if (hwOutputDesc != 0) {
3168 // release patch between src device and output device
3169 // close Hwoutput and remove from mHwOutputs
3170 } else {
3171 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3172 }
3173 }
3174 return NO_ERROR;
3175}
3176
3177sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3178 audio_io_handle_t output, routing_strategy strategy)
3179{
3180 sp<AudioSourceDescriptor> source;
3181 for (size_t i = 0; i < mAudioSources.size(); i++) {
3182 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3183 routing_strategy sourceStrategy =
3184 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3185 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3186 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3187 source = sourceDesc;
3188 break;
3189 }
3190 }
3191 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003192}
3193
Eric Laurente552edb2014-03-10 17:42:56 -07003194// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003195// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003196// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003197uint32_t AudioPolicyManager::nextAudioPortGeneration()
3198{
3199 return android_atomic_inc(&mAudioPortGeneration);
3200}
3201
Eric Laurente0720872014-03-11 09:30:41 -07003202AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003203 :
3204#ifdef AUDIO_POLICY_TEST
3205 Thread(false),
3206#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003207 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003208 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003209 mAudioPortGeneration(1),
3210 mBeaconMuteRefCount(0),
3211 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003212 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003213 mTtsOutputAvailable(false),
3214 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003215{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003216 mUidCached = getuid();
3217 mpClientInterface = clientInterface;
3218
3219 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3220 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3221 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3222 bool speakerDrcEnabled = false;
3223
3224#ifdef USE_XML_AUDIO_POLICY_CONF
3225 mVolumeCurves = new VolumeCurvesCollection();
3226 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3227 mDefaultOutputDevice, speakerDrcEnabled,
3228 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3229 PolicySerializer serializer;
3230 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3231#else
3232 mVolumeCurves = new StreamDescriptorCollection();
3233 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3234 mDefaultOutputDevice, speakerDrcEnabled);
3235 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3236 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3237#endif
3238 ALOGE("could not load audio policy configuration file, setting defaults");
3239 config.setDefault();
3240 }
3241 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3242 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3243
3244 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003245 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3246 if (!engineInstance) {
3247 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3248 return;
3249 }
3250 // Retrieve the Policy Manager Interface
3251 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3252 if (mEngine == NULL) {
3253 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3254 return;
3255 }
3256 mEngine->setObserver(this);
3257 status_t status = mEngine->initCheck();
3258 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3259
Eric Laurent3a4311c2014-03-17 12:00:47 -07003260 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003261 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003262 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3263 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003264 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003265 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003266 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003267 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003268 continue;
3269 }
3270 // open all output streams needed to access attached devices
3271 // except for direct output streams that are only opened when they are actually
3272 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003273 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003274 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3275 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003276 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003277
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003278 if (!outProfile->hasSupportedDevices()) {
3279 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003280 continue;
3281 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003282 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003283 mTtsOutputAvailable = true;
3284 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003285
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003286 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003287 continue;
3288 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003289 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003290 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3291 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003292 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003293 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003294 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003295 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003296 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003297 if ((profileType & outputDeviceTypes) == 0) {
3298 continue;
3299 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003300 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3301 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003302 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3303 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3304 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3305 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003306
3307 outputDesc->mDevice = profileType;
3308 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3309 config.sample_rate = outputDesc->mSamplingRate;
3310 config.channel_mask = outputDesc->mChannelMask;
3311 config.format = outputDesc->mFormat;
3312 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003313 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003314 &output,
3315 &config,
3316 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003317 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003318 &outputDesc->mLatency,
3319 outputDesc->mFlags);
3320
3321 if (status != NO_ERROR) {
3322 ALOGW("Cannot open output stream for device %08x on hw module %s",
3323 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003324 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003325 } else {
3326 outputDesc->mSamplingRate = config.sample_rate;
3327 outputDesc->mChannelMask = config.channel_mask;
3328 outputDesc->mFormat = config.format;
3329
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003330 for (size_t k = 0; k < supportedDevices.size(); k++) {
3331 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003332 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003333 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3334 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003335 }
3336 }
3337 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003338 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003339 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003340 }
3341 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003342 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003343 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003344 true,
3345 0,
3346 NULL,
3347 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003348 }
Eric Laurente552edb2014-03-10 17:42:56 -07003349 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003350 // open input streams needed to access attached devices to validate
3351 // mAvailableInputDevices list
3352 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3353 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003354 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003355
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003356 if (!inProfile->hasSupportedDevices()) {
3357 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003358 continue;
3359 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003360 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003361 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003362 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3363
Eric Laurentd78f1532014-09-16 16:38:20 -07003364 if ((profileType & inputDeviceTypes) == 0) {
3365 continue;
3366 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003367 sp<AudioInputDescriptor> inputDesc =
3368 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003369
Eric Laurentd78f1532014-09-16 16:38:20 -07003370 inputDesc->mDevice = profileType;
3371
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003372 // find the address
3373 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3374 // the inputs vector must be of size 1, but we don't want to crash here
3375 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3376 : String8("");
3377 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3378 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3379
Eric Laurentd78f1532014-09-16 16:38:20 -07003380 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3381 config.sample_rate = inputDesc->mSamplingRate;
3382 config.channel_mask = inputDesc->mChannelMask;
3383 config.format = inputDesc->mFormat;
3384 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003385 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003386 &input,
3387 &config,
3388 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003389 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003390 AUDIO_SOURCE_MIC,
3391 AUDIO_INPUT_FLAG_NONE);
3392
3393 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003394 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3395 for (size_t k = 0; k < supportedDevices.size(); k++) {
3396 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003397 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003398 if (index >= 0) {
3399 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3400 if (!devDesc->isAttached()) {
3401 devDesc->attach(mHwModules[i]);
3402 devDesc->importAudioPort(inProfile);
3403 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003404 }
3405 }
3406 mpClientInterface->closeInput(input);
3407 } else {
3408 ALOGW("Cannot open input stream for device %08x on hw module %s",
3409 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003410 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003411 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003412 }
3413 }
3414 // make sure all attached devices have been allocated a unique ID
3415 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003416 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003417 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003418 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3419 continue;
3420 }
François Gaffie2110e042015-03-24 08:41:51 +01003421 // The device is now validated and can be appended to the available devices of the engine
3422 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3423 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003424 i++;
3425 }
3426 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003427 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003428 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003429 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3430 continue;
3431 }
François Gaffie2110e042015-03-24 08:41:51 +01003432 // The device is now validated and can be appended to the available devices of the engine
3433 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3434 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003435 i++;
3436 }
3437 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003438 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003439 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003440 }
Eric Laurente552edb2014-03-10 17:42:56 -07003441
3442 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3443
3444 updateDevicesAndOutputs();
3445
3446#ifdef AUDIO_POLICY_TEST
3447 if (mPrimaryOutput != 0) {
3448 AudioParameter outputCmd = AudioParameter();
3449 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003450 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003451
3452 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3453 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003454 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3455 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003456 mTestLatencyMs = 0;
3457 mCurOutput = 0;
3458 mDirectOutput = false;
3459 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3460 mTestOutputs[i] = 0;
3461 }
3462
3463 const size_t SIZE = 256;
3464 char buffer[SIZE];
3465 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3466 run(buffer, ANDROID_PRIORITY_AUDIO);
3467 }
3468#endif //AUDIO_POLICY_TEST
3469}
3470
Eric Laurente0720872014-03-11 09:30:41 -07003471AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003472{
3473#ifdef AUDIO_POLICY_TEST
3474 exit();
3475#endif //AUDIO_POLICY_TEST
3476 for (size_t i = 0; i < mOutputs.size(); i++) {
3477 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003478 }
3479 for (size_t i = 0; i < mInputs.size(); i++) {
3480 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003481 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003482 mAvailableOutputDevices.clear();
3483 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003484 mOutputs.clear();
3485 mInputs.clear();
3486 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003487}
3488
Eric Laurente0720872014-03-11 09:30:41 -07003489status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003490{
Eric Laurent87ffa392015-05-22 10:32:38 -07003491 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003492}
3493
3494#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003495bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003496{
3497 ALOGV("entering threadLoop()");
3498 while (!exitPending())
3499 {
3500 String8 command;
3501 int valueInt;
3502 String8 value;
3503
3504 Mutex::Autolock _l(mLock);
3505 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3506
3507 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3508 AudioParameter param = AudioParameter(command);
3509
3510 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3511 valueInt != 0) {
3512 ALOGV("Test command %s received", command.string());
3513 String8 target;
3514 if (param.get(String8("target"), target) != NO_ERROR) {
3515 target = "Manager";
3516 }
3517 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3518 param.remove(String8("test_cmd_policy_output"));
3519 mCurOutput = valueInt;
3520 }
3521 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3522 param.remove(String8("test_cmd_policy_direct"));
3523 if (value == "false") {
3524 mDirectOutput = false;
3525 } else if (value == "true") {
3526 mDirectOutput = true;
3527 }
3528 }
3529 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3530 param.remove(String8("test_cmd_policy_input"));
3531 mTestInput = valueInt;
3532 }
3533
3534 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3535 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003536 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003537 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003538 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003539 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003540 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003541 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003542 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003543 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003544 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003545 if (target == "Manager") {
3546 mTestFormat = format;
3547 } else if (mTestOutputs[mCurOutput] != 0) {
3548 AudioParameter outputParam = AudioParameter();
3549 outputParam.addInt(String8("format"), format);
3550 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3551 }
3552 }
3553 }
3554 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3555 param.remove(String8("test_cmd_policy_channels"));
3556 int channels = 0;
3557
3558 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003559 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003560 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003561 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003562 }
3563 if (channels != 0) {
3564 if (target == "Manager") {
3565 mTestChannels = channels;
3566 } else if (mTestOutputs[mCurOutput] != 0) {
3567 AudioParameter outputParam = AudioParameter();
3568 outputParam.addInt(String8("channels"), channels);
3569 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3570 }
3571 }
3572 }
3573 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3574 param.remove(String8("test_cmd_policy_sampleRate"));
3575 if (valueInt >= 0 && valueInt <= 96000) {
3576 int samplingRate = valueInt;
3577 if (target == "Manager") {
3578 mTestSamplingRate = samplingRate;
3579 } else if (mTestOutputs[mCurOutput] != 0) {
3580 AudioParameter outputParam = AudioParameter();
3581 outputParam.addInt(String8("sampling_rate"), samplingRate);
3582 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3583 }
3584 }
3585 }
3586
3587 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3588 param.remove(String8("test_cmd_policy_reopen"));
3589
Eric Laurentc75307b2015-03-17 15:29:32 -07003590 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003591
Eric Laurentc75307b2015-03-17 15:29:32 -07003592 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003593
Eric Laurentc75307b2015-03-17 15:29:32 -07003594 removeOutput(mPrimaryOutput->mIoHandle);
3595 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3596 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003597 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003598 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3599 config.sample_rate = outputDesc->mSamplingRate;
3600 config.channel_mask = outputDesc->mChannelMask;
3601 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003602 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003603 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003604 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003605 &config,
3606 &outputDesc->mDevice,
3607 String8(""),
3608 &outputDesc->mLatency,
3609 outputDesc->mFlags);
3610 if (status != NO_ERROR) {
3611 ALOGE("Failed to reopen hardware output stream, "
3612 "samplingRate: %d, format %d, channels %d",
3613 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003614 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003615 outputDesc->mSamplingRate = config.sample_rate;
3616 outputDesc->mChannelMask = config.channel_mask;
3617 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003618 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003619 AudioParameter outputCmd = AudioParameter();
3620 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003621 mpClientInterface->setParameters(handle, outputCmd.toString());
3622 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003623 }
3624 }
3625
3626
3627 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3628 }
3629 }
3630 return false;
3631}
3632
Eric Laurente0720872014-03-11 09:30:41 -07003633void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003634{
3635 {
3636 AutoMutex _l(mLock);
3637 requestExit();
3638 mWaitWorkCV.signal();
3639 }
3640 requestExitAndWait();
3641}
3642
Eric Laurente0720872014-03-11 09:30:41 -07003643int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003644{
3645 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3646 if (output == mTestOutputs[i]) return i;
3647 }
3648 return 0;
3649}
3650#endif //AUDIO_POLICY_TEST
3651
3652// ---
3653
Eric Laurentc75307b2015-03-17 15:29:32 -07003654void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003655{
François Gaffie98cc1912015-03-18 17:52:40 +01003656 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003657 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003658 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003659 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003660}
3661
François Gaffie53615e22015-03-19 09:24:12 +01003662void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3663{
3664 mOutputs.removeItem(output);
3665}
3666
Eric Laurent1f2f2232014-06-02 12:01:23 -07003667void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003668{
François Gaffie98cc1912015-03-18 17:52:40 +01003669 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003670 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003671 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003672}
Eric Laurente552edb2014-03-10 17:42:56 -07003673
Eric Laurentc75307b2015-03-17 15:29:32 -07003674void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003675 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003676 const String8 address /*in*/,
3677 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003678 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003679 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003680 if (devDesc != 0) {
3681 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3682 desc->mIoHandle, address.string());
3683 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003684 }
3685}
3686
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003687status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003688 audio_policy_dev_state_t state,
3689 SortedVector<audio_io_handle_t>& outputs,
3690 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003691{
François Gaffie53615e22015-03-19 09:24:12 +01003692 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003693 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003694
3695 if (audio_device_is_digital(device)) {
3696 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003697 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003698 }
Eric Laurente552edb2014-03-10 17:42:56 -07003699
Eric Laurent3b73df72014-03-11 09:06:29 -07003700 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003701 // first list already open outputs that can be routed to this device
3702 for (size_t i = 0; i < mOutputs.size(); i++) {
3703 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003704 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003705 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003706 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3707 outputs.add(mOutputs.keyAt(i));
3708 } else {
3709 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003710 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003711 }
Eric Laurente552edb2014-03-10 17:42:56 -07003712 }
3713 }
3714 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003715 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003716 for (size_t i = 0; i < mHwModules.size(); i++)
3717 {
3718 if (mHwModules[i]->mHandle == 0) {
3719 continue;
3720 }
3721 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3722 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003723 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003724 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003725 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003726 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003727 profiles.add(profile);
3728 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3729 }
Eric Laurente552edb2014-03-10 17:42:56 -07003730 }
3731 }
3732 }
3733
Eric Laurent7b279bb2015-12-14 10:18:23 -08003734 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003735
Eric Laurente552edb2014-03-10 17:42:56 -07003736 if (profiles.isEmpty() && outputs.isEmpty()) {
3737 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3738 return BAD_VALUE;
3739 }
3740
3741 // open outputs for matching profiles if needed. Direct outputs are also opened to
3742 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3743 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003744 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003745
3746 // nothing to do if one output is already opened for this profile
3747 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003748 for (j = 0; j < outputs.size(); j++) {
3749 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003750 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003751 // matching profile: save the sample rates, format and channel masks supported
3752 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003753 if (audio_device_is_digital(device)) {
3754 devDesc->importAudioPort(profile);
3755 }
Eric Laurente552edb2014-03-10 17:42:56 -07003756 break;
3757 }
3758 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003759 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003760 continue;
3761 }
3762
Eric Laurent83b88082014-06-20 18:31:16 -07003763 ALOGV("opening output for device %08x with params %s profile %p",
3764 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003765 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003766 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003767 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3768 config.sample_rate = desc->mSamplingRate;
3769 config.channel_mask = desc->mChannelMask;
3770 config.format = desc->mFormat;
3771 config.offload_info.sample_rate = desc->mSamplingRate;
3772 config.offload_info.channel_mask = desc->mChannelMask;
3773 config.offload_info.format = desc->mFormat;
3774 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003775 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003776 &output,
3777 &config,
3778 &desc->mDevice,
3779 address,
3780 &desc->mLatency,
3781 desc->mFlags);
3782 if (status == NO_ERROR) {
3783 desc->mSamplingRate = config.sample_rate;
3784 desc->mChannelMask = config.channel_mask;
3785 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003786
Eric Laurentd4692962014-05-05 18:13:44 -07003787 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003788 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003789 char *param = audio_device_address_to_parameter(device, address);
3790 mpClientInterface->setParameters(output, String8(param));
3791 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003792 }
Phil Burk00eeb322016-03-31 12:41:00 -07003793 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003794 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003795 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003796 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003797 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003798 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003799 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003800 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003801 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003802 config.offload_info.sample_rate = config.sample_rate;
3803 config.offload_info.channel_mask = config.channel_mask;
3804 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003805 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003806 &output,
3807 &config,
3808 &desc->mDevice,
3809 address,
3810 &desc->mLatency,
3811 desc->mFlags);
3812 if (status == NO_ERROR) {
3813 desc->mSamplingRate = config.sample_rate;
3814 desc->mChannelMask = config.channel_mask;
3815 desc->mFormat = config.format;
3816 } else {
3817 output = AUDIO_IO_HANDLE_NONE;
3818 }
Eric Laurentd4692962014-05-05 18:13:44 -07003819 }
3820
Eric Laurentcf2c0212014-07-25 16:20:43 -07003821 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003822 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003823 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003824 sp<AudioPolicyMix> policyMix;
3825 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003826 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3827 address.string());
3828 }
François Gaffie036e1e92015-03-19 10:16:24 +01003829 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003830 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003831
Eric Laurent87ffa392015-05-22 10:32:38 -07003832 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3833 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003834 // no duplicated output for direct outputs and
3835 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003836 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003837
Eric Laurentd4692962014-05-05 18:13:44 -07003838 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003839 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003840
Eric Laurentd4692962014-05-05 18:13:44 -07003841 //TODO: configure audio effect output stage here
3842
3843 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003844 duplicatedOutput =
3845 mpClientInterface->openDuplicateOutput(output,
3846 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003847 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003848 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003849 sp<SwAudioOutputDescriptor> dupOutputDesc =
3850 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3851 dupOutputDesc->mOutput1 = mPrimaryOutput;
3852 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003853 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3854 dupOutputDesc->mFormat = desc->mFormat;
3855 dupOutputDesc->mChannelMask = desc->mChannelMask;
3856 dupOutputDesc->mLatency = desc->mLatency;
3857 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003858 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003859 } else {
3860 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003861 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003862 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003863 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003864 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003865 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003866 }
Eric Laurente552edb2014-03-10 17:42:56 -07003867 }
3868 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003869 } else {
3870 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003871 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003872 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003873 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003874 profiles.removeAt(profile_index);
3875 profile_index--;
3876 } else {
3877 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003878 // Load digital format info only for digital devices
3879 if (audio_device_is_digital(device)) {
3880 devDesc->importAudioPort(profile);
3881 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003882
François Gaffie53615e22015-03-19 09:24:12 +01003883 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003884 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3885 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003886 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003887 NULL/*patch handle*/, address.string());
3888 }
Eric Laurente552edb2014-03-10 17:42:56 -07003889 ALOGV("checkOutputsForDevice(): adding output %d", output);
3890 }
3891 }
3892
3893 if (profiles.isEmpty()) {
3894 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3895 return BAD_VALUE;
3896 }
Eric Laurentd4692962014-05-05 18:13:44 -07003897 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003898 // check if one opened output is not needed any more after disconnecting one device
3899 for (size_t i = 0; i < mOutputs.size(); i++) {
3900 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003901 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003902 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003903 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003904 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003905 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003906 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003907 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3908 mOutputs.keyAt(i));
3909 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003910 }
Eric Laurente552edb2014-03-10 17:42:56 -07003911 }
3912 }
Eric Laurentd4692962014-05-05 18:13:44 -07003913 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003914 for (size_t i = 0; i < mHwModules.size(); i++)
3915 {
3916 if (mHwModules[i]->mHandle == 0) {
3917 continue;
3918 }
3919 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3920 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003921 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003922 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003923 ALOGV("checkOutputsForDevice(): "
3924 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003925 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003926 }
3927 }
3928 }
3929 }
3930 return NO_ERROR;
3931}
3932
Paul McLean9080a4c2015-06-18 08:24:02 -07003933status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003934 audio_policy_dev_state_t state,
3935 SortedVector<audio_io_handle_t>& inputs,
3936 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003937{
Paul McLean9080a4c2015-06-18 08:24:02 -07003938 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003939 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003940
3941 if (audio_device_is_digital(device)) {
3942 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003943 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003944 }
3945
Eric Laurentd4692962014-05-05 18:13:44 -07003946 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3947 // first list already open inputs that can be routed to this device
3948 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3949 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003950 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003951 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3952 inputs.add(mInputs.keyAt(input_index));
3953 }
3954 }
3955
3956 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003957 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003958 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3959 {
3960 if (mHwModules[module_idx]->mHandle == 0) {
3961 continue;
3962 }
3963 for (size_t profile_index = 0;
3964 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3965 profile_index++)
3966 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003967 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3968
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003969 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003970 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003971 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003972 profiles.add(profile);
3973 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3974 profile_index, module_idx);
3975 }
Eric Laurentd4692962014-05-05 18:13:44 -07003976 }
3977 }
3978 }
3979
3980 if (profiles.isEmpty() && inputs.isEmpty()) {
3981 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3982 return BAD_VALUE;
3983 }
3984
3985 // open inputs for matching profiles if needed. Direct inputs are also opened to
3986 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3987 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3988
Eric Laurent1c333e22014-05-20 10:48:17 -07003989 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003990 // nothing to do if one input is already opened for this profile
3991 size_t input_index;
3992 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3993 desc = mInputs.valueAt(input_index);
3994 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003995 if (audio_device_is_digital(device)) {
3996 devDesc->importAudioPort(profile);
3997 }
Eric Laurentd4692962014-05-05 18:13:44 -07003998 break;
3999 }
4000 }
4001 if (input_index != mInputs.size()) {
4002 continue;
4003 }
4004
4005 ALOGV("opening input for device 0x%X with params %s", device, address.string());
4006 desc = new AudioInputDescriptor(profile);
4007 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07004008 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
4009 config.sample_rate = desc->mSamplingRate;
4010 config.channel_mask = desc->mChannelMask;
4011 config.format = desc->mFormat;
4012 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07004013 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07004014 &input,
4015 &config,
4016 &desc->mDevice,
4017 address,
4018 AUDIO_SOURCE_MIC,
4019 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07004020
Eric Laurentcf2c0212014-07-25 16:20:43 -07004021 if (status == NO_ERROR) {
4022 desc->mSamplingRate = config.sample_rate;
4023 desc->mChannelMask = config.channel_mask;
4024 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004025
Eric Laurentd4692962014-05-05 18:13:44 -07004026 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004027 char *param = audio_device_address_to_parameter(device, address);
4028 mpClientInterface->setParameters(input, String8(param));
4029 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004030 }
Phil Burk00eeb322016-03-31 12:41:00 -07004031 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004032 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004033 ALOGW("checkInputsForDevice() direct input missing param");
4034 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004035 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004036 }
4037
4038 if (input != 0) {
4039 addInput(input, desc);
4040 }
4041 } // endif input != 0
4042
Eric Laurentcf2c0212014-07-25 16:20:43 -07004043 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004044 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004045 profiles.removeAt(profile_index);
4046 profile_index--;
4047 } else {
4048 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004049 if (audio_device_is_digital(device)) {
4050 devDesc->importAudioPort(profile);
4051 }
Eric Laurentd4692962014-05-05 18:13:44 -07004052 ALOGV("checkInputsForDevice(): adding input %d", input);
4053 }
4054 } // end scan profiles
4055
4056 if (profiles.isEmpty()) {
4057 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4058 return BAD_VALUE;
4059 }
4060 } else {
4061 // Disconnect
4062 // check if one opened input is not needed any more after disconnecting one device
4063 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4064 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004065 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004066 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4067 mInputs.keyAt(input_index));
4068 inputs.add(mInputs.keyAt(input_index));
4069 }
4070 }
4071 // Clear any profiles associated with the disconnected device.
4072 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4073 if (mHwModules[module_index]->mHandle == 0) {
4074 continue;
4075 }
4076 for (size_t profile_index = 0;
4077 profile_index < mHwModules[module_index]->mInputProfiles.size();
4078 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004079 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004080 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004081 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004082 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004083 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004084 }
4085 }
4086 }
4087 } // end disconnect
4088
4089 return NO_ERROR;
4090}
4091
4092
Eric Laurente0720872014-03-11 09:30:41 -07004093void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004094{
4095 ALOGV("closeOutput(%d)", output);
4096
Eric Laurentc75307b2015-03-17 15:29:32 -07004097 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004098 if (outputDesc == NULL) {
4099 ALOGW("closeOutput() unknown output %d", output);
4100 return;
4101 }
François Gaffie036e1e92015-03-19 10:16:24 +01004102 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004103
Eric Laurente552edb2014-03-10 17:42:56 -07004104 // look for duplicated outputs connected to the output being removed.
4105 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004106 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004107 if (dupOutputDesc->isDuplicated() &&
4108 (dupOutputDesc->mOutput1 == outputDesc ||
4109 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004110 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004111 if (dupOutputDesc->mOutput1 == outputDesc) {
4112 outputDesc2 = dupOutputDesc->mOutput2;
4113 } else {
4114 outputDesc2 = dupOutputDesc->mOutput1;
4115 }
4116 // As all active tracks on duplicated output will be deleted,
4117 // and as they were also referenced on the other output, the reference
4118 // count for their stream type must be adjusted accordingly on
4119 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004120 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004121 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004122 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004123 }
4124 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4125 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4126
4127 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004128 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004129 }
4130 }
4131
Eric Laurent05b90f82014-08-27 15:32:29 -07004132 nextAudioPortGeneration();
4133
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004134 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004135 if (index >= 0) {
4136 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4137 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4138 mAudioPatches.removeItemsAt(index);
4139 mpClientInterface->onAudioPatchListUpdate();
4140 }
4141
Eric Laurente552edb2014-03-10 17:42:56 -07004142 AudioParameter param;
4143 param.add(String8("closing"), String8("true"));
4144 mpClientInterface->setParameters(output, param.toString());
4145
4146 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004147 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004148 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004149}
4150
4151void AudioPolicyManager::closeInput(audio_io_handle_t input)
4152{
4153 ALOGV("closeInput(%d)", input);
4154
4155 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4156 if (inputDesc == NULL) {
4157 ALOGW("closeInput() unknown input %d", input);
4158 return;
4159 }
4160
Eric Laurent6a94d692014-05-20 11:18:06 -07004161 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004162
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004163 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004164 if (index >= 0) {
4165 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4166 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4167 mAudioPatches.removeItemsAt(index);
4168 mpClientInterface->onAudioPatchListUpdate();
4169 }
4170
4171 mpClientInterface->closeInput(input);
4172 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004173}
4174
Eric Laurentc75307b2015-03-17 15:29:32 -07004175SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4176 audio_devices_t device,
4177 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004178{
4179 SortedVector<audio_io_handle_t> outputs;
4180
4181 ALOGVV("getOutputsForDevice() device %04x", device);
4182 for (size_t i = 0; i < openOutputs.size(); i++) {
4183 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004184 i, openOutputs.valueAt(i)->isDuplicated(),
4185 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004186 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4187 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4188 outputs.add(openOutputs.keyAt(i));
4189 }
4190 }
4191 return outputs;
4192}
4193
Eric Laurente0720872014-03-11 09:30:41 -07004194bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004195 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004196{
4197 if (outputs1.size() != outputs2.size()) {
4198 return false;
4199 }
4200 for (size_t i = 0; i < outputs1.size(); i++) {
4201 if (outputs1[i] != outputs2[i]) {
4202 return false;
4203 }
4204 }
4205 return true;
4206}
4207
Eric Laurente0720872014-03-11 09:30:41 -07004208void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004209{
4210 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4211 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4212 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4213 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4214
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004215 // also take into account external policy-related changes: add all outputs which are
4216 // associated with policies in the "before" and "after" output vectors
4217 ALOGVV("checkOutputForStrategy(): policy related outputs");
4218 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004219 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004220 if (desc != 0 && desc->mPolicyMix != NULL) {
4221 srcOutputs.add(desc->mIoHandle);
4222 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4223 }
4224 }
4225 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004226 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004227 if (desc != 0 && desc->mPolicyMix != NULL) {
4228 dstOutputs.add(desc->mIoHandle);
4229 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4230 }
4231 }
4232
Eric Laurente552edb2014-03-10 17:42:56 -07004233 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4234 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4235 strategy, srcOutputs[0], dstOutputs[0]);
4236 // mute strategy while moving tracks from one output to another
4237 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004238 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004239 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004240 setStrategyMute(strategy, true, desc);
4241 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004242 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004243 sp<AudioSourceDescriptor> source =
4244 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4245 if (source != 0){
4246 connectAudioSource(source);
4247 }
Eric Laurente552edb2014-03-10 17:42:56 -07004248 }
4249
4250 // Move effects associated to this strategy from previous output to new output
4251 if (strategy == STRATEGY_MEDIA) {
4252 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4253 SortedVector<audio_io_handle_t> moved;
4254 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004255 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4256 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4257 effectDesc->mIo != fxOutput) {
4258 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004259 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4260 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004261 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004262 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004263 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004264 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004265 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004266 }
4267 }
4268 }
4269 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004270 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004271 if (getStrategy((audio_stream_type_t)i) == strategy) {
4272 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004273 }
4274 }
4275 }
4276}
4277
Eric Laurente0720872014-03-11 09:30:41 -07004278void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004279{
François Gaffie2110e042015-03-24 08:41:51 +01004280 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004281 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004282 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004283 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004284 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004285 checkOutputForStrategy(STRATEGY_SONIFICATION);
4286 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004287 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004288 checkOutputForStrategy(STRATEGY_MEDIA);
4289 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004290 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004291}
4292
Eric Laurente0720872014-03-11 09:30:41 -07004293void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004294{
François Gaffie53615e22015-03-19 09:24:12 +01004295 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004296 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004297 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004298 return;
4299 }
4300
Eric Laurent3a4311c2014-03-17 12:00:47 -07004301 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004302 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4303 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4304 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004305 // suspend A2DP output if:
4306 // (NOT already suspended) &&
4307 // ((SCO device is connected &&
4308 // (forced usage for communication || for record is SCO))) ||
4309 // (phone state is ringing || in call)
4310 //
4311 // restore A2DP output if:
4312 // (Already suspended) &&
4313 // ((SCO device is NOT connected ||
4314 // (forced usage NOT for communication && NOT for record is SCO))) &&
4315 // (phone state is NOT ringing && NOT in call)
4316 //
4317 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004318 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004319 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4320 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4321 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4322 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004323
4324 mpClientInterface->restoreOutput(a2dpOutput);
4325 mA2dpSuspended = false;
4326 }
4327 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004328 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004329 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4330 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4331 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4332 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004333
4334 mpClientInterface->suspendOutput(a2dpOutput);
4335 mA2dpSuspended = true;
4336 }
4337 }
4338}
4339
Eric Laurentc75307b2015-03-17 15:29:32 -07004340audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4341 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004342{
4343 audio_devices_t device = AUDIO_DEVICE_NONE;
4344
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004345 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004346 if (index >= 0) {
4347 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4348 if (patchDesc->mUid != mUidCached) {
4349 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004350 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004351 return outputDesc->device();
4352 }
4353 }
4354
Eric Laurente552edb2014-03-10 17:42:56 -07004355 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004356 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004357 // use device for strategy enforced audible
4358 // 2: we are in call or the strategy phone is active on the output:
4359 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004360 // 3: the strategy for enforced audible is active but not enforced on the output:
4361 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004362 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004363 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004364 // 5: the strategy accessibility is active on the output:
4365 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004366 // 6: the strategy "respectful" sonification is active on the output:
4367 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004368 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004369 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004370 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004371 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004372 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004373 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004374 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004375 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004376 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4377 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004378 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004379 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004380 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004381 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004382 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004383 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004384 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4385 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004386 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004387 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004388 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004389 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004390 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004391 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004392 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004393 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004394 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004395 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004396 }
4397
Eric Laurent1c333e22014-05-20 10:48:17 -07004398 ALOGV("getNewOutputDevice() selected device %x", device);
4399 return device;
4400}
4401
Eric Laurentfb66dd92016-01-28 18:32:03 -08004402audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004403{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004404 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004405
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004406 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004407 if (index >= 0) {
4408 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4409 if (patchDesc->mUid != mUidCached) {
4410 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004411 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004412 return inputDesc->mDevice;
4413 }
4414 }
4415
Eric Laurentfb66dd92016-01-28 18:32:03 -08004416 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4417 if (isInCall()) {
4418 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4419 } else if (source != AUDIO_SOURCE_DEFAULT) {
4420 device = getDeviceAndMixForInputSource(source);
4421 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004422
Eric Laurente552edb2014-03-10 17:42:56 -07004423 return device;
4424}
4425
Eric Laurent794fde22016-03-11 09:50:45 -08004426bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4427 audio_stream_type_t stream2) {
4428 return ((stream1 == stream2) ||
4429 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4430 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004431}
4432
Eric Laurente0720872014-03-11 09:30:41 -07004433uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004434 return (uint32_t)getStrategy(stream);
4435}
4436
Eric Laurente0720872014-03-11 09:30:41 -07004437audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004438 // By checking the range of stream before calling getStrategy, we avoid
4439 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4440 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004441 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004442 return AUDIO_DEVICE_NONE;
4443 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004444 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004445 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4446 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004447 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004448 }
Eric Laurent794fde22016-03-11 09:50:45 -08004449 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004450 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004451 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004452 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4453 for (size_t i = 0; i < outputs.size(); i++) {
4454 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004455 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004456 curDevices |= outputDesc->device();
4457 }
4458 }
4459 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004460 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004461
4462 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4463 and doesn't really need to.*/
4464 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4465 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4466 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4467 }
Eric Laurente552edb2014-03-10 17:42:56 -07004468 return devices;
4469}
4470
François Gaffie2110e042015-03-24 08:41:51 +01004471routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4472{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004473 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004474 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004475}
4476
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004477uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4478 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004479 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4480 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4481 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004482 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4483 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4484 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004485 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004486 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004487}
4488
Eric Laurente0720872014-03-11 09:30:41 -07004489void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004490 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004491 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004492 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4493 updateDevicesAndOutputs();
4494 break;
4495 default:
4496 break;
4497 }
4498}
4499
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004500uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004501
4502 // skip beacon mute management if a dedicated TTS output is available
4503 if (mTtsOutputAvailable) {
4504 return 0;
4505 }
4506
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004507 switch(event) {
4508 case STARTING_OUTPUT:
4509 mBeaconMuteRefCount++;
4510 break;
4511 case STOPPING_OUTPUT:
4512 if (mBeaconMuteRefCount > 0) {
4513 mBeaconMuteRefCount--;
4514 }
4515 break;
4516 case STARTING_BEACON:
4517 mBeaconPlayingRefCount++;
4518 break;
4519 case STOPPING_BEACON:
4520 if (mBeaconPlayingRefCount > 0) {
4521 mBeaconPlayingRefCount--;
4522 }
4523 break;
4524 }
4525
4526 if (mBeaconMuteRefCount > 0) {
4527 // any playback causes beacon to be muted
4528 return setBeaconMute(true);
4529 } else {
4530 // no other playback: unmute when beacon starts playing, mute when it stops
4531 return setBeaconMute(mBeaconPlayingRefCount == 0);
4532 }
4533}
4534
4535uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4536 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4537 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4538 // keep track of muted state to avoid repeating mute/unmute operations
4539 if (mBeaconMuted != mute) {
4540 // mute/unmute AUDIO_STREAM_TTS on all outputs
4541 ALOGV("\t muting %d", mute);
4542 uint32_t maxLatency = 0;
4543 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004544 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004545 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004546 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004547 0 /*delay*/, AUDIO_DEVICE_NONE);
4548 const uint32_t latency = desc->latency() * 2;
4549 if (latency > maxLatency) {
4550 maxLatency = latency;
4551 }
4552 }
4553 mBeaconMuted = mute;
4554 return maxLatency;
4555 }
4556 return 0;
4557}
4558
Eric Laurente0720872014-03-11 09:30:41 -07004559audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004560 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004561{
Paul McLeanaa981192015-03-21 09:55:15 -07004562 // Routing
4563 // see if we have an explicit route
4564 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4565 // (getStrategy(stream)).
4566 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4567 // and activity count is non-zero
4568 // the device = the device from the descriptor in the RouteMap, and exit.
4569 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4570 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004571 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4572 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004573 return route->mDeviceDescriptor->type();
4574 }
4575 }
4576
Eric Laurente552edb2014-03-10 17:42:56 -07004577 if (fromCache) {
4578 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4579 strategy, mDeviceForStrategy[strategy]);
4580 return mDeviceForStrategy[strategy];
4581 }
François Gaffie2110e042015-03-24 08:41:51 +01004582 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004583}
4584
Eric Laurente0720872014-03-11 09:30:41 -07004585void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004586{
4587 for (int i = 0; i < NUM_STRATEGIES; i++) {
4588 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4589 }
4590 mPreviousOutputs = mOutputs;
4591}
4592
Eric Laurent1f2f2232014-06-02 12:01:23 -07004593uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004594 audio_devices_t prevDevice,
4595 uint32_t delayMs)
4596{
4597 // mute/unmute strategies using an incompatible device combination
4598 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4599 // if unmuting, unmute only after the specified delay
4600 if (outputDesc->isDuplicated()) {
4601 return 0;
4602 }
4603
4604 uint32_t muteWaitMs = 0;
4605 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004606 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004607
4608 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4609 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004610 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004611 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4612 bool doMute = false;
4613
4614 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4615 doMute = true;
4616 outputDesc->mStrategyMutedByDevice[i] = true;
4617 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4618 doMute = true;
4619 outputDesc->mStrategyMutedByDevice[i] = false;
4620 }
Eric Laurent99401132014-05-07 19:48:15 -07004621 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004622 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004623 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004624 // skip output if it does not share any device with current output
4625 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4626 == AUDIO_DEVICE_NONE) {
4627 continue;
4628 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004629 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4630 mute ? "muting" : "unmuting", i, curDevice);
4631 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004632 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004633 if (mute) {
4634 // FIXME: should not need to double latency if volume could be applied
4635 // immediately by the audioflinger mixer. We must account for the delay
4636 // between now and the next time the audioflinger thread for this output
4637 // will process a buffer (which corresponds to one buffer size,
4638 // usually 1/2 or 1/4 of the latency).
4639 if (muteWaitMs < desc->latency() * 2) {
4640 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004641 }
4642 }
4643 }
4644 }
4645 }
4646 }
4647
Eric Laurent99401132014-05-07 19:48:15 -07004648 // temporary mute output if device selection changes to avoid volume bursts due to
4649 // different per device volumes
4650 if (outputDesc->isActive() && (device != prevDevice)) {
4651 if (muteWaitMs < outputDesc->latency() * 2) {
4652 muteWaitMs = outputDesc->latency() * 2;
4653 }
4654 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004655 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004656 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004657 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004658 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004659 muteWaitMs *2, device);
4660 }
4661 }
4662 }
4663
Eric Laurente552edb2014-03-10 17:42:56 -07004664 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4665 if (muteWaitMs > delayMs) {
4666 muteWaitMs -= delayMs;
4667 usleep(muteWaitMs * 1000);
4668 return muteWaitMs;
4669 }
4670 return 0;
4671}
4672
Eric Laurentc75307b2015-03-17 15:29:32 -07004673uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004674 audio_devices_t device,
4675 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004676 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004677 audio_patch_handle_t *patchHandle,
4678 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004679{
Eric Laurentc75307b2015-03-17 15:29:32 -07004680 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004681 AudioParameter param;
4682 uint32_t muteWaitMs;
4683
4684 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004685 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4686 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004687 return muteWaitMs;
4688 }
4689 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4690 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004691 if ((device != AUDIO_DEVICE_NONE) &&
4692 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004693 return 0;
4694 }
4695
4696 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004697 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004698
4699 audio_devices_t prevDevice = outputDesc->mDevice;
4700
Paul McLeanaa981192015-03-21 09:55:15 -07004701 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004702
4703 if (device != AUDIO_DEVICE_NONE) {
4704 outputDesc->mDevice = device;
4705 }
4706 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4707
4708 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004709 // the requested device is AUDIO_DEVICE_NONE
4710 // OR the requested device is the same as current device
4711 // AND force is not specified
4712 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004713 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004714 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4715 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004716 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004717 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004718 return muteWaitMs;
4719 }
4720
4721 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004722
Eric Laurente552edb2014-03-10 17:42:56 -07004723 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004724 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004725 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004726 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004727 DeviceVector deviceList;
4728 if ((address == NULL) || (strlen(address) == 0)) {
4729 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4730 } else {
4731 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4732 }
4733
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 if (!deviceList.isEmpty()) {
4735 struct audio_patch patch;
4736 outputDesc->toAudioPortConfig(&patch.sources[0]);
4737 patch.num_sources = 1;
4738 patch.num_sinks = 0;
4739 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4740 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004741 patch.num_sinks++;
4742 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004743 ssize_t index;
4744 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4745 index = mAudioPatches.indexOfKey(*patchHandle);
4746 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004747 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004748 }
4749 sp< AudioPatch> patchDesc;
4750 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4751 if (index >= 0) {
4752 patchDesc = mAudioPatches.valueAt(index);
4753 afPatchHandle = patchDesc->mAfPatchHandle;
4754 }
4755
Eric Laurent1c333e22014-05-20 10:48:17 -07004756 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004757 &afPatchHandle,
4758 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004759 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4760 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004761 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004762 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004763 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004764 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004765 addAudioPatch(patchDesc->mHandle, patchDesc);
4766 } else {
4767 patchDesc->mPatch = patch;
4768 }
4769 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004770 if (patchHandle) {
4771 *patchHandle = patchDesc->mHandle;
4772 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004773 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004774 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004775 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004776 }
4777 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004778
4779 // inform all input as well
4780 for (size_t i = 0; i < mInputs.size(); i++) {
4781 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004782 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004783 AudioParameter inputCmd = AudioParameter();
4784 ALOGV("%s: inform input %d of device:%d", __func__,
4785 inputDescriptor->mIoHandle, device);
4786 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4787 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4788 inputCmd.toString(),
4789 delayMs);
4790 }
4791 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004792 }
Eric Laurente552edb2014-03-10 17:42:56 -07004793
4794 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004795 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004796
4797 return muteWaitMs;
4798}
4799
Eric Laurentc75307b2015-03-17 15:29:32 -07004800status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004801 int delayMs,
4802 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004803{
Eric Laurent6a94d692014-05-20 11:18:06 -07004804 ssize_t index;
4805 if (patchHandle) {
4806 index = mAudioPatches.indexOfKey(*patchHandle);
4807 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004808 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004809 }
4810 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004811 return INVALID_OPERATION;
4812 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004813 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4814 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004815 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004816 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004817 removeAudioPatch(patchDesc->mHandle);
4818 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004819 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004820 return status;
4821}
4822
4823status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4824 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004825 bool force,
4826 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004827{
4828 status_t status = NO_ERROR;
4829
Eric Laurent1f2f2232014-06-02 12:01:23 -07004830 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004831 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4832 inputDesc->mDevice = device;
4833
4834 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4835 if (!deviceList.isEmpty()) {
4836 struct audio_patch patch;
4837 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004838 // AUDIO_SOURCE_HOTWORD is for internal use only:
4839 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004840 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004841 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004842 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4843 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004844 patch.num_sinks = 1;
4845 //only one input device for now
4846 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004847 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004848 ssize_t index;
4849 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4850 index = mAudioPatches.indexOfKey(*patchHandle);
4851 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004852 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004853 }
4854 sp< AudioPatch> patchDesc;
4855 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4856 if (index >= 0) {
4857 patchDesc = mAudioPatches.valueAt(index);
4858 afPatchHandle = patchDesc->mAfPatchHandle;
4859 }
4860
Eric Laurent1c333e22014-05-20 10:48:17 -07004861 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004862 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004863 0);
4864 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004865 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004868 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004869 addAudioPatch(patchDesc->mHandle, patchDesc);
4870 } else {
4871 patchDesc->mPatch = patch;
4872 }
4873 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004874 if (patchHandle) {
4875 *patchHandle = patchDesc->mHandle;
4876 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004877 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004878 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004879 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004880 }
4881 }
4882 }
4883 return status;
4884}
4885
Eric Laurent6a94d692014-05-20 11:18:06 -07004886status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4887 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004888{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004889 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004890 ssize_t index;
4891 if (patchHandle) {
4892 index = mAudioPatches.indexOfKey(*patchHandle);
4893 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004894 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004895 }
4896 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004897 return INVALID_OPERATION;
4898 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004899 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4900 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004901 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004902 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004903 removeAudioPatch(patchDesc->mHandle);
4904 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004905 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004906 return status;
4907}
4908
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004909sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004910 String8 address,
4911 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004912 audio_format_t& format,
4913 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004914 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004915{
4916 // Choose an input profile based on the requested capture parameters: select the first available
4917 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004918 //
4919 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4920 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004921
4922 for (size_t i = 0; i < mHwModules.size(); i++)
4923 {
4924 if (mHwModules[i]->mHandle == 0) {
4925 continue;
4926 }
4927 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4928 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004929 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004930 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004931 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004932 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004933 format,
4934 &format /*updatedFormat*/,
4935 channelMask,
4936 &channelMask /*updatedChannelMask*/,
4937 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004938
Eric Laurente552edb2014-03-10 17:42:56 -07004939 return profile;
4940 }
4941 }
4942 }
4943 return NULL;
4944}
4945
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004946
4947audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004948 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004949{
François Gaffie036e1e92015-03-19 10:16:24 +01004950 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4951 audio_devices_t selectedDeviceFromMix =
4952 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004953
François Gaffie036e1e92015-03-19 10:16:24 +01004954 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4955 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004956 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004957 return getDeviceForInputSource(inputSource);
4958}
4959
4960audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4961{
Paul McLean466dc8e2015-04-17 13:15:36 -06004962 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4963 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004964 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004965 return route->mDeviceDescriptor->type();
4966 }
4967 }
4968
4969 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004970}
4971
Eric Laurente0720872014-03-11 09:30:41 -07004972float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004973 int index,
4974 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004975{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004976 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004977 // if a headset is connected, apply the following rules to ring tones and notifications
4978 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004979 // - always attenuate notifications volume by 6dB
4980 // - attenuate ring tones volume by 6dB unless music is not playing and
4981 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004982 // - if music is playing, always limit the volume to current music volume,
4983 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004984 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004985 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4986 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4987 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4988 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4989 ((stream_strategy == STRATEGY_SONIFICATION)
4990 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004991 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004992 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004993 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004994 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004995 // when the phone is ringing we must consider that music could have been paused just before
4996 // by the music application and behave as if music was active if the last music track was
4997 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004998 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004999 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005000 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005001 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005002 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005003 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5004 musicDevice),
5005 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005006 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5007 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005008 if (volumeDB > minVolDB) {
5009 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005010 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005011 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005012 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5013 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5014 // on A2DP, also ensure notification volume is not too low compared to media when
5015 // intended to be played
5016 if ((volumeDB > -96.0f) &&
5017 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5018 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5019 stream, device,
5020 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5021 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5022 }
5023 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005024 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5025 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005026 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005027 }
5028 }
5029
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005030 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005031}
5032
Eric Laurente0720872014-03-11 09:30:41 -07005033status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005034 int index,
5035 const sp<AudioOutputDescriptor>& outputDesc,
5036 audio_devices_t device,
5037 int delayMs,
5038 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005039{
Eric Laurente552edb2014-03-10 17:42:56 -07005040 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005041 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005042 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005043 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005044 return NO_ERROR;
5045 }
François Gaffie2110e042015-03-24 08:41:51 +01005046 audio_policy_forced_cfg_t forceUseForComm =
5047 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005048 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005049 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5050 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005051 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005052 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005053 return INVALID_OPERATION;
5054 }
5055
Eric Laurentc75307b2015-03-17 15:29:32 -07005056 if (device == AUDIO_DEVICE_NONE) {
5057 device = outputDesc->device();
5058 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005059
Eric Laurentffbc80f2015-03-18 18:30:19 -07005060 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005061 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005062 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005063 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005064
Eric Laurentffbc80f2015-03-18 18:30:19 -07005065 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005066
Eric Laurent3b73df72014-03-11 09:06:29 -07005067 if (stream == AUDIO_STREAM_VOICE_CALL ||
5068 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005069 float voiceVolume;
5070 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005071 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005072 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005073 } else {
5074 voiceVolume = 1.0;
5075 }
5076
Eric Laurent18fba842016-03-31 14:41:26 -07005077 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005078 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5079 mLastVoiceVolume = voiceVolume;
5080 }
5081 }
5082
5083 return NO_ERROR;
5084}
5085
Eric Laurentc75307b2015-03-17 15:29:32 -07005086void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5087 audio_devices_t device,
5088 int delayMs,
5089 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005090{
Eric Laurentc75307b2015-03-17 15:29:32 -07005091 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005092
Eric Laurent794fde22016-03-11 09:50:45 -08005093 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005094 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005095 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005096 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005097 device,
5098 delayMs,
5099 force);
5100 }
5101}
5102
Eric Laurente0720872014-03-11 09:30:41 -07005103void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005104 bool on,
5105 const sp<AudioOutputDescriptor>& outputDesc,
5106 int delayMs,
5107 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005108{
Eric Laurent72249d52015-06-08 11:12:15 -07005109 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5110 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005111 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005112 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005113 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005114 }
5115 }
5116}
5117
Eric Laurente0720872014-03-11 09:30:41 -07005118void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005119 bool on,
5120 const sp<AudioOutputDescriptor>& outputDesc,
5121 int delayMs,
5122 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005123{
Eric Laurente552edb2014-03-10 17:42:56 -07005124 if (device == AUDIO_DEVICE_NONE) {
5125 device = outputDesc->device();
5126 }
5127
Eric Laurentc75307b2015-03-17 15:29:32 -07005128 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5129 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005130
5131 if (on) {
5132 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005133 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005134 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005135 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005136 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005137 }
5138 }
5139 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5140 outputDesc->mMuteCount[stream]++;
5141 } else {
5142 if (outputDesc->mMuteCount[stream] == 0) {
5143 ALOGV("setStreamMute() unmuting non muted stream!");
5144 return;
5145 }
5146 if (--outputDesc->mMuteCount[stream] == 0) {
5147 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005148 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005149 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005150 device,
5151 delayMs);
5152 }
5153 }
5154}
5155
Eric Laurente0720872014-03-11 09:30:41 -07005156void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005157 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005158{
Eric Laurent87ffa392015-05-22 10:32:38 -07005159 if(!hasPrimaryOutput()) {
5160 return;
5161 }
5162
Eric Laurente552edb2014-03-10 17:42:56 -07005163 // if the stream pertains to sonification strategy and we are in call we must
5164 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5165 // in the device used for phone strategy and play the tone if the selected device does not
5166 // interfere with the device used for phone strategy
5167 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5168 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005169 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005170 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5171 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005172 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005173 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5174 stream, starting, outputDesc->mDevice, stateChange);
5175 if (outputDesc->mRefCount[stream]) {
5176 int muteCount = 1;
5177 if (stateChange) {
5178 muteCount = outputDesc->mRefCount[stream];
5179 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005180 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005181 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5182 for (int i = 0; i < muteCount; i++) {
5183 setStreamMute(stream, starting, mPrimaryOutput);
5184 }
5185 } else {
5186 ALOGV("handleIncallSonification() high visibility");
5187 if (outputDesc->device() &
5188 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5189 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5190 for (int i = 0; i < muteCount; i++) {
5191 setStreamMute(stream, starting, mPrimaryOutput);
5192 }
5193 }
5194 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005195 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5196 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005197 } else {
5198 mpClientInterface->stopTone();
5199 }
5200 }
5201 }
5202 }
5203}
5204
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005205audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5206{
5207 // flags to stream type mapping
5208 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5209 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5210 }
5211 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5212 return AUDIO_STREAM_BLUETOOTH_SCO;
5213 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005214 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5215 return AUDIO_STREAM_TTS;
5216 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005217
5218 // usage to stream type mapping
5219 switch (attr->usage) {
5220 case AUDIO_USAGE_MEDIA:
5221 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005222 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5223 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005224 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5225 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005226 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5227 return AUDIO_STREAM_SYSTEM;
5228 case AUDIO_USAGE_VOICE_COMMUNICATION:
5229 return AUDIO_STREAM_VOICE_CALL;
5230
5231 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5232 return AUDIO_STREAM_DTMF;
5233
5234 case AUDIO_USAGE_ALARM:
5235 return AUDIO_STREAM_ALARM;
5236 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5237 return AUDIO_STREAM_RING;
5238
5239 case AUDIO_USAGE_NOTIFICATION:
5240 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5241 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5242 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5243 case AUDIO_USAGE_NOTIFICATION_EVENT:
5244 return AUDIO_STREAM_NOTIFICATION;
5245
5246 case AUDIO_USAGE_UNKNOWN:
5247 default:
5248 return AUDIO_STREAM_MUSIC;
5249 }
5250}
Eric Laurente83b55d2014-11-14 10:06:21 -08005251
François Gaffie53615e22015-03-19 09:24:12 +01005252bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5253{
Eric Laurente83b55d2014-11-14 10:06:21 -08005254 // has flags that map to a strategy?
5255 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5256 return true;
5257 }
5258
5259 // has known usage?
5260 switch (paa->usage) {
5261 case AUDIO_USAGE_UNKNOWN:
5262 case AUDIO_USAGE_MEDIA:
5263 case AUDIO_USAGE_VOICE_COMMUNICATION:
5264 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5265 case AUDIO_USAGE_ALARM:
5266 case AUDIO_USAGE_NOTIFICATION:
5267 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5268 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5269 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5270 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5271 case AUDIO_USAGE_NOTIFICATION_EVENT:
5272 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5273 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5274 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5275 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005276 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005277 break;
5278 default:
5279 return false;
5280 }
5281 return true;
5282}
5283
François Gaffiead3183e2015-03-18 16:55:35 +01005284bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5285 routing_strategy strategy, uint32_t inPastMs,
5286 nsecs_t sysTime) const
5287{
5288 if ((sysTime == 0) && (inPastMs != 0)) {
5289 sysTime = systemTime();
5290 }
Eric Laurent794fde22016-03-11 09:50:45 -08005291 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005292 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5293 (NUM_STRATEGIES == strategy)) &&
5294 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5295 return true;
5296 }
5297 }
5298 return false;
5299}
5300
François Gaffie2110e042015-03-24 08:41:51 +01005301audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5302{
5303 return mEngine->getForceUse(usage);
5304}
5305
5306bool AudioPolicyManager::isInCall()
5307{
5308 return isStateInCall(mEngine->getPhoneState());
5309}
5310
5311bool AudioPolicyManager::isStateInCall(int state)
5312{
5313 return is_state_in_call(state);
5314}
5315
Eric Laurentd60560a2015-04-10 11:31:20 -07005316void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5317{
5318 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5319 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5320 if (sourceDesc->mDevice->equals(deviceDesc)) {
5321 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5322 stopAudioSource(sourceDesc->getHandle());
5323 }
5324 }
5325
5326 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5327 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5328 bool release = false;
5329 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5330 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5331 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5332 source->ext.device.type == deviceDesc->type()) {
5333 release = true;
5334 }
5335 }
5336 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5337 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5338 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5339 sink->ext.device.type == deviceDesc->type()) {
5340 release = true;
5341 }
5342 }
5343 if (release) {
5344 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5345 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5346 }
5347 }
5348}
5349
Phil Burk09bc4612016-02-24 15:58:15 -08005350// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005351void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5352 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005353 // TODO Set this based on Config properties.
5354 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005355
5356 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5357 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005358 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005359
5360 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005361 bool supportsAC3 = false;
5362 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005363 bool supportsIEC61937 = false;
5364 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5365 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005366 switch (format) {
5367 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005368 supportsAC3 = true;
5369 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005370 case AUDIO_FORMAT_E_AC3:
5371 case AUDIO_FORMAT_DTS:
5372 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005373 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005374 break;
5375 case AUDIO_FORMAT_IEC61937:
5376 supportsIEC61937 = true;
5377 break;
5378 default:
5379 break;
5380 }
5381 }
Phil Burk09bc4612016-02-24 15:58:15 -08005382
5383 // Modify formats based on surround preferences.
5384 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005385 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5386 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5387 // Remove surround sound related formats.
5388 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5389 audio_format_t format = formats[formatIndex];
5390 switch(format) {
5391 case AUDIO_FORMAT_AC3:
5392 case AUDIO_FORMAT_E_AC3:
5393 case AUDIO_FORMAT_DTS:
5394 case AUDIO_FORMAT_DTS_HD:
5395 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005396 formats.removeAt(formatIndex);
5397 break;
5398 default:
5399 formatIndex++; // keep it
5400 break;
5401 }
Phil Burk09bc4612016-02-24 15:58:15 -08005402 }
Phil Burk07ac1142016-03-25 13:39:29 -07005403 supportsAC3 = false;
5404 supportsOtherSurround = false;
5405 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005406 }
Phil Burk07ac1142016-03-25 13:39:29 -07005407 } else { // AUTO or ALWAYS
5408 // Most TVs support AC3 even if they do not report it in the EDID.
5409 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5410 && !supportsAC3) {
5411 formats.add(AUDIO_FORMAT_AC3);
5412 supportsAC3 = true;
5413 }
5414
5415 // If ALWAYS, add support for raw surround formats if all are missing.
5416 // This assumes that if any of these formats are reported by the HAL
5417 // then the report is valid and should not be modified.
5418 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5419 && !supportsOtherSurround) {
5420 formats.add(AUDIO_FORMAT_E_AC3);
5421 formats.add(AUDIO_FORMAT_DTS);
5422 formats.add(AUDIO_FORMAT_DTS_HD);
5423 supportsOtherSurround = true;
5424 }
5425
5426 // Add support for IEC61937 if any raw surround supported.
5427 // The HAL could do this but add it here, just in case.
5428 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5429 formats.add(AUDIO_FORMAT_IEC61937);
5430 supportsIEC61937 = true;
5431 }
Phil Burk09bc4612016-02-24 15:58:15 -08005432 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005433}
5434
5435// Modify the list of channel masks supported.
5436void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5437 ChannelsVector &channelMasks = *channelMasksPtr;
5438 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5439 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5440
5441 // If NEVER, then remove support for channelMasks > stereo.
5442 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5443 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5444 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5445 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5446 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5447 channelMasks.removeAt(maskIndex);
5448 } else {
5449 maskIndex++;
5450 }
5451 }
5452 // If ALWAYS, then make sure we at least support 5.1
5453 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5454 bool supports5dot1 = false;
5455 // Are there any channel masks that can be considered "surround"?
5456 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5457 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5458 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5459 supports5dot1 = true;
5460 break;
5461 }
5462 }
5463 // If not then add 5.1 support.
5464 if (!supports5dot1) {
5465 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5466 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5467 }
Phil Burk09bc4612016-02-24 15:58:15 -08005468 }
5469}
5470
Phil Burk00eeb322016-03-31 12:41:00 -07005471void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5472 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005473 AudioProfileVector &profiles)
5474{
5475 String8 reply;
5476 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005477
François Gaffie112b0af2015-11-19 16:13:25 +01005478 // Format MUST be checked first to update the list of AudioProfile
5479 if (profiles.hasDynamicFormat()) {
5480 reply = mpClientInterface->getParameters(ioHandle,
5481 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005482 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005483 AudioParameter repliedParameters(reply);
5484 if (repliedParameters.get(
5485 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005486 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5487 return;
5488 }
Phil Burk09bc4612016-02-24 15:58:15 -08005489 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005490 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005491 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005492 }
Phil Burk09bc4612016-02-24 15:58:15 -08005493 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005494 }
5495 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5496
Eric Laurent20eb3a42016-01-26 18:39:17 -08005497 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005498 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005499 ChannelsVector channelMasks;
5500 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005501 AudioParameter requestedParameters;
5502 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5503
5504 if (profiles.hasDynamicRateFor(format)) {
5505 reply = mpClientInterface->getParameters(ioHandle,
5506 requestedParameters.toString() + ";" +
5507 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5508 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005509 AudioParameter repliedParameters(reply);
5510 if (repliedParameters.get(
5511 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5512 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005513 }
5514 }
5515 if (profiles.hasDynamicChannelsFor(format)) {
5516 reply = mpClientInterface->getParameters(ioHandle,
5517 requestedParameters.toString() + ";" +
5518 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5519 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005520 AudioParameter repliedParameters(reply);
5521 if (repliedParameters.get(
5522 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5523 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005524 if (device == AUDIO_DEVICE_OUT_HDMI) {
5525 filterSurroundChannelMasks(&channelMasks);
5526 }
François Gaffie112b0af2015-11-19 16:13:25 +01005527 }
5528 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005529 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005530 }
5531}
Eric Laurentd60560a2015-04-10 11:31:20 -07005532
Eric Laurente552edb2014-03-10 17:42:56 -07005533}; // namespace android