blob: 0a0036825e7040d625ca2ce707626fa2a3749277 [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
1502 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1503 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1504 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001505 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1506 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001507 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001508 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001509 }
Eric Laurente552edb2014-03-10 17:42:56 -07001510 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001511 // Pick input sampling rate if not specified by client
1512 if (samplingRate == 0) {
1513 samplingRate = profileSamplingRate;
1514 }
Eric Laurente552edb2014-03-10 17:42:56 -07001515
Eric Laurent322b4d22015-04-03 15:57:54 -07001516 if (profile->getModuleHandle() == 0) {
1517 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001518 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001519 }
1520
Eric Laurent599c7582015-12-07 18:05:55 -08001521 sp<AudioSession> audioSession = new AudioSession(session,
1522 inputSource,
1523 format,
1524 samplingRate,
1525 channelMask,
1526 flags,
1527 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001528 isSoundTrigger,
1529 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001530
Eric Laurent232f26f2016-02-17 18:06:27 -08001531// TODO enable input reuse
1532#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001533 // reuse an open input if possible
1534 for (size_t i = 0; i < mInputs.size(); i++) {
1535 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001536 // reuse input if it shares the same profile and same sound trigger attribute
1537 if (profile == desc->mProfile &&
1538 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001539
1540 sp<AudioSession> as = desc->getAudioSession(session);
1541 if (as != 0) {
1542 // do not allow unmatching properties on same session
1543 if (as->matches(audioSession)) {
1544 as->changeOpenCount(1);
1545 } else {
1546 ALOGW("getInputForDevice() record with different attributes"
1547 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001548 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001549 }
1550 } else {
1551 desc->addAudioSession(session, audioSession);
1552 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001553 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1554 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001555 }
1556 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001557#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001558
Eric Laurentcf2c0212014-07-25 16:20:43 -07001559 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001560 config.sample_rate = profileSamplingRate;
1561 config.channel_mask = profileChannelMask;
1562 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001563
Eric Laurent322b4d22015-04-03 15:57:54 -07001564 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001565 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001566 &config,
1567 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001568 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001569 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001570 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001571
1572 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001573 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001574 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001575 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001576 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001577 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1578 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001579 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001580 if (input != AUDIO_IO_HANDLE_NONE) {
1581 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001582 }
Eric Laurent599c7582015-12-07 18:05:55 -08001583 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001584 }
1585
Eric Laurent1f2f2232014-06-02 12:01:23 -07001586 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001587 inputDesc->mSamplingRate = profileSamplingRate;
1588 inputDesc->mFormat = profileFormat;
1589 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001590 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001591 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001592 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001593
Eric Laurent599c7582015-12-07 18:05:55 -08001594 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001595 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001596
Eric Laurent599c7582015-12-07 18:05:55 -08001597 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001598}
1599
Eric Laurent4dc68062014-07-28 17:26:49 -07001600status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001601 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001602{
1603 ALOGV("startInput() input %d", input);
1604 ssize_t index = mInputs.indexOfKey(input);
1605 if (index < 0) {
1606 ALOGW("startInput() unknown input %d", input);
1607 return BAD_VALUE;
1608 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001609 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001610
Eric Laurent599c7582015-12-07 18:05:55 -08001611 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1612 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001613 ALOGW("startInput() unknown session %d on input %d", session, input);
1614 return BAD_VALUE;
1615 }
1616
Eric Laurent232f26f2016-02-17 18:06:27 -08001617 // virtual input devices are compatible with other input devices
1618 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001619
Eric Laurent232f26f2016-02-17 18:06:27 -08001620 // for a non-virtual input device, check if there is another (non-virtual) active input
1621 audio_io_handle_t activeInput = mInputs.getActiveInput();
1622 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001623
Eric Laurent232f26f2016-02-17 18:06:27 -08001624 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1625 // otherwise the active input continues and the new input cannot be started.
1626 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1627 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1628 !activeDesc->hasPreemptedSession(session)) {
1629 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1630 //FIXME: consider all active sessions
1631 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1632 audio_session_t activeSession = activeSessions.keyAt(0);
1633 SortedVector<audio_session_t> sessions =
1634 activeDesc->getPreemptedSessions();
1635 sessions.add(activeSession);
1636 inputDesc->setPreemptedSessions(sessions);
1637 stopInput(activeInput, activeSession);
1638 releaseInput(activeInput, activeSession);
1639 } else {
1640 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1641 return INVALID_OPERATION;
1642 }
1643 }
1644
1645 // Do not allow capture if an active voice call is using a software patch and
1646 // the call TX source device is on the same HW module.
1647 // FIXME: would be better to refine to only inputs whose profile connects to the
1648 // call TX device but this information is not in the audio patch
1649 if (mCallTxPatch != 0 &&
1650 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1651 return INVALID_OPERATION;
1652 }
1653 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001654
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001655 // Routing?
1656 mInputRoutes.incRouteActivity(session);
1657
Eric Laurent232f26f2016-02-17 18:06:27 -08001658 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1659 // if input maps to a dynamic policy with an activity listener, notify of state change
1660 if ((inputDesc->mPolicyMix != NULL)
1661 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001662 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001663 MIX_STATE_MIXING);
1664 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001665
Eric Laurent232f26f2016-02-17 18:06:27 -08001666 if (mInputs.activeInputsCount() == 0) {
1667 SoundTrigger::setCaptureState(true);
1668 }
1669 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001670
Eric Laurent232f26f2016-02-17 18:06:27 -08001671 // automatically enable the remote submix output when input is started if not
1672 // used by a policy mix of type MIX_TYPE_RECORDERS
1673 // For remote submix (a virtual device), we open only one input per capture request.
1674 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1675 String8 address = String8("");
1676 if (inputDesc->mPolicyMix == NULL) {
1677 address = String8("0");
1678 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001679 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001680 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001681 if (address != "") {
1682 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1683 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1684 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001685 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001686 }
Eric Laurente552edb2014-03-10 17:42:56 -07001687 }
1688
Eric Laurent599c7582015-12-07 18:05:55 -08001689 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001690
Eric Laurent232f26f2016-02-17 18:06:27 -08001691 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001692 return NO_ERROR;
1693}
1694
Eric Laurent4dc68062014-07-28 17:26:49 -07001695status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1696 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001697{
1698 ALOGV("stopInput() input %d", input);
1699 ssize_t index = mInputs.indexOfKey(input);
1700 if (index < 0) {
1701 ALOGW("stopInput() unknown input %d", input);
1702 return BAD_VALUE;
1703 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001704 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001705
Eric Laurent599c7582015-12-07 18:05:55 -08001706 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001707 if (index < 0) {
1708 ALOGW("stopInput() unknown session %d on input %d", session, input);
1709 return BAD_VALUE;
1710 }
1711
Eric Laurent599c7582015-12-07 18:05:55 -08001712 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001713 ALOGW("stopInput() input %d already stopped", input);
1714 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001715 }
1716
Eric Laurent599c7582015-12-07 18:05:55 -08001717 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001718
1719 // Routing?
1720 mInputRoutes.decRouteActivity(session);
1721
Eric Laurent232f26f2016-02-17 18:06:27 -08001722 if (!inputDesc->isActive()) {
1723 // if input maps to a dynamic policy with an activity listener, notify of state change
1724 if ((inputDesc->mPolicyMix != NULL)
1725 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001726 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001727 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001728 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001729
1730 // automatically disable the remote submix output when input is stopped if not
1731 // used by a policy mix of type MIX_TYPE_RECORDERS
1732 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1733 String8 address = String8("");
1734 if (inputDesc->mPolicyMix == NULL) {
1735 address = String8("0");
1736 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001737 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001738 }
1739 if (address != "") {
1740 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1741 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1742 address, "remote-submix");
1743 }
1744 }
1745
1746 resetInputDevice(input);
1747
1748 if (mInputs.activeInputsCount() == 0) {
1749 SoundTrigger::setCaptureState(false);
1750 }
1751 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001752 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001753 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001754}
1755
Eric Laurent4dc68062014-07-28 17:26:49 -07001756void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1757 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001758{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001759
Eric Laurente552edb2014-03-10 17:42:56 -07001760 ALOGV("releaseInput() %d", input);
1761 ssize_t index = mInputs.indexOfKey(input);
1762 if (index < 0) {
1763 ALOGW("releaseInput() releasing unknown input %d", input);
1764 return;
1765 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001766
1767 // Routing
1768 mInputRoutes.removeRoute(session);
1769
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001770 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1771 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001772
Eric Laurent599c7582015-12-07 18:05:55 -08001773 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001774 if (index < 0) {
1775 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1776 return;
1777 }
Eric Laurent599c7582015-12-07 18:05:55 -08001778
1779 if (audioSession->openCount() == 0) {
1780 ALOGW("releaseInput() invalid open count %d on session %d",
1781 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001782 return;
1783 }
Eric Laurent599c7582015-12-07 18:05:55 -08001784
1785 if (audioSession->changeOpenCount(-1) == 0) {
1786 inputDesc->removeAudioSession(session);
1787 }
1788
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001789 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001790 ALOGV("releaseInput() exit > 0");
1791 return;
1792 }
1793
Eric Laurent05b90f82014-08-27 15:32:29 -07001794 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001795 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001796 ALOGV("releaseInput() exit");
1797}
1798
Eric Laurentd4692962014-05-05 18:13:44 -07001799void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001800 bool patchRemoved = false;
1801
Eric Laurentd4692962014-05-05 18:13:44 -07001802 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001803 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001804 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001805 if (patch_index >= 0) {
1806 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1807 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1808 mAudioPatches.removeItemsAt(patch_index);
1809 patchRemoved = true;
1810 }
Eric Laurentd4692962014-05-05 18:13:44 -07001811 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1812 }
1813 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001814 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001815 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001816
1817 if (patchRemoved) {
1818 mpClientInterface->onAudioPatchListUpdate();
1819 }
Eric Laurentd4692962014-05-05 18:13:44 -07001820}
1821
Eric Laurente0720872014-03-11 09:30:41 -07001822void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001823 int indexMin,
1824 int indexMax)
1825{
1826 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001827 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001828
1829 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001830 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1831 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001832 continue;
1833 }
1834 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001835 }
Eric Laurente552edb2014-03-10 17:42:56 -07001836}
1837
Eric Laurente0720872014-03-11 09:30:41 -07001838status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001839 int index,
1840 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001841{
1842
François Gaffied1ab2bd2015-12-02 18:20:06 +01001843 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1844 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001845 return BAD_VALUE;
1846 }
1847 if (!audio_is_output_device(device)) {
1848 return BAD_VALUE;
1849 }
1850
1851 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001852 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001853
Eric Laurent1fd372e2016-04-06 14:23:53 -07001854 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001855 stream, device, index);
1856
Eric Laurent28d09f02016-03-08 10:43:05 -08001857 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001858 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1859 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001860 continue;
1861 }
1862 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1863 }
Eric Laurente552edb2014-03-10 17:42:56 -07001864
Eric Laurent1fd372e2016-04-06 14:23:53 -07001865 // update volume on all outputs and streams matching the following:
1866 // - The requested stream (or a stream matching for volume control) is active on the output
1867 // - The device (or devices) selected by the strategy corresponding to this stream includes
1868 // the requested device
1869 // - For non default requested device, currently selected device on the output is either the
1870 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001871 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1872 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001873 status_t status = NO_ERROR;
1874 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001875 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1876 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001877 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1878 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001879 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001880 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001881 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1882 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001883 continue;
1884 }
Eric Laurent794fde22016-03-11 09:50:45 -08001885 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001886 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001887 if ((curStreamDevice & device) == 0) {
1888 continue;
1889 }
1890 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001891 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001892 curStreamDevice |= device;
1893 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1894 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1895 applyDefault = true;
1896 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001897
Eric Laurent1fd372e2016-04-06 14:23:53 -07001898 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001899 status_t volStatus =
1900 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1901 if (volStatus != NO_ERROR) {
1902 status = volStatus;
1903 }
1904 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001905 }
Eric Laurente552edb2014-03-10 17:42:56 -07001906 }
1907 return status;
1908}
1909
Eric Laurente0720872014-03-11 09:30:41 -07001910status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001911 int *index,
1912 audio_devices_t device)
1913{
1914 if (index == NULL) {
1915 return BAD_VALUE;
1916 }
1917 if (!audio_is_output_device(device)) {
1918 return BAD_VALUE;
1919 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001920 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001921 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001922 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001923 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1924 }
François Gaffiedfd74092015-03-19 12:10:59 +01001925 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001926
François Gaffied1ab2bd2015-12-02 18:20:06 +01001927 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001928 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1929 return NO_ERROR;
1930}
1931
Eric Laurente0720872014-03-11 09:30:41 -07001932audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001933 const SortedVector<audio_io_handle_t>& outputs)
1934{
1935 // select one output among several suitable for global effects.
1936 // The priority is as follows:
1937 // 1: An offloaded output. If the effect ends up not being offloadable,
1938 // AudioFlinger will invalidate the track and the offloaded output
1939 // will be closed causing the effect to be moved to a PCM output.
1940 // 2: A deep buffer output
1941 // 3: the first output in the list
1942
1943 if (outputs.size() == 0) {
1944 return 0;
1945 }
1946
1947 audio_io_handle_t outputOffloaded = 0;
1948 audio_io_handle_t outputDeepBuffer = 0;
1949
1950 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001951 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001952 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001953 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1954 outputOffloaded = outputs[i];
1955 }
1956 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1957 outputDeepBuffer = outputs[i];
1958 }
1959 }
1960
1961 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1962 outputOffloaded, outputDeepBuffer);
1963 if (outputOffloaded != 0) {
1964 return outputOffloaded;
1965 }
1966 if (outputDeepBuffer != 0) {
1967 return outputDeepBuffer;
1968 }
1969
1970 return outputs[0];
1971}
1972
Eric Laurente0720872014-03-11 09:30:41 -07001973audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001974{
1975 // apply simple rule where global effects are attached to the same output as MUSIC streams
1976
Eric Laurent3b73df72014-03-11 09:06:29 -07001977 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001978 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1979 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1980
1981 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1982 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1983 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1984
1985 return output;
1986}
1987
Eric Laurente0720872014-03-11 09:30:41 -07001988status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001989 audio_io_handle_t io,
1990 uint32_t strategy,
1991 int session,
1992 int id)
1993{
1994 ssize_t index = mOutputs.indexOfKey(io);
1995 if (index < 0) {
1996 index = mInputs.indexOfKey(io);
1997 if (index < 0) {
1998 ALOGW("registerEffect() unknown io %d", io);
1999 return INVALID_OPERATION;
2000 }
2001 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002002 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002003}
2004
Eric Laurentc75307b2015-03-17 15:29:32 -07002005bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2006{
Eric Laurent28d09f02016-03-08 10:43:05 -08002007 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002008 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2009 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002010 continue;
2011 }
2012 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2013 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002014 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002015}
2016
2017bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2018{
2019 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2020}
2021
Eric Laurente0720872014-03-11 09:30:41 -07002022bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002023{
2024 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002025 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002026 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002027 return true;
2028 }
2029 }
2030 return false;
2031}
2032
Eric Laurent275e8e92014-11-30 15:14:47 -08002033// Register a list of custom mixes with their attributes and format.
2034// When a mix is registered, corresponding input and output profiles are
2035// added to the remote submix hw module. The profile contains only the
2036// parameters (sampling rate, format...) specified by the mix.
2037// The corresponding input remote submix device is also connected.
2038//
2039// When a remote submix device is connected, the address is checked to select the
2040// appropriate profile and the corresponding input or output stream is opened.
2041//
2042// When capture starts, getInputForAttr() will:
2043// - 1 look for a mix matching the address passed in attribtutes tags if any
2044// - 2 if none found, getDeviceForInputSource() will:
2045// - 2.1 look for a mix matching the attributes source
2046// - 2.2 if none found, default to device selection by policy rules
2047// At this time, the corresponding output remote submix device is also connected
2048// and active playback use cases can be transferred to this mix if needed when reconnecting
2049// after AudioTracks are invalidated
2050//
2051// When playback starts, getOutputForAttr() will:
2052// - 1 look for a mix matching the address passed in attribtutes tags if any
2053// - 2 if none found, look for a mix matching the attributes usage
2054// - 3 if none found, default to device and output selection by policy rules.
2055
2056status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2057{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002058 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2059 status_t res = NO_ERROR;
2060
2061 sp<HwModule> rSubmixModule;
2062 // examine each mix's route type
2063 for (size_t i = 0; i < mixes.size(); i++) {
2064 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2065 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2066 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002067 break;
2068 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002069 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2070 // Loop back through "remote submix"
2071 if (rSubmixModule == 0) {
2072 for (size_t j = 0; i < mHwModules.size(); j++) {
2073 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2074 && mHwModules[j]->mHandle != 0) {
2075 rSubmixModule = mHwModules[j];
2076 break;
2077 }
2078 }
2079 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002080
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002081 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002082
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002083 if (rSubmixModule == 0) {
2084 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2085 res = INVALID_OPERATION;
2086 break;
2087 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002088
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002089 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002090
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002091 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002092 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002093 res = INVALID_OPERATION;
2094 break;
2095 }
2096 audio_config_t outputConfig = mixes[i].mFormat;
2097 audio_config_t inputConfig = mixes[i].mFormat;
2098 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2099 // stereo and let audio flinger do the channel conversion if needed.
2100 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2101 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2102 rSubmixModule->addOutputProfile(address, &outputConfig,
2103 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2104 rSubmixModule->addInputProfile(address, &inputConfig,
2105 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002106
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002107 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2108 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2109 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2110 address.string(), "remote-submix");
2111 } else {
2112 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2113 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2114 address.string(), "remote-submix");
2115 }
2116 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002117 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002118 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002119 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2120 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002121
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002122 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002123 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2124 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2125 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2126 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2127 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2128 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002129 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2130 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002131 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2132 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002133 } else {
2134 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002135 }
2136 break;
2137 }
2138 }
2139
2140 if (res != NO_ERROR) {
2141 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002142 i, device, address.string());
2143 res = INVALID_OPERATION;
2144 break;
2145 } else if (!foundOutput) {
2146 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2147 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002148 res = INVALID_OPERATION;
2149 break;
2150 }
Eric Laurentc722f302014-12-10 11:21:49 -08002151 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002152 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002153 if (res != NO_ERROR) {
2154 unregisterPolicyMixes(mixes);
2155 }
2156 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002157}
2158
2159status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2160{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002161 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002162 status_t res = NO_ERROR;
2163 sp<HwModule> rSubmixModule;
2164 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002165 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002166 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002167
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002168 if (rSubmixModule == 0) {
2169 for (size_t j = 0; i < mHwModules.size(); j++) {
2170 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2171 && mHwModules[j]->mHandle != 0) {
2172 rSubmixModule = mHwModules[j];
2173 break;
2174 }
2175 }
2176 }
2177 if (rSubmixModule == 0) {
2178 res = INVALID_OPERATION;
2179 continue;
2180 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002181
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002182 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002183
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002184 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2185 res = INVALID_OPERATION;
2186 continue;
2187 }
2188
2189 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2190 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2191 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2192 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2193 address.string(), "remote-submix");
2194 }
2195 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2196 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2197 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2198 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2199 address.string(), "remote-submix");
2200 }
2201 rSubmixModule->removeOutputProfile(address);
2202 rSubmixModule->removeInputProfile(address);
2203
2204 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2205 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2206 res = INVALID_OPERATION;
2207 continue;
2208 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002209 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002210 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002211 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002212}
2213
Eric Laurente552edb2014-03-10 17:42:56 -07002214
Eric Laurente0720872014-03-11 09:30:41 -07002215status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002216{
2217 const size_t SIZE = 256;
2218 char buffer[SIZE];
2219 String8 result;
2220
2221 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2222 result.append(buffer);
2223
Eric Laurent87ffa392015-05-22 10:32:38 -07002224 snprintf(buffer, SIZE, " Primary Output: %d\n",
2225 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002226 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002227 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002228 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002229 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002230 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002231 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002232 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
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 dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
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 system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002239 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002240 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002241 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002242 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002243 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2244 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2245 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002246 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2247 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002248 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2249 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002250
François Gaffie2110e042015-03-24 08:41:51 +01002251 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002252
François Gaffie112b0af2015-11-19 16:13:25 +01002253 mAvailableOutputDevices.dump(fd, String8("Available output"));
2254 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002255 mHwModules.dump(fd);
2256 mOutputs.dump(fd);
2257 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002258 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002259 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002260 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002261
2262 return NO_ERROR;
2263}
2264
2265// This function checks for the parameters which can be offloaded.
2266// This can be enhanced depending on the capability of the DSP and policy
2267// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002268bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002269{
2270 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002271 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002272 offloadInfo.sample_rate, offloadInfo.channel_mask,
2273 offloadInfo.format,
2274 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2275 offloadInfo.has_video);
2276
Andy Hung2ddee192015-12-18 17:34:44 -08002277 if (mMasterMono) {
2278 return false; // no offloading if mono is set.
2279 }
2280
Eric Laurente552edb2014-03-10 17:42:56 -07002281 // Check if offload has been disabled
2282 char propValue[PROPERTY_VALUE_MAX];
2283 if (property_get("audio.offload.disable", propValue, "0")) {
2284 if (atoi(propValue) != 0) {
2285 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2286 return false;
2287 }
2288 }
2289
2290 // Check if stream type is music, then only allow offload as of now.
2291 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2292 {
2293 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2294 return false;
2295 }
2296
2297 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002298 const bool allowOffloadWithVideo =
2299 property_get_bool("audio.offload.video", false /* default_value */);
2300 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002301 ALOGV("isOffloadSupported: has_video == true, returning false");
2302 return false;
2303 }
2304
2305 //If duration is less than minimum value defined in property, return false
2306 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2307 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2308 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2309 return false;
2310 }
2311 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2312 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2313 return false;
2314 }
2315
2316 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2317 // creating an offloaded track and tearing it down immediately after start when audioflinger
2318 // detects there is an active non offloadable effect.
2319 // FIXME: We should check the audio session here but we do not have it in this context.
2320 // This may prevent offloading in rare situations where effects are left active by apps
2321 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002322 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002323 return false;
2324 }
2325
2326 // See if there is a profile to support this.
2327 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002328 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002329 offloadInfo.sample_rate,
2330 offloadInfo.format,
2331 offloadInfo.channel_mask,
2332 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002333 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2334 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002335}
2336
Eric Laurent6a94d692014-05-20 11:18:06 -07002337status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2338 audio_port_type_t type,
2339 unsigned int *num_ports,
2340 struct audio_port *ports,
2341 unsigned int *generation)
2342{
2343 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2344 generation == NULL) {
2345 return BAD_VALUE;
2346 }
2347 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2348 if (ports == NULL) {
2349 *num_ports = 0;
2350 }
2351
2352 size_t portsWritten = 0;
2353 size_t portsMax = *num_ports;
2354 *num_ports = 0;
2355 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002356 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2357 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002358 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002359 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2360 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2361 continue;
2362 }
2363 if (portsWritten < portsMax) {
2364 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2365 }
2366 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002367 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002368 }
2369 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002370 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2371 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2372 continue;
2373 }
2374 if (portsWritten < portsMax) {
2375 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2376 }
2377 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002378 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002379 }
2380 }
2381 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2382 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2383 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2384 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2385 }
2386 *num_ports += mInputs.size();
2387 }
2388 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002389 size_t numOutputs = 0;
2390 for (size_t i = 0; i < mOutputs.size(); i++) {
2391 if (!mOutputs[i]->isDuplicated()) {
2392 numOutputs++;
2393 if (portsWritten < portsMax) {
2394 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2395 }
2396 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002397 }
Eric Laurent84c70242014-06-23 08:46:27 -07002398 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002399 }
2400 }
2401 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002402 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002403 return NO_ERROR;
2404}
2405
2406status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2407{
2408 return NO_ERROR;
2409}
2410
Eric Laurent6a94d692014-05-20 11:18:06 -07002411status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2412 audio_patch_handle_t *handle,
2413 uid_t uid)
2414{
2415 ALOGV("createAudioPatch()");
2416
2417 if (handle == NULL || patch == NULL) {
2418 return BAD_VALUE;
2419 }
2420 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2421
Eric Laurent874c42872014-08-08 15:13:39 -07002422 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2423 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2424 return BAD_VALUE;
2425 }
2426 // only one source per audio patch supported for now
2427 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002428 return INVALID_OPERATION;
2429 }
Eric Laurent874c42872014-08-08 15:13:39 -07002430
2431 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 return INVALID_OPERATION;
2433 }
Eric Laurent874c42872014-08-08 15:13:39 -07002434 for (size_t i = 0; i < patch->num_sinks; i++) {
2435 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2436 return INVALID_OPERATION;
2437 }
2438 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002439
2440 sp<AudioPatch> patchDesc;
2441 ssize_t index = mAudioPatches.indexOfKey(*handle);
2442
Eric Laurent6a94d692014-05-20 11:18:06 -07002443 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2444 patch->sources[0].role,
2445 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002446#if LOG_NDEBUG == 0
2447 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002448 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002449 patch->sinks[i].role,
2450 patch->sinks[i].type);
2451 }
2452#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002453
2454 if (index >= 0) {
2455 patchDesc = mAudioPatches.valueAt(index);
2456 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2457 mUidCached, patchDesc->mUid, uid);
2458 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2459 return INVALID_OPERATION;
2460 }
2461 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002462 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002463 }
2464
2465 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002466 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002467 if (outputDesc == NULL) {
2468 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2469 return BAD_VALUE;
2470 }
Eric Laurent84c70242014-06-23 08:46:27 -07002471 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2472 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002473 if (patchDesc != 0) {
2474 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2475 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2476 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2477 return BAD_VALUE;
2478 }
2479 }
Eric Laurent874c42872014-08-08 15:13:39 -07002480 DeviceVector devices;
2481 for (size_t i = 0; i < patch->num_sinks; i++) {
2482 // Only support mix to devices connection
2483 // TODO add support for mix to mix connection
2484 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2485 ALOGV("createAudioPatch() source mix but sink is not a device");
2486 return INVALID_OPERATION;
2487 }
2488 sp<DeviceDescriptor> devDesc =
2489 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2490 if (devDesc == 0) {
2491 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2492 return BAD_VALUE;
2493 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002494
François Gaffie53615e22015-03-19 09:24:12 +01002495 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002496 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002497 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002498 NULL, // updatedSamplingRate
2499 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002500 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002501 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002502 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002503 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002504 ALOGV("createAudioPatch() profile not supported for device %08x",
2505 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002506 return INVALID_OPERATION;
2507 }
2508 devices.add(devDesc);
2509 }
2510 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002511 return INVALID_OPERATION;
2512 }
Eric Laurent874c42872014-08-08 15:13:39 -07002513
Eric Laurent6a94d692014-05-20 11:18:06 -07002514 // TODO: reconfigure output format and channels here
2515 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002516 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002517 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002518 index = mAudioPatches.indexOfKey(*handle);
2519 if (index >= 0) {
2520 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2521 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2522 }
2523 patchDesc = mAudioPatches.valueAt(index);
2524 patchDesc->mUid = uid;
2525 ALOGV("createAudioPatch() success");
2526 } else {
2527 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2528 return INVALID_OPERATION;
2529 }
2530 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2531 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2532 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002533 // only one sink supported when connecting an input device to a mix
2534 if (patch->num_sinks > 1) {
2535 return INVALID_OPERATION;
2536 }
François Gaffie53615e22015-03-19 09:24:12 +01002537 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002538 if (inputDesc == NULL) {
2539 return BAD_VALUE;
2540 }
2541 if (patchDesc != 0) {
2542 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2543 return BAD_VALUE;
2544 }
2545 }
2546 sp<DeviceDescriptor> devDesc =
2547 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2548 if (devDesc == 0) {
2549 return BAD_VALUE;
2550 }
2551
François Gaffie53615e22015-03-19 09:24:12 +01002552 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002553 devDesc->mAddress,
2554 patch->sinks[0].sample_rate,
2555 NULL, /*updatedSampleRate*/
2556 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002557 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002558 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002559 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002560 // FIXME for the parameter type,
2561 // and the NONE
2562 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002563 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002564 return INVALID_OPERATION;
2565 }
2566 // TODO: reconfigure output format and channels here
2567 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002568 devDesc->type(), inputDesc->mIoHandle);
2569 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002570 index = mAudioPatches.indexOfKey(*handle);
2571 if (index >= 0) {
2572 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2573 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2574 }
2575 patchDesc = mAudioPatches.valueAt(index);
2576 patchDesc->mUid = uid;
2577 ALOGV("createAudioPatch() success");
2578 } else {
2579 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2580 return INVALID_OPERATION;
2581 }
2582 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2583 // device to device connection
2584 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002585 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002586 return BAD_VALUE;
2587 }
2588 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002589 sp<DeviceDescriptor> srcDeviceDesc =
2590 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002591 if (srcDeviceDesc == 0) {
2592 return BAD_VALUE;
2593 }
Eric Laurent874c42872014-08-08 15:13:39 -07002594
Eric Laurent6a94d692014-05-20 11:18:06 -07002595 //update source and sink with our own data as the data passed in the patch may
2596 // be incomplete.
2597 struct audio_patch newPatch = *patch;
2598 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002599
Eric Laurent874c42872014-08-08 15:13:39 -07002600 for (size_t i = 0; i < patch->num_sinks; i++) {
2601 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2602 ALOGV("createAudioPatch() source device but one sink is not a device");
2603 return INVALID_OPERATION;
2604 }
2605
2606 sp<DeviceDescriptor> sinkDeviceDesc =
2607 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2608 if (sinkDeviceDesc == 0) {
2609 return BAD_VALUE;
2610 }
2611 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2612
Eric Laurent3bcf8592015-04-03 12:13:24 -07002613 // create a software bridge in PatchPanel if:
2614 // - source and sink devices are on differnt HW modules OR
2615 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002616 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002617 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002618 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002619 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002620 return INVALID_OPERATION;
2621 }
Eric Laurent874c42872014-08-08 15:13:39 -07002622 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002623 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002624 // if the sink device is reachable via an opened output stream, request to go via
2625 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002626 audio_io_handle_t output = selectOutput(outputs,
2627 AUDIO_OUTPUT_FLAG_NONE,
2628 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002629 if (output != AUDIO_IO_HANDLE_NONE) {
2630 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2631 if (outputDesc->isDuplicated()) {
2632 return INVALID_OPERATION;
2633 }
2634 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002635 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002636 newPatch.num_sources = 2;
2637 }
Eric Laurent83b88082014-06-20 18:31:16 -07002638 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002639 }
2640 // TODO: check from routing capabilities in config file and other conflicting patches
2641
2642 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2643 if (index >= 0) {
2644 afPatchHandle = patchDesc->mAfPatchHandle;
2645 }
2646
2647 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2648 &afPatchHandle,
2649 0);
2650 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2651 status, afPatchHandle);
2652 if (status == NO_ERROR) {
2653 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002654 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002655 addAudioPatch(patchDesc->mHandle, patchDesc);
2656 } else {
2657 patchDesc->mPatch = newPatch;
2658 }
2659 patchDesc->mAfPatchHandle = afPatchHandle;
2660 *handle = patchDesc->mHandle;
2661 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002662 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002663 } else {
2664 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2665 status);
2666 return INVALID_OPERATION;
2667 }
2668 } else {
2669 return BAD_VALUE;
2670 }
2671 } else {
2672 return BAD_VALUE;
2673 }
2674 return NO_ERROR;
2675}
2676
2677status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2678 uid_t uid)
2679{
2680 ALOGV("releaseAudioPatch() patch %d", handle);
2681
2682 ssize_t index = mAudioPatches.indexOfKey(handle);
2683
2684 if (index < 0) {
2685 return BAD_VALUE;
2686 }
2687 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2688 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2689 mUidCached, patchDesc->mUid, uid);
2690 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2691 return INVALID_OPERATION;
2692 }
2693
2694 struct audio_patch *patch = &patchDesc->mPatch;
2695 patchDesc->mUid = mUidCached;
2696 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002697 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002698 if (outputDesc == NULL) {
2699 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2700 return BAD_VALUE;
2701 }
2702
Eric Laurentc75307b2015-03-17 15:29:32 -07002703 setOutputDevice(outputDesc,
2704 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002705 true,
2706 0,
2707 NULL);
2708 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2709 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002710 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002711 if (inputDesc == NULL) {
2712 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2713 return BAD_VALUE;
2714 }
2715 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002716 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002717 true,
2718 NULL);
2719 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2720 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2721 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2722 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2723 status, patchDesc->mAfPatchHandle);
2724 removeAudioPatch(patchDesc->mHandle);
2725 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002726 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002727 } else {
2728 return BAD_VALUE;
2729 }
2730 } else {
2731 return BAD_VALUE;
2732 }
2733 return NO_ERROR;
2734}
2735
2736status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2737 struct audio_patch *patches,
2738 unsigned int *generation)
2739{
François Gaffie53615e22015-03-19 09:24:12 +01002740 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002741 return BAD_VALUE;
2742 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002743 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002744 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002745}
2746
Eric Laurente1715a42014-05-20 11:30:42 -07002747status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002748{
Eric Laurente1715a42014-05-20 11:30:42 -07002749 ALOGV("setAudioPortConfig()");
2750
2751 if (config == NULL) {
2752 return BAD_VALUE;
2753 }
2754 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2755 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002756 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2757 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002758 }
2759
Eric Laurenta121f902014-06-03 13:32:54 -07002760 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002761 if (config->type == AUDIO_PORT_TYPE_MIX) {
2762 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002763 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002764 if (outputDesc == NULL) {
2765 return BAD_VALUE;
2766 }
Eric Laurent84c70242014-06-23 08:46:27 -07002767 ALOG_ASSERT(!outputDesc->isDuplicated(),
2768 "setAudioPortConfig() called on duplicated output %d",
2769 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002770 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002771 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002772 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002773 if (inputDesc == NULL) {
2774 return BAD_VALUE;
2775 }
Eric Laurenta121f902014-06-03 13:32:54 -07002776 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002777 } else {
2778 return BAD_VALUE;
2779 }
2780 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2781 sp<DeviceDescriptor> deviceDesc;
2782 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2783 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2784 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2785 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2786 } else {
2787 return BAD_VALUE;
2788 }
2789 if (deviceDesc == NULL) {
2790 return BAD_VALUE;
2791 }
Eric Laurenta121f902014-06-03 13:32:54 -07002792 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002793 } else {
2794 return BAD_VALUE;
2795 }
2796
Eric Laurenta121f902014-06-03 13:32:54 -07002797 struct audio_port_config backupConfig;
2798 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2799 if (status == NO_ERROR) {
2800 struct audio_port_config newConfig;
2801 audioPortConfig->toAudioPortConfig(&newConfig, config);
2802 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002803 }
Eric Laurenta121f902014-06-03 13:32:54 -07002804 if (status != NO_ERROR) {
2805 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002806 }
Eric Laurente1715a42014-05-20 11:30:42 -07002807
2808 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002809}
2810
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002811void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2812{
Eric Laurentd60560a2015-04-10 11:31:20 -07002813 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002814 clearAudioPatches(uid);
2815 clearSessionRoutes(uid);
2816}
2817
Eric Laurent6a94d692014-05-20 11:18:06 -07002818void AudioPolicyManager::clearAudioPatches(uid_t uid)
2819{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002820 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002821 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2822 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002823 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002824 }
2825 }
2826}
2827
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002828void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2829 audio_io_handle_t ouptutToSkip)
2830{
2831 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2832 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2833 for (size_t j = 0; j < mOutputs.size(); j++) {
2834 if (mOutputs.keyAt(j) == ouptutToSkip) {
2835 continue;
2836 }
2837 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2838 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2839 continue;
2840 }
2841 // If the default device for this strategy is on another output mix,
2842 // invalidate all tracks in this strategy to force re connection.
2843 // Otherwise select new device on the output mix.
2844 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002845 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002846 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2847 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2848 }
2849 }
2850 } else {
2851 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2852 setOutputDevice(outputDesc, newDevice, false);
2853 }
2854 }
2855}
2856
2857void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2858{
2859 // remove output routes associated with this uid
2860 SortedVector<routing_strategy> affectedStrategies;
2861 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2862 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2863 if (route->mUid == uid) {
2864 mOutputRoutes.removeItemsAt(i);
2865 if (route->mDeviceDescriptor != 0) {
2866 affectedStrategies.add(getStrategy(route->mStreamType));
2867 }
2868 }
2869 }
2870 // reroute outputs if necessary
2871 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2872 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2873 }
2874
2875 // remove input routes associated with this uid
2876 SortedVector<audio_source_t> affectedSources;
2877 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2878 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2879 if (route->mUid == uid) {
2880 mInputRoutes.removeItemsAt(i);
2881 if (route->mDeviceDescriptor != 0) {
2882 affectedSources.add(route->mSource);
2883 }
2884 }
2885 }
2886 // reroute inputs if necessary
2887 SortedVector<audio_io_handle_t> inputsToClose;
2888 for (size_t i = 0; i < mInputs.size(); i++) {
2889 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002890 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002891 inputsToClose.add(inputDesc->mIoHandle);
2892 }
2893 }
2894 for (size_t i = 0; i < inputsToClose.size(); i++) {
2895 closeInput(inputsToClose[i]);
2896 }
2897}
2898
Eric Laurentd60560a2015-04-10 11:31:20 -07002899void AudioPolicyManager::clearAudioSources(uid_t uid)
2900{
2901 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2902 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2903 if (sourceDesc->mUid == uid) {
2904 stopAudioSource(mAudioSources.keyAt(i));
2905 }
2906 }
2907}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002908
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002909status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2910 audio_io_handle_t *ioHandle,
2911 audio_devices_t *device)
2912{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002913 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2914 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002915 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002916
François Gaffiedf372692015-03-19 10:43:27 +01002917 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002918}
2919
Eric Laurentd60560a2015-04-10 11:31:20 -07002920status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2921 const audio_attributes_t *attributes,
2922 audio_io_handle_t *handle,
2923 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002924{
Eric Laurentd60560a2015-04-10 11:31:20 -07002925 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2926 if (source == NULL || attributes == NULL || handle == NULL) {
2927 return BAD_VALUE;
2928 }
2929
2930 *handle = AUDIO_IO_HANDLE_NONE;
2931
2932 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2933 source->type != AUDIO_PORT_TYPE_DEVICE) {
2934 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2935 return INVALID_OPERATION;
2936 }
2937
2938 sp<DeviceDescriptor> srcDeviceDesc =
2939 mAvailableInputDevices.getDevice(source->ext.device.type,
2940 String8(source->ext.device.address));
2941 if (srcDeviceDesc == 0) {
2942 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2943 return BAD_VALUE;
2944 }
2945 sp<AudioSourceDescriptor> sourceDesc =
2946 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2947
2948 struct audio_patch dummyPatch;
2949 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2950 sourceDesc->mPatchDesc = patchDesc;
2951
2952 status_t status = connectAudioSource(sourceDesc);
2953 if (status == NO_ERROR) {
2954 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2955 *handle = sourceDesc->getHandle();
2956 }
2957 return status;
2958}
2959
2960status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2961{
2962 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2963
2964 // make sure we only have one patch per source.
2965 disconnectAudioSource(sourceDesc);
2966
2967 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002968 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002969 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2970
2971 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2972 sp<DeviceDescriptor> sinkDeviceDesc =
2973 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2974
2975 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2976 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2977
2978 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2979 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002980 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002981 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2982 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2983 // create patch between src device and output device
2984 // create Hwoutput and add to mHwOutputs
2985 } else {
2986 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2987 audio_io_handle_t output =
2988 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2989 if (output == AUDIO_IO_HANDLE_NONE) {
2990 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2991 return INVALID_OPERATION;
2992 }
2993 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2994 if (outputDesc->isDuplicated()) {
2995 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2996 return INVALID_OPERATION;
2997 }
2998 // create a special patch with no sink and two sources:
2999 // - the second source indicates to PatchPanel through which output mix this patch should
3000 // be connected as well as the stream type for volume control
3001 // - the sink is defined by whatever output device is currently selected for the output
3002 // though which this patch is routed.
3003 patch->num_sinks = 0;
3004 patch->num_sources = 2;
3005 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3006 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3007 patch->sources[1].ext.mix.usecase.stream = stream;
3008 status_t status = mpClientInterface->createAudioPatch(patch,
3009 &afPatchHandle,
3010 0);
3011 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3012 status, afPatchHandle);
3013 if (status != NO_ERROR) {
3014 ALOGW("%s patch panel could not connect device patch, error %d",
3015 __FUNCTION__, status);
3016 return INVALID_OPERATION;
3017 }
3018 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003019 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003020
3021 if (status != NO_ERROR) {
3022 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3023 return status;
3024 }
3025 sourceDesc->mSwOutput = outputDesc;
3026 if (delayMs != 0) {
3027 usleep(delayMs * 1000);
3028 }
3029 }
3030
3031 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3032 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3033
3034 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003035}
3036
Eric Laurent493404d2015-04-21 15:07:36 -07003037status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003038{
Eric Laurentd60560a2015-04-10 11:31:20 -07003039 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3040 ALOGV("%s handle %d", __FUNCTION__, handle);
3041 if (sourceDesc == 0) {
3042 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3043 return BAD_VALUE;
3044 }
3045 status_t status = disconnectAudioSource(sourceDesc);
3046
3047 mAudioSources.removeItem(handle);
3048 return status;
3049}
3050
Andy Hung2ddee192015-12-18 17:34:44 -08003051status_t AudioPolicyManager::setMasterMono(bool mono)
3052{
3053 if (mMasterMono == mono) {
3054 return NO_ERROR;
3055 }
3056 mMasterMono = mono;
3057 // if enabling mono we close all offloaded devices, which will invalidate the
3058 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3059 // for recreating the new AudioTrack as non-offloaded PCM.
3060 //
3061 // If disabling mono, we leave all tracks as is: we don't know which clients
3062 // and tracks are able to be recreated as offloaded. The next "song" should
3063 // play back offloaded.
3064 if (mMasterMono) {
3065 Vector<audio_io_handle_t> offloaded;
3066 for (size_t i = 0; i < mOutputs.size(); ++i) {
3067 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3068 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3069 offloaded.push(desc->mIoHandle);
3070 }
3071 }
3072 for (size_t i = 0; i < offloaded.size(); ++i) {
3073 closeOutput(offloaded[i]);
3074 }
3075 }
3076 // update master mono for all remaining outputs
3077 for (size_t i = 0; i < mOutputs.size(); ++i) {
3078 updateMono(mOutputs.keyAt(i));
3079 }
3080 return NO_ERROR;
3081}
3082
3083status_t AudioPolicyManager::getMasterMono(bool *mono)
3084{
3085 *mono = mMasterMono;
3086 return NO_ERROR;
3087}
3088
Eric Laurentd60560a2015-04-10 11:31:20 -07003089status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3090{
3091 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3092
3093 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3094 if (patchDesc == 0) {
3095 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3096 sourceDesc->mPatchDesc->mHandle);
3097 return BAD_VALUE;
3098 }
3099 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3100
Eric Laurent28d09f02016-03-08 10:43:05 -08003101 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003102 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3103 if (swOutputDesc != 0) {
3104 stopSource(swOutputDesc, stream, false);
3105 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3106 } else {
3107 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3108 if (hwOutputDesc != 0) {
3109 // release patch between src device and output device
3110 // close Hwoutput and remove from mHwOutputs
3111 } else {
3112 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3113 }
3114 }
3115 return NO_ERROR;
3116}
3117
3118sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3119 audio_io_handle_t output, routing_strategy strategy)
3120{
3121 sp<AudioSourceDescriptor> source;
3122 for (size_t i = 0; i < mAudioSources.size(); i++) {
3123 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3124 routing_strategy sourceStrategy =
3125 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3126 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3127 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3128 source = sourceDesc;
3129 break;
3130 }
3131 }
3132 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003133}
3134
Eric Laurente552edb2014-03-10 17:42:56 -07003135// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003136// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003137// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003138uint32_t AudioPolicyManager::nextAudioPortGeneration()
3139{
3140 return android_atomic_inc(&mAudioPortGeneration);
3141}
3142
Eric Laurente0720872014-03-11 09:30:41 -07003143AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003144 :
3145#ifdef AUDIO_POLICY_TEST
3146 Thread(false),
3147#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003148 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003149 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003150 mAudioPortGeneration(1),
3151 mBeaconMuteRefCount(0),
3152 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003153 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003154 mTtsOutputAvailable(false),
3155 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003156{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003157 mUidCached = getuid();
3158 mpClientInterface = clientInterface;
3159
3160 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3161 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3162 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3163 bool speakerDrcEnabled = false;
3164
3165#ifdef USE_XML_AUDIO_POLICY_CONF
3166 mVolumeCurves = new VolumeCurvesCollection();
3167 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3168 mDefaultOutputDevice, speakerDrcEnabled,
3169 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3170 PolicySerializer serializer;
3171 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3172#else
3173 mVolumeCurves = new StreamDescriptorCollection();
3174 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3175 mDefaultOutputDevice, speakerDrcEnabled);
3176 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3177 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3178#endif
3179 ALOGE("could not load audio policy configuration file, setting defaults");
3180 config.setDefault();
3181 }
3182 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3183 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3184
3185 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003186 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3187 if (!engineInstance) {
3188 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3189 return;
3190 }
3191 // Retrieve the Policy Manager Interface
3192 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3193 if (mEngine == NULL) {
3194 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3195 return;
3196 }
3197 mEngine->setObserver(this);
3198 status_t status = mEngine->initCheck();
3199 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3200
Eric Laurent3a4311c2014-03-17 12:00:47 -07003201 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003202 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003203 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3204 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003205 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003206 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003207 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003208 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003209 continue;
3210 }
3211 // open all output streams needed to access attached devices
3212 // except for direct output streams that are only opened when they are actually
3213 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003214 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003215 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3216 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003217 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003218
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003219 if (!outProfile->hasSupportedDevices()) {
3220 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003221 continue;
3222 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003223 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003224 mTtsOutputAvailable = true;
3225 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003226
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003227 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003228 continue;
3229 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003230 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003231 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3232 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003233 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003234 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003235 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003236 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003237 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003238 if ((profileType & outputDeviceTypes) == 0) {
3239 continue;
3240 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003241 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3242 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003243 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3244 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3245 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3246 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003247
3248 outputDesc->mDevice = profileType;
3249 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3250 config.sample_rate = outputDesc->mSamplingRate;
3251 config.channel_mask = outputDesc->mChannelMask;
3252 config.format = outputDesc->mFormat;
3253 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003254 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003255 &output,
3256 &config,
3257 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003258 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003259 &outputDesc->mLatency,
3260 outputDesc->mFlags);
3261
3262 if (status != NO_ERROR) {
3263 ALOGW("Cannot open output stream for device %08x on hw module %s",
3264 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003265 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003266 } else {
3267 outputDesc->mSamplingRate = config.sample_rate;
3268 outputDesc->mChannelMask = config.channel_mask;
3269 outputDesc->mFormat = config.format;
3270
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003271 for (size_t k = 0; k < supportedDevices.size(); k++) {
3272 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003273 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003274 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3275 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003276 }
3277 }
3278 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003279 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003280 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003281 }
3282 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003283 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003284 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003285 true,
3286 0,
3287 NULL,
3288 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003289 }
Eric Laurente552edb2014-03-10 17:42:56 -07003290 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003291 // open input streams needed to access attached devices to validate
3292 // mAvailableInputDevices list
3293 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3294 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003295 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003296
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003297 if (!inProfile->hasSupportedDevices()) {
3298 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003299 continue;
3300 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003301 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003302 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003303 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3304
Eric Laurentd78f1532014-09-16 16:38:20 -07003305 if ((profileType & inputDeviceTypes) == 0) {
3306 continue;
3307 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003308 sp<AudioInputDescriptor> inputDesc =
3309 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003310
Eric Laurentd78f1532014-09-16 16:38:20 -07003311 inputDesc->mDevice = profileType;
3312
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003313 // find the address
3314 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3315 // the inputs vector must be of size 1, but we don't want to crash here
3316 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3317 : String8("");
3318 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3319 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3320
Eric Laurentd78f1532014-09-16 16:38:20 -07003321 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3322 config.sample_rate = inputDesc->mSamplingRate;
3323 config.channel_mask = inputDesc->mChannelMask;
3324 config.format = inputDesc->mFormat;
3325 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003326 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003327 &input,
3328 &config,
3329 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003330 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003331 AUDIO_SOURCE_MIC,
3332 AUDIO_INPUT_FLAG_NONE);
3333
3334 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003335 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3336 for (size_t k = 0; k < supportedDevices.size(); k++) {
3337 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003338 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003339 if (index >= 0) {
3340 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3341 if (!devDesc->isAttached()) {
3342 devDesc->attach(mHwModules[i]);
3343 devDesc->importAudioPort(inProfile);
3344 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003345 }
3346 }
3347 mpClientInterface->closeInput(input);
3348 } else {
3349 ALOGW("Cannot open input stream for device %08x on hw module %s",
3350 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003351 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003352 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003353 }
3354 }
3355 // make sure all attached devices have been allocated a unique ID
3356 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003357 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003358 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003359 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3360 continue;
3361 }
François Gaffie2110e042015-03-24 08:41:51 +01003362 // The device is now validated and can be appended to the available devices of the engine
3363 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3364 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003365 i++;
3366 }
3367 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003368 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003369 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003370 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3371 continue;
3372 }
François Gaffie2110e042015-03-24 08:41:51 +01003373 // The device is now validated and can be appended to the available devices of the engine
3374 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3375 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003376 i++;
3377 }
3378 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003379 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003380 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003381 }
Eric Laurente552edb2014-03-10 17:42:56 -07003382
3383 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3384
3385 updateDevicesAndOutputs();
3386
3387#ifdef AUDIO_POLICY_TEST
3388 if (mPrimaryOutput != 0) {
3389 AudioParameter outputCmd = AudioParameter();
3390 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003391 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003392
3393 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3394 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003395 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3396 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003397 mTestLatencyMs = 0;
3398 mCurOutput = 0;
3399 mDirectOutput = false;
3400 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3401 mTestOutputs[i] = 0;
3402 }
3403
3404 const size_t SIZE = 256;
3405 char buffer[SIZE];
3406 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3407 run(buffer, ANDROID_PRIORITY_AUDIO);
3408 }
3409#endif //AUDIO_POLICY_TEST
3410}
3411
Eric Laurente0720872014-03-11 09:30:41 -07003412AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003413{
3414#ifdef AUDIO_POLICY_TEST
3415 exit();
3416#endif //AUDIO_POLICY_TEST
3417 for (size_t i = 0; i < mOutputs.size(); i++) {
3418 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003419 }
3420 for (size_t i = 0; i < mInputs.size(); i++) {
3421 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003422 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003423 mAvailableOutputDevices.clear();
3424 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003425 mOutputs.clear();
3426 mInputs.clear();
3427 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003428}
3429
Eric Laurente0720872014-03-11 09:30:41 -07003430status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003431{
Eric Laurent87ffa392015-05-22 10:32:38 -07003432 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003433}
3434
3435#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003436bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003437{
3438 ALOGV("entering threadLoop()");
3439 while (!exitPending())
3440 {
3441 String8 command;
3442 int valueInt;
3443 String8 value;
3444
3445 Mutex::Autolock _l(mLock);
3446 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3447
3448 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3449 AudioParameter param = AudioParameter(command);
3450
3451 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3452 valueInt != 0) {
3453 ALOGV("Test command %s received", command.string());
3454 String8 target;
3455 if (param.get(String8("target"), target) != NO_ERROR) {
3456 target = "Manager";
3457 }
3458 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3459 param.remove(String8("test_cmd_policy_output"));
3460 mCurOutput = valueInt;
3461 }
3462 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3463 param.remove(String8("test_cmd_policy_direct"));
3464 if (value == "false") {
3465 mDirectOutput = false;
3466 } else if (value == "true") {
3467 mDirectOutput = true;
3468 }
3469 }
3470 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3471 param.remove(String8("test_cmd_policy_input"));
3472 mTestInput = valueInt;
3473 }
3474
3475 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3476 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003477 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003478 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003479 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003480 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003481 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003482 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003483 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003484 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003485 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003486 if (target == "Manager") {
3487 mTestFormat = format;
3488 } else if (mTestOutputs[mCurOutput] != 0) {
3489 AudioParameter outputParam = AudioParameter();
3490 outputParam.addInt(String8("format"), format);
3491 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3492 }
3493 }
3494 }
3495 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3496 param.remove(String8("test_cmd_policy_channels"));
3497 int channels = 0;
3498
3499 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003500 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003501 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003502 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003503 }
3504 if (channels != 0) {
3505 if (target == "Manager") {
3506 mTestChannels = channels;
3507 } else if (mTestOutputs[mCurOutput] != 0) {
3508 AudioParameter outputParam = AudioParameter();
3509 outputParam.addInt(String8("channels"), channels);
3510 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3511 }
3512 }
3513 }
3514 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3515 param.remove(String8("test_cmd_policy_sampleRate"));
3516 if (valueInt >= 0 && valueInt <= 96000) {
3517 int samplingRate = valueInt;
3518 if (target == "Manager") {
3519 mTestSamplingRate = samplingRate;
3520 } else if (mTestOutputs[mCurOutput] != 0) {
3521 AudioParameter outputParam = AudioParameter();
3522 outputParam.addInt(String8("sampling_rate"), samplingRate);
3523 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3524 }
3525 }
3526 }
3527
3528 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3529 param.remove(String8("test_cmd_policy_reopen"));
3530
Eric Laurentc75307b2015-03-17 15:29:32 -07003531 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003532
Eric Laurentc75307b2015-03-17 15:29:32 -07003533 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003534
Eric Laurentc75307b2015-03-17 15:29:32 -07003535 removeOutput(mPrimaryOutput->mIoHandle);
3536 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3537 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003538 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003539 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3540 config.sample_rate = outputDesc->mSamplingRate;
3541 config.channel_mask = outputDesc->mChannelMask;
3542 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003543 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003544 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003545 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003546 &config,
3547 &outputDesc->mDevice,
3548 String8(""),
3549 &outputDesc->mLatency,
3550 outputDesc->mFlags);
3551 if (status != NO_ERROR) {
3552 ALOGE("Failed to reopen hardware output stream, "
3553 "samplingRate: %d, format %d, channels %d",
3554 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003555 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003556 outputDesc->mSamplingRate = config.sample_rate;
3557 outputDesc->mChannelMask = config.channel_mask;
3558 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003559 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003560 AudioParameter outputCmd = AudioParameter();
3561 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003562 mpClientInterface->setParameters(handle, outputCmd.toString());
3563 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003564 }
3565 }
3566
3567
3568 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3569 }
3570 }
3571 return false;
3572}
3573
Eric Laurente0720872014-03-11 09:30:41 -07003574void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003575{
3576 {
3577 AutoMutex _l(mLock);
3578 requestExit();
3579 mWaitWorkCV.signal();
3580 }
3581 requestExitAndWait();
3582}
3583
Eric Laurente0720872014-03-11 09:30:41 -07003584int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003585{
3586 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3587 if (output == mTestOutputs[i]) return i;
3588 }
3589 return 0;
3590}
3591#endif //AUDIO_POLICY_TEST
3592
3593// ---
3594
Eric Laurentc75307b2015-03-17 15:29:32 -07003595void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003596{
François Gaffie98cc1912015-03-18 17:52:40 +01003597 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003598 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003599 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003600 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003601}
3602
François Gaffie53615e22015-03-19 09:24:12 +01003603void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3604{
3605 mOutputs.removeItem(output);
3606}
3607
Eric Laurent1f2f2232014-06-02 12:01:23 -07003608void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003609{
François Gaffie98cc1912015-03-18 17:52:40 +01003610 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003611 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003612 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003613}
Eric Laurente552edb2014-03-10 17:42:56 -07003614
Eric Laurentc75307b2015-03-17 15:29:32 -07003615void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003616 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003617 const String8 address /*in*/,
3618 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003619 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003620 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003621 if (devDesc != 0) {
3622 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3623 desc->mIoHandle, address.string());
3624 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003625 }
3626}
3627
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003628status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003629 audio_policy_dev_state_t state,
3630 SortedVector<audio_io_handle_t>& outputs,
3631 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003632{
François Gaffie53615e22015-03-19 09:24:12 +01003633 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003634 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003635
3636 if (audio_device_is_digital(device)) {
3637 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003638 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003639 }
Eric Laurente552edb2014-03-10 17:42:56 -07003640
Eric Laurent3b73df72014-03-11 09:06:29 -07003641 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003642 // first list already open outputs that can be routed to this device
3643 for (size_t i = 0; i < mOutputs.size(); i++) {
3644 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003645 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003646 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003647 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3648 outputs.add(mOutputs.keyAt(i));
3649 } else {
3650 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003651 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003652 }
Eric Laurente552edb2014-03-10 17:42:56 -07003653 }
3654 }
3655 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003656 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003657 for (size_t i = 0; i < mHwModules.size(); i++)
3658 {
3659 if (mHwModules[i]->mHandle == 0) {
3660 continue;
3661 }
3662 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3663 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003664 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003665 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003666 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003667 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003668 profiles.add(profile);
3669 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3670 }
Eric Laurente552edb2014-03-10 17:42:56 -07003671 }
3672 }
3673 }
3674
Eric Laurent7b279bb2015-12-14 10:18:23 -08003675 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003676
Eric Laurente552edb2014-03-10 17:42:56 -07003677 if (profiles.isEmpty() && outputs.isEmpty()) {
3678 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3679 return BAD_VALUE;
3680 }
3681
3682 // open outputs for matching profiles if needed. Direct outputs are also opened to
3683 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3684 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003685 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003686
3687 // nothing to do if one output is already opened for this profile
3688 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003689 for (j = 0; j < outputs.size(); j++) {
3690 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003691 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003692 // matching profile: save the sample rates, format and channel masks supported
3693 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003694 if (audio_device_is_digital(device)) {
3695 devDesc->importAudioPort(profile);
3696 }
Eric Laurente552edb2014-03-10 17:42:56 -07003697 break;
3698 }
3699 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003700 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003701 continue;
3702 }
3703
Eric Laurent83b88082014-06-20 18:31:16 -07003704 ALOGV("opening output for device %08x with params %s profile %p",
3705 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003706 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003707 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003708 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3709 config.sample_rate = desc->mSamplingRate;
3710 config.channel_mask = desc->mChannelMask;
3711 config.format = desc->mFormat;
3712 config.offload_info.sample_rate = desc->mSamplingRate;
3713 config.offload_info.channel_mask = desc->mChannelMask;
3714 config.offload_info.format = desc->mFormat;
3715 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003716 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003717 &output,
3718 &config,
3719 &desc->mDevice,
3720 address,
3721 &desc->mLatency,
3722 desc->mFlags);
3723 if (status == NO_ERROR) {
3724 desc->mSamplingRate = config.sample_rate;
3725 desc->mChannelMask = config.channel_mask;
3726 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003727
Eric Laurentd4692962014-05-05 18:13:44 -07003728 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003729 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003730 char *param = audio_device_address_to_parameter(device, address);
3731 mpClientInterface->setParameters(output, String8(param));
3732 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003733 }
Phil Burk00eeb322016-03-31 12:41:00 -07003734 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003735 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003736 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003737 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003738 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003739 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003740 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003741 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003742 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003743 config.offload_info.sample_rate = config.sample_rate;
3744 config.offload_info.channel_mask = config.channel_mask;
3745 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003746 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003747 &output,
3748 &config,
3749 &desc->mDevice,
3750 address,
3751 &desc->mLatency,
3752 desc->mFlags);
3753 if (status == NO_ERROR) {
3754 desc->mSamplingRate = config.sample_rate;
3755 desc->mChannelMask = config.channel_mask;
3756 desc->mFormat = config.format;
3757 } else {
3758 output = AUDIO_IO_HANDLE_NONE;
3759 }
Eric Laurentd4692962014-05-05 18:13:44 -07003760 }
3761
Eric Laurentcf2c0212014-07-25 16:20:43 -07003762 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003763 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003764 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003765 sp<AudioPolicyMix> policyMix;
3766 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003767 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3768 address.string());
3769 }
François Gaffie036e1e92015-03-19 10:16:24 +01003770 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003771 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003772
Eric Laurent87ffa392015-05-22 10:32:38 -07003773 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3774 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003775 // no duplicated output for direct outputs and
3776 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003777 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003778
Eric Laurentd4692962014-05-05 18:13:44 -07003779 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003780 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003781
Eric Laurentd4692962014-05-05 18:13:44 -07003782 //TODO: configure audio effect output stage here
3783
3784 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003785 duplicatedOutput =
3786 mpClientInterface->openDuplicateOutput(output,
3787 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003788 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003789 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003790 sp<SwAudioOutputDescriptor> dupOutputDesc =
3791 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3792 dupOutputDesc->mOutput1 = mPrimaryOutput;
3793 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003794 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3795 dupOutputDesc->mFormat = desc->mFormat;
3796 dupOutputDesc->mChannelMask = desc->mChannelMask;
3797 dupOutputDesc->mLatency = desc->mLatency;
3798 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003799 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003800 } else {
3801 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003802 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003803 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003804 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003805 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003806 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003807 }
Eric Laurente552edb2014-03-10 17:42:56 -07003808 }
3809 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003810 } else {
3811 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003812 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003813 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003814 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003815 profiles.removeAt(profile_index);
3816 profile_index--;
3817 } else {
3818 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003819 // Load digital format info only for digital devices
3820 if (audio_device_is_digital(device)) {
3821 devDesc->importAudioPort(profile);
3822 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003823
François Gaffie53615e22015-03-19 09:24:12 +01003824 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003825 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3826 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003827 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003828 NULL/*patch handle*/, address.string());
3829 }
Eric Laurente552edb2014-03-10 17:42:56 -07003830 ALOGV("checkOutputsForDevice(): adding output %d", output);
3831 }
3832 }
3833
3834 if (profiles.isEmpty()) {
3835 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3836 return BAD_VALUE;
3837 }
Eric Laurentd4692962014-05-05 18:13:44 -07003838 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003839 // check if one opened output is not needed any more after disconnecting one device
3840 for (size_t i = 0; i < mOutputs.size(); i++) {
3841 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003842 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003843 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003844 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003845 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003846 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003847 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003848 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3849 mOutputs.keyAt(i));
3850 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003851 }
Eric Laurente552edb2014-03-10 17:42:56 -07003852 }
3853 }
Eric Laurentd4692962014-05-05 18:13:44 -07003854 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003855 for (size_t i = 0; i < mHwModules.size(); i++)
3856 {
3857 if (mHwModules[i]->mHandle == 0) {
3858 continue;
3859 }
3860 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3861 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003862 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003863 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003864 ALOGV("checkOutputsForDevice(): "
3865 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003866 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003867 }
3868 }
3869 }
3870 }
3871 return NO_ERROR;
3872}
3873
Paul McLean9080a4c2015-06-18 08:24:02 -07003874status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003875 audio_policy_dev_state_t state,
3876 SortedVector<audio_io_handle_t>& inputs,
3877 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003878{
Paul McLean9080a4c2015-06-18 08:24:02 -07003879 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003880 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003881
3882 if (audio_device_is_digital(device)) {
3883 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003884 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003885 }
3886
Eric Laurentd4692962014-05-05 18:13:44 -07003887 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3888 // first list already open inputs that can be routed to this device
3889 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3890 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003891 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003892 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3893 inputs.add(mInputs.keyAt(input_index));
3894 }
3895 }
3896
3897 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003898 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003899 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3900 {
3901 if (mHwModules[module_idx]->mHandle == 0) {
3902 continue;
3903 }
3904 for (size_t profile_index = 0;
3905 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3906 profile_index++)
3907 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003908 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3909
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003910 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003911 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003912 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003913 profiles.add(profile);
3914 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3915 profile_index, module_idx);
3916 }
Eric Laurentd4692962014-05-05 18:13:44 -07003917 }
3918 }
3919 }
3920
3921 if (profiles.isEmpty() && inputs.isEmpty()) {
3922 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3923 return BAD_VALUE;
3924 }
3925
3926 // open inputs for matching profiles if needed. Direct inputs are also opened to
3927 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3928 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3929
Eric Laurent1c333e22014-05-20 10:48:17 -07003930 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003931 // nothing to do if one input is already opened for this profile
3932 size_t input_index;
3933 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3934 desc = mInputs.valueAt(input_index);
3935 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003936 if (audio_device_is_digital(device)) {
3937 devDesc->importAudioPort(profile);
3938 }
Eric Laurentd4692962014-05-05 18:13:44 -07003939 break;
3940 }
3941 }
3942 if (input_index != mInputs.size()) {
3943 continue;
3944 }
3945
3946 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3947 desc = new AudioInputDescriptor(profile);
3948 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003949 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3950 config.sample_rate = desc->mSamplingRate;
3951 config.channel_mask = desc->mChannelMask;
3952 config.format = desc->mFormat;
3953 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003954 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003955 &input,
3956 &config,
3957 &desc->mDevice,
3958 address,
3959 AUDIO_SOURCE_MIC,
3960 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003961
Eric Laurentcf2c0212014-07-25 16:20:43 -07003962 if (status == NO_ERROR) {
3963 desc->mSamplingRate = config.sample_rate;
3964 desc->mChannelMask = config.channel_mask;
3965 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003966
Eric Laurentd4692962014-05-05 18:13:44 -07003967 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003968 char *param = audio_device_address_to_parameter(device, address);
3969 mpClientInterface->setParameters(input, String8(param));
3970 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003971 }
Phil Burk00eeb322016-03-31 12:41:00 -07003972 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003973 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003974 ALOGW("checkInputsForDevice() direct input missing param");
3975 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003976 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003977 }
3978
3979 if (input != 0) {
3980 addInput(input, desc);
3981 }
3982 } // endif input != 0
3983
Eric Laurentcf2c0212014-07-25 16:20:43 -07003984 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003985 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003986 profiles.removeAt(profile_index);
3987 profile_index--;
3988 } else {
3989 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003990 if (audio_device_is_digital(device)) {
3991 devDesc->importAudioPort(profile);
3992 }
Eric Laurentd4692962014-05-05 18:13:44 -07003993 ALOGV("checkInputsForDevice(): adding input %d", input);
3994 }
3995 } // end scan profiles
3996
3997 if (profiles.isEmpty()) {
3998 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3999 return BAD_VALUE;
4000 }
4001 } else {
4002 // Disconnect
4003 // check if one opened input is not needed any more after disconnecting one device
4004 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4005 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004006 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004007 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4008 mInputs.keyAt(input_index));
4009 inputs.add(mInputs.keyAt(input_index));
4010 }
4011 }
4012 // Clear any profiles associated with the disconnected device.
4013 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4014 if (mHwModules[module_index]->mHandle == 0) {
4015 continue;
4016 }
4017 for (size_t profile_index = 0;
4018 profile_index < mHwModules[module_index]->mInputProfiles.size();
4019 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004020 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004021 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004022 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004023 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004024 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004025 }
4026 }
4027 }
4028 } // end disconnect
4029
4030 return NO_ERROR;
4031}
4032
4033
Eric Laurente0720872014-03-11 09:30:41 -07004034void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004035{
4036 ALOGV("closeOutput(%d)", output);
4037
Eric Laurentc75307b2015-03-17 15:29:32 -07004038 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004039 if (outputDesc == NULL) {
4040 ALOGW("closeOutput() unknown output %d", output);
4041 return;
4042 }
François Gaffie036e1e92015-03-19 10:16:24 +01004043 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004044
Eric Laurente552edb2014-03-10 17:42:56 -07004045 // look for duplicated outputs connected to the output being removed.
4046 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004047 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004048 if (dupOutputDesc->isDuplicated() &&
4049 (dupOutputDesc->mOutput1 == outputDesc ||
4050 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004051 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004052 if (dupOutputDesc->mOutput1 == outputDesc) {
4053 outputDesc2 = dupOutputDesc->mOutput2;
4054 } else {
4055 outputDesc2 = dupOutputDesc->mOutput1;
4056 }
4057 // As all active tracks on duplicated output will be deleted,
4058 // and as they were also referenced on the other output, the reference
4059 // count for their stream type must be adjusted accordingly on
4060 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004061 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004062 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004063 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004064 }
4065 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4066 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4067
4068 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004069 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004070 }
4071 }
4072
Eric Laurent05b90f82014-08-27 15:32:29 -07004073 nextAudioPortGeneration();
4074
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004075 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004076 if (index >= 0) {
4077 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4078 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4079 mAudioPatches.removeItemsAt(index);
4080 mpClientInterface->onAudioPatchListUpdate();
4081 }
4082
Eric Laurente552edb2014-03-10 17:42:56 -07004083 AudioParameter param;
4084 param.add(String8("closing"), String8("true"));
4085 mpClientInterface->setParameters(output, param.toString());
4086
4087 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004088 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004089 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004090}
4091
4092void AudioPolicyManager::closeInput(audio_io_handle_t input)
4093{
4094 ALOGV("closeInput(%d)", input);
4095
4096 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4097 if (inputDesc == NULL) {
4098 ALOGW("closeInput() unknown input %d", input);
4099 return;
4100 }
4101
Eric Laurent6a94d692014-05-20 11:18:06 -07004102 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004103
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004104 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004105 if (index >= 0) {
4106 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4107 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4108 mAudioPatches.removeItemsAt(index);
4109 mpClientInterface->onAudioPatchListUpdate();
4110 }
4111
4112 mpClientInterface->closeInput(input);
4113 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004114}
4115
Eric Laurentc75307b2015-03-17 15:29:32 -07004116SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4117 audio_devices_t device,
4118 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004119{
4120 SortedVector<audio_io_handle_t> outputs;
4121
4122 ALOGVV("getOutputsForDevice() device %04x", device);
4123 for (size_t i = 0; i < openOutputs.size(); i++) {
4124 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004125 i, openOutputs.valueAt(i)->isDuplicated(),
4126 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004127 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4128 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4129 outputs.add(openOutputs.keyAt(i));
4130 }
4131 }
4132 return outputs;
4133}
4134
Eric Laurente0720872014-03-11 09:30:41 -07004135bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004136 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004137{
4138 if (outputs1.size() != outputs2.size()) {
4139 return false;
4140 }
4141 for (size_t i = 0; i < outputs1.size(); i++) {
4142 if (outputs1[i] != outputs2[i]) {
4143 return false;
4144 }
4145 }
4146 return true;
4147}
4148
Eric Laurente0720872014-03-11 09:30:41 -07004149void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004150{
4151 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4152 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4153 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4154 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4155
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004156 // also take into account external policy-related changes: add all outputs which are
4157 // associated with policies in the "before" and "after" output vectors
4158 ALOGVV("checkOutputForStrategy(): policy related outputs");
4159 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004160 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004161 if (desc != 0 && desc->mPolicyMix != NULL) {
4162 srcOutputs.add(desc->mIoHandle);
4163 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4164 }
4165 }
4166 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004167 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004168 if (desc != 0 && desc->mPolicyMix != NULL) {
4169 dstOutputs.add(desc->mIoHandle);
4170 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4171 }
4172 }
4173
Eric Laurente552edb2014-03-10 17:42:56 -07004174 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4175 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4176 strategy, srcOutputs[0], dstOutputs[0]);
4177 // mute strategy while moving tracks from one output to another
4178 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004180 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004181 setStrategyMute(strategy, true, desc);
4182 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004183 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004184 sp<AudioSourceDescriptor> source =
4185 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4186 if (source != 0){
4187 connectAudioSource(source);
4188 }
Eric Laurente552edb2014-03-10 17:42:56 -07004189 }
4190
4191 // Move effects associated to this strategy from previous output to new output
4192 if (strategy == STRATEGY_MEDIA) {
4193 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4194 SortedVector<audio_io_handle_t> moved;
4195 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004196 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4197 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4198 effectDesc->mIo != fxOutput) {
4199 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004200 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4201 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004202 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004203 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004204 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004205 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004206 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004207 }
4208 }
4209 }
4210 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004211 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004212 if (getStrategy((audio_stream_type_t)i) == strategy) {
4213 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004214 }
4215 }
4216 }
4217}
4218
Eric Laurente0720872014-03-11 09:30:41 -07004219void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004220{
François Gaffie2110e042015-03-24 08:41:51 +01004221 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004222 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004223 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004224 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004225 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004226 checkOutputForStrategy(STRATEGY_SONIFICATION);
4227 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004228 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004229 checkOutputForStrategy(STRATEGY_MEDIA);
4230 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004231 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004232}
4233
Eric Laurente0720872014-03-11 09:30:41 -07004234void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004235{
François Gaffie53615e22015-03-19 09:24:12 +01004236 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004237 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004238 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004239 return;
4240 }
4241
Eric Laurent3a4311c2014-03-17 12:00:47 -07004242 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004243 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4244 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4245 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004246 // suspend A2DP output if:
4247 // (NOT already suspended) &&
4248 // ((SCO device is connected &&
4249 // (forced usage for communication || for record is SCO))) ||
4250 // (phone state is ringing || in call)
4251 //
4252 // restore A2DP output if:
4253 // (Already suspended) &&
4254 // ((SCO device is NOT connected ||
4255 // (forced usage NOT for communication && NOT for record is SCO))) &&
4256 // (phone state is NOT ringing && NOT in call)
4257 //
4258 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004259 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004260 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4261 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4262 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4263 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004264
4265 mpClientInterface->restoreOutput(a2dpOutput);
4266 mA2dpSuspended = false;
4267 }
4268 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004269 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004270 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4271 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4272 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4273 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004274
4275 mpClientInterface->suspendOutput(a2dpOutput);
4276 mA2dpSuspended = true;
4277 }
4278 }
4279}
4280
Eric Laurentc75307b2015-03-17 15:29:32 -07004281audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4282 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004283{
4284 audio_devices_t device = AUDIO_DEVICE_NONE;
4285
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004286 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004287 if (index >= 0) {
4288 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4289 if (patchDesc->mUid != mUidCached) {
4290 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004291 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004292 return outputDesc->device();
4293 }
4294 }
4295
Eric Laurente552edb2014-03-10 17:42:56 -07004296 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004297 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004298 // use device for strategy enforced audible
4299 // 2: we are in call or the strategy phone is active on the output:
4300 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004301 // 3: the strategy for enforced audible is active but not enforced on the output:
4302 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004303 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004304 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004305 // 5: the strategy accessibility is active on the output:
4306 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004307 // 6: the strategy "respectful" sonification is active on the output:
4308 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004309 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004310 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004311 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004312 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004313 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004314 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004315 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004316 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004317 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4318 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004319 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004320 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004321 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004322 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004323 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004324 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004325 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4326 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004327 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004328 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004329 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004330 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004331 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004332 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004333 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004334 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004335 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004336 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004337 }
4338
Eric Laurent1c333e22014-05-20 10:48:17 -07004339 ALOGV("getNewOutputDevice() selected device %x", device);
4340 return device;
4341}
4342
Eric Laurent232f26f2016-02-17 18:06:27 -08004343audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004344{
Eric Laurent232f26f2016-02-17 18:06:27 -08004345 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004346
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004347 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004348 if (index >= 0) {
4349 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4350 if (patchDesc->mUid != mUidCached) {
4351 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004352 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004353 return inputDesc->mDevice;
4354 }
4355 }
4356
Eric Laurent232f26f2016-02-17 18:06:27 -08004357 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004358
Eric Laurente552edb2014-03-10 17:42:56 -07004359 return device;
4360}
4361
Eric Laurent794fde22016-03-11 09:50:45 -08004362bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4363 audio_stream_type_t stream2) {
4364 return ((stream1 == stream2) ||
4365 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4366 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004367}
4368
Eric Laurente0720872014-03-11 09:30:41 -07004369uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004370 return (uint32_t)getStrategy(stream);
4371}
4372
Eric Laurente0720872014-03-11 09:30:41 -07004373audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004374 // By checking the range of stream before calling getStrategy, we avoid
4375 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4376 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004377 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004378 return AUDIO_DEVICE_NONE;
4379 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004380 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004381 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4382 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004383 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004384 }
Eric Laurent794fde22016-03-11 09:50:45 -08004385 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004386 audio_devices_t curDevices =
4387 getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
4388 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4389 for (size_t i = 0; i < outputs.size(); i++) {
4390 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004391 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004392 curDevices |= outputDesc->device();
4393 }
4394 }
4395 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004396 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004397
4398 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4399 and doesn't really need to.*/
4400 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4401 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4402 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4403 }
Eric Laurente552edb2014-03-10 17:42:56 -07004404 return devices;
4405}
4406
François Gaffie2110e042015-03-24 08:41:51 +01004407routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4408{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004409 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004410 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004411}
4412
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004413uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4414 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004415 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4416 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4417 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004418 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4419 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4420 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004421 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004422 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004423}
4424
Eric Laurente0720872014-03-11 09:30:41 -07004425void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004426 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004427 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004428 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4429 updateDevicesAndOutputs();
4430 break;
4431 default:
4432 break;
4433 }
4434}
4435
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004436uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004437
4438 // skip beacon mute management if a dedicated TTS output is available
4439 if (mTtsOutputAvailable) {
4440 return 0;
4441 }
4442
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004443 switch(event) {
4444 case STARTING_OUTPUT:
4445 mBeaconMuteRefCount++;
4446 break;
4447 case STOPPING_OUTPUT:
4448 if (mBeaconMuteRefCount > 0) {
4449 mBeaconMuteRefCount--;
4450 }
4451 break;
4452 case STARTING_BEACON:
4453 mBeaconPlayingRefCount++;
4454 break;
4455 case STOPPING_BEACON:
4456 if (mBeaconPlayingRefCount > 0) {
4457 mBeaconPlayingRefCount--;
4458 }
4459 break;
4460 }
4461
4462 if (mBeaconMuteRefCount > 0) {
4463 // any playback causes beacon to be muted
4464 return setBeaconMute(true);
4465 } else {
4466 // no other playback: unmute when beacon starts playing, mute when it stops
4467 return setBeaconMute(mBeaconPlayingRefCount == 0);
4468 }
4469}
4470
4471uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4472 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4473 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4474 // keep track of muted state to avoid repeating mute/unmute operations
4475 if (mBeaconMuted != mute) {
4476 // mute/unmute AUDIO_STREAM_TTS on all outputs
4477 ALOGV("\t muting %d", mute);
4478 uint32_t maxLatency = 0;
4479 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004480 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004481 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004482 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004483 0 /*delay*/, AUDIO_DEVICE_NONE);
4484 const uint32_t latency = desc->latency() * 2;
4485 if (latency > maxLatency) {
4486 maxLatency = latency;
4487 }
4488 }
4489 mBeaconMuted = mute;
4490 return maxLatency;
4491 }
4492 return 0;
4493}
4494
Eric Laurente0720872014-03-11 09:30:41 -07004495audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004496 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004497{
Paul McLeanaa981192015-03-21 09:55:15 -07004498 // Routing
4499 // see if we have an explicit route
4500 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4501 // (getStrategy(stream)).
4502 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4503 // and activity count is non-zero
4504 // the device = the device from the descriptor in the RouteMap, and exit.
4505 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4506 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004507 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4508 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004509 return route->mDeviceDescriptor->type();
4510 }
4511 }
4512
Eric Laurente552edb2014-03-10 17:42:56 -07004513 if (fromCache) {
4514 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4515 strategy, mDeviceForStrategy[strategy]);
4516 return mDeviceForStrategy[strategy];
4517 }
François Gaffie2110e042015-03-24 08:41:51 +01004518 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004519}
4520
Eric Laurente0720872014-03-11 09:30:41 -07004521void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004522{
4523 for (int i = 0; i < NUM_STRATEGIES; i++) {
4524 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4525 }
4526 mPreviousOutputs = mOutputs;
4527}
4528
Eric Laurent1f2f2232014-06-02 12:01:23 -07004529uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004530 audio_devices_t prevDevice,
4531 uint32_t delayMs)
4532{
4533 // mute/unmute strategies using an incompatible device combination
4534 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4535 // if unmuting, unmute only after the specified delay
4536 if (outputDesc->isDuplicated()) {
4537 return 0;
4538 }
4539
4540 uint32_t muteWaitMs = 0;
4541 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004542 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004543
4544 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4545 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004546 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004547 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4548 bool doMute = false;
4549
4550 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4551 doMute = true;
4552 outputDesc->mStrategyMutedByDevice[i] = true;
4553 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4554 doMute = true;
4555 outputDesc->mStrategyMutedByDevice[i] = false;
4556 }
Eric Laurent99401132014-05-07 19:48:15 -07004557 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004558 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004559 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004560 // skip output if it does not share any device with current output
4561 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4562 == AUDIO_DEVICE_NONE) {
4563 continue;
4564 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004565 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4566 mute ? "muting" : "unmuting", i, curDevice);
4567 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004568 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004569 if (mute) {
4570 // FIXME: should not need to double latency if volume could be applied
4571 // immediately by the audioflinger mixer. We must account for the delay
4572 // between now and the next time the audioflinger thread for this output
4573 // will process a buffer (which corresponds to one buffer size,
4574 // usually 1/2 or 1/4 of the latency).
4575 if (muteWaitMs < desc->latency() * 2) {
4576 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004577 }
4578 }
4579 }
4580 }
4581 }
4582 }
4583
Eric Laurent99401132014-05-07 19:48:15 -07004584 // temporary mute output if device selection changes to avoid volume bursts due to
4585 // different per device volumes
4586 if (outputDesc->isActive() && (device != prevDevice)) {
4587 if (muteWaitMs < outputDesc->latency() * 2) {
4588 muteWaitMs = outputDesc->latency() * 2;
4589 }
4590 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004591 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004592 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004593 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004594 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004595 muteWaitMs *2, device);
4596 }
4597 }
4598 }
4599
Eric Laurente552edb2014-03-10 17:42:56 -07004600 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4601 if (muteWaitMs > delayMs) {
4602 muteWaitMs -= delayMs;
4603 usleep(muteWaitMs * 1000);
4604 return muteWaitMs;
4605 }
4606 return 0;
4607}
4608
Eric Laurentc75307b2015-03-17 15:29:32 -07004609uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004610 audio_devices_t device,
4611 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004612 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004613 audio_patch_handle_t *patchHandle,
4614 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004615{
Eric Laurentc75307b2015-03-17 15:29:32 -07004616 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004617 AudioParameter param;
4618 uint32_t muteWaitMs;
4619
4620 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004621 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4622 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004623 return muteWaitMs;
4624 }
4625 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4626 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004627 if ((device != AUDIO_DEVICE_NONE) &&
4628 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004629 return 0;
4630 }
4631
4632 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004633 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004634
4635 audio_devices_t prevDevice = outputDesc->mDevice;
4636
Paul McLeanaa981192015-03-21 09:55:15 -07004637 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004638
4639 if (device != AUDIO_DEVICE_NONE) {
4640 outputDesc->mDevice = device;
4641 }
4642 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4643
4644 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004645 // the requested device is AUDIO_DEVICE_NONE
4646 // OR the requested device is the same as current device
4647 // AND force is not specified
4648 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004649 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004650 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4651 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004652 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004653 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004654 return muteWaitMs;
4655 }
4656
4657 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004658
Eric Laurente552edb2014-03-10 17:42:56 -07004659 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004660 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004661 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004662 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004663 DeviceVector deviceList;
4664 if ((address == NULL) || (strlen(address) == 0)) {
4665 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4666 } else {
4667 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4668 }
4669
Eric Laurent1c333e22014-05-20 10:48:17 -07004670 if (!deviceList.isEmpty()) {
4671 struct audio_patch patch;
4672 outputDesc->toAudioPortConfig(&patch.sources[0]);
4673 patch.num_sources = 1;
4674 patch.num_sinks = 0;
4675 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4676 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004677 patch.num_sinks++;
4678 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004679 ssize_t index;
4680 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4681 index = mAudioPatches.indexOfKey(*patchHandle);
4682 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004683 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004684 }
4685 sp< AudioPatch> patchDesc;
4686 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4687 if (index >= 0) {
4688 patchDesc = mAudioPatches.valueAt(index);
4689 afPatchHandle = patchDesc->mAfPatchHandle;
4690 }
4691
Eric Laurent1c333e22014-05-20 10:48:17 -07004692 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004693 &afPatchHandle,
4694 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004695 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4696 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004697 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004698 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004699 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004700 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004701 addAudioPatch(patchDesc->mHandle, patchDesc);
4702 } else {
4703 patchDesc->mPatch = patch;
4704 }
4705 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004706 if (patchHandle) {
4707 *patchHandle = patchDesc->mHandle;
4708 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004709 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004710 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004711 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004712 }
4713 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004714
4715 // inform all input as well
4716 for (size_t i = 0; i < mInputs.size(); i++) {
4717 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004718 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004719 AudioParameter inputCmd = AudioParameter();
4720 ALOGV("%s: inform input %d of device:%d", __func__,
4721 inputDescriptor->mIoHandle, device);
4722 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4723 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4724 inputCmd.toString(),
4725 delayMs);
4726 }
4727 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004728 }
Eric Laurente552edb2014-03-10 17:42:56 -07004729
4730 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004731 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004732
4733 return muteWaitMs;
4734}
4735
Eric Laurentc75307b2015-03-17 15:29:32 -07004736status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004737 int delayMs,
4738 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004739{
Eric Laurent6a94d692014-05-20 11:18:06 -07004740 ssize_t index;
4741 if (patchHandle) {
4742 index = mAudioPatches.indexOfKey(*patchHandle);
4743 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004744 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004745 }
4746 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004747 return INVALID_OPERATION;
4748 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004749 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4750 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004751 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004752 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004753 removeAudioPatch(patchDesc->mHandle);
4754 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004755 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004756 return status;
4757}
4758
4759status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4760 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004761 bool force,
4762 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004763{
4764 status_t status = NO_ERROR;
4765
Eric Laurent1f2f2232014-06-02 12:01:23 -07004766 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004767 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4768 inputDesc->mDevice = device;
4769
4770 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4771 if (!deviceList.isEmpty()) {
4772 struct audio_patch patch;
4773 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004774 // AUDIO_SOURCE_HOTWORD is for internal use only:
4775 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004776 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004777 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004778 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4779 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004780 patch.num_sinks = 1;
4781 //only one input device for now
4782 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004783 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004784 ssize_t index;
4785 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4786 index = mAudioPatches.indexOfKey(*patchHandle);
4787 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004788 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004789 }
4790 sp< AudioPatch> patchDesc;
4791 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4792 if (index >= 0) {
4793 patchDesc = mAudioPatches.valueAt(index);
4794 afPatchHandle = patchDesc->mAfPatchHandle;
4795 }
4796
Eric Laurent1c333e22014-05-20 10:48:17 -07004797 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004798 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004799 0);
4800 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004801 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004802 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004803 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004804 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004805 addAudioPatch(patchDesc->mHandle, patchDesc);
4806 } else {
4807 patchDesc->mPatch = patch;
4808 }
4809 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004810 if (patchHandle) {
4811 *patchHandle = patchDesc->mHandle;
4812 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004813 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004814 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004815 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004816 }
4817 }
4818 }
4819 return status;
4820}
4821
Eric Laurent6a94d692014-05-20 11:18:06 -07004822status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4823 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004824{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004825 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004826 ssize_t index;
4827 if (patchHandle) {
4828 index = mAudioPatches.indexOfKey(*patchHandle);
4829 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004830 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004831 }
4832 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004833 return INVALID_OPERATION;
4834 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004835 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4836 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004837 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004838 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004839 removeAudioPatch(patchDesc->mHandle);
4840 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004841 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004842 return status;
4843}
4844
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004845sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004846 String8 address,
4847 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004848 audio_format_t& format,
4849 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004850 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004851{
4852 // Choose an input profile based on the requested capture parameters: select the first available
4853 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004854 //
4855 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4856 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004857
4858 for (size_t i = 0; i < mHwModules.size(); i++)
4859 {
4860 if (mHwModules[i]->mHandle == 0) {
4861 continue;
4862 }
4863 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4864 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004865 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004866 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004867 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004868 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004869 format,
4870 &format /*updatedFormat*/,
4871 channelMask,
4872 &channelMask /*updatedChannelMask*/,
4873 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004874
Eric Laurente552edb2014-03-10 17:42:56 -07004875 return profile;
4876 }
4877 }
4878 }
4879 return NULL;
4880}
4881
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004882
4883audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004884 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004885{
François Gaffie036e1e92015-03-19 10:16:24 +01004886 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4887 audio_devices_t selectedDeviceFromMix =
4888 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004889
François Gaffie036e1e92015-03-19 10:16:24 +01004890 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4891 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004892 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004893 return getDeviceForInputSource(inputSource);
4894}
4895
4896audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4897{
Paul McLean466dc8e2015-04-17 13:15:36 -06004898 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4899 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004900 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004901 return route->mDeviceDescriptor->type();
4902 }
4903 }
4904
4905 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004906}
4907
Eric Laurente0720872014-03-11 09:30:41 -07004908float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004909 int index,
4910 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004911{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004912 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004913 // if a headset is connected, apply the following rules to ring tones and notifications
4914 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004915 // - always attenuate notifications volume by 6dB
4916 // - attenuate ring tones volume by 6dB unless music is not playing and
4917 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004918 // - if music is playing, always limit the volume to current music volume,
4919 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004920 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004921 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4922 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4923 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4924 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4925 ((stream_strategy == STRATEGY_SONIFICATION)
4926 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004927 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004928 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004929 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004930 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004931 // when the phone is ringing we must consider that music could have been paused just before
4932 // by the music application and behave as if music was active if the last music track was
4933 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004934 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004935 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004936 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004937 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004938 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004939 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4940 musicDevice),
4941 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004942 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4943 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004944 if (volumeDB > minVolDB) {
4945 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004946 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004947 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004948 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4949 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4950 // on A2DP, also ensure notification volume is not too low compared to media when
4951 // intended to be played
4952 if ((volumeDB > -96.0f) &&
4953 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4954 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4955 stream, device,
4956 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4957 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4958 }
4959 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004960 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4961 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004962 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004963 }
4964 }
4965
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004966 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07004967}
4968
Eric Laurente0720872014-03-11 09:30:41 -07004969status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004970 int index,
4971 const sp<AudioOutputDescriptor>& outputDesc,
4972 audio_devices_t device,
4973 int delayMs,
4974 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004975{
Eric Laurente552edb2014-03-10 17:42:56 -07004976 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004977 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004978 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004979 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004980 return NO_ERROR;
4981 }
François Gaffie2110e042015-03-24 08:41:51 +01004982 audio_policy_forced_cfg_t forceUseForComm =
4983 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004984 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004985 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4986 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004987 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004988 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004989 return INVALID_OPERATION;
4990 }
4991
Eric Laurentc75307b2015-03-17 15:29:32 -07004992 if (device == AUDIO_DEVICE_NONE) {
4993 device = outputDesc->device();
4994 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004995
Eric Laurentffbc80f2015-03-18 18:30:19 -07004996 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004997 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004998 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004999 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005000
Eric Laurentffbc80f2015-03-18 18:30:19 -07005001 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005002
Eric Laurent3b73df72014-03-11 09:06:29 -07005003 if (stream == AUDIO_STREAM_VOICE_CALL ||
5004 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005005 float voiceVolume;
5006 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005007 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005008 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005009 } else {
5010 voiceVolume = 1.0;
5011 }
5012
Eric Laurent18fba842016-03-31 14:41:26 -07005013 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005014 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5015 mLastVoiceVolume = voiceVolume;
5016 }
5017 }
5018
5019 return NO_ERROR;
5020}
5021
Eric Laurentc75307b2015-03-17 15:29:32 -07005022void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5023 audio_devices_t device,
5024 int delayMs,
5025 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005026{
Eric Laurentc75307b2015-03-17 15:29:32 -07005027 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005028
Eric Laurent794fde22016-03-11 09:50:45 -08005029 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005030 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005031 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005032 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005033 device,
5034 delayMs,
5035 force);
5036 }
5037}
5038
Eric Laurente0720872014-03-11 09:30:41 -07005039void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005040 bool on,
5041 const sp<AudioOutputDescriptor>& outputDesc,
5042 int delayMs,
5043 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005044{
Eric Laurent72249d52015-06-08 11:12:15 -07005045 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5046 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005047 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005048 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005049 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005050 }
5051 }
5052}
5053
Eric Laurente0720872014-03-11 09:30:41 -07005054void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005055 bool on,
5056 const sp<AudioOutputDescriptor>& outputDesc,
5057 int delayMs,
5058 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005059{
Eric Laurente552edb2014-03-10 17:42:56 -07005060 if (device == AUDIO_DEVICE_NONE) {
5061 device = outputDesc->device();
5062 }
5063
Eric Laurentc75307b2015-03-17 15:29:32 -07005064 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5065 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005066
5067 if (on) {
5068 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005069 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005070 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005071 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005072 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005073 }
5074 }
5075 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5076 outputDesc->mMuteCount[stream]++;
5077 } else {
5078 if (outputDesc->mMuteCount[stream] == 0) {
5079 ALOGV("setStreamMute() unmuting non muted stream!");
5080 return;
5081 }
5082 if (--outputDesc->mMuteCount[stream] == 0) {
5083 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005084 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005085 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005086 device,
5087 delayMs);
5088 }
5089 }
5090}
5091
Eric Laurente0720872014-03-11 09:30:41 -07005092void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005093 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005094{
Eric Laurent87ffa392015-05-22 10:32:38 -07005095 if(!hasPrimaryOutput()) {
5096 return;
5097 }
5098
Eric Laurente552edb2014-03-10 17:42:56 -07005099 // if the stream pertains to sonification strategy and we are in call we must
5100 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5101 // in the device used for phone strategy and play the tone if the selected device does not
5102 // interfere with the device used for phone strategy
5103 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5104 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005105 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005106 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5107 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005108 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005109 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5110 stream, starting, outputDesc->mDevice, stateChange);
5111 if (outputDesc->mRefCount[stream]) {
5112 int muteCount = 1;
5113 if (stateChange) {
5114 muteCount = outputDesc->mRefCount[stream];
5115 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005116 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005117 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5118 for (int i = 0; i < muteCount; i++) {
5119 setStreamMute(stream, starting, mPrimaryOutput);
5120 }
5121 } else {
5122 ALOGV("handleIncallSonification() high visibility");
5123 if (outputDesc->device() &
5124 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5125 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5126 for (int i = 0; i < muteCount; i++) {
5127 setStreamMute(stream, starting, mPrimaryOutput);
5128 }
5129 }
5130 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005131 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5132 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005133 } else {
5134 mpClientInterface->stopTone();
5135 }
5136 }
5137 }
5138 }
5139}
5140
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005141audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5142{
5143 // flags to stream type mapping
5144 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5145 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5146 }
5147 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5148 return AUDIO_STREAM_BLUETOOTH_SCO;
5149 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005150 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5151 return AUDIO_STREAM_TTS;
5152 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005153
5154 // usage to stream type mapping
5155 switch (attr->usage) {
5156 case AUDIO_USAGE_MEDIA:
5157 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005158 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5159 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005160 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5161 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005162 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5163 return AUDIO_STREAM_SYSTEM;
5164 case AUDIO_USAGE_VOICE_COMMUNICATION:
5165 return AUDIO_STREAM_VOICE_CALL;
5166
5167 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5168 return AUDIO_STREAM_DTMF;
5169
5170 case AUDIO_USAGE_ALARM:
5171 return AUDIO_STREAM_ALARM;
5172 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5173 return AUDIO_STREAM_RING;
5174
5175 case AUDIO_USAGE_NOTIFICATION:
5176 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5177 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5178 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5179 case AUDIO_USAGE_NOTIFICATION_EVENT:
5180 return AUDIO_STREAM_NOTIFICATION;
5181
5182 case AUDIO_USAGE_UNKNOWN:
5183 default:
5184 return AUDIO_STREAM_MUSIC;
5185 }
5186}
Eric Laurente83b55d2014-11-14 10:06:21 -08005187
François Gaffie53615e22015-03-19 09:24:12 +01005188bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5189{
Eric Laurente83b55d2014-11-14 10:06:21 -08005190 // has flags that map to a strategy?
5191 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5192 return true;
5193 }
5194
5195 // has known usage?
5196 switch (paa->usage) {
5197 case AUDIO_USAGE_UNKNOWN:
5198 case AUDIO_USAGE_MEDIA:
5199 case AUDIO_USAGE_VOICE_COMMUNICATION:
5200 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5201 case AUDIO_USAGE_ALARM:
5202 case AUDIO_USAGE_NOTIFICATION:
5203 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5204 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5205 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5206 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5207 case AUDIO_USAGE_NOTIFICATION_EVENT:
5208 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5209 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5210 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5211 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005212 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005213 break;
5214 default:
5215 return false;
5216 }
5217 return true;
5218}
5219
François Gaffiead3183e2015-03-18 16:55:35 +01005220bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5221 routing_strategy strategy, uint32_t inPastMs,
5222 nsecs_t sysTime) const
5223{
5224 if ((sysTime == 0) && (inPastMs != 0)) {
5225 sysTime = systemTime();
5226 }
Eric Laurent794fde22016-03-11 09:50:45 -08005227 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005228 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5229 (NUM_STRATEGIES == strategy)) &&
5230 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5231 return true;
5232 }
5233 }
5234 return false;
5235}
5236
François Gaffie2110e042015-03-24 08:41:51 +01005237audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5238{
5239 return mEngine->getForceUse(usage);
5240}
5241
5242bool AudioPolicyManager::isInCall()
5243{
5244 return isStateInCall(mEngine->getPhoneState());
5245}
5246
5247bool AudioPolicyManager::isStateInCall(int state)
5248{
5249 return is_state_in_call(state);
5250}
5251
Eric Laurentd60560a2015-04-10 11:31:20 -07005252void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5253{
5254 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5255 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5256 if (sourceDesc->mDevice->equals(deviceDesc)) {
5257 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5258 stopAudioSource(sourceDesc->getHandle());
5259 }
5260 }
5261
5262 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5263 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5264 bool release = false;
5265 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5266 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5267 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5268 source->ext.device.type == deviceDesc->type()) {
5269 release = true;
5270 }
5271 }
5272 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5273 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5274 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5275 sink->ext.device.type == deviceDesc->type()) {
5276 release = true;
5277 }
5278 }
5279 if (release) {
5280 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5281 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5282 }
5283 }
5284}
5285
Phil Burk09bc4612016-02-24 15:58:15 -08005286// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005287void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5288 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005289 // TODO Set this based on Config properties.
5290 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005291
5292 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5293 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005294 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005295
5296 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005297 bool supportsAC3 = false;
5298 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005299 bool supportsIEC61937 = false;
5300 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5301 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005302 switch (format) {
5303 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005304 supportsAC3 = true;
5305 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005306 case AUDIO_FORMAT_E_AC3:
5307 case AUDIO_FORMAT_DTS:
5308 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005309 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005310 break;
5311 case AUDIO_FORMAT_IEC61937:
5312 supportsIEC61937 = true;
5313 break;
5314 default:
5315 break;
5316 }
5317 }
Phil Burk09bc4612016-02-24 15:58:15 -08005318
5319 // Modify formats based on surround preferences.
5320 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005321 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5322 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5323 // Remove surround sound related formats.
5324 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5325 audio_format_t format = formats[formatIndex];
5326 switch(format) {
5327 case AUDIO_FORMAT_AC3:
5328 case AUDIO_FORMAT_E_AC3:
5329 case AUDIO_FORMAT_DTS:
5330 case AUDIO_FORMAT_DTS_HD:
5331 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005332 formats.removeAt(formatIndex);
5333 break;
5334 default:
5335 formatIndex++; // keep it
5336 break;
5337 }
Phil Burk09bc4612016-02-24 15:58:15 -08005338 }
Phil Burk07ac1142016-03-25 13:39:29 -07005339 supportsAC3 = false;
5340 supportsOtherSurround = false;
5341 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005342 }
Phil Burk07ac1142016-03-25 13:39:29 -07005343 } else { // AUTO or ALWAYS
5344 // Most TVs support AC3 even if they do not report it in the EDID.
5345 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5346 && !supportsAC3) {
5347 formats.add(AUDIO_FORMAT_AC3);
5348 supportsAC3 = true;
5349 }
5350
5351 // If ALWAYS, add support for raw surround formats if all are missing.
5352 // This assumes that if any of these formats are reported by the HAL
5353 // then the report is valid and should not be modified.
5354 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5355 && !supportsOtherSurround) {
5356 formats.add(AUDIO_FORMAT_E_AC3);
5357 formats.add(AUDIO_FORMAT_DTS);
5358 formats.add(AUDIO_FORMAT_DTS_HD);
5359 supportsOtherSurround = true;
5360 }
5361
5362 // Add support for IEC61937 if any raw surround supported.
5363 // The HAL could do this but add it here, just in case.
5364 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5365 formats.add(AUDIO_FORMAT_IEC61937);
5366 supportsIEC61937 = true;
5367 }
Phil Burk09bc4612016-02-24 15:58:15 -08005368 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005369}
5370
5371// Modify the list of channel masks supported.
5372void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5373 ChannelsVector &channelMasks = *channelMasksPtr;
5374 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5375 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5376
5377 // If NEVER, then remove support for channelMasks > stereo.
5378 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5379 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5380 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5381 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5382 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5383 channelMasks.removeAt(maskIndex);
5384 } else {
5385 maskIndex++;
5386 }
5387 }
5388 // If ALWAYS, then make sure we at least support 5.1
5389 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5390 bool supports5dot1 = false;
5391 // Are there any channel masks that can be considered "surround"?
5392 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5393 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5394 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5395 supports5dot1 = true;
5396 break;
5397 }
5398 }
5399 // If not then add 5.1 support.
5400 if (!supports5dot1) {
5401 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5402 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5403 }
Phil Burk09bc4612016-02-24 15:58:15 -08005404 }
5405}
5406
Phil Burk00eeb322016-03-31 12:41:00 -07005407void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5408 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005409 AudioProfileVector &profiles)
5410{
5411 String8 reply;
5412 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005413
François Gaffie112b0af2015-11-19 16:13:25 +01005414 // Format MUST be checked first to update the list of AudioProfile
5415 if (profiles.hasDynamicFormat()) {
5416 reply = mpClientInterface->getParameters(ioHandle,
5417 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005418 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005419 AudioParameter repliedParameters(reply);
5420 if (repliedParameters.get(
5421 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005422 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5423 return;
5424 }
Phil Burk09bc4612016-02-24 15:58:15 -08005425 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005426 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005427 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005428 }
Phil Burk09bc4612016-02-24 15:58:15 -08005429 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005430 }
5431 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5432
Eric Laurent20eb3a42016-01-26 18:39:17 -08005433 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005434 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005435 ChannelsVector channelMasks;
5436 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005437 AudioParameter requestedParameters;
5438 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5439
5440 if (profiles.hasDynamicRateFor(format)) {
5441 reply = mpClientInterface->getParameters(ioHandle,
5442 requestedParameters.toString() + ";" +
5443 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5444 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005445 AudioParameter repliedParameters(reply);
5446 if (repliedParameters.get(
5447 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5448 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005449 }
5450 }
5451 if (profiles.hasDynamicChannelsFor(format)) {
5452 reply = mpClientInterface->getParameters(ioHandle,
5453 requestedParameters.toString() + ";" +
5454 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5455 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005456 AudioParameter repliedParameters(reply);
5457 if (repliedParameters.get(
5458 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5459 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005460 if (device == AUDIO_DEVICE_OUT_HDMI) {
5461 filterSurroundChannelMasks(&channelMasks);
5462 }
François Gaffie112b0af2015-11-19 16:13:25 +01005463 }
5464 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005465 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005466 }
5467}
Eric Laurentd60560a2015-04-10 11:31:20 -07005468
Eric Laurente552edb2014-03-10 17:42:56 -07005469}; // namespace android