blob: 3b075fa9190fb7b16d295ba9b89f656a14e450a8 [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
64status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080065 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080066 const char *device_address,
67 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080068{
Paul McLeane743a472015-01-28 11:07:31 -080069 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
70- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070071
72 // connect/disconnect only 1 device at a time
73 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
74
François Gaffie53615e22015-03-19 09:24:12 +010075 sp<DeviceDescriptor> devDesc =
76 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080077
Eric Laurente552edb2014-03-10 17:42:56 -070078 // handle output devices
79 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070080 SortedVector <audio_io_handle_t> outputs;
81
Eric Laurent3a4311c2014-03-17 12:00:47 -070082 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
83
Eric Laurente552edb2014-03-10 17:42:56 -070084 // save a copy of the opened output descriptors before any output is opened or closed
85 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
86 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070087 switch (state)
88 {
89 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080090 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070091 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070092 ALOGW("setDeviceConnectionState() device already connected: %x", device);
93 return INVALID_OPERATION;
94 }
95 ALOGV("setDeviceConnectionState() connecting device %x", device);
96
Eric Laurente552edb2014-03-10 17:42:56 -070097 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070098 index = mAvailableOutputDevices.add(devDesc);
99 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100100 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700101 if (module == 0) {
102 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
103 device);
104 mAvailableOutputDevices.remove(devDesc);
105 return INVALID_OPERATION;
106 }
Paul McLeane743a472015-01-28 11:07:31 -0800107 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700108 } else {
109 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700110 }
111
Eric Laurenta1d525f2015-01-29 13:36:45 -0800112 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 mAvailableOutputDevices.remove(devDesc);
114 return INVALID_OPERATION;
115 }
François Gaffie2110e042015-03-24 08:41:51 +0100116 // Propagate device availability to Engine
117 mEngine->setDeviceConnectionState(devDesc, state);
118
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700119 // outputs should never be empty here
120 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
121 "checkOutputsForDevice() returned no outputs but status OK");
122 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
123 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800124
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800125 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800126 AudioParameter param = AudioParameter(devDesc->mAddress);
127 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
128 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
129
130 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700131 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700132 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700133 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700134 ALOGW("setDeviceConnectionState() device not connected: %x", device);
135 return INVALID_OPERATION;
136 }
137
Paul McLean5c477aa2014-08-20 16:47:57 -0700138 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
139
Paul McLeane743a472015-01-28 11:07:31 -0800140 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800141 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700142 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
143 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
144
Eric Laurente552edb2014-03-10 17:42:56 -0700145 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700146 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700147
Eric Laurenta1d525f2015-01-29 13:36:45 -0800148 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100149
150 // Propagate device availability to Engine
151 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700152 } break;
153
154 default:
155 ALOGE("setDeviceConnectionState() invalid state: %x", state);
156 return BAD_VALUE;
157 }
158
Eric Laurent3a4311c2014-03-17 12:00:47 -0700159 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
160 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700161 checkA2dpSuspend();
162 checkOutputForAllStrategies();
163 // outputs must be closed after checkOutputForAllStrategies() is executed
164 if (!outputs.isEmpty()) {
165 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700166 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700167 // close unused outputs after device disconnection or direct outputs that have been
168 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700169 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700170 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
171 (desc->mDirectOpenCount == 0))) {
172 closeOutput(outputs[i]);
173 }
174 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700175 // check again after closing A2DP output to reset mA2dpSuspended if needed
176 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700177 }
178
179 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700180 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700181 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
182 updateCallRouting(newDevice);
183 }
Eric Laurente552edb2014-03-10 17:42:56 -0700184 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
186 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
187 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700188 // do not force device change on duplicated output because if device is 0, it will
189 // also force a device 0 for the two outputs it is duplicated to which may override
190 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700191 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100192 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700193 // always force when disconnecting (a non-duplicated device)
194 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700195 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700196 }
Eric Laurente552edb2014-03-10 17:42:56 -0700197 }
198
Eric Laurentd60560a2015-04-10 11:31:20 -0700199 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
200 cleanUpForDevice(devDesc);
201 }
202
Eric Laurent72aa32f2014-05-30 18:51:48 -0700203 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700204 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700205 } // end if is output device
206
Eric Laurente552edb2014-03-10 17:42:56 -0700207 // handle input devices
208 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700209 SortedVector <audio_io_handle_t> inputs;
210
Eric Laurent3a4311c2014-03-17 12:00:47 -0700211 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700212 switch (state)
213 {
214 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700215 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700216 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700217 ALOGW("setDeviceConnectionState() device already connected: %d", device);
218 return INVALID_OPERATION;
219 }
François Gaffie53615e22015-03-19 09:24:12 +0100220 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700221 if (module == NULL) {
222 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
223 device);
224 return INVALID_OPERATION;
225 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700226 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700227 return INVALID_OPERATION;
228 }
229
Eric Laurent3a4311c2014-03-17 12:00:47 -0700230 index = mAvailableInputDevices.add(devDesc);
231 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800232 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700233 } else {
234 return NO_MEMORY;
235 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800236
237 // Set connect to HALs
238 AudioParameter param = AudioParameter(devDesc->mAddress);
239 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
240 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
241
François Gaffie2110e042015-03-24 08:41:51 +0100242 // Propagate device availability to Engine
243 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700244 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700245
246 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700247 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700248 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700249 ALOGW("setDeviceConnectionState() device not connected: %d", device);
250 return INVALID_OPERATION;
251 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
253 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
254
255 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800256 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700257 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
258 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
259
Paul McLean9080a4c2015-06-18 08:24:02 -0700260 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700262
François Gaffie2110e042015-03-24 08:41:51 +0100263 // Propagate device availability to Engine
264 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700265 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700266
267 default:
268 ALOGE("setDeviceConnectionState() invalid state: %x", state);
269 return BAD_VALUE;
270 }
271
Eric Laurentd4692962014-05-05 18:13:44 -0700272 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700273
Eric Laurent87ffa392015-05-22 10:32:38 -0700274 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700275 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
276 updateCallRouting(newDevice);
277 }
278
Eric Laurentd60560a2015-04-10 11:31:20 -0700279 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
280 cleanUpForDevice(devDesc);
281 }
282
Eric Laurentb52c1522014-05-20 11:27:36 -0700283 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700284 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700285 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700286
287 ALOGW("setDeviceConnectionState() invalid device: %x", device);
288 return BAD_VALUE;
289}
290
Eric Laurente0720872014-03-11 09:30:41 -0700291audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100292 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700293{
Eric Laurent634b7142016-04-20 13:48:02 -0700294 sp<DeviceDescriptor> devDesc =
295 mHwModules.getDeviceDescriptor(device, device_address, "",
296 (strlen(device_address) != 0)/*matchAddress*/);
297
298 if (devDesc == 0) {
299 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
300 device, device_address);
301 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
302 }
François Gaffie53615e22015-03-19 09:24:12 +0100303
Eric Laurent3a4311c2014-03-17 12:00:47 -0700304 DeviceVector *deviceVector;
305
Eric Laurente552edb2014-03-10 17:42:56 -0700306 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700307 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700308 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700309 deviceVector = &mAvailableInputDevices;
310 } else {
311 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
312 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700313 }
Eric Laurent634b7142016-04-20 13:48:02 -0700314
315 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
316 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800317}
318
Eric Laurentc2730ba2014-07-20 15:47:07 -0700319void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
320{
321 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700322 status_t status;
323 audio_patch_handle_t afPatchHandle;
324 DeviceVector deviceList;
325
Eric Laurent87ffa392015-05-22 10:32:38 -0700326 if(!hasPrimaryOutput()) {
327 return;
328 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800329 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700330 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
331
332 // release existing RX patch if any
333 if (mCallRxPatch != 0) {
334 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
335 mCallRxPatch.clear();
336 }
337 // release TX patch if any
338 if (mCallTxPatch != 0) {
339 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
340 mCallTxPatch.clear();
341 }
342
343 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
344 // via setOutputDevice() on primary output.
345 // Otherwise, create two audio patches for TX and RX path.
346 if (availablePrimaryOutputDevices() & rxDevice) {
347 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
348 // If the TX device is also on the primary HW module, setOutputDevice() will take care
349 // of it due to legacy implementation. If not, create a patch.
350 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
351 == AUDIO_DEVICE_NONE) {
352 createTxPatch = true;
353 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700354 } else { // create RX path audio patch
355 struct audio_patch patch;
356
357 patch.num_sources = 1;
358 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
360 ALOG_ASSERT(!deviceList.isEmpty(),
361 "updateCallRouting() selected device not in output device list");
362 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
363 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
364 ALOG_ASSERT(!deviceList.isEmpty(),
365 "updateCallRouting() no telephony RX device");
366 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
367
368 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
369 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
370
371 // request to reuse existing output stream if one is already opened to reach the RX device
372 SortedVector<audio_io_handle_t> outputs =
373 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700374 audio_io_handle_t output = selectOutput(outputs,
375 AUDIO_OUTPUT_FLAG_NONE,
376 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700377 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700378 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700379 ALOG_ASSERT(!outputDesc->isDuplicated(),
380 "updateCallRouting() RX device output is duplicated");
381 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700382 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700383 patch.num_sources = 2;
384 }
385
386 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
387 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
388 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
389 status);
390 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100391 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700392 mCallRxPatch->mAfPatchHandle = afPatchHandle;
393 mCallRxPatch->mUid = mUidCached;
394 }
395 createTxPatch = true;
396 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700397 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700398 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700399
Eric Laurentc2730ba2014-07-20 15:47:07 -0700400 patch.num_sources = 1;
401 patch.num_sinks = 1;
402 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
403 ALOG_ASSERT(!deviceList.isEmpty(),
404 "updateCallRouting() selected device not in input device list");
405 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
406 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
407 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
408 ALOG_ASSERT(!deviceList.isEmpty(),
409 "updateCallRouting() no telephony TX device");
410 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
411 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
412
413 SortedVector<audio_io_handle_t> outputs =
414 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700415 audio_io_handle_t output = selectOutput(outputs,
416 AUDIO_OUTPUT_FLAG_NONE,
417 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700418 // request to reuse existing output stream if one is already opened to reach the TX
419 // path output device
420 if (output != AUDIO_IO_HANDLE_NONE) {
421 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
422 ALOG_ASSERT(!outputDesc->isDuplicated(),
423 "updateCallRouting() RX device output is duplicated");
424 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700425 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700426 patch.num_sources = 2;
427 }
428
Eric Laurentc0a889f2015-10-14 14:36:34 -0700429 // terminate active capture if on the same HW module as the call TX source device
430 // FIXME: would be better to refine to only inputs whose profile connects to the
431 // call TX device but this information is not in the audio patch and logic here must be
432 // symmetric to the one in startInput()
Eric Laurent232f26f2016-02-17 18:06:27 -0800433 audio_io_handle_t activeInput = mInputs.getActiveInput();
434 if (activeInput != 0) {
435 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
436 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
437 //FIXME: consider all active sessions
438 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
439 audio_session_t activeSession = activeSessions.keyAt(0);
440 stopInput(activeInput, activeSession);
441 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700442 }
443 }
444
Eric Laurentc2730ba2014-07-20 15:47:07 -0700445 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
446 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
447 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
448 status);
449 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100450 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700451 mCallTxPatch->mAfPatchHandle = afPatchHandle;
452 mCallTxPatch->mUid = mUidCached;
453 }
454 }
455}
456
Eric Laurente0720872014-03-11 09:30:41 -0700457void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700458{
459 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100460 // store previous phone state for management of sonification strategy below
461 int oldState = mEngine->getPhoneState();
462
463 if (mEngine->setPhoneState(state) != NO_ERROR) {
464 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700465 return;
466 }
François Gaffie2110e042015-03-24 08:41:51 +0100467 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700468 // if leaving call state, handle special case of active streams
469 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700470 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700471 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800472 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700473 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700474 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800475
Eric Laurent63dea1d2015-07-02 17:10:28 -0700476 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800477 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700478 }
479
François Gaffie2110e042015-03-24 08:41:51 +0100480 /**
481 * Switching to or from incall state or switching between telephony and VoIP lead to force
482 * routing command.
483 */
484 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
485 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700486
487 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700488 checkA2dpSuspend();
489 checkOutputForAllStrategies();
490 updateDevicesAndOutputs();
491
Eric Laurente552edb2014-03-10 17:42:56 -0700492 int delayMs = 0;
493 if (isStateInCall(state)) {
494 nsecs_t sysTime = systemTime();
495 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700496 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700497 // mute media and sonification strategies and delay device switch by the largest
498 // latency of any output where either strategy is active.
499 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100500 if ((isStrategyActive(desc, STRATEGY_MEDIA,
501 SONIFICATION_HEADSET_MUSIC_DELAY,
502 sysTime) ||
503 isStrategyActive(desc, STRATEGY_SONIFICATION,
504 SONIFICATION_HEADSET_MUSIC_DELAY,
505 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700506 (delayMs < (int)desc->latency()*2)) {
507 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700508 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700509 setStrategyMute(STRATEGY_MEDIA, true, desc);
510 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700511 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700512 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
513 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700514 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
515 }
516 }
517
Eric Laurent87ffa392015-05-22 10:32:38 -0700518 if (hasPrimaryOutput()) {
519 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
520 // the device returned is not necessarily reachable via this output
521 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
522 // force routing command to audio hardware when ending call
523 // even if no device change is needed
524 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
525 rxDevice = mPrimaryOutput->device();
526 }
Eric Laurente552edb2014-03-10 17:42:56 -0700527
Eric Laurent87ffa392015-05-22 10:32:38 -0700528 if (state == AUDIO_MODE_IN_CALL) {
529 updateCallRouting(rxDevice, delayMs);
530 } else if (oldState == AUDIO_MODE_IN_CALL) {
531 if (mCallRxPatch != 0) {
532 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
533 mCallRxPatch.clear();
534 }
535 if (mCallTxPatch != 0) {
536 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
537 mCallTxPatch.clear();
538 }
539 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
540 } else {
541 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700542 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700543 }
Eric Laurente552edb2014-03-10 17:42:56 -0700544 // if entering in call state, handle special case of active streams
545 // pertaining to sonification strategy see handleIncallSonification()
546 if (isStateInCall(state)) {
547 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800548 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700549 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700550 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700551
552 // force reevaluating accessibility routing when call starts
553 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700554 }
555
556 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700557 if (state == AUDIO_MODE_RINGTONE &&
558 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700559 mLimitRingtoneVolume = true;
560 } else {
561 mLimitRingtoneVolume = false;
562 }
563}
564
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700565audio_mode_t AudioPolicyManager::getPhoneState() {
566 return mEngine->getPhoneState();
567}
568
Eric Laurente0720872014-03-11 09:30:41 -0700569void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700570 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700571{
François Gaffie2110e042015-03-24 08:41:51 +0100572 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700573
François Gaffie2110e042015-03-24 08:41:51 +0100574 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
575 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
576 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700577 }
François Gaffie2110e042015-03-24 08:41:51 +0100578 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
579 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
580 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700581
582 // check for device and output changes triggered by new force usage
583 checkA2dpSuspend();
584 checkOutputForAllStrategies();
585 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800586
Eric Laurent87ffa392015-05-22 10:32:38 -0700587 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700588 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
589 updateCallRouting(newDevice);
590 }
Eric Laurente552edb2014-03-10 17:42:56 -0700591 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700592 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
593 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
594 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
595 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700596 }
Eric Laurente552edb2014-03-10 17:42:56 -0700597 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700598 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700599 }
600 }
601
Eric Laurent232f26f2016-02-17 18:06:27 -0800602 audio_io_handle_t activeInput = mInputs.getActiveInput();
603 if (activeInput != 0) {
604 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
605 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700606 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800607 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
608 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700609 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800610 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700611 }
Eric Laurente552edb2014-03-10 17:42:56 -0700612 }
Eric Laurente552edb2014-03-10 17:42:56 -0700613}
614
Eric Laurente0720872014-03-11 09:30:41 -0700615void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700616{
617 ALOGV("setSystemProperty() property %s, value %s", property, value);
618}
619
620// Find a direct output profile compatible with the parameters passed, even if the input flags do
621// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800622sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700623 audio_devices_t device,
624 uint32_t samplingRate,
625 audio_format_t format,
626 audio_channel_mask_t channelMask,
627 audio_output_flags_t flags)
628{
Eric Laurent861a6282015-05-18 15:40:16 -0700629 // only retain flags that will drive the direct output profile selection
630 // if explicitly requested
631 static const uint32_t kRelevantFlags =
632 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
633 flags =
634 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
635
636 sp<IOProfile> profile;
637
Eric Laurente552edb2014-03-10 17:42:56 -0700638 for (size_t i = 0; i < mHwModules.size(); i++) {
639 if (mHwModules[i]->mHandle == 0) {
640 continue;
641 }
642 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700643 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
644 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700645 samplingRate, NULL /*updatedSamplingRate*/,
646 format, NULL /*updatedFormat*/,
647 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700648 flags)) {
649 continue;
650 }
651 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100652 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700653 continue;
654 }
655 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100656 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700657 continue;
658 }
659 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100660 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700661 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700662 }
Eric Laurente552edb2014-03-10 17:42:56 -0700663 }
664 }
Eric Laurent861a6282015-05-18 15:40:16 -0700665 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700666}
667
Eric Laurente0720872014-03-11 09:30:41 -0700668audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100669 uint32_t samplingRate,
670 audio_format_t format,
671 audio_channel_mask_t channelMask,
672 audio_output_flags_t flags,
673 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700674{
Eric Laurent3b73df72014-03-11 09:06:29 -0700675 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700676 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
677 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
678 device, stream, samplingRate, format, channelMask, flags);
679
Eric Laurente83b55d2014-11-14 10:06:21 -0800680 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
681 stream, samplingRate,format, channelMask,
682 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700683}
684
Eric Laurente83b55d2014-11-14 10:06:21 -0800685status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
686 audio_io_handle_t *output,
687 audio_session_t session,
688 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700689 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800690 uint32_t samplingRate,
691 audio_format_t format,
692 audio_channel_mask_t channelMask,
693 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700694 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800695 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700696{
Eric Laurente83b55d2014-11-14 10:06:21 -0800697 audio_attributes_t attributes;
698 if (attr != NULL) {
699 if (!isValidAttributes(attr)) {
700 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
701 attr->usage, attr->content_type, attr->flags,
702 attr->tags);
703 return BAD_VALUE;
704 }
705 attributes = *attr;
706 } else {
707 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
708 ALOGE("getOutputForAttr(): invalid stream type");
709 return BAD_VALUE;
710 }
711 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700712 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700713 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800714 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100715 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800716 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100717 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800718 }
François Gaffie036e1e92015-03-19 10:16:24 +0100719 *stream = streamTypefromAttributesInt(&attributes);
720 *output = desc->mIoHandle;
721 ALOGV("getOutputForAttr() returns output %d", *output);
722 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800723 }
724 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
725 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
726 return BAD_VALUE;
727 }
728
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700729 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
730 " session %d selectedDeviceId %d",
731 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
732 session, selectedDeviceId);
733
734 *stream = streamTypefromAttributesInt(&attributes);
735
736 // Explicit routing?
737 sp<DeviceDescriptor> deviceDesc;
738 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
739 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
740 deviceDesc = mAvailableOutputDevices[i];
741 break;
742 }
743 }
744 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700745
Eric Laurente83b55d2014-11-14 10:06:21 -0800746 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700747 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700748
Eric Laurente83b55d2014-11-14 10:06:21 -0800749 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700750 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
751 }
752
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700753 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700754 device, samplingRate, format, channelMask, flags);
755
Eric Laurente83b55d2014-11-14 10:06:21 -0800756 *output = getOutputForDevice(device, session, *stream,
757 samplingRate, format, channelMask,
758 flags, offloadInfo);
759 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700760 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800761 return INVALID_OPERATION;
762 }
Paul McLeanaa981192015-03-21 09:55:15 -0700763
Eric Laurente83b55d2014-11-14 10:06:21 -0800764 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700765}
766
767audio_io_handle_t AudioPolicyManager::getOutputForDevice(
768 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800769 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700770 audio_stream_type_t stream,
771 uint32_t samplingRate,
772 audio_format_t format,
773 audio_channel_mask_t channelMask,
774 audio_output_flags_t flags,
775 const audio_offload_info_t *offloadInfo)
776{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700777 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700778 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700779 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700780
Eric Laurente552edb2014-03-10 17:42:56 -0700781#ifdef AUDIO_POLICY_TEST
782 if (mCurOutput != 0) {
783 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
784 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
785
786 if (mTestOutputs[mCurOutput] == 0) {
787 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700788 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
789 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700790 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700791 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700792 outputDesc->mFlags =
793 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700794 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700795 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
796 config.sample_rate = mTestSamplingRate;
797 config.channel_mask = mTestChannels;
798 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700799 if (offloadInfo != NULL) {
800 config.offload_info = *offloadInfo;
801 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700802 status = mpClientInterface->openOutput(0,
803 &mTestOutputs[mCurOutput],
804 &config,
805 &outputDesc->mDevice,
806 String8(""),
807 &outputDesc->mLatency,
808 outputDesc->mFlags);
809 if (status == NO_ERROR) {
810 outputDesc->mSamplingRate = config.sample_rate;
811 outputDesc->mFormat = config.format;
812 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700813 AudioParameter outputCmd = AudioParameter();
814 outputCmd.addInt(String8("set_id"),mCurOutput);
815 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
816 addOutput(mTestOutputs[mCurOutput], outputDesc);
817 }
818 }
819 return mTestOutputs[mCurOutput];
820 }
821#endif //AUDIO_POLICY_TEST
822
823 // open a direct output if required by specified parameters
824 //force direct flag if offload flag is set: offloading implies a direct output stream
825 // and all common behaviors are driven by checking only the direct flag
826 // this should normally be set appropriately in the policy configuration file
827 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700828 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700829 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700830 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
831 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
832 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800833 // only allow deep buffering for music stream type
834 if (stream != AUDIO_STREAM_MUSIC) {
835 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700836 } else if (/* stream == AUDIO_STREAM_MUSIC && */
837 flags == AUDIO_OUTPUT_FLAG_NONE &&
838 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
839 // use DEEP_BUFFER as default output for music stream type
840 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800841 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700842 if (stream == AUDIO_STREAM_TTS) {
843 flags = AUDIO_OUTPUT_FLAG_TTS;
844 }
Eric Laurente552edb2014-03-10 17:42:56 -0700845
Eric Laurentb732cf52014-09-24 19:08:21 -0700846 sp<IOProfile> profile;
847
848 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700849 // and not explicitly requested
850 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800851 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700852 audio_channel_count_from_out_mask(channelMask) <= 2) {
853 goto non_direct_output;
854 }
855
Andy Hung2ddee192015-12-18 17:34:44 -0800856 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
857 // This prevents creating an offloaded track and tearing it down immediately after start
858 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700859 // FIXME: We should check the audio session here but we do not have it in this context.
860 // This may prevent offloading in rare situations where effects are left active by apps
861 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700862
Eric Laurente552edb2014-03-10 17:42:56 -0700863 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800864 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700865 profile = getProfileForDirectOutput(device,
866 samplingRate,
867 format,
868 channelMask,
869 (audio_output_flags_t)flags);
870 }
871
Eric Laurent1c333e22014-05-20 10:48:17 -0700872 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700873 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700874
875 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700876 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700877 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
878 outputDesc = desc;
879 // reuse direct output if currently open and configured with same parameters
880 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800881 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700882 (channelMask == outputDesc->mChannelMask)) {
883 outputDesc->mDirectOpenCount++;
884 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
885 return mOutputs.keyAt(i);
886 }
887 }
888 }
889 // close direct output if currently open and configured with different parameters
890 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700891 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700892 }
Eric Laurent861a6282015-05-18 15:40:16 -0700893
894 // if the selected profile is offloaded and no offload info was specified,
895 // create a default one
896 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100897 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700898 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
899 defaultOffloadInfo.sample_rate = samplingRate;
900 defaultOffloadInfo.channel_mask = channelMask;
901 defaultOffloadInfo.format = format;
902 defaultOffloadInfo.stream_type = stream;
903 defaultOffloadInfo.bit_rate = 0;
904 defaultOffloadInfo.duration_us = -1;
905 defaultOffloadInfo.has_video = true; // conservative
906 defaultOffloadInfo.is_streaming = true; // likely
907 offloadInfo = &defaultOffloadInfo;
908 }
909
Eric Laurentc75307b2015-03-17 15:29:32 -0700910 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700911 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700912 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700913 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700914 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
915 config.sample_rate = samplingRate;
916 config.channel_mask = channelMask;
917 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700918 if (offloadInfo != NULL) {
919 config.offload_info = *offloadInfo;
920 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700921 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700922 &output,
923 &config,
924 &outputDesc->mDevice,
925 String8(""),
926 &outputDesc->mLatency,
927 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700928
929 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700930 if (status != NO_ERROR ||
931 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800932 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700933 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700934 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
935 "format %d %d, channelMask %04x %04x", output, samplingRate,
936 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
937 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700938 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700939 mpClientInterface->closeOutput(output);
940 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800941 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800942 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800943 goto non_direct_output;
944 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700945 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700946 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700947 outputDesc->mSamplingRate = config.sample_rate;
948 outputDesc->mChannelMask = config.channel_mask;
949 outputDesc->mFormat = config.format;
950 outputDesc->mRefCount[stream] = 0;
951 outputDesc->mStopTime[stream] = 0;
952 outputDesc->mDirectOpenCount = 1;
953
Eric Laurente552edb2014-03-10 17:42:56 -0700954 audio_io_handle_t srcOutput = getOutputForEffect();
955 addOutput(output, outputDesc);
956 audio_io_handle_t dstOutput = getOutputForEffect();
957 if (dstOutput == output) {
958 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
959 }
960 mPreviousOutputs = mOutputs;
961 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700962 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700963 return output;
964 }
965
Eric Laurentb732cf52014-09-24 19:08:21 -0700966non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700967
968 // A request for HW A/V sync cannot fallback to a mixed output because time
969 // stamps are embedded in audio data
970 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
971 return AUDIO_IO_HANDLE_NONE;
972 }
973
Eric Laurente552edb2014-03-10 17:42:56 -0700974 // ignoring channel mask due to downmix capability in mixer
975
976 // open a non direct output
977
978 // for non direct outputs, only PCM is supported
979 if (audio_is_linear_pcm(format)) {
980 // get which output is suitable for the specified stream. The actual
981 // routing change will happen when startOutput() will be called
982 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
983
Eric Laurent8838a382014-09-08 16:44:28 -0700984 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
985 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
986 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700987 }
988 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
989 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
990
Paul McLeanaa981192015-03-21 09:55:15 -0700991 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700992
993 return output;
994}
995
Eric Laurente0720872014-03-11 09:30:41 -0700996audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700997 audio_output_flags_t flags,
998 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700999{
1000 // select one output among several that provide a path to a particular device or set of
1001 // devices (the list was previously build by getOutputsForDevice()).
1002 // The priority is as follows:
1003 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001004 // 2: the output with the bit depth the closest to the requested one
1005 // 3: the primary output
1006 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001007
1008 if (outputs.size() == 0) {
1009 return 0;
1010 }
1011 if (outputs.size() == 1) {
1012 return outputs[0];
1013 }
1014
1015 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001016 audio_io_handle_t outputForFlags = 0;
1017 audio_io_handle_t outputForPrimary = 0;
1018 audio_io_handle_t outputForFormat = 0;
1019 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1020 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001021
1022 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001023 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001024 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001025 // if a valid format is specified, skip output if not compatible
1026 if (format != AUDIO_FORMAT_INVALID) {
1027 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001028 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001029 continue;
1030 }
1031 } else if (!audio_is_linear_pcm(format)) {
1032 continue;
1033 }
Eric Laurente6930022016-02-11 10:20:40 -08001034 if (AudioPort::isBetterFormatMatch(
1035 outputDesc->mFormat, bestFormat, format)) {
1036 outputForFormat = outputs[i];
1037 bestFormat = outputDesc->mFormat;
1038 }
Eric Laurent8838a382014-09-08 16:44:28 -07001039 }
1040
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001041 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001042 if (commonFlags >= maxCommonFlags) {
1043 if (commonFlags == maxCommonFlags) {
1044 if (AudioPort::isBetterFormatMatch(
1045 outputDesc->mFormat, bestFormatForFlags, format)) {
1046 outputForFlags = outputs[i];
1047 bestFormatForFlags = outputDesc->mFormat;
1048 }
1049 } else {
1050 outputForFlags = outputs[i];
1051 maxCommonFlags = commonFlags;
1052 bestFormatForFlags = outputDesc->mFormat;
1053 }
Eric Laurente552edb2014-03-10 17:42:56 -07001054 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1055 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001056 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001057 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001058 }
1059 }
1060 }
1061
Eric Laurente6930022016-02-11 10:20:40 -08001062 if (outputForFlags != 0) {
1063 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001064 }
Eric Laurente6930022016-02-11 10:20:40 -08001065 if (outputForFormat != 0) {
1066 return outputForFormat;
1067 }
1068 if (outputForPrimary != 0) {
1069 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001070 }
1071
1072 return outputs[0];
1073}
1074
Eric Laurente0720872014-03-11 09:30:41 -07001075status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001076 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001077 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001078{
Paul McLeanaa981192015-03-21 09:55:15 -07001079 ALOGV("startOutput() output %d, stream %d, session %d",
1080 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001081 ssize_t index = mOutputs.indexOfKey(output);
1082 if (index < 0) {
1083 ALOGW("startOutput() unknown output %d", output);
1084 return BAD_VALUE;
1085 }
1086
Eric Laurentc75307b2015-03-17 15:29:32 -07001087 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1088
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001089 // Routing?
1090 mOutputRoutes.incRouteActivity(session);
1091
Eric Laurentc75307b2015-03-17 15:29:32 -07001092 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001093 AudioMix *policyMix = NULL;
1094 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001095 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001096 policyMix = outputDesc->mPolicyMix;
1097 address = policyMix->mDeviceAddress.string();
1098 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1099 newDevice = policyMix->mDeviceType;
1100 } else {
1101 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1102 }
Eric Laurent493404d2015-04-21 15:07:36 -07001103 } else if (mOutputRoutes.hasRouteChanged(session)) {
1104 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001105 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001106 } else {
1107 newDevice = AUDIO_DEVICE_NONE;
1108 }
1109
1110 uint32_t delayMs = 0;
1111
Eric Laurentc40d9692016-04-13 19:14:13 -07001112 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001113
1114 if (status != NO_ERROR) {
1115 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001116 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001117 }
1118 // Automatically enable the remote submix input when output is started on a re routing mix
1119 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001120 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1121 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001122 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1123 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001124 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001125 "remote-submix");
1126 }
1127
1128 if (delayMs != 0) {
1129 usleep(delayMs * 1000);
1130 }
1131
1132 return status;
1133}
1134
1135status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1136 audio_stream_type_t stream,
1137 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001138 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001139 uint32_t *delayMs)
1140{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001141 // cannot start playback of STREAM_TTS if any other output is being used
1142 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001143
1144 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001145 if (stream == AUDIO_STREAM_TTS) {
1146 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001147 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001148 return INVALID_OPERATION;
1149 } else {
1150 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1151 }
1152 } else {
1153 // some playback other than beacon starts
1154 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1155 }
1156
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001157 // check active before incrementing usage count
1158 bool force = !outputDesc->isActive();
1159
Eric Laurente552edb2014-03-10 17:42:56 -07001160 // increment usage count for this stream on the requested output:
1161 // NOTE that the usage count is the same for duplicated output and hardware output which is
1162 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1163 outputDesc->changeRefCount(stream, 1);
1164
Eric Laurent493404d2015-04-21 15:07:36 -07001165 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001166 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001167 if (device == AUDIO_DEVICE_NONE) {
1168 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001169 }
Eric Laurente552edb2014-03-10 17:42:56 -07001170 routing_strategy strategy = getStrategy(stream);
1171 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001172 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1173 (beaconMuteLatency > 0);
1174 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001175 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001176 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001177 if (desc != outputDesc) {
1178 // force a device change if any other output is managed by the same hw
1179 // module and has a current device selection that differs from selected device.
1180 // In this case, the audio HAL must receive the new device selection so that it can
1181 // change the device currently selected by the other active output.
1182 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001183 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001184 force = true;
1185 }
1186 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001187 // a notification so that audio focus effect can propagate, or that a mute/unmute
1188 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001189 uint32_t latency = desc->latency();
1190 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1191 waitMs = latency;
1192 }
1193 }
1194 }
Eric Laurentc40d9692016-04-13 19:14:13 -07001195 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001196
1197 // handle special case for sonification while in call
1198 if (isInCall()) {
1199 handleIncallSonification(stream, true, false);
1200 }
1201
1202 // apply volume rules for current stream and device if necessary
1203 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001204 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001205 outputDesc,
1206 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001207
1208 // update the outputs if starting an output with a stream that can affect notification
1209 // routing
1210 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001211
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001212 // force reevaluating accessibility routing when ringtone or alarm starts
1213 if (strategy == STRATEGY_SONIFICATION) {
1214 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1215 }
Eric Laurente552edb2014-03-10 17:42:56 -07001216 }
1217 return NO_ERROR;
1218}
1219
1220
Eric Laurente0720872014-03-11 09:30:41 -07001221status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001222 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001223 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001224{
1225 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1226 ssize_t index = mOutputs.indexOfKey(output);
1227 if (index < 0) {
1228 ALOGW("stopOutput() unknown output %d", output);
1229 return BAD_VALUE;
1230 }
1231
Eric Laurentc75307b2015-03-17 15:29:32 -07001232 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001233
Eric Laurentc75307b2015-03-17 15:29:32 -07001234 if (outputDesc->mRefCount[stream] == 1) {
1235 // Automatically disable the remote submix input when output is stopped on a
1236 // re routing mix of type MIX_TYPE_RECORDERS
1237 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1238 outputDesc->mPolicyMix != NULL &&
1239 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1240 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1241 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001242 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001243 "remote-submix");
1244 }
1245 }
1246
1247 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001248 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001249 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001250 int activityCount = mOutputRoutes.decRouteActivity(session);
1251 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1252
1253 if (forceDeviceUpdate) {
1254 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1255 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001256 }
1257
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001258 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001259}
1260
1261status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001262 audio_stream_type_t stream,
1263 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001264{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001265 // always handle stream stop, check which stream type is stopping
1266 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1267
Eric Laurente552edb2014-03-10 17:42:56 -07001268 // handle special case for sonification while in call
1269 if (isInCall()) {
1270 handleIncallSonification(stream, false, false);
1271 }
1272
1273 if (outputDesc->mRefCount[stream] > 0) {
1274 // decrement usage count of this stream on the output
1275 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001276
Eric Laurente552edb2014-03-10 17:42:56 -07001277 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001278 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001279 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001280 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001281 // delay the device switch by twice the latency because stopOutput() is executed when
1282 // the track stop() command is received and at that time the audio track buffer can
1283 // still contain data that needs to be drained. The latency only covers the audio HAL
1284 // and kernel buffers. Also the latency does not always include additional delay in the
1285 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001286 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001287
1288 // force restoring the device selection on other active outputs if it differs from the
1289 // one being selected for this output
1290 for (size_t i = 0; i < mOutputs.size(); i++) {
1291 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001292 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001293 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001294 desc->isActive() &&
1295 outputDesc->sharesHwModuleWith(desc) &&
1296 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001297 setOutputDevice(desc,
1298 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001299 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001300 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001301 }
1302 }
1303 // update the outputs if stopping one with a stream that can affect notification routing
1304 handleNotificationRoutingForStream(stream);
1305 }
1306 return NO_ERROR;
1307 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001308 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001309 return INVALID_OPERATION;
1310 }
1311}
1312
Eric Laurente83b55d2014-11-14 10:06:21 -08001313void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001314 audio_stream_type_t stream __unused,
1315 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001316{
1317 ALOGV("releaseOutput() %d", output);
1318 ssize_t index = mOutputs.indexOfKey(output);
1319 if (index < 0) {
1320 ALOGW("releaseOutput() releasing unknown output %d", output);
1321 return;
1322 }
1323
1324#ifdef AUDIO_POLICY_TEST
1325 int testIndex = testOutputIndex(output);
1326 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001327 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001328 if (outputDesc->isActive()) {
1329 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001330 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001331 mTestOutputs[testIndex] = 0;
1332 }
1333 return;
1334 }
1335#endif //AUDIO_POLICY_TEST
1336
Paul McLeanaa981192015-03-21 09:55:15 -07001337 // Routing
1338 mOutputRoutes.removeRoute(session);
1339
Eric Laurentc75307b2015-03-17 15:29:32 -07001340 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001341 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001342 if (desc->mDirectOpenCount <= 0) {
1343 ALOGW("releaseOutput() invalid open count %d for output %d",
1344 desc->mDirectOpenCount, output);
1345 return;
1346 }
1347 if (--desc->mDirectOpenCount == 0) {
1348 closeOutput(output);
1349 // If effects where present on the output, audioflinger moved them to the primary
1350 // output by default: move them back to the appropriate output.
1351 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001352 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001353 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1354 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001355 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001356 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001357 }
1358 }
1359}
1360
1361
Eric Laurentcaf7f482014-11-25 17:50:47 -08001362status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1363 audio_io_handle_t *input,
1364 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001365 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001366 uint32_t samplingRate,
1367 audio_format_t format,
1368 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001369 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001370 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001371 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001372{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001373 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1374 "session %d, flags %#x",
1375 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001376
Eric Laurentcaf7f482014-11-25 17:50:47 -08001377 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001378 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001379 audio_devices_t device;
1380 // handle legacy remote submix case where the address was not always specified
1381 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001382 audio_source_t inputSource = attr->source;
1383 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001384 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001385
Eric Laurentc447ded2015-01-06 08:47:05 -08001386 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1387 inputSource = AUDIO_SOURCE_MIC;
1388 }
1389 halInputSource = inputSource;
1390
Paul McLean466dc8e2015-04-17 13:15:36 -06001391 // Explicit routing?
1392 sp<DeviceDescriptor> deviceDesc;
1393 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1394 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1395 deviceDesc = mAvailableInputDevices[i];
1396 break;
1397 }
1398 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001399 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001400
Eric Laurentc447ded2015-01-06 08:47:05 -08001401 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001402 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001403 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001404 if (ret != NO_ERROR) {
1405 return ret;
1406 }
1407 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001408 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1409 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001410 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001411 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001412 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001413 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001414 return BAD_VALUE;
1415 }
Eric Laurentc722f302014-12-10 11:21:49 -08001416 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001417 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001418 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1419 // there is an external policy, but this input is attached to a mix of recorders,
1420 // meaning it receives audio injected into the framework, so the recorder doesn't
1421 // know about it and is therefore considered "legacy"
1422 *inputType = API_INPUT_LEGACY;
1423 } else {
1424 // recording a mix of players defined by an external policy, we're rerouting for
1425 // an external policy
1426 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1427 }
Eric Laurentc722f302014-12-10 11:21:49 -08001428 } else if (audio_is_remote_submix_device(device)) {
1429 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001430 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001431 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1432 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001433 } else {
1434 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001435 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001436
Eric Laurent599c7582015-12-07 18:05:55 -08001437 }
1438
1439 *input = getInputForDevice(device, address, session, uid, inputSource,
1440 samplingRate, format, channelMask, flags,
1441 policyMix);
1442 if (*input == AUDIO_IO_HANDLE_NONE) {
1443 mInputRoutes.removeRoute(session);
1444 return INVALID_OPERATION;
1445 }
1446 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1447 return NO_ERROR;
1448}
1449
1450
1451audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1452 String8 address,
1453 audio_session_t session,
1454 uid_t uid,
1455 audio_source_t inputSource,
1456 uint32_t samplingRate,
1457 audio_format_t format,
1458 audio_channel_mask_t channelMask,
1459 audio_input_flags_t flags,
1460 AudioMix *policyMix)
1461{
1462 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1463 audio_source_t halInputSource = inputSource;
1464 bool isSoundTrigger = false;
1465
1466 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1467 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1468 if (index >= 0) {
1469 input = mSoundTriggerSessions.valueFor(session);
1470 isSoundTrigger = true;
1471 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1472 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1473 } else {
1474 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001475 }
1476 }
1477
Andy Hungf129b032015-04-07 13:45:50 -07001478 // find a compatible input profile (not necessarily identical in parameters)
1479 sp<IOProfile> profile;
1480 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001481 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001482 audio_format_t profileFormat = format;
1483 audio_channel_mask_t profileChannelMask = channelMask;
1484 audio_input_flags_t profileFlags = flags;
1485 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001486 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001487 profileSamplingRate, profileFormat, profileChannelMask,
1488 profileFlags);
1489 if (profile != 0) {
1490 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001491 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1492 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001493 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1494 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1495 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001496 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1497 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001498 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001499 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001500 }
Eric Laurente552edb2014-03-10 17:42:56 -07001501 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001502 // Pick input sampling rate if not specified by client
1503 if (samplingRate == 0) {
1504 samplingRate = profileSamplingRate;
1505 }
Eric Laurente552edb2014-03-10 17:42:56 -07001506
Eric Laurent322b4d22015-04-03 15:57:54 -07001507 if (profile->getModuleHandle() == 0) {
1508 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001509 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001510 }
1511
Eric Laurent599c7582015-12-07 18:05:55 -08001512 sp<AudioSession> audioSession = new AudioSession(session,
1513 inputSource,
1514 format,
1515 samplingRate,
1516 channelMask,
1517 flags,
1518 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001519 isSoundTrigger,
1520 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001521
Eric Laurent232f26f2016-02-17 18:06:27 -08001522// TODO enable input reuse
1523#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001524 // reuse an open input if possible
1525 for (size_t i = 0; i < mInputs.size(); i++) {
1526 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001527 // reuse input if it shares the same profile and same sound trigger attribute
1528 if (profile == desc->mProfile &&
1529 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001530
1531 sp<AudioSession> as = desc->getAudioSession(session);
1532 if (as != 0) {
1533 // do not allow unmatching properties on same session
1534 if (as->matches(audioSession)) {
1535 as->changeOpenCount(1);
1536 } else {
1537 ALOGW("getInputForDevice() record with different attributes"
1538 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001539 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001540 }
1541 } else {
1542 desc->addAudioSession(session, audioSession);
1543 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001544 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1545 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001546 }
1547 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001548#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001549
Eric Laurentcf2c0212014-07-25 16:20:43 -07001550 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001551 config.sample_rate = profileSamplingRate;
1552 config.channel_mask = profileChannelMask;
1553 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001554
Eric Laurent322b4d22015-04-03 15:57:54 -07001555 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001556 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001557 &config,
1558 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001559 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001560 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001561 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001562
1563 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001564 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001565 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001566 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001567 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001568 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1569 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001570 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001571 if (input != AUDIO_IO_HANDLE_NONE) {
1572 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001573 }
Eric Laurent599c7582015-12-07 18:05:55 -08001574 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001575 }
1576
Eric Laurent1f2f2232014-06-02 12:01:23 -07001577 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001578 inputDesc->mSamplingRate = profileSamplingRate;
1579 inputDesc->mFormat = profileFormat;
1580 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001581 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001582 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001583 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001584
Eric Laurent599c7582015-12-07 18:05:55 -08001585 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001586 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001587
Eric Laurent599c7582015-12-07 18:05:55 -08001588 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001589}
1590
Eric Laurent4dc68062014-07-28 17:26:49 -07001591status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001592 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001593{
1594 ALOGV("startInput() input %d", input);
1595 ssize_t index = mInputs.indexOfKey(input);
1596 if (index < 0) {
1597 ALOGW("startInput() unknown input %d", input);
1598 return BAD_VALUE;
1599 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001600 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001601
Eric Laurent599c7582015-12-07 18:05:55 -08001602 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1603 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001604 ALOGW("startInput() unknown session %d on input %d", session, input);
1605 return BAD_VALUE;
1606 }
1607
Eric Laurent232f26f2016-02-17 18:06:27 -08001608 // virtual input devices are compatible with other input devices
1609 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001610
Eric Laurent232f26f2016-02-17 18:06:27 -08001611 // for a non-virtual input device, check if there is another (non-virtual) active input
1612 audio_io_handle_t activeInput = mInputs.getActiveInput();
1613 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001614
Eric Laurent232f26f2016-02-17 18:06:27 -08001615 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1616 // otherwise the active input continues and the new input cannot be started.
1617 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1618 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1619 !activeDesc->hasPreemptedSession(session)) {
1620 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1621 //FIXME: consider all active sessions
1622 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1623 audio_session_t activeSession = activeSessions.keyAt(0);
1624 SortedVector<audio_session_t> sessions =
1625 activeDesc->getPreemptedSessions();
1626 sessions.add(activeSession);
1627 inputDesc->setPreemptedSessions(sessions);
1628 stopInput(activeInput, activeSession);
1629 releaseInput(activeInput, activeSession);
1630 } else {
1631 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1632 return INVALID_OPERATION;
1633 }
1634 }
1635
1636 // Do not allow capture if an active voice call is using a software patch and
1637 // the call TX source device is on the same HW module.
1638 // FIXME: would be better to refine to only inputs whose profile connects to the
1639 // call TX device but this information is not in the audio patch
1640 if (mCallTxPatch != 0 &&
1641 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1642 return INVALID_OPERATION;
1643 }
1644 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001645
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001646 // Routing?
1647 mInputRoutes.incRouteActivity(session);
1648
Eric Laurent232f26f2016-02-17 18:06:27 -08001649 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1650 // if input maps to a dynamic policy with an activity listener, notify of state change
1651 if ((inputDesc->mPolicyMix != NULL)
1652 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001653 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001654 MIX_STATE_MIXING);
1655 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001656
Eric Laurent232f26f2016-02-17 18:06:27 -08001657 if (mInputs.activeInputsCount() == 0) {
1658 SoundTrigger::setCaptureState(true);
1659 }
1660 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001661
Eric Laurent232f26f2016-02-17 18:06:27 -08001662 // automatically enable the remote submix output when input is started if not
1663 // used by a policy mix of type MIX_TYPE_RECORDERS
1664 // For remote submix (a virtual device), we open only one input per capture request.
1665 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1666 String8 address = String8("");
1667 if (inputDesc->mPolicyMix == NULL) {
1668 address = String8("0");
1669 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001670 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001671 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001672 if (address != "") {
1673 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1674 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1675 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001676 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001677 }
Eric Laurente552edb2014-03-10 17:42:56 -07001678 }
1679
Eric Laurent599c7582015-12-07 18:05:55 -08001680 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001681
Eric Laurent232f26f2016-02-17 18:06:27 -08001682 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001683 return NO_ERROR;
1684}
1685
Eric Laurent4dc68062014-07-28 17:26:49 -07001686status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1687 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001688{
1689 ALOGV("stopInput() input %d", input);
1690 ssize_t index = mInputs.indexOfKey(input);
1691 if (index < 0) {
1692 ALOGW("stopInput() unknown input %d", input);
1693 return BAD_VALUE;
1694 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001695 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001696
Eric Laurent599c7582015-12-07 18:05:55 -08001697 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001698 if (index < 0) {
1699 ALOGW("stopInput() unknown session %d on input %d", session, input);
1700 return BAD_VALUE;
1701 }
1702
Eric Laurent599c7582015-12-07 18:05:55 -08001703 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001704 ALOGW("stopInput() input %d already stopped", input);
1705 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001706 }
1707
Eric Laurent599c7582015-12-07 18:05:55 -08001708 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001709
1710 // Routing?
1711 mInputRoutes.decRouteActivity(session);
1712
Eric Laurent232f26f2016-02-17 18:06:27 -08001713 if (!inputDesc->isActive()) {
1714 // if input maps to a dynamic policy with an activity listener, notify of state change
1715 if ((inputDesc->mPolicyMix != NULL)
1716 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001717 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001718 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001719 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001720
1721 // automatically disable the remote submix output when input is stopped if not
1722 // used by a policy mix of type MIX_TYPE_RECORDERS
1723 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1724 String8 address = String8("");
1725 if (inputDesc->mPolicyMix == NULL) {
1726 address = String8("0");
1727 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001728 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001729 }
1730 if (address != "") {
1731 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1732 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1733 address, "remote-submix");
1734 }
1735 }
1736
1737 resetInputDevice(input);
1738
1739 if (mInputs.activeInputsCount() == 0) {
1740 SoundTrigger::setCaptureState(false);
1741 }
1742 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001743 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001744 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001745}
1746
Eric Laurent4dc68062014-07-28 17:26:49 -07001747void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1748 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001749{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001750
Eric Laurente552edb2014-03-10 17:42:56 -07001751 ALOGV("releaseInput() %d", input);
1752 ssize_t index = mInputs.indexOfKey(input);
1753 if (index < 0) {
1754 ALOGW("releaseInput() releasing unknown input %d", input);
1755 return;
1756 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001757
1758 // Routing
1759 mInputRoutes.removeRoute(session);
1760
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001761 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1762 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001763
Eric Laurent599c7582015-12-07 18:05:55 -08001764 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001765 if (index < 0) {
1766 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1767 return;
1768 }
Eric Laurent599c7582015-12-07 18:05:55 -08001769
1770 if (audioSession->openCount() == 0) {
1771 ALOGW("releaseInput() invalid open count %d on session %d",
1772 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001773 return;
1774 }
Eric Laurent599c7582015-12-07 18:05:55 -08001775
1776 if (audioSession->changeOpenCount(-1) == 0) {
1777 inputDesc->removeAudioSession(session);
1778 }
1779
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001780 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001781 ALOGV("releaseInput() exit > 0");
1782 return;
1783 }
1784
Eric Laurent05b90f82014-08-27 15:32:29 -07001785 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001786 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001787 ALOGV("releaseInput() exit");
1788}
1789
Eric Laurentd4692962014-05-05 18:13:44 -07001790void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001791 bool patchRemoved = false;
1792
Eric Laurentd4692962014-05-05 18:13:44 -07001793 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001794 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001795 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001796 if (patch_index >= 0) {
1797 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1798 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1799 mAudioPatches.removeItemsAt(patch_index);
1800 patchRemoved = true;
1801 }
Eric Laurentd4692962014-05-05 18:13:44 -07001802 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1803 }
1804 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001805 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001806 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001807
1808 if (patchRemoved) {
1809 mpClientInterface->onAudioPatchListUpdate();
1810 }
Eric Laurentd4692962014-05-05 18:13:44 -07001811}
1812
Eric Laurente0720872014-03-11 09:30:41 -07001813void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001814 int indexMin,
1815 int indexMax)
1816{
1817 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001818 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001819
1820 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001821 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1822 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001823 continue;
1824 }
1825 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001826 }
Eric Laurente552edb2014-03-10 17:42:56 -07001827}
1828
Eric Laurente0720872014-03-11 09:30:41 -07001829status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001830 int index,
1831 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001832{
1833
François Gaffied1ab2bd2015-12-02 18:20:06 +01001834 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1835 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001836 return BAD_VALUE;
1837 }
1838 if (!audio_is_output_device(device)) {
1839 return BAD_VALUE;
1840 }
1841
1842 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001843 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001844
Eric Laurent1fd372e2016-04-06 14:23:53 -07001845 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001846 stream, device, index);
1847
Eric Laurent28d09f02016-03-08 10:43:05 -08001848 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001849 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1850 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001851 continue;
1852 }
1853 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1854 }
Eric Laurente552edb2014-03-10 17:42:56 -07001855
Eric Laurent1fd372e2016-04-06 14:23:53 -07001856 // update volume on all outputs and streams matching the following:
1857 // - The requested stream (or a stream matching for volume control) is active on the output
1858 // - The device (or devices) selected by the strategy corresponding to this stream includes
1859 // the requested device
1860 // - For non default requested device, currently selected device on the output is either the
1861 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001862 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1863 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001864 status_t status = NO_ERROR;
1865 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001866 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1867 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001868 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1869 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001870 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001871 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001872 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1873 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001874 continue;
1875 }
Eric Laurent794fde22016-03-11 09:50:45 -08001876 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001877 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001878 if ((curStreamDevice & device) == 0) {
1879 continue;
1880 }
1881 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001882 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001883 curStreamDevice |= device;
1884 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1885 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1886 applyDefault = true;
1887 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001888
Eric Laurent1fd372e2016-04-06 14:23:53 -07001889 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001890 status_t volStatus =
1891 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1892 if (volStatus != NO_ERROR) {
1893 status = volStatus;
1894 }
1895 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001896 }
Eric Laurente552edb2014-03-10 17:42:56 -07001897 }
1898 return status;
1899}
1900
Eric Laurente0720872014-03-11 09:30:41 -07001901status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001902 int *index,
1903 audio_devices_t device)
1904{
1905 if (index == NULL) {
1906 return BAD_VALUE;
1907 }
1908 if (!audio_is_output_device(device)) {
1909 return BAD_VALUE;
1910 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001911 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001912 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001913 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001914 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1915 }
François Gaffiedfd74092015-03-19 12:10:59 +01001916 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001917
François Gaffied1ab2bd2015-12-02 18:20:06 +01001918 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001919 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1920 return NO_ERROR;
1921}
1922
Eric Laurente0720872014-03-11 09:30:41 -07001923audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001924 const SortedVector<audio_io_handle_t>& outputs)
1925{
1926 // select one output among several suitable for global effects.
1927 // The priority is as follows:
1928 // 1: An offloaded output. If the effect ends up not being offloadable,
1929 // AudioFlinger will invalidate the track and the offloaded output
1930 // will be closed causing the effect to be moved to a PCM output.
1931 // 2: A deep buffer output
1932 // 3: the first output in the list
1933
1934 if (outputs.size() == 0) {
1935 return 0;
1936 }
1937
1938 audio_io_handle_t outputOffloaded = 0;
1939 audio_io_handle_t outputDeepBuffer = 0;
1940
1941 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001942 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001943 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001944 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1945 outputOffloaded = outputs[i];
1946 }
1947 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1948 outputDeepBuffer = outputs[i];
1949 }
1950 }
1951
1952 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1953 outputOffloaded, outputDeepBuffer);
1954 if (outputOffloaded != 0) {
1955 return outputOffloaded;
1956 }
1957 if (outputDeepBuffer != 0) {
1958 return outputDeepBuffer;
1959 }
1960
1961 return outputs[0];
1962}
1963
Eric Laurente0720872014-03-11 09:30:41 -07001964audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001965{
1966 // apply simple rule where global effects are attached to the same output as MUSIC streams
1967
Eric Laurent3b73df72014-03-11 09:06:29 -07001968 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001969 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1970 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1971
1972 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1973 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1974 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1975
1976 return output;
1977}
1978
Eric Laurente0720872014-03-11 09:30:41 -07001979status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001980 audio_io_handle_t io,
1981 uint32_t strategy,
1982 int session,
1983 int id)
1984{
1985 ssize_t index = mOutputs.indexOfKey(io);
1986 if (index < 0) {
1987 index = mInputs.indexOfKey(io);
1988 if (index < 0) {
1989 ALOGW("registerEffect() unknown io %d", io);
1990 return INVALID_OPERATION;
1991 }
1992 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001993 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001994}
1995
Eric Laurentc75307b2015-03-17 15:29:32 -07001996bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1997{
Eric Laurent28d09f02016-03-08 10:43:05 -08001998 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08001999 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2000 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002001 continue;
2002 }
2003 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2004 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002005 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002006}
2007
2008bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2009{
2010 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2011}
2012
Eric Laurente0720872014-03-11 09:30:41 -07002013bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002014{
2015 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002016 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002017 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002018 return true;
2019 }
2020 }
2021 return false;
2022}
2023
Eric Laurent275e8e92014-11-30 15:14:47 -08002024// Register a list of custom mixes with their attributes and format.
2025// When a mix is registered, corresponding input and output profiles are
2026// added to the remote submix hw module. The profile contains only the
2027// parameters (sampling rate, format...) specified by the mix.
2028// The corresponding input remote submix device is also connected.
2029//
2030// When a remote submix device is connected, the address is checked to select the
2031// appropriate profile and the corresponding input or output stream is opened.
2032//
2033// When capture starts, getInputForAttr() will:
2034// - 1 look for a mix matching the address passed in attribtutes tags if any
2035// - 2 if none found, getDeviceForInputSource() will:
2036// - 2.1 look for a mix matching the attributes source
2037// - 2.2 if none found, default to device selection by policy rules
2038// At this time, the corresponding output remote submix device is also connected
2039// and active playback use cases can be transferred to this mix if needed when reconnecting
2040// after AudioTracks are invalidated
2041//
2042// When playback starts, getOutputForAttr() will:
2043// - 1 look for a mix matching the address passed in attribtutes tags if any
2044// - 2 if none found, look for a mix matching the attributes usage
2045// - 3 if none found, default to device and output selection by policy rules.
2046
2047status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2048{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002049 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2050 status_t res = NO_ERROR;
2051
2052 sp<HwModule> rSubmixModule;
2053 // examine each mix's route type
2054 for (size_t i = 0; i < mixes.size(); i++) {
2055 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2056 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2057 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002058 break;
2059 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002060 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2061 // Loop back through "remote submix"
2062 if (rSubmixModule == 0) {
2063 for (size_t j = 0; i < mHwModules.size(); j++) {
2064 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2065 && mHwModules[j]->mHandle != 0) {
2066 rSubmixModule = mHwModules[j];
2067 break;
2068 }
2069 }
2070 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002071
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002072 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002073
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002074 if (rSubmixModule == 0) {
2075 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2076 res = INVALID_OPERATION;
2077 break;
2078 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002079
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002080 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002081
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002082 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002083 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002084 res = INVALID_OPERATION;
2085 break;
2086 }
2087 audio_config_t outputConfig = mixes[i].mFormat;
2088 audio_config_t inputConfig = mixes[i].mFormat;
2089 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2090 // stereo and let audio flinger do the channel conversion if needed.
2091 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2092 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2093 rSubmixModule->addOutputProfile(address, &outputConfig,
2094 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2095 rSubmixModule->addInputProfile(address, &inputConfig,
2096 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002097
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002098 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2099 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2100 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2101 address.string(), "remote-submix");
2102 } else {
2103 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2104 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2105 address.string(), "remote-submix");
2106 }
2107 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002108 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002109 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002110 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2111 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002112
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002113 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002114 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2115 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2116 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2117 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2118 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2119 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002120 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2121 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002122 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2123 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002124 } else {
2125 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002126 }
2127 break;
2128 }
2129 }
2130
2131 if (res != NO_ERROR) {
2132 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002133 i, device, address.string());
2134 res = INVALID_OPERATION;
2135 break;
2136 } else if (!foundOutput) {
2137 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2138 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002139 res = INVALID_OPERATION;
2140 break;
2141 }
Eric Laurentc722f302014-12-10 11:21:49 -08002142 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002143 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002144 if (res != NO_ERROR) {
2145 unregisterPolicyMixes(mixes);
2146 }
2147 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002148}
2149
2150status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2151{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002152 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002153 status_t res = NO_ERROR;
2154 sp<HwModule> rSubmixModule;
2155 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002156 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002157 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002158
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002159 if (rSubmixModule == 0) {
2160 for (size_t j = 0; i < mHwModules.size(); j++) {
2161 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2162 && mHwModules[j]->mHandle != 0) {
2163 rSubmixModule = mHwModules[j];
2164 break;
2165 }
2166 }
2167 }
2168 if (rSubmixModule == 0) {
2169 res = INVALID_OPERATION;
2170 continue;
2171 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002172
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002173 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002174
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002175 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2176 res = INVALID_OPERATION;
2177 continue;
2178 }
2179
2180 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2181 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2182 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2183 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2184 address.string(), "remote-submix");
2185 }
2186 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2187 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2188 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2189 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2190 address.string(), "remote-submix");
2191 }
2192 rSubmixModule->removeOutputProfile(address);
2193 rSubmixModule->removeInputProfile(address);
2194
2195 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2196 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2197 res = INVALID_OPERATION;
2198 continue;
2199 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002200 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002201 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002202 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002203}
2204
Eric Laurente552edb2014-03-10 17:42:56 -07002205
Eric Laurente0720872014-03-11 09:30:41 -07002206status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002207{
2208 const size_t SIZE = 256;
2209 char buffer[SIZE];
2210 String8 result;
2211
2212 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2213 result.append(buffer);
2214
Eric Laurent87ffa392015-05-22 10:32:38 -07002215 snprintf(buffer, SIZE, " Primary Output: %d\n",
2216 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002217 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002218 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002219 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002220 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002221 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002222 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002223 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002224 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002225 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002226 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002227 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002228 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002229 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002230 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002231 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002232 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002233 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002234 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2235 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2236 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002237 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2238 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002239 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2240 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002241
François Gaffie2110e042015-03-24 08:41:51 +01002242 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002243
François Gaffie112b0af2015-11-19 16:13:25 +01002244 mAvailableOutputDevices.dump(fd, String8("Available output"));
2245 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002246 mHwModules.dump(fd);
2247 mOutputs.dump(fd);
2248 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002249 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002250 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002251 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002252
2253 return NO_ERROR;
2254}
2255
2256// This function checks for the parameters which can be offloaded.
2257// This can be enhanced depending on the capability of the DSP and policy
2258// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002259bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002260{
2261 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002262 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002263 offloadInfo.sample_rate, offloadInfo.channel_mask,
2264 offloadInfo.format,
2265 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2266 offloadInfo.has_video);
2267
Andy Hung2ddee192015-12-18 17:34:44 -08002268 if (mMasterMono) {
2269 return false; // no offloading if mono is set.
2270 }
2271
Eric Laurente552edb2014-03-10 17:42:56 -07002272 // Check if offload has been disabled
2273 char propValue[PROPERTY_VALUE_MAX];
2274 if (property_get("audio.offload.disable", propValue, "0")) {
2275 if (atoi(propValue) != 0) {
2276 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2277 return false;
2278 }
2279 }
2280
2281 // Check if stream type is music, then only allow offload as of now.
2282 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2283 {
2284 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2285 return false;
2286 }
2287
2288 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002289 const bool allowOffloadWithVideo =
2290 property_get_bool("audio.offload.video", false /* default_value */);
2291 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002292 ALOGV("isOffloadSupported: has_video == true, returning false");
2293 return false;
2294 }
2295
2296 //If duration is less than minimum value defined in property, return false
2297 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2298 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2299 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2300 return false;
2301 }
2302 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2303 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2304 return false;
2305 }
2306
2307 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2308 // creating an offloaded track and tearing it down immediately after start when audioflinger
2309 // detects there is an active non offloadable effect.
2310 // FIXME: We should check the audio session here but we do not have it in this context.
2311 // This may prevent offloading in rare situations where effects are left active by apps
2312 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002313 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002314 return false;
2315 }
2316
2317 // See if there is a profile to support this.
2318 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002319 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002320 offloadInfo.sample_rate,
2321 offloadInfo.format,
2322 offloadInfo.channel_mask,
2323 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002324 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2325 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002326}
2327
Eric Laurent6a94d692014-05-20 11:18:06 -07002328status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2329 audio_port_type_t type,
2330 unsigned int *num_ports,
2331 struct audio_port *ports,
2332 unsigned int *generation)
2333{
2334 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2335 generation == NULL) {
2336 return BAD_VALUE;
2337 }
2338 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2339 if (ports == NULL) {
2340 *num_ports = 0;
2341 }
2342
2343 size_t portsWritten = 0;
2344 size_t portsMax = *num_ports;
2345 *num_ports = 0;
2346 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002347 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2348 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002349 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002350 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2351 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2352 continue;
2353 }
2354 if (portsWritten < portsMax) {
2355 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2356 }
2357 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002358 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002359 }
2360 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002361 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2362 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2363 continue;
2364 }
2365 if (portsWritten < portsMax) {
2366 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2367 }
2368 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002369 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002370 }
2371 }
2372 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2373 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2374 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2375 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2376 }
2377 *num_ports += mInputs.size();
2378 }
2379 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002380 size_t numOutputs = 0;
2381 for (size_t i = 0; i < mOutputs.size(); i++) {
2382 if (!mOutputs[i]->isDuplicated()) {
2383 numOutputs++;
2384 if (portsWritten < portsMax) {
2385 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2386 }
2387 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002388 }
Eric Laurent84c70242014-06-23 08:46:27 -07002389 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002390 }
2391 }
2392 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002393 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002394 return NO_ERROR;
2395}
2396
2397status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2398{
2399 return NO_ERROR;
2400}
2401
Eric Laurent6a94d692014-05-20 11:18:06 -07002402status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2403 audio_patch_handle_t *handle,
2404 uid_t uid)
2405{
2406 ALOGV("createAudioPatch()");
2407
2408 if (handle == NULL || patch == NULL) {
2409 return BAD_VALUE;
2410 }
2411 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2412
Eric Laurent874c42872014-08-08 15:13:39 -07002413 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2414 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2415 return BAD_VALUE;
2416 }
2417 // only one source per audio patch supported for now
2418 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002419 return INVALID_OPERATION;
2420 }
Eric Laurent874c42872014-08-08 15:13:39 -07002421
2422 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002423 return INVALID_OPERATION;
2424 }
Eric Laurent874c42872014-08-08 15:13:39 -07002425 for (size_t i = 0; i < patch->num_sinks; i++) {
2426 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2427 return INVALID_OPERATION;
2428 }
2429 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002430
2431 sp<AudioPatch> patchDesc;
2432 ssize_t index = mAudioPatches.indexOfKey(*handle);
2433
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2435 patch->sources[0].role,
2436 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002437#if LOG_NDEBUG == 0
2438 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002439 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002440 patch->sinks[i].role,
2441 patch->sinks[i].type);
2442 }
2443#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002444
2445 if (index >= 0) {
2446 patchDesc = mAudioPatches.valueAt(index);
2447 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2448 mUidCached, patchDesc->mUid, uid);
2449 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2450 return INVALID_OPERATION;
2451 }
2452 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002453 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002454 }
2455
2456 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002457 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002458 if (outputDesc == NULL) {
2459 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2460 return BAD_VALUE;
2461 }
Eric Laurent84c70242014-06-23 08:46:27 -07002462 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2463 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002464 if (patchDesc != 0) {
2465 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2466 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2467 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2468 return BAD_VALUE;
2469 }
2470 }
Eric Laurent874c42872014-08-08 15:13:39 -07002471 DeviceVector devices;
2472 for (size_t i = 0; i < patch->num_sinks; i++) {
2473 // Only support mix to devices connection
2474 // TODO add support for mix to mix connection
2475 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2476 ALOGV("createAudioPatch() source mix but sink is not a device");
2477 return INVALID_OPERATION;
2478 }
2479 sp<DeviceDescriptor> devDesc =
2480 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2481 if (devDesc == 0) {
2482 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2483 return BAD_VALUE;
2484 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002485
François Gaffie53615e22015-03-19 09:24:12 +01002486 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002487 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002488 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002489 NULL, // updatedSamplingRate
2490 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002491 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002492 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002493 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002494 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002495 ALOGV("createAudioPatch() profile not supported for device %08x",
2496 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002497 return INVALID_OPERATION;
2498 }
2499 devices.add(devDesc);
2500 }
2501 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002502 return INVALID_OPERATION;
2503 }
Eric Laurent874c42872014-08-08 15:13:39 -07002504
Eric Laurent6a94d692014-05-20 11:18:06 -07002505 // TODO: reconfigure output format and channels here
2506 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002507 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002508 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002509 index = mAudioPatches.indexOfKey(*handle);
2510 if (index >= 0) {
2511 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2512 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2513 }
2514 patchDesc = mAudioPatches.valueAt(index);
2515 patchDesc->mUid = uid;
2516 ALOGV("createAudioPatch() success");
2517 } else {
2518 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2519 return INVALID_OPERATION;
2520 }
2521 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2522 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2523 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002524 // only one sink supported when connecting an input device to a mix
2525 if (patch->num_sinks > 1) {
2526 return INVALID_OPERATION;
2527 }
François Gaffie53615e22015-03-19 09:24:12 +01002528 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002529 if (inputDesc == NULL) {
2530 return BAD_VALUE;
2531 }
2532 if (patchDesc != 0) {
2533 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2534 return BAD_VALUE;
2535 }
2536 }
2537 sp<DeviceDescriptor> devDesc =
2538 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2539 if (devDesc == 0) {
2540 return BAD_VALUE;
2541 }
2542
François Gaffie53615e22015-03-19 09:24:12 +01002543 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002544 devDesc->mAddress,
2545 patch->sinks[0].sample_rate,
2546 NULL, /*updatedSampleRate*/
2547 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002548 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002549 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002550 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002551 // FIXME for the parameter type,
2552 // and the NONE
2553 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002554 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002555 return INVALID_OPERATION;
2556 }
2557 // TODO: reconfigure output format and channels here
2558 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002559 devDesc->type(), inputDesc->mIoHandle);
2560 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002561 index = mAudioPatches.indexOfKey(*handle);
2562 if (index >= 0) {
2563 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2564 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2565 }
2566 patchDesc = mAudioPatches.valueAt(index);
2567 patchDesc->mUid = uid;
2568 ALOGV("createAudioPatch() success");
2569 } else {
2570 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2571 return INVALID_OPERATION;
2572 }
2573 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2574 // device to device connection
2575 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002576 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002577 return BAD_VALUE;
2578 }
2579 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002580 sp<DeviceDescriptor> srcDeviceDesc =
2581 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002582 if (srcDeviceDesc == 0) {
2583 return BAD_VALUE;
2584 }
Eric Laurent874c42872014-08-08 15:13:39 -07002585
Eric Laurent6a94d692014-05-20 11:18:06 -07002586 //update source and sink with our own data as the data passed in the patch may
2587 // be incomplete.
2588 struct audio_patch newPatch = *patch;
2589 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002590
Eric Laurent874c42872014-08-08 15:13:39 -07002591 for (size_t i = 0; i < patch->num_sinks; i++) {
2592 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2593 ALOGV("createAudioPatch() source device but one sink is not a device");
2594 return INVALID_OPERATION;
2595 }
2596
2597 sp<DeviceDescriptor> sinkDeviceDesc =
2598 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2599 if (sinkDeviceDesc == 0) {
2600 return BAD_VALUE;
2601 }
2602 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2603
Eric Laurent3bcf8592015-04-03 12:13:24 -07002604 // create a software bridge in PatchPanel if:
2605 // - source and sink devices are on differnt HW modules OR
2606 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002607 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002608 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002609 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002610 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002611 return INVALID_OPERATION;
2612 }
Eric Laurent874c42872014-08-08 15:13:39 -07002613 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002614 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002615 // if the sink device is reachable via an opened output stream, request to go via
2616 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002617 audio_io_handle_t output = selectOutput(outputs,
2618 AUDIO_OUTPUT_FLAG_NONE,
2619 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002620 if (output != AUDIO_IO_HANDLE_NONE) {
2621 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2622 if (outputDesc->isDuplicated()) {
2623 return INVALID_OPERATION;
2624 }
2625 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002626 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002627 newPatch.num_sources = 2;
2628 }
Eric Laurent83b88082014-06-20 18:31:16 -07002629 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002630 }
2631 // TODO: check from routing capabilities in config file and other conflicting patches
2632
2633 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2634 if (index >= 0) {
2635 afPatchHandle = patchDesc->mAfPatchHandle;
2636 }
2637
2638 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2639 &afPatchHandle,
2640 0);
2641 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2642 status, afPatchHandle);
2643 if (status == NO_ERROR) {
2644 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002645 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002646 addAudioPatch(patchDesc->mHandle, patchDesc);
2647 } else {
2648 patchDesc->mPatch = newPatch;
2649 }
2650 patchDesc->mAfPatchHandle = afPatchHandle;
2651 *handle = patchDesc->mHandle;
2652 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002653 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002654 } else {
2655 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2656 status);
2657 return INVALID_OPERATION;
2658 }
2659 } else {
2660 return BAD_VALUE;
2661 }
2662 } else {
2663 return BAD_VALUE;
2664 }
2665 return NO_ERROR;
2666}
2667
2668status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2669 uid_t uid)
2670{
2671 ALOGV("releaseAudioPatch() patch %d", handle);
2672
2673 ssize_t index = mAudioPatches.indexOfKey(handle);
2674
2675 if (index < 0) {
2676 return BAD_VALUE;
2677 }
2678 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2679 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2680 mUidCached, patchDesc->mUid, uid);
2681 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2682 return INVALID_OPERATION;
2683 }
2684
2685 struct audio_patch *patch = &patchDesc->mPatch;
2686 patchDesc->mUid = mUidCached;
2687 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002688 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002689 if (outputDesc == NULL) {
2690 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2691 return BAD_VALUE;
2692 }
2693
Eric Laurentc75307b2015-03-17 15:29:32 -07002694 setOutputDevice(outputDesc,
2695 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002696 true,
2697 0,
2698 NULL);
2699 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2700 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002701 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002702 if (inputDesc == NULL) {
2703 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2704 return BAD_VALUE;
2705 }
2706 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002707 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002708 true,
2709 NULL);
2710 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2711 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2712 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2713 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2714 status, patchDesc->mAfPatchHandle);
2715 removeAudioPatch(patchDesc->mHandle);
2716 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002717 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002718 } else {
2719 return BAD_VALUE;
2720 }
2721 } else {
2722 return BAD_VALUE;
2723 }
2724 return NO_ERROR;
2725}
2726
2727status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2728 struct audio_patch *patches,
2729 unsigned int *generation)
2730{
François Gaffie53615e22015-03-19 09:24:12 +01002731 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002732 return BAD_VALUE;
2733 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002734 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002735 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002736}
2737
Eric Laurente1715a42014-05-20 11:30:42 -07002738status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002739{
Eric Laurente1715a42014-05-20 11:30:42 -07002740 ALOGV("setAudioPortConfig()");
2741
2742 if (config == NULL) {
2743 return BAD_VALUE;
2744 }
2745 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2746 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002747 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2748 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002749 }
2750
Eric Laurenta121f902014-06-03 13:32:54 -07002751 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002752 if (config->type == AUDIO_PORT_TYPE_MIX) {
2753 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002754 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002755 if (outputDesc == NULL) {
2756 return BAD_VALUE;
2757 }
Eric Laurent84c70242014-06-23 08:46:27 -07002758 ALOG_ASSERT(!outputDesc->isDuplicated(),
2759 "setAudioPortConfig() called on duplicated output %d",
2760 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002761 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002762 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002763 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002764 if (inputDesc == NULL) {
2765 return BAD_VALUE;
2766 }
Eric Laurenta121f902014-06-03 13:32:54 -07002767 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002768 } else {
2769 return BAD_VALUE;
2770 }
2771 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2772 sp<DeviceDescriptor> deviceDesc;
2773 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2774 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2775 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2776 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2777 } else {
2778 return BAD_VALUE;
2779 }
2780 if (deviceDesc == NULL) {
2781 return BAD_VALUE;
2782 }
Eric Laurenta121f902014-06-03 13:32:54 -07002783 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002784 } else {
2785 return BAD_VALUE;
2786 }
2787
Eric Laurenta121f902014-06-03 13:32:54 -07002788 struct audio_port_config backupConfig;
2789 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2790 if (status == NO_ERROR) {
2791 struct audio_port_config newConfig;
2792 audioPortConfig->toAudioPortConfig(&newConfig, config);
2793 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002794 }
Eric Laurenta121f902014-06-03 13:32:54 -07002795 if (status != NO_ERROR) {
2796 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002797 }
Eric Laurente1715a42014-05-20 11:30:42 -07002798
2799 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002800}
2801
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002802void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2803{
Eric Laurentd60560a2015-04-10 11:31:20 -07002804 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002805 clearAudioPatches(uid);
2806 clearSessionRoutes(uid);
2807}
2808
Eric Laurent6a94d692014-05-20 11:18:06 -07002809void AudioPolicyManager::clearAudioPatches(uid_t uid)
2810{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002811 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002812 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2813 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002814 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002815 }
2816 }
2817}
2818
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002819void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2820 audio_io_handle_t ouptutToSkip)
2821{
2822 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2823 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2824 for (size_t j = 0; j < mOutputs.size(); j++) {
2825 if (mOutputs.keyAt(j) == ouptutToSkip) {
2826 continue;
2827 }
2828 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2829 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2830 continue;
2831 }
2832 // If the default device for this strategy is on another output mix,
2833 // invalidate all tracks in this strategy to force re connection.
2834 // Otherwise select new device on the output mix.
2835 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002836 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002837 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2838 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2839 }
2840 }
2841 } else {
2842 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2843 setOutputDevice(outputDesc, newDevice, false);
2844 }
2845 }
2846}
2847
2848void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2849{
2850 // remove output routes associated with this uid
2851 SortedVector<routing_strategy> affectedStrategies;
2852 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2853 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2854 if (route->mUid == uid) {
2855 mOutputRoutes.removeItemsAt(i);
2856 if (route->mDeviceDescriptor != 0) {
2857 affectedStrategies.add(getStrategy(route->mStreamType));
2858 }
2859 }
2860 }
2861 // reroute outputs if necessary
2862 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2863 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2864 }
2865
2866 // remove input routes associated with this uid
2867 SortedVector<audio_source_t> affectedSources;
2868 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2869 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2870 if (route->mUid == uid) {
2871 mInputRoutes.removeItemsAt(i);
2872 if (route->mDeviceDescriptor != 0) {
2873 affectedSources.add(route->mSource);
2874 }
2875 }
2876 }
2877 // reroute inputs if necessary
2878 SortedVector<audio_io_handle_t> inputsToClose;
2879 for (size_t i = 0; i < mInputs.size(); i++) {
2880 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002881 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002882 inputsToClose.add(inputDesc->mIoHandle);
2883 }
2884 }
2885 for (size_t i = 0; i < inputsToClose.size(); i++) {
2886 closeInput(inputsToClose[i]);
2887 }
2888}
2889
Eric Laurentd60560a2015-04-10 11:31:20 -07002890void AudioPolicyManager::clearAudioSources(uid_t uid)
2891{
2892 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2893 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2894 if (sourceDesc->mUid == uid) {
2895 stopAudioSource(mAudioSources.keyAt(i));
2896 }
2897 }
2898}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002899
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002900status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2901 audio_io_handle_t *ioHandle,
2902 audio_devices_t *device)
2903{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002904 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2905 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002906 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002907
François Gaffiedf372692015-03-19 10:43:27 +01002908 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002909}
2910
Eric Laurentd60560a2015-04-10 11:31:20 -07002911status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2912 const audio_attributes_t *attributes,
2913 audio_io_handle_t *handle,
2914 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002915{
Eric Laurentd60560a2015-04-10 11:31:20 -07002916 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2917 if (source == NULL || attributes == NULL || handle == NULL) {
2918 return BAD_VALUE;
2919 }
2920
2921 *handle = AUDIO_IO_HANDLE_NONE;
2922
2923 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2924 source->type != AUDIO_PORT_TYPE_DEVICE) {
2925 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2926 return INVALID_OPERATION;
2927 }
2928
2929 sp<DeviceDescriptor> srcDeviceDesc =
2930 mAvailableInputDevices.getDevice(source->ext.device.type,
2931 String8(source->ext.device.address));
2932 if (srcDeviceDesc == 0) {
2933 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2934 return BAD_VALUE;
2935 }
2936 sp<AudioSourceDescriptor> sourceDesc =
2937 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2938
2939 struct audio_patch dummyPatch;
2940 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2941 sourceDesc->mPatchDesc = patchDesc;
2942
2943 status_t status = connectAudioSource(sourceDesc);
2944 if (status == NO_ERROR) {
2945 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2946 *handle = sourceDesc->getHandle();
2947 }
2948 return status;
2949}
2950
2951status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2952{
2953 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2954
2955 // make sure we only have one patch per source.
2956 disconnectAudioSource(sourceDesc);
2957
2958 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002959 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002960 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2961
2962 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2963 sp<DeviceDescriptor> sinkDeviceDesc =
2964 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2965
2966 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2967 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2968
2969 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2970 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002971 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002972 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2973 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2974 // create patch between src device and output device
2975 // create Hwoutput and add to mHwOutputs
2976 } else {
2977 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2978 audio_io_handle_t output =
2979 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2980 if (output == AUDIO_IO_HANDLE_NONE) {
2981 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2982 return INVALID_OPERATION;
2983 }
2984 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2985 if (outputDesc->isDuplicated()) {
2986 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2987 return INVALID_OPERATION;
2988 }
2989 // create a special patch with no sink and two sources:
2990 // - the second source indicates to PatchPanel through which output mix this patch should
2991 // be connected as well as the stream type for volume control
2992 // - the sink is defined by whatever output device is currently selected for the output
2993 // though which this patch is routed.
2994 patch->num_sinks = 0;
2995 patch->num_sources = 2;
2996 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
2997 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
2998 patch->sources[1].ext.mix.usecase.stream = stream;
2999 status_t status = mpClientInterface->createAudioPatch(patch,
3000 &afPatchHandle,
3001 0);
3002 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3003 status, afPatchHandle);
3004 if (status != NO_ERROR) {
3005 ALOGW("%s patch panel could not connect device patch, error %d",
3006 __FUNCTION__, status);
3007 return INVALID_OPERATION;
3008 }
3009 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003010 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003011
3012 if (status != NO_ERROR) {
3013 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3014 return status;
3015 }
3016 sourceDesc->mSwOutput = outputDesc;
3017 if (delayMs != 0) {
3018 usleep(delayMs * 1000);
3019 }
3020 }
3021
3022 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3023 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3024
3025 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003026}
3027
Eric Laurent493404d2015-04-21 15:07:36 -07003028status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003029{
Eric Laurentd60560a2015-04-10 11:31:20 -07003030 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3031 ALOGV("%s handle %d", __FUNCTION__, handle);
3032 if (sourceDesc == 0) {
3033 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3034 return BAD_VALUE;
3035 }
3036 status_t status = disconnectAudioSource(sourceDesc);
3037
3038 mAudioSources.removeItem(handle);
3039 return status;
3040}
3041
Andy Hung2ddee192015-12-18 17:34:44 -08003042status_t AudioPolicyManager::setMasterMono(bool mono)
3043{
3044 if (mMasterMono == mono) {
3045 return NO_ERROR;
3046 }
3047 mMasterMono = mono;
3048 // if enabling mono we close all offloaded devices, which will invalidate the
3049 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3050 // for recreating the new AudioTrack as non-offloaded PCM.
3051 //
3052 // If disabling mono, we leave all tracks as is: we don't know which clients
3053 // and tracks are able to be recreated as offloaded. The next "song" should
3054 // play back offloaded.
3055 if (mMasterMono) {
3056 Vector<audio_io_handle_t> offloaded;
3057 for (size_t i = 0; i < mOutputs.size(); ++i) {
3058 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3059 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3060 offloaded.push(desc->mIoHandle);
3061 }
3062 }
3063 for (size_t i = 0; i < offloaded.size(); ++i) {
3064 closeOutput(offloaded[i]);
3065 }
3066 }
3067 // update master mono for all remaining outputs
3068 for (size_t i = 0; i < mOutputs.size(); ++i) {
3069 updateMono(mOutputs.keyAt(i));
3070 }
3071 return NO_ERROR;
3072}
3073
3074status_t AudioPolicyManager::getMasterMono(bool *mono)
3075{
3076 *mono = mMasterMono;
3077 return NO_ERROR;
3078}
3079
Eric Laurentd60560a2015-04-10 11:31:20 -07003080status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3081{
3082 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3083
3084 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3085 if (patchDesc == 0) {
3086 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3087 sourceDesc->mPatchDesc->mHandle);
3088 return BAD_VALUE;
3089 }
3090 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3091
Eric Laurent28d09f02016-03-08 10:43:05 -08003092 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003093 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3094 if (swOutputDesc != 0) {
3095 stopSource(swOutputDesc, stream, false);
3096 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3097 } else {
3098 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3099 if (hwOutputDesc != 0) {
3100 // release patch between src device and output device
3101 // close Hwoutput and remove from mHwOutputs
3102 } else {
3103 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3104 }
3105 }
3106 return NO_ERROR;
3107}
3108
3109sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3110 audio_io_handle_t output, routing_strategy strategy)
3111{
3112 sp<AudioSourceDescriptor> source;
3113 for (size_t i = 0; i < mAudioSources.size(); i++) {
3114 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3115 routing_strategy sourceStrategy =
3116 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3117 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3118 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3119 source = sourceDesc;
3120 break;
3121 }
3122 }
3123 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003124}
3125
Eric Laurente552edb2014-03-10 17:42:56 -07003126// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003127// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003128// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003129uint32_t AudioPolicyManager::nextAudioPortGeneration()
3130{
3131 return android_atomic_inc(&mAudioPortGeneration);
3132}
3133
Eric Laurente0720872014-03-11 09:30:41 -07003134AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003135 :
3136#ifdef AUDIO_POLICY_TEST
3137 Thread(false),
3138#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003139 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003140 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003141 mAudioPortGeneration(1),
3142 mBeaconMuteRefCount(0),
3143 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003144 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003145 mTtsOutputAvailable(false),
3146 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003147{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003148 mUidCached = getuid();
3149 mpClientInterface = clientInterface;
3150
3151 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3152 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3153 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3154 bool speakerDrcEnabled = false;
3155
3156#ifdef USE_XML_AUDIO_POLICY_CONF
3157 mVolumeCurves = new VolumeCurvesCollection();
3158 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3159 mDefaultOutputDevice, speakerDrcEnabled,
3160 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3161 PolicySerializer serializer;
3162 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3163#else
3164 mVolumeCurves = new StreamDescriptorCollection();
3165 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3166 mDefaultOutputDevice, speakerDrcEnabled);
3167 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3168 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3169#endif
3170 ALOGE("could not load audio policy configuration file, setting defaults");
3171 config.setDefault();
3172 }
3173 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3174 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3175
3176 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003177 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3178 if (!engineInstance) {
3179 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3180 return;
3181 }
3182 // Retrieve the Policy Manager Interface
3183 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3184 if (mEngine == NULL) {
3185 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3186 return;
3187 }
3188 mEngine->setObserver(this);
3189 status_t status = mEngine->initCheck();
3190 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3191
Eric Laurent3a4311c2014-03-17 12:00:47 -07003192 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003193 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003194 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3195 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003196 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003197 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003198 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003199 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003200 continue;
3201 }
3202 // open all output streams needed to access attached devices
3203 // except for direct output streams that are only opened when they are actually
3204 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003205 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003206 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3207 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003208 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003209
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003210 if (!outProfile->hasSupportedDevices()) {
3211 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003212 continue;
3213 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003214 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003215 mTtsOutputAvailable = true;
3216 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003217
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003218 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003219 continue;
3220 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003221 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003222 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3223 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003224 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003225 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003226 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003227 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003228 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003229 if ((profileType & outputDeviceTypes) == 0) {
3230 continue;
3231 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003232 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3233 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003234 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3235 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3236 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3237 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003238
3239 outputDesc->mDevice = profileType;
3240 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3241 config.sample_rate = outputDesc->mSamplingRate;
3242 config.channel_mask = outputDesc->mChannelMask;
3243 config.format = outputDesc->mFormat;
3244 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003245 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003246 &output,
3247 &config,
3248 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003249 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003250 &outputDesc->mLatency,
3251 outputDesc->mFlags);
3252
3253 if (status != NO_ERROR) {
3254 ALOGW("Cannot open output stream for device %08x on hw module %s",
3255 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003256 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003257 } else {
3258 outputDesc->mSamplingRate = config.sample_rate;
3259 outputDesc->mChannelMask = config.channel_mask;
3260 outputDesc->mFormat = config.format;
3261
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003262 for (size_t k = 0; k < supportedDevices.size(); k++) {
3263 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003264 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003265 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3266 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003267 }
3268 }
3269 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003270 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003271 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003272 }
3273 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003274 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003275 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003276 true,
3277 0,
3278 NULL,
3279 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003280 }
Eric Laurente552edb2014-03-10 17:42:56 -07003281 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003282 // open input streams needed to access attached devices to validate
3283 // mAvailableInputDevices list
3284 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3285 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003286 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003287
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003288 if (!inProfile->hasSupportedDevices()) {
3289 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003290 continue;
3291 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003292 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003293 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003294 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3295
Eric Laurentd78f1532014-09-16 16:38:20 -07003296 if ((profileType & inputDeviceTypes) == 0) {
3297 continue;
3298 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003299 sp<AudioInputDescriptor> inputDesc =
3300 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003301
Eric Laurentd78f1532014-09-16 16:38:20 -07003302 inputDesc->mDevice = profileType;
3303
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003304 // find the address
3305 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3306 // the inputs vector must be of size 1, but we don't want to crash here
3307 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3308 : String8("");
3309 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3310 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3311
Eric Laurentd78f1532014-09-16 16:38:20 -07003312 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3313 config.sample_rate = inputDesc->mSamplingRate;
3314 config.channel_mask = inputDesc->mChannelMask;
3315 config.format = inputDesc->mFormat;
3316 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003317 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003318 &input,
3319 &config,
3320 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003321 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003322 AUDIO_SOURCE_MIC,
3323 AUDIO_INPUT_FLAG_NONE);
3324
3325 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003326 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3327 for (size_t k = 0; k < supportedDevices.size(); k++) {
3328 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003329 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003330 if (index >= 0) {
3331 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3332 if (!devDesc->isAttached()) {
3333 devDesc->attach(mHwModules[i]);
3334 devDesc->importAudioPort(inProfile);
3335 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003336 }
3337 }
3338 mpClientInterface->closeInput(input);
3339 } else {
3340 ALOGW("Cannot open input stream for device %08x on hw module %s",
3341 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003342 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003343 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003344 }
3345 }
3346 // make sure all attached devices have been allocated a unique ID
3347 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003348 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003349 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003350 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3351 continue;
3352 }
François Gaffie2110e042015-03-24 08:41:51 +01003353 // The device is now validated and can be appended to the available devices of the engine
3354 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3355 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003356 i++;
3357 }
3358 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003359 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003360 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003361 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3362 continue;
3363 }
François Gaffie2110e042015-03-24 08:41:51 +01003364 // The device is now validated and can be appended to the available devices of the engine
3365 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3366 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003367 i++;
3368 }
3369 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003370 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003371 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003372 }
Eric Laurente552edb2014-03-10 17:42:56 -07003373
3374 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3375
3376 updateDevicesAndOutputs();
3377
3378#ifdef AUDIO_POLICY_TEST
3379 if (mPrimaryOutput != 0) {
3380 AudioParameter outputCmd = AudioParameter();
3381 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003382 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003383
3384 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3385 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003386 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3387 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003388 mTestLatencyMs = 0;
3389 mCurOutput = 0;
3390 mDirectOutput = false;
3391 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3392 mTestOutputs[i] = 0;
3393 }
3394
3395 const size_t SIZE = 256;
3396 char buffer[SIZE];
3397 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3398 run(buffer, ANDROID_PRIORITY_AUDIO);
3399 }
3400#endif //AUDIO_POLICY_TEST
3401}
3402
Eric Laurente0720872014-03-11 09:30:41 -07003403AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003404{
3405#ifdef AUDIO_POLICY_TEST
3406 exit();
3407#endif //AUDIO_POLICY_TEST
3408 for (size_t i = 0; i < mOutputs.size(); i++) {
3409 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003410 }
3411 for (size_t i = 0; i < mInputs.size(); i++) {
3412 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003413 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003414 mAvailableOutputDevices.clear();
3415 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003416 mOutputs.clear();
3417 mInputs.clear();
3418 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003419}
3420
Eric Laurente0720872014-03-11 09:30:41 -07003421status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003422{
Eric Laurent87ffa392015-05-22 10:32:38 -07003423 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003424}
3425
3426#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003427bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003428{
3429 ALOGV("entering threadLoop()");
3430 while (!exitPending())
3431 {
3432 String8 command;
3433 int valueInt;
3434 String8 value;
3435
3436 Mutex::Autolock _l(mLock);
3437 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3438
3439 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3440 AudioParameter param = AudioParameter(command);
3441
3442 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3443 valueInt != 0) {
3444 ALOGV("Test command %s received", command.string());
3445 String8 target;
3446 if (param.get(String8("target"), target) != NO_ERROR) {
3447 target = "Manager";
3448 }
3449 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3450 param.remove(String8("test_cmd_policy_output"));
3451 mCurOutput = valueInt;
3452 }
3453 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3454 param.remove(String8("test_cmd_policy_direct"));
3455 if (value == "false") {
3456 mDirectOutput = false;
3457 } else if (value == "true") {
3458 mDirectOutput = true;
3459 }
3460 }
3461 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3462 param.remove(String8("test_cmd_policy_input"));
3463 mTestInput = valueInt;
3464 }
3465
3466 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3467 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003468 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003469 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003470 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003471 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003472 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003473 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003474 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003475 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003476 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003477 if (target == "Manager") {
3478 mTestFormat = format;
3479 } else if (mTestOutputs[mCurOutput] != 0) {
3480 AudioParameter outputParam = AudioParameter();
3481 outputParam.addInt(String8("format"), format);
3482 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3483 }
3484 }
3485 }
3486 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3487 param.remove(String8("test_cmd_policy_channels"));
3488 int channels = 0;
3489
3490 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003491 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003492 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003493 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003494 }
3495 if (channels != 0) {
3496 if (target == "Manager") {
3497 mTestChannels = channels;
3498 } else if (mTestOutputs[mCurOutput] != 0) {
3499 AudioParameter outputParam = AudioParameter();
3500 outputParam.addInt(String8("channels"), channels);
3501 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3502 }
3503 }
3504 }
3505 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3506 param.remove(String8("test_cmd_policy_sampleRate"));
3507 if (valueInt >= 0 && valueInt <= 96000) {
3508 int samplingRate = valueInt;
3509 if (target == "Manager") {
3510 mTestSamplingRate = samplingRate;
3511 } else if (mTestOutputs[mCurOutput] != 0) {
3512 AudioParameter outputParam = AudioParameter();
3513 outputParam.addInt(String8("sampling_rate"), samplingRate);
3514 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3515 }
3516 }
3517 }
3518
3519 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3520 param.remove(String8("test_cmd_policy_reopen"));
3521
Eric Laurentc75307b2015-03-17 15:29:32 -07003522 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003523
Eric Laurentc75307b2015-03-17 15:29:32 -07003524 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003525
Eric Laurentc75307b2015-03-17 15:29:32 -07003526 removeOutput(mPrimaryOutput->mIoHandle);
3527 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3528 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003529 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003530 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3531 config.sample_rate = outputDesc->mSamplingRate;
3532 config.channel_mask = outputDesc->mChannelMask;
3533 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003534 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003535 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003536 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003537 &config,
3538 &outputDesc->mDevice,
3539 String8(""),
3540 &outputDesc->mLatency,
3541 outputDesc->mFlags);
3542 if (status != NO_ERROR) {
3543 ALOGE("Failed to reopen hardware output stream, "
3544 "samplingRate: %d, format %d, channels %d",
3545 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003546 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003547 outputDesc->mSamplingRate = config.sample_rate;
3548 outputDesc->mChannelMask = config.channel_mask;
3549 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003550 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003551 AudioParameter outputCmd = AudioParameter();
3552 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003553 mpClientInterface->setParameters(handle, outputCmd.toString());
3554 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003555 }
3556 }
3557
3558
3559 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3560 }
3561 }
3562 return false;
3563}
3564
Eric Laurente0720872014-03-11 09:30:41 -07003565void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003566{
3567 {
3568 AutoMutex _l(mLock);
3569 requestExit();
3570 mWaitWorkCV.signal();
3571 }
3572 requestExitAndWait();
3573}
3574
Eric Laurente0720872014-03-11 09:30:41 -07003575int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003576{
3577 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3578 if (output == mTestOutputs[i]) return i;
3579 }
3580 return 0;
3581}
3582#endif //AUDIO_POLICY_TEST
3583
3584// ---
3585
Eric Laurentc75307b2015-03-17 15:29:32 -07003586void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003587{
François Gaffie98cc1912015-03-18 17:52:40 +01003588 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003589 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003590 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003591 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003592}
3593
François Gaffie53615e22015-03-19 09:24:12 +01003594void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3595{
3596 mOutputs.removeItem(output);
3597}
3598
Eric Laurent1f2f2232014-06-02 12:01:23 -07003599void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003600{
François Gaffie98cc1912015-03-18 17:52:40 +01003601 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003602 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003603 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003604}
Eric Laurente552edb2014-03-10 17:42:56 -07003605
Eric Laurentc75307b2015-03-17 15:29:32 -07003606void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003607 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003608 const String8 address /*in*/,
3609 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003610 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003611 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003612 if (devDesc != 0) {
3613 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3614 desc->mIoHandle, address.string());
3615 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003616 }
3617}
3618
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003619status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003620 audio_policy_dev_state_t state,
3621 SortedVector<audio_io_handle_t>& outputs,
3622 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003623{
François Gaffie53615e22015-03-19 09:24:12 +01003624 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003625 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003626
3627 if (audio_device_is_digital(device)) {
3628 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003629 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003630 }
Eric Laurente552edb2014-03-10 17:42:56 -07003631
Eric Laurent3b73df72014-03-11 09:06:29 -07003632 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003633 // first list already open outputs that can be routed to this device
3634 for (size_t i = 0; i < mOutputs.size(); i++) {
3635 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003636 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003637 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003638 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3639 outputs.add(mOutputs.keyAt(i));
3640 } else {
3641 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003642 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003643 }
Eric Laurente552edb2014-03-10 17:42:56 -07003644 }
3645 }
3646 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003647 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003648 for (size_t i = 0; i < mHwModules.size(); i++)
3649 {
3650 if (mHwModules[i]->mHandle == 0) {
3651 continue;
3652 }
3653 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3654 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003655 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003656 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003657 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003658 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003659 profiles.add(profile);
3660 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3661 }
Eric Laurente552edb2014-03-10 17:42:56 -07003662 }
3663 }
3664 }
3665
Eric Laurent7b279bb2015-12-14 10:18:23 -08003666 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003667
Eric Laurente552edb2014-03-10 17:42:56 -07003668 if (profiles.isEmpty() && outputs.isEmpty()) {
3669 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3670 return BAD_VALUE;
3671 }
3672
3673 // open outputs for matching profiles if needed. Direct outputs are also opened to
3674 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3675 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003676 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003677
3678 // nothing to do if one output is already opened for this profile
3679 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003680 for (j = 0; j < outputs.size(); j++) {
3681 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003682 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003683 // matching profile: save the sample rates, format and channel masks supported
3684 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003685 if (audio_device_is_digital(device)) {
3686 devDesc->importAudioPort(profile);
3687 }
Eric Laurente552edb2014-03-10 17:42:56 -07003688 break;
3689 }
3690 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003691 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003692 continue;
3693 }
3694
Eric Laurent83b88082014-06-20 18:31:16 -07003695 ALOGV("opening output for device %08x with params %s profile %p",
3696 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003697 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003698 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003699 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3700 config.sample_rate = desc->mSamplingRate;
3701 config.channel_mask = desc->mChannelMask;
3702 config.format = desc->mFormat;
3703 config.offload_info.sample_rate = desc->mSamplingRate;
3704 config.offload_info.channel_mask = desc->mChannelMask;
3705 config.offload_info.format = desc->mFormat;
3706 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003707 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003708 &output,
3709 &config,
3710 &desc->mDevice,
3711 address,
3712 &desc->mLatency,
3713 desc->mFlags);
3714 if (status == NO_ERROR) {
3715 desc->mSamplingRate = config.sample_rate;
3716 desc->mChannelMask = config.channel_mask;
3717 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003718
Eric Laurentd4692962014-05-05 18:13:44 -07003719 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003720 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003721 char *param = audio_device_address_to_parameter(device, address);
3722 mpClientInterface->setParameters(output, String8(param));
3723 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003724 }
Phil Burk00eeb322016-03-31 12:41:00 -07003725 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003726 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003727 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003728 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003729 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003730 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003731 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003732 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003733 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003734 config.offload_info.sample_rate = config.sample_rate;
3735 config.offload_info.channel_mask = config.channel_mask;
3736 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003737 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003738 &output,
3739 &config,
3740 &desc->mDevice,
3741 address,
3742 &desc->mLatency,
3743 desc->mFlags);
3744 if (status == NO_ERROR) {
3745 desc->mSamplingRate = config.sample_rate;
3746 desc->mChannelMask = config.channel_mask;
3747 desc->mFormat = config.format;
3748 } else {
3749 output = AUDIO_IO_HANDLE_NONE;
3750 }
Eric Laurentd4692962014-05-05 18:13:44 -07003751 }
3752
Eric Laurentcf2c0212014-07-25 16:20:43 -07003753 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003754 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003755 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003756 sp<AudioPolicyMix> policyMix;
3757 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003758 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3759 address.string());
3760 }
François Gaffie036e1e92015-03-19 10:16:24 +01003761 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003762 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003763
Eric Laurent87ffa392015-05-22 10:32:38 -07003764 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3765 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003766 // no duplicated output for direct outputs and
3767 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003768 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003769
Eric Laurentd4692962014-05-05 18:13:44 -07003770 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003771 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003772
Eric Laurentd4692962014-05-05 18:13:44 -07003773 //TODO: configure audio effect output stage here
3774
3775 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003776 duplicatedOutput =
3777 mpClientInterface->openDuplicateOutput(output,
3778 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003779 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003780 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003781 sp<SwAudioOutputDescriptor> dupOutputDesc =
3782 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3783 dupOutputDesc->mOutput1 = mPrimaryOutput;
3784 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003785 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3786 dupOutputDesc->mFormat = desc->mFormat;
3787 dupOutputDesc->mChannelMask = desc->mChannelMask;
3788 dupOutputDesc->mLatency = desc->mLatency;
3789 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003790 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003791 } else {
3792 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003793 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003794 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003795 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003796 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003797 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003798 }
Eric Laurente552edb2014-03-10 17:42:56 -07003799 }
3800 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003801 } else {
3802 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003803 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003804 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003805 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003806 profiles.removeAt(profile_index);
3807 profile_index--;
3808 } else {
3809 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003810 // Load digital format info only for digital devices
3811 if (audio_device_is_digital(device)) {
3812 devDesc->importAudioPort(profile);
3813 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003814
François Gaffie53615e22015-03-19 09:24:12 +01003815 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003816 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3817 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003818 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003819 NULL/*patch handle*/, address.string());
3820 }
Eric Laurente552edb2014-03-10 17:42:56 -07003821 ALOGV("checkOutputsForDevice(): adding output %d", output);
3822 }
3823 }
3824
3825 if (profiles.isEmpty()) {
3826 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3827 return BAD_VALUE;
3828 }
Eric Laurentd4692962014-05-05 18:13:44 -07003829 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003830 // check if one opened output is not needed any more after disconnecting one device
3831 for (size_t i = 0; i < mOutputs.size(); i++) {
3832 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003833 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003834 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003835 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003836 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003837 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003838 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003839 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3840 mOutputs.keyAt(i));
3841 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003842 }
Eric Laurente552edb2014-03-10 17:42:56 -07003843 }
3844 }
Eric Laurentd4692962014-05-05 18:13:44 -07003845 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003846 for (size_t i = 0; i < mHwModules.size(); i++)
3847 {
3848 if (mHwModules[i]->mHandle == 0) {
3849 continue;
3850 }
3851 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3852 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003853 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003854 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003855 ALOGV("checkOutputsForDevice(): "
3856 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003857 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003858 }
3859 }
3860 }
3861 }
3862 return NO_ERROR;
3863}
3864
Paul McLean9080a4c2015-06-18 08:24:02 -07003865status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003866 audio_policy_dev_state_t state,
3867 SortedVector<audio_io_handle_t>& inputs,
3868 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003869{
Paul McLean9080a4c2015-06-18 08:24:02 -07003870 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003871 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003872
3873 if (audio_device_is_digital(device)) {
3874 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003875 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003876 }
3877
Eric Laurentd4692962014-05-05 18:13:44 -07003878 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3879 // first list already open inputs that can be routed to this device
3880 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3881 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003882 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003883 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3884 inputs.add(mInputs.keyAt(input_index));
3885 }
3886 }
3887
3888 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003889 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003890 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3891 {
3892 if (mHwModules[module_idx]->mHandle == 0) {
3893 continue;
3894 }
3895 for (size_t profile_index = 0;
3896 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3897 profile_index++)
3898 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003899 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3900
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003901 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003902 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003903 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003904 profiles.add(profile);
3905 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3906 profile_index, module_idx);
3907 }
Eric Laurentd4692962014-05-05 18:13:44 -07003908 }
3909 }
3910 }
3911
3912 if (profiles.isEmpty() && inputs.isEmpty()) {
3913 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3914 return BAD_VALUE;
3915 }
3916
3917 // open inputs for matching profiles if needed. Direct inputs are also opened to
3918 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3919 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3920
Eric Laurent1c333e22014-05-20 10:48:17 -07003921 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003922 // nothing to do if one input is already opened for this profile
3923 size_t input_index;
3924 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3925 desc = mInputs.valueAt(input_index);
3926 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003927 if (audio_device_is_digital(device)) {
3928 devDesc->importAudioPort(profile);
3929 }
Eric Laurentd4692962014-05-05 18:13:44 -07003930 break;
3931 }
3932 }
3933 if (input_index != mInputs.size()) {
3934 continue;
3935 }
3936
3937 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3938 desc = new AudioInputDescriptor(profile);
3939 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003940 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3941 config.sample_rate = desc->mSamplingRate;
3942 config.channel_mask = desc->mChannelMask;
3943 config.format = desc->mFormat;
3944 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003945 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003946 &input,
3947 &config,
3948 &desc->mDevice,
3949 address,
3950 AUDIO_SOURCE_MIC,
3951 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003952
Eric Laurentcf2c0212014-07-25 16:20:43 -07003953 if (status == NO_ERROR) {
3954 desc->mSamplingRate = config.sample_rate;
3955 desc->mChannelMask = config.channel_mask;
3956 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003957
Eric Laurentd4692962014-05-05 18:13:44 -07003958 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003959 char *param = audio_device_address_to_parameter(device, address);
3960 mpClientInterface->setParameters(input, String8(param));
3961 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003962 }
Phil Burk00eeb322016-03-31 12:41:00 -07003963 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003964 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003965 ALOGW("checkInputsForDevice() direct input missing param");
3966 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003967 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003968 }
3969
3970 if (input != 0) {
3971 addInput(input, desc);
3972 }
3973 } // endif input != 0
3974
Eric Laurentcf2c0212014-07-25 16:20:43 -07003975 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003976 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003977 profiles.removeAt(profile_index);
3978 profile_index--;
3979 } else {
3980 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003981 if (audio_device_is_digital(device)) {
3982 devDesc->importAudioPort(profile);
3983 }
Eric Laurentd4692962014-05-05 18:13:44 -07003984 ALOGV("checkInputsForDevice(): adding input %d", input);
3985 }
3986 } // end scan profiles
3987
3988 if (profiles.isEmpty()) {
3989 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3990 return BAD_VALUE;
3991 }
3992 } else {
3993 // Disconnect
3994 // check if one opened input is not needed any more after disconnecting one device
3995 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3996 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003997 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07003998 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3999 mInputs.keyAt(input_index));
4000 inputs.add(mInputs.keyAt(input_index));
4001 }
4002 }
4003 // Clear any profiles associated with the disconnected device.
4004 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4005 if (mHwModules[module_index]->mHandle == 0) {
4006 continue;
4007 }
4008 for (size_t profile_index = 0;
4009 profile_index < mHwModules[module_index]->mInputProfiles.size();
4010 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004011 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004012 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004013 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004014 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004015 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004016 }
4017 }
4018 }
4019 } // end disconnect
4020
4021 return NO_ERROR;
4022}
4023
4024
Eric Laurente0720872014-03-11 09:30:41 -07004025void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004026{
4027 ALOGV("closeOutput(%d)", output);
4028
Eric Laurentc75307b2015-03-17 15:29:32 -07004029 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004030 if (outputDesc == NULL) {
4031 ALOGW("closeOutput() unknown output %d", output);
4032 return;
4033 }
François Gaffie036e1e92015-03-19 10:16:24 +01004034 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004035
Eric Laurente552edb2014-03-10 17:42:56 -07004036 // look for duplicated outputs connected to the output being removed.
4037 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004038 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004039 if (dupOutputDesc->isDuplicated() &&
4040 (dupOutputDesc->mOutput1 == outputDesc ||
4041 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004042 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004043 if (dupOutputDesc->mOutput1 == outputDesc) {
4044 outputDesc2 = dupOutputDesc->mOutput2;
4045 } else {
4046 outputDesc2 = dupOutputDesc->mOutput1;
4047 }
4048 // As all active tracks on duplicated output will be deleted,
4049 // and as they were also referenced on the other output, the reference
4050 // count for their stream type must be adjusted accordingly on
4051 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004052 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004053 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004054 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004055 }
4056 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4057 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4058
4059 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004060 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004061 }
4062 }
4063
Eric Laurent05b90f82014-08-27 15:32:29 -07004064 nextAudioPortGeneration();
4065
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004066 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004067 if (index >= 0) {
4068 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4069 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4070 mAudioPatches.removeItemsAt(index);
4071 mpClientInterface->onAudioPatchListUpdate();
4072 }
4073
Eric Laurente552edb2014-03-10 17:42:56 -07004074 AudioParameter param;
4075 param.add(String8("closing"), String8("true"));
4076 mpClientInterface->setParameters(output, param.toString());
4077
4078 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004079 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004080 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004081}
4082
4083void AudioPolicyManager::closeInput(audio_io_handle_t input)
4084{
4085 ALOGV("closeInput(%d)", input);
4086
4087 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4088 if (inputDesc == NULL) {
4089 ALOGW("closeInput() unknown input %d", input);
4090 return;
4091 }
4092
Eric Laurent6a94d692014-05-20 11:18:06 -07004093 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004094
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004095 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004096 if (index >= 0) {
4097 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4098 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4099 mAudioPatches.removeItemsAt(index);
4100 mpClientInterface->onAudioPatchListUpdate();
4101 }
4102
4103 mpClientInterface->closeInput(input);
4104 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004105}
4106
Eric Laurentc75307b2015-03-17 15:29:32 -07004107SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4108 audio_devices_t device,
4109 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004110{
4111 SortedVector<audio_io_handle_t> outputs;
4112
4113 ALOGVV("getOutputsForDevice() device %04x", device);
4114 for (size_t i = 0; i < openOutputs.size(); i++) {
4115 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004116 i, openOutputs.valueAt(i)->isDuplicated(),
4117 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004118 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4119 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4120 outputs.add(openOutputs.keyAt(i));
4121 }
4122 }
4123 return outputs;
4124}
4125
Eric Laurente0720872014-03-11 09:30:41 -07004126bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004127 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004128{
4129 if (outputs1.size() != outputs2.size()) {
4130 return false;
4131 }
4132 for (size_t i = 0; i < outputs1.size(); i++) {
4133 if (outputs1[i] != outputs2[i]) {
4134 return false;
4135 }
4136 }
4137 return true;
4138}
4139
Eric Laurente0720872014-03-11 09:30:41 -07004140void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004141{
4142 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4143 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4144 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4145 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4146
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004147 // also take into account external policy-related changes: add all outputs which are
4148 // associated with policies in the "before" and "after" output vectors
4149 ALOGVV("checkOutputForStrategy(): policy related outputs");
4150 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004151 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004152 if (desc != 0 && desc->mPolicyMix != NULL) {
4153 srcOutputs.add(desc->mIoHandle);
4154 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4155 }
4156 }
4157 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004158 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004159 if (desc != 0 && desc->mPolicyMix != NULL) {
4160 dstOutputs.add(desc->mIoHandle);
4161 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4162 }
4163 }
4164
Eric Laurente552edb2014-03-10 17:42:56 -07004165 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4166 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4167 strategy, srcOutputs[0], dstOutputs[0]);
4168 // mute strategy while moving tracks from one output to another
4169 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004170 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004171 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004172 setStrategyMute(strategy, true, desc);
4173 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004174 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004175 sp<AudioSourceDescriptor> source =
4176 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4177 if (source != 0){
4178 connectAudioSource(source);
4179 }
Eric Laurente552edb2014-03-10 17:42:56 -07004180 }
4181
4182 // Move effects associated to this strategy from previous output to new output
4183 if (strategy == STRATEGY_MEDIA) {
4184 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4185 SortedVector<audio_io_handle_t> moved;
4186 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004187 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4188 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4189 effectDesc->mIo != fxOutput) {
4190 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004191 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4192 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004193 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004194 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004195 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004196 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004197 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004198 }
4199 }
4200 }
4201 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004202 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004203 if (getStrategy((audio_stream_type_t)i) == strategy) {
4204 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004205 }
4206 }
4207 }
4208}
4209
Eric Laurente0720872014-03-11 09:30:41 -07004210void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004211{
François Gaffie2110e042015-03-24 08:41:51 +01004212 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004213 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004214 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004215 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004216 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004217 checkOutputForStrategy(STRATEGY_SONIFICATION);
4218 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004219 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004220 checkOutputForStrategy(STRATEGY_MEDIA);
4221 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004222 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004223}
4224
Eric Laurente0720872014-03-11 09:30:41 -07004225void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004226{
François Gaffie53615e22015-03-19 09:24:12 +01004227 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004228 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004229 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004230 return;
4231 }
4232
Eric Laurent3a4311c2014-03-17 12:00:47 -07004233 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004234 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4235 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4236 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004237 // suspend A2DP output if:
4238 // (NOT already suspended) &&
4239 // ((SCO device is connected &&
4240 // (forced usage for communication || for record is SCO))) ||
4241 // (phone state is ringing || in call)
4242 //
4243 // restore A2DP output if:
4244 // (Already suspended) &&
4245 // ((SCO device is NOT connected ||
4246 // (forced usage NOT for communication && NOT for record is SCO))) &&
4247 // (phone state is NOT ringing && NOT in call)
4248 //
4249 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004250 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004251 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4252 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4253 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4254 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004255
4256 mpClientInterface->restoreOutput(a2dpOutput);
4257 mA2dpSuspended = false;
4258 }
4259 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004260 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004261 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4262 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4263 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4264 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004265
4266 mpClientInterface->suspendOutput(a2dpOutput);
4267 mA2dpSuspended = true;
4268 }
4269 }
4270}
4271
Eric Laurentc75307b2015-03-17 15:29:32 -07004272audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4273 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004274{
4275 audio_devices_t device = AUDIO_DEVICE_NONE;
4276
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004277 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004278 if (index >= 0) {
4279 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4280 if (patchDesc->mUid != mUidCached) {
4281 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004282 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004283 return outputDesc->device();
4284 }
4285 }
4286
Eric Laurente552edb2014-03-10 17:42:56 -07004287 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004288 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004289 // use device for strategy enforced audible
4290 // 2: we are in call or the strategy phone is active on the output:
4291 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004292 // 3: the strategy for enforced audible is active but not enforced on the output:
4293 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004294 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004295 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004296 // 5: the strategy accessibility is active on the output:
4297 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004298 // 6: the strategy "respectful" sonification is active on the output:
4299 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004300 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004301 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004302 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004303 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004304 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004305 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004306 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004307 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004308 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4309 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004310 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004311 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004312 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004313 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004314 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004315 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004316 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4317 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004318 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004319 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004320 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004321 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004322 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004323 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004324 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004325 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004326 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004327 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004328 }
4329
Eric Laurent1c333e22014-05-20 10:48:17 -07004330 ALOGV("getNewOutputDevice() selected device %x", device);
4331 return device;
4332}
4333
Eric Laurent232f26f2016-02-17 18:06:27 -08004334audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004335{
Eric Laurent232f26f2016-02-17 18:06:27 -08004336 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004337
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004338 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004339 if (index >= 0) {
4340 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4341 if (patchDesc->mUid != mUidCached) {
4342 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004343 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004344 return inputDesc->mDevice;
4345 }
4346 }
4347
Eric Laurent232f26f2016-02-17 18:06:27 -08004348 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004349
Eric Laurente552edb2014-03-10 17:42:56 -07004350 return device;
4351}
4352
Eric Laurent794fde22016-03-11 09:50:45 -08004353bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4354 audio_stream_type_t stream2) {
4355 return ((stream1 == stream2) ||
4356 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4357 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004358}
4359
Eric Laurente0720872014-03-11 09:30:41 -07004360uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004361 return (uint32_t)getStrategy(stream);
4362}
4363
Eric Laurente0720872014-03-11 09:30:41 -07004364audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004365 // By checking the range of stream before calling getStrategy, we avoid
4366 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4367 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004368 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004369 return AUDIO_DEVICE_NONE;
4370 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004371 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004372 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4373 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004374 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004375 }
Eric Laurent794fde22016-03-11 09:50:45 -08004376 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004377 audio_devices_t curDevices =
4378 getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
4379 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4380 for (size_t i = 0; i < outputs.size(); i++) {
4381 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004382 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004383 curDevices |= outputDesc->device();
4384 }
4385 }
4386 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004387 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004388
4389 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4390 and doesn't really need to.*/
4391 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4392 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4393 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4394 }
Eric Laurente552edb2014-03-10 17:42:56 -07004395 return devices;
4396}
4397
François Gaffie2110e042015-03-24 08:41:51 +01004398routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4399{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004400 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004401 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004402}
4403
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004404uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4405 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004406 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4407 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4408 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004409 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4410 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4411 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004412 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004413 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004414}
4415
Eric Laurente0720872014-03-11 09:30:41 -07004416void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004417 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004418 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004419 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4420 updateDevicesAndOutputs();
4421 break;
4422 default:
4423 break;
4424 }
4425}
4426
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004427uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004428
4429 // skip beacon mute management if a dedicated TTS output is available
4430 if (mTtsOutputAvailable) {
4431 return 0;
4432 }
4433
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004434 switch(event) {
4435 case STARTING_OUTPUT:
4436 mBeaconMuteRefCount++;
4437 break;
4438 case STOPPING_OUTPUT:
4439 if (mBeaconMuteRefCount > 0) {
4440 mBeaconMuteRefCount--;
4441 }
4442 break;
4443 case STARTING_BEACON:
4444 mBeaconPlayingRefCount++;
4445 break;
4446 case STOPPING_BEACON:
4447 if (mBeaconPlayingRefCount > 0) {
4448 mBeaconPlayingRefCount--;
4449 }
4450 break;
4451 }
4452
4453 if (mBeaconMuteRefCount > 0) {
4454 // any playback causes beacon to be muted
4455 return setBeaconMute(true);
4456 } else {
4457 // no other playback: unmute when beacon starts playing, mute when it stops
4458 return setBeaconMute(mBeaconPlayingRefCount == 0);
4459 }
4460}
4461
4462uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4463 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4464 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4465 // keep track of muted state to avoid repeating mute/unmute operations
4466 if (mBeaconMuted != mute) {
4467 // mute/unmute AUDIO_STREAM_TTS on all outputs
4468 ALOGV("\t muting %d", mute);
4469 uint32_t maxLatency = 0;
4470 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004471 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004472 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004473 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004474 0 /*delay*/, AUDIO_DEVICE_NONE);
4475 const uint32_t latency = desc->latency() * 2;
4476 if (latency > maxLatency) {
4477 maxLatency = latency;
4478 }
4479 }
4480 mBeaconMuted = mute;
4481 return maxLatency;
4482 }
4483 return 0;
4484}
4485
Eric Laurente0720872014-03-11 09:30:41 -07004486audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004487 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004488{
Paul McLeanaa981192015-03-21 09:55:15 -07004489 // Routing
4490 // see if we have an explicit route
4491 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4492 // (getStrategy(stream)).
4493 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4494 // and activity count is non-zero
4495 // the device = the device from the descriptor in the RouteMap, and exit.
4496 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4497 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004498 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4499 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004500 return route->mDeviceDescriptor->type();
4501 }
4502 }
4503
Eric Laurente552edb2014-03-10 17:42:56 -07004504 if (fromCache) {
4505 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4506 strategy, mDeviceForStrategy[strategy]);
4507 return mDeviceForStrategy[strategy];
4508 }
François Gaffie2110e042015-03-24 08:41:51 +01004509 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004510}
4511
Eric Laurente0720872014-03-11 09:30:41 -07004512void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004513{
4514 for (int i = 0; i < NUM_STRATEGIES; i++) {
4515 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4516 }
4517 mPreviousOutputs = mOutputs;
4518}
4519
Eric Laurent1f2f2232014-06-02 12:01:23 -07004520uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004521 audio_devices_t prevDevice,
4522 uint32_t delayMs)
4523{
4524 // mute/unmute strategies using an incompatible device combination
4525 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4526 // if unmuting, unmute only after the specified delay
4527 if (outputDesc->isDuplicated()) {
4528 return 0;
4529 }
4530
4531 uint32_t muteWaitMs = 0;
4532 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004533 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004534
4535 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4536 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004537 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004538 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4539 bool doMute = false;
4540
4541 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4542 doMute = true;
4543 outputDesc->mStrategyMutedByDevice[i] = true;
4544 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4545 doMute = true;
4546 outputDesc->mStrategyMutedByDevice[i] = false;
4547 }
Eric Laurent99401132014-05-07 19:48:15 -07004548 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004549 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004550 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004551 // skip output if it does not share any device with current output
4552 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4553 == AUDIO_DEVICE_NONE) {
4554 continue;
4555 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004556 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4557 mute ? "muting" : "unmuting", i, curDevice);
4558 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004559 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004560 if (mute) {
4561 // FIXME: should not need to double latency if volume could be applied
4562 // immediately by the audioflinger mixer. We must account for the delay
4563 // between now and the next time the audioflinger thread for this output
4564 // will process a buffer (which corresponds to one buffer size,
4565 // usually 1/2 or 1/4 of the latency).
4566 if (muteWaitMs < desc->latency() * 2) {
4567 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004568 }
4569 }
4570 }
4571 }
4572 }
4573 }
4574
Eric Laurent99401132014-05-07 19:48:15 -07004575 // temporary mute output if device selection changes to avoid volume bursts due to
4576 // different per device volumes
4577 if (outputDesc->isActive() && (device != prevDevice)) {
4578 if (muteWaitMs < outputDesc->latency() * 2) {
4579 muteWaitMs = outputDesc->latency() * 2;
4580 }
4581 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004582 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004583 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004584 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004585 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004586 muteWaitMs *2, device);
4587 }
4588 }
4589 }
4590
Eric Laurente552edb2014-03-10 17:42:56 -07004591 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4592 if (muteWaitMs > delayMs) {
4593 muteWaitMs -= delayMs;
4594 usleep(muteWaitMs * 1000);
4595 return muteWaitMs;
4596 }
4597 return 0;
4598}
4599
Eric Laurentc75307b2015-03-17 15:29:32 -07004600uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004601 audio_devices_t device,
4602 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004603 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004604 audio_patch_handle_t *patchHandle,
4605 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004606{
Eric Laurentc75307b2015-03-17 15:29:32 -07004607 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004608 AudioParameter param;
4609 uint32_t muteWaitMs;
4610
4611 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004612 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4613 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004614 return muteWaitMs;
4615 }
4616 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4617 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004618 if ((device != AUDIO_DEVICE_NONE) &&
4619 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004620 return 0;
4621 }
4622
4623 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004624 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004625
4626 audio_devices_t prevDevice = outputDesc->mDevice;
4627
Paul McLeanaa981192015-03-21 09:55:15 -07004628 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004629
4630 if (device != AUDIO_DEVICE_NONE) {
4631 outputDesc->mDevice = device;
4632 }
4633 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4634
4635 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004636 // the requested device is AUDIO_DEVICE_NONE
4637 // OR the requested device is the same as current device
4638 // AND force is not specified
4639 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004640 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004641 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4642 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004643 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004644 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004645 return muteWaitMs;
4646 }
4647
4648 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004649
Eric Laurente552edb2014-03-10 17:42:56 -07004650 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004651 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004652 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004653 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004654 DeviceVector deviceList;
4655 if ((address == NULL) || (strlen(address) == 0)) {
4656 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4657 } else {
4658 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4659 }
4660
Eric Laurent1c333e22014-05-20 10:48:17 -07004661 if (!deviceList.isEmpty()) {
4662 struct audio_patch patch;
4663 outputDesc->toAudioPortConfig(&patch.sources[0]);
4664 patch.num_sources = 1;
4665 patch.num_sinks = 0;
4666 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4667 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004668 patch.num_sinks++;
4669 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004670 ssize_t index;
4671 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4672 index = mAudioPatches.indexOfKey(*patchHandle);
4673 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004674 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004675 }
4676 sp< AudioPatch> patchDesc;
4677 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4678 if (index >= 0) {
4679 patchDesc = mAudioPatches.valueAt(index);
4680 afPatchHandle = patchDesc->mAfPatchHandle;
4681 }
4682
Eric Laurent1c333e22014-05-20 10:48:17 -07004683 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004684 &afPatchHandle,
4685 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004686 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4687 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004688 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004689 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004690 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004691 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004692 addAudioPatch(patchDesc->mHandle, patchDesc);
4693 } else {
4694 patchDesc->mPatch = patch;
4695 }
4696 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004697 if (patchHandle) {
4698 *patchHandle = patchDesc->mHandle;
4699 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004700 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004701 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004702 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004703 }
4704 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004705
4706 // inform all input as well
4707 for (size_t i = 0; i < mInputs.size(); i++) {
4708 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004709 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004710 AudioParameter inputCmd = AudioParameter();
4711 ALOGV("%s: inform input %d of device:%d", __func__,
4712 inputDescriptor->mIoHandle, device);
4713 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4714 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4715 inputCmd.toString(),
4716 delayMs);
4717 }
4718 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004719 }
Eric Laurente552edb2014-03-10 17:42:56 -07004720
4721 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004722 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004723
4724 return muteWaitMs;
4725}
4726
Eric Laurentc75307b2015-03-17 15:29:32 -07004727status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004728 int delayMs,
4729 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004730{
Eric Laurent6a94d692014-05-20 11:18:06 -07004731 ssize_t index;
4732 if (patchHandle) {
4733 index = mAudioPatches.indexOfKey(*patchHandle);
4734 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004735 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004736 }
4737 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004738 return INVALID_OPERATION;
4739 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004740 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4741 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004742 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004743 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004744 removeAudioPatch(patchDesc->mHandle);
4745 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004746 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004747 return status;
4748}
4749
4750status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4751 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004752 bool force,
4753 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004754{
4755 status_t status = NO_ERROR;
4756
Eric Laurent1f2f2232014-06-02 12:01:23 -07004757 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004758 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4759 inputDesc->mDevice = device;
4760
4761 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4762 if (!deviceList.isEmpty()) {
4763 struct audio_patch patch;
4764 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004765 // AUDIO_SOURCE_HOTWORD is for internal use only:
4766 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004767 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004768 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004769 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4770 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004771 patch.num_sinks = 1;
4772 //only one input device for now
4773 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004774 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004775 ssize_t index;
4776 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4777 index = mAudioPatches.indexOfKey(*patchHandle);
4778 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004779 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004780 }
4781 sp< AudioPatch> patchDesc;
4782 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4783 if (index >= 0) {
4784 patchDesc = mAudioPatches.valueAt(index);
4785 afPatchHandle = patchDesc->mAfPatchHandle;
4786 }
4787
Eric Laurent1c333e22014-05-20 10:48:17 -07004788 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004789 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004790 0);
4791 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004792 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004793 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004794 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004795 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004796 addAudioPatch(patchDesc->mHandle, patchDesc);
4797 } else {
4798 patchDesc->mPatch = patch;
4799 }
4800 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004801 if (patchHandle) {
4802 *patchHandle = patchDesc->mHandle;
4803 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004804 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004805 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004806 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004807 }
4808 }
4809 }
4810 return status;
4811}
4812
Eric Laurent6a94d692014-05-20 11:18:06 -07004813status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4814 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004815{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004816 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004817 ssize_t index;
4818 if (patchHandle) {
4819 index = mAudioPatches.indexOfKey(*patchHandle);
4820 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004821 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004822 }
4823 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004824 return INVALID_OPERATION;
4825 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004826 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4827 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004828 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004829 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004830 removeAudioPatch(patchDesc->mHandle);
4831 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004832 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004833 return status;
4834}
4835
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004836sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004837 String8 address,
4838 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004839 audio_format_t& format,
4840 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004841 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004842{
4843 // Choose an input profile based on the requested capture parameters: select the first available
4844 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004845 //
4846 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4847 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004848
4849 for (size_t i = 0; i < mHwModules.size(); i++)
4850 {
4851 if (mHwModules[i]->mHandle == 0) {
4852 continue;
4853 }
4854 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4855 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004856 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004857 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004858 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004859 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004860 format,
4861 &format /*updatedFormat*/,
4862 channelMask,
4863 &channelMask /*updatedChannelMask*/,
4864 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004865
Eric Laurente552edb2014-03-10 17:42:56 -07004866 return profile;
4867 }
4868 }
4869 }
4870 return NULL;
4871}
4872
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004873
4874audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004875 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004876{
François Gaffie036e1e92015-03-19 10:16:24 +01004877 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4878 audio_devices_t selectedDeviceFromMix =
4879 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004880
François Gaffie036e1e92015-03-19 10:16:24 +01004881 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4882 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004883 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004884 return getDeviceForInputSource(inputSource);
4885}
4886
4887audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4888{
Paul McLean466dc8e2015-04-17 13:15:36 -06004889 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4890 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004891 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004892 return route->mDeviceDescriptor->type();
4893 }
4894 }
4895
4896 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004897}
4898
Eric Laurente0720872014-03-11 09:30:41 -07004899float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004900 int index,
4901 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004902{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004903 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004904 // if a headset is connected, apply the following rules to ring tones and notifications
4905 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004906 // - always attenuate notifications volume by 6dB
4907 // - attenuate ring tones volume by 6dB unless music is not playing and
4908 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004909 // - if music is playing, always limit the volume to current music volume,
4910 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004911 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004912 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4913 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4914 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4915 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4916 ((stream_strategy == STRATEGY_SONIFICATION)
4917 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004918 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004919 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004920 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004921 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004922 // when the phone is ringing we must consider that music could have been paused just before
4923 // by the music application and behave as if music was active if the last music track was
4924 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004925 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004926 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004927 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004928 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004929 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004930 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4931 musicDevice),
4932 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004933 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4934 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004935 if (volumeDB > minVolDB) {
4936 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004937 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004938 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004939 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4940 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4941 // on A2DP, also ensure notification volume is not too low compared to media when
4942 // intended to be played
4943 if ((volumeDB > -96.0f) &&
4944 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4945 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4946 stream, device,
4947 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4948 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4949 }
4950 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004951 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4952 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004953 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004954 }
4955 }
4956
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004957 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07004958}
4959
Eric Laurente0720872014-03-11 09:30:41 -07004960status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004961 int index,
4962 const sp<AudioOutputDescriptor>& outputDesc,
4963 audio_devices_t device,
4964 int delayMs,
4965 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004966{
Eric Laurente552edb2014-03-10 17:42:56 -07004967 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004968 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004969 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004970 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004971 return NO_ERROR;
4972 }
François Gaffie2110e042015-03-24 08:41:51 +01004973 audio_policy_forced_cfg_t forceUseForComm =
4974 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004975 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004976 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4977 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004978 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004979 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004980 return INVALID_OPERATION;
4981 }
4982
Eric Laurentc75307b2015-03-17 15:29:32 -07004983 if (device == AUDIO_DEVICE_NONE) {
4984 device = outputDesc->device();
4985 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004986
Eric Laurentffbc80f2015-03-18 18:30:19 -07004987 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004988 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004989 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004990 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004991
Eric Laurentffbc80f2015-03-18 18:30:19 -07004992 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004993
Eric Laurent3b73df72014-03-11 09:06:29 -07004994 if (stream == AUDIO_STREAM_VOICE_CALL ||
4995 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004996 float voiceVolume;
4997 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004998 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004999 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005000 } else {
5001 voiceVolume = 1.0;
5002 }
5003
Eric Laurent18fba842016-03-31 14:41:26 -07005004 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005005 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5006 mLastVoiceVolume = voiceVolume;
5007 }
5008 }
5009
5010 return NO_ERROR;
5011}
5012
Eric Laurentc75307b2015-03-17 15:29:32 -07005013void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5014 audio_devices_t device,
5015 int delayMs,
5016 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005017{
Eric Laurentc75307b2015-03-17 15:29:32 -07005018 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005019
Eric Laurent794fde22016-03-11 09:50:45 -08005020 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005021 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005022 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005023 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005024 device,
5025 delayMs,
5026 force);
5027 }
5028}
5029
Eric Laurente0720872014-03-11 09:30:41 -07005030void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005031 bool on,
5032 const sp<AudioOutputDescriptor>& outputDesc,
5033 int delayMs,
5034 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005035{
Eric Laurent72249d52015-06-08 11:12:15 -07005036 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5037 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005038 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005039 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005040 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005041 }
5042 }
5043}
5044
Eric Laurente0720872014-03-11 09:30:41 -07005045void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005046 bool on,
5047 const sp<AudioOutputDescriptor>& outputDesc,
5048 int delayMs,
5049 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005050{
Eric Laurente552edb2014-03-10 17:42:56 -07005051 if (device == AUDIO_DEVICE_NONE) {
5052 device = outputDesc->device();
5053 }
5054
Eric Laurentc75307b2015-03-17 15:29:32 -07005055 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5056 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005057
5058 if (on) {
5059 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005060 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005061 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005062 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005063 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005064 }
5065 }
5066 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5067 outputDesc->mMuteCount[stream]++;
5068 } else {
5069 if (outputDesc->mMuteCount[stream] == 0) {
5070 ALOGV("setStreamMute() unmuting non muted stream!");
5071 return;
5072 }
5073 if (--outputDesc->mMuteCount[stream] == 0) {
5074 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005075 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005076 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005077 device,
5078 delayMs);
5079 }
5080 }
5081}
5082
Eric Laurente0720872014-03-11 09:30:41 -07005083void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005084 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005085{
Eric Laurent87ffa392015-05-22 10:32:38 -07005086 if(!hasPrimaryOutput()) {
5087 return;
5088 }
5089
Eric Laurente552edb2014-03-10 17:42:56 -07005090 // if the stream pertains to sonification strategy and we are in call we must
5091 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5092 // in the device used for phone strategy and play the tone if the selected device does not
5093 // interfere with the device used for phone strategy
5094 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5095 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005096 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005097 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5098 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005099 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005100 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5101 stream, starting, outputDesc->mDevice, stateChange);
5102 if (outputDesc->mRefCount[stream]) {
5103 int muteCount = 1;
5104 if (stateChange) {
5105 muteCount = outputDesc->mRefCount[stream];
5106 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005107 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005108 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5109 for (int i = 0; i < muteCount; i++) {
5110 setStreamMute(stream, starting, mPrimaryOutput);
5111 }
5112 } else {
5113 ALOGV("handleIncallSonification() high visibility");
5114 if (outputDesc->device() &
5115 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5116 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5117 for (int i = 0; i < muteCount; i++) {
5118 setStreamMute(stream, starting, mPrimaryOutput);
5119 }
5120 }
5121 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005122 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5123 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005124 } else {
5125 mpClientInterface->stopTone();
5126 }
5127 }
5128 }
5129 }
5130}
5131
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005132audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5133{
5134 // flags to stream type mapping
5135 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5136 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5137 }
5138 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5139 return AUDIO_STREAM_BLUETOOTH_SCO;
5140 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005141 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5142 return AUDIO_STREAM_TTS;
5143 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005144
5145 // usage to stream type mapping
5146 switch (attr->usage) {
5147 case AUDIO_USAGE_MEDIA:
5148 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005149 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5150 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005151 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5152 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005153 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5154 return AUDIO_STREAM_SYSTEM;
5155 case AUDIO_USAGE_VOICE_COMMUNICATION:
5156 return AUDIO_STREAM_VOICE_CALL;
5157
5158 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5159 return AUDIO_STREAM_DTMF;
5160
5161 case AUDIO_USAGE_ALARM:
5162 return AUDIO_STREAM_ALARM;
5163 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5164 return AUDIO_STREAM_RING;
5165
5166 case AUDIO_USAGE_NOTIFICATION:
5167 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5168 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5169 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5170 case AUDIO_USAGE_NOTIFICATION_EVENT:
5171 return AUDIO_STREAM_NOTIFICATION;
5172
5173 case AUDIO_USAGE_UNKNOWN:
5174 default:
5175 return AUDIO_STREAM_MUSIC;
5176 }
5177}
Eric Laurente83b55d2014-11-14 10:06:21 -08005178
François Gaffie53615e22015-03-19 09:24:12 +01005179bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5180{
Eric Laurente83b55d2014-11-14 10:06:21 -08005181 // has flags that map to a strategy?
5182 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5183 return true;
5184 }
5185
5186 // has known usage?
5187 switch (paa->usage) {
5188 case AUDIO_USAGE_UNKNOWN:
5189 case AUDIO_USAGE_MEDIA:
5190 case AUDIO_USAGE_VOICE_COMMUNICATION:
5191 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5192 case AUDIO_USAGE_ALARM:
5193 case AUDIO_USAGE_NOTIFICATION:
5194 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5195 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5196 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5197 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5198 case AUDIO_USAGE_NOTIFICATION_EVENT:
5199 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5200 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5201 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5202 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005203 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005204 break;
5205 default:
5206 return false;
5207 }
5208 return true;
5209}
5210
François Gaffiead3183e2015-03-18 16:55:35 +01005211bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5212 routing_strategy strategy, uint32_t inPastMs,
5213 nsecs_t sysTime) const
5214{
5215 if ((sysTime == 0) && (inPastMs != 0)) {
5216 sysTime = systemTime();
5217 }
Eric Laurent794fde22016-03-11 09:50:45 -08005218 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005219 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5220 (NUM_STRATEGIES == strategy)) &&
5221 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5222 return true;
5223 }
5224 }
5225 return false;
5226}
5227
François Gaffie2110e042015-03-24 08:41:51 +01005228audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5229{
5230 return mEngine->getForceUse(usage);
5231}
5232
5233bool AudioPolicyManager::isInCall()
5234{
5235 return isStateInCall(mEngine->getPhoneState());
5236}
5237
5238bool AudioPolicyManager::isStateInCall(int state)
5239{
5240 return is_state_in_call(state);
5241}
5242
Eric Laurentd60560a2015-04-10 11:31:20 -07005243void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5244{
5245 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5246 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5247 if (sourceDesc->mDevice->equals(deviceDesc)) {
5248 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5249 stopAudioSource(sourceDesc->getHandle());
5250 }
5251 }
5252
5253 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5254 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5255 bool release = false;
5256 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5257 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5258 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5259 source->ext.device.type == deviceDesc->type()) {
5260 release = true;
5261 }
5262 }
5263 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5264 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5265 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5266 sink->ext.device.type == deviceDesc->type()) {
5267 release = true;
5268 }
5269 }
5270 if (release) {
5271 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5272 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5273 }
5274 }
5275}
5276
Phil Burk09bc4612016-02-24 15:58:15 -08005277// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005278void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5279 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005280 // TODO Set this based on Config properties.
5281 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005282
5283 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5284 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005285 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005286
5287 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005288 bool supportsAC3 = false;
5289 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005290 bool supportsIEC61937 = false;
5291 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5292 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005293 switch (format) {
5294 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005295 supportsAC3 = true;
5296 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005297 case AUDIO_FORMAT_E_AC3:
5298 case AUDIO_FORMAT_DTS:
5299 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005300 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005301 break;
5302 case AUDIO_FORMAT_IEC61937:
5303 supportsIEC61937 = true;
5304 break;
5305 default:
5306 break;
5307 }
5308 }
Phil Burk09bc4612016-02-24 15:58:15 -08005309
5310 // Modify formats based on surround preferences.
5311 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005312 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5313 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5314 // Remove surround sound related formats.
5315 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5316 audio_format_t format = formats[formatIndex];
5317 switch(format) {
5318 case AUDIO_FORMAT_AC3:
5319 case AUDIO_FORMAT_E_AC3:
5320 case AUDIO_FORMAT_DTS:
5321 case AUDIO_FORMAT_DTS_HD:
5322 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005323 formats.removeAt(formatIndex);
5324 break;
5325 default:
5326 formatIndex++; // keep it
5327 break;
5328 }
Phil Burk09bc4612016-02-24 15:58:15 -08005329 }
Phil Burk07ac1142016-03-25 13:39:29 -07005330 supportsAC3 = false;
5331 supportsOtherSurround = false;
5332 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005333 }
Phil Burk07ac1142016-03-25 13:39:29 -07005334 } else { // AUTO or ALWAYS
5335 // Most TVs support AC3 even if they do not report it in the EDID.
5336 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5337 && !supportsAC3) {
5338 formats.add(AUDIO_FORMAT_AC3);
5339 supportsAC3 = true;
5340 }
5341
5342 // If ALWAYS, add support for raw surround formats if all are missing.
5343 // This assumes that if any of these formats are reported by the HAL
5344 // then the report is valid and should not be modified.
5345 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5346 && !supportsOtherSurround) {
5347 formats.add(AUDIO_FORMAT_E_AC3);
5348 formats.add(AUDIO_FORMAT_DTS);
5349 formats.add(AUDIO_FORMAT_DTS_HD);
5350 supportsOtherSurround = true;
5351 }
5352
5353 // Add support for IEC61937 if any raw surround supported.
5354 // The HAL could do this but add it here, just in case.
5355 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5356 formats.add(AUDIO_FORMAT_IEC61937);
5357 supportsIEC61937 = true;
5358 }
Phil Burk09bc4612016-02-24 15:58:15 -08005359 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005360}
5361
5362// Modify the list of channel masks supported.
5363void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5364 ChannelsVector &channelMasks = *channelMasksPtr;
5365 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5366 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5367
5368 // If NEVER, then remove support for channelMasks > stereo.
5369 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5370 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5371 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5372 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5373 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5374 channelMasks.removeAt(maskIndex);
5375 } else {
5376 maskIndex++;
5377 }
5378 }
5379 // If ALWAYS, then make sure we at least support 5.1
5380 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5381 bool supports5dot1 = false;
5382 // Are there any channel masks that can be considered "surround"?
5383 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5384 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5385 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5386 supports5dot1 = true;
5387 break;
5388 }
5389 }
5390 // If not then add 5.1 support.
5391 if (!supports5dot1) {
5392 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5393 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5394 }
Phil Burk09bc4612016-02-24 15:58:15 -08005395 }
5396}
5397
Phil Burk00eeb322016-03-31 12:41:00 -07005398void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5399 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005400 AudioProfileVector &profiles)
5401{
5402 String8 reply;
5403 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005404
François Gaffie112b0af2015-11-19 16:13:25 +01005405 // Format MUST be checked first to update the list of AudioProfile
5406 if (profiles.hasDynamicFormat()) {
5407 reply = mpClientInterface->getParameters(ioHandle,
5408 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005409 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005410 AudioParameter repliedParameters(reply);
5411 if (repliedParameters.get(
5412 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005413 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5414 return;
5415 }
Phil Burk09bc4612016-02-24 15:58:15 -08005416 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005417 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005418 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005419 }
Phil Burk09bc4612016-02-24 15:58:15 -08005420 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005421 }
5422 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5423
Eric Laurent20eb3a42016-01-26 18:39:17 -08005424 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005425 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005426 ChannelsVector channelMasks;
5427 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005428 AudioParameter requestedParameters;
5429 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5430
5431 if (profiles.hasDynamicRateFor(format)) {
5432 reply = mpClientInterface->getParameters(ioHandle,
5433 requestedParameters.toString() + ";" +
5434 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5435 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005436 AudioParameter repliedParameters(reply);
5437 if (repliedParameters.get(
5438 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5439 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005440 }
5441 }
5442 if (profiles.hasDynamicChannelsFor(format)) {
5443 reply = mpClientInterface->getParameters(ioHandle,
5444 requestedParameters.toString() + ";" +
5445 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5446 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005447 AudioParameter repliedParameters(reply);
5448 if (repliedParameters.get(
5449 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5450 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005451 if (device == AUDIO_DEVICE_OUT_HDMI) {
5452 filterSurroundChannelMasks(&channelMasks);
5453 }
François Gaffie112b0af2015-11-19 16:13:25 +01005454 }
5455 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005456 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005457 }
5458}
Eric Laurentd60560a2015-04-10 11:31:20 -07005459
Eric Laurente552edb2014-03-10 17:42:56 -07005460}; // namespace android