blob: 1bd5b68e07bf0173efd3348c1a4aefd9dd895b82 [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 Laurent232f26f2016-02-17 18:06:27 -0800444 audio_io_handle_t activeInput = mInputs.getActiveInput();
445 if (activeInput != 0) {
446 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
447 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
448 //FIXME: consider all active sessions
449 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
450 audio_session_t activeSession = activeSessions.keyAt(0);
451 stopInput(activeInput, activeSession);
452 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700453 }
454 }
455
Eric Laurentc2730ba2014-07-20 15:47:07 -0700456 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
457 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
458 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
459 status);
460 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100461 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700462 mCallTxPatch->mAfPatchHandle = afPatchHandle;
463 mCallTxPatch->mUid = mUidCached;
464 }
465 }
466}
467
Eric Laurente0720872014-03-11 09:30:41 -0700468void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700469{
470 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100471 // store previous phone state for management of sonification strategy below
472 int oldState = mEngine->getPhoneState();
473
474 if (mEngine->setPhoneState(state) != NO_ERROR) {
475 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700476 return;
477 }
François Gaffie2110e042015-03-24 08:41:51 +0100478 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700479 // if leaving call state, handle special case of active streams
480 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700481 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700482 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800483 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700484 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700485 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800486
Eric Laurent63dea1d2015-07-02 17:10:28 -0700487 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800488 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700489 }
490
François Gaffie2110e042015-03-24 08:41:51 +0100491 /**
492 * Switching to or from incall state or switching between telephony and VoIP lead to force
493 * routing command.
494 */
495 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
496 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700497
498 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700499 checkA2dpSuspend();
500 checkOutputForAllStrategies();
501 updateDevicesAndOutputs();
502
Eric Laurente552edb2014-03-10 17:42:56 -0700503 int delayMs = 0;
504 if (isStateInCall(state)) {
505 nsecs_t sysTime = systemTime();
506 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700507 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700508 // mute media and sonification strategies and delay device switch by the largest
509 // latency of any output where either strategy is active.
510 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100511 if ((isStrategyActive(desc, STRATEGY_MEDIA,
512 SONIFICATION_HEADSET_MUSIC_DELAY,
513 sysTime) ||
514 isStrategyActive(desc, STRATEGY_SONIFICATION,
515 SONIFICATION_HEADSET_MUSIC_DELAY,
516 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700517 (delayMs < (int)desc->latency()*2)) {
518 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700519 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700520 setStrategyMute(STRATEGY_MEDIA, true, desc);
521 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700522 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700523 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
524 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700525 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
526 }
527 }
528
Eric Laurent87ffa392015-05-22 10:32:38 -0700529 if (hasPrimaryOutput()) {
530 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
531 // the device returned is not necessarily reachable via this output
532 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
533 // force routing command to audio hardware when ending call
534 // even if no device change is needed
535 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
536 rxDevice = mPrimaryOutput->device();
537 }
Eric Laurente552edb2014-03-10 17:42:56 -0700538
Eric Laurent87ffa392015-05-22 10:32:38 -0700539 if (state == AUDIO_MODE_IN_CALL) {
540 updateCallRouting(rxDevice, delayMs);
541 } else if (oldState == AUDIO_MODE_IN_CALL) {
542 if (mCallRxPatch != 0) {
543 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
544 mCallRxPatch.clear();
545 }
546 if (mCallTxPatch != 0) {
547 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
548 mCallTxPatch.clear();
549 }
550 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
551 } else {
552 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700553 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700554 }
Eric Laurente552edb2014-03-10 17:42:56 -0700555 // if entering in call state, handle special case of active streams
556 // pertaining to sonification strategy see handleIncallSonification()
557 if (isStateInCall(state)) {
558 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800559 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700560 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700561 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700562
563 // force reevaluating accessibility routing when call starts
564 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700565 }
566
567 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700568 if (state == AUDIO_MODE_RINGTONE &&
569 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700570 mLimitRingtoneVolume = true;
571 } else {
572 mLimitRingtoneVolume = false;
573 }
574}
575
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700576audio_mode_t AudioPolicyManager::getPhoneState() {
577 return mEngine->getPhoneState();
578}
579
Eric Laurente0720872014-03-11 09:30:41 -0700580void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700581 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700582{
François Gaffie2110e042015-03-24 08:41:51 +0100583 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700584
François Gaffie2110e042015-03-24 08:41:51 +0100585 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
586 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
587 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700588 }
François Gaffie2110e042015-03-24 08:41:51 +0100589 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
590 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
591 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700592
593 // check for device and output changes triggered by new force usage
594 checkA2dpSuspend();
595 checkOutputForAllStrategies();
596 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800597
Eric Laurent87ffa392015-05-22 10:32:38 -0700598 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700599 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
600 updateCallRouting(newDevice);
601 }
Eric Laurente552edb2014-03-10 17:42:56 -0700602 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700603 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
604 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
605 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
606 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700607 }
Eric Laurente552edb2014-03-10 17:42:56 -0700608 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700609 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 }
611 }
612
Eric Laurent232f26f2016-02-17 18:06:27 -0800613 audio_io_handle_t activeInput = mInputs.getActiveInput();
614 if (activeInput != 0) {
615 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
616 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700617 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800618 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
619 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700620 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800621 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700622 }
Eric Laurente552edb2014-03-10 17:42:56 -0700623 }
Eric Laurente552edb2014-03-10 17:42:56 -0700624}
625
Eric Laurente0720872014-03-11 09:30:41 -0700626void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700627{
628 ALOGV("setSystemProperty() property %s, value %s", property, value);
629}
630
631// Find a direct output profile compatible with the parameters passed, even if the input flags do
632// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800633sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700634 audio_devices_t device,
635 uint32_t samplingRate,
636 audio_format_t format,
637 audio_channel_mask_t channelMask,
638 audio_output_flags_t flags)
639{
Eric Laurent861a6282015-05-18 15:40:16 -0700640 // only retain flags that will drive the direct output profile selection
641 // if explicitly requested
642 static const uint32_t kRelevantFlags =
643 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
644 flags =
645 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
646
647 sp<IOProfile> profile;
648
Eric Laurente552edb2014-03-10 17:42:56 -0700649 for (size_t i = 0; i < mHwModules.size(); i++) {
650 if (mHwModules[i]->mHandle == 0) {
651 continue;
652 }
653 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700654 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
655 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700656 samplingRate, NULL /*updatedSamplingRate*/,
657 format, NULL /*updatedFormat*/,
658 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700659 flags)) {
660 continue;
661 }
662 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100663 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700664 continue;
665 }
666 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100667 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700668 continue;
669 }
670 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100671 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700672 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700673 }
Eric Laurente552edb2014-03-10 17:42:56 -0700674 }
675 }
Eric Laurent861a6282015-05-18 15:40:16 -0700676 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700677}
678
Eric Laurente0720872014-03-11 09:30:41 -0700679audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100680 uint32_t samplingRate,
681 audio_format_t format,
682 audio_channel_mask_t channelMask,
683 audio_output_flags_t flags,
684 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700685{
Eric Laurent3b73df72014-03-11 09:06:29 -0700686 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700687 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
688 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
689 device, stream, samplingRate, format, channelMask, flags);
690
Eric Laurente83b55d2014-11-14 10:06:21 -0800691 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
692 stream, samplingRate,format, channelMask,
693 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700694}
695
Eric Laurente83b55d2014-11-14 10:06:21 -0800696status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
697 audio_io_handle_t *output,
698 audio_session_t session,
699 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700700 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800701 uint32_t samplingRate,
702 audio_format_t format,
703 audio_channel_mask_t channelMask,
704 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700705 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800706 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707{
Eric Laurente83b55d2014-11-14 10:06:21 -0800708 audio_attributes_t attributes;
709 if (attr != NULL) {
710 if (!isValidAttributes(attr)) {
711 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
712 attr->usage, attr->content_type, attr->flags,
713 attr->tags);
714 return BAD_VALUE;
715 }
716 attributes = *attr;
717 } else {
718 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
719 ALOGE("getOutputForAttr(): invalid stream type");
720 return BAD_VALUE;
721 }
722 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700723 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700724 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800725 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100726 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800727 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100728 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800729 }
François Gaffie036e1e92015-03-19 10:16:24 +0100730 *stream = streamTypefromAttributesInt(&attributes);
731 *output = desc->mIoHandle;
732 ALOGV("getOutputForAttr() returns output %d", *output);
733 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800734 }
735 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
736 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
737 return BAD_VALUE;
738 }
739
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700740 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
741 " session %d selectedDeviceId %d",
742 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
743 session, selectedDeviceId);
744
745 *stream = streamTypefromAttributesInt(&attributes);
746
747 // Explicit routing?
748 sp<DeviceDescriptor> deviceDesc;
749 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
750 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
751 deviceDesc = mAvailableOutputDevices[i];
752 break;
753 }
754 }
755 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700756
Eric Laurente83b55d2014-11-14 10:06:21 -0800757 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700758 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700759
Eric Laurente83b55d2014-11-14 10:06:21 -0800760 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700761 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
762 }
763
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700764 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700765 device, samplingRate, format, channelMask, flags);
766
Eric Laurente83b55d2014-11-14 10:06:21 -0800767 *output = getOutputForDevice(device, session, *stream,
768 samplingRate, format, channelMask,
769 flags, offloadInfo);
770 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700771 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800772 return INVALID_OPERATION;
773 }
Paul McLeanaa981192015-03-21 09:55:15 -0700774
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700776}
777
778audio_io_handle_t AudioPolicyManager::getOutputForDevice(
779 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800780 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700781 audio_stream_type_t stream,
782 uint32_t samplingRate,
783 audio_format_t format,
784 audio_channel_mask_t channelMask,
785 audio_output_flags_t flags,
786 const audio_offload_info_t *offloadInfo)
787{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700788 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700789 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700790 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700791
Eric Laurente552edb2014-03-10 17:42:56 -0700792#ifdef AUDIO_POLICY_TEST
793 if (mCurOutput != 0) {
794 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
795 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
796
797 if (mTestOutputs[mCurOutput] == 0) {
798 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700799 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
800 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700801 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700802 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700803 outputDesc->mFlags =
804 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700805 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700806 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
807 config.sample_rate = mTestSamplingRate;
808 config.channel_mask = mTestChannels;
809 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700810 if (offloadInfo != NULL) {
811 config.offload_info = *offloadInfo;
812 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700813 status = mpClientInterface->openOutput(0,
814 &mTestOutputs[mCurOutput],
815 &config,
816 &outputDesc->mDevice,
817 String8(""),
818 &outputDesc->mLatency,
819 outputDesc->mFlags);
820 if (status == NO_ERROR) {
821 outputDesc->mSamplingRate = config.sample_rate;
822 outputDesc->mFormat = config.format;
823 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700824 AudioParameter outputCmd = AudioParameter();
825 outputCmd.addInt(String8("set_id"),mCurOutput);
826 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
827 addOutput(mTestOutputs[mCurOutput], outputDesc);
828 }
829 }
830 return mTestOutputs[mCurOutput];
831 }
832#endif //AUDIO_POLICY_TEST
833
834 // open a direct output if required by specified parameters
835 //force direct flag if offload flag is set: offloading implies a direct output stream
836 // and all common behaviors are driven by checking only the direct flag
837 // this should normally be set appropriately in the policy configuration file
838 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700839 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700840 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700841 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
842 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
843 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800844 // only allow deep buffering for music stream type
845 if (stream != AUDIO_STREAM_MUSIC) {
846 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700847 } else if (/* stream == AUDIO_STREAM_MUSIC && */
848 flags == AUDIO_OUTPUT_FLAG_NONE &&
849 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
850 // use DEEP_BUFFER as default output for music stream type
851 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800852 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700853 if (stream == AUDIO_STREAM_TTS) {
854 flags = AUDIO_OUTPUT_FLAG_TTS;
855 }
Eric Laurente552edb2014-03-10 17:42:56 -0700856
Eric Laurentb732cf52014-09-24 19:08:21 -0700857 sp<IOProfile> profile;
858
859 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700860 // and not explicitly requested
861 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800862 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700863 audio_channel_count_from_out_mask(channelMask) <= 2) {
864 goto non_direct_output;
865 }
866
Andy Hung2ddee192015-12-18 17:34:44 -0800867 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
868 // This prevents creating an offloaded track and tearing it down immediately after start
869 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700870 // FIXME: We should check the audio session here but we do not have it in this context.
871 // This may prevent offloading in rare situations where effects are left active by apps
872 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700873
Eric Laurente552edb2014-03-10 17:42:56 -0700874 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800875 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700876 profile = getProfileForDirectOutput(device,
877 samplingRate,
878 format,
879 channelMask,
880 (audio_output_flags_t)flags);
881 }
882
Eric Laurent1c333e22014-05-20 10:48:17 -0700883 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700884 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700885
886 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700887 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700888 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
889 outputDesc = desc;
890 // reuse direct output if currently open and configured with same parameters
891 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800892 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700893 (channelMask == outputDesc->mChannelMask)) {
894 outputDesc->mDirectOpenCount++;
895 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
896 return mOutputs.keyAt(i);
897 }
898 }
899 }
900 // close direct output if currently open and configured with different parameters
901 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700902 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700903 }
Eric Laurent861a6282015-05-18 15:40:16 -0700904
905 // if the selected profile is offloaded and no offload info was specified,
906 // create a default one
907 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100908 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700909 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
910 defaultOffloadInfo.sample_rate = samplingRate;
911 defaultOffloadInfo.channel_mask = channelMask;
912 defaultOffloadInfo.format = format;
913 defaultOffloadInfo.stream_type = stream;
914 defaultOffloadInfo.bit_rate = 0;
915 defaultOffloadInfo.duration_us = -1;
916 defaultOffloadInfo.has_video = true; // conservative
917 defaultOffloadInfo.is_streaming = true; // likely
918 offloadInfo = &defaultOffloadInfo;
919 }
920
Eric Laurentc75307b2015-03-17 15:29:32 -0700921 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700922 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700923 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700924 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700925 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
926 config.sample_rate = samplingRate;
927 config.channel_mask = channelMask;
928 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700929 if (offloadInfo != NULL) {
930 config.offload_info = *offloadInfo;
931 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700932 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700933 &output,
934 &config,
935 &outputDesc->mDevice,
936 String8(""),
937 &outputDesc->mLatency,
938 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700939
940 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700941 if (status != NO_ERROR ||
942 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800943 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700944 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700945 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
946 "format %d %d, channelMask %04x %04x", output, samplingRate,
947 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
948 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700949 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700950 mpClientInterface->closeOutput(output);
951 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800952 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800953 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800954 goto non_direct_output;
955 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700956 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700957 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700958 outputDesc->mSamplingRate = config.sample_rate;
959 outputDesc->mChannelMask = config.channel_mask;
960 outputDesc->mFormat = config.format;
961 outputDesc->mRefCount[stream] = 0;
962 outputDesc->mStopTime[stream] = 0;
963 outputDesc->mDirectOpenCount = 1;
964
Eric Laurente552edb2014-03-10 17:42:56 -0700965 audio_io_handle_t srcOutput = getOutputForEffect();
966 addOutput(output, outputDesc);
967 audio_io_handle_t dstOutput = getOutputForEffect();
968 if (dstOutput == output) {
969 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
970 }
971 mPreviousOutputs = mOutputs;
972 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700973 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700974 return output;
975 }
976
Eric Laurentb732cf52014-09-24 19:08:21 -0700977non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700978
979 // A request for HW A/V sync cannot fallback to a mixed output because time
980 // stamps are embedded in audio data
981 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
982 return AUDIO_IO_HANDLE_NONE;
983 }
984
Eric Laurente552edb2014-03-10 17:42:56 -0700985 // ignoring channel mask due to downmix capability in mixer
986
987 // open a non direct output
988
989 // for non direct outputs, only PCM is supported
990 if (audio_is_linear_pcm(format)) {
991 // get which output is suitable for the specified stream. The actual
992 // routing change will happen when startOutput() will be called
993 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
994
Eric Laurent8838a382014-09-08 16:44:28 -0700995 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
996 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
997 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700998 }
999 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1000 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1001
Paul McLeanaa981192015-03-21 09:55:15 -07001002 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001003
1004 return output;
1005}
1006
Eric Laurente0720872014-03-11 09:30:41 -07001007audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001008 audio_output_flags_t flags,
1009 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001010{
1011 // select one output among several that provide a path to a particular device or set of
1012 // devices (the list was previously build by getOutputsForDevice()).
1013 // The priority is as follows:
1014 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001015 // 2: the output with the bit depth the closest to the requested one
1016 // 3: the primary output
1017 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001018
1019 if (outputs.size() == 0) {
1020 return 0;
1021 }
1022 if (outputs.size() == 1) {
1023 return outputs[0];
1024 }
1025
1026 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001027 audio_io_handle_t outputForFlags = 0;
1028 audio_io_handle_t outputForPrimary = 0;
1029 audio_io_handle_t outputForFormat = 0;
1030 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1031 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001032
1033 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001034 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001035 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001036 // if a valid format is specified, skip output if not compatible
1037 if (format != AUDIO_FORMAT_INVALID) {
1038 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001039 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001040 continue;
1041 }
1042 } else if (!audio_is_linear_pcm(format)) {
1043 continue;
1044 }
Eric Laurente6930022016-02-11 10:20:40 -08001045 if (AudioPort::isBetterFormatMatch(
1046 outputDesc->mFormat, bestFormat, format)) {
1047 outputForFormat = outputs[i];
1048 bestFormat = outputDesc->mFormat;
1049 }
Eric Laurent8838a382014-09-08 16:44:28 -07001050 }
1051
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001052 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001053 if (commonFlags >= maxCommonFlags) {
1054 if (commonFlags == maxCommonFlags) {
1055 if (AudioPort::isBetterFormatMatch(
1056 outputDesc->mFormat, bestFormatForFlags, format)) {
1057 outputForFlags = outputs[i];
1058 bestFormatForFlags = outputDesc->mFormat;
1059 }
1060 } else {
1061 outputForFlags = outputs[i];
1062 maxCommonFlags = commonFlags;
1063 bestFormatForFlags = outputDesc->mFormat;
1064 }
Eric Laurente552edb2014-03-10 17:42:56 -07001065 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1066 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001067 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001068 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001069 }
1070 }
1071 }
1072
Eric Laurente6930022016-02-11 10:20:40 -08001073 if (outputForFlags != 0) {
1074 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001075 }
Eric Laurente6930022016-02-11 10:20:40 -08001076 if (outputForFormat != 0) {
1077 return outputForFormat;
1078 }
1079 if (outputForPrimary != 0) {
1080 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001081 }
1082
1083 return outputs[0];
1084}
1085
Eric Laurente0720872014-03-11 09:30:41 -07001086status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001087 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001088 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001089{
Paul McLeanaa981192015-03-21 09:55:15 -07001090 ALOGV("startOutput() output %d, stream %d, session %d",
1091 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001092 ssize_t index = mOutputs.indexOfKey(output);
1093 if (index < 0) {
1094 ALOGW("startOutput() unknown output %d", output);
1095 return BAD_VALUE;
1096 }
1097
Eric Laurentc75307b2015-03-17 15:29:32 -07001098 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1099
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001100 // Routing?
1101 mOutputRoutes.incRouteActivity(session);
1102
Eric Laurentc75307b2015-03-17 15:29:32 -07001103 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001104 AudioMix *policyMix = NULL;
1105 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001106 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001107 policyMix = outputDesc->mPolicyMix;
1108 address = policyMix->mDeviceAddress.string();
1109 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1110 newDevice = policyMix->mDeviceType;
1111 } else {
1112 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1113 }
Eric Laurent493404d2015-04-21 15:07:36 -07001114 } else if (mOutputRoutes.hasRouteChanged(session)) {
1115 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001116 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001117 } else {
1118 newDevice = AUDIO_DEVICE_NONE;
1119 }
1120
1121 uint32_t delayMs = 0;
1122
Eric Laurentc40d9692016-04-13 19:14:13 -07001123 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001124
1125 if (status != NO_ERROR) {
1126 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001127 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001128 }
1129 // Automatically enable the remote submix input when output is started on a re routing mix
1130 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001131 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1132 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001133 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1134 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001135 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001136 "remote-submix");
1137 }
1138
1139 if (delayMs != 0) {
1140 usleep(delayMs * 1000);
1141 }
1142
1143 return status;
1144}
1145
1146status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1147 audio_stream_type_t stream,
1148 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001149 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001150 uint32_t *delayMs)
1151{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001152 // cannot start playback of STREAM_TTS if any other output is being used
1153 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001154
1155 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001156 if (stream == AUDIO_STREAM_TTS) {
1157 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001158 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001159 return INVALID_OPERATION;
1160 } else {
1161 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1162 }
1163 } else {
1164 // some playback other than beacon starts
1165 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1166 }
1167
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001168 // check active before incrementing usage count
1169 bool force = !outputDesc->isActive();
1170
Eric Laurente552edb2014-03-10 17:42:56 -07001171 // increment usage count for this stream on the requested output:
1172 // NOTE that the usage count is the same for duplicated output and hardware output which is
1173 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1174 outputDesc->changeRefCount(stream, 1);
1175
Eric Laurent493404d2015-04-21 15:07:36 -07001176 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001177 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001178 if (device == AUDIO_DEVICE_NONE) {
1179 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001180 }
Eric Laurente552edb2014-03-10 17:42:56 -07001181 routing_strategy strategy = getStrategy(stream);
1182 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001183 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1184 (beaconMuteLatency > 0);
1185 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001186 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001187 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001188 if (desc != outputDesc) {
1189 // force a device change if any other output is managed by the same hw
1190 // module and has a current device selection that differs from selected device.
1191 // In this case, the audio HAL must receive the new device selection so that it can
1192 // change the device currently selected by the other active output.
1193 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001194 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001195 force = true;
1196 }
1197 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001198 // a notification so that audio focus effect can propagate, or that a mute/unmute
1199 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001200 uint32_t latency = desc->latency();
1201 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1202 waitMs = latency;
1203 }
1204 }
1205 }
Eric Laurentc40d9692016-04-13 19:14:13 -07001206 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001207
1208 // handle special case for sonification while in call
1209 if (isInCall()) {
1210 handleIncallSonification(stream, true, false);
1211 }
1212
1213 // apply volume rules for current stream and device if necessary
1214 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001215 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001216 outputDesc,
1217 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001218
1219 // update the outputs if starting an output with a stream that can affect notification
1220 // routing
1221 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001222
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001223 // force reevaluating accessibility routing when ringtone or alarm starts
1224 if (strategy == STRATEGY_SONIFICATION) {
1225 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1226 }
Eric Laurente552edb2014-03-10 17:42:56 -07001227 }
1228 return NO_ERROR;
1229}
1230
1231
Eric Laurente0720872014-03-11 09:30:41 -07001232status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001233 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001234 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001235{
1236 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1237 ssize_t index = mOutputs.indexOfKey(output);
1238 if (index < 0) {
1239 ALOGW("stopOutput() unknown output %d", output);
1240 return BAD_VALUE;
1241 }
1242
Eric Laurentc75307b2015-03-17 15:29:32 -07001243 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001244
Eric Laurentc75307b2015-03-17 15:29:32 -07001245 if (outputDesc->mRefCount[stream] == 1) {
1246 // Automatically disable the remote submix input when output is stopped on a
1247 // re routing mix of type MIX_TYPE_RECORDERS
1248 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1249 outputDesc->mPolicyMix != NULL &&
1250 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1251 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1252 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001253 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001254 "remote-submix");
1255 }
1256 }
1257
1258 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001259 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001260 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001261 int activityCount = mOutputRoutes.decRouteActivity(session);
1262 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1263
1264 if (forceDeviceUpdate) {
1265 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1266 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001267 }
1268
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001269 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001270}
1271
1272status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001273 audio_stream_type_t stream,
1274 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001275{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001276 // always handle stream stop, check which stream type is stopping
1277 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1278
Eric Laurente552edb2014-03-10 17:42:56 -07001279 // handle special case for sonification while in call
1280 if (isInCall()) {
1281 handleIncallSonification(stream, false, false);
1282 }
1283
1284 if (outputDesc->mRefCount[stream] > 0) {
1285 // decrement usage count of this stream on the output
1286 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001287
Eric Laurente552edb2014-03-10 17:42:56 -07001288 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001289 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001290 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001291 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001292 // delay the device switch by twice the latency because stopOutput() is executed when
1293 // the track stop() command is received and at that time the audio track buffer can
1294 // still contain data that needs to be drained. The latency only covers the audio HAL
1295 // and kernel buffers. Also the latency does not always include additional delay in the
1296 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001297 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001298
1299 // force restoring the device selection on other active outputs if it differs from the
1300 // one being selected for this output
1301 for (size_t i = 0; i < mOutputs.size(); i++) {
1302 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001303 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001304 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001305 desc->isActive() &&
1306 outputDesc->sharesHwModuleWith(desc) &&
1307 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001308 setOutputDevice(desc,
1309 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001310 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001311 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001312 }
1313 }
1314 // update the outputs if stopping one with a stream that can affect notification routing
1315 handleNotificationRoutingForStream(stream);
1316 }
1317 return NO_ERROR;
1318 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001319 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001320 return INVALID_OPERATION;
1321 }
1322}
1323
Eric Laurente83b55d2014-11-14 10:06:21 -08001324void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001325 audio_stream_type_t stream __unused,
1326 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001327{
1328 ALOGV("releaseOutput() %d", output);
1329 ssize_t index = mOutputs.indexOfKey(output);
1330 if (index < 0) {
1331 ALOGW("releaseOutput() releasing unknown output %d", output);
1332 return;
1333 }
1334
1335#ifdef AUDIO_POLICY_TEST
1336 int testIndex = testOutputIndex(output);
1337 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001338 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001339 if (outputDesc->isActive()) {
1340 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001341 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001342 mTestOutputs[testIndex] = 0;
1343 }
1344 return;
1345 }
1346#endif //AUDIO_POLICY_TEST
1347
Paul McLeanaa981192015-03-21 09:55:15 -07001348 // Routing
1349 mOutputRoutes.removeRoute(session);
1350
Eric Laurentc75307b2015-03-17 15:29:32 -07001351 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001352 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001353 if (desc->mDirectOpenCount <= 0) {
1354 ALOGW("releaseOutput() invalid open count %d for output %d",
1355 desc->mDirectOpenCount, output);
1356 return;
1357 }
1358 if (--desc->mDirectOpenCount == 0) {
1359 closeOutput(output);
1360 // If effects where present on the output, audioflinger moved them to the primary
1361 // output by default: move them back to the appropriate output.
1362 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001363 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001364 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1365 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001366 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001367 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001368 }
1369 }
1370}
1371
1372
Eric Laurentcaf7f482014-11-25 17:50:47 -08001373status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1374 audio_io_handle_t *input,
1375 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001376 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001377 uint32_t samplingRate,
1378 audio_format_t format,
1379 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001380 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001381 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001382 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001383{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001384 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1385 "session %d, flags %#x",
1386 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001387
Eric Laurentcaf7f482014-11-25 17:50:47 -08001388 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001389 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001390 audio_devices_t device;
1391 // handle legacy remote submix case where the address was not always specified
1392 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001393 audio_source_t inputSource = attr->source;
1394 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001395 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001396
Eric Laurentc447ded2015-01-06 08:47:05 -08001397 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1398 inputSource = AUDIO_SOURCE_MIC;
1399 }
1400 halInputSource = inputSource;
1401
Paul McLean466dc8e2015-04-17 13:15:36 -06001402 // Explicit routing?
1403 sp<DeviceDescriptor> deviceDesc;
1404 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1405 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1406 deviceDesc = mAvailableInputDevices[i];
1407 break;
1408 }
1409 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001410 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001411
Eric Laurentc447ded2015-01-06 08:47:05 -08001412 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001413 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001414 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001415 if (ret != NO_ERROR) {
1416 return ret;
1417 }
1418 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001419 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1420 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001421 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001422 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001423 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001424 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001425 return BAD_VALUE;
1426 }
Eric Laurentc722f302014-12-10 11:21:49 -08001427 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001428 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001429 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1430 // there is an external policy, but this input is attached to a mix of recorders,
1431 // meaning it receives audio injected into the framework, so the recorder doesn't
1432 // know about it and is therefore considered "legacy"
1433 *inputType = API_INPUT_LEGACY;
1434 } else {
1435 // recording a mix of players defined by an external policy, we're rerouting for
1436 // an external policy
1437 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1438 }
Eric Laurentc722f302014-12-10 11:21:49 -08001439 } else if (audio_is_remote_submix_device(device)) {
1440 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001441 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001442 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1443 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001444 } else {
1445 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001446 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001447
Eric Laurent599c7582015-12-07 18:05:55 -08001448 }
1449
1450 *input = getInputForDevice(device, address, session, uid, inputSource,
1451 samplingRate, format, channelMask, flags,
1452 policyMix);
1453 if (*input == AUDIO_IO_HANDLE_NONE) {
1454 mInputRoutes.removeRoute(session);
1455 return INVALID_OPERATION;
1456 }
1457 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1458 return NO_ERROR;
1459}
1460
1461
1462audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1463 String8 address,
1464 audio_session_t session,
1465 uid_t uid,
1466 audio_source_t inputSource,
1467 uint32_t samplingRate,
1468 audio_format_t format,
1469 audio_channel_mask_t channelMask,
1470 audio_input_flags_t flags,
1471 AudioMix *policyMix)
1472{
1473 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1474 audio_source_t halInputSource = inputSource;
1475 bool isSoundTrigger = false;
1476
1477 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1478 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1479 if (index >= 0) {
1480 input = mSoundTriggerSessions.valueFor(session);
1481 isSoundTrigger = true;
1482 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1483 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1484 } else {
1485 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001486 }
1487 }
1488
Andy Hungf129b032015-04-07 13:45:50 -07001489 // find a compatible input profile (not necessarily identical in parameters)
1490 sp<IOProfile> profile;
1491 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001492 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001493 audio_format_t profileFormat = format;
1494 audio_channel_mask_t profileChannelMask = channelMask;
1495 audio_input_flags_t profileFlags = flags;
1496 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001497 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001498 profileSamplingRate, profileFormat, profileChannelMask,
1499 profileFlags);
1500 if (profile != 0) {
1501 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001502 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1503 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001504 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1505 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1506 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001507 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1508 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001509 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001510 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001511 }
Eric Laurente552edb2014-03-10 17:42:56 -07001512 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001513 // Pick input sampling rate if not specified by client
1514 if (samplingRate == 0) {
1515 samplingRate = profileSamplingRate;
1516 }
Eric Laurente552edb2014-03-10 17:42:56 -07001517
Eric Laurent322b4d22015-04-03 15:57:54 -07001518 if (profile->getModuleHandle() == 0) {
1519 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001520 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001521 }
1522
Eric Laurent599c7582015-12-07 18:05:55 -08001523 sp<AudioSession> audioSession = new AudioSession(session,
1524 inputSource,
1525 format,
1526 samplingRate,
1527 channelMask,
1528 flags,
1529 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001530 isSoundTrigger,
1531 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001532
Eric Laurent232f26f2016-02-17 18:06:27 -08001533// TODO enable input reuse
1534#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001535 // reuse an open input if possible
1536 for (size_t i = 0; i < mInputs.size(); i++) {
1537 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001538 // reuse input if it shares the same profile and same sound trigger attribute
1539 if (profile == desc->mProfile &&
1540 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001541
1542 sp<AudioSession> as = desc->getAudioSession(session);
1543 if (as != 0) {
1544 // do not allow unmatching properties on same session
1545 if (as->matches(audioSession)) {
1546 as->changeOpenCount(1);
1547 } else {
1548 ALOGW("getInputForDevice() record with different attributes"
1549 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001550 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001551 }
1552 } else {
1553 desc->addAudioSession(session, audioSession);
1554 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001555 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1556 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001557 }
1558 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001559#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001560
Eric Laurentcf2c0212014-07-25 16:20:43 -07001561 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001562 config.sample_rate = profileSamplingRate;
1563 config.channel_mask = profileChannelMask;
1564 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001565
Eric Laurent322b4d22015-04-03 15:57:54 -07001566 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001567 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001568 &config,
1569 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001570 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001571 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001572 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001573
1574 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001575 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001576 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001577 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001578 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001579 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1580 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001581 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001582 if (input != AUDIO_IO_HANDLE_NONE) {
1583 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001584 }
Eric Laurent599c7582015-12-07 18:05:55 -08001585 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001586 }
1587
Eric Laurent1f2f2232014-06-02 12:01:23 -07001588 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001589 inputDesc->mSamplingRate = profileSamplingRate;
1590 inputDesc->mFormat = profileFormat;
1591 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001592 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001593 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001594 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001595
Eric Laurent599c7582015-12-07 18:05:55 -08001596 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001597 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001598
Eric Laurent599c7582015-12-07 18:05:55 -08001599 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001600}
1601
Eric Laurent4dc68062014-07-28 17:26:49 -07001602status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001603 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001604{
1605 ALOGV("startInput() input %d", input);
1606 ssize_t index = mInputs.indexOfKey(input);
1607 if (index < 0) {
1608 ALOGW("startInput() unknown input %d", input);
1609 return BAD_VALUE;
1610 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001611 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001612
Eric Laurent599c7582015-12-07 18:05:55 -08001613 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1614 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001615 ALOGW("startInput() unknown session %d on input %d", session, input);
1616 return BAD_VALUE;
1617 }
1618
Eric Laurent232f26f2016-02-17 18:06:27 -08001619 // virtual input devices are compatible with other input devices
1620 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001621
Eric Laurent232f26f2016-02-17 18:06:27 -08001622 // for a non-virtual input device, check if there is another (non-virtual) active input
1623 audio_io_handle_t activeInput = mInputs.getActiveInput();
1624 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001625
Eric Laurent232f26f2016-02-17 18:06:27 -08001626 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1627 // otherwise the active input continues and the new input cannot be started.
1628 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1629 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1630 !activeDesc->hasPreemptedSession(session)) {
1631 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1632 //FIXME: consider all active sessions
1633 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1634 audio_session_t activeSession = activeSessions.keyAt(0);
1635 SortedVector<audio_session_t> sessions =
1636 activeDesc->getPreemptedSessions();
1637 sessions.add(activeSession);
1638 inputDesc->setPreemptedSessions(sessions);
1639 stopInput(activeInput, activeSession);
1640 releaseInput(activeInput, activeSession);
1641 } else {
1642 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1643 return INVALID_OPERATION;
1644 }
1645 }
1646
1647 // Do not allow capture if an active voice call is using a software patch and
1648 // the call TX source device is on the same HW module.
1649 // FIXME: would be better to refine to only inputs whose profile connects to the
1650 // call TX device but this information is not in the audio patch
1651 if (mCallTxPatch != 0 &&
1652 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1653 return INVALID_OPERATION;
1654 }
1655 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001656
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001657 // Routing?
1658 mInputRoutes.incRouteActivity(session);
1659
Eric Laurent232f26f2016-02-17 18:06:27 -08001660 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1661 // if input maps to a dynamic policy with an activity listener, notify of state change
1662 if ((inputDesc->mPolicyMix != NULL)
1663 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001664 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001665 MIX_STATE_MIXING);
1666 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001667
Eric Laurent232f26f2016-02-17 18:06:27 -08001668 if (mInputs.activeInputsCount() == 0) {
1669 SoundTrigger::setCaptureState(true);
1670 }
1671 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001672
Eric Laurent232f26f2016-02-17 18:06:27 -08001673 // automatically enable the remote submix output when input is started if not
1674 // used by a policy mix of type MIX_TYPE_RECORDERS
1675 // For remote submix (a virtual device), we open only one input per capture request.
1676 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1677 String8 address = String8("");
1678 if (inputDesc->mPolicyMix == NULL) {
1679 address = String8("0");
1680 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001681 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001682 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001683 if (address != "") {
1684 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1685 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1686 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001687 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001688 }
Eric Laurente552edb2014-03-10 17:42:56 -07001689 }
1690
Eric Laurent599c7582015-12-07 18:05:55 -08001691 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001692
Eric Laurent232f26f2016-02-17 18:06:27 -08001693 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001694 return NO_ERROR;
1695}
1696
Eric Laurent4dc68062014-07-28 17:26:49 -07001697status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1698 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001699{
1700 ALOGV("stopInput() input %d", input);
1701 ssize_t index = mInputs.indexOfKey(input);
1702 if (index < 0) {
1703 ALOGW("stopInput() unknown input %d", input);
1704 return BAD_VALUE;
1705 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001706 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001707
Eric Laurent599c7582015-12-07 18:05:55 -08001708 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001709 if (index < 0) {
1710 ALOGW("stopInput() unknown session %d on input %d", session, input);
1711 return BAD_VALUE;
1712 }
1713
Eric Laurent599c7582015-12-07 18:05:55 -08001714 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001715 ALOGW("stopInput() input %d already stopped", input);
1716 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001717 }
1718
Eric Laurent599c7582015-12-07 18:05:55 -08001719 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001720
1721 // Routing?
1722 mInputRoutes.decRouteActivity(session);
1723
Eric Laurent232f26f2016-02-17 18:06:27 -08001724 if (!inputDesc->isActive()) {
1725 // if input maps to a dynamic policy with an activity listener, notify of state change
1726 if ((inputDesc->mPolicyMix != NULL)
1727 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001728 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001729 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001730 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001731
1732 // automatically disable the remote submix output when input is stopped if not
1733 // used by a policy mix of type MIX_TYPE_RECORDERS
1734 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1735 String8 address = String8("");
1736 if (inputDesc->mPolicyMix == NULL) {
1737 address = String8("0");
1738 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001739 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001740 }
1741 if (address != "") {
1742 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1743 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1744 address, "remote-submix");
1745 }
1746 }
1747
1748 resetInputDevice(input);
1749
1750 if (mInputs.activeInputsCount() == 0) {
1751 SoundTrigger::setCaptureState(false);
1752 }
1753 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001754 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001755 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001756}
1757
Eric Laurent4dc68062014-07-28 17:26:49 -07001758void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1759 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001760{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001761
Eric Laurente552edb2014-03-10 17:42:56 -07001762 ALOGV("releaseInput() %d", input);
1763 ssize_t index = mInputs.indexOfKey(input);
1764 if (index < 0) {
1765 ALOGW("releaseInput() releasing unknown input %d", input);
1766 return;
1767 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001768
1769 // Routing
1770 mInputRoutes.removeRoute(session);
1771
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001772 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1773 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001774
Eric Laurent599c7582015-12-07 18:05:55 -08001775 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001776 if (index < 0) {
1777 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1778 return;
1779 }
Eric Laurent599c7582015-12-07 18:05:55 -08001780
1781 if (audioSession->openCount() == 0) {
1782 ALOGW("releaseInput() invalid open count %d on session %d",
1783 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001784 return;
1785 }
Eric Laurent599c7582015-12-07 18:05:55 -08001786
1787 if (audioSession->changeOpenCount(-1) == 0) {
1788 inputDesc->removeAudioSession(session);
1789 }
1790
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001791 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001792 ALOGV("releaseInput() exit > 0");
1793 return;
1794 }
1795
Eric Laurent05b90f82014-08-27 15:32:29 -07001796 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001797 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001798 ALOGV("releaseInput() exit");
1799}
1800
Eric Laurentd4692962014-05-05 18:13:44 -07001801void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001802 bool patchRemoved = false;
1803
Eric Laurentd4692962014-05-05 18:13:44 -07001804 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001805 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001806 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001807 if (patch_index >= 0) {
1808 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1809 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1810 mAudioPatches.removeItemsAt(patch_index);
1811 patchRemoved = true;
1812 }
Eric Laurentd4692962014-05-05 18:13:44 -07001813 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1814 }
1815 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001816 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001817 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001818
1819 if (patchRemoved) {
1820 mpClientInterface->onAudioPatchListUpdate();
1821 }
Eric Laurentd4692962014-05-05 18:13:44 -07001822}
1823
Eric Laurente0720872014-03-11 09:30:41 -07001824void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001825 int indexMin,
1826 int indexMax)
1827{
1828 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001829 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001830
1831 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001832 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1833 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001834 continue;
1835 }
1836 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001837 }
Eric Laurente552edb2014-03-10 17:42:56 -07001838}
1839
Eric Laurente0720872014-03-11 09:30:41 -07001840status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001841 int index,
1842 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001843{
1844
François Gaffied1ab2bd2015-12-02 18:20:06 +01001845 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1846 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001847 return BAD_VALUE;
1848 }
1849 if (!audio_is_output_device(device)) {
1850 return BAD_VALUE;
1851 }
1852
1853 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001854 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001855
Eric Laurent1fd372e2016-04-06 14:23:53 -07001856 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001857 stream, device, index);
1858
Eric Laurent28d09f02016-03-08 10:43:05 -08001859 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001860 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1861 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001862 continue;
1863 }
1864 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1865 }
Eric Laurente552edb2014-03-10 17:42:56 -07001866
Eric Laurent1fd372e2016-04-06 14:23:53 -07001867 // update volume on all outputs and streams matching the following:
1868 // - The requested stream (or a stream matching for volume control) is active on the output
1869 // - The device (or devices) selected by the strategy corresponding to this stream includes
1870 // the requested device
1871 // - For non default requested device, currently selected device on the output is either the
1872 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001873 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1874 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001875 status_t status = NO_ERROR;
1876 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001877 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1878 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001879 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1880 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001881 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001882 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001883 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1884 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001885 continue;
1886 }
Eric Laurent794fde22016-03-11 09:50:45 -08001887 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001888 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001889 if ((curStreamDevice & device) == 0) {
1890 continue;
1891 }
1892 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001893 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001894 curStreamDevice |= device;
1895 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1896 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1897 applyDefault = true;
1898 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001899
Eric Laurent1fd372e2016-04-06 14:23:53 -07001900 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001901 status_t volStatus =
1902 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1903 if (volStatus != NO_ERROR) {
1904 status = volStatus;
1905 }
1906 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001907 }
Eric Laurente552edb2014-03-10 17:42:56 -07001908 }
1909 return status;
1910}
1911
Eric Laurente0720872014-03-11 09:30:41 -07001912status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001913 int *index,
1914 audio_devices_t device)
1915{
1916 if (index == NULL) {
1917 return BAD_VALUE;
1918 }
1919 if (!audio_is_output_device(device)) {
1920 return BAD_VALUE;
1921 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001922 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001923 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001924 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001925 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1926 }
François Gaffiedfd74092015-03-19 12:10:59 +01001927 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001928
François Gaffied1ab2bd2015-12-02 18:20:06 +01001929 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001930 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1931 return NO_ERROR;
1932}
1933
Eric Laurente0720872014-03-11 09:30:41 -07001934audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001935 const SortedVector<audio_io_handle_t>& outputs)
1936{
1937 // select one output among several suitable for global effects.
1938 // The priority is as follows:
1939 // 1: An offloaded output. If the effect ends up not being offloadable,
1940 // AudioFlinger will invalidate the track and the offloaded output
1941 // will be closed causing the effect to be moved to a PCM output.
1942 // 2: A deep buffer output
1943 // 3: the first output in the list
1944
1945 if (outputs.size() == 0) {
1946 return 0;
1947 }
1948
1949 audio_io_handle_t outputOffloaded = 0;
1950 audio_io_handle_t outputDeepBuffer = 0;
1951
1952 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001953 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001954 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001955 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1956 outputOffloaded = outputs[i];
1957 }
1958 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1959 outputDeepBuffer = outputs[i];
1960 }
1961 }
1962
1963 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1964 outputOffloaded, outputDeepBuffer);
1965 if (outputOffloaded != 0) {
1966 return outputOffloaded;
1967 }
1968 if (outputDeepBuffer != 0) {
1969 return outputDeepBuffer;
1970 }
1971
1972 return outputs[0];
1973}
1974
Eric Laurente0720872014-03-11 09:30:41 -07001975audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001976{
1977 // apply simple rule where global effects are attached to the same output as MUSIC streams
1978
Eric Laurent3b73df72014-03-11 09:06:29 -07001979 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001980 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1981 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1982
1983 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1984 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1985 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1986
1987 return output;
1988}
1989
Eric Laurente0720872014-03-11 09:30:41 -07001990status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001991 audio_io_handle_t io,
1992 uint32_t strategy,
1993 int session,
1994 int id)
1995{
1996 ssize_t index = mOutputs.indexOfKey(io);
1997 if (index < 0) {
1998 index = mInputs.indexOfKey(io);
1999 if (index < 0) {
2000 ALOGW("registerEffect() unknown io %d", io);
2001 return INVALID_OPERATION;
2002 }
2003 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002004 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002005}
2006
Eric Laurentc75307b2015-03-17 15:29:32 -07002007bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2008{
Eric Laurent28d09f02016-03-08 10:43:05 -08002009 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002010 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2011 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002012 continue;
2013 }
2014 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2015 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002016 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002017}
2018
2019bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2020{
2021 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2022}
2023
Eric Laurente0720872014-03-11 09:30:41 -07002024bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002025{
2026 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002027 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002028 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002029 return true;
2030 }
2031 }
2032 return false;
2033}
2034
Eric Laurent275e8e92014-11-30 15:14:47 -08002035// Register a list of custom mixes with their attributes and format.
2036// When a mix is registered, corresponding input and output profiles are
2037// added to the remote submix hw module. The profile contains only the
2038// parameters (sampling rate, format...) specified by the mix.
2039// The corresponding input remote submix device is also connected.
2040//
2041// When a remote submix device is connected, the address is checked to select the
2042// appropriate profile and the corresponding input or output stream is opened.
2043//
2044// When capture starts, getInputForAttr() will:
2045// - 1 look for a mix matching the address passed in attribtutes tags if any
2046// - 2 if none found, getDeviceForInputSource() will:
2047// - 2.1 look for a mix matching the attributes source
2048// - 2.2 if none found, default to device selection by policy rules
2049// At this time, the corresponding output remote submix device is also connected
2050// and active playback use cases can be transferred to this mix if needed when reconnecting
2051// after AudioTracks are invalidated
2052//
2053// When playback starts, getOutputForAttr() will:
2054// - 1 look for a mix matching the address passed in attribtutes tags if any
2055// - 2 if none found, look for a mix matching the attributes usage
2056// - 3 if none found, default to device and output selection by policy rules.
2057
2058status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2059{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002060 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2061 status_t res = NO_ERROR;
2062
2063 sp<HwModule> rSubmixModule;
2064 // examine each mix's route type
2065 for (size_t i = 0; i < mixes.size(); i++) {
2066 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2067 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2068 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002069 break;
2070 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002071 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2072 // Loop back through "remote submix"
2073 if (rSubmixModule == 0) {
2074 for (size_t j = 0; i < mHwModules.size(); j++) {
2075 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2076 && mHwModules[j]->mHandle != 0) {
2077 rSubmixModule = mHwModules[j];
2078 break;
2079 }
2080 }
2081 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002082
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002083 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002084
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002085 if (rSubmixModule == 0) {
2086 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2087 res = INVALID_OPERATION;
2088 break;
2089 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002090
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002091 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002092
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002093 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002094 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002095 res = INVALID_OPERATION;
2096 break;
2097 }
2098 audio_config_t outputConfig = mixes[i].mFormat;
2099 audio_config_t inputConfig = mixes[i].mFormat;
2100 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2101 // stereo and let audio flinger do the channel conversion if needed.
2102 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2103 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2104 rSubmixModule->addOutputProfile(address, &outputConfig,
2105 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2106 rSubmixModule->addInputProfile(address, &inputConfig,
2107 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002108
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002109 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2110 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2111 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2112 address.string(), "remote-submix");
2113 } else {
2114 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2115 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2116 address.string(), "remote-submix");
2117 }
2118 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002119 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002120 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002121 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2122 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002123
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002124 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002125 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2126 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2127 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2128 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2129 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2130 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002131 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2132 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002133 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2134 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002135 } else {
2136 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002137 }
2138 break;
2139 }
2140 }
2141
2142 if (res != NO_ERROR) {
2143 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002144 i, device, address.string());
2145 res = INVALID_OPERATION;
2146 break;
2147 } else if (!foundOutput) {
2148 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2149 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002150 res = INVALID_OPERATION;
2151 break;
2152 }
Eric Laurentc722f302014-12-10 11:21:49 -08002153 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002154 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002155 if (res != NO_ERROR) {
2156 unregisterPolicyMixes(mixes);
2157 }
2158 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002159}
2160
2161status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2162{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002163 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002164 status_t res = NO_ERROR;
2165 sp<HwModule> rSubmixModule;
2166 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002167 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002168 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002169
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002170 if (rSubmixModule == 0) {
2171 for (size_t j = 0; i < mHwModules.size(); j++) {
2172 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2173 && mHwModules[j]->mHandle != 0) {
2174 rSubmixModule = mHwModules[j];
2175 break;
2176 }
2177 }
2178 }
2179 if (rSubmixModule == 0) {
2180 res = INVALID_OPERATION;
2181 continue;
2182 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002183
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002184 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002185
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002186 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2187 res = INVALID_OPERATION;
2188 continue;
2189 }
2190
2191 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2192 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2193 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2194 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2195 address.string(), "remote-submix");
2196 }
2197 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2198 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2199 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2200 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2201 address.string(), "remote-submix");
2202 }
2203 rSubmixModule->removeOutputProfile(address);
2204 rSubmixModule->removeInputProfile(address);
2205
2206 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2207 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2208 res = INVALID_OPERATION;
2209 continue;
2210 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002211 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002212 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002213 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002214}
2215
Eric Laurente552edb2014-03-10 17:42:56 -07002216
Eric Laurente0720872014-03-11 09:30:41 -07002217status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002218{
2219 const size_t SIZE = 256;
2220 char buffer[SIZE];
2221 String8 result;
2222
2223 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2224 result.append(buffer);
2225
Eric Laurent87ffa392015-05-22 10:32:38 -07002226 snprintf(buffer, SIZE, " Primary Output: %d\n",
2227 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002228 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002229 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002230 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002231 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002232 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002233 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002234 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002235 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002236 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002237 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002238 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002239 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002240 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002241 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002242 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002243 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002244 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002245 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2246 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2247 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002248 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2249 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002250 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2251 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002252
François Gaffie2110e042015-03-24 08:41:51 +01002253 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002254
François Gaffie112b0af2015-11-19 16:13:25 +01002255 mAvailableOutputDevices.dump(fd, String8("Available output"));
2256 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002257 mHwModules.dump(fd);
2258 mOutputs.dump(fd);
2259 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002260 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002261 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002262 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002263
2264 return NO_ERROR;
2265}
2266
2267// This function checks for the parameters which can be offloaded.
2268// This can be enhanced depending on the capability of the DSP and policy
2269// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002270bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002271{
2272 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002273 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002274 offloadInfo.sample_rate, offloadInfo.channel_mask,
2275 offloadInfo.format,
2276 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2277 offloadInfo.has_video);
2278
Andy Hung2ddee192015-12-18 17:34:44 -08002279 if (mMasterMono) {
2280 return false; // no offloading if mono is set.
2281 }
2282
Eric Laurente552edb2014-03-10 17:42:56 -07002283 // Check if offload has been disabled
2284 char propValue[PROPERTY_VALUE_MAX];
2285 if (property_get("audio.offload.disable", propValue, "0")) {
2286 if (atoi(propValue) != 0) {
2287 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2288 return false;
2289 }
2290 }
2291
2292 // Check if stream type is music, then only allow offload as of now.
2293 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2294 {
2295 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2296 return false;
2297 }
2298
2299 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002300 const bool allowOffloadWithVideo =
2301 property_get_bool("audio.offload.video", false /* default_value */);
2302 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002303 ALOGV("isOffloadSupported: has_video == true, returning false");
2304 return false;
2305 }
2306
2307 //If duration is less than minimum value defined in property, return false
2308 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2309 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2310 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2311 return false;
2312 }
2313 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2314 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2315 return false;
2316 }
2317
2318 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2319 // creating an offloaded track and tearing it down immediately after start when audioflinger
2320 // detects there is an active non offloadable effect.
2321 // FIXME: We should check the audio session here but we do not have it in this context.
2322 // This may prevent offloading in rare situations where effects are left active by apps
2323 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002324 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002325 return false;
2326 }
2327
2328 // See if there is a profile to support this.
2329 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002330 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002331 offloadInfo.sample_rate,
2332 offloadInfo.format,
2333 offloadInfo.channel_mask,
2334 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002335 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2336 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002337}
2338
Eric Laurent6a94d692014-05-20 11:18:06 -07002339status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2340 audio_port_type_t type,
2341 unsigned int *num_ports,
2342 struct audio_port *ports,
2343 unsigned int *generation)
2344{
2345 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2346 generation == NULL) {
2347 return BAD_VALUE;
2348 }
2349 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2350 if (ports == NULL) {
2351 *num_ports = 0;
2352 }
2353
2354 size_t portsWritten = 0;
2355 size_t portsMax = *num_ports;
2356 *num_ports = 0;
2357 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002358 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2359 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002360 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002361 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2362 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2363 continue;
2364 }
2365 if (portsWritten < portsMax) {
2366 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2367 }
2368 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002369 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002370 }
2371 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002372 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2373 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2374 continue;
2375 }
2376 if (portsWritten < portsMax) {
2377 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2378 }
2379 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002380 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002381 }
2382 }
2383 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2384 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2385 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2386 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2387 }
2388 *num_ports += mInputs.size();
2389 }
2390 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002391 size_t numOutputs = 0;
2392 for (size_t i = 0; i < mOutputs.size(); i++) {
2393 if (!mOutputs[i]->isDuplicated()) {
2394 numOutputs++;
2395 if (portsWritten < portsMax) {
2396 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2397 }
2398 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002399 }
Eric Laurent84c70242014-06-23 08:46:27 -07002400 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002401 }
2402 }
2403 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002404 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002405 return NO_ERROR;
2406}
2407
2408status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2409{
2410 return NO_ERROR;
2411}
2412
Eric Laurent6a94d692014-05-20 11:18:06 -07002413status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2414 audio_patch_handle_t *handle,
2415 uid_t uid)
2416{
2417 ALOGV("createAudioPatch()");
2418
2419 if (handle == NULL || patch == NULL) {
2420 return BAD_VALUE;
2421 }
2422 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2423
Eric Laurent874c42872014-08-08 15:13:39 -07002424 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2425 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2426 return BAD_VALUE;
2427 }
2428 // only one source per audio patch supported for now
2429 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 return INVALID_OPERATION;
2431 }
Eric Laurent874c42872014-08-08 15:13:39 -07002432
2433 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 return INVALID_OPERATION;
2435 }
Eric Laurent874c42872014-08-08 15:13:39 -07002436 for (size_t i = 0; i < patch->num_sinks; i++) {
2437 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2438 return INVALID_OPERATION;
2439 }
2440 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002441
2442 sp<AudioPatch> patchDesc;
2443 ssize_t index = mAudioPatches.indexOfKey(*handle);
2444
Eric Laurent6a94d692014-05-20 11:18:06 -07002445 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2446 patch->sources[0].role,
2447 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002448#if LOG_NDEBUG == 0
2449 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002450 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002451 patch->sinks[i].role,
2452 patch->sinks[i].type);
2453 }
2454#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002455
2456 if (index >= 0) {
2457 patchDesc = mAudioPatches.valueAt(index);
2458 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2459 mUidCached, patchDesc->mUid, uid);
2460 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2461 return INVALID_OPERATION;
2462 }
2463 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002464 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002465 }
2466
2467 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002468 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002469 if (outputDesc == NULL) {
2470 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2471 return BAD_VALUE;
2472 }
Eric Laurent84c70242014-06-23 08:46:27 -07002473 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2474 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002475 if (patchDesc != 0) {
2476 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2477 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2478 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2479 return BAD_VALUE;
2480 }
2481 }
Eric Laurent874c42872014-08-08 15:13:39 -07002482 DeviceVector devices;
2483 for (size_t i = 0; i < patch->num_sinks; i++) {
2484 // Only support mix to devices connection
2485 // TODO add support for mix to mix connection
2486 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2487 ALOGV("createAudioPatch() source mix but sink is not a device");
2488 return INVALID_OPERATION;
2489 }
2490 sp<DeviceDescriptor> devDesc =
2491 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2492 if (devDesc == 0) {
2493 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2494 return BAD_VALUE;
2495 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002496
François Gaffie53615e22015-03-19 09:24:12 +01002497 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002498 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002499 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002500 NULL, // updatedSamplingRate
2501 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002502 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002503 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002504 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002505 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002506 ALOGV("createAudioPatch() profile not supported for device %08x",
2507 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002508 return INVALID_OPERATION;
2509 }
2510 devices.add(devDesc);
2511 }
2512 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002513 return INVALID_OPERATION;
2514 }
Eric Laurent874c42872014-08-08 15:13:39 -07002515
Eric Laurent6a94d692014-05-20 11:18:06 -07002516 // TODO: reconfigure output format and channels here
2517 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002518 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002519 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002520 index = mAudioPatches.indexOfKey(*handle);
2521 if (index >= 0) {
2522 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2523 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2524 }
2525 patchDesc = mAudioPatches.valueAt(index);
2526 patchDesc->mUid = uid;
2527 ALOGV("createAudioPatch() success");
2528 } else {
2529 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2530 return INVALID_OPERATION;
2531 }
2532 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2533 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2534 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002535 // only one sink supported when connecting an input device to a mix
2536 if (patch->num_sinks > 1) {
2537 return INVALID_OPERATION;
2538 }
François Gaffie53615e22015-03-19 09:24:12 +01002539 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002540 if (inputDesc == NULL) {
2541 return BAD_VALUE;
2542 }
2543 if (patchDesc != 0) {
2544 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2545 return BAD_VALUE;
2546 }
2547 }
2548 sp<DeviceDescriptor> devDesc =
2549 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2550 if (devDesc == 0) {
2551 return BAD_VALUE;
2552 }
2553
François Gaffie53615e22015-03-19 09:24:12 +01002554 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002555 devDesc->mAddress,
2556 patch->sinks[0].sample_rate,
2557 NULL, /*updatedSampleRate*/
2558 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002559 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002560 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002561 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002562 // FIXME for the parameter type,
2563 // and the NONE
2564 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002565 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002566 return INVALID_OPERATION;
2567 }
2568 // TODO: reconfigure output format and channels here
2569 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002570 devDesc->type(), inputDesc->mIoHandle);
2571 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002572 index = mAudioPatches.indexOfKey(*handle);
2573 if (index >= 0) {
2574 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2575 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2576 }
2577 patchDesc = mAudioPatches.valueAt(index);
2578 patchDesc->mUid = uid;
2579 ALOGV("createAudioPatch() success");
2580 } else {
2581 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2582 return INVALID_OPERATION;
2583 }
2584 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2585 // device to device connection
2586 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002587 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002588 return BAD_VALUE;
2589 }
2590 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002591 sp<DeviceDescriptor> srcDeviceDesc =
2592 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002593 if (srcDeviceDesc == 0) {
2594 return BAD_VALUE;
2595 }
Eric Laurent874c42872014-08-08 15:13:39 -07002596
Eric Laurent6a94d692014-05-20 11:18:06 -07002597 //update source and sink with our own data as the data passed in the patch may
2598 // be incomplete.
2599 struct audio_patch newPatch = *patch;
2600 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002601
Eric Laurent874c42872014-08-08 15:13:39 -07002602 for (size_t i = 0; i < patch->num_sinks; i++) {
2603 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2604 ALOGV("createAudioPatch() source device but one sink is not a device");
2605 return INVALID_OPERATION;
2606 }
2607
2608 sp<DeviceDescriptor> sinkDeviceDesc =
2609 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2610 if (sinkDeviceDesc == 0) {
2611 return BAD_VALUE;
2612 }
2613 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2614
Eric Laurent3bcf8592015-04-03 12:13:24 -07002615 // create a software bridge in PatchPanel if:
2616 // - source and sink devices are on differnt HW modules OR
2617 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002618 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002619 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002620 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002621 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002622 return INVALID_OPERATION;
2623 }
Eric Laurent874c42872014-08-08 15:13:39 -07002624 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002625 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002626 // if the sink device is reachable via an opened output stream, request to go via
2627 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002628 audio_io_handle_t output = selectOutput(outputs,
2629 AUDIO_OUTPUT_FLAG_NONE,
2630 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002631 if (output != AUDIO_IO_HANDLE_NONE) {
2632 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2633 if (outputDesc->isDuplicated()) {
2634 return INVALID_OPERATION;
2635 }
2636 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002637 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002638 newPatch.num_sources = 2;
2639 }
Eric Laurent83b88082014-06-20 18:31:16 -07002640 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002641 }
2642 // TODO: check from routing capabilities in config file and other conflicting patches
2643
2644 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2645 if (index >= 0) {
2646 afPatchHandle = patchDesc->mAfPatchHandle;
2647 }
2648
2649 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2650 &afPatchHandle,
2651 0);
2652 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2653 status, afPatchHandle);
2654 if (status == NO_ERROR) {
2655 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002656 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002657 addAudioPatch(patchDesc->mHandle, patchDesc);
2658 } else {
2659 patchDesc->mPatch = newPatch;
2660 }
2661 patchDesc->mAfPatchHandle = afPatchHandle;
2662 *handle = patchDesc->mHandle;
2663 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002664 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002665 } else {
2666 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2667 status);
2668 return INVALID_OPERATION;
2669 }
2670 } else {
2671 return BAD_VALUE;
2672 }
2673 } else {
2674 return BAD_VALUE;
2675 }
2676 return NO_ERROR;
2677}
2678
2679status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2680 uid_t uid)
2681{
2682 ALOGV("releaseAudioPatch() patch %d", handle);
2683
2684 ssize_t index = mAudioPatches.indexOfKey(handle);
2685
2686 if (index < 0) {
2687 return BAD_VALUE;
2688 }
2689 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2690 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2691 mUidCached, patchDesc->mUid, uid);
2692 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2693 return INVALID_OPERATION;
2694 }
2695
2696 struct audio_patch *patch = &patchDesc->mPatch;
2697 patchDesc->mUid = mUidCached;
2698 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002699 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002700 if (outputDesc == NULL) {
2701 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2702 return BAD_VALUE;
2703 }
2704
Eric Laurentc75307b2015-03-17 15:29:32 -07002705 setOutputDevice(outputDesc,
2706 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002707 true,
2708 0,
2709 NULL);
2710 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2711 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002712 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002713 if (inputDesc == NULL) {
2714 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2715 return BAD_VALUE;
2716 }
2717 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002718 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002719 true,
2720 NULL);
2721 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2722 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2723 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2724 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2725 status, patchDesc->mAfPatchHandle);
2726 removeAudioPatch(patchDesc->mHandle);
2727 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002728 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002729 } else {
2730 return BAD_VALUE;
2731 }
2732 } else {
2733 return BAD_VALUE;
2734 }
2735 return NO_ERROR;
2736}
2737
2738status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2739 struct audio_patch *patches,
2740 unsigned int *generation)
2741{
François Gaffie53615e22015-03-19 09:24:12 +01002742 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002743 return BAD_VALUE;
2744 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002745 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002746 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002747}
2748
Eric Laurente1715a42014-05-20 11:30:42 -07002749status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002750{
Eric Laurente1715a42014-05-20 11:30:42 -07002751 ALOGV("setAudioPortConfig()");
2752
2753 if (config == NULL) {
2754 return BAD_VALUE;
2755 }
2756 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2757 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002758 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2759 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002760 }
2761
Eric Laurenta121f902014-06-03 13:32:54 -07002762 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002763 if (config->type == AUDIO_PORT_TYPE_MIX) {
2764 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002765 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002766 if (outputDesc == NULL) {
2767 return BAD_VALUE;
2768 }
Eric Laurent84c70242014-06-23 08:46:27 -07002769 ALOG_ASSERT(!outputDesc->isDuplicated(),
2770 "setAudioPortConfig() called on duplicated output %d",
2771 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002772 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002773 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002774 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002775 if (inputDesc == NULL) {
2776 return BAD_VALUE;
2777 }
Eric Laurenta121f902014-06-03 13:32:54 -07002778 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002779 } else {
2780 return BAD_VALUE;
2781 }
2782 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2783 sp<DeviceDescriptor> deviceDesc;
2784 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2785 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2786 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2787 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2788 } else {
2789 return BAD_VALUE;
2790 }
2791 if (deviceDesc == NULL) {
2792 return BAD_VALUE;
2793 }
Eric Laurenta121f902014-06-03 13:32:54 -07002794 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002795 } else {
2796 return BAD_VALUE;
2797 }
2798
Eric Laurenta121f902014-06-03 13:32:54 -07002799 struct audio_port_config backupConfig;
2800 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2801 if (status == NO_ERROR) {
2802 struct audio_port_config newConfig;
2803 audioPortConfig->toAudioPortConfig(&newConfig, config);
2804 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002805 }
Eric Laurenta121f902014-06-03 13:32:54 -07002806 if (status != NO_ERROR) {
2807 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002808 }
Eric Laurente1715a42014-05-20 11:30:42 -07002809
2810 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002811}
2812
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002813void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2814{
Eric Laurentd60560a2015-04-10 11:31:20 -07002815 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002816 clearAudioPatches(uid);
2817 clearSessionRoutes(uid);
2818}
2819
Eric Laurent6a94d692014-05-20 11:18:06 -07002820void AudioPolicyManager::clearAudioPatches(uid_t uid)
2821{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002822 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002823 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2824 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002825 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002826 }
2827 }
2828}
2829
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002830void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2831 audio_io_handle_t ouptutToSkip)
2832{
2833 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2834 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2835 for (size_t j = 0; j < mOutputs.size(); j++) {
2836 if (mOutputs.keyAt(j) == ouptutToSkip) {
2837 continue;
2838 }
2839 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2840 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2841 continue;
2842 }
2843 // If the default device for this strategy is on another output mix,
2844 // invalidate all tracks in this strategy to force re connection.
2845 // Otherwise select new device on the output mix.
2846 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002847 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002848 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2849 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2850 }
2851 }
2852 } else {
2853 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2854 setOutputDevice(outputDesc, newDevice, false);
2855 }
2856 }
2857}
2858
2859void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2860{
2861 // remove output routes associated with this uid
2862 SortedVector<routing_strategy> affectedStrategies;
2863 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2864 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2865 if (route->mUid == uid) {
2866 mOutputRoutes.removeItemsAt(i);
2867 if (route->mDeviceDescriptor != 0) {
2868 affectedStrategies.add(getStrategy(route->mStreamType));
2869 }
2870 }
2871 }
2872 // reroute outputs if necessary
2873 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2874 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2875 }
2876
2877 // remove input routes associated with this uid
2878 SortedVector<audio_source_t> affectedSources;
2879 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2880 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2881 if (route->mUid == uid) {
2882 mInputRoutes.removeItemsAt(i);
2883 if (route->mDeviceDescriptor != 0) {
2884 affectedSources.add(route->mSource);
2885 }
2886 }
2887 }
2888 // reroute inputs if necessary
2889 SortedVector<audio_io_handle_t> inputsToClose;
2890 for (size_t i = 0; i < mInputs.size(); i++) {
2891 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002892 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002893 inputsToClose.add(inputDesc->mIoHandle);
2894 }
2895 }
2896 for (size_t i = 0; i < inputsToClose.size(); i++) {
2897 closeInput(inputsToClose[i]);
2898 }
2899}
2900
Eric Laurentd60560a2015-04-10 11:31:20 -07002901void AudioPolicyManager::clearAudioSources(uid_t uid)
2902{
2903 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2904 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2905 if (sourceDesc->mUid == uid) {
2906 stopAudioSource(mAudioSources.keyAt(i));
2907 }
2908 }
2909}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002910
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002911status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2912 audio_io_handle_t *ioHandle,
2913 audio_devices_t *device)
2914{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002915 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2916 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002917 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002918
François Gaffiedf372692015-03-19 10:43:27 +01002919 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002920}
2921
Eric Laurentd60560a2015-04-10 11:31:20 -07002922status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2923 const audio_attributes_t *attributes,
2924 audio_io_handle_t *handle,
2925 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002926{
Eric Laurentd60560a2015-04-10 11:31:20 -07002927 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2928 if (source == NULL || attributes == NULL || handle == NULL) {
2929 return BAD_VALUE;
2930 }
2931
2932 *handle = AUDIO_IO_HANDLE_NONE;
2933
2934 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2935 source->type != AUDIO_PORT_TYPE_DEVICE) {
2936 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2937 return INVALID_OPERATION;
2938 }
2939
2940 sp<DeviceDescriptor> srcDeviceDesc =
2941 mAvailableInputDevices.getDevice(source->ext.device.type,
2942 String8(source->ext.device.address));
2943 if (srcDeviceDesc == 0) {
2944 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2945 return BAD_VALUE;
2946 }
2947 sp<AudioSourceDescriptor> sourceDesc =
2948 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2949
2950 struct audio_patch dummyPatch;
2951 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2952 sourceDesc->mPatchDesc = patchDesc;
2953
2954 status_t status = connectAudioSource(sourceDesc);
2955 if (status == NO_ERROR) {
2956 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2957 *handle = sourceDesc->getHandle();
2958 }
2959 return status;
2960}
2961
2962status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2963{
2964 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2965
2966 // make sure we only have one patch per source.
2967 disconnectAudioSource(sourceDesc);
2968
2969 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002970 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002971 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2972
2973 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2974 sp<DeviceDescriptor> sinkDeviceDesc =
2975 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2976
2977 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2978 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2979
2980 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2981 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002982 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002983 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2984 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2985 // create patch between src device and output device
2986 // create Hwoutput and add to mHwOutputs
2987 } else {
2988 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2989 audio_io_handle_t output =
2990 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2991 if (output == AUDIO_IO_HANDLE_NONE) {
2992 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2993 return INVALID_OPERATION;
2994 }
2995 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2996 if (outputDesc->isDuplicated()) {
2997 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2998 return INVALID_OPERATION;
2999 }
3000 // create a special patch with no sink and two sources:
3001 // - the second source indicates to PatchPanel through which output mix this patch should
3002 // be connected as well as the stream type for volume control
3003 // - the sink is defined by whatever output device is currently selected for the output
3004 // though which this patch is routed.
3005 patch->num_sinks = 0;
3006 patch->num_sources = 2;
3007 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3008 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3009 patch->sources[1].ext.mix.usecase.stream = stream;
3010 status_t status = mpClientInterface->createAudioPatch(patch,
3011 &afPatchHandle,
3012 0);
3013 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3014 status, afPatchHandle);
3015 if (status != NO_ERROR) {
3016 ALOGW("%s patch panel could not connect device patch, error %d",
3017 __FUNCTION__, status);
3018 return INVALID_OPERATION;
3019 }
3020 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003021 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003022
3023 if (status != NO_ERROR) {
3024 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3025 return status;
3026 }
3027 sourceDesc->mSwOutput = outputDesc;
3028 if (delayMs != 0) {
3029 usleep(delayMs * 1000);
3030 }
3031 }
3032
3033 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3034 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3035
3036 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003037}
3038
Eric Laurent493404d2015-04-21 15:07:36 -07003039status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003040{
Eric Laurentd60560a2015-04-10 11:31:20 -07003041 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3042 ALOGV("%s handle %d", __FUNCTION__, handle);
3043 if (sourceDesc == 0) {
3044 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3045 return BAD_VALUE;
3046 }
3047 status_t status = disconnectAudioSource(sourceDesc);
3048
3049 mAudioSources.removeItem(handle);
3050 return status;
3051}
3052
Andy Hung2ddee192015-12-18 17:34:44 -08003053status_t AudioPolicyManager::setMasterMono(bool mono)
3054{
3055 if (mMasterMono == mono) {
3056 return NO_ERROR;
3057 }
3058 mMasterMono = mono;
3059 // if enabling mono we close all offloaded devices, which will invalidate the
3060 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3061 // for recreating the new AudioTrack as non-offloaded PCM.
3062 //
3063 // If disabling mono, we leave all tracks as is: we don't know which clients
3064 // and tracks are able to be recreated as offloaded. The next "song" should
3065 // play back offloaded.
3066 if (mMasterMono) {
3067 Vector<audio_io_handle_t> offloaded;
3068 for (size_t i = 0; i < mOutputs.size(); ++i) {
3069 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3070 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3071 offloaded.push(desc->mIoHandle);
3072 }
3073 }
3074 for (size_t i = 0; i < offloaded.size(); ++i) {
3075 closeOutput(offloaded[i]);
3076 }
3077 }
3078 // update master mono for all remaining outputs
3079 for (size_t i = 0; i < mOutputs.size(); ++i) {
3080 updateMono(mOutputs.keyAt(i));
3081 }
3082 return NO_ERROR;
3083}
3084
3085status_t AudioPolicyManager::getMasterMono(bool *mono)
3086{
3087 *mono = mMasterMono;
3088 return NO_ERROR;
3089}
3090
Eric Laurentd60560a2015-04-10 11:31:20 -07003091status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3092{
3093 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3094
3095 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3096 if (patchDesc == 0) {
3097 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3098 sourceDesc->mPatchDesc->mHandle);
3099 return BAD_VALUE;
3100 }
3101 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3102
Eric Laurent28d09f02016-03-08 10:43:05 -08003103 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003104 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3105 if (swOutputDesc != 0) {
3106 stopSource(swOutputDesc, stream, false);
3107 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3108 } else {
3109 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3110 if (hwOutputDesc != 0) {
3111 // release patch between src device and output device
3112 // close Hwoutput and remove from mHwOutputs
3113 } else {
3114 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3115 }
3116 }
3117 return NO_ERROR;
3118}
3119
3120sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3121 audio_io_handle_t output, routing_strategy strategy)
3122{
3123 sp<AudioSourceDescriptor> source;
3124 for (size_t i = 0; i < mAudioSources.size(); i++) {
3125 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3126 routing_strategy sourceStrategy =
3127 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3128 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3129 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3130 source = sourceDesc;
3131 break;
3132 }
3133 }
3134 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003135}
3136
Eric Laurente552edb2014-03-10 17:42:56 -07003137// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003138// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003139// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003140uint32_t AudioPolicyManager::nextAudioPortGeneration()
3141{
3142 return android_atomic_inc(&mAudioPortGeneration);
3143}
3144
Eric Laurente0720872014-03-11 09:30:41 -07003145AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003146 :
3147#ifdef AUDIO_POLICY_TEST
3148 Thread(false),
3149#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003150 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003151 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003152 mAudioPortGeneration(1),
3153 mBeaconMuteRefCount(0),
3154 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003155 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003156 mTtsOutputAvailable(false),
3157 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003158{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003159 mUidCached = getuid();
3160 mpClientInterface = clientInterface;
3161
3162 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3163 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3164 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3165 bool speakerDrcEnabled = false;
3166
3167#ifdef USE_XML_AUDIO_POLICY_CONF
3168 mVolumeCurves = new VolumeCurvesCollection();
3169 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3170 mDefaultOutputDevice, speakerDrcEnabled,
3171 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3172 PolicySerializer serializer;
3173 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3174#else
3175 mVolumeCurves = new StreamDescriptorCollection();
3176 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3177 mDefaultOutputDevice, speakerDrcEnabled);
3178 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3179 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3180#endif
3181 ALOGE("could not load audio policy configuration file, setting defaults");
3182 config.setDefault();
3183 }
3184 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3185 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3186
3187 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003188 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3189 if (!engineInstance) {
3190 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3191 return;
3192 }
3193 // Retrieve the Policy Manager Interface
3194 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3195 if (mEngine == NULL) {
3196 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3197 return;
3198 }
3199 mEngine->setObserver(this);
3200 status_t status = mEngine->initCheck();
3201 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3202
Eric Laurent3a4311c2014-03-17 12:00:47 -07003203 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003204 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003205 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3206 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003207 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003208 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003209 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003210 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003211 continue;
3212 }
3213 // open all output streams needed to access attached devices
3214 // except for direct output streams that are only opened when they are actually
3215 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003216 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003217 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3218 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003219 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003220
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003221 if (!outProfile->hasSupportedDevices()) {
3222 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003223 continue;
3224 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003225 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003226 mTtsOutputAvailable = true;
3227 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003228
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003229 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003230 continue;
3231 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003232 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003233 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3234 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003235 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003236 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003237 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003238 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003239 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003240 if ((profileType & outputDeviceTypes) == 0) {
3241 continue;
3242 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003243 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3244 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003245 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3246 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3247 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3248 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003249
3250 outputDesc->mDevice = profileType;
3251 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3252 config.sample_rate = outputDesc->mSamplingRate;
3253 config.channel_mask = outputDesc->mChannelMask;
3254 config.format = outputDesc->mFormat;
3255 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003256 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003257 &output,
3258 &config,
3259 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003260 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003261 &outputDesc->mLatency,
3262 outputDesc->mFlags);
3263
3264 if (status != NO_ERROR) {
3265 ALOGW("Cannot open output stream for device %08x on hw module %s",
3266 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003267 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003268 } else {
3269 outputDesc->mSamplingRate = config.sample_rate;
3270 outputDesc->mChannelMask = config.channel_mask;
3271 outputDesc->mFormat = config.format;
3272
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003273 for (size_t k = 0; k < supportedDevices.size(); k++) {
3274 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003275 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003276 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3277 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003278 }
3279 }
3280 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003281 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003282 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003283 }
3284 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003285 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003286 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003287 true,
3288 0,
3289 NULL,
3290 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003291 }
Eric Laurente552edb2014-03-10 17:42:56 -07003292 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003293 // open input streams needed to access attached devices to validate
3294 // mAvailableInputDevices list
3295 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3296 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003297 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003298
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003299 if (!inProfile->hasSupportedDevices()) {
3300 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003301 continue;
3302 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003303 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003304 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003305 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3306
Eric Laurentd78f1532014-09-16 16:38:20 -07003307 if ((profileType & inputDeviceTypes) == 0) {
3308 continue;
3309 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003310 sp<AudioInputDescriptor> inputDesc =
3311 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003312
Eric Laurentd78f1532014-09-16 16:38:20 -07003313 inputDesc->mDevice = profileType;
3314
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003315 // find the address
3316 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3317 // the inputs vector must be of size 1, but we don't want to crash here
3318 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3319 : String8("");
3320 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3321 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3322
Eric Laurentd78f1532014-09-16 16:38:20 -07003323 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3324 config.sample_rate = inputDesc->mSamplingRate;
3325 config.channel_mask = inputDesc->mChannelMask;
3326 config.format = inputDesc->mFormat;
3327 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003328 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003329 &input,
3330 &config,
3331 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003332 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003333 AUDIO_SOURCE_MIC,
3334 AUDIO_INPUT_FLAG_NONE);
3335
3336 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003337 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3338 for (size_t k = 0; k < supportedDevices.size(); k++) {
3339 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003340 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003341 if (index >= 0) {
3342 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3343 if (!devDesc->isAttached()) {
3344 devDesc->attach(mHwModules[i]);
3345 devDesc->importAudioPort(inProfile);
3346 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003347 }
3348 }
3349 mpClientInterface->closeInput(input);
3350 } else {
3351 ALOGW("Cannot open input stream for device %08x on hw module %s",
3352 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003353 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003354 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003355 }
3356 }
3357 // make sure all attached devices have been allocated a unique ID
3358 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003359 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003360 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003361 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3362 continue;
3363 }
François Gaffie2110e042015-03-24 08:41:51 +01003364 // The device is now validated and can be appended to the available devices of the engine
3365 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3366 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003367 i++;
3368 }
3369 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003370 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003371 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003372 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3373 continue;
3374 }
François Gaffie2110e042015-03-24 08:41:51 +01003375 // The device is now validated and can be appended to the available devices of the engine
3376 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3377 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003378 i++;
3379 }
3380 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003381 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003382 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003383 }
Eric Laurente552edb2014-03-10 17:42:56 -07003384
3385 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3386
3387 updateDevicesAndOutputs();
3388
3389#ifdef AUDIO_POLICY_TEST
3390 if (mPrimaryOutput != 0) {
3391 AudioParameter outputCmd = AudioParameter();
3392 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003393 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003394
3395 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3396 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003397 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3398 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003399 mTestLatencyMs = 0;
3400 mCurOutput = 0;
3401 mDirectOutput = false;
3402 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3403 mTestOutputs[i] = 0;
3404 }
3405
3406 const size_t SIZE = 256;
3407 char buffer[SIZE];
3408 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3409 run(buffer, ANDROID_PRIORITY_AUDIO);
3410 }
3411#endif //AUDIO_POLICY_TEST
3412}
3413
Eric Laurente0720872014-03-11 09:30:41 -07003414AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003415{
3416#ifdef AUDIO_POLICY_TEST
3417 exit();
3418#endif //AUDIO_POLICY_TEST
3419 for (size_t i = 0; i < mOutputs.size(); i++) {
3420 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003421 }
3422 for (size_t i = 0; i < mInputs.size(); i++) {
3423 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003424 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003425 mAvailableOutputDevices.clear();
3426 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003427 mOutputs.clear();
3428 mInputs.clear();
3429 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003430}
3431
Eric Laurente0720872014-03-11 09:30:41 -07003432status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003433{
Eric Laurent87ffa392015-05-22 10:32:38 -07003434 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003435}
3436
3437#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003438bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003439{
3440 ALOGV("entering threadLoop()");
3441 while (!exitPending())
3442 {
3443 String8 command;
3444 int valueInt;
3445 String8 value;
3446
3447 Mutex::Autolock _l(mLock);
3448 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3449
3450 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3451 AudioParameter param = AudioParameter(command);
3452
3453 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3454 valueInt != 0) {
3455 ALOGV("Test command %s received", command.string());
3456 String8 target;
3457 if (param.get(String8("target"), target) != NO_ERROR) {
3458 target = "Manager";
3459 }
3460 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3461 param.remove(String8("test_cmd_policy_output"));
3462 mCurOutput = valueInt;
3463 }
3464 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3465 param.remove(String8("test_cmd_policy_direct"));
3466 if (value == "false") {
3467 mDirectOutput = false;
3468 } else if (value == "true") {
3469 mDirectOutput = true;
3470 }
3471 }
3472 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3473 param.remove(String8("test_cmd_policy_input"));
3474 mTestInput = valueInt;
3475 }
3476
3477 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3478 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003479 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003480 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003481 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003482 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003483 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003484 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003485 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003486 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003487 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003488 if (target == "Manager") {
3489 mTestFormat = format;
3490 } else if (mTestOutputs[mCurOutput] != 0) {
3491 AudioParameter outputParam = AudioParameter();
3492 outputParam.addInt(String8("format"), format);
3493 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3494 }
3495 }
3496 }
3497 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3498 param.remove(String8("test_cmd_policy_channels"));
3499 int channels = 0;
3500
3501 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003502 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003503 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003504 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003505 }
3506 if (channels != 0) {
3507 if (target == "Manager") {
3508 mTestChannels = channels;
3509 } else if (mTestOutputs[mCurOutput] != 0) {
3510 AudioParameter outputParam = AudioParameter();
3511 outputParam.addInt(String8("channels"), channels);
3512 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3513 }
3514 }
3515 }
3516 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3517 param.remove(String8("test_cmd_policy_sampleRate"));
3518 if (valueInt >= 0 && valueInt <= 96000) {
3519 int samplingRate = valueInt;
3520 if (target == "Manager") {
3521 mTestSamplingRate = samplingRate;
3522 } else if (mTestOutputs[mCurOutput] != 0) {
3523 AudioParameter outputParam = AudioParameter();
3524 outputParam.addInt(String8("sampling_rate"), samplingRate);
3525 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3526 }
3527 }
3528 }
3529
3530 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3531 param.remove(String8("test_cmd_policy_reopen"));
3532
Eric Laurentc75307b2015-03-17 15:29:32 -07003533 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003534
Eric Laurentc75307b2015-03-17 15:29:32 -07003535 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003536
Eric Laurentc75307b2015-03-17 15:29:32 -07003537 removeOutput(mPrimaryOutput->mIoHandle);
3538 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3539 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003540 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003541 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3542 config.sample_rate = outputDesc->mSamplingRate;
3543 config.channel_mask = outputDesc->mChannelMask;
3544 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003545 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003546 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003547 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003548 &config,
3549 &outputDesc->mDevice,
3550 String8(""),
3551 &outputDesc->mLatency,
3552 outputDesc->mFlags);
3553 if (status != NO_ERROR) {
3554 ALOGE("Failed to reopen hardware output stream, "
3555 "samplingRate: %d, format %d, channels %d",
3556 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003557 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003558 outputDesc->mSamplingRate = config.sample_rate;
3559 outputDesc->mChannelMask = config.channel_mask;
3560 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003561 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003562 AudioParameter outputCmd = AudioParameter();
3563 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003564 mpClientInterface->setParameters(handle, outputCmd.toString());
3565 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003566 }
3567 }
3568
3569
3570 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3571 }
3572 }
3573 return false;
3574}
3575
Eric Laurente0720872014-03-11 09:30:41 -07003576void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003577{
3578 {
3579 AutoMutex _l(mLock);
3580 requestExit();
3581 mWaitWorkCV.signal();
3582 }
3583 requestExitAndWait();
3584}
3585
Eric Laurente0720872014-03-11 09:30:41 -07003586int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003587{
3588 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3589 if (output == mTestOutputs[i]) return i;
3590 }
3591 return 0;
3592}
3593#endif //AUDIO_POLICY_TEST
3594
3595// ---
3596
Eric Laurentc75307b2015-03-17 15:29:32 -07003597void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003598{
François Gaffie98cc1912015-03-18 17:52:40 +01003599 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003600 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003601 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003602 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003603}
3604
François Gaffie53615e22015-03-19 09:24:12 +01003605void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3606{
3607 mOutputs.removeItem(output);
3608}
3609
Eric Laurent1f2f2232014-06-02 12:01:23 -07003610void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003611{
François Gaffie98cc1912015-03-18 17:52:40 +01003612 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003613 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003614 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003615}
Eric Laurente552edb2014-03-10 17:42:56 -07003616
Eric Laurentc75307b2015-03-17 15:29:32 -07003617void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003618 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003619 const String8 address /*in*/,
3620 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003621 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003622 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003623 if (devDesc != 0) {
3624 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3625 desc->mIoHandle, address.string());
3626 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003627 }
3628}
3629
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003630status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003631 audio_policy_dev_state_t state,
3632 SortedVector<audio_io_handle_t>& outputs,
3633 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003634{
François Gaffie53615e22015-03-19 09:24:12 +01003635 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003636 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003637
3638 if (audio_device_is_digital(device)) {
3639 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003640 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003641 }
Eric Laurente552edb2014-03-10 17:42:56 -07003642
Eric Laurent3b73df72014-03-11 09:06:29 -07003643 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003644 // first list already open outputs that can be routed to this device
3645 for (size_t i = 0; i < mOutputs.size(); i++) {
3646 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003647 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003648 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003649 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3650 outputs.add(mOutputs.keyAt(i));
3651 } else {
3652 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003653 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003654 }
Eric Laurente552edb2014-03-10 17:42:56 -07003655 }
3656 }
3657 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003658 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003659 for (size_t i = 0; i < mHwModules.size(); i++)
3660 {
3661 if (mHwModules[i]->mHandle == 0) {
3662 continue;
3663 }
3664 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3665 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003666 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003667 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003668 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003669 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003670 profiles.add(profile);
3671 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3672 }
Eric Laurente552edb2014-03-10 17:42:56 -07003673 }
3674 }
3675 }
3676
Eric Laurent7b279bb2015-12-14 10:18:23 -08003677 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003678
Eric Laurente552edb2014-03-10 17:42:56 -07003679 if (profiles.isEmpty() && outputs.isEmpty()) {
3680 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3681 return BAD_VALUE;
3682 }
3683
3684 // open outputs for matching profiles if needed. Direct outputs are also opened to
3685 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3686 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003687 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003688
3689 // nothing to do if one output is already opened for this profile
3690 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003691 for (j = 0; j < outputs.size(); j++) {
3692 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003693 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003694 // matching profile: save the sample rates, format and channel masks supported
3695 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003696 if (audio_device_is_digital(device)) {
3697 devDesc->importAudioPort(profile);
3698 }
Eric Laurente552edb2014-03-10 17:42:56 -07003699 break;
3700 }
3701 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003702 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003703 continue;
3704 }
3705
Eric Laurent83b88082014-06-20 18:31:16 -07003706 ALOGV("opening output for device %08x with params %s profile %p",
3707 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003708 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003709 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003710 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3711 config.sample_rate = desc->mSamplingRate;
3712 config.channel_mask = desc->mChannelMask;
3713 config.format = desc->mFormat;
3714 config.offload_info.sample_rate = desc->mSamplingRate;
3715 config.offload_info.channel_mask = desc->mChannelMask;
3716 config.offload_info.format = desc->mFormat;
3717 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003718 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003719 &output,
3720 &config,
3721 &desc->mDevice,
3722 address,
3723 &desc->mLatency,
3724 desc->mFlags);
3725 if (status == NO_ERROR) {
3726 desc->mSamplingRate = config.sample_rate;
3727 desc->mChannelMask = config.channel_mask;
3728 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003729
Eric Laurentd4692962014-05-05 18:13:44 -07003730 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003731 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003732 char *param = audio_device_address_to_parameter(device, address);
3733 mpClientInterface->setParameters(output, String8(param));
3734 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003735 }
Phil Burk00eeb322016-03-31 12:41:00 -07003736 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003737 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003738 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003739 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003740 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003741 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003742 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003743 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003744 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003745 config.offload_info.sample_rate = config.sample_rate;
3746 config.offload_info.channel_mask = config.channel_mask;
3747 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003748 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003749 &output,
3750 &config,
3751 &desc->mDevice,
3752 address,
3753 &desc->mLatency,
3754 desc->mFlags);
3755 if (status == NO_ERROR) {
3756 desc->mSamplingRate = config.sample_rate;
3757 desc->mChannelMask = config.channel_mask;
3758 desc->mFormat = config.format;
3759 } else {
3760 output = AUDIO_IO_HANDLE_NONE;
3761 }
Eric Laurentd4692962014-05-05 18:13:44 -07003762 }
3763
Eric Laurentcf2c0212014-07-25 16:20:43 -07003764 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003765 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003766 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003767 sp<AudioPolicyMix> policyMix;
3768 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003769 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3770 address.string());
3771 }
François Gaffie036e1e92015-03-19 10:16:24 +01003772 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003773 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003774
Eric Laurent87ffa392015-05-22 10:32:38 -07003775 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3776 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003777 // no duplicated output for direct outputs and
3778 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003779 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003780
Eric Laurentd4692962014-05-05 18:13:44 -07003781 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003782 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003783
Eric Laurentd4692962014-05-05 18:13:44 -07003784 //TODO: configure audio effect output stage here
3785
3786 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003787 duplicatedOutput =
3788 mpClientInterface->openDuplicateOutput(output,
3789 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003790 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003791 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003792 sp<SwAudioOutputDescriptor> dupOutputDesc =
3793 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3794 dupOutputDesc->mOutput1 = mPrimaryOutput;
3795 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003796 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3797 dupOutputDesc->mFormat = desc->mFormat;
3798 dupOutputDesc->mChannelMask = desc->mChannelMask;
3799 dupOutputDesc->mLatency = desc->mLatency;
3800 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003801 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003802 } else {
3803 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003804 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003805 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003806 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003807 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003808 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003809 }
Eric Laurente552edb2014-03-10 17:42:56 -07003810 }
3811 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003812 } else {
3813 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003814 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003815 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003816 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003817 profiles.removeAt(profile_index);
3818 profile_index--;
3819 } else {
3820 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003821 // Load digital format info only for digital devices
3822 if (audio_device_is_digital(device)) {
3823 devDesc->importAudioPort(profile);
3824 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003825
François Gaffie53615e22015-03-19 09:24:12 +01003826 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003827 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3828 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003829 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003830 NULL/*patch handle*/, address.string());
3831 }
Eric Laurente552edb2014-03-10 17:42:56 -07003832 ALOGV("checkOutputsForDevice(): adding output %d", output);
3833 }
3834 }
3835
3836 if (profiles.isEmpty()) {
3837 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3838 return BAD_VALUE;
3839 }
Eric Laurentd4692962014-05-05 18:13:44 -07003840 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003841 // check if one opened output is not needed any more after disconnecting one device
3842 for (size_t i = 0; i < mOutputs.size(); i++) {
3843 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003844 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003845 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003846 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003847 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003848 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003849 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003850 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3851 mOutputs.keyAt(i));
3852 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003853 }
Eric Laurente552edb2014-03-10 17:42:56 -07003854 }
3855 }
Eric Laurentd4692962014-05-05 18:13:44 -07003856 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003857 for (size_t i = 0; i < mHwModules.size(); i++)
3858 {
3859 if (mHwModules[i]->mHandle == 0) {
3860 continue;
3861 }
3862 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3863 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003864 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003865 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003866 ALOGV("checkOutputsForDevice(): "
3867 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003868 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003869 }
3870 }
3871 }
3872 }
3873 return NO_ERROR;
3874}
3875
Paul McLean9080a4c2015-06-18 08:24:02 -07003876status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003877 audio_policy_dev_state_t state,
3878 SortedVector<audio_io_handle_t>& inputs,
3879 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003880{
Paul McLean9080a4c2015-06-18 08:24:02 -07003881 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003882 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003883
3884 if (audio_device_is_digital(device)) {
3885 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003886 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003887 }
3888
Eric Laurentd4692962014-05-05 18:13:44 -07003889 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3890 // first list already open inputs that can be routed to this device
3891 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3892 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003893 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003894 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3895 inputs.add(mInputs.keyAt(input_index));
3896 }
3897 }
3898
3899 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003900 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003901 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3902 {
3903 if (mHwModules[module_idx]->mHandle == 0) {
3904 continue;
3905 }
3906 for (size_t profile_index = 0;
3907 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3908 profile_index++)
3909 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003910 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3911
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003912 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003913 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003914 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003915 profiles.add(profile);
3916 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3917 profile_index, module_idx);
3918 }
Eric Laurentd4692962014-05-05 18:13:44 -07003919 }
3920 }
3921 }
3922
3923 if (profiles.isEmpty() && inputs.isEmpty()) {
3924 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3925 return BAD_VALUE;
3926 }
3927
3928 // open inputs for matching profiles if needed. Direct inputs are also opened to
3929 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3930 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3931
Eric Laurent1c333e22014-05-20 10:48:17 -07003932 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003933 // nothing to do if one input is already opened for this profile
3934 size_t input_index;
3935 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3936 desc = mInputs.valueAt(input_index);
3937 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003938 if (audio_device_is_digital(device)) {
3939 devDesc->importAudioPort(profile);
3940 }
Eric Laurentd4692962014-05-05 18:13:44 -07003941 break;
3942 }
3943 }
3944 if (input_index != mInputs.size()) {
3945 continue;
3946 }
3947
3948 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3949 desc = new AudioInputDescriptor(profile);
3950 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003951 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3952 config.sample_rate = desc->mSamplingRate;
3953 config.channel_mask = desc->mChannelMask;
3954 config.format = desc->mFormat;
3955 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003956 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003957 &input,
3958 &config,
3959 &desc->mDevice,
3960 address,
3961 AUDIO_SOURCE_MIC,
3962 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003963
Eric Laurentcf2c0212014-07-25 16:20:43 -07003964 if (status == NO_ERROR) {
3965 desc->mSamplingRate = config.sample_rate;
3966 desc->mChannelMask = config.channel_mask;
3967 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003968
Eric Laurentd4692962014-05-05 18:13:44 -07003969 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003970 char *param = audio_device_address_to_parameter(device, address);
3971 mpClientInterface->setParameters(input, String8(param));
3972 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003973 }
Phil Burk00eeb322016-03-31 12:41:00 -07003974 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003975 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003976 ALOGW("checkInputsForDevice() direct input missing param");
3977 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003978 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003979 }
3980
3981 if (input != 0) {
3982 addInput(input, desc);
3983 }
3984 } // endif input != 0
3985
Eric Laurentcf2c0212014-07-25 16:20:43 -07003986 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003987 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003988 profiles.removeAt(profile_index);
3989 profile_index--;
3990 } else {
3991 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003992 if (audio_device_is_digital(device)) {
3993 devDesc->importAudioPort(profile);
3994 }
Eric Laurentd4692962014-05-05 18:13:44 -07003995 ALOGV("checkInputsForDevice(): adding input %d", input);
3996 }
3997 } // end scan profiles
3998
3999 if (profiles.isEmpty()) {
4000 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4001 return BAD_VALUE;
4002 }
4003 } else {
4004 // Disconnect
4005 // check if one opened input is not needed any more after disconnecting one device
4006 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4007 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004008 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004009 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4010 mInputs.keyAt(input_index));
4011 inputs.add(mInputs.keyAt(input_index));
4012 }
4013 }
4014 // Clear any profiles associated with the disconnected device.
4015 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4016 if (mHwModules[module_index]->mHandle == 0) {
4017 continue;
4018 }
4019 for (size_t profile_index = 0;
4020 profile_index < mHwModules[module_index]->mInputProfiles.size();
4021 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004022 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004023 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004024 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004025 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004026 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004027 }
4028 }
4029 }
4030 } // end disconnect
4031
4032 return NO_ERROR;
4033}
4034
4035
Eric Laurente0720872014-03-11 09:30:41 -07004036void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004037{
4038 ALOGV("closeOutput(%d)", output);
4039
Eric Laurentc75307b2015-03-17 15:29:32 -07004040 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004041 if (outputDesc == NULL) {
4042 ALOGW("closeOutput() unknown output %d", output);
4043 return;
4044 }
François Gaffie036e1e92015-03-19 10:16:24 +01004045 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004046
Eric Laurente552edb2014-03-10 17:42:56 -07004047 // look for duplicated outputs connected to the output being removed.
4048 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004049 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004050 if (dupOutputDesc->isDuplicated() &&
4051 (dupOutputDesc->mOutput1 == outputDesc ||
4052 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004053 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004054 if (dupOutputDesc->mOutput1 == outputDesc) {
4055 outputDesc2 = dupOutputDesc->mOutput2;
4056 } else {
4057 outputDesc2 = dupOutputDesc->mOutput1;
4058 }
4059 // As all active tracks on duplicated output will be deleted,
4060 // and as they were also referenced on the other output, the reference
4061 // count for their stream type must be adjusted accordingly on
4062 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004063 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004064 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004065 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004066 }
4067 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4068 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4069
4070 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004071 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004072 }
4073 }
4074
Eric Laurent05b90f82014-08-27 15:32:29 -07004075 nextAudioPortGeneration();
4076
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004077 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004078 if (index >= 0) {
4079 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4080 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4081 mAudioPatches.removeItemsAt(index);
4082 mpClientInterface->onAudioPatchListUpdate();
4083 }
4084
Eric Laurente552edb2014-03-10 17:42:56 -07004085 AudioParameter param;
4086 param.add(String8("closing"), String8("true"));
4087 mpClientInterface->setParameters(output, param.toString());
4088
4089 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004090 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004091 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004092}
4093
4094void AudioPolicyManager::closeInput(audio_io_handle_t input)
4095{
4096 ALOGV("closeInput(%d)", input);
4097
4098 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4099 if (inputDesc == NULL) {
4100 ALOGW("closeInput() unknown input %d", input);
4101 return;
4102 }
4103
Eric Laurent6a94d692014-05-20 11:18:06 -07004104 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004105
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004106 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004107 if (index >= 0) {
4108 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4109 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4110 mAudioPatches.removeItemsAt(index);
4111 mpClientInterface->onAudioPatchListUpdate();
4112 }
4113
4114 mpClientInterface->closeInput(input);
4115 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004116}
4117
Eric Laurentc75307b2015-03-17 15:29:32 -07004118SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4119 audio_devices_t device,
4120 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004121{
4122 SortedVector<audio_io_handle_t> outputs;
4123
4124 ALOGVV("getOutputsForDevice() device %04x", device);
4125 for (size_t i = 0; i < openOutputs.size(); i++) {
4126 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004127 i, openOutputs.valueAt(i)->isDuplicated(),
4128 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004129 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4130 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4131 outputs.add(openOutputs.keyAt(i));
4132 }
4133 }
4134 return outputs;
4135}
4136
Eric Laurente0720872014-03-11 09:30:41 -07004137bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004138 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004139{
4140 if (outputs1.size() != outputs2.size()) {
4141 return false;
4142 }
4143 for (size_t i = 0; i < outputs1.size(); i++) {
4144 if (outputs1[i] != outputs2[i]) {
4145 return false;
4146 }
4147 }
4148 return true;
4149}
4150
Eric Laurente0720872014-03-11 09:30:41 -07004151void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004152{
4153 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4154 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4155 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4156 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4157
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004158 // also take into account external policy-related changes: add all outputs which are
4159 // associated with policies in the "before" and "after" output vectors
4160 ALOGVV("checkOutputForStrategy(): policy related outputs");
4161 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004162 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004163 if (desc != 0 && desc->mPolicyMix != NULL) {
4164 srcOutputs.add(desc->mIoHandle);
4165 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4166 }
4167 }
4168 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004169 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004170 if (desc != 0 && desc->mPolicyMix != NULL) {
4171 dstOutputs.add(desc->mIoHandle);
4172 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4173 }
4174 }
4175
Eric Laurente552edb2014-03-10 17:42:56 -07004176 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4177 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4178 strategy, srcOutputs[0], dstOutputs[0]);
4179 // mute strategy while moving tracks from one output to another
4180 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004181 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004182 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004183 setStrategyMute(strategy, true, desc);
4184 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004185 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004186 sp<AudioSourceDescriptor> source =
4187 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4188 if (source != 0){
4189 connectAudioSource(source);
4190 }
Eric Laurente552edb2014-03-10 17:42:56 -07004191 }
4192
4193 // Move effects associated to this strategy from previous output to new output
4194 if (strategy == STRATEGY_MEDIA) {
4195 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4196 SortedVector<audio_io_handle_t> moved;
4197 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004198 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4199 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4200 effectDesc->mIo != fxOutput) {
4201 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004202 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4203 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004204 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004205 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004206 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004207 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004208 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004209 }
4210 }
4211 }
4212 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004213 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004214 if (getStrategy((audio_stream_type_t)i) == strategy) {
4215 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004216 }
4217 }
4218 }
4219}
4220
Eric Laurente0720872014-03-11 09:30:41 -07004221void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004222{
François Gaffie2110e042015-03-24 08:41:51 +01004223 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004224 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004225 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004226 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004227 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004228 checkOutputForStrategy(STRATEGY_SONIFICATION);
4229 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004230 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004231 checkOutputForStrategy(STRATEGY_MEDIA);
4232 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004233 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004234}
4235
Eric Laurente0720872014-03-11 09:30:41 -07004236void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004237{
François Gaffie53615e22015-03-19 09:24:12 +01004238 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004239 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004240 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004241 return;
4242 }
4243
Eric Laurent3a4311c2014-03-17 12:00:47 -07004244 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004245 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4246 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4247 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004248 // suspend A2DP output if:
4249 // (NOT already suspended) &&
4250 // ((SCO device is connected &&
4251 // (forced usage for communication || for record is SCO))) ||
4252 // (phone state is ringing || in call)
4253 //
4254 // restore A2DP output if:
4255 // (Already suspended) &&
4256 // ((SCO device is NOT connected ||
4257 // (forced usage NOT for communication && NOT for record is SCO))) &&
4258 // (phone state is NOT ringing && NOT in call)
4259 //
4260 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004261 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004262 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4263 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4264 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4265 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004266
4267 mpClientInterface->restoreOutput(a2dpOutput);
4268 mA2dpSuspended = false;
4269 }
4270 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004271 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004272 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4273 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4274 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4275 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004276
4277 mpClientInterface->suspendOutput(a2dpOutput);
4278 mA2dpSuspended = true;
4279 }
4280 }
4281}
4282
Eric Laurentc75307b2015-03-17 15:29:32 -07004283audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4284 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004285{
4286 audio_devices_t device = AUDIO_DEVICE_NONE;
4287
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004288 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004289 if (index >= 0) {
4290 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4291 if (patchDesc->mUid != mUidCached) {
4292 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004293 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004294 return outputDesc->device();
4295 }
4296 }
4297
Eric Laurente552edb2014-03-10 17:42:56 -07004298 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004299 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004300 // use device for strategy enforced audible
4301 // 2: we are in call or the strategy phone is active on the output:
4302 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004303 // 3: the strategy for enforced audible is active but not enforced on the output:
4304 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004305 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004306 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004307 // 5: the strategy accessibility is active on the output:
4308 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004309 // 6: the strategy "respectful" sonification is active on the output:
4310 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004311 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004312 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004313 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004314 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004315 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004316 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004317 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004318 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004319 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4320 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004321 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004322 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004323 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004324 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004325 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004326 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004327 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4328 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004329 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004330 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004331 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004332 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004333 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004334 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004335 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004336 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004337 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004338 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004339 }
4340
Eric Laurent1c333e22014-05-20 10:48:17 -07004341 ALOGV("getNewOutputDevice() selected device %x", device);
4342 return device;
4343}
4344
Eric Laurent232f26f2016-02-17 18:06:27 -08004345audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004346{
Eric Laurent232f26f2016-02-17 18:06:27 -08004347 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004348
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004349 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004350 if (index >= 0) {
4351 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4352 if (patchDesc->mUid != mUidCached) {
4353 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004354 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004355 return inputDesc->mDevice;
4356 }
4357 }
4358
Eric Laurent232f26f2016-02-17 18:06:27 -08004359 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004360
Eric Laurente552edb2014-03-10 17:42:56 -07004361 return device;
4362}
4363
Eric Laurent794fde22016-03-11 09:50:45 -08004364bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4365 audio_stream_type_t stream2) {
4366 return ((stream1 == stream2) ||
4367 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4368 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004369}
4370
Eric Laurente0720872014-03-11 09:30:41 -07004371uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004372 return (uint32_t)getStrategy(stream);
4373}
4374
Eric Laurente0720872014-03-11 09:30:41 -07004375audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004376 // By checking the range of stream before calling getStrategy, we avoid
4377 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4378 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004379 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004380 return AUDIO_DEVICE_NONE;
4381 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004382 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004383 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4384 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004385 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004386 }
Eric Laurent794fde22016-03-11 09:50:45 -08004387 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004388 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004389 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004390 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4391 for (size_t i = 0; i < outputs.size(); i++) {
4392 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004393 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004394 curDevices |= outputDesc->device();
4395 }
4396 }
4397 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004398 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004399
4400 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4401 and doesn't really need to.*/
4402 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4403 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4404 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4405 }
Eric Laurente552edb2014-03-10 17:42:56 -07004406 return devices;
4407}
4408
François Gaffie2110e042015-03-24 08:41:51 +01004409routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4410{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004411 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004412 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004413}
4414
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004415uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4416 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004417 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4418 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4419 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004420 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4421 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4422 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004423 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004424 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004425}
4426
Eric Laurente0720872014-03-11 09:30:41 -07004427void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004428 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004429 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004430 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4431 updateDevicesAndOutputs();
4432 break;
4433 default:
4434 break;
4435 }
4436}
4437
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004438uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004439
4440 // skip beacon mute management if a dedicated TTS output is available
4441 if (mTtsOutputAvailable) {
4442 return 0;
4443 }
4444
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004445 switch(event) {
4446 case STARTING_OUTPUT:
4447 mBeaconMuteRefCount++;
4448 break;
4449 case STOPPING_OUTPUT:
4450 if (mBeaconMuteRefCount > 0) {
4451 mBeaconMuteRefCount--;
4452 }
4453 break;
4454 case STARTING_BEACON:
4455 mBeaconPlayingRefCount++;
4456 break;
4457 case STOPPING_BEACON:
4458 if (mBeaconPlayingRefCount > 0) {
4459 mBeaconPlayingRefCount--;
4460 }
4461 break;
4462 }
4463
4464 if (mBeaconMuteRefCount > 0) {
4465 // any playback causes beacon to be muted
4466 return setBeaconMute(true);
4467 } else {
4468 // no other playback: unmute when beacon starts playing, mute when it stops
4469 return setBeaconMute(mBeaconPlayingRefCount == 0);
4470 }
4471}
4472
4473uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4474 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4475 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4476 // keep track of muted state to avoid repeating mute/unmute operations
4477 if (mBeaconMuted != mute) {
4478 // mute/unmute AUDIO_STREAM_TTS on all outputs
4479 ALOGV("\t muting %d", mute);
4480 uint32_t maxLatency = 0;
4481 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004482 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004483 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004484 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004485 0 /*delay*/, AUDIO_DEVICE_NONE);
4486 const uint32_t latency = desc->latency() * 2;
4487 if (latency > maxLatency) {
4488 maxLatency = latency;
4489 }
4490 }
4491 mBeaconMuted = mute;
4492 return maxLatency;
4493 }
4494 return 0;
4495}
4496
Eric Laurente0720872014-03-11 09:30:41 -07004497audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004498 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004499{
Paul McLeanaa981192015-03-21 09:55:15 -07004500 // Routing
4501 // see if we have an explicit route
4502 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4503 // (getStrategy(stream)).
4504 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4505 // and activity count is non-zero
4506 // the device = the device from the descriptor in the RouteMap, and exit.
4507 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4508 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004509 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4510 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004511 return route->mDeviceDescriptor->type();
4512 }
4513 }
4514
Eric Laurente552edb2014-03-10 17:42:56 -07004515 if (fromCache) {
4516 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4517 strategy, mDeviceForStrategy[strategy]);
4518 return mDeviceForStrategy[strategy];
4519 }
François Gaffie2110e042015-03-24 08:41:51 +01004520 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004521}
4522
Eric Laurente0720872014-03-11 09:30:41 -07004523void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004524{
4525 for (int i = 0; i < NUM_STRATEGIES; i++) {
4526 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4527 }
4528 mPreviousOutputs = mOutputs;
4529}
4530
Eric Laurent1f2f2232014-06-02 12:01:23 -07004531uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004532 audio_devices_t prevDevice,
4533 uint32_t delayMs)
4534{
4535 // mute/unmute strategies using an incompatible device combination
4536 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4537 // if unmuting, unmute only after the specified delay
4538 if (outputDesc->isDuplicated()) {
4539 return 0;
4540 }
4541
4542 uint32_t muteWaitMs = 0;
4543 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004544 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004545
4546 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4547 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004548 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004549 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4550 bool doMute = false;
4551
4552 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4553 doMute = true;
4554 outputDesc->mStrategyMutedByDevice[i] = true;
4555 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4556 doMute = true;
4557 outputDesc->mStrategyMutedByDevice[i] = false;
4558 }
Eric Laurent99401132014-05-07 19:48:15 -07004559 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004560 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004561 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004562 // skip output if it does not share any device with current output
4563 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4564 == AUDIO_DEVICE_NONE) {
4565 continue;
4566 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004567 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4568 mute ? "muting" : "unmuting", i, curDevice);
4569 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004570 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004571 if (mute) {
4572 // FIXME: should not need to double latency if volume could be applied
4573 // immediately by the audioflinger mixer. We must account for the delay
4574 // between now and the next time the audioflinger thread for this output
4575 // will process a buffer (which corresponds to one buffer size,
4576 // usually 1/2 or 1/4 of the latency).
4577 if (muteWaitMs < desc->latency() * 2) {
4578 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004579 }
4580 }
4581 }
4582 }
4583 }
4584 }
4585
Eric Laurent99401132014-05-07 19:48:15 -07004586 // temporary mute output if device selection changes to avoid volume bursts due to
4587 // different per device volumes
4588 if (outputDesc->isActive() && (device != prevDevice)) {
4589 if (muteWaitMs < outputDesc->latency() * 2) {
4590 muteWaitMs = outputDesc->latency() * 2;
4591 }
4592 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004593 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004594 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004595 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004596 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004597 muteWaitMs *2, device);
4598 }
4599 }
4600 }
4601
Eric Laurente552edb2014-03-10 17:42:56 -07004602 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4603 if (muteWaitMs > delayMs) {
4604 muteWaitMs -= delayMs;
4605 usleep(muteWaitMs * 1000);
4606 return muteWaitMs;
4607 }
4608 return 0;
4609}
4610
Eric Laurentc75307b2015-03-17 15:29:32 -07004611uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004612 audio_devices_t device,
4613 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004614 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004615 audio_patch_handle_t *patchHandle,
4616 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004617{
Eric Laurentc75307b2015-03-17 15:29:32 -07004618 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004619 AudioParameter param;
4620 uint32_t muteWaitMs;
4621
4622 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004623 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4624 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004625 return muteWaitMs;
4626 }
4627 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4628 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004629 if ((device != AUDIO_DEVICE_NONE) &&
4630 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004631 return 0;
4632 }
4633
4634 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004635 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004636
4637 audio_devices_t prevDevice = outputDesc->mDevice;
4638
Paul McLeanaa981192015-03-21 09:55:15 -07004639 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004640
4641 if (device != AUDIO_DEVICE_NONE) {
4642 outputDesc->mDevice = device;
4643 }
4644 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4645
4646 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004647 // the requested device is AUDIO_DEVICE_NONE
4648 // OR the requested device is the same as current device
4649 // AND force is not specified
4650 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004651 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004652 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4653 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004654 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004655 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004656 return muteWaitMs;
4657 }
4658
4659 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004660
Eric Laurente552edb2014-03-10 17:42:56 -07004661 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004662 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004663 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004664 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004665 DeviceVector deviceList;
4666 if ((address == NULL) || (strlen(address) == 0)) {
4667 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4668 } else {
4669 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4670 }
4671
Eric Laurent1c333e22014-05-20 10:48:17 -07004672 if (!deviceList.isEmpty()) {
4673 struct audio_patch patch;
4674 outputDesc->toAudioPortConfig(&patch.sources[0]);
4675 patch.num_sources = 1;
4676 patch.num_sinks = 0;
4677 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4678 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004679 patch.num_sinks++;
4680 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004681 ssize_t index;
4682 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4683 index = mAudioPatches.indexOfKey(*patchHandle);
4684 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004685 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004686 }
4687 sp< AudioPatch> patchDesc;
4688 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4689 if (index >= 0) {
4690 patchDesc = mAudioPatches.valueAt(index);
4691 afPatchHandle = patchDesc->mAfPatchHandle;
4692 }
4693
Eric Laurent1c333e22014-05-20 10:48:17 -07004694 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004695 &afPatchHandle,
4696 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004697 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4698 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004699 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004700 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004701 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004702 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004703 addAudioPatch(patchDesc->mHandle, patchDesc);
4704 } else {
4705 patchDesc->mPatch = patch;
4706 }
4707 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004708 if (patchHandle) {
4709 *patchHandle = patchDesc->mHandle;
4710 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004711 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004712 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004713 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004714 }
4715 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004716
4717 // inform all input as well
4718 for (size_t i = 0; i < mInputs.size(); i++) {
4719 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004720 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004721 AudioParameter inputCmd = AudioParameter();
4722 ALOGV("%s: inform input %d of device:%d", __func__,
4723 inputDescriptor->mIoHandle, device);
4724 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4725 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4726 inputCmd.toString(),
4727 delayMs);
4728 }
4729 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004730 }
Eric Laurente552edb2014-03-10 17:42:56 -07004731
4732 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004733 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004734
4735 return muteWaitMs;
4736}
4737
Eric Laurentc75307b2015-03-17 15:29:32 -07004738status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004739 int delayMs,
4740 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004741{
Eric Laurent6a94d692014-05-20 11:18:06 -07004742 ssize_t index;
4743 if (patchHandle) {
4744 index = mAudioPatches.indexOfKey(*patchHandle);
4745 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004746 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004747 }
4748 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004749 return INVALID_OPERATION;
4750 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004751 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4752 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004753 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004754 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004755 removeAudioPatch(patchDesc->mHandle);
4756 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004757 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004758 return status;
4759}
4760
4761status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4762 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004763 bool force,
4764 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004765{
4766 status_t status = NO_ERROR;
4767
Eric Laurent1f2f2232014-06-02 12:01:23 -07004768 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004769 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4770 inputDesc->mDevice = device;
4771
4772 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4773 if (!deviceList.isEmpty()) {
4774 struct audio_patch patch;
4775 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004776 // AUDIO_SOURCE_HOTWORD is for internal use only:
4777 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004778 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004779 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004780 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4781 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004782 patch.num_sinks = 1;
4783 //only one input device for now
4784 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004785 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004786 ssize_t index;
4787 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4788 index = mAudioPatches.indexOfKey(*patchHandle);
4789 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004790 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004791 }
4792 sp< AudioPatch> patchDesc;
4793 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4794 if (index >= 0) {
4795 patchDesc = mAudioPatches.valueAt(index);
4796 afPatchHandle = patchDesc->mAfPatchHandle;
4797 }
4798
Eric Laurent1c333e22014-05-20 10:48:17 -07004799 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004800 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004801 0);
4802 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004803 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004804 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004805 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004806 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004807 addAudioPatch(patchDesc->mHandle, patchDesc);
4808 } else {
4809 patchDesc->mPatch = patch;
4810 }
4811 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004812 if (patchHandle) {
4813 *patchHandle = patchDesc->mHandle;
4814 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004815 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004816 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004817 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004818 }
4819 }
4820 }
4821 return status;
4822}
4823
Eric Laurent6a94d692014-05-20 11:18:06 -07004824status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4825 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004826{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004827 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004828 ssize_t index;
4829 if (patchHandle) {
4830 index = mAudioPatches.indexOfKey(*patchHandle);
4831 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004832 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004833 }
4834 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004835 return INVALID_OPERATION;
4836 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004837 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4838 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004839 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004840 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004841 removeAudioPatch(patchDesc->mHandle);
4842 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004843 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004844 return status;
4845}
4846
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004847sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004848 String8 address,
4849 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004850 audio_format_t& format,
4851 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004852 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004853{
4854 // Choose an input profile based on the requested capture parameters: select the first available
4855 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004856 //
4857 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4858 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004859
4860 for (size_t i = 0; i < mHwModules.size(); i++)
4861 {
4862 if (mHwModules[i]->mHandle == 0) {
4863 continue;
4864 }
4865 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4866 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004867 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004868 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004869 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004870 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004871 format,
4872 &format /*updatedFormat*/,
4873 channelMask,
4874 &channelMask /*updatedChannelMask*/,
4875 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004876
Eric Laurente552edb2014-03-10 17:42:56 -07004877 return profile;
4878 }
4879 }
4880 }
4881 return NULL;
4882}
4883
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004884
4885audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004886 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004887{
François Gaffie036e1e92015-03-19 10:16:24 +01004888 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4889 audio_devices_t selectedDeviceFromMix =
4890 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004891
François Gaffie036e1e92015-03-19 10:16:24 +01004892 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4893 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004894 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004895 return getDeviceForInputSource(inputSource);
4896}
4897
4898audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4899{
Paul McLean466dc8e2015-04-17 13:15:36 -06004900 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4901 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004902 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004903 return route->mDeviceDescriptor->type();
4904 }
4905 }
4906
4907 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004908}
4909
Eric Laurente0720872014-03-11 09:30:41 -07004910float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004911 int index,
4912 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004913{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004914 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004915 // if a headset is connected, apply the following rules to ring tones and notifications
4916 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004917 // - always attenuate notifications volume by 6dB
4918 // - attenuate ring tones volume by 6dB unless music is not playing and
4919 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004920 // - if music is playing, always limit the volume to current music volume,
4921 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004922 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004923 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4924 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4925 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4926 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4927 ((stream_strategy == STRATEGY_SONIFICATION)
4928 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004929 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004930 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004931 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004932 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004933 // when the phone is ringing we must consider that music could have been paused just before
4934 // by the music application and behave as if music was active if the last music track was
4935 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004936 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004937 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004938 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004939 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004940 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004941 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4942 musicDevice),
4943 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004944 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4945 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004946 if (volumeDB > minVolDB) {
4947 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004948 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004949 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004950 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4951 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4952 // on A2DP, also ensure notification volume is not too low compared to media when
4953 // intended to be played
4954 if ((volumeDB > -96.0f) &&
4955 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4956 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4957 stream, device,
4958 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4959 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4960 }
4961 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004962 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4963 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004964 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004965 }
4966 }
4967
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004968 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07004969}
4970
Eric Laurente0720872014-03-11 09:30:41 -07004971status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004972 int index,
4973 const sp<AudioOutputDescriptor>& outputDesc,
4974 audio_devices_t device,
4975 int delayMs,
4976 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004977{
Eric Laurente552edb2014-03-10 17:42:56 -07004978 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004979 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004980 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004981 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004982 return NO_ERROR;
4983 }
François Gaffie2110e042015-03-24 08:41:51 +01004984 audio_policy_forced_cfg_t forceUseForComm =
4985 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004986 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004987 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4988 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004989 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004990 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004991 return INVALID_OPERATION;
4992 }
4993
Eric Laurentc75307b2015-03-17 15:29:32 -07004994 if (device == AUDIO_DEVICE_NONE) {
4995 device = outputDesc->device();
4996 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004997
Eric Laurentffbc80f2015-03-18 18:30:19 -07004998 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004999 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005000 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005001 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005002
Eric Laurentffbc80f2015-03-18 18:30:19 -07005003 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005004
Eric Laurent3b73df72014-03-11 09:06:29 -07005005 if (stream == AUDIO_STREAM_VOICE_CALL ||
5006 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005007 float voiceVolume;
5008 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005009 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005010 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005011 } else {
5012 voiceVolume = 1.0;
5013 }
5014
Eric Laurent18fba842016-03-31 14:41:26 -07005015 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005016 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5017 mLastVoiceVolume = voiceVolume;
5018 }
5019 }
5020
5021 return NO_ERROR;
5022}
5023
Eric Laurentc75307b2015-03-17 15:29:32 -07005024void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5025 audio_devices_t device,
5026 int delayMs,
5027 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005028{
Eric Laurentc75307b2015-03-17 15:29:32 -07005029 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005030
Eric Laurent794fde22016-03-11 09:50:45 -08005031 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005032 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005033 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005034 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005035 device,
5036 delayMs,
5037 force);
5038 }
5039}
5040
Eric Laurente0720872014-03-11 09:30:41 -07005041void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005042 bool on,
5043 const sp<AudioOutputDescriptor>& outputDesc,
5044 int delayMs,
5045 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005046{
Eric Laurent72249d52015-06-08 11:12:15 -07005047 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5048 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005049 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005050 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005051 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005052 }
5053 }
5054}
5055
Eric Laurente0720872014-03-11 09:30:41 -07005056void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005057 bool on,
5058 const sp<AudioOutputDescriptor>& outputDesc,
5059 int delayMs,
5060 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005061{
Eric Laurente552edb2014-03-10 17:42:56 -07005062 if (device == AUDIO_DEVICE_NONE) {
5063 device = outputDesc->device();
5064 }
5065
Eric Laurentc75307b2015-03-17 15:29:32 -07005066 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5067 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005068
5069 if (on) {
5070 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005071 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005072 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005073 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005074 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005075 }
5076 }
5077 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5078 outputDesc->mMuteCount[stream]++;
5079 } else {
5080 if (outputDesc->mMuteCount[stream] == 0) {
5081 ALOGV("setStreamMute() unmuting non muted stream!");
5082 return;
5083 }
5084 if (--outputDesc->mMuteCount[stream] == 0) {
5085 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005086 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005087 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005088 device,
5089 delayMs);
5090 }
5091 }
5092}
5093
Eric Laurente0720872014-03-11 09:30:41 -07005094void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005095 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005096{
Eric Laurent87ffa392015-05-22 10:32:38 -07005097 if(!hasPrimaryOutput()) {
5098 return;
5099 }
5100
Eric Laurente552edb2014-03-10 17:42:56 -07005101 // if the stream pertains to sonification strategy and we are in call we must
5102 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5103 // in the device used for phone strategy and play the tone if the selected device does not
5104 // interfere with the device used for phone strategy
5105 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5106 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005107 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005108 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5109 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005110 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005111 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5112 stream, starting, outputDesc->mDevice, stateChange);
5113 if (outputDesc->mRefCount[stream]) {
5114 int muteCount = 1;
5115 if (stateChange) {
5116 muteCount = outputDesc->mRefCount[stream];
5117 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005118 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005119 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5120 for (int i = 0; i < muteCount; i++) {
5121 setStreamMute(stream, starting, mPrimaryOutput);
5122 }
5123 } else {
5124 ALOGV("handleIncallSonification() high visibility");
5125 if (outputDesc->device() &
5126 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5127 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5128 for (int i = 0; i < muteCount; i++) {
5129 setStreamMute(stream, starting, mPrimaryOutput);
5130 }
5131 }
5132 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005133 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5134 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005135 } else {
5136 mpClientInterface->stopTone();
5137 }
5138 }
5139 }
5140 }
5141}
5142
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005143audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5144{
5145 // flags to stream type mapping
5146 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5147 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5148 }
5149 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5150 return AUDIO_STREAM_BLUETOOTH_SCO;
5151 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005152 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5153 return AUDIO_STREAM_TTS;
5154 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005155
5156 // usage to stream type mapping
5157 switch (attr->usage) {
5158 case AUDIO_USAGE_MEDIA:
5159 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005160 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5161 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005162 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5163 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005164 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5165 return AUDIO_STREAM_SYSTEM;
5166 case AUDIO_USAGE_VOICE_COMMUNICATION:
5167 return AUDIO_STREAM_VOICE_CALL;
5168
5169 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5170 return AUDIO_STREAM_DTMF;
5171
5172 case AUDIO_USAGE_ALARM:
5173 return AUDIO_STREAM_ALARM;
5174 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5175 return AUDIO_STREAM_RING;
5176
5177 case AUDIO_USAGE_NOTIFICATION:
5178 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5179 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5180 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5181 case AUDIO_USAGE_NOTIFICATION_EVENT:
5182 return AUDIO_STREAM_NOTIFICATION;
5183
5184 case AUDIO_USAGE_UNKNOWN:
5185 default:
5186 return AUDIO_STREAM_MUSIC;
5187 }
5188}
Eric Laurente83b55d2014-11-14 10:06:21 -08005189
François Gaffie53615e22015-03-19 09:24:12 +01005190bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5191{
Eric Laurente83b55d2014-11-14 10:06:21 -08005192 // has flags that map to a strategy?
5193 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5194 return true;
5195 }
5196
5197 // has known usage?
5198 switch (paa->usage) {
5199 case AUDIO_USAGE_UNKNOWN:
5200 case AUDIO_USAGE_MEDIA:
5201 case AUDIO_USAGE_VOICE_COMMUNICATION:
5202 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5203 case AUDIO_USAGE_ALARM:
5204 case AUDIO_USAGE_NOTIFICATION:
5205 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5206 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5207 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5208 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5209 case AUDIO_USAGE_NOTIFICATION_EVENT:
5210 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5211 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5212 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5213 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005214 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005215 break;
5216 default:
5217 return false;
5218 }
5219 return true;
5220}
5221
François Gaffiead3183e2015-03-18 16:55:35 +01005222bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5223 routing_strategy strategy, uint32_t inPastMs,
5224 nsecs_t sysTime) const
5225{
5226 if ((sysTime == 0) && (inPastMs != 0)) {
5227 sysTime = systemTime();
5228 }
Eric Laurent794fde22016-03-11 09:50:45 -08005229 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005230 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5231 (NUM_STRATEGIES == strategy)) &&
5232 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5233 return true;
5234 }
5235 }
5236 return false;
5237}
5238
François Gaffie2110e042015-03-24 08:41:51 +01005239audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5240{
5241 return mEngine->getForceUse(usage);
5242}
5243
5244bool AudioPolicyManager::isInCall()
5245{
5246 return isStateInCall(mEngine->getPhoneState());
5247}
5248
5249bool AudioPolicyManager::isStateInCall(int state)
5250{
5251 return is_state_in_call(state);
5252}
5253
Eric Laurentd60560a2015-04-10 11:31:20 -07005254void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5255{
5256 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5257 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5258 if (sourceDesc->mDevice->equals(deviceDesc)) {
5259 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5260 stopAudioSource(sourceDesc->getHandle());
5261 }
5262 }
5263
5264 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5265 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5266 bool release = false;
5267 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5268 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5269 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5270 source->ext.device.type == deviceDesc->type()) {
5271 release = true;
5272 }
5273 }
5274 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5275 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5276 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5277 sink->ext.device.type == deviceDesc->type()) {
5278 release = true;
5279 }
5280 }
5281 if (release) {
5282 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5283 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5284 }
5285 }
5286}
5287
Phil Burk09bc4612016-02-24 15:58:15 -08005288// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005289void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5290 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005291 // TODO Set this based on Config properties.
5292 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005293
5294 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5295 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005296 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005297
5298 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005299 bool supportsAC3 = false;
5300 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005301 bool supportsIEC61937 = false;
5302 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5303 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005304 switch (format) {
5305 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005306 supportsAC3 = true;
5307 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005308 case AUDIO_FORMAT_E_AC3:
5309 case AUDIO_FORMAT_DTS:
5310 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005311 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005312 break;
5313 case AUDIO_FORMAT_IEC61937:
5314 supportsIEC61937 = true;
5315 break;
5316 default:
5317 break;
5318 }
5319 }
Phil Burk09bc4612016-02-24 15:58:15 -08005320
5321 // Modify formats based on surround preferences.
5322 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005323 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5324 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5325 // Remove surround sound related formats.
5326 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5327 audio_format_t format = formats[formatIndex];
5328 switch(format) {
5329 case AUDIO_FORMAT_AC3:
5330 case AUDIO_FORMAT_E_AC3:
5331 case AUDIO_FORMAT_DTS:
5332 case AUDIO_FORMAT_DTS_HD:
5333 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005334 formats.removeAt(formatIndex);
5335 break;
5336 default:
5337 formatIndex++; // keep it
5338 break;
5339 }
Phil Burk09bc4612016-02-24 15:58:15 -08005340 }
Phil Burk07ac1142016-03-25 13:39:29 -07005341 supportsAC3 = false;
5342 supportsOtherSurround = false;
5343 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005344 }
Phil Burk07ac1142016-03-25 13:39:29 -07005345 } else { // AUTO or ALWAYS
5346 // Most TVs support AC3 even if they do not report it in the EDID.
5347 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5348 && !supportsAC3) {
5349 formats.add(AUDIO_FORMAT_AC3);
5350 supportsAC3 = true;
5351 }
5352
5353 // If ALWAYS, add support for raw surround formats if all are missing.
5354 // This assumes that if any of these formats are reported by the HAL
5355 // then the report is valid and should not be modified.
5356 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5357 && !supportsOtherSurround) {
5358 formats.add(AUDIO_FORMAT_E_AC3);
5359 formats.add(AUDIO_FORMAT_DTS);
5360 formats.add(AUDIO_FORMAT_DTS_HD);
5361 supportsOtherSurround = true;
5362 }
5363
5364 // Add support for IEC61937 if any raw surround supported.
5365 // The HAL could do this but add it here, just in case.
5366 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5367 formats.add(AUDIO_FORMAT_IEC61937);
5368 supportsIEC61937 = true;
5369 }
Phil Burk09bc4612016-02-24 15:58:15 -08005370 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005371}
5372
5373// Modify the list of channel masks supported.
5374void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5375 ChannelsVector &channelMasks = *channelMasksPtr;
5376 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5377 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5378
5379 // If NEVER, then remove support for channelMasks > stereo.
5380 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5381 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5382 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5383 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5384 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5385 channelMasks.removeAt(maskIndex);
5386 } else {
5387 maskIndex++;
5388 }
5389 }
5390 // If ALWAYS, then make sure we at least support 5.1
5391 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5392 bool supports5dot1 = false;
5393 // Are there any channel masks that can be considered "surround"?
5394 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5395 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5396 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5397 supports5dot1 = true;
5398 break;
5399 }
5400 }
5401 // If not then add 5.1 support.
5402 if (!supports5dot1) {
5403 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5404 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5405 }
Phil Burk09bc4612016-02-24 15:58:15 -08005406 }
5407}
5408
Phil Burk00eeb322016-03-31 12:41:00 -07005409void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5410 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005411 AudioProfileVector &profiles)
5412{
5413 String8 reply;
5414 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005415
François Gaffie112b0af2015-11-19 16:13:25 +01005416 // Format MUST be checked first to update the list of AudioProfile
5417 if (profiles.hasDynamicFormat()) {
5418 reply = mpClientInterface->getParameters(ioHandle,
5419 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005420 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005421 AudioParameter repliedParameters(reply);
5422 if (repliedParameters.get(
5423 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005424 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5425 return;
5426 }
Phil Burk09bc4612016-02-24 15:58:15 -08005427 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005428 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005429 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005430 }
Phil Burk09bc4612016-02-24 15:58:15 -08005431 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005432 }
5433 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5434
Eric Laurent20eb3a42016-01-26 18:39:17 -08005435 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005436 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005437 ChannelsVector channelMasks;
5438 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005439 AudioParameter requestedParameters;
5440 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5441
5442 if (profiles.hasDynamicRateFor(format)) {
5443 reply = mpClientInterface->getParameters(ioHandle,
5444 requestedParameters.toString() + ";" +
5445 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5446 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005447 AudioParameter repliedParameters(reply);
5448 if (repliedParameters.get(
5449 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5450 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005451 }
5452 }
5453 if (profiles.hasDynamicChannelsFor(format)) {
5454 reply = mpClientInterface->getParameters(ioHandle,
5455 requestedParameters.toString() + ";" +
5456 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5457 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005458 AudioParameter repliedParameters(reply);
5459 if (repliedParameters.get(
5460 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5461 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005462 if (device == AUDIO_DEVICE_OUT_HDMI) {
5463 filterSurroundChannelMasks(&channelMasks);
5464 }
François Gaffie112b0af2015-11-19 16:13:25 +01005465 }
5466 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005467 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005468 }
5469}
Eric Laurentd60560a2015-04-10 11:31:20 -07005470
Eric Laurente552edb2014-03-10 17:42:56 -07005471}; // namespace android