blob: 444e48ab6e1d5b5dbab0f1fddf89f0838dd651b7 [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{
François Gaffie53615e22015-03-19 09:24:12 +0100294 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
295
Eric Laurent3a4311c2014-03-17 12:00:47 -0700296 DeviceVector *deviceVector;
297
Eric Laurente552edb2014-03-10 17:42:56 -0700298 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700299 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700300 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700301 deviceVector = &mAvailableInputDevices;
302 } else {
303 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
304 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700305 }
François Gaffie53615e22015-03-19 09:24:12 +0100306 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800307}
308
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
310{
311 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700312 status_t status;
313 audio_patch_handle_t afPatchHandle;
314 DeviceVector deviceList;
315
Eric Laurent87ffa392015-05-22 10:32:38 -0700316 if(!hasPrimaryOutput()) {
317 return;
318 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800319 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700320 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
321
322 // release existing RX patch if any
323 if (mCallRxPatch != 0) {
324 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
325 mCallRxPatch.clear();
326 }
327 // release TX patch if any
328 if (mCallTxPatch != 0) {
329 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
330 mCallTxPatch.clear();
331 }
332
333 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
334 // via setOutputDevice() on primary output.
335 // Otherwise, create two audio patches for TX and RX path.
336 if (availablePrimaryOutputDevices() & rxDevice) {
337 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
338 // If the TX device is also on the primary HW module, setOutputDevice() will take care
339 // of it due to legacy implementation. If not, create a patch.
340 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
341 == AUDIO_DEVICE_NONE) {
342 createTxPatch = true;
343 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700344 } else { // create RX path audio patch
345 struct audio_patch patch;
346
347 patch.num_sources = 1;
348 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700349 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
350 ALOG_ASSERT(!deviceList.isEmpty(),
351 "updateCallRouting() selected device not in output device list");
352 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
353 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
354 ALOG_ASSERT(!deviceList.isEmpty(),
355 "updateCallRouting() no telephony RX device");
356 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
357
358 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
359 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
360
361 // request to reuse existing output stream if one is already opened to reach the RX device
362 SortedVector<audio_io_handle_t> outputs =
363 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700364 audio_io_handle_t output = selectOutput(outputs,
365 AUDIO_OUTPUT_FLAG_NONE,
366 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700367 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700368 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700369 ALOG_ASSERT(!outputDesc->isDuplicated(),
370 "updateCallRouting() RX device output is duplicated");
371 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700372 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700373 patch.num_sources = 2;
374 }
375
376 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
377 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
378 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
379 status);
380 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100381 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700382 mCallRxPatch->mAfPatchHandle = afPatchHandle;
383 mCallRxPatch->mUid = mUidCached;
384 }
385 createTxPatch = true;
386 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700387 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700388 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700389
Eric Laurentc2730ba2014-07-20 15:47:07 -0700390 patch.num_sources = 1;
391 patch.num_sinks = 1;
392 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
393 ALOG_ASSERT(!deviceList.isEmpty(),
394 "updateCallRouting() selected device not in input device list");
395 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
396 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
397 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
398 ALOG_ASSERT(!deviceList.isEmpty(),
399 "updateCallRouting() no telephony TX device");
400 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
401 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
402
403 SortedVector<audio_io_handle_t> outputs =
404 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700405 audio_io_handle_t output = selectOutput(outputs,
406 AUDIO_OUTPUT_FLAG_NONE,
407 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 // request to reuse existing output stream if one is already opened to reach the TX
409 // path output device
410 if (output != AUDIO_IO_HANDLE_NONE) {
411 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
412 ALOG_ASSERT(!outputDesc->isDuplicated(),
413 "updateCallRouting() RX device output is duplicated");
414 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700415 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700416 patch.num_sources = 2;
417 }
418
Eric Laurentc0a889f2015-10-14 14:36:34 -0700419 // terminate active capture if on the same HW module as the call TX source device
420 // FIXME: would be better to refine to only inputs whose profile connects to the
421 // call TX device but this information is not in the audio patch and logic here must be
422 // symmetric to the one in startInput()
Eric Laurentfb66dd92016-01-28 18:32:03 -0800423 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
424 for (size_t i = 0; i < activeInputs.size(); i++) {
425 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
426 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
427 AudioSessionCollection activeSessions =
428 activeDesc->getAudioSessions(true /*activeOnly*/);
429 for (size_t j = 0; j < activeSessions.size(); j++) {
430 audio_session_t activeSession = activeSessions.keyAt(j);
431 stopInput(activeDesc->mIoHandle, activeSession);
432 releaseInput(activeDesc->mIoHandle, activeSession);
433 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700434 }
435 }
436
Eric Laurentc2730ba2014-07-20 15:47:07 -0700437 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
438 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
439 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
440 status);
441 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100442 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700443 mCallTxPatch->mAfPatchHandle = afPatchHandle;
444 mCallTxPatch->mUid = mUidCached;
445 }
446 }
447}
448
Eric Laurente0720872014-03-11 09:30:41 -0700449void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700450{
451 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100452 // store previous phone state for management of sonification strategy below
453 int oldState = mEngine->getPhoneState();
454
455 if (mEngine->setPhoneState(state) != NO_ERROR) {
456 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700457 return;
458 }
François Gaffie2110e042015-03-24 08:41:51 +0100459 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // if leaving call state, handle special case of active streams
461 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700462 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700463 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800464 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700465 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700466 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800467
Eric Laurent63dea1d2015-07-02 17:10:28 -0700468 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800469 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
471
François Gaffie2110e042015-03-24 08:41:51 +0100472 /**
473 * Switching to or from incall state or switching between telephony and VoIP lead to force
474 * routing command.
475 */
476 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
477 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700478
479 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700480 checkA2dpSuspend();
481 checkOutputForAllStrategies();
482 updateDevicesAndOutputs();
483
Eric Laurente552edb2014-03-10 17:42:56 -0700484 int delayMs = 0;
485 if (isStateInCall(state)) {
486 nsecs_t sysTime = systemTime();
487 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700488 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700489 // mute media and sonification strategies and delay device switch by the largest
490 // latency of any output where either strategy is active.
491 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100492 if ((isStrategyActive(desc, STRATEGY_MEDIA,
493 SONIFICATION_HEADSET_MUSIC_DELAY,
494 sysTime) ||
495 isStrategyActive(desc, STRATEGY_SONIFICATION,
496 SONIFICATION_HEADSET_MUSIC_DELAY,
497 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700498 (delayMs < (int)desc->latency()*2)) {
499 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700500 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700501 setStrategyMute(STRATEGY_MEDIA, true, desc);
502 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700503 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700504 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
505 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700506 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
507 }
508 }
509
Eric Laurent87ffa392015-05-22 10:32:38 -0700510 if (hasPrimaryOutput()) {
511 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
512 // the device returned is not necessarily reachable via this output
513 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
514 // force routing command to audio hardware when ending call
515 // even if no device change is needed
516 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
517 rxDevice = mPrimaryOutput->device();
518 }
Eric Laurente552edb2014-03-10 17:42:56 -0700519
Eric Laurent87ffa392015-05-22 10:32:38 -0700520 if (state == AUDIO_MODE_IN_CALL) {
521 updateCallRouting(rxDevice, delayMs);
522 } else if (oldState == AUDIO_MODE_IN_CALL) {
523 if (mCallRxPatch != 0) {
524 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
525 mCallRxPatch.clear();
526 }
527 if (mCallTxPatch != 0) {
528 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
529 mCallTxPatch.clear();
530 }
531 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
532 } else {
533 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700534 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700535 }
Eric Laurente552edb2014-03-10 17:42:56 -0700536 // if entering in call state, handle special case of active streams
537 // pertaining to sonification strategy see handleIncallSonification()
538 if (isStateInCall(state)) {
539 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800540 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700541 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700542 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700543
544 // force reevaluating accessibility routing when call starts
545 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700546 }
547
548 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700549 if (state == AUDIO_MODE_RINGTONE &&
550 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700551 mLimitRingtoneVolume = true;
552 } else {
553 mLimitRingtoneVolume = false;
554 }
555}
556
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700557audio_mode_t AudioPolicyManager::getPhoneState() {
558 return mEngine->getPhoneState();
559}
560
Eric Laurente0720872014-03-11 09:30:41 -0700561void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700562 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700563{
François Gaffie2110e042015-03-24 08:41:51 +0100564 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700565
François Gaffie2110e042015-03-24 08:41:51 +0100566 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
567 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
568 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700569 }
François Gaffie2110e042015-03-24 08:41:51 +0100570 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
571 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
572 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700573
574 // check for device and output changes triggered by new force usage
575 checkA2dpSuspend();
576 checkOutputForAllStrategies();
577 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800578
Eric Laurent87ffa392015-05-22 10:32:38 -0700579 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700580 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
581 updateCallRouting(newDevice);
582 }
Eric Laurente552edb2014-03-10 17:42:56 -0700583 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700584 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
585 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
586 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
587 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700588 }
Eric Laurente552edb2014-03-10 17:42:56 -0700589 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700590 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700591 }
592 }
593
Eric Laurentfb66dd92016-01-28 18:32:03 -0800594 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
595 for (size_t i = 0; i < activeInputs.size(); i++) {
596 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
597 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700598 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800599 if (activeDesc->mProfile->getSupportedDevices().types() &
600 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
601 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700602 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800603 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700604 }
Eric Laurente552edb2014-03-10 17:42:56 -0700605 }
Eric Laurente552edb2014-03-10 17:42:56 -0700606}
607
Eric Laurente0720872014-03-11 09:30:41 -0700608void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700609{
610 ALOGV("setSystemProperty() property %s, value %s", property, value);
611}
612
613// Find a direct output profile compatible with the parameters passed, even if the input flags do
614// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800615sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700616 audio_devices_t device,
617 uint32_t samplingRate,
618 audio_format_t format,
619 audio_channel_mask_t channelMask,
620 audio_output_flags_t flags)
621{
Eric Laurent861a6282015-05-18 15:40:16 -0700622 // only retain flags that will drive the direct output profile selection
623 // if explicitly requested
624 static const uint32_t kRelevantFlags =
625 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
626 flags =
627 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
628
629 sp<IOProfile> profile;
630
Eric Laurente552edb2014-03-10 17:42:56 -0700631 for (size_t i = 0; i < mHwModules.size(); i++) {
632 if (mHwModules[i]->mHandle == 0) {
633 continue;
634 }
635 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700636 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
637 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700638 samplingRate, NULL /*updatedSamplingRate*/,
639 format, NULL /*updatedFormat*/,
640 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700641 flags)) {
642 continue;
643 }
644 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100645 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700646 continue;
647 }
648 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100649 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700650 continue;
651 }
652 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100653 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700654 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700655 }
Eric Laurente552edb2014-03-10 17:42:56 -0700656 }
657 }
Eric Laurent861a6282015-05-18 15:40:16 -0700658 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700659}
660
Eric Laurente0720872014-03-11 09:30:41 -0700661audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100662 uint32_t samplingRate,
663 audio_format_t format,
664 audio_channel_mask_t channelMask,
665 audio_output_flags_t flags,
666 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700667{
Eric Laurent3b73df72014-03-11 09:06:29 -0700668 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700669 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
670 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
671 device, stream, samplingRate, format, channelMask, flags);
672
Eric Laurente83b55d2014-11-14 10:06:21 -0800673 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
674 stream, samplingRate,format, channelMask,
675 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700676}
677
Eric Laurente83b55d2014-11-14 10:06:21 -0800678status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
679 audio_io_handle_t *output,
680 audio_session_t session,
681 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700682 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800683 uint32_t samplingRate,
684 audio_format_t format,
685 audio_channel_mask_t channelMask,
686 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700687 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800688 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700689{
Eric Laurente83b55d2014-11-14 10:06:21 -0800690 audio_attributes_t attributes;
691 if (attr != NULL) {
692 if (!isValidAttributes(attr)) {
693 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
694 attr->usage, attr->content_type, attr->flags,
695 attr->tags);
696 return BAD_VALUE;
697 }
698 attributes = *attr;
699 } else {
700 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
701 ALOGE("getOutputForAttr(): invalid stream type");
702 return BAD_VALUE;
703 }
704 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700705 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700706 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800707 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100708 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800709 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100710 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800711 }
François Gaffie036e1e92015-03-19 10:16:24 +0100712 *stream = streamTypefromAttributesInt(&attributes);
713 *output = desc->mIoHandle;
714 ALOGV("getOutputForAttr() returns output %d", *output);
715 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800716 }
717 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
718 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
719 return BAD_VALUE;
720 }
721
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700722 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
723 " session %d selectedDeviceId %d",
724 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
725 session, selectedDeviceId);
726
727 *stream = streamTypefromAttributesInt(&attributes);
728
729 // Explicit routing?
730 sp<DeviceDescriptor> deviceDesc;
731 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
732 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
733 deviceDesc = mAvailableOutputDevices[i];
734 break;
735 }
736 }
737 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700738
Eric Laurente83b55d2014-11-14 10:06:21 -0800739 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700740 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700741
Eric Laurente83b55d2014-11-14 10:06:21 -0800742 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700743 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
744 }
745
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700746 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700747 device, samplingRate, format, channelMask, flags);
748
Eric Laurente83b55d2014-11-14 10:06:21 -0800749 *output = getOutputForDevice(device, session, *stream,
750 samplingRate, format, channelMask,
751 flags, offloadInfo);
752 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700753 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800754 return INVALID_OPERATION;
755 }
Paul McLeanaa981192015-03-21 09:55:15 -0700756
Eric Laurente83b55d2014-11-14 10:06:21 -0800757 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700758}
759
760audio_io_handle_t AudioPolicyManager::getOutputForDevice(
761 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800762 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700763 audio_stream_type_t stream,
764 uint32_t samplingRate,
765 audio_format_t format,
766 audio_channel_mask_t channelMask,
767 audio_output_flags_t flags,
768 const audio_offload_info_t *offloadInfo)
769{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700770 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700771 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700772 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700773
Eric Laurente552edb2014-03-10 17:42:56 -0700774#ifdef AUDIO_POLICY_TEST
775 if (mCurOutput != 0) {
776 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
777 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
778
779 if (mTestOutputs[mCurOutput] == 0) {
780 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700781 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
782 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700783 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700784 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700785 outputDesc->mFlags =
786 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700787 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700788 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
789 config.sample_rate = mTestSamplingRate;
790 config.channel_mask = mTestChannels;
791 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700792 if (offloadInfo != NULL) {
793 config.offload_info = *offloadInfo;
794 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700795 status = mpClientInterface->openOutput(0,
796 &mTestOutputs[mCurOutput],
797 &config,
798 &outputDesc->mDevice,
799 String8(""),
800 &outputDesc->mLatency,
801 outputDesc->mFlags);
802 if (status == NO_ERROR) {
803 outputDesc->mSamplingRate = config.sample_rate;
804 outputDesc->mFormat = config.format;
805 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700806 AudioParameter outputCmd = AudioParameter();
807 outputCmd.addInt(String8("set_id"),mCurOutput);
808 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
809 addOutput(mTestOutputs[mCurOutput], outputDesc);
810 }
811 }
812 return mTestOutputs[mCurOutput];
813 }
814#endif //AUDIO_POLICY_TEST
815
816 // open a direct output if required by specified parameters
817 //force direct flag if offload flag is set: offloading implies a direct output stream
818 // and all common behaviors are driven by checking only the direct flag
819 // this should normally be set appropriately in the policy configuration file
820 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700821 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700822 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700823 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
824 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
825 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800826 // only allow deep buffering for music stream type
827 if (stream != AUDIO_STREAM_MUSIC) {
828 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700829 } else if (/* stream == AUDIO_STREAM_MUSIC && */
830 flags == AUDIO_OUTPUT_FLAG_NONE &&
831 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
832 // use DEEP_BUFFER as default output for music stream type
833 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800834 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700835 if (stream == AUDIO_STREAM_TTS) {
836 flags = AUDIO_OUTPUT_FLAG_TTS;
837 }
Eric Laurente552edb2014-03-10 17:42:56 -0700838
Eric Laurentb732cf52014-09-24 19:08:21 -0700839 sp<IOProfile> profile;
840
841 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700842 // and not explicitly requested
843 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800844 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700845 audio_channel_count_from_out_mask(channelMask) <= 2) {
846 goto non_direct_output;
847 }
848
Andy Hung2ddee192015-12-18 17:34:44 -0800849 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
850 // This prevents creating an offloaded track and tearing it down immediately after start
851 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700852 // FIXME: We should check the audio session here but we do not have it in this context.
853 // This may prevent offloading in rare situations where effects are left active by apps
854 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700855
Eric Laurente552edb2014-03-10 17:42:56 -0700856 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800857 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700858 profile = getProfileForDirectOutput(device,
859 samplingRate,
860 format,
861 channelMask,
862 (audio_output_flags_t)flags);
863 }
864
Eric Laurent1c333e22014-05-20 10:48:17 -0700865 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700866 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700867
868 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700869 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700870 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
871 outputDesc = desc;
872 // reuse direct output if currently open and configured with same parameters
873 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800874 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700875 (channelMask == outputDesc->mChannelMask)) {
876 outputDesc->mDirectOpenCount++;
877 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
878 return mOutputs.keyAt(i);
879 }
880 }
881 }
882 // close direct output if currently open and configured with different parameters
883 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700884 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700885 }
Eric Laurent861a6282015-05-18 15:40:16 -0700886
887 // if the selected profile is offloaded and no offload info was specified,
888 // create a default one
889 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100890 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700891 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
892 defaultOffloadInfo.sample_rate = samplingRate;
893 defaultOffloadInfo.channel_mask = channelMask;
894 defaultOffloadInfo.format = format;
895 defaultOffloadInfo.stream_type = stream;
896 defaultOffloadInfo.bit_rate = 0;
897 defaultOffloadInfo.duration_us = -1;
898 defaultOffloadInfo.has_video = true; // conservative
899 defaultOffloadInfo.is_streaming = true; // likely
900 offloadInfo = &defaultOffloadInfo;
901 }
902
Eric Laurentc75307b2015-03-17 15:29:32 -0700903 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700904 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700905 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700906 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700907 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
908 config.sample_rate = samplingRate;
909 config.channel_mask = channelMask;
910 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700911 if (offloadInfo != NULL) {
912 config.offload_info = *offloadInfo;
913 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700914 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700915 &output,
916 &config,
917 &outputDesc->mDevice,
918 String8(""),
919 &outputDesc->mLatency,
920 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700921
922 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700923 if (status != NO_ERROR ||
924 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800925 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700926 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700927 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
928 "format %d %d, channelMask %04x %04x", output, samplingRate,
929 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
930 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700931 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700932 mpClientInterface->closeOutput(output);
933 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800934 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800935 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800936 goto non_direct_output;
937 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700938 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700939 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700940 outputDesc->mSamplingRate = config.sample_rate;
941 outputDesc->mChannelMask = config.channel_mask;
942 outputDesc->mFormat = config.format;
943 outputDesc->mRefCount[stream] = 0;
944 outputDesc->mStopTime[stream] = 0;
945 outputDesc->mDirectOpenCount = 1;
946
Eric Laurente552edb2014-03-10 17:42:56 -0700947 audio_io_handle_t srcOutput = getOutputForEffect();
948 addOutput(output, outputDesc);
949 audio_io_handle_t dstOutput = getOutputForEffect();
950 if (dstOutput == output) {
951 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
952 }
953 mPreviousOutputs = mOutputs;
954 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700955 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700956 return output;
957 }
958
Eric Laurentb732cf52014-09-24 19:08:21 -0700959non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700960
961 // A request for HW A/V sync cannot fallback to a mixed output because time
962 // stamps are embedded in audio data
963 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
964 return AUDIO_IO_HANDLE_NONE;
965 }
966
Eric Laurente552edb2014-03-10 17:42:56 -0700967 // ignoring channel mask due to downmix capability in mixer
968
969 // open a non direct output
970
971 // for non direct outputs, only PCM is supported
972 if (audio_is_linear_pcm(format)) {
973 // get which output is suitable for the specified stream. The actual
974 // routing change will happen when startOutput() will be called
975 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
976
Eric Laurent8838a382014-09-08 16:44:28 -0700977 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
978 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
979 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700980 }
981 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
982 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
983
Paul McLeanaa981192015-03-21 09:55:15 -0700984 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700985
986 return output;
987}
988
Eric Laurente0720872014-03-11 09:30:41 -0700989audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700990 audio_output_flags_t flags,
991 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700992{
993 // select one output among several that provide a path to a particular device or set of
994 // devices (the list was previously build by getOutputsForDevice()).
995 // The priority is as follows:
996 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -0800997 // 2: the output with the bit depth the closest to the requested one
998 // 3: the primary output
999 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001000
1001 if (outputs.size() == 0) {
1002 return 0;
1003 }
1004 if (outputs.size() == 1) {
1005 return outputs[0];
1006 }
1007
1008 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001009 audio_io_handle_t outputForFlags = 0;
1010 audio_io_handle_t outputForPrimary = 0;
1011 audio_io_handle_t outputForFormat = 0;
1012 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1013 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001014
1015 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001016 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001017 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001018 // if a valid format is specified, skip output if not compatible
1019 if (format != AUDIO_FORMAT_INVALID) {
1020 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001021 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001022 continue;
1023 }
1024 } else if (!audio_is_linear_pcm(format)) {
1025 continue;
1026 }
Eric Laurente6930022016-02-11 10:20:40 -08001027 if (AudioPort::isBetterFormatMatch(
1028 outputDesc->mFormat, bestFormat, format)) {
1029 outputForFormat = outputs[i];
1030 bestFormat = outputDesc->mFormat;
1031 }
Eric Laurent8838a382014-09-08 16:44:28 -07001032 }
1033
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001034 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001035 if (commonFlags >= maxCommonFlags) {
1036 if (commonFlags == maxCommonFlags) {
1037 if (AudioPort::isBetterFormatMatch(
1038 outputDesc->mFormat, bestFormatForFlags, format)) {
1039 outputForFlags = outputs[i];
1040 bestFormatForFlags = outputDesc->mFormat;
1041 }
1042 } else {
1043 outputForFlags = outputs[i];
1044 maxCommonFlags = commonFlags;
1045 bestFormatForFlags = outputDesc->mFormat;
1046 }
Eric Laurente552edb2014-03-10 17:42:56 -07001047 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1048 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001049 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001050 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001051 }
1052 }
1053 }
1054
Eric Laurente6930022016-02-11 10:20:40 -08001055 if (outputForFlags != 0) {
1056 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001057 }
Eric Laurente6930022016-02-11 10:20:40 -08001058 if (outputForFormat != 0) {
1059 return outputForFormat;
1060 }
1061 if (outputForPrimary != 0) {
1062 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001063 }
1064
1065 return outputs[0];
1066}
1067
Eric Laurente0720872014-03-11 09:30:41 -07001068status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001069 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001070 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001071{
Paul McLeanaa981192015-03-21 09:55:15 -07001072 ALOGV("startOutput() output %d, stream %d, session %d",
1073 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001074 ssize_t index = mOutputs.indexOfKey(output);
1075 if (index < 0) {
1076 ALOGW("startOutput() unknown output %d", output);
1077 return BAD_VALUE;
1078 }
1079
Eric Laurentc75307b2015-03-17 15:29:32 -07001080 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1081
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001082 // Routing?
1083 mOutputRoutes.incRouteActivity(session);
1084
Eric Laurentc75307b2015-03-17 15:29:32 -07001085 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001086 AudioMix *policyMix = NULL;
1087 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001088 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001089 policyMix = outputDesc->mPolicyMix;
1090 address = policyMix->mDeviceAddress.string();
1091 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1092 newDevice = policyMix->mDeviceType;
1093 } else {
1094 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1095 }
Eric Laurent493404d2015-04-21 15:07:36 -07001096 } else if (mOutputRoutes.hasRouteChanged(session)) {
1097 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001098 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001099 } else {
1100 newDevice = AUDIO_DEVICE_NONE;
1101 }
1102
1103 uint32_t delayMs = 0;
1104
Eric Laurentc40d9692016-04-13 19:14:13 -07001105 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001106
1107 if (status != NO_ERROR) {
1108 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001109 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001110 }
1111 // Automatically enable the remote submix input when output is started on a re routing mix
1112 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001113 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1114 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001115 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1116 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001117 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001118 "remote-submix");
1119 }
1120
1121 if (delayMs != 0) {
1122 usleep(delayMs * 1000);
1123 }
1124
1125 return status;
1126}
1127
1128status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1129 audio_stream_type_t stream,
1130 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001131 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001132 uint32_t *delayMs)
1133{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001134 // cannot start playback of STREAM_TTS if any other output is being used
1135 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001136
1137 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001138 if (stream == AUDIO_STREAM_TTS) {
1139 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001140 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001141 return INVALID_OPERATION;
1142 } else {
1143 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1144 }
1145 } else {
1146 // some playback other than beacon starts
1147 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1148 }
1149
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001150 // check active before incrementing usage count
1151 bool force = !outputDesc->isActive();
1152
Eric Laurente552edb2014-03-10 17:42:56 -07001153 // increment usage count for this stream on the requested output:
1154 // NOTE that the usage count is the same for duplicated output and hardware output which is
1155 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1156 outputDesc->changeRefCount(stream, 1);
1157
Eric Laurent493404d2015-04-21 15:07:36 -07001158 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001159 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 if (device == AUDIO_DEVICE_NONE) {
1161 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001162 }
Eric Laurente552edb2014-03-10 17:42:56 -07001163 routing_strategy strategy = getStrategy(stream);
1164 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001165 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1166 (beaconMuteLatency > 0);
1167 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001168 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001169 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001170 if (desc != outputDesc) {
1171 // force a device change if any other output is managed by the same hw
1172 // module and has a current device selection that differs from selected device.
1173 // In this case, the audio HAL must receive the new device selection so that it can
1174 // change the device currently selected by the other active output.
1175 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001176 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001177 force = true;
1178 }
1179 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001180 // a notification so that audio focus effect can propagate, or that a mute/unmute
1181 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001182 uint32_t latency = desc->latency();
1183 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1184 waitMs = latency;
1185 }
1186 }
1187 }
Eric Laurentc40d9692016-04-13 19:14:13 -07001188 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001189
1190 // handle special case for sonification while in call
1191 if (isInCall()) {
1192 handleIncallSonification(stream, true, false);
1193 }
1194
1195 // apply volume rules for current stream and device if necessary
1196 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001197 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 outputDesc,
1199 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001200
1201 // update the outputs if starting an output with a stream that can affect notification
1202 // routing
1203 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001204
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001205 // force reevaluating accessibility routing when ringtone or alarm starts
1206 if (strategy == STRATEGY_SONIFICATION) {
1207 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1208 }
Eric Laurente552edb2014-03-10 17:42:56 -07001209 }
1210 return NO_ERROR;
1211}
1212
1213
Eric Laurente0720872014-03-11 09:30:41 -07001214status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001215 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001216 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001217{
1218 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1219 ssize_t index = mOutputs.indexOfKey(output);
1220 if (index < 0) {
1221 ALOGW("stopOutput() unknown output %d", output);
1222 return BAD_VALUE;
1223 }
1224
Eric Laurentc75307b2015-03-17 15:29:32 -07001225 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001226
Eric Laurentc75307b2015-03-17 15:29:32 -07001227 if (outputDesc->mRefCount[stream] == 1) {
1228 // Automatically disable the remote submix input when output is stopped on a
1229 // re routing mix of type MIX_TYPE_RECORDERS
1230 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1231 outputDesc->mPolicyMix != NULL &&
1232 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1233 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1234 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001235 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001236 "remote-submix");
1237 }
1238 }
1239
1240 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001241 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001242 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001243 int activityCount = mOutputRoutes.decRouteActivity(session);
1244 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1245
1246 if (forceDeviceUpdate) {
1247 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1248 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001249 }
1250
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001251 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001252}
1253
1254status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001255 audio_stream_type_t stream,
1256 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001257{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001258 // always handle stream stop, check which stream type is stopping
1259 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1260
Eric Laurente552edb2014-03-10 17:42:56 -07001261 // handle special case for sonification while in call
1262 if (isInCall()) {
1263 handleIncallSonification(stream, false, false);
1264 }
1265
1266 if (outputDesc->mRefCount[stream] > 0) {
1267 // decrement usage count of this stream on the output
1268 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001269
Eric Laurente552edb2014-03-10 17:42:56 -07001270 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001271 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001272 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001273 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001274 // delay the device switch by twice the latency because stopOutput() is executed when
1275 // the track stop() command is received and at that time the audio track buffer can
1276 // still contain data that needs to be drained. The latency only covers the audio HAL
1277 // and kernel buffers. Also the latency does not always include additional delay in the
1278 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001279 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001280
1281 // force restoring the device selection on other active outputs if it differs from the
1282 // one being selected for this output
1283 for (size_t i = 0; i < mOutputs.size(); i++) {
1284 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001285 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001286 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001287 desc->isActive() &&
1288 outputDesc->sharesHwModuleWith(desc) &&
1289 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001290 setOutputDevice(desc,
1291 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001292 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001293 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001294 }
1295 }
1296 // update the outputs if stopping one with a stream that can affect notification routing
1297 handleNotificationRoutingForStream(stream);
1298 }
1299 return NO_ERROR;
1300 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001301 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001302 return INVALID_OPERATION;
1303 }
1304}
1305
Eric Laurente83b55d2014-11-14 10:06:21 -08001306void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001307 audio_stream_type_t stream __unused,
1308 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001309{
1310 ALOGV("releaseOutput() %d", output);
1311 ssize_t index = mOutputs.indexOfKey(output);
1312 if (index < 0) {
1313 ALOGW("releaseOutput() releasing unknown output %d", output);
1314 return;
1315 }
1316
1317#ifdef AUDIO_POLICY_TEST
1318 int testIndex = testOutputIndex(output);
1319 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001320 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001321 if (outputDesc->isActive()) {
1322 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001323 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001324 mTestOutputs[testIndex] = 0;
1325 }
1326 return;
1327 }
1328#endif //AUDIO_POLICY_TEST
1329
Paul McLeanaa981192015-03-21 09:55:15 -07001330 // Routing
1331 mOutputRoutes.removeRoute(session);
1332
Eric Laurentc75307b2015-03-17 15:29:32 -07001333 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001334 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001335 if (desc->mDirectOpenCount <= 0) {
1336 ALOGW("releaseOutput() invalid open count %d for output %d",
1337 desc->mDirectOpenCount, output);
1338 return;
1339 }
1340 if (--desc->mDirectOpenCount == 0) {
1341 closeOutput(output);
1342 // If effects where present on the output, audioflinger moved them to the primary
1343 // output by default: move them back to the appropriate output.
1344 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001345 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001346 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1347 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001348 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001349 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001350 }
1351 }
1352}
1353
1354
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1356 audio_io_handle_t *input,
1357 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001358 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001359 uint32_t samplingRate,
1360 audio_format_t format,
1361 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001362 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001363 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001364 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001365{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001366 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1367 "session %d, flags %#x",
1368 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001369
Eric Laurentcaf7f482014-11-25 17:50:47 -08001370 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001371 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001372
Eric Laurent275e8e92014-11-30 15:14:47 -08001373 audio_devices_t device;
1374 // handle legacy remote submix case where the address was not always specified
1375 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001376 audio_source_t inputSource = attr->source;
1377 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001378 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001379
Eric Laurentc447ded2015-01-06 08:47:05 -08001380 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1381 inputSource = AUDIO_SOURCE_MIC;
1382 }
1383 halInputSource = inputSource;
1384
Paul McLean466dc8e2015-04-17 13:15:36 -06001385 // Explicit routing?
1386 sp<DeviceDescriptor> deviceDesc;
1387 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1388 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1389 deviceDesc = mAvailableInputDevices[i];
1390 break;
1391 }
1392 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001393 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001394
Eric Laurentc447ded2015-01-06 08:47:05 -08001395 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001396 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001397 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001398 if (ret != NO_ERROR) {
1399 return ret;
1400 }
1401 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001402 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1403 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001404 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001405 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001406 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001407 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001408 return BAD_VALUE;
1409 }
Eric Laurentc722f302014-12-10 11:21:49 -08001410 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001411 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001412 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1413 // there is an external policy, but this input is attached to a mix of recorders,
1414 // meaning it receives audio injected into the framework, so the recorder doesn't
1415 // know about it and is therefore considered "legacy"
1416 *inputType = API_INPUT_LEGACY;
1417 } else {
1418 // recording a mix of players defined by an external policy, we're rerouting for
1419 // an external policy
1420 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1421 }
Eric Laurentc722f302014-12-10 11:21:49 -08001422 } else if (audio_is_remote_submix_device(device)) {
1423 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001424 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001425 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1426 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001427 } else {
1428 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001429 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001430
Eric Laurent599c7582015-12-07 18:05:55 -08001431 }
1432
1433 *input = getInputForDevice(device, address, session, uid, inputSource,
1434 samplingRate, format, channelMask, flags,
1435 policyMix);
1436 if (*input == AUDIO_IO_HANDLE_NONE) {
1437 mInputRoutes.removeRoute(session);
1438 return INVALID_OPERATION;
1439 }
1440 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1441 return NO_ERROR;
1442}
1443
1444
1445audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1446 String8 address,
1447 audio_session_t session,
1448 uid_t uid,
1449 audio_source_t inputSource,
1450 uint32_t samplingRate,
1451 audio_format_t format,
1452 audio_channel_mask_t channelMask,
1453 audio_input_flags_t flags,
1454 AudioMix *policyMix)
1455{
1456 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1457 audio_source_t halInputSource = inputSource;
1458 bool isSoundTrigger = false;
1459
1460 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1461 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1462 if (index >= 0) {
1463 input = mSoundTriggerSessions.valueFor(session);
1464 isSoundTrigger = true;
1465 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1466 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1467 } else {
1468 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001469 }
1470 }
1471
Andy Hungf129b032015-04-07 13:45:50 -07001472 // find a compatible input profile (not necessarily identical in parameters)
1473 sp<IOProfile> profile;
1474 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001475 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001476 audio_format_t profileFormat = format;
1477 audio_channel_mask_t profileChannelMask = channelMask;
1478 audio_input_flags_t profileFlags = flags;
1479 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001480 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001481 profileSamplingRate, profileFormat, profileChannelMask,
1482 profileFlags);
1483 if (profile != 0) {
1484 break; // success
1485 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1486 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1487 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001488 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1489 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001490 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001491 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001492 }
Eric Laurente552edb2014-03-10 17:42:56 -07001493 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001494 // Pick input sampling rate if not specified by client
1495 if (samplingRate == 0) {
1496 samplingRate = profileSamplingRate;
1497 }
Eric Laurente552edb2014-03-10 17:42:56 -07001498
Eric Laurent322b4d22015-04-03 15:57:54 -07001499 if (profile->getModuleHandle() == 0) {
1500 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001501 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001502 }
1503
Eric Laurent599c7582015-12-07 18:05:55 -08001504 sp<AudioSession> audioSession = new AudioSession(session,
1505 inputSource,
1506 format,
1507 samplingRate,
1508 channelMask,
1509 flags,
1510 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001511 isSoundTrigger,
1512 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001513
Eric Laurentfb66dd92016-01-28 18:32:03 -08001514
Eric Laurent599c7582015-12-07 18:05:55 -08001515 // reuse an open input if possible
1516 for (size_t i = 0; i < mInputs.size(); i++) {
1517 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001518 // reuse input if:
1519 // - it shares the same profile
1520 // AND
1521 // - it is not a reroute submix input
1522 // AND
1523 // - it is: not used for sound trigger
1524 // OR
1525 // used for sound trigger and all clients use the same session ID
1526 //
1527 if ((profile == desc->mProfile) &&
1528 (isSoundTrigger == desc->isSoundTrigger()) &&
1529 !is_virtual_input_device(device)) {
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 Laurentfb66dd92016-01-28 18:32:03 -08001539 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001540 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001541 } else if (isSoundTrigger) {
1542 break;
1543 }
1544 // force close input if current source is now the highest priority request on this input
1545 // and current input properties are not exactly as requested.
1546 if ((desc->mSamplingRate != samplingRate ||
1547 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001548 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001549 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
1550 source_priority(inputSource))) {
1551 ALOGV("%s: ", __FUNCTION__);
1552 AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/);
1553 for (size_t j = 0; j < sessions.size(); j++) {
1554 audio_session_t currentSession = sessions.keyAt(j);
1555 stopInput(desc->mIoHandle, currentSession);
1556 releaseInput(desc->mIoHandle, currentSession);
1557 }
1558 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001559 } else {
1560 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001561 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1562 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001563 }
Eric Laurent599c7582015-12-07 18:05:55 -08001564 }
1565 }
Eric Laurent599c7582015-12-07 18:05:55 -08001566
Eric Laurentcf2c0212014-07-25 16:20:43 -07001567 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001568 config.sample_rate = profileSamplingRate;
1569 config.channel_mask = profileChannelMask;
1570 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001571
Eric Laurent322b4d22015-04-03 15:57:54 -07001572 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001573 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001574 &config,
1575 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001576 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001577 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001578 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001579
1580 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001581 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001582 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001583 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001584 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001585 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1586 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001587 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001588 if (input != AUDIO_IO_HANDLE_NONE) {
1589 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001590 }
Eric Laurent599c7582015-12-07 18:05:55 -08001591 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001592 }
1593
Eric Laurent1f2f2232014-06-02 12:01:23 -07001594 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001595 inputDesc->mSamplingRate = profileSamplingRate;
1596 inputDesc->mFormat = profileFormat;
1597 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001598 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001599 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001600 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001601
Eric Laurent599c7582015-12-07 18:05:55 -08001602 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001603 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001604
Eric Laurent599c7582015-12-07 18:05:55 -08001605 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001606}
1607
Eric Laurentfb66dd92016-01-28 18:32:03 -08001608bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1609 const sp<AudioSession>& audioSession)
1610{
1611 // Do not allow capture if an active voice call is using a software patch and
1612 // the call TX source device is on the same HW module.
1613 // FIXME: would be better to refine to only inputs whose profile connects to the
1614 // call TX device but this information is not in the audio patch
1615 if (mCallTxPatch != 0 &&
1616 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1617 return false;
1618 }
1619
1620 // starting concurrent capture is enabled if:
1621 // 1) capturing for re-routing
1622 // 2) capturing for HOTWORD source
1623 // 3) capturing for FM TUNER source
1624 // 3) All other active captures are either for re-routing or HOTWORD
1625
1626 if (is_virtual_input_device(inputDesc->mDevice) ||
1627 audioSession->inputSource() == AUDIO_SOURCE_HOTWORD ||
1628 audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) {
1629 return true;
1630 }
1631
1632 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1633 for (size_t i = 0; i < activeInputs.size(); i++) {
1634 sp<AudioInputDescriptor> activeInput = activeInputs[i];
1635 if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) &&
1636 (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) &&
1637 !is_virtual_input_device(activeInput->mDevice)) {
1638 return false;
1639 }
1640 }
1641
1642 return true;
1643}
1644
1645
Eric Laurent4dc68062014-07-28 17:26:49 -07001646status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001647 audio_session_t session,
1648 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001649{
1650 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001651 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001652 ssize_t index = mInputs.indexOfKey(input);
1653 if (index < 0) {
1654 ALOGW("startInput() unknown input %d", input);
1655 return BAD_VALUE;
1656 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001657 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001658
Eric Laurent599c7582015-12-07 18:05:55 -08001659 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1660 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001661 ALOGW("startInput() unknown session %d on input %d", session, input);
1662 return BAD_VALUE;
1663 }
1664
Eric Laurentfb66dd92016-01-28 18:32:03 -08001665 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1666 ALOGW("startInput(%d) failed: other input already started", input);
1667 return INVALID_OPERATION;
1668 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001669
Eric Laurentfb66dd92016-01-28 18:32:03 -08001670 if (isInCall()) {
1671 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1672 }
1673 if (mInputs.activeInputsCount() != 0) {
1674 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001675 }
1676
Eric Laurent313d1e72016-01-29 09:56:57 -08001677 // increment activity count before calling getNewInputDevice() below as only active sessions
1678 // are considered for device selection
1679 audioSession->changeActiveCount(1);
1680
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001681 // Routing?
1682 mInputRoutes.incRouteActivity(session);
1683
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001684 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001685
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001686 setInputDevice(input, getNewInputDevice(inputDesc), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001687
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001688 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
1689 // if input maps to a dynamic policy with an activity listener, notify of state change
1690 if ((inputDesc->mPolicyMix != NULL)
1691 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1692 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1693 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001694 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001695
1696 if (mInputs.activeInputsCount() == 0) {
1697 SoundTrigger::setCaptureState(true);
1698 }
1699
1700 // automatically enable the remote submix output when input is started if not
1701 // used by a policy mix of type MIX_TYPE_RECORDERS
1702 // For remote submix (a virtual device), we open only one input per capture request.
1703 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1704 String8 address = String8("");
1705 if (inputDesc->mPolicyMix == NULL) {
1706 address = String8("0");
1707 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1708 address = inputDesc->mPolicyMix->mDeviceAddress;
1709 }
1710 if (address != "") {
1711 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1712 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1713 address, "remote-submix");
1714 }
Eric Laurentc722f302014-12-10 11:21:49 -08001715 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001716 }
Eric Laurente552edb2014-03-10 17:42:56 -07001717 }
1718
Eric Laurent599c7582015-12-07 18:05:55 -08001719 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001720
Eric Laurente552edb2014-03-10 17:42:56 -07001721 return NO_ERROR;
1722}
1723
Eric Laurent4dc68062014-07-28 17:26:49 -07001724status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1725 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001726{
1727 ALOGV("stopInput() input %d", input);
1728 ssize_t index = mInputs.indexOfKey(input);
1729 if (index < 0) {
1730 ALOGW("stopInput() unknown input %d", input);
1731 return BAD_VALUE;
1732 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001733 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001734
Eric Laurent599c7582015-12-07 18:05:55 -08001735 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001736 if (index < 0) {
1737 ALOGW("stopInput() unknown session %d on input %d", session, input);
1738 return BAD_VALUE;
1739 }
1740
Eric Laurent599c7582015-12-07 18:05:55 -08001741 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001742 ALOGW("stopInput() input %d already stopped", input);
1743 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001744 }
1745
Eric Laurent599c7582015-12-07 18:05:55 -08001746 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001747
1748 // Routing?
1749 mInputRoutes.decRouteActivity(session);
1750
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001751 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001752
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001753 if (inputDesc->isActive()) {
1754 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1755 } else {
1756 // if input maps to a dynamic policy with an activity listener, notify of state change
1757 if ((inputDesc->mPolicyMix != NULL)
1758 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1759 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1760 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001761 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001762
1763 // automatically disable the remote submix output when input is stopped if not
1764 // used by a policy mix of type MIX_TYPE_RECORDERS
1765 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1766 String8 address = String8("");
1767 if (inputDesc->mPolicyMix == NULL) {
1768 address = String8("0");
1769 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1770 address = inputDesc->mPolicyMix->mDeviceAddress;
1771 }
1772 if (address != "") {
1773 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1774 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1775 address, "remote-submix");
1776 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001777 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001778
Eric Laurentfb66dd92016-01-28 18:32:03 -08001779 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001780
Eric Laurentfb66dd92016-01-28 18:32:03 -08001781 if (mInputs.activeInputsCount() == 0) {
1782 SoundTrigger::setCaptureState(false);
1783 }
1784 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00001785 }
Eric Laurente552edb2014-03-10 17:42:56 -07001786 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001787 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001788}
1789
Eric Laurent4dc68062014-07-28 17:26:49 -07001790void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1791 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001792{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001793
Eric Laurente552edb2014-03-10 17:42:56 -07001794 ALOGV("releaseInput() %d", input);
1795 ssize_t index = mInputs.indexOfKey(input);
1796 if (index < 0) {
1797 ALOGW("releaseInput() releasing unknown input %d", input);
1798 return;
1799 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001800
1801 // Routing
1802 mInputRoutes.removeRoute(session);
1803
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001804 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1805 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001806
Eric Laurent599c7582015-12-07 18:05:55 -08001807 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001808 if (index < 0) {
1809 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1810 return;
1811 }
Eric Laurent599c7582015-12-07 18:05:55 -08001812
1813 if (audioSession->openCount() == 0) {
1814 ALOGW("releaseInput() invalid open count %d on session %d",
1815 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001816 return;
1817 }
Eric Laurent599c7582015-12-07 18:05:55 -08001818
1819 if (audioSession->changeOpenCount(-1) == 0) {
1820 inputDesc->removeAudioSession(session);
1821 }
1822
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001823 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001824 ALOGV("releaseInput() exit > 0");
1825 return;
1826 }
1827
Eric Laurent05b90f82014-08-27 15:32:29 -07001828 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001829 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001830 ALOGV("releaseInput() exit");
1831}
1832
Eric Laurentd4692962014-05-05 18:13:44 -07001833void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001834 bool patchRemoved = false;
1835
Eric Laurentd4692962014-05-05 18:13:44 -07001836 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001837 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001838 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001839 if (patch_index >= 0) {
1840 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1841 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1842 mAudioPatches.removeItemsAt(patch_index);
1843 patchRemoved = true;
1844 }
Eric Laurentd4692962014-05-05 18:13:44 -07001845 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1846 }
1847 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001848 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001849 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001850
1851 if (patchRemoved) {
1852 mpClientInterface->onAudioPatchListUpdate();
1853 }
Eric Laurentd4692962014-05-05 18:13:44 -07001854}
1855
Eric Laurente0720872014-03-11 09:30:41 -07001856void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001857 int indexMin,
1858 int indexMax)
1859{
1860 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001861 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001862
1863 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001864 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1865 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001866 continue;
1867 }
1868 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001869 }
Eric Laurente552edb2014-03-10 17:42:56 -07001870}
1871
Eric Laurente0720872014-03-11 09:30:41 -07001872status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001873 int index,
1874 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001875{
1876
François Gaffied1ab2bd2015-12-02 18:20:06 +01001877 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1878 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001879 return BAD_VALUE;
1880 }
1881 if (!audio_is_output_device(device)) {
1882 return BAD_VALUE;
1883 }
1884
1885 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001886 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001887
Eric Laurent1fd372e2016-04-06 14:23:53 -07001888 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001889 stream, device, index);
1890
Eric Laurent28d09f02016-03-08 10:43:05 -08001891 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001892 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1893 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001894 continue;
1895 }
1896 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1897 }
Eric Laurente552edb2014-03-10 17:42:56 -07001898
Eric Laurent1fd372e2016-04-06 14:23:53 -07001899 // update volume on all outputs and streams matching the following:
1900 // - The requested stream (or a stream matching for volume control) is active on the output
1901 // - The device (or devices) selected by the strategy corresponding to this stream includes
1902 // the requested device
1903 // - For non default requested device, currently selected device on the output is either the
1904 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001905 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1906 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001907 status_t status = NO_ERROR;
1908 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001909 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1910 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001911 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1912 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001913 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001914 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001915 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1916 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001917 continue;
1918 }
Eric Laurent794fde22016-03-11 09:50:45 -08001919 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001920 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001921 if ((curStreamDevice & device) == 0) {
1922 continue;
1923 }
1924 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001925 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001926 curStreamDevice |= device;
1927 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1928 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1929 applyDefault = true;
1930 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001931
Eric Laurent1fd372e2016-04-06 14:23:53 -07001932 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001933 status_t volStatus =
1934 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1935 if (volStatus != NO_ERROR) {
1936 status = volStatus;
1937 }
1938 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001939 }
Eric Laurente552edb2014-03-10 17:42:56 -07001940 }
1941 return status;
1942}
1943
Eric Laurente0720872014-03-11 09:30:41 -07001944status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001945 int *index,
1946 audio_devices_t device)
1947{
1948 if (index == NULL) {
1949 return BAD_VALUE;
1950 }
1951 if (!audio_is_output_device(device)) {
1952 return BAD_VALUE;
1953 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001954 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001955 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001956 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001957 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1958 }
François Gaffiedfd74092015-03-19 12:10:59 +01001959 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001960
François Gaffied1ab2bd2015-12-02 18:20:06 +01001961 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001962 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1963 return NO_ERROR;
1964}
1965
Eric Laurente0720872014-03-11 09:30:41 -07001966audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001967 const SortedVector<audio_io_handle_t>& outputs)
1968{
1969 // select one output among several suitable for global effects.
1970 // The priority is as follows:
1971 // 1: An offloaded output. If the effect ends up not being offloadable,
1972 // AudioFlinger will invalidate the track and the offloaded output
1973 // will be closed causing the effect to be moved to a PCM output.
1974 // 2: A deep buffer output
1975 // 3: the first output in the list
1976
1977 if (outputs.size() == 0) {
1978 return 0;
1979 }
1980
1981 audio_io_handle_t outputOffloaded = 0;
1982 audio_io_handle_t outputDeepBuffer = 0;
1983
1984 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001985 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001986 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001987 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1988 outputOffloaded = outputs[i];
1989 }
1990 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1991 outputDeepBuffer = outputs[i];
1992 }
1993 }
1994
1995 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1996 outputOffloaded, outputDeepBuffer);
1997 if (outputOffloaded != 0) {
1998 return outputOffloaded;
1999 }
2000 if (outputDeepBuffer != 0) {
2001 return outputDeepBuffer;
2002 }
2003
2004 return outputs[0];
2005}
2006
Eric Laurente0720872014-03-11 09:30:41 -07002007audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002008{
2009 // apply simple rule where global effects are attached to the same output as MUSIC streams
2010
Eric Laurent3b73df72014-03-11 09:06:29 -07002011 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002012 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2013 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2014
2015 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2016 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2017 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2018
2019 return output;
2020}
2021
Eric Laurente0720872014-03-11 09:30:41 -07002022status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002023 audio_io_handle_t io,
2024 uint32_t strategy,
2025 int session,
2026 int id)
2027{
2028 ssize_t index = mOutputs.indexOfKey(io);
2029 if (index < 0) {
2030 index = mInputs.indexOfKey(io);
2031 if (index < 0) {
2032 ALOGW("registerEffect() unknown io %d", io);
2033 return INVALID_OPERATION;
2034 }
2035 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002036 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002037}
2038
Eric Laurentc75307b2015-03-17 15:29:32 -07002039bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2040{
Eric Laurent28d09f02016-03-08 10:43:05 -08002041 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002042 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2043 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002044 continue;
2045 }
2046 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2047 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002048 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002049}
2050
2051bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2052{
2053 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2054}
2055
Eric Laurente0720872014-03-11 09:30:41 -07002056bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002057{
2058 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002059 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002060 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002061 return true;
2062 }
2063 }
2064 return false;
2065}
2066
Eric Laurent275e8e92014-11-30 15:14:47 -08002067// Register a list of custom mixes with their attributes and format.
2068// When a mix is registered, corresponding input and output profiles are
2069// added to the remote submix hw module. The profile contains only the
2070// parameters (sampling rate, format...) specified by the mix.
2071// The corresponding input remote submix device is also connected.
2072//
2073// When a remote submix device is connected, the address is checked to select the
2074// appropriate profile and the corresponding input or output stream is opened.
2075//
2076// When capture starts, getInputForAttr() will:
2077// - 1 look for a mix matching the address passed in attribtutes tags if any
2078// - 2 if none found, getDeviceForInputSource() will:
2079// - 2.1 look for a mix matching the attributes source
2080// - 2.2 if none found, default to device selection by policy rules
2081// At this time, the corresponding output remote submix device is also connected
2082// and active playback use cases can be transferred to this mix if needed when reconnecting
2083// after AudioTracks are invalidated
2084//
2085// When playback starts, getOutputForAttr() will:
2086// - 1 look for a mix matching the address passed in attribtutes tags if any
2087// - 2 if none found, look for a mix matching the attributes usage
2088// - 3 if none found, default to device and output selection by policy rules.
2089
2090status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2091{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002092 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2093 status_t res = NO_ERROR;
2094
2095 sp<HwModule> rSubmixModule;
2096 // examine each mix's route type
2097 for (size_t i = 0; i < mixes.size(); i++) {
2098 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2099 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2100 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002101 break;
2102 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002103 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2104 // Loop back through "remote submix"
2105 if (rSubmixModule == 0) {
2106 for (size_t j = 0; i < mHwModules.size(); j++) {
2107 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2108 && mHwModules[j]->mHandle != 0) {
2109 rSubmixModule = mHwModules[j];
2110 break;
2111 }
2112 }
2113 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002114
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002115 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002116
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002117 if (rSubmixModule == 0) {
2118 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2119 res = INVALID_OPERATION;
2120 break;
2121 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002122
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002123 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002124
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002125 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002126 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002127 res = INVALID_OPERATION;
2128 break;
2129 }
2130 audio_config_t outputConfig = mixes[i].mFormat;
2131 audio_config_t inputConfig = mixes[i].mFormat;
2132 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2133 // stereo and let audio flinger do the channel conversion if needed.
2134 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2135 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2136 rSubmixModule->addOutputProfile(address, &outputConfig,
2137 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2138 rSubmixModule->addInputProfile(address, &inputConfig,
2139 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002140
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002141 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2142 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2143 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2144 address.string(), "remote-submix");
2145 } else {
2146 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2147 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2148 address.string(), "remote-submix");
2149 }
2150 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002151 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002152 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002153 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2154 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002155
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002156 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002157 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2158 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2159 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2160 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2161 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2162 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002163 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2164 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002165 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2166 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002167 } else {
2168 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002169 }
2170 break;
2171 }
2172 }
2173
2174 if (res != NO_ERROR) {
2175 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002176 i, device, address.string());
2177 res = INVALID_OPERATION;
2178 break;
2179 } else if (!foundOutput) {
2180 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2181 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002182 res = INVALID_OPERATION;
2183 break;
2184 }
Eric Laurentc722f302014-12-10 11:21:49 -08002185 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002186 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002187 if (res != NO_ERROR) {
2188 unregisterPolicyMixes(mixes);
2189 }
2190 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002191}
2192
2193status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2194{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002195 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002196 status_t res = NO_ERROR;
2197 sp<HwModule> rSubmixModule;
2198 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002199 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002200 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002201
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002202 if (rSubmixModule == 0) {
2203 for (size_t j = 0; i < mHwModules.size(); j++) {
2204 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2205 && mHwModules[j]->mHandle != 0) {
2206 rSubmixModule = mHwModules[j];
2207 break;
2208 }
2209 }
2210 }
2211 if (rSubmixModule == 0) {
2212 res = INVALID_OPERATION;
2213 continue;
2214 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002215
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002216 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002217
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002218 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2219 res = INVALID_OPERATION;
2220 continue;
2221 }
2222
2223 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2224 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2225 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2226 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2227 address.string(), "remote-submix");
2228 }
2229 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2230 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2231 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2232 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2233 address.string(), "remote-submix");
2234 }
2235 rSubmixModule->removeOutputProfile(address);
2236 rSubmixModule->removeInputProfile(address);
2237
2238 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2239 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2240 res = INVALID_OPERATION;
2241 continue;
2242 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002243 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002244 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002245 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002246}
2247
Eric Laurente552edb2014-03-10 17:42:56 -07002248
Eric Laurente0720872014-03-11 09:30:41 -07002249status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002250{
2251 const size_t SIZE = 256;
2252 char buffer[SIZE];
2253 String8 result;
2254
2255 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2256 result.append(buffer);
2257
Eric Laurent87ffa392015-05-22 10:32:38 -07002258 snprintf(buffer, SIZE, " Primary Output: %d\n",
2259 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002260 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002261 std::string stateLiteral;
2262 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2263 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002264 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002265 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002266 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002267 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002268 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002269 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002270 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002271 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002272 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002273 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002274 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002275 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002276 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002277 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002278 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002279 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2280 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2281 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002282 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2283 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002284 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2285 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002286
François Gaffie2110e042015-03-24 08:41:51 +01002287 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002288
François Gaffie112b0af2015-11-19 16:13:25 +01002289 mAvailableOutputDevices.dump(fd, String8("Available output"));
2290 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002291 mHwModules.dump(fd);
2292 mOutputs.dump(fd);
2293 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002294 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002295 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002296 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002297
2298 return NO_ERROR;
2299}
2300
2301// This function checks for the parameters which can be offloaded.
2302// This can be enhanced depending on the capability of the DSP and policy
2303// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002304bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002305{
2306 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002307 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002308 offloadInfo.sample_rate, offloadInfo.channel_mask,
2309 offloadInfo.format,
2310 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2311 offloadInfo.has_video);
2312
Andy Hung2ddee192015-12-18 17:34:44 -08002313 if (mMasterMono) {
2314 return false; // no offloading if mono is set.
2315 }
2316
Eric Laurente552edb2014-03-10 17:42:56 -07002317 // Check if offload has been disabled
2318 char propValue[PROPERTY_VALUE_MAX];
2319 if (property_get("audio.offload.disable", propValue, "0")) {
2320 if (atoi(propValue) != 0) {
2321 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2322 return false;
2323 }
2324 }
2325
2326 // Check if stream type is music, then only allow offload as of now.
2327 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2328 {
2329 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2330 return false;
2331 }
2332
2333 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002334 const bool allowOffloadWithVideo =
2335 property_get_bool("audio.offload.video", false /* default_value */);
2336 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002337 ALOGV("isOffloadSupported: has_video == true, returning false");
2338 return false;
2339 }
2340
2341 //If duration is less than minimum value defined in property, return false
2342 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2343 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2344 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2345 return false;
2346 }
2347 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2348 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2349 return false;
2350 }
2351
2352 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2353 // creating an offloaded track and tearing it down immediately after start when audioflinger
2354 // detects there is an active non offloadable effect.
2355 // FIXME: We should check the audio session here but we do not have it in this context.
2356 // This may prevent offloading in rare situations where effects are left active by apps
2357 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002358 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002359 return false;
2360 }
2361
2362 // See if there is a profile to support this.
2363 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002364 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002365 offloadInfo.sample_rate,
2366 offloadInfo.format,
2367 offloadInfo.channel_mask,
2368 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002369 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2370 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002371}
2372
Eric Laurent6a94d692014-05-20 11:18:06 -07002373status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2374 audio_port_type_t type,
2375 unsigned int *num_ports,
2376 struct audio_port *ports,
2377 unsigned int *generation)
2378{
2379 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2380 generation == NULL) {
2381 return BAD_VALUE;
2382 }
2383 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2384 if (ports == NULL) {
2385 *num_ports = 0;
2386 }
2387
2388 size_t portsWritten = 0;
2389 size_t portsMax = *num_ports;
2390 *num_ports = 0;
2391 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002392 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2393 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002394 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002395 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2396 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2397 continue;
2398 }
2399 if (portsWritten < portsMax) {
2400 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2401 }
2402 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002403 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002404 }
2405 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002406 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2407 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2408 continue;
2409 }
2410 if (portsWritten < portsMax) {
2411 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2412 }
2413 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002414 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002415 }
2416 }
2417 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2418 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2419 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2420 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2421 }
2422 *num_ports += mInputs.size();
2423 }
2424 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002425 size_t numOutputs = 0;
2426 for (size_t i = 0; i < mOutputs.size(); i++) {
2427 if (!mOutputs[i]->isDuplicated()) {
2428 numOutputs++;
2429 if (portsWritten < portsMax) {
2430 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2431 }
2432 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002433 }
Eric Laurent84c70242014-06-23 08:46:27 -07002434 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002435 }
2436 }
2437 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002438 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002439 return NO_ERROR;
2440}
2441
2442status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2443{
2444 return NO_ERROR;
2445}
2446
Eric Laurent6a94d692014-05-20 11:18:06 -07002447status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2448 audio_patch_handle_t *handle,
2449 uid_t uid)
2450{
2451 ALOGV("createAudioPatch()");
2452
2453 if (handle == NULL || patch == NULL) {
2454 return BAD_VALUE;
2455 }
2456 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2457
Eric Laurent874c42872014-08-08 15:13:39 -07002458 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2459 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2460 return BAD_VALUE;
2461 }
2462 // only one source per audio patch supported for now
2463 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002464 return INVALID_OPERATION;
2465 }
Eric Laurent874c42872014-08-08 15:13:39 -07002466
2467 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002468 return INVALID_OPERATION;
2469 }
Eric Laurent874c42872014-08-08 15:13:39 -07002470 for (size_t i = 0; i < patch->num_sinks; i++) {
2471 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2472 return INVALID_OPERATION;
2473 }
2474 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002475
2476 sp<AudioPatch> patchDesc;
2477 ssize_t index = mAudioPatches.indexOfKey(*handle);
2478
Eric Laurent6a94d692014-05-20 11:18:06 -07002479 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2480 patch->sources[0].role,
2481 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002482#if LOG_NDEBUG == 0
2483 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002484 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002485 patch->sinks[i].role,
2486 patch->sinks[i].type);
2487 }
2488#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002489
2490 if (index >= 0) {
2491 patchDesc = mAudioPatches.valueAt(index);
2492 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2493 mUidCached, patchDesc->mUid, uid);
2494 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2495 return INVALID_OPERATION;
2496 }
2497 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002498 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002499 }
2500
2501 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002502 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002503 if (outputDesc == NULL) {
2504 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2505 return BAD_VALUE;
2506 }
Eric Laurent84c70242014-06-23 08:46:27 -07002507 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2508 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002509 if (patchDesc != 0) {
2510 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2511 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2512 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2513 return BAD_VALUE;
2514 }
2515 }
Eric Laurent874c42872014-08-08 15:13:39 -07002516 DeviceVector devices;
2517 for (size_t i = 0; i < patch->num_sinks; i++) {
2518 // Only support mix to devices connection
2519 // TODO add support for mix to mix connection
2520 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2521 ALOGV("createAudioPatch() source mix but sink is not a device");
2522 return INVALID_OPERATION;
2523 }
2524 sp<DeviceDescriptor> devDesc =
2525 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2526 if (devDesc == 0) {
2527 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2528 return BAD_VALUE;
2529 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002530
François Gaffie53615e22015-03-19 09:24:12 +01002531 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002532 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002533 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002534 NULL, // updatedSamplingRate
2535 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002536 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002537 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002538 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002539 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002540 ALOGV("createAudioPatch() profile not supported for device %08x",
2541 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002542 return INVALID_OPERATION;
2543 }
2544 devices.add(devDesc);
2545 }
2546 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002547 return INVALID_OPERATION;
2548 }
Eric Laurent874c42872014-08-08 15:13:39 -07002549
Eric Laurent6a94d692014-05-20 11:18:06 -07002550 // TODO: reconfigure output format and channels here
2551 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002552 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002553 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002554 index = mAudioPatches.indexOfKey(*handle);
2555 if (index >= 0) {
2556 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2557 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2558 }
2559 patchDesc = mAudioPatches.valueAt(index);
2560 patchDesc->mUid = uid;
2561 ALOGV("createAudioPatch() success");
2562 } else {
2563 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2564 return INVALID_OPERATION;
2565 }
2566 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2567 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2568 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002569 // only one sink supported when connecting an input device to a mix
2570 if (patch->num_sinks > 1) {
2571 return INVALID_OPERATION;
2572 }
François Gaffie53615e22015-03-19 09:24:12 +01002573 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002574 if (inputDesc == NULL) {
2575 return BAD_VALUE;
2576 }
2577 if (patchDesc != 0) {
2578 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2579 return BAD_VALUE;
2580 }
2581 }
2582 sp<DeviceDescriptor> devDesc =
2583 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2584 if (devDesc == 0) {
2585 return BAD_VALUE;
2586 }
2587
François Gaffie53615e22015-03-19 09:24:12 +01002588 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002589 devDesc->mAddress,
2590 patch->sinks[0].sample_rate,
2591 NULL, /*updatedSampleRate*/
2592 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002593 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002594 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002595 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002596 // FIXME for the parameter type,
2597 // and the NONE
2598 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002599 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002600 return INVALID_OPERATION;
2601 }
2602 // TODO: reconfigure output format and channels here
2603 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002604 devDesc->type(), inputDesc->mIoHandle);
2605 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002606 index = mAudioPatches.indexOfKey(*handle);
2607 if (index >= 0) {
2608 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2609 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2610 }
2611 patchDesc = mAudioPatches.valueAt(index);
2612 patchDesc->mUid = uid;
2613 ALOGV("createAudioPatch() success");
2614 } else {
2615 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2616 return INVALID_OPERATION;
2617 }
2618 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2619 // device to device connection
2620 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002621 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002622 return BAD_VALUE;
2623 }
2624 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002625 sp<DeviceDescriptor> srcDeviceDesc =
2626 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002627 if (srcDeviceDesc == 0) {
2628 return BAD_VALUE;
2629 }
Eric Laurent874c42872014-08-08 15:13:39 -07002630
Eric Laurent6a94d692014-05-20 11:18:06 -07002631 //update source and sink with our own data as the data passed in the patch may
2632 // be incomplete.
2633 struct audio_patch newPatch = *patch;
2634 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002635
Eric Laurent874c42872014-08-08 15:13:39 -07002636 for (size_t i = 0; i < patch->num_sinks; i++) {
2637 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2638 ALOGV("createAudioPatch() source device but one sink is not a device");
2639 return INVALID_OPERATION;
2640 }
2641
2642 sp<DeviceDescriptor> sinkDeviceDesc =
2643 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2644 if (sinkDeviceDesc == 0) {
2645 return BAD_VALUE;
2646 }
2647 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2648
Eric Laurent3bcf8592015-04-03 12:13:24 -07002649 // create a software bridge in PatchPanel if:
2650 // - source and sink devices are on differnt HW modules OR
2651 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002652 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002653 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002654 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002655 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002656 return INVALID_OPERATION;
2657 }
Eric Laurent874c42872014-08-08 15:13:39 -07002658 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002659 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002660 // if the sink device is reachable via an opened output stream, request to go via
2661 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002662 audio_io_handle_t output = selectOutput(outputs,
2663 AUDIO_OUTPUT_FLAG_NONE,
2664 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002665 if (output != AUDIO_IO_HANDLE_NONE) {
2666 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2667 if (outputDesc->isDuplicated()) {
2668 return INVALID_OPERATION;
2669 }
2670 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002671 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002672 newPatch.num_sources = 2;
2673 }
Eric Laurent83b88082014-06-20 18:31:16 -07002674 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002675 }
2676 // TODO: check from routing capabilities in config file and other conflicting patches
2677
2678 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2679 if (index >= 0) {
2680 afPatchHandle = patchDesc->mAfPatchHandle;
2681 }
2682
2683 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2684 &afPatchHandle,
2685 0);
2686 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2687 status, afPatchHandle);
2688 if (status == NO_ERROR) {
2689 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002690 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002691 addAudioPatch(patchDesc->mHandle, patchDesc);
2692 } else {
2693 patchDesc->mPatch = newPatch;
2694 }
2695 patchDesc->mAfPatchHandle = afPatchHandle;
2696 *handle = patchDesc->mHandle;
2697 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002698 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002699 } else {
2700 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2701 status);
2702 return INVALID_OPERATION;
2703 }
2704 } else {
2705 return BAD_VALUE;
2706 }
2707 } else {
2708 return BAD_VALUE;
2709 }
2710 return NO_ERROR;
2711}
2712
2713status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2714 uid_t uid)
2715{
2716 ALOGV("releaseAudioPatch() patch %d", handle);
2717
2718 ssize_t index = mAudioPatches.indexOfKey(handle);
2719
2720 if (index < 0) {
2721 return BAD_VALUE;
2722 }
2723 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2724 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2725 mUidCached, patchDesc->mUid, uid);
2726 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2727 return INVALID_OPERATION;
2728 }
2729
2730 struct audio_patch *patch = &patchDesc->mPatch;
2731 patchDesc->mUid = mUidCached;
2732 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002733 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002734 if (outputDesc == NULL) {
2735 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2736 return BAD_VALUE;
2737 }
2738
Eric Laurentc75307b2015-03-17 15:29:32 -07002739 setOutputDevice(outputDesc,
2740 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002741 true,
2742 0,
2743 NULL);
2744 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2745 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002746 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002747 if (inputDesc == NULL) {
2748 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2749 return BAD_VALUE;
2750 }
2751 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002752 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002753 true,
2754 NULL);
2755 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2756 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2757 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2758 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2759 status, patchDesc->mAfPatchHandle);
2760 removeAudioPatch(patchDesc->mHandle);
2761 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002762 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002763 } else {
2764 return BAD_VALUE;
2765 }
2766 } else {
2767 return BAD_VALUE;
2768 }
2769 return NO_ERROR;
2770}
2771
2772status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2773 struct audio_patch *patches,
2774 unsigned int *generation)
2775{
François Gaffie53615e22015-03-19 09:24:12 +01002776 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002777 return BAD_VALUE;
2778 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002779 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002780 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002781}
2782
Eric Laurente1715a42014-05-20 11:30:42 -07002783status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002784{
Eric Laurente1715a42014-05-20 11:30:42 -07002785 ALOGV("setAudioPortConfig()");
2786
2787 if (config == NULL) {
2788 return BAD_VALUE;
2789 }
2790 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2791 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002792 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2793 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002794 }
2795
Eric Laurenta121f902014-06-03 13:32:54 -07002796 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002797 if (config->type == AUDIO_PORT_TYPE_MIX) {
2798 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002799 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002800 if (outputDesc == NULL) {
2801 return BAD_VALUE;
2802 }
Eric Laurent84c70242014-06-23 08:46:27 -07002803 ALOG_ASSERT(!outputDesc->isDuplicated(),
2804 "setAudioPortConfig() called on duplicated output %d",
2805 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002806 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002807 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002808 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002809 if (inputDesc == NULL) {
2810 return BAD_VALUE;
2811 }
Eric Laurenta121f902014-06-03 13:32:54 -07002812 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002813 } else {
2814 return BAD_VALUE;
2815 }
2816 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2817 sp<DeviceDescriptor> deviceDesc;
2818 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2819 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2820 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2821 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2822 } else {
2823 return BAD_VALUE;
2824 }
2825 if (deviceDesc == NULL) {
2826 return BAD_VALUE;
2827 }
Eric Laurenta121f902014-06-03 13:32:54 -07002828 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002829 } else {
2830 return BAD_VALUE;
2831 }
2832
Eric Laurenta121f902014-06-03 13:32:54 -07002833 struct audio_port_config backupConfig;
2834 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2835 if (status == NO_ERROR) {
2836 struct audio_port_config newConfig;
2837 audioPortConfig->toAudioPortConfig(&newConfig, config);
2838 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002839 }
Eric Laurenta121f902014-06-03 13:32:54 -07002840 if (status != NO_ERROR) {
2841 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002842 }
Eric Laurente1715a42014-05-20 11:30:42 -07002843
2844 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002845}
2846
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002847void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2848{
Eric Laurentd60560a2015-04-10 11:31:20 -07002849 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002850 clearAudioPatches(uid);
2851 clearSessionRoutes(uid);
2852}
2853
Eric Laurent6a94d692014-05-20 11:18:06 -07002854void AudioPolicyManager::clearAudioPatches(uid_t uid)
2855{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002856 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002857 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2858 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002859 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002860 }
2861 }
2862}
2863
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002864void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2865 audio_io_handle_t ouptutToSkip)
2866{
2867 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2868 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2869 for (size_t j = 0; j < mOutputs.size(); j++) {
2870 if (mOutputs.keyAt(j) == ouptutToSkip) {
2871 continue;
2872 }
2873 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2874 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2875 continue;
2876 }
2877 // If the default device for this strategy is on another output mix,
2878 // invalidate all tracks in this strategy to force re connection.
2879 // Otherwise select new device on the output mix.
2880 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002881 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002882 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2883 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2884 }
2885 }
2886 } else {
2887 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2888 setOutputDevice(outputDesc, newDevice, false);
2889 }
2890 }
2891}
2892
2893void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2894{
2895 // remove output routes associated with this uid
2896 SortedVector<routing_strategy> affectedStrategies;
2897 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2898 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2899 if (route->mUid == uid) {
2900 mOutputRoutes.removeItemsAt(i);
2901 if (route->mDeviceDescriptor != 0) {
2902 affectedStrategies.add(getStrategy(route->mStreamType));
2903 }
2904 }
2905 }
2906 // reroute outputs if necessary
2907 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2908 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2909 }
2910
2911 // remove input routes associated with this uid
2912 SortedVector<audio_source_t> affectedSources;
2913 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2914 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2915 if (route->mUid == uid) {
2916 mInputRoutes.removeItemsAt(i);
2917 if (route->mDeviceDescriptor != 0) {
2918 affectedSources.add(route->mSource);
2919 }
2920 }
2921 }
2922 // reroute inputs if necessary
2923 SortedVector<audio_io_handle_t> inputsToClose;
2924 for (size_t i = 0; i < mInputs.size(); i++) {
2925 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002926 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002927 inputsToClose.add(inputDesc->mIoHandle);
2928 }
2929 }
2930 for (size_t i = 0; i < inputsToClose.size(); i++) {
2931 closeInput(inputsToClose[i]);
2932 }
2933}
2934
Eric Laurentd60560a2015-04-10 11:31:20 -07002935void AudioPolicyManager::clearAudioSources(uid_t uid)
2936{
2937 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2938 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2939 if (sourceDesc->mUid == uid) {
2940 stopAudioSource(mAudioSources.keyAt(i));
2941 }
2942 }
2943}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002944
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002945status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2946 audio_io_handle_t *ioHandle,
2947 audio_devices_t *device)
2948{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08002949 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2950 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002951 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002952
François Gaffiedf372692015-03-19 10:43:27 +01002953 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002954}
2955
Eric Laurentd60560a2015-04-10 11:31:20 -07002956status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2957 const audio_attributes_t *attributes,
2958 audio_io_handle_t *handle,
2959 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002960{
Eric Laurentd60560a2015-04-10 11:31:20 -07002961 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2962 if (source == NULL || attributes == NULL || handle == NULL) {
2963 return BAD_VALUE;
2964 }
2965
2966 *handle = AUDIO_IO_HANDLE_NONE;
2967
2968 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2969 source->type != AUDIO_PORT_TYPE_DEVICE) {
2970 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2971 return INVALID_OPERATION;
2972 }
2973
2974 sp<DeviceDescriptor> srcDeviceDesc =
2975 mAvailableInputDevices.getDevice(source->ext.device.type,
2976 String8(source->ext.device.address));
2977 if (srcDeviceDesc == 0) {
2978 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2979 return BAD_VALUE;
2980 }
2981 sp<AudioSourceDescriptor> sourceDesc =
2982 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2983
2984 struct audio_patch dummyPatch;
2985 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2986 sourceDesc->mPatchDesc = patchDesc;
2987
2988 status_t status = connectAudioSource(sourceDesc);
2989 if (status == NO_ERROR) {
2990 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2991 *handle = sourceDesc->getHandle();
2992 }
2993 return status;
2994}
2995
2996status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2997{
2998 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2999
3000 // make sure we only have one patch per source.
3001 disconnectAudioSource(sourceDesc);
3002
3003 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003004 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003005 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3006
3007 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3008 sp<DeviceDescriptor> sinkDeviceDesc =
3009 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3010
3011 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3012 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3013
3014 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3015 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003016 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003017 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3018 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3019 // create patch between src device and output device
3020 // create Hwoutput and add to mHwOutputs
3021 } else {
3022 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3023 audio_io_handle_t output =
3024 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3025 if (output == AUDIO_IO_HANDLE_NONE) {
3026 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3027 return INVALID_OPERATION;
3028 }
3029 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3030 if (outputDesc->isDuplicated()) {
3031 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3032 return INVALID_OPERATION;
3033 }
3034 // create a special patch with no sink and two sources:
3035 // - the second source indicates to PatchPanel through which output mix this patch should
3036 // be connected as well as the stream type for volume control
3037 // - the sink is defined by whatever output device is currently selected for the output
3038 // though which this patch is routed.
3039 patch->num_sinks = 0;
3040 patch->num_sources = 2;
3041 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3042 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3043 patch->sources[1].ext.mix.usecase.stream = stream;
3044 status_t status = mpClientInterface->createAudioPatch(patch,
3045 &afPatchHandle,
3046 0);
3047 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3048 status, afPatchHandle);
3049 if (status != NO_ERROR) {
3050 ALOGW("%s patch panel could not connect device patch, error %d",
3051 __FUNCTION__, status);
3052 return INVALID_OPERATION;
3053 }
3054 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003055 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003056
3057 if (status != NO_ERROR) {
3058 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3059 return status;
3060 }
3061 sourceDesc->mSwOutput = outputDesc;
3062 if (delayMs != 0) {
3063 usleep(delayMs * 1000);
3064 }
3065 }
3066
3067 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3068 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3069
3070 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003071}
3072
Eric Laurent493404d2015-04-21 15:07:36 -07003073status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003074{
Eric Laurentd60560a2015-04-10 11:31:20 -07003075 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3076 ALOGV("%s handle %d", __FUNCTION__, handle);
3077 if (sourceDesc == 0) {
3078 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3079 return BAD_VALUE;
3080 }
3081 status_t status = disconnectAudioSource(sourceDesc);
3082
3083 mAudioSources.removeItem(handle);
3084 return status;
3085}
3086
Andy Hung2ddee192015-12-18 17:34:44 -08003087status_t AudioPolicyManager::setMasterMono(bool mono)
3088{
3089 if (mMasterMono == mono) {
3090 return NO_ERROR;
3091 }
3092 mMasterMono = mono;
3093 // if enabling mono we close all offloaded devices, which will invalidate the
3094 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3095 // for recreating the new AudioTrack as non-offloaded PCM.
3096 //
3097 // If disabling mono, we leave all tracks as is: we don't know which clients
3098 // and tracks are able to be recreated as offloaded. The next "song" should
3099 // play back offloaded.
3100 if (mMasterMono) {
3101 Vector<audio_io_handle_t> offloaded;
3102 for (size_t i = 0; i < mOutputs.size(); ++i) {
3103 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3104 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3105 offloaded.push(desc->mIoHandle);
3106 }
3107 }
3108 for (size_t i = 0; i < offloaded.size(); ++i) {
3109 closeOutput(offloaded[i]);
3110 }
3111 }
3112 // update master mono for all remaining outputs
3113 for (size_t i = 0; i < mOutputs.size(); ++i) {
3114 updateMono(mOutputs.keyAt(i));
3115 }
3116 return NO_ERROR;
3117}
3118
3119status_t AudioPolicyManager::getMasterMono(bool *mono)
3120{
3121 *mono = mMasterMono;
3122 return NO_ERROR;
3123}
3124
Eric Laurentd60560a2015-04-10 11:31:20 -07003125status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3126{
3127 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3128
3129 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3130 if (patchDesc == 0) {
3131 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3132 sourceDesc->mPatchDesc->mHandle);
3133 return BAD_VALUE;
3134 }
3135 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3136
Eric Laurent28d09f02016-03-08 10:43:05 -08003137 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003138 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3139 if (swOutputDesc != 0) {
3140 stopSource(swOutputDesc, stream, false);
3141 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3142 } else {
3143 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3144 if (hwOutputDesc != 0) {
3145 // release patch between src device and output device
3146 // close Hwoutput and remove from mHwOutputs
3147 } else {
3148 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3149 }
3150 }
3151 return NO_ERROR;
3152}
3153
3154sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3155 audio_io_handle_t output, routing_strategy strategy)
3156{
3157 sp<AudioSourceDescriptor> source;
3158 for (size_t i = 0; i < mAudioSources.size(); i++) {
3159 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3160 routing_strategy sourceStrategy =
3161 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3162 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3163 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3164 source = sourceDesc;
3165 break;
3166 }
3167 }
3168 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003169}
3170
Eric Laurente552edb2014-03-10 17:42:56 -07003171// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003172// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003173// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003174uint32_t AudioPolicyManager::nextAudioPortGeneration()
3175{
3176 return android_atomic_inc(&mAudioPortGeneration);
3177}
3178
Eric Laurente0720872014-03-11 09:30:41 -07003179AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003180 :
3181#ifdef AUDIO_POLICY_TEST
3182 Thread(false),
3183#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003184 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003185 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003186 mAudioPortGeneration(1),
3187 mBeaconMuteRefCount(0),
3188 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003189 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003190 mTtsOutputAvailable(false),
3191 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003192{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003193 mUidCached = getuid();
3194 mpClientInterface = clientInterface;
3195
3196 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3197 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3198 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3199 bool speakerDrcEnabled = false;
3200
3201#ifdef USE_XML_AUDIO_POLICY_CONF
3202 mVolumeCurves = new VolumeCurvesCollection();
3203 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3204 mDefaultOutputDevice, speakerDrcEnabled,
3205 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3206 PolicySerializer serializer;
3207 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3208#else
3209 mVolumeCurves = new StreamDescriptorCollection();
3210 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3211 mDefaultOutputDevice, speakerDrcEnabled);
3212 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3213 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3214#endif
3215 ALOGE("could not load audio policy configuration file, setting defaults");
3216 config.setDefault();
3217 }
3218 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3219 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3220
3221 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003222 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3223 if (!engineInstance) {
3224 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3225 return;
3226 }
3227 // Retrieve the Policy Manager Interface
3228 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3229 if (mEngine == NULL) {
3230 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3231 return;
3232 }
3233 mEngine->setObserver(this);
3234 status_t status = mEngine->initCheck();
3235 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3236
Eric Laurent3a4311c2014-03-17 12:00:47 -07003237 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003238 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003239 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3240 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003241 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003242 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003243 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003244 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003245 continue;
3246 }
3247 // open all output streams needed to access attached devices
3248 // except for direct output streams that are only opened when they are actually
3249 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003250 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003251 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3252 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003253 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003254
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003255 if (!outProfile->hasSupportedDevices()) {
3256 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003257 continue;
3258 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003259 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003260 mTtsOutputAvailable = true;
3261 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003262
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003263 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003264 continue;
3265 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003266 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003267 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3268 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003269 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003270 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003271 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003272 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003273 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003274 if ((profileType & outputDeviceTypes) == 0) {
3275 continue;
3276 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003277 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3278 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003279 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3280 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3281 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3282 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003283
3284 outputDesc->mDevice = profileType;
3285 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3286 config.sample_rate = outputDesc->mSamplingRate;
3287 config.channel_mask = outputDesc->mChannelMask;
3288 config.format = outputDesc->mFormat;
3289 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003290 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003291 &output,
3292 &config,
3293 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003294 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003295 &outputDesc->mLatency,
3296 outputDesc->mFlags);
3297
3298 if (status != NO_ERROR) {
3299 ALOGW("Cannot open output stream for device %08x on hw module %s",
3300 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003301 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003302 } else {
3303 outputDesc->mSamplingRate = config.sample_rate;
3304 outputDesc->mChannelMask = config.channel_mask;
3305 outputDesc->mFormat = config.format;
3306
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003307 for (size_t k = 0; k < supportedDevices.size(); k++) {
3308 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003309 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003310 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3311 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003312 }
3313 }
3314 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003315 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003316 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003317 }
3318 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003319 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003320 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003321 true,
3322 0,
3323 NULL,
3324 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003325 }
Eric Laurente552edb2014-03-10 17:42:56 -07003326 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003327 // open input streams needed to access attached devices to validate
3328 // mAvailableInputDevices list
3329 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3330 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003331 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003332
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003333 if (!inProfile->hasSupportedDevices()) {
3334 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003335 continue;
3336 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003337 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003338 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003339 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3340
Eric Laurentd78f1532014-09-16 16:38:20 -07003341 if ((profileType & inputDeviceTypes) == 0) {
3342 continue;
3343 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003344 sp<AudioInputDescriptor> inputDesc =
3345 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003346
Eric Laurentd78f1532014-09-16 16:38:20 -07003347 inputDesc->mDevice = profileType;
3348
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003349 // find the address
3350 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3351 // the inputs vector must be of size 1, but we don't want to crash here
3352 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3353 : String8("");
3354 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3355 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3356
Eric Laurentd78f1532014-09-16 16:38:20 -07003357 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3358 config.sample_rate = inputDesc->mSamplingRate;
3359 config.channel_mask = inputDesc->mChannelMask;
3360 config.format = inputDesc->mFormat;
3361 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003362 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003363 &input,
3364 &config,
3365 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003366 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003367 AUDIO_SOURCE_MIC,
3368 AUDIO_INPUT_FLAG_NONE);
3369
3370 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003371 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3372 for (size_t k = 0; k < supportedDevices.size(); k++) {
3373 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003374 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003375 if (index >= 0) {
3376 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3377 if (!devDesc->isAttached()) {
3378 devDesc->attach(mHwModules[i]);
3379 devDesc->importAudioPort(inProfile);
3380 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003381 }
3382 }
3383 mpClientInterface->closeInput(input);
3384 } else {
3385 ALOGW("Cannot open input stream for device %08x on hw module %s",
3386 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003387 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003388 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003389 }
3390 }
3391 // make sure all attached devices have been allocated a unique ID
3392 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003393 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003394 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003395 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3396 continue;
3397 }
François Gaffie2110e042015-03-24 08:41:51 +01003398 // The device is now validated and can be appended to the available devices of the engine
3399 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3400 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003401 i++;
3402 }
3403 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003404 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003405 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003406 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3407 continue;
3408 }
François Gaffie2110e042015-03-24 08:41:51 +01003409 // The device is now validated and can be appended to the available devices of the engine
3410 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3411 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003412 i++;
3413 }
3414 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003415 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003416 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003417 }
Eric Laurente552edb2014-03-10 17:42:56 -07003418
3419 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3420
3421 updateDevicesAndOutputs();
3422
3423#ifdef AUDIO_POLICY_TEST
3424 if (mPrimaryOutput != 0) {
3425 AudioParameter outputCmd = AudioParameter();
3426 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003427 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003428
3429 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3430 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003431 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3432 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003433 mTestLatencyMs = 0;
3434 mCurOutput = 0;
3435 mDirectOutput = false;
3436 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3437 mTestOutputs[i] = 0;
3438 }
3439
3440 const size_t SIZE = 256;
3441 char buffer[SIZE];
3442 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3443 run(buffer, ANDROID_PRIORITY_AUDIO);
3444 }
3445#endif //AUDIO_POLICY_TEST
3446}
3447
Eric Laurente0720872014-03-11 09:30:41 -07003448AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003449{
3450#ifdef AUDIO_POLICY_TEST
3451 exit();
3452#endif //AUDIO_POLICY_TEST
3453 for (size_t i = 0; i < mOutputs.size(); i++) {
3454 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003455 }
3456 for (size_t i = 0; i < mInputs.size(); i++) {
3457 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003458 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003459 mAvailableOutputDevices.clear();
3460 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003461 mOutputs.clear();
3462 mInputs.clear();
3463 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003464}
3465
Eric Laurente0720872014-03-11 09:30:41 -07003466status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003467{
Eric Laurent87ffa392015-05-22 10:32:38 -07003468 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003469}
3470
3471#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003472bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003473{
3474 ALOGV("entering threadLoop()");
3475 while (!exitPending())
3476 {
3477 String8 command;
3478 int valueInt;
3479 String8 value;
3480
3481 Mutex::Autolock _l(mLock);
3482 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3483
3484 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3485 AudioParameter param = AudioParameter(command);
3486
3487 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3488 valueInt != 0) {
3489 ALOGV("Test command %s received", command.string());
3490 String8 target;
3491 if (param.get(String8("target"), target) != NO_ERROR) {
3492 target = "Manager";
3493 }
3494 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3495 param.remove(String8("test_cmd_policy_output"));
3496 mCurOutput = valueInt;
3497 }
3498 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3499 param.remove(String8("test_cmd_policy_direct"));
3500 if (value == "false") {
3501 mDirectOutput = false;
3502 } else if (value == "true") {
3503 mDirectOutput = true;
3504 }
3505 }
3506 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3507 param.remove(String8("test_cmd_policy_input"));
3508 mTestInput = valueInt;
3509 }
3510
3511 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3512 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003513 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003514 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003515 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003516 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003517 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003518 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003519 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003520 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003521 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003522 if (target == "Manager") {
3523 mTestFormat = format;
3524 } else if (mTestOutputs[mCurOutput] != 0) {
3525 AudioParameter outputParam = AudioParameter();
3526 outputParam.addInt(String8("format"), format);
3527 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3528 }
3529 }
3530 }
3531 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3532 param.remove(String8("test_cmd_policy_channels"));
3533 int channels = 0;
3534
3535 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003536 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003537 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003538 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003539 }
3540 if (channels != 0) {
3541 if (target == "Manager") {
3542 mTestChannels = channels;
3543 } else if (mTestOutputs[mCurOutput] != 0) {
3544 AudioParameter outputParam = AudioParameter();
3545 outputParam.addInt(String8("channels"), channels);
3546 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3547 }
3548 }
3549 }
3550 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3551 param.remove(String8("test_cmd_policy_sampleRate"));
3552 if (valueInt >= 0 && valueInt <= 96000) {
3553 int samplingRate = valueInt;
3554 if (target == "Manager") {
3555 mTestSamplingRate = samplingRate;
3556 } else if (mTestOutputs[mCurOutput] != 0) {
3557 AudioParameter outputParam = AudioParameter();
3558 outputParam.addInt(String8("sampling_rate"), samplingRate);
3559 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3560 }
3561 }
3562 }
3563
3564 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3565 param.remove(String8("test_cmd_policy_reopen"));
3566
Eric Laurentc75307b2015-03-17 15:29:32 -07003567 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003568
Eric Laurentc75307b2015-03-17 15:29:32 -07003569 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003570
Eric Laurentc75307b2015-03-17 15:29:32 -07003571 removeOutput(mPrimaryOutput->mIoHandle);
3572 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3573 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003574 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003575 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3576 config.sample_rate = outputDesc->mSamplingRate;
3577 config.channel_mask = outputDesc->mChannelMask;
3578 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003579 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003580 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003581 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003582 &config,
3583 &outputDesc->mDevice,
3584 String8(""),
3585 &outputDesc->mLatency,
3586 outputDesc->mFlags);
3587 if (status != NO_ERROR) {
3588 ALOGE("Failed to reopen hardware output stream, "
3589 "samplingRate: %d, format %d, channels %d",
3590 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003591 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003592 outputDesc->mSamplingRate = config.sample_rate;
3593 outputDesc->mChannelMask = config.channel_mask;
3594 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003595 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003596 AudioParameter outputCmd = AudioParameter();
3597 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003598 mpClientInterface->setParameters(handle, outputCmd.toString());
3599 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003600 }
3601 }
3602
3603
3604 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3605 }
3606 }
3607 return false;
3608}
3609
Eric Laurente0720872014-03-11 09:30:41 -07003610void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003611{
3612 {
3613 AutoMutex _l(mLock);
3614 requestExit();
3615 mWaitWorkCV.signal();
3616 }
3617 requestExitAndWait();
3618}
3619
Eric Laurente0720872014-03-11 09:30:41 -07003620int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003621{
3622 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3623 if (output == mTestOutputs[i]) return i;
3624 }
3625 return 0;
3626}
3627#endif //AUDIO_POLICY_TEST
3628
3629// ---
3630
Eric Laurentc75307b2015-03-17 15:29:32 -07003631void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003632{
François Gaffie98cc1912015-03-18 17:52:40 +01003633 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003634 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003635 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003636 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003637}
3638
François Gaffie53615e22015-03-19 09:24:12 +01003639void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3640{
3641 mOutputs.removeItem(output);
3642}
3643
Eric Laurent1f2f2232014-06-02 12:01:23 -07003644void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003645{
François Gaffie98cc1912015-03-18 17:52:40 +01003646 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003647 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003648 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003649}
Eric Laurente552edb2014-03-10 17:42:56 -07003650
Eric Laurentc75307b2015-03-17 15:29:32 -07003651void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003652 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003653 const String8 address /*in*/,
3654 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003655 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003656 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003657 if (devDesc != 0) {
3658 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3659 desc->mIoHandle, address.string());
3660 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003661 }
3662}
3663
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003664status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003665 audio_policy_dev_state_t state,
3666 SortedVector<audio_io_handle_t>& outputs,
3667 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003668{
François Gaffie53615e22015-03-19 09:24:12 +01003669 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003670 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003671
3672 if (audio_device_is_digital(device)) {
3673 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003674 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003675 }
Eric Laurente552edb2014-03-10 17:42:56 -07003676
Eric Laurent3b73df72014-03-11 09:06:29 -07003677 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003678 // first list already open outputs that can be routed to this device
3679 for (size_t i = 0; i < mOutputs.size(); i++) {
3680 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003681 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003682 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003683 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3684 outputs.add(mOutputs.keyAt(i));
3685 } else {
3686 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003687 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003688 }
Eric Laurente552edb2014-03-10 17:42:56 -07003689 }
3690 }
3691 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003692 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003693 for (size_t i = 0; i < mHwModules.size(); i++)
3694 {
3695 if (mHwModules[i]->mHandle == 0) {
3696 continue;
3697 }
3698 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3699 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003700 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003701 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003702 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003703 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003704 profiles.add(profile);
3705 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3706 }
Eric Laurente552edb2014-03-10 17:42:56 -07003707 }
3708 }
3709 }
3710
Eric Laurent7b279bb2015-12-14 10:18:23 -08003711 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003712
Eric Laurente552edb2014-03-10 17:42:56 -07003713 if (profiles.isEmpty() && outputs.isEmpty()) {
3714 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3715 return BAD_VALUE;
3716 }
3717
3718 // open outputs for matching profiles if needed. Direct outputs are also opened to
3719 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3720 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003721 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003722
3723 // nothing to do if one output is already opened for this profile
3724 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003725 for (j = 0; j < outputs.size(); j++) {
3726 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003727 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003728 // matching profile: save the sample rates, format and channel masks supported
3729 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003730 if (audio_device_is_digital(device)) {
3731 devDesc->importAudioPort(profile);
3732 }
Eric Laurente552edb2014-03-10 17:42:56 -07003733 break;
3734 }
3735 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003736 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003737 continue;
3738 }
3739
Eric Laurent83b88082014-06-20 18:31:16 -07003740 ALOGV("opening output for device %08x with params %s profile %p",
3741 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003742 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003743 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003744 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3745 config.sample_rate = desc->mSamplingRate;
3746 config.channel_mask = desc->mChannelMask;
3747 config.format = desc->mFormat;
3748 config.offload_info.sample_rate = desc->mSamplingRate;
3749 config.offload_info.channel_mask = desc->mChannelMask;
3750 config.offload_info.format = desc->mFormat;
3751 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003752 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003753 &output,
3754 &config,
3755 &desc->mDevice,
3756 address,
3757 &desc->mLatency,
3758 desc->mFlags);
3759 if (status == NO_ERROR) {
3760 desc->mSamplingRate = config.sample_rate;
3761 desc->mChannelMask = config.channel_mask;
3762 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003763
Eric Laurentd4692962014-05-05 18:13:44 -07003764 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003765 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003766 char *param = audio_device_address_to_parameter(device, address);
3767 mpClientInterface->setParameters(output, String8(param));
3768 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003769 }
Phil Burk00eeb322016-03-31 12:41:00 -07003770 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003771 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003772 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003773 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003774 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003775 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003776 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003777 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003778 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003779 config.offload_info.sample_rate = config.sample_rate;
3780 config.offload_info.channel_mask = config.channel_mask;
3781 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003782 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003783 &output,
3784 &config,
3785 &desc->mDevice,
3786 address,
3787 &desc->mLatency,
3788 desc->mFlags);
3789 if (status == NO_ERROR) {
3790 desc->mSamplingRate = config.sample_rate;
3791 desc->mChannelMask = config.channel_mask;
3792 desc->mFormat = config.format;
3793 } else {
3794 output = AUDIO_IO_HANDLE_NONE;
3795 }
Eric Laurentd4692962014-05-05 18:13:44 -07003796 }
3797
Eric Laurentcf2c0212014-07-25 16:20:43 -07003798 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003799 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003800 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003801 sp<AudioPolicyMix> policyMix;
3802 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003803 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3804 address.string());
3805 }
François Gaffie036e1e92015-03-19 10:16:24 +01003806 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003807 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003808
Eric Laurent87ffa392015-05-22 10:32:38 -07003809 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3810 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003811 // no duplicated output for direct outputs and
3812 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003813 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003814
Eric Laurentd4692962014-05-05 18:13:44 -07003815 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003816 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003817
Eric Laurentd4692962014-05-05 18:13:44 -07003818 //TODO: configure audio effect output stage here
3819
3820 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003821 duplicatedOutput =
3822 mpClientInterface->openDuplicateOutput(output,
3823 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003824 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003825 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003826 sp<SwAudioOutputDescriptor> dupOutputDesc =
3827 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3828 dupOutputDesc->mOutput1 = mPrimaryOutput;
3829 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003830 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3831 dupOutputDesc->mFormat = desc->mFormat;
3832 dupOutputDesc->mChannelMask = desc->mChannelMask;
3833 dupOutputDesc->mLatency = desc->mLatency;
3834 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003835 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003836 } else {
3837 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003838 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003839 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003840 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003841 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003842 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003843 }
Eric Laurente552edb2014-03-10 17:42:56 -07003844 }
3845 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003846 } else {
3847 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003848 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003849 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003850 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003851 profiles.removeAt(profile_index);
3852 profile_index--;
3853 } else {
3854 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003855 // Load digital format info only for digital devices
3856 if (audio_device_is_digital(device)) {
3857 devDesc->importAudioPort(profile);
3858 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003859
François Gaffie53615e22015-03-19 09:24:12 +01003860 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003861 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3862 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003863 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003864 NULL/*patch handle*/, address.string());
3865 }
Eric Laurente552edb2014-03-10 17:42:56 -07003866 ALOGV("checkOutputsForDevice(): adding output %d", output);
3867 }
3868 }
3869
3870 if (profiles.isEmpty()) {
3871 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3872 return BAD_VALUE;
3873 }
Eric Laurentd4692962014-05-05 18:13:44 -07003874 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003875 // check if one opened output is not needed any more after disconnecting one device
3876 for (size_t i = 0; i < mOutputs.size(); i++) {
3877 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003878 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003879 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003880 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003881 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003882 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003883 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003884 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3885 mOutputs.keyAt(i));
3886 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003887 }
Eric Laurente552edb2014-03-10 17:42:56 -07003888 }
3889 }
Eric Laurentd4692962014-05-05 18:13:44 -07003890 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003891 for (size_t i = 0; i < mHwModules.size(); i++)
3892 {
3893 if (mHwModules[i]->mHandle == 0) {
3894 continue;
3895 }
3896 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3897 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003898 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003899 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003900 ALOGV("checkOutputsForDevice(): "
3901 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003902 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003903 }
3904 }
3905 }
3906 }
3907 return NO_ERROR;
3908}
3909
Paul McLean9080a4c2015-06-18 08:24:02 -07003910status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003911 audio_policy_dev_state_t state,
3912 SortedVector<audio_io_handle_t>& inputs,
3913 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003914{
Paul McLean9080a4c2015-06-18 08:24:02 -07003915 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003916 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003917
3918 if (audio_device_is_digital(device)) {
3919 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003920 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003921 }
3922
Eric Laurentd4692962014-05-05 18:13:44 -07003923 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3924 // first list already open inputs that can be routed to this device
3925 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3926 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003927 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003928 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3929 inputs.add(mInputs.keyAt(input_index));
3930 }
3931 }
3932
3933 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003934 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003935 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3936 {
3937 if (mHwModules[module_idx]->mHandle == 0) {
3938 continue;
3939 }
3940 for (size_t profile_index = 0;
3941 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3942 profile_index++)
3943 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003944 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3945
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003946 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003947 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003948 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003949 profiles.add(profile);
3950 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3951 profile_index, module_idx);
3952 }
Eric Laurentd4692962014-05-05 18:13:44 -07003953 }
3954 }
3955 }
3956
3957 if (profiles.isEmpty() && inputs.isEmpty()) {
3958 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3959 return BAD_VALUE;
3960 }
3961
3962 // open inputs for matching profiles if needed. Direct inputs are also opened to
3963 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3964 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3965
Eric Laurent1c333e22014-05-20 10:48:17 -07003966 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003967 // nothing to do if one input is already opened for this profile
3968 size_t input_index;
3969 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3970 desc = mInputs.valueAt(input_index);
3971 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003972 if (audio_device_is_digital(device)) {
3973 devDesc->importAudioPort(profile);
3974 }
Eric Laurentd4692962014-05-05 18:13:44 -07003975 break;
3976 }
3977 }
3978 if (input_index != mInputs.size()) {
3979 continue;
3980 }
3981
3982 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3983 desc = new AudioInputDescriptor(profile);
3984 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003985 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3986 config.sample_rate = desc->mSamplingRate;
3987 config.channel_mask = desc->mChannelMask;
3988 config.format = desc->mFormat;
3989 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003990 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003991 &input,
3992 &config,
3993 &desc->mDevice,
3994 address,
3995 AUDIO_SOURCE_MIC,
3996 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003997
Eric Laurentcf2c0212014-07-25 16:20:43 -07003998 if (status == NO_ERROR) {
3999 desc->mSamplingRate = config.sample_rate;
4000 desc->mChannelMask = config.channel_mask;
4001 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004002
Eric Laurentd4692962014-05-05 18:13:44 -07004003 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004004 char *param = audio_device_address_to_parameter(device, address);
4005 mpClientInterface->setParameters(input, String8(param));
4006 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004007 }
Phil Burk00eeb322016-03-31 12:41:00 -07004008 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004009 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004010 ALOGW("checkInputsForDevice() direct input missing param");
4011 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004012 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004013 }
4014
4015 if (input != 0) {
4016 addInput(input, desc);
4017 }
4018 } // endif input != 0
4019
Eric Laurentcf2c0212014-07-25 16:20:43 -07004020 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004021 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004022 profiles.removeAt(profile_index);
4023 profile_index--;
4024 } else {
4025 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004026 if (audio_device_is_digital(device)) {
4027 devDesc->importAudioPort(profile);
4028 }
Eric Laurentd4692962014-05-05 18:13:44 -07004029 ALOGV("checkInputsForDevice(): adding input %d", input);
4030 }
4031 } // end scan profiles
4032
4033 if (profiles.isEmpty()) {
4034 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4035 return BAD_VALUE;
4036 }
4037 } else {
4038 // Disconnect
4039 // check if one opened input is not needed any more after disconnecting one device
4040 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4041 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004042 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004043 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4044 mInputs.keyAt(input_index));
4045 inputs.add(mInputs.keyAt(input_index));
4046 }
4047 }
4048 // Clear any profiles associated with the disconnected device.
4049 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4050 if (mHwModules[module_index]->mHandle == 0) {
4051 continue;
4052 }
4053 for (size_t profile_index = 0;
4054 profile_index < mHwModules[module_index]->mInputProfiles.size();
4055 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004056 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004057 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004058 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004059 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004060 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004061 }
4062 }
4063 }
4064 } // end disconnect
4065
4066 return NO_ERROR;
4067}
4068
4069
Eric Laurente0720872014-03-11 09:30:41 -07004070void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004071{
4072 ALOGV("closeOutput(%d)", output);
4073
Eric Laurentc75307b2015-03-17 15:29:32 -07004074 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004075 if (outputDesc == NULL) {
4076 ALOGW("closeOutput() unknown output %d", output);
4077 return;
4078 }
François Gaffie036e1e92015-03-19 10:16:24 +01004079 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004080
Eric Laurente552edb2014-03-10 17:42:56 -07004081 // look for duplicated outputs connected to the output being removed.
4082 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004083 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004084 if (dupOutputDesc->isDuplicated() &&
4085 (dupOutputDesc->mOutput1 == outputDesc ||
4086 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004087 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004088 if (dupOutputDesc->mOutput1 == outputDesc) {
4089 outputDesc2 = dupOutputDesc->mOutput2;
4090 } else {
4091 outputDesc2 = dupOutputDesc->mOutput1;
4092 }
4093 // As all active tracks on duplicated output will be deleted,
4094 // and as they were also referenced on the other output, the reference
4095 // count for their stream type must be adjusted accordingly on
4096 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004097 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004098 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004099 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004100 }
4101 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4102 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4103
4104 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004105 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004106 }
4107 }
4108
Eric Laurent05b90f82014-08-27 15:32:29 -07004109 nextAudioPortGeneration();
4110
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004111 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004112 if (index >= 0) {
4113 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4114 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4115 mAudioPatches.removeItemsAt(index);
4116 mpClientInterface->onAudioPatchListUpdate();
4117 }
4118
Eric Laurente552edb2014-03-10 17:42:56 -07004119 AudioParameter param;
4120 param.add(String8("closing"), String8("true"));
4121 mpClientInterface->setParameters(output, param.toString());
4122
4123 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004124 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004125 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004126}
4127
4128void AudioPolicyManager::closeInput(audio_io_handle_t input)
4129{
4130 ALOGV("closeInput(%d)", input);
4131
4132 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4133 if (inputDesc == NULL) {
4134 ALOGW("closeInput() unknown input %d", input);
4135 return;
4136 }
4137
Eric Laurent6a94d692014-05-20 11:18:06 -07004138 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004139
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004140 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004141 if (index >= 0) {
4142 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4143 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4144 mAudioPatches.removeItemsAt(index);
4145 mpClientInterface->onAudioPatchListUpdate();
4146 }
4147
4148 mpClientInterface->closeInput(input);
4149 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004150}
4151
Eric Laurentc75307b2015-03-17 15:29:32 -07004152SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4153 audio_devices_t device,
4154 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004155{
4156 SortedVector<audio_io_handle_t> outputs;
4157
4158 ALOGVV("getOutputsForDevice() device %04x", device);
4159 for (size_t i = 0; i < openOutputs.size(); i++) {
4160 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004161 i, openOutputs.valueAt(i)->isDuplicated(),
4162 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004163 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4164 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4165 outputs.add(openOutputs.keyAt(i));
4166 }
4167 }
4168 return outputs;
4169}
4170
Eric Laurente0720872014-03-11 09:30:41 -07004171bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004172 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004173{
4174 if (outputs1.size() != outputs2.size()) {
4175 return false;
4176 }
4177 for (size_t i = 0; i < outputs1.size(); i++) {
4178 if (outputs1[i] != outputs2[i]) {
4179 return false;
4180 }
4181 }
4182 return true;
4183}
4184
Eric Laurente0720872014-03-11 09:30:41 -07004185void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004186{
4187 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4188 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4189 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4190 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4191
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004192 // also take into account external policy-related changes: add all outputs which are
4193 // associated with policies in the "before" and "after" output vectors
4194 ALOGVV("checkOutputForStrategy(): policy related outputs");
4195 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004196 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004197 if (desc != 0 && desc->mPolicyMix != NULL) {
4198 srcOutputs.add(desc->mIoHandle);
4199 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4200 }
4201 }
4202 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004203 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004204 if (desc != 0 && desc->mPolicyMix != NULL) {
4205 dstOutputs.add(desc->mIoHandle);
4206 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4207 }
4208 }
4209
Eric Laurente552edb2014-03-10 17:42:56 -07004210 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4211 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4212 strategy, srcOutputs[0], dstOutputs[0]);
4213 // mute strategy while moving tracks from one output to another
4214 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004215 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004216 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004217 setStrategyMute(strategy, true, desc);
4218 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004219 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004220 sp<AudioSourceDescriptor> source =
4221 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4222 if (source != 0){
4223 connectAudioSource(source);
4224 }
Eric Laurente552edb2014-03-10 17:42:56 -07004225 }
4226
4227 // Move effects associated to this strategy from previous output to new output
4228 if (strategy == STRATEGY_MEDIA) {
4229 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4230 SortedVector<audio_io_handle_t> moved;
4231 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004232 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4233 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4234 effectDesc->mIo != fxOutput) {
4235 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004236 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4237 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004238 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004239 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004240 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004241 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004242 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004243 }
4244 }
4245 }
4246 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004247 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004248 if (getStrategy((audio_stream_type_t)i) == strategy) {
4249 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004250 }
4251 }
4252 }
4253}
4254
Eric Laurente0720872014-03-11 09:30:41 -07004255void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004256{
François Gaffie2110e042015-03-24 08:41:51 +01004257 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004258 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004259 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004260 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004261 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 checkOutputForStrategy(STRATEGY_SONIFICATION);
4263 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004264 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004265 checkOutputForStrategy(STRATEGY_MEDIA);
4266 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004267 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004268}
4269
Eric Laurente0720872014-03-11 09:30:41 -07004270void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004271{
François Gaffie53615e22015-03-19 09:24:12 +01004272 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004273 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004274 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004275 return;
4276 }
4277
Eric Laurent3a4311c2014-03-17 12:00:47 -07004278 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004279 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4280 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4281 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004282 // suspend A2DP output if:
4283 // (NOT already suspended) &&
4284 // ((SCO device is connected &&
4285 // (forced usage for communication || for record is SCO))) ||
4286 // (phone state is ringing || in call)
4287 //
4288 // restore A2DP output if:
4289 // (Already suspended) &&
4290 // ((SCO device is NOT connected ||
4291 // (forced usage NOT for communication && NOT for record is SCO))) &&
4292 // (phone state is NOT ringing && NOT in call)
4293 //
4294 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004295 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004296 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4297 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4298 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4299 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004300
4301 mpClientInterface->restoreOutput(a2dpOutput);
4302 mA2dpSuspended = false;
4303 }
4304 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004305 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004306 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4307 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4308 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4309 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004310
4311 mpClientInterface->suspendOutput(a2dpOutput);
4312 mA2dpSuspended = true;
4313 }
4314 }
4315}
4316
Eric Laurentc75307b2015-03-17 15:29:32 -07004317audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4318 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004319{
4320 audio_devices_t device = AUDIO_DEVICE_NONE;
4321
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004322 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004323 if (index >= 0) {
4324 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4325 if (patchDesc->mUid != mUidCached) {
4326 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004327 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004328 return outputDesc->device();
4329 }
4330 }
4331
Eric Laurente552edb2014-03-10 17:42:56 -07004332 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004333 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004334 // use device for strategy enforced audible
4335 // 2: we are in call or the strategy phone is active on the output:
4336 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004337 // 3: the strategy for enforced audible is active but not enforced on the output:
4338 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004339 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004340 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004341 // 5: the strategy accessibility is active on the output:
4342 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004343 // 6: the strategy "respectful" sonification is active on the output:
4344 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004345 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004346 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004347 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004348 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004349 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004350 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004351 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004352 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004353 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4354 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004355 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004356 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004357 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004358 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004359 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004360 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004361 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4362 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004363 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004364 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004365 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004366 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004367 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004368 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004369 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004370 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004371 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004372 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004373 }
4374
Eric Laurent1c333e22014-05-20 10:48:17 -07004375 ALOGV("getNewOutputDevice() selected device %x", device);
4376 return device;
4377}
4378
Eric Laurentfb66dd92016-01-28 18:32:03 -08004379audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004380{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004381 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004382
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004383 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004384 if (index >= 0) {
4385 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4386 if (patchDesc->mUid != mUidCached) {
4387 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004388 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004389 return inputDesc->mDevice;
4390 }
4391 }
4392
Eric Laurentfb66dd92016-01-28 18:32:03 -08004393 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4394 if (isInCall()) {
4395 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4396 } else if (source != AUDIO_SOURCE_DEFAULT) {
4397 device = getDeviceAndMixForInputSource(source);
4398 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004399
Eric Laurente552edb2014-03-10 17:42:56 -07004400 return device;
4401}
4402
Eric Laurent794fde22016-03-11 09:50:45 -08004403bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4404 audio_stream_type_t stream2) {
4405 return ((stream1 == stream2) ||
4406 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4407 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004408}
4409
Eric Laurente0720872014-03-11 09:30:41 -07004410uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004411 return (uint32_t)getStrategy(stream);
4412}
4413
Eric Laurente0720872014-03-11 09:30:41 -07004414audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004415 // By checking the range of stream before calling getStrategy, we avoid
4416 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4417 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004418 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004419 return AUDIO_DEVICE_NONE;
4420 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004421 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004422 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4423 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004424 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004425 }
Eric Laurent794fde22016-03-11 09:50:45 -08004426 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004427 audio_devices_t curDevices =
4428 getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
4429 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4430 for (size_t i = 0; i < outputs.size(); i++) {
4431 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004432 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004433 curDevices |= outputDesc->device();
4434 }
4435 }
4436 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004437 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004438
4439 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4440 and doesn't really need to.*/
4441 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4442 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4443 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4444 }
Eric Laurente552edb2014-03-10 17:42:56 -07004445 return devices;
4446}
4447
François Gaffie2110e042015-03-24 08:41:51 +01004448routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4449{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004450 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004451 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004452}
4453
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004454uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4455 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004456 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4457 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4458 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004459 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4460 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4461 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004462 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004463 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004464}
4465
Eric Laurente0720872014-03-11 09:30:41 -07004466void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004467 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004468 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004469 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4470 updateDevicesAndOutputs();
4471 break;
4472 default:
4473 break;
4474 }
4475}
4476
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004477uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004478
4479 // skip beacon mute management if a dedicated TTS output is available
4480 if (mTtsOutputAvailable) {
4481 return 0;
4482 }
4483
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004484 switch(event) {
4485 case STARTING_OUTPUT:
4486 mBeaconMuteRefCount++;
4487 break;
4488 case STOPPING_OUTPUT:
4489 if (mBeaconMuteRefCount > 0) {
4490 mBeaconMuteRefCount--;
4491 }
4492 break;
4493 case STARTING_BEACON:
4494 mBeaconPlayingRefCount++;
4495 break;
4496 case STOPPING_BEACON:
4497 if (mBeaconPlayingRefCount > 0) {
4498 mBeaconPlayingRefCount--;
4499 }
4500 break;
4501 }
4502
4503 if (mBeaconMuteRefCount > 0) {
4504 // any playback causes beacon to be muted
4505 return setBeaconMute(true);
4506 } else {
4507 // no other playback: unmute when beacon starts playing, mute when it stops
4508 return setBeaconMute(mBeaconPlayingRefCount == 0);
4509 }
4510}
4511
4512uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4513 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4514 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4515 // keep track of muted state to avoid repeating mute/unmute operations
4516 if (mBeaconMuted != mute) {
4517 // mute/unmute AUDIO_STREAM_TTS on all outputs
4518 ALOGV("\t muting %d", mute);
4519 uint32_t maxLatency = 0;
4520 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004521 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004522 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004523 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004524 0 /*delay*/, AUDIO_DEVICE_NONE);
4525 const uint32_t latency = desc->latency() * 2;
4526 if (latency > maxLatency) {
4527 maxLatency = latency;
4528 }
4529 }
4530 mBeaconMuted = mute;
4531 return maxLatency;
4532 }
4533 return 0;
4534}
4535
Eric Laurente0720872014-03-11 09:30:41 -07004536audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004537 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004538{
Paul McLeanaa981192015-03-21 09:55:15 -07004539 // Routing
4540 // see if we have an explicit route
4541 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4542 // (getStrategy(stream)).
4543 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4544 // and activity count is non-zero
4545 // the device = the device from the descriptor in the RouteMap, and exit.
4546 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4547 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004548 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4549 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004550 return route->mDeviceDescriptor->type();
4551 }
4552 }
4553
Eric Laurente552edb2014-03-10 17:42:56 -07004554 if (fromCache) {
4555 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4556 strategy, mDeviceForStrategy[strategy]);
4557 return mDeviceForStrategy[strategy];
4558 }
François Gaffie2110e042015-03-24 08:41:51 +01004559 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004560}
4561
Eric Laurente0720872014-03-11 09:30:41 -07004562void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004563{
4564 for (int i = 0; i < NUM_STRATEGIES; i++) {
4565 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4566 }
4567 mPreviousOutputs = mOutputs;
4568}
4569
Eric Laurent1f2f2232014-06-02 12:01:23 -07004570uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004571 audio_devices_t prevDevice,
4572 uint32_t delayMs)
4573{
4574 // mute/unmute strategies using an incompatible device combination
4575 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4576 // if unmuting, unmute only after the specified delay
4577 if (outputDesc->isDuplicated()) {
4578 return 0;
4579 }
4580
4581 uint32_t muteWaitMs = 0;
4582 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004583 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004584
4585 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4586 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004587 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004588 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4589 bool doMute = false;
4590
4591 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4592 doMute = true;
4593 outputDesc->mStrategyMutedByDevice[i] = true;
4594 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4595 doMute = true;
4596 outputDesc->mStrategyMutedByDevice[i] = false;
4597 }
Eric Laurent99401132014-05-07 19:48:15 -07004598 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004599 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004600 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004601 // skip output if it does not share any device with current output
4602 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4603 == AUDIO_DEVICE_NONE) {
4604 continue;
4605 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004606 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4607 mute ? "muting" : "unmuting", i, curDevice);
4608 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004609 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004610 if (mute) {
4611 // FIXME: should not need to double latency if volume could be applied
4612 // immediately by the audioflinger mixer. We must account for the delay
4613 // between now and the next time the audioflinger thread for this output
4614 // will process a buffer (which corresponds to one buffer size,
4615 // usually 1/2 or 1/4 of the latency).
4616 if (muteWaitMs < desc->latency() * 2) {
4617 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004618 }
4619 }
4620 }
4621 }
4622 }
4623 }
4624
Eric Laurent99401132014-05-07 19:48:15 -07004625 // temporary mute output if device selection changes to avoid volume bursts due to
4626 // different per device volumes
4627 if (outputDesc->isActive() && (device != prevDevice)) {
4628 if (muteWaitMs < outputDesc->latency() * 2) {
4629 muteWaitMs = outputDesc->latency() * 2;
4630 }
4631 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004632 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004633 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004634 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004635 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004636 muteWaitMs *2, device);
4637 }
4638 }
4639 }
4640
Eric Laurente552edb2014-03-10 17:42:56 -07004641 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4642 if (muteWaitMs > delayMs) {
4643 muteWaitMs -= delayMs;
4644 usleep(muteWaitMs * 1000);
4645 return muteWaitMs;
4646 }
4647 return 0;
4648}
4649
Eric Laurentc75307b2015-03-17 15:29:32 -07004650uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004651 audio_devices_t device,
4652 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004653 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004654 audio_patch_handle_t *patchHandle,
4655 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004656{
Eric Laurentc75307b2015-03-17 15:29:32 -07004657 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004658 AudioParameter param;
4659 uint32_t muteWaitMs;
4660
4661 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004662 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4663 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004664 return muteWaitMs;
4665 }
4666 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4667 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004668 if ((device != AUDIO_DEVICE_NONE) &&
4669 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004670 return 0;
4671 }
4672
4673 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004674 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004675
4676 audio_devices_t prevDevice = outputDesc->mDevice;
4677
Paul McLeanaa981192015-03-21 09:55:15 -07004678 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004679
4680 if (device != AUDIO_DEVICE_NONE) {
4681 outputDesc->mDevice = device;
4682 }
4683 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4684
4685 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004686 // the requested device is AUDIO_DEVICE_NONE
4687 // OR the requested device is the same as current device
4688 // AND force is not specified
4689 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004690 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004691 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4692 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004693 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004694 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004695 return muteWaitMs;
4696 }
4697
4698 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004699
Eric Laurente552edb2014-03-10 17:42:56 -07004700 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004701 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004702 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004703 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004704 DeviceVector deviceList;
4705 if ((address == NULL) || (strlen(address) == 0)) {
4706 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4707 } else {
4708 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4709 }
4710
Eric Laurent1c333e22014-05-20 10:48:17 -07004711 if (!deviceList.isEmpty()) {
4712 struct audio_patch patch;
4713 outputDesc->toAudioPortConfig(&patch.sources[0]);
4714 patch.num_sources = 1;
4715 patch.num_sinks = 0;
4716 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4717 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004718 patch.num_sinks++;
4719 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004720 ssize_t index;
4721 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4722 index = mAudioPatches.indexOfKey(*patchHandle);
4723 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004724 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004725 }
4726 sp< AudioPatch> patchDesc;
4727 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4728 if (index >= 0) {
4729 patchDesc = mAudioPatches.valueAt(index);
4730 afPatchHandle = patchDesc->mAfPatchHandle;
4731 }
4732
Eric Laurent1c333e22014-05-20 10:48:17 -07004733 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004734 &afPatchHandle,
4735 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004736 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4737 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004738 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004739 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004740 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004741 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004742 addAudioPatch(patchDesc->mHandle, patchDesc);
4743 } else {
4744 patchDesc->mPatch = patch;
4745 }
4746 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004747 if (patchHandle) {
4748 *patchHandle = patchDesc->mHandle;
4749 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004750 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004751 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004752 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004753 }
4754 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004755
4756 // inform all input as well
4757 for (size_t i = 0; i < mInputs.size(); i++) {
4758 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004759 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004760 AudioParameter inputCmd = AudioParameter();
4761 ALOGV("%s: inform input %d of device:%d", __func__,
4762 inputDescriptor->mIoHandle, device);
4763 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4764 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4765 inputCmd.toString(),
4766 delayMs);
4767 }
4768 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004769 }
Eric Laurente552edb2014-03-10 17:42:56 -07004770
4771 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004772 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004773
4774 return muteWaitMs;
4775}
4776
Eric Laurentc75307b2015-03-17 15:29:32 -07004777status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004778 int delayMs,
4779 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004780{
Eric Laurent6a94d692014-05-20 11:18:06 -07004781 ssize_t index;
4782 if (patchHandle) {
4783 index = mAudioPatches.indexOfKey(*patchHandle);
4784 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004785 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004786 }
4787 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004788 return INVALID_OPERATION;
4789 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004790 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4791 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004792 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004793 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004794 removeAudioPatch(patchDesc->mHandle);
4795 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004796 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004797 return status;
4798}
4799
4800status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4801 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004802 bool force,
4803 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004804{
4805 status_t status = NO_ERROR;
4806
Eric Laurent1f2f2232014-06-02 12:01:23 -07004807 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004808 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4809 inputDesc->mDevice = device;
4810
4811 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4812 if (!deviceList.isEmpty()) {
4813 struct audio_patch patch;
4814 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004815 // AUDIO_SOURCE_HOTWORD is for internal use only:
4816 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004817 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004818 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004819 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4820 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004821 patch.num_sinks = 1;
4822 //only one input device for now
4823 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004824 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004825 ssize_t index;
4826 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4827 index = mAudioPatches.indexOfKey(*patchHandle);
4828 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004829 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004830 }
4831 sp< AudioPatch> patchDesc;
4832 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4833 if (index >= 0) {
4834 patchDesc = mAudioPatches.valueAt(index);
4835 afPatchHandle = patchDesc->mAfPatchHandle;
4836 }
4837
Eric Laurent1c333e22014-05-20 10:48:17 -07004838 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004839 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004840 0);
4841 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004842 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004843 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004844 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004845 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004846 addAudioPatch(patchDesc->mHandle, patchDesc);
4847 } else {
4848 patchDesc->mPatch = patch;
4849 }
4850 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004851 if (patchHandle) {
4852 *patchHandle = patchDesc->mHandle;
4853 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004854 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004855 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004856 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004857 }
4858 }
4859 }
4860 return status;
4861}
4862
Eric Laurent6a94d692014-05-20 11:18:06 -07004863status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4864 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004865{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004866 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 ssize_t index;
4868 if (patchHandle) {
4869 index = mAudioPatches.indexOfKey(*patchHandle);
4870 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004871 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004872 }
4873 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004874 return INVALID_OPERATION;
4875 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004876 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4877 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004878 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004879 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004880 removeAudioPatch(patchDesc->mHandle);
4881 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004882 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004883 return status;
4884}
4885
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004886sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004887 String8 address,
4888 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004889 audio_format_t& format,
4890 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004891 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004892{
4893 // Choose an input profile based on the requested capture parameters: select the first available
4894 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004895 //
4896 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4897 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004898
4899 for (size_t i = 0; i < mHwModules.size(); i++)
4900 {
4901 if (mHwModules[i]->mHandle == 0) {
4902 continue;
4903 }
4904 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4905 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004906 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004907 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004908 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004909 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004910 format,
4911 &format /*updatedFormat*/,
4912 channelMask,
4913 &channelMask /*updatedChannelMask*/,
4914 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004915
Eric Laurente552edb2014-03-10 17:42:56 -07004916 return profile;
4917 }
4918 }
4919 }
4920 return NULL;
4921}
4922
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004923
4924audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004925 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004926{
François Gaffie036e1e92015-03-19 10:16:24 +01004927 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4928 audio_devices_t selectedDeviceFromMix =
4929 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004930
François Gaffie036e1e92015-03-19 10:16:24 +01004931 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4932 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004933 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004934 return getDeviceForInputSource(inputSource);
4935}
4936
4937audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4938{
Paul McLean466dc8e2015-04-17 13:15:36 -06004939 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4940 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004941 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004942 return route->mDeviceDescriptor->type();
4943 }
4944 }
4945
4946 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004947}
4948
Eric Laurente0720872014-03-11 09:30:41 -07004949float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004950 int index,
4951 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004952{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004953 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004954 // if a headset is connected, apply the following rules to ring tones and notifications
4955 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004956 // - always attenuate notifications volume by 6dB
4957 // - attenuate ring tones volume by 6dB unless music is not playing and
4958 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004959 // - if music is playing, always limit the volume to current music volume,
4960 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004961 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004962 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4963 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4964 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4965 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4966 ((stream_strategy == STRATEGY_SONIFICATION)
4967 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004968 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004969 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004970 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004971 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004972 // when the phone is ringing we must consider that music could have been paused just before
4973 // by the music application and behave as if music was active if the last music track was
4974 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004975 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004976 mLimitRingtoneVolume) {
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004977 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004978 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004979 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004980 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4981 musicDevice),
4982 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004983 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4984 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4985 if (volumeDb > minVolDB) {
4986 volumeDb = minVolDB;
4987 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004988 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004989 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4990 stream_strategy != STRATEGY_SONIFICATION) {
4991 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004992 }
4993 }
4994
Eric Laurentffbc80f2015-03-18 18:30:19 -07004995 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004996}
4997
Eric Laurente0720872014-03-11 09:30:41 -07004998status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004999 int index,
5000 const sp<AudioOutputDescriptor>& outputDesc,
5001 audio_devices_t device,
5002 int delayMs,
5003 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005004{
Eric Laurente552edb2014-03-10 17:42:56 -07005005 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005006 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005007 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005008 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005009 return NO_ERROR;
5010 }
François Gaffie2110e042015-03-24 08:41:51 +01005011 audio_policy_forced_cfg_t forceUseForComm =
5012 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005013 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005014 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5015 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005016 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005017 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005018 return INVALID_OPERATION;
5019 }
5020
Eric Laurentc75307b2015-03-17 15:29:32 -07005021 if (device == AUDIO_DEVICE_NONE) {
5022 device = outputDesc->device();
5023 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005024
Eric Laurentffbc80f2015-03-18 18:30:19 -07005025 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005026 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005027 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005028 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005029
Eric Laurentffbc80f2015-03-18 18:30:19 -07005030 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005031
Eric Laurent3b73df72014-03-11 09:06:29 -07005032 if (stream == AUDIO_STREAM_VOICE_CALL ||
5033 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005034 float voiceVolume;
5035 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005036 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005037 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005038 } else {
5039 voiceVolume = 1.0;
5040 }
5041
Eric Laurent18fba842016-03-31 14:41:26 -07005042 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005043 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5044 mLastVoiceVolume = voiceVolume;
5045 }
5046 }
5047
5048 return NO_ERROR;
5049}
5050
Eric Laurentc75307b2015-03-17 15:29:32 -07005051void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5052 audio_devices_t device,
5053 int delayMs,
5054 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005055{
Eric Laurentc75307b2015-03-17 15:29:32 -07005056 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005057
Eric Laurent794fde22016-03-11 09:50:45 -08005058 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005059 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005060 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005061 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005062 device,
5063 delayMs,
5064 force);
5065 }
5066}
5067
Eric Laurente0720872014-03-11 09:30:41 -07005068void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005069 bool on,
5070 const sp<AudioOutputDescriptor>& outputDesc,
5071 int delayMs,
5072 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005073{
Eric Laurent72249d52015-06-08 11:12:15 -07005074 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5075 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005076 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005077 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005078 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005079 }
5080 }
5081}
5082
Eric Laurente0720872014-03-11 09:30:41 -07005083void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005084 bool on,
5085 const sp<AudioOutputDescriptor>& outputDesc,
5086 int delayMs,
5087 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005088{
Eric Laurente552edb2014-03-10 17:42:56 -07005089 if (device == AUDIO_DEVICE_NONE) {
5090 device = outputDesc->device();
5091 }
5092
Eric Laurentc75307b2015-03-17 15:29:32 -07005093 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5094 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005095
5096 if (on) {
5097 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005098 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005099 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005100 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005101 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005102 }
5103 }
5104 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5105 outputDesc->mMuteCount[stream]++;
5106 } else {
5107 if (outputDesc->mMuteCount[stream] == 0) {
5108 ALOGV("setStreamMute() unmuting non muted stream!");
5109 return;
5110 }
5111 if (--outputDesc->mMuteCount[stream] == 0) {
5112 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005113 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005114 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005115 device,
5116 delayMs);
5117 }
5118 }
5119}
5120
Eric Laurente0720872014-03-11 09:30:41 -07005121void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005122 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005123{
Eric Laurent87ffa392015-05-22 10:32:38 -07005124 if(!hasPrimaryOutput()) {
5125 return;
5126 }
5127
Eric Laurente552edb2014-03-10 17:42:56 -07005128 // if the stream pertains to sonification strategy and we are in call we must
5129 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5130 // in the device used for phone strategy and play the tone if the selected device does not
5131 // interfere with the device used for phone strategy
5132 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5133 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005134 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005135 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5136 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005137 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005138 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5139 stream, starting, outputDesc->mDevice, stateChange);
5140 if (outputDesc->mRefCount[stream]) {
5141 int muteCount = 1;
5142 if (stateChange) {
5143 muteCount = outputDesc->mRefCount[stream];
5144 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005145 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005146 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5147 for (int i = 0; i < muteCount; i++) {
5148 setStreamMute(stream, starting, mPrimaryOutput);
5149 }
5150 } else {
5151 ALOGV("handleIncallSonification() high visibility");
5152 if (outputDesc->device() &
5153 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5154 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5155 for (int i = 0; i < muteCount; i++) {
5156 setStreamMute(stream, starting, mPrimaryOutput);
5157 }
5158 }
5159 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005160 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5161 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005162 } else {
5163 mpClientInterface->stopTone();
5164 }
5165 }
5166 }
5167 }
5168}
5169
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005170audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5171{
5172 // flags to stream type mapping
5173 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5174 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5175 }
5176 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5177 return AUDIO_STREAM_BLUETOOTH_SCO;
5178 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005179 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5180 return AUDIO_STREAM_TTS;
5181 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005182
5183 // usage to stream type mapping
5184 switch (attr->usage) {
5185 case AUDIO_USAGE_MEDIA:
5186 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005187 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5188 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005189 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5190 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005191 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5192 return AUDIO_STREAM_SYSTEM;
5193 case AUDIO_USAGE_VOICE_COMMUNICATION:
5194 return AUDIO_STREAM_VOICE_CALL;
5195
5196 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5197 return AUDIO_STREAM_DTMF;
5198
5199 case AUDIO_USAGE_ALARM:
5200 return AUDIO_STREAM_ALARM;
5201 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5202 return AUDIO_STREAM_RING;
5203
5204 case AUDIO_USAGE_NOTIFICATION:
5205 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5206 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5207 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5208 case AUDIO_USAGE_NOTIFICATION_EVENT:
5209 return AUDIO_STREAM_NOTIFICATION;
5210
5211 case AUDIO_USAGE_UNKNOWN:
5212 default:
5213 return AUDIO_STREAM_MUSIC;
5214 }
5215}
Eric Laurente83b55d2014-11-14 10:06:21 -08005216
François Gaffie53615e22015-03-19 09:24:12 +01005217bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5218{
Eric Laurente83b55d2014-11-14 10:06:21 -08005219 // has flags that map to a strategy?
5220 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5221 return true;
5222 }
5223
5224 // has known usage?
5225 switch (paa->usage) {
5226 case AUDIO_USAGE_UNKNOWN:
5227 case AUDIO_USAGE_MEDIA:
5228 case AUDIO_USAGE_VOICE_COMMUNICATION:
5229 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5230 case AUDIO_USAGE_ALARM:
5231 case AUDIO_USAGE_NOTIFICATION:
5232 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5233 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5234 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5235 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5236 case AUDIO_USAGE_NOTIFICATION_EVENT:
5237 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5238 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5239 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5240 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005241 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005242 break;
5243 default:
5244 return false;
5245 }
5246 return true;
5247}
5248
François Gaffiead3183e2015-03-18 16:55:35 +01005249bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5250 routing_strategy strategy, uint32_t inPastMs,
5251 nsecs_t sysTime) const
5252{
5253 if ((sysTime == 0) && (inPastMs != 0)) {
5254 sysTime = systemTime();
5255 }
Eric Laurent794fde22016-03-11 09:50:45 -08005256 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005257 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5258 (NUM_STRATEGIES == strategy)) &&
5259 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5260 return true;
5261 }
5262 }
5263 return false;
5264}
5265
François Gaffie2110e042015-03-24 08:41:51 +01005266audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5267{
5268 return mEngine->getForceUse(usage);
5269}
5270
5271bool AudioPolicyManager::isInCall()
5272{
5273 return isStateInCall(mEngine->getPhoneState());
5274}
5275
5276bool AudioPolicyManager::isStateInCall(int state)
5277{
5278 return is_state_in_call(state);
5279}
5280
Eric Laurentd60560a2015-04-10 11:31:20 -07005281void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5282{
5283 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5284 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5285 if (sourceDesc->mDevice->equals(deviceDesc)) {
5286 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5287 stopAudioSource(sourceDesc->getHandle());
5288 }
5289 }
5290
5291 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5292 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5293 bool release = false;
5294 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5295 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5296 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5297 source->ext.device.type == deviceDesc->type()) {
5298 release = true;
5299 }
5300 }
5301 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5302 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5303 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5304 sink->ext.device.type == deviceDesc->type()) {
5305 release = true;
5306 }
5307 }
5308 if (release) {
5309 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5310 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5311 }
5312 }
5313}
5314
Phil Burk09bc4612016-02-24 15:58:15 -08005315// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005316void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5317 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005318 // TODO Set this based on Config properties.
5319 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005320
5321 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5322 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005323 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005324
5325 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005326 bool supportsAC3 = false;
5327 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005328 bool supportsIEC61937 = false;
5329 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5330 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005331 switch (format) {
5332 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005333 supportsAC3 = true;
5334 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005335 case AUDIO_FORMAT_E_AC3:
5336 case AUDIO_FORMAT_DTS:
5337 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005338 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005339 break;
5340 case AUDIO_FORMAT_IEC61937:
5341 supportsIEC61937 = true;
5342 break;
5343 default:
5344 break;
5345 }
5346 }
Phil Burk09bc4612016-02-24 15:58:15 -08005347
5348 // Modify formats based on surround preferences.
5349 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005350 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5351 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5352 // Remove surround sound related formats.
5353 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5354 audio_format_t format = formats[formatIndex];
5355 switch(format) {
5356 case AUDIO_FORMAT_AC3:
5357 case AUDIO_FORMAT_E_AC3:
5358 case AUDIO_FORMAT_DTS:
5359 case AUDIO_FORMAT_DTS_HD:
5360 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005361 formats.removeAt(formatIndex);
5362 break;
5363 default:
5364 formatIndex++; // keep it
5365 break;
5366 }
Phil Burk09bc4612016-02-24 15:58:15 -08005367 }
Phil Burk07ac1142016-03-25 13:39:29 -07005368 supportsAC3 = false;
5369 supportsOtherSurround = false;
5370 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005371 }
Phil Burk07ac1142016-03-25 13:39:29 -07005372 } else { // AUTO or ALWAYS
5373 // Most TVs support AC3 even if they do not report it in the EDID.
5374 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5375 && !supportsAC3) {
5376 formats.add(AUDIO_FORMAT_AC3);
5377 supportsAC3 = true;
5378 }
5379
5380 // If ALWAYS, add support for raw surround formats if all are missing.
5381 // This assumes that if any of these formats are reported by the HAL
5382 // then the report is valid and should not be modified.
5383 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5384 && !supportsOtherSurround) {
5385 formats.add(AUDIO_FORMAT_E_AC3);
5386 formats.add(AUDIO_FORMAT_DTS);
5387 formats.add(AUDIO_FORMAT_DTS_HD);
5388 supportsOtherSurround = true;
5389 }
5390
5391 // Add support for IEC61937 if any raw surround supported.
5392 // The HAL could do this but add it here, just in case.
5393 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5394 formats.add(AUDIO_FORMAT_IEC61937);
5395 supportsIEC61937 = true;
5396 }
Phil Burk09bc4612016-02-24 15:58:15 -08005397 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005398}
5399
5400// Modify the list of channel masks supported.
5401void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5402 ChannelsVector &channelMasks = *channelMasksPtr;
5403 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5404 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5405
5406 // If NEVER, then remove support for channelMasks > stereo.
5407 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5408 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5409 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5410 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5411 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5412 channelMasks.removeAt(maskIndex);
5413 } else {
5414 maskIndex++;
5415 }
5416 }
5417 // If ALWAYS, then make sure we at least support 5.1
5418 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5419 bool supports5dot1 = false;
5420 // Are there any channel masks that can be considered "surround"?
5421 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5422 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5423 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5424 supports5dot1 = true;
5425 break;
5426 }
5427 }
5428 // If not then add 5.1 support.
5429 if (!supports5dot1) {
5430 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5431 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5432 }
Phil Burk09bc4612016-02-24 15:58:15 -08005433 }
5434}
5435
Phil Burk00eeb322016-03-31 12:41:00 -07005436void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5437 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005438 AudioProfileVector &profiles)
5439{
5440 String8 reply;
5441 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005442
François Gaffie112b0af2015-11-19 16:13:25 +01005443 // Format MUST be checked first to update the list of AudioProfile
5444 if (profiles.hasDynamicFormat()) {
5445 reply = mpClientInterface->getParameters(ioHandle,
5446 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005447 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005448 AudioParameter repliedParameters(reply);
5449 if (repliedParameters.get(
5450 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005451 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5452 return;
5453 }
Phil Burk09bc4612016-02-24 15:58:15 -08005454 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005455 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005456 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005457 }
Phil Burk09bc4612016-02-24 15:58:15 -08005458 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005459 }
5460 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5461
Eric Laurent20eb3a42016-01-26 18:39:17 -08005462 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005463 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005464 ChannelsVector channelMasks;
5465 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005466 AudioParameter requestedParameters;
5467 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5468
5469 if (profiles.hasDynamicRateFor(format)) {
5470 reply = mpClientInterface->getParameters(ioHandle,
5471 requestedParameters.toString() + ";" +
5472 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5473 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005474 AudioParameter repliedParameters(reply);
5475 if (repliedParameters.get(
5476 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5477 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005478 }
5479 }
5480 if (profiles.hasDynamicChannelsFor(format)) {
5481 reply = mpClientInterface->getParameters(ioHandle,
5482 requestedParameters.toString() + ";" +
5483 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5484 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005485 AudioParameter repliedParameters(reply);
5486 if (repliedParameters.get(
5487 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5488 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005489 if (device == AUDIO_DEVICE_OUT_HDMI) {
5490 filterSurroundChannelMasks(&channelMasks);
5491 }
François Gaffie112b0af2015-11-19 16:13:25 +01005492 }
5493 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005494 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005495 }
5496}
Eric Laurentd60560a2015-04-10 11:31:20 -07005497
Eric Laurente552edb2014-03-10 17:42:56 -07005498}; // namespace android