blob: a19a4be8072c83c29aa501ea886f63f80b66d490 [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);
François Gaffie2110e042015-03-24 08:41:51 +01002261 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002262 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002263 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002264 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002265 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002266 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
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 dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
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 system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002273 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002274 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002275 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002276 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002277 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2278 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2279 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002280 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2281 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002282 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2283 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002284
François Gaffie2110e042015-03-24 08:41:51 +01002285 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002286
François Gaffie112b0af2015-11-19 16:13:25 +01002287 mAvailableOutputDevices.dump(fd, String8("Available output"));
2288 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002289 mHwModules.dump(fd);
2290 mOutputs.dump(fd);
2291 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002292 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002293 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002294 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002295
2296 return NO_ERROR;
2297}
2298
2299// This function checks for the parameters which can be offloaded.
2300// This can be enhanced depending on the capability of the DSP and policy
2301// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002302bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002303{
2304 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002305 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002306 offloadInfo.sample_rate, offloadInfo.channel_mask,
2307 offloadInfo.format,
2308 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2309 offloadInfo.has_video);
2310
Andy Hung2ddee192015-12-18 17:34:44 -08002311 if (mMasterMono) {
2312 return false; // no offloading if mono is set.
2313 }
2314
Eric Laurente552edb2014-03-10 17:42:56 -07002315 // Check if offload has been disabled
2316 char propValue[PROPERTY_VALUE_MAX];
2317 if (property_get("audio.offload.disable", propValue, "0")) {
2318 if (atoi(propValue) != 0) {
2319 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2320 return false;
2321 }
2322 }
2323
2324 // Check if stream type is music, then only allow offload as of now.
2325 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2326 {
2327 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2328 return false;
2329 }
2330
2331 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002332 const bool allowOffloadWithVideo =
2333 property_get_bool("audio.offload.video", false /* default_value */);
2334 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002335 ALOGV("isOffloadSupported: has_video == true, returning false");
2336 return false;
2337 }
2338
2339 //If duration is less than minimum value defined in property, return false
2340 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2341 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2342 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2343 return false;
2344 }
2345 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2346 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2347 return false;
2348 }
2349
2350 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2351 // creating an offloaded track and tearing it down immediately after start when audioflinger
2352 // detects there is an active non offloadable effect.
2353 // FIXME: We should check the audio session here but we do not have it in this context.
2354 // This may prevent offloading in rare situations where effects are left active by apps
2355 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002356 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002357 return false;
2358 }
2359
2360 // See if there is a profile to support this.
2361 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002362 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002363 offloadInfo.sample_rate,
2364 offloadInfo.format,
2365 offloadInfo.channel_mask,
2366 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002367 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2368 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002369}
2370
Eric Laurent6a94d692014-05-20 11:18:06 -07002371status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2372 audio_port_type_t type,
2373 unsigned int *num_ports,
2374 struct audio_port *ports,
2375 unsigned int *generation)
2376{
2377 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2378 generation == NULL) {
2379 return BAD_VALUE;
2380 }
2381 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2382 if (ports == NULL) {
2383 *num_ports = 0;
2384 }
2385
2386 size_t portsWritten = 0;
2387 size_t portsMax = *num_ports;
2388 *num_ports = 0;
2389 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002390 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2391 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002392 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002393 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2394 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2395 continue;
2396 }
2397 if (portsWritten < portsMax) {
2398 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2399 }
2400 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002401 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002402 }
2403 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002404 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2405 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2406 continue;
2407 }
2408 if (portsWritten < portsMax) {
2409 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2410 }
2411 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002412 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002413 }
2414 }
2415 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2416 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2417 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2418 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2419 }
2420 *num_ports += mInputs.size();
2421 }
2422 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002423 size_t numOutputs = 0;
2424 for (size_t i = 0; i < mOutputs.size(); i++) {
2425 if (!mOutputs[i]->isDuplicated()) {
2426 numOutputs++;
2427 if (portsWritten < portsMax) {
2428 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2429 }
2430 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002431 }
Eric Laurent84c70242014-06-23 08:46:27 -07002432 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002433 }
2434 }
2435 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002436 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002437 return NO_ERROR;
2438}
2439
2440status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2441{
2442 return NO_ERROR;
2443}
2444
Eric Laurent6a94d692014-05-20 11:18:06 -07002445status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2446 audio_patch_handle_t *handle,
2447 uid_t uid)
2448{
2449 ALOGV("createAudioPatch()");
2450
2451 if (handle == NULL || patch == NULL) {
2452 return BAD_VALUE;
2453 }
2454 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2455
Eric Laurent874c42872014-08-08 15:13:39 -07002456 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2457 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2458 return BAD_VALUE;
2459 }
2460 // only one source per audio patch supported for now
2461 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002462 return INVALID_OPERATION;
2463 }
Eric Laurent874c42872014-08-08 15:13:39 -07002464
2465 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002466 return INVALID_OPERATION;
2467 }
Eric Laurent874c42872014-08-08 15:13:39 -07002468 for (size_t i = 0; i < patch->num_sinks; i++) {
2469 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2470 return INVALID_OPERATION;
2471 }
2472 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002473
2474 sp<AudioPatch> patchDesc;
2475 ssize_t index = mAudioPatches.indexOfKey(*handle);
2476
Eric Laurent6a94d692014-05-20 11:18:06 -07002477 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2478 patch->sources[0].role,
2479 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002480#if LOG_NDEBUG == 0
2481 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002482 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002483 patch->sinks[i].role,
2484 patch->sinks[i].type);
2485 }
2486#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002487
2488 if (index >= 0) {
2489 patchDesc = mAudioPatches.valueAt(index);
2490 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2491 mUidCached, patchDesc->mUid, uid);
2492 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2493 return INVALID_OPERATION;
2494 }
2495 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002496 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002497 }
2498
2499 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002500 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002501 if (outputDesc == NULL) {
2502 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2503 return BAD_VALUE;
2504 }
Eric Laurent84c70242014-06-23 08:46:27 -07002505 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2506 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002507 if (patchDesc != 0) {
2508 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2509 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2510 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2511 return BAD_VALUE;
2512 }
2513 }
Eric Laurent874c42872014-08-08 15:13:39 -07002514 DeviceVector devices;
2515 for (size_t i = 0; i < patch->num_sinks; i++) {
2516 // Only support mix to devices connection
2517 // TODO add support for mix to mix connection
2518 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2519 ALOGV("createAudioPatch() source mix but sink is not a device");
2520 return INVALID_OPERATION;
2521 }
2522 sp<DeviceDescriptor> devDesc =
2523 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2524 if (devDesc == 0) {
2525 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2526 return BAD_VALUE;
2527 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002528
François Gaffie53615e22015-03-19 09:24:12 +01002529 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002530 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002531 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002532 NULL, // updatedSamplingRate
2533 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002534 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002535 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002536 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002537 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002538 ALOGV("createAudioPatch() profile not supported for device %08x",
2539 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002540 return INVALID_OPERATION;
2541 }
2542 devices.add(devDesc);
2543 }
2544 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002545 return INVALID_OPERATION;
2546 }
Eric Laurent874c42872014-08-08 15:13:39 -07002547
Eric Laurent6a94d692014-05-20 11:18:06 -07002548 // TODO: reconfigure output format and channels here
2549 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002550 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002551 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002552 index = mAudioPatches.indexOfKey(*handle);
2553 if (index >= 0) {
2554 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2555 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2556 }
2557 patchDesc = mAudioPatches.valueAt(index);
2558 patchDesc->mUid = uid;
2559 ALOGV("createAudioPatch() success");
2560 } else {
2561 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2562 return INVALID_OPERATION;
2563 }
2564 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2565 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2566 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002567 // only one sink supported when connecting an input device to a mix
2568 if (patch->num_sinks > 1) {
2569 return INVALID_OPERATION;
2570 }
François Gaffie53615e22015-03-19 09:24:12 +01002571 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002572 if (inputDesc == NULL) {
2573 return BAD_VALUE;
2574 }
2575 if (patchDesc != 0) {
2576 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2577 return BAD_VALUE;
2578 }
2579 }
2580 sp<DeviceDescriptor> devDesc =
2581 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2582 if (devDesc == 0) {
2583 return BAD_VALUE;
2584 }
2585
François Gaffie53615e22015-03-19 09:24:12 +01002586 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002587 devDesc->mAddress,
2588 patch->sinks[0].sample_rate,
2589 NULL, /*updatedSampleRate*/
2590 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002591 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002592 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002593 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002594 // FIXME for the parameter type,
2595 // and the NONE
2596 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002597 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002598 return INVALID_OPERATION;
2599 }
2600 // TODO: reconfigure output format and channels here
2601 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002602 devDesc->type(), inputDesc->mIoHandle);
2603 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002604 index = mAudioPatches.indexOfKey(*handle);
2605 if (index >= 0) {
2606 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2607 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2608 }
2609 patchDesc = mAudioPatches.valueAt(index);
2610 patchDesc->mUid = uid;
2611 ALOGV("createAudioPatch() success");
2612 } else {
2613 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2614 return INVALID_OPERATION;
2615 }
2616 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2617 // device to device connection
2618 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002619 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002620 return BAD_VALUE;
2621 }
2622 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002623 sp<DeviceDescriptor> srcDeviceDesc =
2624 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002625 if (srcDeviceDesc == 0) {
2626 return BAD_VALUE;
2627 }
Eric Laurent874c42872014-08-08 15:13:39 -07002628
Eric Laurent6a94d692014-05-20 11:18:06 -07002629 //update source and sink with our own data as the data passed in the patch may
2630 // be incomplete.
2631 struct audio_patch newPatch = *patch;
2632 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002633
Eric Laurent874c42872014-08-08 15:13:39 -07002634 for (size_t i = 0; i < patch->num_sinks; i++) {
2635 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2636 ALOGV("createAudioPatch() source device but one sink is not a device");
2637 return INVALID_OPERATION;
2638 }
2639
2640 sp<DeviceDescriptor> sinkDeviceDesc =
2641 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2642 if (sinkDeviceDesc == 0) {
2643 return BAD_VALUE;
2644 }
2645 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2646
Eric Laurent3bcf8592015-04-03 12:13:24 -07002647 // create a software bridge in PatchPanel if:
2648 // - source and sink devices are on differnt HW modules OR
2649 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002650 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002651 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002652 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002653 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002654 return INVALID_OPERATION;
2655 }
Eric Laurent874c42872014-08-08 15:13:39 -07002656 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002657 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002658 // if the sink device is reachable via an opened output stream, request to go via
2659 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002660 audio_io_handle_t output = selectOutput(outputs,
2661 AUDIO_OUTPUT_FLAG_NONE,
2662 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002663 if (output != AUDIO_IO_HANDLE_NONE) {
2664 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2665 if (outputDesc->isDuplicated()) {
2666 return INVALID_OPERATION;
2667 }
2668 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002669 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002670 newPatch.num_sources = 2;
2671 }
Eric Laurent83b88082014-06-20 18:31:16 -07002672 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002673 }
2674 // TODO: check from routing capabilities in config file and other conflicting patches
2675
2676 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2677 if (index >= 0) {
2678 afPatchHandle = patchDesc->mAfPatchHandle;
2679 }
2680
2681 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2682 &afPatchHandle,
2683 0);
2684 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2685 status, afPatchHandle);
2686 if (status == NO_ERROR) {
2687 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002688 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002689 addAudioPatch(patchDesc->mHandle, patchDesc);
2690 } else {
2691 patchDesc->mPatch = newPatch;
2692 }
2693 patchDesc->mAfPatchHandle = afPatchHandle;
2694 *handle = patchDesc->mHandle;
2695 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002696 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002697 } else {
2698 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2699 status);
2700 return INVALID_OPERATION;
2701 }
2702 } else {
2703 return BAD_VALUE;
2704 }
2705 } else {
2706 return BAD_VALUE;
2707 }
2708 return NO_ERROR;
2709}
2710
2711status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2712 uid_t uid)
2713{
2714 ALOGV("releaseAudioPatch() patch %d", handle);
2715
2716 ssize_t index = mAudioPatches.indexOfKey(handle);
2717
2718 if (index < 0) {
2719 return BAD_VALUE;
2720 }
2721 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2722 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2723 mUidCached, patchDesc->mUid, uid);
2724 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2725 return INVALID_OPERATION;
2726 }
2727
2728 struct audio_patch *patch = &patchDesc->mPatch;
2729 patchDesc->mUid = mUidCached;
2730 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002731 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002732 if (outputDesc == NULL) {
2733 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2734 return BAD_VALUE;
2735 }
2736
Eric Laurentc75307b2015-03-17 15:29:32 -07002737 setOutputDevice(outputDesc,
2738 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002739 true,
2740 0,
2741 NULL);
2742 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2743 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002744 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002745 if (inputDesc == NULL) {
2746 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2747 return BAD_VALUE;
2748 }
2749 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002750 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002751 true,
2752 NULL);
2753 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2754 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2755 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2756 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2757 status, patchDesc->mAfPatchHandle);
2758 removeAudioPatch(patchDesc->mHandle);
2759 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002760 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002761 } else {
2762 return BAD_VALUE;
2763 }
2764 } else {
2765 return BAD_VALUE;
2766 }
2767 return NO_ERROR;
2768}
2769
2770status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2771 struct audio_patch *patches,
2772 unsigned int *generation)
2773{
François Gaffie53615e22015-03-19 09:24:12 +01002774 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002775 return BAD_VALUE;
2776 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002777 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002778 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002779}
2780
Eric Laurente1715a42014-05-20 11:30:42 -07002781status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002782{
Eric Laurente1715a42014-05-20 11:30:42 -07002783 ALOGV("setAudioPortConfig()");
2784
2785 if (config == NULL) {
2786 return BAD_VALUE;
2787 }
2788 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2789 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002790 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2791 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002792 }
2793
Eric Laurenta121f902014-06-03 13:32:54 -07002794 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002795 if (config->type == AUDIO_PORT_TYPE_MIX) {
2796 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002797 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002798 if (outputDesc == NULL) {
2799 return BAD_VALUE;
2800 }
Eric Laurent84c70242014-06-23 08:46:27 -07002801 ALOG_ASSERT(!outputDesc->isDuplicated(),
2802 "setAudioPortConfig() called on duplicated output %d",
2803 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002804 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002805 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002806 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002807 if (inputDesc == NULL) {
2808 return BAD_VALUE;
2809 }
Eric Laurenta121f902014-06-03 13:32:54 -07002810 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002811 } else {
2812 return BAD_VALUE;
2813 }
2814 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2815 sp<DeviceDescriptor> deviceDesc;
2816 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2817 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2818 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2819 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2820 } else {
2821 return BAD_VALUE;
2822 }
2823 if (deviceDesc == NULL) {
2824 return BAD_VALUE;
2825 }
Eric Laurenta121f902014-06-03 13:32:54 -07002826 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002827 } else {
2828 return BAD_VALUE;
2829 }
2830
Eric Laurenta121f902014-06-03 13:32:54 -07002831 struct audio_port_config backupConfig;
2832 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2833 if (status == NO_ERROR) {
2834 struct audio_port_config newConfig;
2835 audioPortConfig->toAudioPortConfig(&newConfig, config);
2836 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002837 }
Eric Laurenta121f902014-06-03 13:32:54 -07002838 if (status != NO_ERROR) {
2839 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002840 }
Eric Laurente1715a42014-05-20 11:30:42 -07002841
2842 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002843}
2844
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002845void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2846{
Eric Laurentd60560a2015-04-10 11:31:20 -07002847 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002848 clearAudioPatches(uid);
2849 clearSessionRoutes(uid);
2850}
2851
Eric Laurent6a94d692014-05-20 11:18:06 -07002852void AudioPolicyManager::clearAudioPatches(uid_t uid)
2853{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002854 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002855 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2856 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002857 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002858 }
2859 }
2860}
2861
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002862void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2863 audio_io_handle_t ouptutToSkip)
2864{
2865 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2866 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2867 for (size_t j = 0; j < mOutputs.size(); j++) {
2868 if (mOutputs.keyAt(j) == ouptutToSkip) {
2869 continue;
2870 }
2871 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2872 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2873 continue;
2874 }
2875 // If the default device for this strategy is on another output mix,
2876 // invalidate all tracks in this strategy to force re connection.
2877 // Otherwise select new device on the output mix.
2878 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002879 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002880 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2881 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2882 }
2883 }
2884 } else {
2885 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2886 setOutputDevice(outputDesc, newDevice, false);
2887 }
2888 }
2889}
2890
2891void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2892{
2893 // remove output routes associated with this uid
2894 SortedVector<routing_strategy> affectedStrategies;
2895 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2896 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2897 if (route->mUid == uid) {
2898 mOutputRoutes.removeItemsAt(i);
2899 if (route->mDeviceDescriptor != 0) {
2900 affectedStrategies.add(getStrategy(route->mStreamType));
2901 }
2902 }
2903 }
2904 // reroute outputs if necessary
2905 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2906 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2907 }
2908
2909 // remove input routes associated with this uid
2910 SortedVector<audio_source_t> affectedSources;
2911 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2912 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2913 if (route->mUid == uid) {
2914 mInputRoutes.removeItemsAt(i);
2915 if (route->mDeviceDescriptor != 0) {
2916 affectedSources.add(route->mSource);
2917 }
2918 }
2919 }
2920 // reroute inputs if necessary
2921 SortedVector<audio_io_handle_t> inputsToClose;
2922 for (size_t i = 0; i < mInputs.size(); i++) {
2923 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002924 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002925 inputsToClose.add(inputDesc->mIoHandle);
2926 }
2927 }
2928 for (size_t i = 0; i < inputsToClose.size(); i++) {
2929 closeInput(inputsToClose[i]);
2930 }
2931}
2932
Eric Laurentd60560a2015-04-10 11:31:20 -07002933void AudioPolicyManager::clearAudioSources(uid_t uid)
2934{
2935 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2936 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2937 if (sourceDesc->mUid == uid) {
2938 stopAudioSource(mAudioSources.keyAt(i));
2939 }
2940 }
2941}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002942
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002943status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2944 audio_io_handle_t *ioHandle,
2945 audio_devices_t *device)
2946{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08002947 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2948 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002949 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002950
François Gaffiedf372692015-03-19 10:43:27 +01002951 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002952}
2953
Eric Laurentd60560a2015-04-10 11:31:20 -07002954status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2955 const audio_attributes_t *attributes,
2956 audio_io_handle_t *handle,
2957 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002958{
Eric Laurentd60560a2015-04-10 11:31:20 -07002959 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2960 if (source == NULL || attributes == NULL || handle == NULL) {
2961 return BAD_VALUE;
2962 }
2963
2964 *handle = AUDIO_IO_HANDLE_NONE;
2965
2966 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2967 source->type != AUDIO_PORT_TYPE_DEVICE) {
2968 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2969 return INVALID_OPERATION;
2970 }
2971
2972 sp<DeviceDescriptor> srcDeviceDesc =
2973 mAvailableInputDevices.getDevice(source->ext.device.type,
2974 String8(source->ext.device.address));
2975 if (srcDeviceDesc == 0) {
2976 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2977 return BAD_VALUE;
2978 }
2979 sp<AudioSourceDescriptor> sourceDesc =
2980 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2981
2982 struct audio_patch dummyPatch;
2983 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2984 sourceDesc->mPatchDesc = patchDesc;
2985
2986 status_t status = connectAudioSource(sourceDesc);
2987 if (status == NO_ERROR) {
2988 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2989 *handle = sourceDesc->getHandle();
2990 }
2991 return status;
2992}
2993
2994status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2995{
2996 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2997
2998 // make sure we only have one patch per source.
2999 disconnectAudioSource(sourceDesc);
3000
3001 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003002 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003003 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3004
3005 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3006 sp<DeviceDescriptor> sinkDeviceDesc =
3007 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3008
3009 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3010 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3011
3012 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3013 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003014 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003015 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3016 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3017 // create patch between src device and output device
3018 // create Hwoutput and add to mHwOutputs
3019 } else {
3020 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3021 audio_io_handle_t output =
3022 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3023 if (output == AUDIO_IO_HANDLE_NONE) {
3024 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3025 return INVALID_OPERATION;
3026 }
3027 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3028 if (outputDesc->isDuplicated()) {
3029 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3030 return INVALID_OPERATION;
3031 }
3032 // create a special patch with no sink and two sources:
3033 // - the second source indicates to PatchPanel through which output mix this patch should
3034 // be connected as well as the stream type for volume control
3035 // - the sink is defined by whatever output device is currently selected for the output
3036 // though which this patch is routed.
3037 patch->num_sinks = 0;
3038 patch->num_sources = 2;
3039 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3040 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3041 patch->sources[1].ext.mix.usecase.stream = stream;
3042 status_t status = mpClientInterface->createAudioPatch(patch,
3043 &afPatchHandle,
3044 0);
3045 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3046 status, afPatchHandle);
3047 if (status != NO_ERROR) {
3048 ALOGW("%s patch panel could not connect device patch, error %d",
3049 __FUNCTION__, status);
3050 return INVALID_OPERATION;
3051 }
3052 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003053 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003054
3055 if (status != NO_ERROR) {
3056 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3057 return status;
3058 }
3059 sourceDesc->mSwOutput = outputDesc;
3060 if (delayMs != 0) {
3061 usleep(delayMs * 1000);
3062 }
3063 }
3064
3065 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3066 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3067
3068 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003069}
3070
Eric Laurent493404d2015-04-21 15:07:36 -07003071status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003072{
Eric Laurentd60560a2015-04-10 11:31:20 -07003073 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3074 ALOGV("%s handle %d", __FUNCTION__, handle);
3075 if (sourceDesc == 0) {
3076 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3077 return BAD_VALUE;
3078 }
3079 status_t status = disconnectAudioSource(sourceDesc);
3080
3081 mAudioSources.removeItem(handle);
3082 return status;
3083}
3084
Andy Hung2ddee192015-12-18 17:34:44 -08003085status_t AudioPolicyManager::setMasterMono(bool mono)
3086{
3087 if (mMasterMono == mono) {
3088 return NO_ERROR;
3089 }
3090 mMasterMono = mono;
3091 // if enabling mono we close all offloaded devices, which will invalidate the
3092 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3093 // for recreating the new AudioTrack as non-offloaded PCM.
3094 //
3095 // If disabling mono, we leave all tracks as is: we don't know which clients
3096 // and tracks are able to be recreated as offloaded. The next "song" should
3097 // play back offloaded.
3098 if (mMasterMono) {
3099 Vector<audio_io_handle_t> offloaded;
3100 for (size_t i = 0; i < mOutputs.size(); ++i) {
3101 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3102 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3103 offloaded.push(desc->mIoHandle);
3104 }
3105 }
3106 for (size_t i = 0; i < offloaded.size(); ++i) {
3107 closeOutput(offloaded[i]);
3108 }
3109 }
3110 // update master mono for all remaining outputs
3111 for (size_t i = 0; i < mOutputs.size(); ++i) {
3112 updateMono(mOutputs.keyAt(i));
3113 }
3114 return NO_ERROR;
3115}
3116
3117status_t AudioPolicyManager::getMasterMono(bool *mono)
3118{
3119 *mono = mMasterMono;
3120 return NO_ERROR;
3121}
3122
Eric Laurentd60560a2015-04-10 11:31:20 -07003123status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3124{
3125 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3126
3127 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3128 if (patchDesc == 0) {
3129 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3130 sourceDesc->mPatchDesc->mHandle);
3131 return BAD_VALUE;
3132 }
3133 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3134
Eric Laurent28d09f02016-03-08 10:43:05 -08003135 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003136 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3137 if (swOutputDesc != 0) {
3138 stopSource(swOutputDesc, stream, false);
3139 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3140 } else {
3141 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3142 if (hwOutputDesc != 0) {
3143 // release patch between src device and output device
3144 // close Hwoutput and remove from mHwOutputs
3145 } else {
3146 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3147 }
3148 }
3149 return NO_ERROR;
3150}
3151
3152sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3153 audio_io_handle_t output, routing_strategy strategy)
3154{
3155 sp<AudioSourceDescriptor> source;
3156 for (size_t i = 0; i < mAudioSources.size(); i++) {
3157 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3158 routing_strategy sourceStrategy =
3159 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3160 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3161 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3162 source = sourceDesc;
3163 break;
3164 }
3165 }
3166 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003167}
3168
Eric Laurente552edb2014-03-10 17:42:56 -07003169// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003170// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003171// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003172uint32_t AudioPolicyManager::nextAudioPortGeneration()
3173{
3174 return android_atomic_inc(&mAudioPortGeneration);
3175}
3176
Eric Laurente0720872014-03-11 09:30:41 -07003177AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003178 :
3179#ifdef AUDIO_POLICY_TEST
3180 Thread(false),
3181#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003182 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003183 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003184 mAudioPortGeneration(1),
3185 mBeaconMuteRefCount(0),
3186 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003187 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003188 mTtsOutputAvailable(false),
3189 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003190{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003191 mUidCached = getuid();
3192 mpClientInterface = clientInterface;
3193
3194 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3195 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3196 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3197 bool speakerDrcEnabled = false;
3198
3199#ifdef USE_XML_AUDIO_POLICY_CONF
3200 mVolumeCurves = new VolumeCurvesCollection();
3201 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3202 mDefaultOutputDevice, speakerDrcEnabled,
3203 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3204 PolicySerializer serializer;
3205 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3206#else
3207 mVolumeCurves = new StreamDescriptorCollection();
3208 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3209 mDefaultOutputDevice, speakerDrcEnabled);
3210 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3211 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3212#endif
3213 ALOGE("could not load audio policy configuration file, setting defaults");
3214 config.setDefault();
3215 }
3216 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3217 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3218
3219 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003220 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3221 if (!engineInstance) {
3222 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3223 return;
3224 }
3225 // Retrieve the Policy Manager Interface
3226 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3227 if (mEngine == NULL) {
3228 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3229 return;
3230 }
3231 mEngine->setObserver(this);
3232 status_t status = mEngine->initCheck();
3233 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3234
Eric Laurent3a4311c2014-03-17 12:00:47 -07003235 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003236 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003237 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3238 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003239 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003240 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003241 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003242 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003243 continue;
3244 }
3245 // open all output streams needed to access attached devices
3246 // except for direct output streams that are only opened when they are actually
3247 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003248 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003249 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3250 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003251 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003252
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003253 if (!outProfile->hasSupportedDevices()) {
3254 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003255 continue;
3256 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003257 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003258 mTtsOutputAvailable = true;
3259 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003260
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003261 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003262 continue;
3263 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003264 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003265 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3266 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003267 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003268 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003269 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003270 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003271 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003272 if ((profileType & outputDeviceTypes) == 0) {
3273 continue;
3274 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003275 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3276 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003277 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3278 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3279 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3280 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003281
3282 outputDesc->mDevice = profileType;
3283 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3284 config.sample_rate = outputDesc->mSamplingRate;
3285 config.channel_mask = outputDesc->mChannelMask;
3286 config.format = outputDesc->mFormat;
3287 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003288 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003289 &output,
3290 &config,
3291 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003292 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003293 &outputDesc->mLatency,
3294 outputDesc->mFlags);
3295
3296 if (status != NO_ERROR) {
3297 ALOGW("Cannot open output stream for device %08x on hw module %s",
3298 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003299 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003300 } else {
3301 outputDesc->mSamplingRate = config.sample_rate;
3302 outputDesc->mChannelMask = config.channel_mask;
3303 outputDesc->mFormat = config.format;
3304
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003305 for (size_t k = 0; k < supportedDevices.size(); k++) {
3306 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003307 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003308 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3309 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003310 }
3311 }
3312 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003313 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003314 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003315 }
3316 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003317 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003318 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003319 true,
3320 0,
3321 NULL,
3322 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003323 }
Eric Laurente552edb2014-03-10 17:42:56 -07003324 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003325 // open input streams needed to access attached devices to validate
3326 // mAvailableInputDevices list
3327 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3328 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003329 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003330
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003331 if (!inProfile->hasSupportedDevices()) {
3332 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003333 continue;
3334 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003335 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003336 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003337 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3338
Eric Laurentd78f1532014-09-16 16:38:20 -07003339 if ((profileType & inputDeviceTypes) == 0) {
3340 continue;
3341 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003342 sp<AudioInputDescriptor> inputDesc =
3343 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003344
Eric Laurentd78f1532014-09-16 16:38:20 -07003345 inputDesc->mDevice = profileType;
3346
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003347 // find the address
3348 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3349 // the inputs vector must be of size 1, but we don't want to crash here
3350 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3351 : String8("");
3352 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3353 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3354
Eric Laurentd78f1532014-09-16 16:38:20 -07003355 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3356 config.sample_rate = inputDesc->mSamplingRate;
3357 config.channel_mask = inputDesc->mChannelMask;
3358 config.format = inputDesc->mFormat;
3359 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003360 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003361 &input,
3362 &config,
3363 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003364 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003365 AUDIO_SOURCE_MIC,
3366 AUDIO_INPUT_FLAG_NONE);
3367
3368 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003369 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3370 for (size_t k = 0; k < supportedDevices.size(); k++) {
3371 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003372 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003373 if (index >= 0) {
3374 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3375 if (!devDesc->isAttached()) {
3376 devDesc->attach(mHwModules[i]);
3377 devDesc->importAudioPort(inProfile);
3378 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003379 }
3380 }
3381 mpClientInterface->closeInput(input);
3382 } else {
3383 ALOGW("Cannot open input stream for device %08x on hw module %s",
3384 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003385 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003386 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003387 }
3388 }
3389 // make sure all attached devices have been allocated a unique ID
3390 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003391 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003392 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003393 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3394 continue;
3395 }
François Gaffie2110e042015-03-24 08:41:51 +01003396 // The device is now validated and can be appended to the available devices of the engine
3397 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3398 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003399 i++;
3400 }
3401 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003402 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003403 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003404 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3405 continue;
3406 }
François Gaffie2110e042015-03-24 08:41:51 +01003407 // The device is now validated and can be appended to the available devices of the engine
3408 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3409 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003410 i++;
3411 }
3412 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003413 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003414 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003415 }
Eric Laurente552edb2014-03-10 17:42:56 -07003416
3417 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3418
3419 updateDevicesAndOutputs();
3420
3421#ifdef AUDIO_POLICY_TEST
3422 if (mPrimaryOutput != 0) {
3423 AudioParameter outputCmd = AudioParameter();
3424 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003425 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003426
3427 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3428 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003429 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3430 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003431 mTestLatencyMs = 0;
3432 mCurOutput = 0;
3433 mDirectOutput = false;
3434 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3435 mTestOutputs[i] = 0;
3436 }
3437
3438 const size_t SIZE = 256;
3439 char buffer[SIZE];
3440 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3441 run(buffer, ANDROID_PRIORITY_AUDIO);
3442 }
3443#endif //AUDIO_POLICY_TEST
3444}
3445
Eric Laurente0720872014-03-11 09:30:41 -07003446AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003447{
3448#ifdef AUDIO_POLICY_TEST
3449 exit();
3450#endif //AUDIO_POLICY_TEST
3451 for (size_t i = 0; i < mOutputs.size(); i++) {
3452 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003453 }
3454 for (size_t i = 0; i < mInputs.size(); i++) {
3455 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003456 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003457 mAvailableOutputDevices.clear();
3458 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003459 mOutputs.clear();
3460 mInputs.clear();
3461 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003462}
3463
Eric Laurente0720872014-03-11 09:30:41 -07003464status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003465{
Eric Laurent87ffa392015-05-22 10:32:38 -07003466 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003467}
3468
3469#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003470bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003471{
3472 ALOGV("entering threadLoop()");
3473 while (!exitPending())
3474 {
3475 String8 command;
3476 int valueInt;
3477 String8 value;
3478
3479 Mutex::Autolock _l(mLock);
3480 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3481
3482 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3483 AudioParameter param = AudioParameter(command);
3484
3485 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3486 valueInt != 0) {
3487 ALOGV("Test command %s received", command.string());
3488 String8 target;
3489 if (param.get(String8("target"), target) != NO_ERROR) {
3490 target = "Manager";
3491 }
3492 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3493 param.remove(String8("test_cmd_policy_output"));
3494 mCurOutput = valueInt;
3495 }
3496 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3497 param.remove(String8("test_cmd_policy_direct"));
3498 if (value == "false") {
3499 mDirectOutput = false;
3500 } else if (value == "true") {
3501 mDirectOutput = true;
3502 }
3503 }
3504 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3505 param.remove(String8("test_cmd_policy_input"));
3506 mTestInput = valueInt;
3507 }
3508
3509 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3510 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003511 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003512 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003513 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003514 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003515 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003516 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003517 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003518 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003519 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003520 if (target == "Manager") {
3521 mTestFormat = format;
3522 } else if (mTestOutputs[mCurOutput] != 0) {
3523 AudioParameter outputParam = AudioParameter();
3524 outputParam.addInt(String8("format"), format);
3525 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3526 }
3527 }
3528 }
3529 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3530 param.remove(String8("test_cmd_policy_channels"));
3531 int channels = 0;
3532
3533 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003534 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003535 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003536 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003537 }
3538 if (channels != 0) {
3539 if (target == "Manager") {
3540 mTestChannels = channels;
3541 } else if (mTestOutputs[mCurOutput] != 0) {
3542 AudioParameter outputParam = AudioParameter();
3543 outputParam.addInt(String8("channels"), channels);
3544 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3545 }
3546 }
3547 }
3548 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3549 param.remove(String8("test_cmd_policy_sampleRate"));
3550 if (valueInt >= 0 && valueInt <= 96000) {
3551 int samplingRate = valueInt;
3552 if (target == "Manager") {
3553 mTestSamplingRate = samplingRate;
3554 } else if (mTestOutputs[mCurOutput] != 0) {
3555 AudioParameter outputParam = AudioParameter();
3556 outputParam.addInt(String8("sampling_rate"), samplingRate);
3557 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3558 }
3559 }
3560 }
3561
3562 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3563 param.remove(String8("test_cmd_policy_reopen"));
3564
Eric Laurentc75307b2015-03-17 15:29:32 -07003565 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003566
Eric Laurentc75307b2015-03-17 15:29:32 -07003567 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003568
Eric Laurentc75307b2015-03-17 15:29:32 -07003569 removeOutput(mPrimaryOutput->mIoHandle);
3570 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3571 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003572 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003573 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3574 config.sample_rate = outputDesc->mSamplingRate;
3575 config.channel_mask = outputDesc->mChannelMask;
3576 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003577 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003578 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003579 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003580 &config,
3581 &outputDesc->mDevice,
3582 String8(""),
3583 &outputDesc->mLatency,
3584 outputDesc->mFlags);
3585 if (status != NO_ERROR) {
3586 ALOGE("Failed to reopen hardware output stream, "
3587 "samplingRate: %d, format %d, channels %d",
3588 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003589 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003590 outputDesc->mSamplingRate = config.sample_rate;
3591 outputDesc->mChannelMask = config.channel_mask;
3592 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003593 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003594 AudioParameter outputCmd = AudioParameter();
3595 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003596 mpClientInterface->setParameters(handle, outputCmd.toString());
3597 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003598 }
3599 }
3600
3601
3602 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3603 }
3604 }
3605 return false;
3606}
3607
Eric Laurente0720872014-03-11 09:30:41 -07003608void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003609{
3610 {
3611 AutoMutex _l(mLock);
3612 requestExit();
3613 mWaitWorkCV.signal();
3614 }
3615 requestExitAndWait();
3616}
3617
Eric Laurente0720872014-03-11 09:30:41 -07003618int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003619{
3620 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3621 if (output == mTestOutputs[i]) return i;
3622 }
3623 return 0;
3624}
3625#endif //AUDIO_POLICY_TEST
3626
3627// ---
3628
Eric Laurentc75307b2015-03-17 15:29:32 -07003629void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003630{
François Gaffie98cc1912015-03-18 17:52:40 +01003631 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003632 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003633 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003634 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003635}
3636
François Gaffie53615e22015-03-19 09:24:12 +01003637void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3638{
3639 mOutputs.removeItem(output);
3640}
3641
Eric Laurent1f2f2232014-06-02 12:01:23 -07003642void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003643{
François Gaffie98cc1912015-03-18 17:52:40 +01003644 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003645 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003646 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003647}
Eric Laurente552edb2014-03-10 17:42:56 -07003648
Eric Laurentc75307b2015-03-17 15:29:32 -07003649void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003650 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003651 const String8 address /*in*/,
3652 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003653 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003654 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003655 if (devDesc != 0) {
3656 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3657 desc->mIoHandle, address.string());
3658 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003659 }
3660}
3661
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003662status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003663 audio_policy_dev_state_t state,
3664 SortedVector<audio_io_handle_t>& outputs,
3665 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003666{
François Gaffie53615e22015-03-19 09:24:12 +01003667 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003668 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003669
3670 if (audio_device_is_digital(device)) {
3671 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003672 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003673 }
Eric Laurente552edb2014-03-10 17:42:56 -07003674
Eric Laurent3b73df72014-03-11 09:06:29 -07003675 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003676 // first list already open outputs that can be routed to this device
3677 for (size_t i = 0; i < mOutputs.size(); i++) {
3678 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003679 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003680 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003681 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3682 outputs.add(mOutputs.keyAt(i));
3683 } else {
3684 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003685 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003686 }
Eric Laurente552edb2014-03-10 17:42:56 -07003687 }
3688 }
3689 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003690 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003691 for (size_t i = 0; i < mHwModules.size(); i++)
3692 {
3693 if (mHwModules[i]->mHandle == 0) {
3694 continue;
3695 }
3696 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3697 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003698 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003699 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003700 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003701 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003702 profiles.add(profile);
3703 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3704 }
Eric Laurente552edb2014-03-10 17:42:56 -07003705 }
3706 }
3707 }
3708
Eric Laurent7b279bb2015-12-14 10:18:23 -08003709 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003710
Eric Laurente552edb2014-03-10 17:42:56 -07003711 if (profiles.isEmpty() && outputs.isEmpty()) {
3712 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3713 return BAD_VALUE;
3714 }
3715
3716 // open outputs for matching profiles if needed. Direct outputs are also opened to
3717 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3718 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003719 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003720
3721 // nothing to do if one output is already opened for this profile
3722 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003723 for (j = 0; j < outputs.size(); j++) {
3724 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003725 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003726 // matching profile: save the sample rates, format and channel masks supported
3727 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003728 if (audio_device_is_digital(device)) {
3729 devDesc->importAudioPort(profile);
3730 }
Eric Laurente552edb2014-03-10 17:42:56 -07003731 break;
3732 }
3733 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003734 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003735 continue;
3736 }
3737
Eric Laurent83b88082014-06-20 18:31:16 -07003738 ALOGV("opening output for device %08x with params %s profile %p",
3739 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003740 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003741 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003742 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3743 config.sample_rate = desc->mSamplingRate;
3744 config.channel_mask = desc->mChannelMask;
3745 config.format = desc->mFormat;
3746 config.offload_info.sample_rate = desc->mSamplingRate;
3747 config.offload_info.channel_mask = desc->mChannelMask;
3748 config.offload_info.format = desc->mFormat;
3749 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003750 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003751 &output,
3752 &config,
3753 &desc->mDevice,
3754 address,
3755 &desc->mLatency,
3756 desc->mFlags);
3757 if (status == NO_ERROR) {
3758 desc->mSamplingRate = config.sample_rate;
3759 desc->mChannelMask = config.channel_mask;
3760 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003761
Eric Laurentd4692962014-05-05 18:13:44 -07003762 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003763 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003764 char *param = audio_device_address_to_parameter(device, address);
3765 mpClientInterface->setParameters(output, String8(param));
3766 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003767 }
Phil Burk00eeb322016-03-31 12:41:00 -07003768 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003769 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003770 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003771 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003772 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003773 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003774 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003775 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003776 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003777 config.offload_info.sample_rate = config.sample_rate;
3778 config.offload_info.channel_mask = config.channel_mask;
3779 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003780 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003781 &output,
3782 &config,
3783 &desc->mDevice,
3784 address,
3785 &desc->mLatency,
3786 desc->mFlags);
3787 if (status == NO_ERROR) {
3788 desc->mSamplingRate = config.sample_rate;
3789 desc->mChannelMask = config.channel_mask;
3790 desc->mFormat = config.format;
3791 } else {
3792 output = AUDIO_IO_HANDLE_NONE;
3793 }
Eric Laurentd4692962014-05-05 18:13:44 -07003794 }
3795
Eric Laurentcf2c0212014-07-25 16:20:43 -07003796 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003797 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003798 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003799 sp<AudioPolicyMix> policyMix;
3800 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003801 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3802 address.string());
3803 }
François Gaffie036e1e92015-03-19 10:16:24 +01003804 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003805 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003806
Eric Laurent87ffa392015-05-22 10:32:38 -07003807 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3808 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003809 // no duplicated output for direct outputs and
3810 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003811 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003812
Eric Laurentd4692962014-05-05 18:13:44 -07003813 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003814 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003815
Eric Laurentd4692962014-05-05 18:13:44 -07003816 //TODO: configure audio effect output stage here
3817
3818 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003819 duplicatedOutput =
3820 mpClientInterface->openDuplicateOutput(output,
3821 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003822 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003823 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003824 sp<SwAudioOutputDescriptor> dupOutputDesc =
3825 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3826 dupOutputDesc->mOutput1 = mPrimaryOutput;
3827 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003828 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3829 dupOutputDesc->mFormat = desc->mFormat;
3830 dupOutputDesc->mChannelMask = desc->mChannelMask;
3831 dupOutputDesc->mLatency = desc->mLatency;
3832 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003833 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003834 } else {
3835 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003836 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003837 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003838 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003839 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003840 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003841 }
Eric Laurente552edb2014-03-10 17:42:56 -07003842 }
3843 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003844 } else {
3845 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003846 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003847 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003848 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 profiles.removeAt(profile_index);
3850 profile_index--;
3851 } else {
3852 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003853 // Load digital format info only for digital devices
3854 if (audio_device_is_digital(device)) {
3855 devDesc->importAudioPort(profile);
3856 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003857
François Gaffie53615e22015-03-19 09:24:12 +01003858 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003859 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3860 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003861 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003862 NULL/*patch handle*/, address.string());
3863 }
Eric Laurente552edb2014-03-10 17:42:56 -07003864 ALOGV("checkOutputsForDevice(): adding output %d", output);
3865 }
3866 }
3867
3868 if (profiles.isEmpty()) {
3869 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3870 return BAD_VALUE;
3871 }
Eric Laurentd4692962014-05-05 18:13:44 -07003872 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003873 // check if one opened output is not needed any more after disconnecting one device
3874 for (size_t i = 0; i < mOutputs.size(); i++) {
3875 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003876 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003877 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003878 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003879 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003880 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003881 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003882 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3883 mOutputs.keyAt(i));
3884 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003885 }
Eric Laurente552edb2014-03-10 17:42:56 -07003886 }
3887 }
Eric Laurentd4692962014-05-05 18:13:44 -07003888 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003889 for (size_t i = 0; i < mHwModules.size(); i++)
3890 {
3891 if (mHwModules[i]->mHandle == 0) {
3892 continue;
3893 }
3894 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3895 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003896 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003897 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003898 ALOGV("checkOutputsForDevice(): "
3899 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003900 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003901 }
3902 }
3903 }
3904 }
3905 return NO_ERROR;
3906}
3907
Paul McLean9080a4c2015-06-18 08:24:02 -07003908status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003909 audio_policy_dev_state_t state,
3910 SortedVector<audio_io_handle_t>& inputs,
3911 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003912{
Paul McLean9080a4c2015-06-18 08:24:02 -07003913 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003914 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003915
3916 if (audio_device_is_digital(device)) {
3917 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003918 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003919 }
3920
Eric Laurentd4692962014-05-05 18:13:44 -07003921 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3922 // first list already open inputs that can be routed to this device
3923 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3924 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003925 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003926 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3927 inputs.add(mInputs.keyAt(input_index));
3928 }
3929 }
3930
3931 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003932 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003933 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3934 {
3935 if (mHwModules[module_idx]->mHandle == 0) {
3936 continue;
3937 }
3938 for (size_t profile_index = 0;
3939 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3940 profile_index++)
3941 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003942 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3943
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003944 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003945 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003946 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003947 profiles.add(profile);
3948 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3949 profile_index, module_idx);
3950 }
Eric Laurentd4692962014-05-05 18:13:44 -07003951 }
3952 }
3953 }
3954
3955 if (profiles.isEmpty() && inputs.isEmpty()) {
3956 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3957 return BAD_VALUE;
3958 }
3959
3960 // open inputs for matching profiles if needed. Direct inputs are also opened to
3961 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3962 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3963
Eric Laurent1c333e22014-05-20 10:48:17 -07003964 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003965 // nothing to do if one input is already opened for this profile
3966 size_t input_index;
3967 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3968 desc = mInputs.valueAt(input_index);
3969 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003970 if (audio_device_is_digital(device)) {
3971 devDesc->importAudioPort(profile);
3972 }
Eric Laurentd4692962014-05-05 18:13:44 -07003973 break;
3974 }
3975 }
3976 if (input_index != mInputs.size()) {
3977 continue;
3978 }
3979
3980 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3981 desc = new AudioInputDescriptor(profile);
3982 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003983 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3984 config.sample_rate = desc->mSamplingRate;
3985 config.channel_mask = desc->mChannelMask;
3986 config.format = desc->mFormat;
3987 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003988 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003989 &input,
3990 &config,
3991 &desc->mDevice,
3992 address,
3993 AUDIO_SOURCE_MIC,
3994 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003995
Eric Laurentcf2c0212014-07-25 16:20:43 -07003996 if (status == NO_ERROR) {
3997 desc->mSamplingRate = config.sample_rate;
3998 desc->mChannelMask = config.channel_mask;
3999 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07004000
Eric Laurentd4692962014-05-05 18:13:44 -07004001 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004002 char *param = audio_device_address_to_parameter(device, address);
4003 mpClientInterface->setParameters(input, String8(param));
4004 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004005 }
Phil Burk00eeb322016-03-31 12:41:00 -07004006 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004007 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004008 ALOGW("checkInputsForDevice() direct input missing param");
4009 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004010 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004011 }
4012
4013 if (input != 0) {
4014 addInput(input, desc);
4015 }
4016 } // endif input != 0
4017
Eric Laurentcf2c0212014-07-25 16:20:43 -07004018 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004019 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004020 profiles.removeAt(profile_index);
4021 profile_index--;
4022 } else {
4023 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004024 if (audio_device_is_digital(device)) {
4025 devDesc->importAudioPort(profile);
4026 }
Eric Laurentd4692962014-05-05 18:13:44 -07004027 ALOGV("checkInputsForDevice(): adding input %d", input);
4028 }
4029 } // end scan profiles
4030
4031 if (profiles.isEmpty()) {
4032 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4033 return BAD_VALUE;
4034 }
4035 } else {
4036 // Disconnect
4037 // check if one opened input is not needed any more after disconnecting one device
4038 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4039 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004040 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004041 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4042 mInputs.keyAt(input_index));
4043 inputs.add(mInputs.keyAt(input_index));
4044 }
4045 }
4046 // Clear any profiles associated with the disconnected device.
4047 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4048 if (mHwModules[module_index]->mHandle == 0) {
4049 continue;
4050 }
4051 for (size_t profile_index = 0;
4052 profile_index < mHwModules[module_index]->mInputProfiles.size();
4053 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004054 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004055 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004056 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004057 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004058 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004059 }
4060 }
4061 }
4062 } // end disconnect
4063
4064 return NO_ERROR;
4065}
4066
4067
Eric Laurente0720872014-03-11 09:30:41 -07004068void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004069{
4070 ALOGV("closeOutput(%d)", output);
4071
Eric Laurentc75307b2015-03-17 15:29:32 -07004072 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004073 if (outputDesc == NULL) {
4074 ALOGW("closeOutput() unknown output %d", output);
4075 return;
4076 }
François Gaffie036e1e92015-03-19 10:16:24 +01004077 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004078
Eric Laurente552edb2014-03-10 17:42:56 -07004079 // look for duplicated outputs connected to the output being removed.
4080 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004081 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004082 if (dupOutputDesc->isDuplicated() &&
4083 (dupOutputDesc->mOutput1 == outputDesc ||
4084 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004085 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004086 if (dupOutputDesc->mOutput1 == outputDesc) {
4087 outputDesc2 = dupOutputDesc->mOutput2;
4088 } else {
4089 outputDesc2 = dupOutputDesc->mOutput1;
4090 }
4091 // As all active tracks on duplicated output will be deleted,
4092 // and as they were also referenced on the other output, the reference
4093 // count for their stream type must be adjusted accordingly on
4094 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004095 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004096 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004097 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004098 }
4099 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4100 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4101
4102 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004103 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004104 }
4105 }
4106
Eric Laurent05b90f82014-08-27 15:32:29 -07004107 nextAudioPortGeneration();
4108
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004109 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004110 if (index >= 0) {
4111 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4112 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4113 mAudioPatches.removeItemsAt(index);
4114 mpClientInterface->onAudioPatchListUpdate();
4115 }
4116
Eric Laurente552edb2014-03-10 17:42:56 -07004117 AudioParameter param;
4118 param.add(String8("closing"), String8("true"));
4119 mpClientInterface->setParameters(output, param.toString());
4120
4121 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004122 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004123 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004124}
4125
4126void AudioPolicyManager::closeInput(audio_io_handle_t input)
4127{
4128 ALOGV("closeInput(%d)", input);
4129
4130 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4131 if (inputDesc == NULL) {
4132 ALOGW("closeInput() unknown input %d", input);
4133 return;
4134 }
4135
Eric Laurent6a94d692014-05-20 11:18:06 -07004136 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004137
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004138 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004139 if (index >= 0) {
4140 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4141 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4142 mAudioPatches.removeItemsAt(index);
4143 mpClientInterface->onAudioPatchListUpdate();
4144 }
4145
4146 mpClientInterface->closeInput(input);
4147 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004148}
4149
Eric Laurentc75307b2015-03-17 15:29:32 -07004150SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4151 audio_devices_t device,
4152 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004153{
4154 SortedVector<audio_io_handle_t> outputs;
4155
4156 ALOGVV("getOutputsForDevice() device %04x", device);
4157 for (size_t i = 0; i < openOutputs.size(); i++) {
4158 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004159 i, openOutputs.valueAt(i)->isDuplicated(),
4160 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004161 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4162 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4163 outputs.add(openOutputs.keyAt(i));
4164 }
4165 }
4166 return outputs;
4167}
4168
Eric Laurente0720872014-03-11 09:30:41 -07004169bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004170 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004171{
4172 if (outputs1.size() != outputs2.size()) {
4173 return false;
4174 }
4175 for (size_t i = 0; i < outputs1.size(); i++) {
4176 if (outputs1[i] != outputs2[i]) {
4177 return false;
4178 }
4179 }
4180 return true;
4181}
4182
Eric Laurente0720872014-03-11 09:30:41 -07004183void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004184{
4185 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4186 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4187 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4188 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4189
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004190 // also take into account external policy-related changes: add all outputs which are
4191 // associated with policies in the "before" and "after" output vectors
4192 ALOGVV("checkOutputForStrategy(): policy related outputs");
4193 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004194 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004195 if (desc != 0 && desc->mPolicyMix != NULL) {
4196 srcOutputs.add(desc->mIoHandle);
4197 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4198 }
4199 }
4200 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004201 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004202 if (desc != 0 && desc->mPolicyMix != NULL) {
4203 dstOutputs.add(desc->mIoHandle);
4204 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4205 }
4206 }
4207
Eric Laurente552edb2014-03-10 17:42:56 -07004208 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4209 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4210 strategy, srcOutputs[0], dstOutputs[0]);
4211 // mute strategy while moving tracks from one output to another
4212 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004213 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004214 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004215 setStrategyMute(strategy, true, desc);
4216 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004217 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004218 sp<AudioSourceDescriptor> source =
4219 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4220 if (source != 0){
4221 connectAudioSource(source);
4222 }
Eric Laurente552edb2014-03-10 17:42:56 -07004223 }
4224
4225 // Move effects associated to this strategy from previous output to new output
4226 if (strategy == STRATEGY_MEDIA) {
4227 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4228 SortedVector<audio_io_handle_t> moved;
4229 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004230 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4231 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4232 effectDesc->mIo != fxOutput) {
4233 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004234 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4235 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004236 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004237 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004238 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004239 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004240 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004241 }
4242 }
4243 }
4244 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004245 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004246 if (getStrategy((audio_stream_type_t)i) == strategy) {
4247 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004248 }
4249 }
4250 }
4251}
4252
Eric Laurente0720872014-03-11 09:30:41 -07004253void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004254{
François Gaffie2110e042015-03-24 08:41:51 +01004255 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004256 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004257 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004258 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004259 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004260 checkOutputForStrategy(STRATEGY_SONIFICATION);
4261 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004262 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004263 checkOutputForStrategy(STRATEGY_MEDIA);
4264 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004265 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004266}
4267
Eric Laurente0720872014-03-11 09:30:41 -07004268void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004269{
François Gaffie53615e22015-03-19 09:24:12 +01004270 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004271 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004272 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004273 return;
4274 }
4275
Eric Laurent3a4311c2014-03-17 12:00:47 -07004276 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004277 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4278 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4279 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004280 // suspend A2DP output if:
4281 // (NOT already suspended) &&
4282 // ((SCO device is connected &&
4283 // (forced usage for communication || for record is SCO))) ||
4284 // (phone state is ringing || in call)
4285 //
4286 // restore A2DP output if:
4287 // (Already suspended) &&
4288 // ((SCO device is NOT connected ||
4289 // (forced usage NOT for communication && NOT for record is SCO))) &&
4290 // (phone state is NOT ringing && NOT in call)
4291 //
4292 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004293 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004294 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4295 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4296 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4297 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004298
4299 mpClientInterface->restoreOutput(a2dpOutput);
4300 mA2dpSuspended = false;
4301 }
4302 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004303 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004304 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4305 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4306 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4307 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004308
4309 mpClientInterface->suspendOutput(a2dpOutput);
4310 mA2dpSuspended = true;
4311 }
4312 }
4313}
4314
Eric Laurentc75307b2015-03-17 15:29:32 -07004315audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4316 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004317{
4318 audio_devices_t device = AUDIO_DEVICE_NONE;
4319
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004320 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004321 if (index >= 0) {
4322 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4323 if (patchDesc->mUid != mUidCached) {
4324 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004325 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004326 return outputDesc->device();
4327 }
4328 }
4329
Eric Laurente552edb2014-03-10 17:42:56 -07004330 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004331 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004332 // use device for strategy enforced audible
4333 // 2: we are in call or the strategy phone is active on the output:
4334 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004335 // 3: the strategy for enforced audible is active but not enforced on the output:
4336 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004337 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004338 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004339 // 5: the strategy accessibility is active on the output:
4340 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004341 // 6: the strategy "respectful" sonification is active on the output:
4342 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004343 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004344 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004345 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004346 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004347 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004348 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004349 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004350 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004351 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4352 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004353 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004354 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004355 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004356 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004357 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004358 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004359 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4360 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004361 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004362 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004363 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004364 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004365 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004366 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004367 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004368 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004369 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004370 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004371 }
4372
Eric Laurent1c333e22014-05-20 10:48:17 -07004373 ALOGV("getNewOutputDevice() selected device %x", device);
4374 return device;
4375}
4376
Eric Laurentfb66dd92016-01-28 18:32:03 -08004377audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004378{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004379 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004380
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004381 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004382 if (index >= 0) {
4383 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4384 if (patchDesc->mUid != mUidCached) {
4385 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004386 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004387 return inputDesc->mDevice;
4388 }
4389 }
4390
Eric Laurentfb66dd92016-01-28 18:32:03 -08004391 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4392 if (isInCall()) {
4393 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4394 } else if (source != AUDIO_SOURCE_DEFAULT) {
4395 device = getDeviceAndMixForInputSource(source);
4396 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004397
Eric Laurente552edb2014-03-10 17:42:56 -07004398 return device;
4399}
4400
Eric Laurent794fde22016-03-11 09:50:45 -08004401bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4402 audio_stream_type_t stream2) {
4403 return ((stream1 == stream2) ||
4404 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4405 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004406}
4407
Eric Laurente0720872014-03-11 09:30:41 -07004408uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004409 return (uint32_t)getStrategy(stream);
4410}
4411
Eric Laurente0720872014-03-11 09:30:41 -07004412audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004413 // By checking the range of stream before calling getStrategy, we avoid
4414 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4415 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004416 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004417 return AUDIO_DEVICE_NONE;
4418 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004419 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004420 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4421 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004422 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004423 }
Eric Laurent794fde22016-03-11 09:50:45 -08004424 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004425 audio_devices_t curDevices =
4426 getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
4427 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4428 for (size_t i = 0; i < outputs.size(); i++) {
4429 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004430 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004431 curDevices |= outputDesc->device();
4432 }
4433 }
4434 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004435 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004436
4437 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4438 and doesn't really need to.*/
4439 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4440 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4441 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4442 }
Eric Laurente552edb2014-03-10 17:42:56 -07004443 return devices;
4444}
4445
François Gaffie2110e042015-03-24 08:41:51 +01004446routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4447{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004448 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004449 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004450}
4451
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004452uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4453 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004454 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4455 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4456 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004457 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4458 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4459 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004460 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004461 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004462}
4463
Eric Laurente0720872014-03-11 09:30:41 -07004464void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004465 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004466 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004467 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4468 updateDevicesAndOutputs();
4469 break;
4470 default:
4471 break;
4472 }
4473}
4474
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004475uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004476
4477 // skip beacon mute management if a dedicated TTS output is available
4478 if (mTtsOutputAvailable) {
4479 return 0;
4480 }
4481
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004482 switch(event) {
4483 case STARTING_OUTPUT:
4484 mBeaconMuteRefCount++;
4485 break;
4486 case STOPPING_OUTPUT:
4487 if (mBeaconMuteRefCount > 0) {
4488 mBeaconMuteRefCount--;
4489 }
4490 break;
4491 case STARTING_BEACON:
4492 mBeaconPlayingRefCount++;
4493 break;
4494 case STOPPING_BEACON:
4495 if (mBeaconPlayingRefCount > 0) {
4496 mBeaconPlayingRefCount--;
4497 }
4498 break;
4499 }
4500
4501 if (mBeaconMuteRefCount > 0) {
4502 // any playback causes beacon to be muted
4503 return setBeaconMute(true);
4504 } else {
4505 // no other playback: unmute when beacon starts playing, mute when it stops
4506 return setBeaconMute(mBeaconPlayingRefCount == 0);
4507 }
4508}
4509
4510uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4511 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4512 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4513 // keep track of muted state to avoid repeating mute/unmute operations
4514 if (mBeaconMuted != mute) {
4515 // mute/unmute AUDIO_STREAM_TTS on all outputs
4516 ALOGV("\t muting %d", mute);
4517 uint32_t maxLatency = 0;
4518 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004519 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004520 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004521 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004522 0 /*delay*/, AUDIO_DEVICE_NONE);
4523 const uint32_t latency = desc->latency() * 2;
4524 if (latency > maxLatency) {
4525 maxLatency = latency;
4526 }
4527 }
4528 mBeaconMuted = mute;
4529 return maxLatency;
4530 }
4531 return 0;
4532}
4533
Eric Laurente0720872014-03-11 09:30:41 -07004534audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004535 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004536{
Paul McLeanaa981192015-03-21 09:55:15 -07004537 // Routing
4538 // see if we have an explicit route
4539 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4540 // (getStrategy(stream)).
4541 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4542 // and activity count is non-zero
4543 // the device = the device from the descriptor in the RouteMap, and exit.
4544 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4545 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004546 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4547 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004548 return route->mDeviceDescriptor->type();
4549 }
4550 }
4551
Eric Laurente552edb2014-03-10 17:42:56 -07004552 if (fromCache) {
4553 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4554 strategy, mDeviceForStrategy[strategy]);
4555 return mDeviceForStrategy[strategy];
4556 }
François Gaffie2110e042015-03-24 08:41:51 +01004557 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004558}
4559
Eric Laurente0720872014-03-11 09:30:41 -07004560void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004561{
4562 for (int i = 0; i < NUM_STRATEGIES; i++) {
4563 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4564 }
4565 mPreviousOutputs = mOutputs;
4566}
4567
Eric Laurent1f2f2232014-06-02 12:01:23 -07004568uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004569 audio_devices_t prevDevice,
4570 uint32_t delayMs)
4571{
4572 // mute/unmute strategies using an incompatible device combination
4573 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4574 // if unmuting, unmute only after the specified delay
4575 if (outputDesc->isDuplicated()) {
4576 return 0;
4577 }
4578
4579 uint32_t muteWaitMs = 0;
4580 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004581 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004582
4583 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4584 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004585 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004586 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4587 bool doMute = false;
4588
4589 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4590 doMute = true;
4591 outputDesc->mStrategyMutedByDevice[i] = true;
4592 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4593 doMute = true;
4594 outputDesc->mStrategyMutedByDevice[i] = false;
4595 }
Eric Laurent99401132014-05-07 19:48:15 -07004596 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004597 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004598 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004599 // skip output if it does not share any device with current output
4600 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4601 == AUDIO_DEVICE_NONE) {
4602 continue;
4603 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004604 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4605 mute ? "muting" : "unmuting", i, curDevice);
4606 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004607 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004608 if (mute) {
4609 // FIXME: should not need to double latency if volume could be applied
4610 // immediately by the audioflinger mixer. We must account for the delay
4611 // between now and the next time the audioflinger thread for this output
4612 // will process a buffer (which corresponds to one buffer size,
4613 // usually 1/2 or 1/4 of the latency).
4614 if (muteWaitMs < desc->latency() * 2) {
4615 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004616 }
4617 }
4618 }
4619 }
4620 }
4621 }
4622
Eric Laurent99401132014-05-07 19:48:15 -07004623 // temporary mute output if device selection changes to avoid volume bursts due to
4624 // different per device volumes
4625 if (outputDesc->isActive() && (device != prevDevice)) {
4626 if (muteWaitMs < outputDesc->latency() * 2) {
4627 muteWaitMs = outputDesc->latency() * 2;
4628 }
4629 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004630 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004631 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004632 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004633 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004634 muteWaitMs *2, device);
4635 }
4636 }
4637 }
4638
Eric Laurente552edb2014-03-10 17:42:56 -07004639 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4640 if (muteWaitMs > delayMs) {
4641 muteWaitMs -= delayMs;
4642 usleep(muteWaitMs * 1000);
4643 return muteWaitMs;
4644 }
4645 return 0;
4646}
4647
Eric Laurentc75307b2015-03-17 15:29:32 -07004648uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004649 audio_devices_t device,
4650 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004651 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004652 audio_patch_handle_t *patchHandle,
4653 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004654{
Eric Laurentc75307b2015-03-17 15:29:32 -07004655 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004656 AudioParameter param;
4657 uint32_t muteWaitMs;
4658
4659 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004660 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4661 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004662 return muteWaitMs;
4663 }
4664 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4665 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004666 if ((device != AUDIO_DEVICE_NONE) &&
4667 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004668 return 0;
4669 }
4670
4671 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004672 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004673
4674 audio_devices_t prevDevice = outputDesc->mDevice;
4675
Paul McLeanaa981192015-03-21 09:55:15 -07004676 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004677
4678 if (device != AUDIO_DEVICE_NONE) {
4679 outputDesc->mDevice = device;
4680 }
4681 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4682
4683 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004684 // the requested device is AUDIO_DEVICE_NONE
4685 // OR the requested device is the same as current device
4686 // AND force is not specified
4687 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004688 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004689 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4690 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004691 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004692 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004693 return muteWaitMs;
4694 }
4695
4696 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004697
Eric Laurente552edb2014-03-10 17:42:56 -07004698 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004699 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004700 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004701 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004702 DeviceVector deviceList;
4703 if ((address == NULL) || (strlen(address) == 0)) {
4704 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4705 } else {
4706 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4707 }
4708
Eric Laurent1c333e22014-05-20 10:48:17 -07004709 if (!deviceList.isEmpty()) {
4710 struct audio_patch patch;
4711 outputDesc->toAudioPortConfig(&patch.sources[0]);
4712 patch.num_sources = 1;
4713 patch.num_sinks = 0;
4714 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4715 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004716 patch.num_sinks++;
4717 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004718 ssize_t index;
4719 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4720 index = mAudioPatches.indexOfKey(*patchHandle);
4721 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004722 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004723 }
4724 sp< AudioPatch> patchDesc;
4725 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4726 if (index >= 0) {
4727 patchDesc = mAudioPatches.valueAt(index);
4728 afPatchHandle = patchDesc->mAfPatchHandle;
4729 }
4730
Eric Laurent1c333e22014-05-20 10:48:17 -07004731 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004732 &afPatchHandle,
4733 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4735 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004736 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004737 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004738 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004739 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004740 addAudioPatch(patchDesc->mHandle, patchDesc);
4741 } else {
4742 patchDesc->mPatch = patch;
4743 }
4744 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004745 if (patchHandle) {
4746 *patchHandle = patchDesc->mHandle;
4747 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004748 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004749 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004750 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004751 }
4752 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004753
4754 // inform all input as well
4755 for (size_t i = 0; i < mInputs.size(); i++) {
4756 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004757 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004758 AudioParameter inputCmd = AudioParameter();
4759 ALOGV("%s: inform input %d of device:%d", __func__,
4760 inputDescriptor->mIoHandle, device);
4761 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4762 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4763 inputCmd.toString(),
4764 delayMs);
4765 }
4766 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004767 }
Eric Laurente552edb2014-03-10 17:42:56 -07004768
4769 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004770 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004771
4772 return muteWaitMs;
4773}
4774
Eric Laurentc75307b2015-03-17 15:29:32 -07004775status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004776 int delayMs,
4777 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004778{
Eric Laurent6a94d692014-05-20 11:18:06 -07004779 ssize_t index;
4780 if (patchHandle) {
4781 index = mAudioPatches.indexOfKey(*patchHandle);
4782 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004783 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004784 }
4785 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004786 return INVALID_OPERATION;
4787 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004788 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4789 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004790 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004791 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004792 removeAudioPatch(patchDesc->mHandle);
4793 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004794 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004795 return status;
4796}
4797
4798status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4799 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004800 bool force,
4801 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004802{
4803 status_t status = NO_ERROR;
4804
Eric Laurent1f2f2232014-06-02 12:01:23 -07004805 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004806 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4807 inputDesc->mDevice = device;
4808
4809 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4810 if (!deviceList.isEmpty()) {
4811 struct audio_patch patch;
4812 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004813 // AUDIO_SOURCE_HOTWORD is for internal use only:
4814 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004815 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004816 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004817 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4818 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004819 patch.num_sinks = 1;
4820 //only one input device for now
4821 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004822 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004823 ssize_t index;
4824 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4825 index = mAudioPatches.indexOfKey(*patchHandle);
4826 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004827 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004828 }
4829 sp< AudioPatch> patchDesc;
4830 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4831 if (index >= 0) {
4832 patchDesc = mAudioPatches.valueAt(index);
4833 afPatchHandle = patchDesc->mAfPatchHandle;
4834 }
4835
Eric Laurent1c333e22014-05-20 10:48:17 -07004836 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004837 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004838 0);
4839 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004840 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004841 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004842 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004843 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004844 addAudioPatch(patchDesc->mHandle, patchDesc);
4845 } else {
4846 patchDesc->mPatch = patch;
4847 }
4848 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004849 if (patchHandle) {
4850 *patchHandle = patchDesc->mHandle;
4851 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004852 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004853 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004854 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004855 }
4856 }
4857 }
4858 return status;
4859}
4860
Eric Laurent6a94d692014-05-20 11:18:06 -07004861status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4862 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004863{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004864 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004865 ssize_t index;
4866 if (patchHandle) {
4867 index = mAudioPatches.indexOfKey(*patchHandle);
4868 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004869 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004870 }
4871 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004872 return INVALID_OPERATION;
4873 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004874 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4875 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004876 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004877 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004878 removeAudioPatch(patchDesc->mHandle);
4879 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004880 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004881 return status;
4882}
4883
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004884sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004885 String8 address,
4886 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004887 audio_format_t& format,
4888 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004889 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004890{
4891 // Choose an input profile based on the requested capture parameters: select the first available
4892 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004893 //
4894 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4895 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004896
4897 for (size_t i = 0; i < mHwModules.size(); i++)
4898 {
4899 if (mHwModules[i]->mHandle == 0) {
4900 continue;
4901 }
4902 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4903 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004904 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004905 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004906 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004907 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004908 format,
4909 &format /*updatedFormat*/,
4910 channelMask,
4911 &channelMask /*updatedChannelMask*/,
4912 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004913
Eric Laurente552edb2014-03-10 17:42:56 -07004914 return profile;
4915 }
4916 }
4917 }
4918 return NULL;
4919}
4920
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004921
4922audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004923 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004924{
François Gaffie036e1e92015-03-19 10:16:24 +01004925 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4926 audio_devices_t selectedDeviceFromMix =
4927 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004928
François Gaffie036e1e92015-03-19 10:16:24 +01004929 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4930 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004931 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004932 return getDeviceForInputSource(inputSource);
4933}
4934
4935audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4936{
Paul McLean466dc8e2015-04-17 13:15:36 -06004937 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4938 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004939 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004940 return route->mDeviceDescriptor->type();
4941 }
4942 }
4943
4944 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004945}
4946
Eric Laurente0720872014-03-11 09:30:41 -07004947float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004948 int index,
4949 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004950{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004951 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004952 // if a headset is connected, apply the following rules to ring tones and notifications
4953 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004954 // - always attenuate notifications volume by 6dB
4955 // - attenuate ring tones volume by 6dB unless music is not playing and
4956 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004957 // - if music is playing, always limit the volume to current music volume,
4958 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004959 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004960 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4961 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4962 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4963 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4964 ((stream_strategy == STRATEGY_SONIFICATION)
4965 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004966 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004967 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004968 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004969 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004970 // when the phone is ringing we must consider that music could have been paused just before
4971 // by the music application and behave as if music was active if the last music track was
4972 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004973 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004974 mLimitRingtoneVolume) {
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004975 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004976 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004977 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004978 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4979 musicDevice),
4980 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004981 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4982 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4983 if (volumeDb > minVolDB) {
4984 volumeDb = minVolDB;
4985 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004986 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004987 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4988 stream_strategy != STRATEGY_SONIFICATION) {
4989 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004990 }
4991 }
4992
Eric Laurentffbc80f2015-03-18 18:30:19 -07004993 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004994}
4995
Eric Laurente0720872014-03-11 09:30:41 -07004996status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004997 int index,
4998 const sp<AudioOutputDescriptor>& outputDesc,
4999 audio_devices_t device,
5000 int delayMs,
5001 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005002{
Eric Laurente552edb2014-03-10 17:42:56 -07005003 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005004 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005005 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005006 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005007 return NO_ERROR;
5008 }
François Gaffie2110e042015-03-24 08:41:51 +01005009 audio_policy_forced_cfg_t forceUseForComm =
5010 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005011 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005012 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5013 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005014 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005015 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005016 return INVALID_OPERATION;
5017 }
5018
Eric Laurentc75307b2015-03-17 15:29:32 -07005019 if (device == AUDIO_DEVICE_NONE) {
5020 device = outputDesc->device();
5021 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005022
Eric Laurentffbc80f2015-03-18 18:30:19 -07005023 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005024 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005025 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005026 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005027
Eric Laurentffbc80f2015-03-18 18:30:19 -07005028 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005029
Eric Laurent3b73df72014-03-11 09:06:29 -07005030 if (stream == AUDIO_STREAM_VOICE_CALL ||
5031 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005032 float voiceVolume;
5033 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005034 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005035 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005036 } else {
5037 voiceVolume = 1.0;
5038 }
5039
Eric Laurent18fba842016-03-31 14:41:26 -07005040 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005041 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5042 mLastVoiceVolume = voiceVolume;
5043 }
5044 }
5045
5046 return NO_ERROR;
5047}
5048
Eric Laurentc75307b2015-03-17 15:29:32 -07005049void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5050 audio_devices_t device,
5051 int delayMs,
5052 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005053{
Eric Laurentc75307b2015-03-17 15:29:32 -07005054 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005055
Eric Laurent794fde22016-03-11 09:50:45 -08005056 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005057 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005058 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005059 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005060 device,
5061 delayMs,
5062 force);
5063 }
5064}
5065
Eric Laurente0720872014-03-11 09:30:41 -07005066void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005067 bool on,
5068 const sp<AudioOutputDescriptor>& outputDesc,
5069 int delayMs,
5070 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005071{
Eric Laurent72249d52015-06-08 11:12:15 -07005072 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5073 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005074 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005075 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005076 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005077 }
5078 }
5079}
5080
Eric Laurente0720872014-03-11 09:30:41 -07005081void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005082 bool on,
5083 const sp<AudioOutputDescriptor>& outputDesc,
5084 int delayMs,
5085 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005086{
Eric Laurente552edb2014-03-10 17:42:56 -07005087 if (device == AUDIO_DEVICE_NONE) {
5088 device = outputDesc->device();
5089 }
5090
Eric Laurentc75307b2015-03-17 15:29:32 -07005091 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5092 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005093
5094 if (on) {
5095 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005096 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005097 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005098 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005099 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005100 }
5101 }
5102 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5103 outputDesc->mMuteCount[stream]++;
5104 } else {
5105 if (outputDesc->mMuteCount[stream] == 0) {
5106 ALOGV("setStreamMute() unmuting non muted stream!");
5107 return;
5108 }
5109 if (--outputDesc->mMuteCount[stream] == 0) {
5110 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005111 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005112 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005113 device,
5114 delayMs);
5115 }
5116 }
5117}
5118
Eric Laurente0720872014-03-11 09:30:41 -07005119void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005120 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005121{
Eric Laurent87ffa392015-05-22 10:32:38 -07005122 if(!hasPrimaryOutput()) {
5123 return;
5124 }
5125
Eric Laurente552edb2014-03-10 17:42:56 -07005126 // if the stream pertains to sonification strategy and we are in call we must
5127 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5128 // in the device used for phone strategy and play the tone if the selected device does not
5129 // interfere with the device used for phone strategy
5130 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5131 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005132 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005133 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5134 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005135 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005136 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5137 stream, starting, outputDesc->mDevice, stateChange);
5138 if (outputDesc->mRefCount[stream]) {
5139 int muteCount = 1;
5140 if (stateChange) {
5141 muteCount = outputDesc->mRefCount[stream];
5142 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005143 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005144 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5145 for (int i = 0; i < muteCount; i++) {
5146 setStreamMute(stream, starting, mPrimaryOutput);
5147 }
5148 } else {
5149 ALOGV("handleIncallSonification() high visibility");
5150 if (outputDesc->device() &
5151 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5152 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5153 for (int i = 0; i < muteCount; i++) {
5154 setStreamMute(stream, starting, mPrimaryOutput);
5155 }
5156 }
5157 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005158 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5159 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005160 } else {
5161 mpClientInterface->stopTone();
5162 }
5163 }
5164 }
5165 }
5166}
5167
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005168audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5169{
5170 // flags to stream type mapping
5171 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5172 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5173 }
5174 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5175 return AUDIO_STREAM_BLUETOOTH_SCO;
5176 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005177 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5178 return AUDIO_STREAM_TTS;
5179 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005180
5181 // usage to stream type mapping
5182 switch (attr->usage) {
5183 case AUDIO_USAGE_MEDIA:
5184 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005185 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5186 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005187 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5188 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005189 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5190 return AUDIO_STREAM_SYSTEM;
5191 case AUDIO_USAGE_VOICE_COMMUNICATION:
5192 return AUDIO_STREAM_VOICE_CALL;
5193
5194 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5195 return AUDIO_STREAM_DTMF;
5196
5197 case AUDIO_USAGE_ALARM:
5198 return AUDIO_STREAM_ALARM;
5199 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5200 return AUDIO_STREAM_RING;
5201
5202 case AUDIO_USAGE_NOTIFICATION:
5203 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5204 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5205 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5206 case AUDIO_USAGE_NOTIFICATION_EVENT:
5207 return AUDIO_STREAM_NOTIFICATION;
5208
5209 case AUDIO_USAGE_UNKNOWN:
5210 default:
5211 return AUDIO_STREAM_MUSIC;
5212 }
5213}
Eric Laurente83b55d2014-11-14 10:06:21 -08005214
François Gaffie53615e22015-03-19 09:24:12 +01005215bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5216{
Eric Laurente83b55d2014-11-14 10:06:21 -08005217 // has flags that map to a strategy?
5218 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5219 return true;
5220 }
5221
5222 // has known usage?
5223 switch (paa->usage) {
5224 case AUDIO_USAGE_UNKNOWN:
5225 case AUDIO_USAGE_MEDIA:
5226 case AUDIO_USAGE_VOICE_COMMUNICATION:
5227 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5228 case AUDIO_USAGE_ALARM:
5229 case AUDIO_USAGE_NOTIFICATION:
5230 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5231 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5232 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5233 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5234 case AUDIO_USAGE_NOTIFICATION_EVENT:
5235 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5236 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5237 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5238 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005239 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005240 break;
5241 default:
5242 return false;
5243 }
5244 return true;
5245}
5246
François Gaffiead3183e2015-03-18 16:55:35 +01005247bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5248 routing_strategy strategy, uint32_t inPastMs,
5249 nsecs_t sysTime) const
5250{
5251 if ((sysTime == 0) && (inPastMs != 0)) {
5252 sysTime = systemTime();
5253 }
Eric Laurent794fde22016-03-11 09:50:45 -08005254 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005255 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5256 (NUM_STRATEGIES == strategy)) &&
5257 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5258 return true;
5259 }
5260 }
5261 return false;
5262}
5263
François Gaffie2110e042015-03-24 08:41:51 +01005264audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5265{
5266 return mEngine->getForceUse(usage);
5267}
5268
5269bool AudioPolicyManager::isInCall()
5270{
5271 return isStateInCall(mEngine->getPhoneState());
5272}
5273
5274bool AudioPolicyManager::isStateInCall(int state)
5275{
5276 return is_state_in_call(state);
5277}
5278
Eric Laurentd60560a2015-04-10 11:31:20 -07005279void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5280{
5281 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5282 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5283 if (sourceDesc->mDevice->equals(deviceDesc)) {
5284 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5285 stopAudioSource(sourceDesc->getHandle());
5286 }
5287 }
5288
5289 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5290 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5291 bool release = false;
5292 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5293 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5294 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5295 source->ext.device.type == deviceDesc->type()) {
5296 release = true;
5297 }
5298 }
5299 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5300 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5301 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5302 sink->ext.device.type == deviceDesc->type()) {
5303 release = true;
5304 }
5305 }
5306 if (release) {
5307 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5308 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5309 }
5310 }
5311}
5312
Phil Burk09bc4612016-02-24 15:58:15 -08005313// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005314void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5315 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005316 // TODO Set this based on Config properties.
5317 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005318
5319 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5320 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005321 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005322
5323 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005324 bool supportsAC3 = false;
5325 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005326 bool supportsIEC61937 = false;
5327 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5328 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005329 switch (format) {
5330 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005331 supportsAC3 = true;
5332 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005333 case AUDIO_FORMAT_E_AC3:
5334 case AUDIO_FORMAT_DTS:
5335 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005336 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005337 break;
5338 case AUDIO_FORMAT_IEC61937:
5339 supportsIEC61937 = true;
5340 break;
5341 default:
5342 break;
5343 }
5344 }
Phil Burk09bc4612016-02-24 15:58:15 -08005345
5346 // Modify formats based on surround preferences.
5347 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005348 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5349 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5350 // Remove surround sound related formats.
5351 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5352 audio_format_t format = formats[formatIndex];
5353 switch(format) {
5354 case AUDIO_FORMAT_AC3:
5355 case AUDIO_FORMAT_E_AC3:
5356 case AUDIO_FORMAT_DTS:
5357 case AUDIO_FORMAT_DTS_HD:
5358 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005359 formats.removeAt(formatIndex);
5360 break;
5361 default:
5362 formatIndex++; // keep it
5363 break;
5364 }
Phil Burk09bc4612016-02-24 15:58:15 -08005365 }
Phil Burk07ac1142016-03-25 13:39:29 -07005366 supportsAC3 = false;
5367 supportsOtherSurround = false;
5368 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005369 }
Phil Burk07ac1142016-03-25 13:39:29 -07005370 } else { // AUTO or ALWAYS
5371 // Most TVs support AC3 even if they do not report it in the EDID.
5372 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5373 && !supportsAC3) {
5374 formats.add(AUDIO_FORMAT_AC3);
5375 supportsAC3 = true;
5376 }
5377
5378 // If ALWAYS, add support for raw surround formats if all are missing.
5379 // This assumes that if any of these formats are reported by the HAL
5380 // then the report is valid and should not be modified.
5381 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5382 && !supportsOtherSurround) {
5383 formats.add(AUDIO_FORMAT_E_AC3);
5384 formats.add(AUDIO_FORMAT_DTS);
5385 formats.add(AUDIO_FORMAT_DTS_HD);
5386 supportsOtherSurround = true;
5387 }
5388
5389 // Add support for IEC61937 if any raw surround supported.
5390 // The HAL could do this but add it here, just in case.
5391 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5392 formats.add(AUDIO_FORMAT_IEC61937);
5393 supportsIEC61937 = true;
5394 }
Phil Burk09bc4612016-02-24 15:58:15 -08005395 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005396}
5397
5398// Modify the list of channel masks supported.
5399void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5400 ChannelsVector &channelMasks = *channelMasksPtr;
5401 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5402 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5403
5404 // If NEVER, then remove support for channelMasks > stereo.
5405 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5406 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5407 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5408 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5409 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5410 channelMasks.removeAt(maskIndex);
5411 } else {
5412 maskIndex++;
5413 }
5414 }
5415 // If ALWAYS, then make sure we at least support 5.1
5416 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5417 bool supports5dot1 = false;
5418 // Are there any channel masks that can be considered "surround"?
5419 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5420 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5421 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5422 supports5dot1 = true;
5423 break;
5424 }
5425 }
5426 // If not then add 5.1 support.
5427 if (!supports5dot1) {
5428 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5429 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5430 }
Phil Burk09bc4612016-02-24 15:58:15 -08005431 }
5432}
5433
Phil Burk00eeb322016-03-31 12:41:00 -07005434void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5435 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005436 AudioProfileVector &profiles)
5437{
5438 String8 reply;
5439 char *value;
Phil Burk0709b0a2016-03-31 12:54:57 -07005440
François Gaffie112b0af2015-11-19 16:13:25 +01005441 // Format MUST be checked first to update the list of AudioProfile
5442 if (profiles.hasDynamicFormat()) {
5443 reply = mpClientInterface->getParameters(ioHandle,
5444 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005445 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005446 AudioParameter repliedParameters(reply);
5447 if (repliedParameters.get(
5448 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005449 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5450 return;
5451 }
Phil Burk09bc4612016-02-24 15:58:15 -08005452 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005453 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005454 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005455 }
Phil Burk09bc4612016-02-24 15:58:15 -08005456 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005457 }
5458 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5459
Eric Laurent20eb3a42016-01-26 18:39:17 -08005460 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005461 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005462 ChannelsVector channelMasks;
5463 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005464 AudioParameter requestedParameters;
5465 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5466
5467 if (profiles.hasDynamicRateFor(format)) {
5468 reply = mpClientInterface->getParameters(ioHandle,
5469 requestedParameters.toString() + ";" +
5470 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5471 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005472 AudioParameter repliedParameters(reply);
5473 if (repliedParameters.get(
5474 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5475 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005476 }
5477 }
5478 if (profiles.hasDynamicChannelsFor(format)) {
5479 reply = mpClientInterface->getParameters(ioHandle,
5480 requestedParameters.toString() + ";" +
5481 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5482 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005483 AudioParameter repliedParameters(reply);
5484 if (repliedParameters.get(
5485 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5486 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005487 if (device == AUDIO_DEVICE_OUT_HDMI) {
5488 filterSurroundChannelMasks(&channelMasks);
5489 }
François Gaffie112b0af2015-11-19 16:13:25 +01005490 }
5491 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005492 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005493 }
5494}
Eric Laurentd60560a2015-04-10 11:31:20 -07005495
Eric Laurente552edb2014-03-10 17:42:56 -07005496}; // namespace android