blob: ac4ea876ba8be02f4a7388e874aff7dba1ade421 [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
Eric Laurentdc462862016-07-19 12:29:53 -070052//FIXME: workaround for truncated touch sounds
53// to be removed when the problem is handled by system UI
54#define TOUCH_SOUND_FIXED_DELAY_MS 100
Eric Laurente552edb2014-03-10 17:42:56 -070055// ----------------------------------------------------------------------------
56// AudioPolicyInterface implementation
57// ----------------------------------------------------------------------------
58
Eric Laurente0720872014-03-11 09:30:41 -070059status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080060 audio_policy_dev_state_t state,
61 const char *device_address,
62 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070063{
Paul McLeane743a472015-01-28 11:07:31 -080064 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080065}
66
67status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080068 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080069 const char *device_address,
70 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080071{
Paul McLeane743a472015-01-28 11:07:31 -080072 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
73- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070074
75 // connect/disconnect only 1 device at a time
76 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
77
François Gaffie53615e22015-03-19 09:24:12 +010078 sp<DeviceDescriptor> devDesc =
79 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080080
Eric Laurente552edb2014-03-10 17:42:56 -070081 // handle output devices
82 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070083 SortedVector <audio_io_handle_t> outputs;
84
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
86
Eric Laurente552edb2014-03-10 17:42:56 -070087 // save a copy of the opened output descriptors before any output is opened or closed
88 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
89 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070090 switch (state)
91 {
92 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080093 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070094 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070095 ALOGW("setDeviceConnectionState() device already connected: %x", device);
96 return INVALID_OPERATION;
97 }
98 ALOGV("setDeviceConnectionState() connecting device %x", device);
99
Eric Laurente552edb2014-03-10 17:42:56 -0700100 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700101 index = mAvailableOutputDevices.add(devDesc);
102 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100103 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700104 if (module == 0) {
105 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
106 device);
107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
Paul McLeane743a472015-01-28 11:07:31 -0800110 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700111 } else {
112 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700113 }
114
Eric Laurenta1d525f2015-01-29 13:36:45 -0800115 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700116 mAvailableOutputDevices.remove(devDesc);
117 return INVALID_OPERATION;
118 }
François Gaffie2110e042015-03-24 08:41:51 +0100119 // Propagate device availability to Engine
120 mEngine->setDeviceConnectionState(devDesc, state);
121
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700122 // outputs should never be empty here
123 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
124 "checkOutputsForDevice() returned no outputs but status OK");
125 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
126 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800127
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800128 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800129 AudioParameter param = AudioParameter(devDesc->mAddress);
130 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
131 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
132
133 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700134 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700135 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700136 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700137 ALOGW("setDeviceConnectionState() device not connected: %x", device);
138 return INVALID_OPERATION;
139 }
140
Paul McLean5c477aa2014-08-20 16:47:57 -0700141 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
142
Paul McLeane743a472015-01-28 11:07:31 -0800143 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800144 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700145 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
146 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
147
Eric Laurente552edb2014-03-10 17:42:56 -0700148 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700149 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700150
Eric Laurenta1d525f2015-01-29 13:36:45 -0800151 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100152
153 // Propagate device availability to Engine
154 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700155 } break;
156
157 default:
158 ALOGE("setDeviceConnectionState() invalid state: %x", state);
159 return BAD_VALUE;
160 }
161
Eric Laurent3a4311c2014-03-17 12:00:47 -0700162 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
163 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700164 checkA2dpSuspend();
165 checkOutputForAllStrategies();
166 // outputs must be closed after checkOutputForAllStrategies() is executed
167 if (!outputs.isEmpty()) {
168 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700169 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700170 // close unused outputs after device disconnection or direct outputs that have been
171 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700172 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700173 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
174 (desc->mDirectOpenCount == 0))) {
175 closeOutput(outputs[i]);
176 }
177 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700178 // check again after closing A2DP output to reset mA2dpSuspended if needed
179 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700180 }
181
182 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700183 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700184 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
185 updateCallRouting(newDevice);
186 }
Eric Laurente552edb2014-03-10 17:42:56 -0700187 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700188 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
189 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
190 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700191 // do not force device change on duplicated output because if device is 0, it will
192 // also force a device 0 for the two outputs it is duplicated to which may override
193 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700194 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100195 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700196 // always force when disconnecting (a non-duplicated device)
197 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700198 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700199 }
Eric Laurente552edb2014-03-10 17:42:56 -0700200 }
201
Eric Laurentd60560a2015-04-10 11:31:20 -0700202 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
203 cleanUpForDevice(devDesc);
204 }
205
Eric Laurent72aa32f2014-05-30 18:51:48 -0700206 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700207 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700208 } // end if is output device
209
Eric Laurente552edb2014-03-10 17:42:56 -0700210 // handle input devices
211 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700212 SortedVector <audio_io_handle_t> inputs;
213
Eric Laurent3a4311c2014-03-17 12:00:47 -0700214 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700215 switch (state)
216 {
217 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700218 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700219 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700220 ALOGW("setDeviceConnectionState() device already connected: %d", device);
221 return INVALID_OPERATION;
222 }
François Gaffie53615e22015-03-19 09:24:12 +0100223 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700224 if (module == NULL) {
225 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
226 device);
227 return INVALID_OPERATION;
228 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700229 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700230 return INVALID_OPERATION;
231 }
232
Eric Laurent3a4311c2014-03-17 12:00:47 -0700233 index = mAvailableInputDevices.add(devDesc);
234 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800235 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700236 } else {
237 return NO_MEMORY;
238 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800239
240 // Set connect to HALs
241 AudioParameter param = AudioParameter(devDesc->mAddress);
242 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
243 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
244
François Gaffie2110e042015-03-24 08:41:51 +0100245 // Propagate device availability to Engine
246 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700247 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700248
249 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700250 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700252 ALOGW("setDeviceConnectionState() device not connected: %d", device);
253 return INVALID_OPERATION;
254 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700255
256 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
257
258 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800259 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700260 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
261 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
262
Paul McLean9080a4c2015-06-18 08:24:02 -0700263 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700264 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700265
François Gaffie2110e042015-03-24 08:41:51 +0100266 // Propagate device availability to Engine
267 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700268 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700269
270 default:
271 ALOGE("setDeviceConnectionState() invalid state: %x", state);
272 return BAD_VALUE;
273 }
274
Eric Laurentd4692962014-05-05 18:13:44 -0700275 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700276 // As the input device list can impact the output device selection, update
277 // getDeviceForStrategy() cache
278 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700279
Eric Laurent87ffa392015-05-22 10:32:38 -0700280 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700281 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
282 updateCallRouting(newDevice);
283 }
284
Eric Laurentd60560a2015-04-10 11:31:20 -0700285 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
286 cleanUpForDevice(devDesc);
287 }
288
Eric Laurentb52c1522014-05-20 11:27:36 -0700289 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700290 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700291 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700292
293 ALOGW("setDeviceConnectionState() invalid device: %x", device);
294 return BAD_VALUE;
295}
296
Eric Laurente0720872014-03-11 09:30:41 -0700297audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100298 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700299{
Eric Laurent634b7142016-04-20 13:48:02 -0700300 sp<DeviceDescriptor> devDesc =
301 mHwModules.getDeviceDescriptor(device, device_address, "",
302 (strlen(device_address) != 0)/*matchAddress*/);
303
304 if (devDesc == 0) {
305 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
306 device, device_address);
307 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
308 }
François Gaffie53615e22015-03-19 09:24:12 +0100309
Eric Laurent3a4311c2014-03-17 12:00:47 -0700310 DeviceVector *deviceVector;
311
Eric Laurente552edb2014-03-10 17:42:56 -0700312 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700313 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700314 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700315 deviceVector = &mAvailableInputDevices;
316 } else {
317 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
318 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700319 }
Eric Laurent634b7142016-04-20 13:48:02 -0700320
321 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
322 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800323}
324
Eric Laurentdc462862016-07-19 12:29:53 -0700325uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700326{
327 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700328 status_t status;
329 audio_patch_handle_t afPatchHandle;
330 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700331 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700332
Eric Laurent87ffa392015-05-22 10:32:38 -0700333 if(!hasPrimaryOutput()) {
Eric Laurentdc462862016-07-19 12:29:53 -0700334 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700335 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800336 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700337 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
338
339 // release existing RX patch if any
340 if (mCallRxPatch != 0) {
341 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
342 mCallRxPatch.clear();
343 }
344 // release TX patch if any
345 if (mCallTxPatch != 0) {
346 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
347 mCallTxPatch.clear();
348 }
349
350 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
351 // via setOutputDevice() on primary output.
352 // Otherwise, create two audio patches for TX and RX path.
353 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700354 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 // If the TX device is also on the primary HW module, setOutputDevice() will take care
356 // of it due to legacy implementation. If not, create a patch.
357 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
358 == AUDIO_DEVICE_NONE) {
359 createTxPatch = true;
360 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700361 } else { // create RX path audio patch
362 struct audio_patch patch;
363
364 patch.num_sources = 1;
365 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700366 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
367 ALOG_ASSERT(!deviceList.isEmpty(),
368 "updateCallRouting() selected device not in output device list");
369 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
370 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
371 ALOG_ASSERT(!deviceList.isEmpty(),
372 "updateCallRouting() no telephony RX device");
373 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
374
375 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
376 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
377
378 // request to reuse existing output stream if one is already opened to reach the RX device
379 SortedVector<audio_io_handle_t> outputs =
380 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700381 audio_io_handle_t output = selectOutput(outputs,
382 AUDIO_OUTPUT_FLAG_NONE,
383 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700384 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700385 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700386 ALOG_ASSERT(!outputDesc->isDuplicated(),
387 "updateCallRouting() RX device output is duplicated");
388 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700389 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700390 patch.num_sources = 2;
391 }
392
393 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700394 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700395 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
396 status);
397 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100398 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 mCallRxPatch->mAfPatchHandle = afPatchHandle;
400 mCallRxPatch->mUid = mUidCached;
401 }
402 createTxPatch = true;
403 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700404 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700405 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700406
Eric Laurentc2730ba2014-07-20 15:47:07 -0700407 patch.num_sources = 1;
408 patch.num_sinks = 1;
409 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
410 ALOG_ASSERT(!deviceList.isEmpty(),
411 "updateCallRouting() selected device not in input device list");
412 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
413 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
414 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
415 ALOG_ASSERT(!deviceList.isEmpty(),
416 "updateCallRouting() no telephony TX device");
417 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
418 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
419
420 SortedVector<audio_io_handle_t> outputs =
421 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700422 audio_io_handle_t output = selectOutput(outputs,
423 AUDIO_OUTPUT_FLAG_NONE,
424 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700425 // request to reuse existing output stream if one is already opened to reach the TX
426 // path output device
427 if (output != AUDIO_IO_HANDLE_NONE) {
428 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
429 ALOG_ASSERT(!outputDesc->isDuplicated(),
430 "updateCallRouting() RX device output is duplicated");
431 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700432 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700433 patch.num_sources = 2;
434 }
435
Eric Laurentc0a889f2015-10-14 14:36:34 -0700436 // terminate active capture if on the same HW module as the call TX source device
437 // FIXME: would be better to refine to only inputs whose profile connects to the
438 // call TX device but this information is not in the audio patch and logic here must be
439 // symmetric to the one in startInput()
Eric Laurent232f26f2016-02-17 18:06:27 -0800440 audio_io_handle_t activeInput = mInputs.getActiveInput();
441 if (activeInput != 0) {
442 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
443 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
444 //FIXME: consider all active sessions
445 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
446 audio_session_t activeSession = activeSessions.keyAt(0);
447 stopInput(activeInput, activeSession);
448 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700449 }
450 }
451
Eric Laurentc2730ba2014-07-20 15:47:07 -0700452 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700453 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700454 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
455 status);
456 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100457 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700458 mCallTxPatch->mAfPatchHandle = afPatchHandle;
459 mCallTxPatch->mUid = mUidCached;
460 }
461 }
Eric Laurentdc462862016-07-19 12:29:53 -0700462
463 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700464}
465
Eric Laurente0720872014-03-11 09:30:41 -0700466void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700467{
468 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100469 // store previous phone state for management of sonification strategy below
470 int oldState = mEngine->getPhoneState();
471
472 if (mEngine->setPhoneState(state) != NO_ERROR) {
473 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700474 return;
475 }
François Gaffie2110e042015-03-24 08:41:51 +0100476 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700477 // if leaving call state, handle special case of active streams
478 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700479 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700480 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800481 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700482 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700483 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800484
Eric Laurent63dea1d2015-07-02 17:10:28 -0700485 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800486 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700487 }
488
François Gaffie2110e042015-03-24 08:41:51 +0100489 /**
490 * Switching to or from incall state or switching between telephony and VoIP lead to force
491 * routing command.
492 */
493 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
494 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700495
496 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700497 checkA2dpSuspend();
498 checkOutputForAllStrategies();
499 updateDevicesAndOutputs();
500
Eric Laurente552edb2014-03-10 17:42:56 -0700501 int delayMs = 0;
502 if (isStateInCall(state)) {
503 nsecs_t sysTime = systemTime();
504 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700505 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700506 // mute media and sonification strategies and delay device switch by the largest
507 // latency of any output where either strategy is active.
508 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100509 if ((isStrategyActive(desc, STRATEGY_MEDIA,
510 SONIFICATION_HEADSET_MUSIC_DELAY,
511 sysTime) ||
512 isStrategyActive(desc, STRATEGY_SONIFICATION,
513 SONIFICATION_HEADSET_MUSIC_DELAY,
514 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700515 (delayMs < (int)desc->latency()*2)) {
516 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700517 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700518 setStrategyMute(STRATEGY_MEDIA, true, desc);
519 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700520 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700521 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
522 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700523 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
524 }
525 }
526
Eric Laurent87ffa392015-05-22 10:32:38 -0700527 if (hasPrimaryOutput()) {
528 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
529 // the device returned is not necessarily reachable via this output
530 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
531 // force routing command to audio hardware when ending call
532 // even if no device change is needed
533 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
534 rxDevice = mPrimaryOutput->device();
535 }
Eric Laurente552edb2014-03-10 17:42:56 -0700536
Eric Laurent87ffa392015-05-22 10:32:38 -0700537 if (state == AUDIO_MODE_IN_CALL) {
538 updateCallRouting(rxDevice, delayMs);
539 } else if (oldState == AUDIO_MODE_IN_CALL) {
540 if (mCallRxPatch != 0) {
541 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
542 mCallRxPatch.clear();
543 }
544 if (mCallTxPatch != 0) {
545 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
546 mCallTxPatch.clear();
547 }
548 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
549 } else {
550 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700551 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700552 }
Eric Laurente552edb2014-03-10 17:42:56 -0700553 // if entering in call state, handle special case of active streams
554 // pertaining to sonification strategy see handleIncallSonification()
555 if (isStateInCall(state)) {
556 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800557 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700558 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700559 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700560
561 // force reevaluating accessibility routing when call starts
562 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700563 }
564
565 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700566 if (state == AUDIO_MODE_RINGTONE &&
567 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700568 mLimitRingtoneVolume = true;
569 } else {
570 mLimitRingtoneVolume = false;
571 }
572}
573
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700574audio_mode_t AudioPolicyManager::getPhoneState() {
575 return mEngine->getPhoneState();
576}
577
Eric Laurente0720872014-03-11 09:30:41 -0700578void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700579 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700580{
François Gaffie2110e042015-03-24 08:41:51 +0100581 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700582
François Gaffie2110e042015-03-24 08:41:51 +0100583 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
584 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
585 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700586 }
François Gaffie2110e042015-03-24 08:41:51 +0100587 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
588 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
589 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700590
591 // check for device and output changes triggered by new force usage
592 checkA2dpSuspend();
593 checkOutputForAllStrategies();
594 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800595
Eric Laurentdc462862016-07-19 12:29:53 -0700596 //FIXME: workaround for truncated touch sounds
597 // to be removed when the problem is handled by system UI
598 uint32_t delayMs = 0;
599 uint32_t waitMs = 0;
600 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
601 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
602 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700603 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700604 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700605 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700606 }
Eric Laurente552edb2014-03-10 17:42:56 -0700607 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700608 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
609 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
610 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700611 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
612 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700613 }
Eric Laurente552edb2014-03-10 17:42:56 -0700614 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700615 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700616 }
617 }
618
Eric Laurent232f26f2016-02-17 18:06:27 -0800619 audio_io_handle_t activeInput = mInputs.getActiveInput();
620 if (activeInput != 0) {
621 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
622 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700623 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800624 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
625 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700626 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800627 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700628 }
Eric Laurente552edb2014-03-10 17:42:56 -0700629 }
Eric Laurente552edb2014-03-10 17:42:56 -0700630}
631
Eric Laurente0720872014-03-11 09:30:41 -0700632void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700633{
634 ALOGV("setSystemProperty() property %s, value %s", property, value);
635}
636
637// Find a direct output profile compatible with the parameters passed, even if the input flags do
638// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800639sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700640 audio_devices_t device,
641 uint32_t samplingRate,
642 audio_format_t format,
643 audio_channel_mask_t channelMask,
644 audio_output_flags_t flags)
645{
Eric Laurent861a6282015-05-18 15:40:16 -0700646 // only retain flags that will drive the direct output profile selection
647 // if explicitly requested
648 static const uint32_t kRelevantFlags =
649 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
650 flags =
651 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
652
653 sp<IOProfile> profile;
654
Eric Laurente552edb2014-03-10 17:42:56 -0700655 for (size_t i = 0; i < mHwModules.size(); i++) {
656 if (mHwModules[i]->mHandle == 0) {
657 continue;
658 }
659 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700660 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
661 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700662 samplingRate, NULL /*updatedSamplingRate*/,
663 format, NULL /*updatedFormat*/,
664 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700665 flags)) {
666 continue;
667 }
668 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100669 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700670 continue;
671 }
672 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100673 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700674 continue;
675 }
676 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100677 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700678 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700679 }
Eric Laurente552edb2014-03-10 17:42:56 -0700680 }
681 }
Eric Laurent861a6282015-05-18 15:40:16 -0700682 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700683}
684
Eric Laurente0720872014-03-11 09:30:41 -0700685audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100686 uint32_t samplingRate,
687 audio_format_t format,
688 audio_channel_mask_t channelMask,
689 audio_output_flags_t flags,
690 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700691{
Eric Laurent3b73df72014-03-11 09:06:29 -0700692 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700693 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
694 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
695 device, stream, samplingRate, format, channelMask, flags);
696
Eric Laurente83b55d2014-11-14 10:06:21 -0800697 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
698 stream, samplingRate,format, channelMask,
699 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700700}
701
Eric Laurente83b55d2014-11-14 10:06:21 -0800702status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
703 audio_io_handle_t *output,
704 audio_session_t session,
705 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700706 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800707 uint32_t samplingRate,
708 audio_format_t format,
709 audio_channel_mask_t channelMask,
710 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700711 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800712 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700713{
Eric Laurente83b55d2014-11-14 10:06:21 -0800714 audio_attributes_t attributes;
715 if (attr != NULL) {
716 if (!isValidAttributes(attr)) {
717 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
718 attr->usage, attr->content_type, attr->flags,
719 attr->tags);
720 return BAD_VALUE;
721 }
722 attributes = *attr;
723 } else {
724 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
725 ALOGE("getOutputForAttr(): invalid stream type");
726 return BAD_VALUE;
727 }
728 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700729 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700730 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800731 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100732 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800733 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100734 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800735 }
François Gaffie036e1e92015-03-19 10:16:24 +0100736 *stream = streamTypefromAttributesInt(&attributes);
737 *output = desc->mIoHandle;
738 ALOGV("getOutputForAttr() returns output %d", *output);
739 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800740 }
741 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
742 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
743 return BAD_VALUE;
744 }
745
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700746 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
747 " session %d selectedDeviceId %d",
748 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
749 session, selectedDeviceId);
750
751 *stream = streamTypefromAttributesInt(&attributes);
752
753 // Explicit routing?
754 sp<DeviceDescriptor> deviceDesc;
755 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
756 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
757 deviceDesc = mAvailableOutputDevices[i];
758 break;
759 }
760 }
761 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700762
Eric Laurente83b55d2014-11-14 10:06:21 -0800763 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700764 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700765
Eric Laurente83b55d2014-11-14 10:06:21 -0800766 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700767 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
768 }
769
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700770 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700771 device, samplingRate, format, channelMask, flags);
772
Eric Laurente83b55d2014-11-14 10:06:21 -0800773 *output = getOutputForDevice(device, session, *stream,
774 samplingRate, format, channelMask,
775 flags, offloadInfo);
776 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700777 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800778 return INVALID_OPERATION;
779 }
Paul McLeanaa981192015-03-21 09:55:15 -0700780
Eric Laurente83b55d2014-11-14 10:06:21 -0800781 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700782}
783
784audio_io_handle_t AudioPolicyManager::getOutputForDevice(
785 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800786 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700787 audio_stream_type_t stream,
788 uint32_t samplingRate,
789 audio_format_t format,
790 audio_channel_mask_t channelMask,
791 audio_output_flags_t flags,
792 const audio_offload_info_t *offloadInfo)
793{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700794 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700795 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700796
Eric Laurente552edb2014-03-10 17:42:56 -0700797#ifdef AUDIO_POLICY_TEST
798 if (mCurOutput != 0) {
799 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
800 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
801
802 if (mTestOutputs[mCurOutput] == 0) {
803 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700804 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
805 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700806 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700807 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700808 outputDesc->mFlags =
809 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700810 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700811 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
812 config.sample_rate = mTestSamplingRate;
813 config.channel_mask = mTestChannels;
814 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700815 if (offloadInfo != NULL) {
816 config.offload_info = *offloadInfo;
817 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700818 status = mpClientInterface->openOutput(0,
819 &mTestOutputs[mCurOutput],
820 &config,
821 &outputDesc->mDevice,
822 String8(""),
823 &outputDesc->mLatency,
824 outputDesc->mFlags);
825 if (status == NO_ERROR) {
826 outputDesc->mSamplingRate = config.sample_rate;
827 outputDesc->mFormat = config.format;
828 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700829 AudioParameter outputCmd = AudioParameter();
830 outputCmd.addInt(String8("set_id"),mCurOutput);
831 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
832 addOutput(mTestOutputs[mCurOutput], outputDesc);
833 }
834 }
835 return mTestOutputs[mCurOutput];
836 }
837#endif //AUDIO_POLICY_TEST
838
839 // open a direct output if required by specified parameters
840 //force direct flag if offload flag is set: offloading implies a direct output stream
841 // and all common behaviors are driven by checking only the direct flag
842 // this should normally be set appropriately in the policy configuration file
843 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700844 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700845 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700846 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
847 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
848 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800849 // only allow deep buffering for music stream type
850 if (stream != AUDIO_STREAM_MUSIC) {
851 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700852 } else if (/* stream == AUDIO_STREAM_MUSIC && */
853 flags == AUDIO_OUTPUT_FLAG_NONE &&
854 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
855 // use DEEP_BUFFER as default output for music stream type
856 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800857 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700858 if (stream == AUDIO_STREAM_TTS) {
859 flags = AUDIO_OUTPUT_FLAG_TTS;
860 }
Eric Laurente552edb2014-03-10 17:42:56 -0700861
Eric Laurentb732cf52014-09-24 19:08:21 -0700862 sp<IOProfile> profile;
863
864 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700865 // and not explicitly requested
866 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800867 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700868 audio_channel_count_from_out_mask(channelMask) <= 2) {
869 goto non_direct_output;
870 }
871
Andy Hung2ddee192015-12-18 17:34:44 -0800872 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
873 // This prevents creating an offloaded track and tearing it down immediately after start
874 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700875 // FIXME: We should check the audio session here but we do not have it in this context.
876 // This may prevent offloading in rare situations where effects are left active by apps
877 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700878
Eric Laurente552edb2014-03-10 17:42:56 -0700879 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800880 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700881 profile = getProfileForDirectOutput(device,
882 samplingRate,
883 format,
884 channelMask,
885 (audio_output_flags_t)flags);
886 }
887
Eric Laurent1c333e22014-05-20 10:48:17 -0700888 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700889 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700890
891 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700892 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700893 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
894 outputDesc = desc;
895 // reuse direct output if currently open and configured with same parameters
896 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800897 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700898 (channelMask == outputDesc->mChannelMask)) {
899 outputDesc->mDirectOpenCount++;
900 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
901 return mOutputs.keyAt(i);
902 }
903 }
904 }
905 // close direct output if currently open and configured with different parameters
906 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700907 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700908 }
Eric Laurent861a6282015-05-18 15:40:16 -0700909
910 // if the selected profile is offloaded and no offload info was specified,
911 // create a default one
912 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100913 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700914 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
915 defaultOffloadInfo.sample_rate = samplingRate;
916 defaultOffloadInfo.channel_mask = channelMask;
917 defaultOffloadInfo.format = format;
918 defaultOffloadInfo.stream_type = stream;
919 defaultOffloadInfo.bit_rate = 0;
920 defaultOffloadInfo.duration_us = -1;
921 defaultOffloadInfo.has_video = true; // conservative
922 defaultOffloadInfo.is_streaming = true; // likely
923 offloadInfo = &defaultOffloadInfo;
924 }
925
Eric Laurentc75307b2015-03-17 15:29:32 -0700926 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700927 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700928 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700929 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700930 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
931 config.sample_rate = samplingRate;
932 config.channel_mask = channelMask;
933 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700934 if (offloadInfo != NULL) {
935 config.offload_info = *offloadInfo;
936 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700937 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700938 &output,
939 &config,
940 &outputDesc->mDevice,
941 String8(""),
942 &outputDesc->mLatency,
943 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700944
945 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700946 if (status != NO_ERROR ||
947 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800948 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700949 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700950 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
951 "format %d %d, channelMask %04x %04x", output, samplingRate,
952 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
953 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700954 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700955 mpClientInterface->closeOutput(output);
956 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800957 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800958 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800959 goto non_direct_output;
960 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700961 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700962 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700963 outputDesc->mSamplingRate = config.sample_rate;
964 outputDesc->mChannelMask = config.channel_mask;
965 outputDesc->mFormat = config.format;
966 outputDesc->mRefCount[stream] = 0;
967 outputDesc->mStopTime[stream] = 0;
968 outputDesc->mDirectOpenCount = 1;
969
Eric Laurente552edb2014-03-10 17:42:56 -0700970 audio_io_handle_t srcOutput = getOutputForEffect();
971 addOutput(output, outputDesc);
972 audio_io_handle_t dstOutput = getOutputForEffect();
973 if (dstOutput == output) {
974 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
975 }
976 mPreviousOutputs = mOutputs;
977 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700978 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700979 return output;
980 }
981
Eric Laurentb732cf52014-09-24 19:08:21 -0700982non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700983
984 // A request for HW A/V sync cannot fallback to a mixed output because time
985 // stamps are embedded in audio data
986 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
987 return AUDIO_IO_HANDLE_NONE;
988 }
989
Eric Laurente552edb2014-03-10 17:42:56 -0700990 // ignoring channel mask due to downmix capability in mixer
991
992 // open a non direct output
993
994 // for non direct outputs, only PCM is supported
995 if (audio_is_linear_pcm(format)) {
996 // get which output is suitable for the specified stream. The actual
997 // routing change will happen when startOutput() will be called
998 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
999
Eric Laurent8838a382014-09-08 16:44:28 -07001000 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1001 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1002 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001003 }
1004 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1005 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1006
Paul McLeanaa981192015-03-21 09:55:15 -07001007 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001008
1009 return output;
1010}
1011
Eric Laurente0720872014-03-11 09:30:41 -07001012audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001013 audio_output_flags_t flags,
1014 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001015{
1016 // select one output among several that provide a path to a particular device or set of
1017 // devices (the list was previously build by getOutputsForDevice()).
1018 // The priority is as follows:
1019 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001020 // 2: the output with the bit depth the closest to the requested one
1021 // 3: the primary output
1022 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001023
1024 if (outputs.size() == 0) {
1025 return 0;
1026 }
1027 if (outputs.size() == 1) {
1028 return outputs[0];
1029 }
1030
1031 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001032 audio_io_handle_t outputForFlags = 0;
1033 audio_io_handle_t outputForPrimary = 0;
1034 audio_io_handle_t outputForFormat = 0;
1035 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1036 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001037
1038 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001039 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001040 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001041 // if a valid format is specified, skip output if not compatible
1042 if (format != AUDIO_FORMAT_INVALID) {
1043 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001044 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001045 continue;
1046 }
1047 } else if (!audio_is_linear_pcm(format)) {
1048 continue;
1049 }
Eric Laurente6930022016-02-11 10:20:40 -08001050 if (AudioPort::isBetterFormatMatch(
1051 outputDesc->mFormat, bestFormat, format)) {
1052 outputForFormat = outputs[i];
1053 bestFormat = outputDesc->mFormat;
1054 }
Eric Laurent8838a382014-09-08 16:44:28 -07001055 }
1056
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001057 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001058 if (commonFlags >= maxCommonFlags) {
1059 if (commonFlags == maxCommonFlags) {
1060 if (AudioPort::isBetterFormatMatch(
1061 outputDesc->mFormat, bestFormatForFlags, format)) {
1062 outputForFlags = outputs[i];
1063 bestFormatForFlags = outputDesc->mFormat;
1064 }
1065 } else {
1066 outputForFlags = outputs[i];
1067 maxCommonFlags = commonFlags;
1068 bestFormatForFlags = outputDesc->mFormat;
1069 }
Eric Laurente552edb2014-03-10 17:42:56 -07001070 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1071 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001072 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001073 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001074 }
1075 }
1076 }
1077
Eric Laurente6930022016-02-11 10:20:40 -08001078 if (outputForFlags != 0) {
1079 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001080 }
Eric Laurente6930022016-02-11 10:20:40 -08001081 if (outputForFormat != 0) {
1082 return outputForFormat;
1083 }
1084 if (outputForPrimary != 0) {
1085 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001086 }
1087
1088 return outputs[0];
1089}
1090
Eric Laurente0720872014-03-11 09:30:41 -07001091status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001092 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001093 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001094{
Paul McLeanaa981192015-03-21 09:55:15 -07001095 ALOGV("startOutput() output %d, stream %d, session %d",
1096 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001097 ssize_t index = mOutputs.indexOfKey(output);
1098 if (index < 0) {
1099 ALOGW("startOutput() unknown output %d", output);
1100 return BAD_VALUE;
1101 }
1102
Eric Laurentc75307b2015-03-17 15:29:32 -07001103 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1104
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001105 // Routing?
1106 mOutputRoutes.incRouteActivity(session);
1107
Eric Laurentc75307b2015-03-17 15:29:32 -07001108 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001109 AudioMix *policyMix = NULL;
1110 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001111 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001112 policyMix = outputDesc->mPolicyMix;
1113 address = policyMix->mDeviceAddress.string();
1114 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1115 newDevice = policyMix->mDeviceType;
1116 } else {
1117 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1118 }
Eric Laurent493404d2015-04-21 15:07:36 -07001119 } else if (mOutputRoutes.hasRouteChanged(session)) {
1120 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001121 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001122 } else {
1123 newDevice = AUDIO_DEVICE_NONE;
1124 }
1125
1126 uint32_t delayMs = 0;
1127
Eric Laurentc40d9692016-04-13 19:14:13 -07001128 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001129
1130 if (status != NO_ERROR) {
1131 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001132 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001133 }
1134 // Automatically enable the remote submix input when output is started on a re routing mix
1135 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001136 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1137 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001138 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1139 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001140 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001141 "remote-submix");
1142 }
1143
1144 if (delayMs != 0) {
1145 usleep(delayMs * 1000);
1146 }
1147
1148 return status;
1149}
1150
1151status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1152 audio_stream_type_t stream,
1153 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001154 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001155 uint32_t *delayMs)
1156{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001157 // cannot start playback of STREAM_TTS if any other output is being used
1158 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001159
1160 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001161 if (stream == AUDIO_STREAM_TTS) {
1162 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001163 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001164 return INVALID_OPERATION;
1165 } else {
1166 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1167 }
1168 } else {
1169 // some playback other than beacon starts
1170 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1171 }
1172
Eric Laurent77305a62016-07-25 16:39:22 -07001173 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001174 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001175 bool force = !outputDesc->isActive() &&
1176 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001177
Eric Laurente552edb2014-03-10 17:42:56 -07001178 // increment usage count for this stream on the requested output:
1179 // NOTE that the usage count is the same for duplicated output and hardware output which is
1180 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1181 outputDesc->changeRefCount(stream, 1);
1182
Eric Laurent493404d2015-04-21 15:07:36 -07001183 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001184 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001185 if (device == AUDIO_DEVICE_NONE) {
1186 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001187 }
Eric Laurente552edb2014-03-10 17:42:56 -07001188 routing_strategy strategy = getStrategy(stream);
1189 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001190 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1191 (beaconMuteLatency > 0);
1192 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001193 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001194 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001195 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001196 // force a device change if any other output is:
1197 // - managed by the same hw module
1198 // - has a current device selection that differs from selected device.
1199 // - supports currently selected device
1200 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001201 // In this case, the audio HAL must receive the new device selection so that it can
1202 // change the device currently selected by the other active output.
1203 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001204 desc->device() != device &&
1205 desc->supportedDevices() & device &&
1206 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001207 force = true;
1208 }
1209 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001210 // a notification so that audio focus effect can propagate, or that a mute/unmute
1211 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001212 uint32_t latency = desc->latency();
1213 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1214 waitMs = latency;
1215 }
1216 }
1217 }
Eric Laurentdc462862016-07-19 12:29:53 -07001218 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001219
1220 // handle special case for sonification while in call
1221 if (isInCall()) {
1222 handleIncallSonification(stream, true, false);
1223 }
1224
1225 // apply volume rules for current stream and device if necessary
1226 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001227 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001228 outputDesc,
1229 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001230
1231 // update the outputs if starting an output with a stream that can affect notification
1232 // routing
1233 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001234
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001235 // force reevaluating accessibility routing when ringtone or alarm starts
1236 if (strategy == STRATEGY_SONIFICATION) {
1237 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1238 }
Eric Laurentdc462862016-07-19 12:29:53 -07001239
1240 if (waitMs > muteWaitMs) {
1241 *delayMs = waitMs - muteWaitMs;
1242 }
Eric Laurente552edb2014-03-10 17:42:56 -07001243 }
Eric Laurentdc462862016-07-19 12:29:53 -07001244
Eric Laurente552edb2014-03-10 17:42:56 -07001245 return NO_ERROR;
1246}
1247
1248
Eric Laurente0720872014-03-11 09:30:41 -07001249status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001250 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001251 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001252{
1253 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1254 ssize_t index = mOutputs.indexOfKey(output);
1255 if (index < 0) {
1256 ALOGW("stopOutput() unknown output %d", output);
1257 return BAD_VALUE;
1258 }
1259
Eric Laurentc75307b2015-03-17 15:29:32 -07001260 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001261
Eric Laurentc75307b2015-03-17 15:29:32 -07001262 if (outputDesc->mRefCount[stream] == 1) {
1263 // Automatically disable the remote submix input when output is stopped on a
1264 // re routing mix of type MIX_TYPE_RECORDERS
1265 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1266 outputDesc->mPolicyMix != NULL &&
1267 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1268 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1269 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001270 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001271 "remote-submix");
1272 }
1273 }
1274
1275 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001276 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001277 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001278 int activityCount = mOutputRoutes.decRouteActivity(session);
1279 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1280
1281 if (forceDeviceUpdate) {
1282 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1283 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001284 }
1285
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001286 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001287}
1288
1289status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001290 audio_stream_type_t stream,
1291 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001292{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001293 // always handle stream stop, check which stream type is stopping
1294 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1295
Eric Laurente552edb2014-03-10 17:42:56 -07001296 // handle special case for sonification while in call
1297 if (isInCall()) {
1298 handleIncallSonification(stream, false, false);
1299 }
1300
1301 if (outputDesc->mRefCount[stream] > 0) {
1302 // decrement usage count of this stream on the output
1303 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001304
Eric Laurente552edb2014-03-10 17:42:56 -07001305 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001306 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001307 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001308 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001309 // delay the device switch by twice the latency because stopOutput() is executed when
1310 // the track stop() command is received and at that time the audio track buffer can
1311 // still contain data that needs to be drained. The latency only covers the audio HAL
1312 // and kernel buffers. Also the latency does not always include additional delay in the
1313 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001314 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001315
1316 // force restoring the device selection on other active outputs if it differs from the
1317 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001318 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001319 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001320 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001321 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001322 desc->isActive() &&
1323 outputDesc->sharesHwModuleWith(desc) &&
1324 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001325 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1326 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001327 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001328 newDevice2,
1329 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001330 delayMs);
1331 // re-apply device specific volume if not done by setOutputDevice()
1332 if (!force) {
1333 applyStreamVolumes(desc, newDevice2, delayMs);
1334 }
Eric Laurente552edb2014-03-10 17:42:56 -07001335 }
1336 }
1337 // update the outputs if stopping one with a stream that can affect notification routing
1338 handleNotificationRoutingForStream(stream);
1339 }
1340 return NO_ERROR;
1341 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001342 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001343 return INVALID_OPERATION;
1344 }
1345}
1346
Eric Laurente83b55d2014-11-14 10:06:21 -08001347void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001348 audio_stream_type_t stream __unused,
1349 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001350{
1351 ALOGV("releaseOutput() %d", output);
1352 ssize_t index = mOutputs.indexOfKey(output);
1353 if (index < 0) {
1354 ALOGW("releaseOutput() releasing unknown output %d", output);
1355 return;
1356 }
1357
1358#ifdef AUDIO_POLICY_TEST
1359 int testIndex = testOutputIndex(output);
1360 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001361 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001362 if (outputDesc->isActive()) {
1363 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001364 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001365 mTestOutputs[testIndex] = 0;
1366 }
1367 return;
1368 }
1369#endif //AUDIO_POLICY_TEST
1370
Paul McLeanaa981192015-03-21 09:55:15 -07001371 // Routing
1372 mOutputRoutes.removeRoute(session);
1373
Eric Laurentc75307b2015-03-17 15:29:32 -07001374 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001375 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001376 if (desc->mDirectOpenCount <= 0) {
1377 ALOGW("releaseOutput() invalid open count %d for output %d",
1378 desc->mDirectOpenCount, output);
1379 return;
1380 }
1381 if (--desc->mDirectOpenCount == 0) {
1382 closeOutput(output);
1383 // If effects where present on the output, audioflinger moved them to the primary
1384 // output by default: move them back to the appropriate output.
1385 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001386 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001387 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1388 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001389 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001390 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001391 }
1392 }
1393}
1394
1395
Eric Laurentcaf7f482014-11-25 17:50:47 -08001396status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1397 audio_io_handle_t *input,
1398 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001399 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001400 uint32_t samplingRate,
1401 audio_format_t format,
1402 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001403 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001404 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001405 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001406{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001407 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1408 "session %d, flags %#x",
1409 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001410
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001412 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001413 audio_devices_t device;
1414 // handle legacy remote submix case where the address was not always specified
1415 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001416 audio_source_t inputSource = attr->source;
1417 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001418 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001419
Eric Laurentc447ded2015-01-06 08:47:05 -08001420 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1421 inputSource = AUDIO_SOURCE_MIC;
1422 }
1423 halInputSource = inputSource;
1424
Paul McLean466dc8e2015-04-17 13:15:36 -06001425 // Explicit routing?
1426 sp<DeviceDescriptor> deviceDesc;
1427 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1428 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1429 deviceDesc = mAvailableInputDevices[i];
1430 break;
1431 }
1432 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001433 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001434
Eric Laurentc447ded2015-01-06 08:47:05 -08001435 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001436 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001437 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001438 if (ret != NO_ERROR) {
1439 return ret;
1440 }
1441 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001442 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1443 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001444 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001445 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001446 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001447 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001448 return BAD_VALUE;
1449 }
Eric Laurentc722f302014-12-10 11:21:49 -08001450 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001451 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001452 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1453 // there is an external policy, but this input is attached to a mix of recorders,
1454 // meaning it receives audio injected into the framework, so the recorder doesn't
1455 // know about it and is therefore considered "legacy"
1456 *inputType = API_INPUT_LEGACY;
1457 } else {
1458 // recording a mix of players defined by an external policy, we're rerouting for
1459 // an external policy
1460 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1461 }
Eric Laurentc722f302014-12-10 11:21:49 -08001462 } else if (audio_is_remote_submix_device(device)) {
1463 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001464 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001465 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1466 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001467 } else {
1468 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001469 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001470
Eric Laurent599c7582015-12-07 18:05:55 -08001471 }
1472
1473 *input = getInputForDevice(device, address, session, uid, inputSource,
1474 samplingRate, format, channelMask, flags,
1475 policyMix);
1476 if (*input == AUDIO_IO_HANDLE_NONE) {
1477 mInputRoutes.removeRoute(session);
1478 return INVALID_OPERATION;
1479 }
1480 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1481 return NO_ERROR;
1482}
1483
1484
1485audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1486 String8 address,
1487 audio_session_t session,
1488 uid_t uid,
1489 audio_source_t inputSource,
1490 uint32_t samplingRate,
1491 audio_format_t format,
1492 audio_channel_mask_t channelMask,
1493 audio_input_flags_t flags,
1494 AudioMix *policyMix)
1495{
1496 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1497 audio_source_t halInputSource = inputSource;
1498 bool isSoundTrigger = false;
1499
1500 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1501 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1502 if (index >= 0) {
1503 input = mSoundTriggerSessions.valueFor(session);
1504 isSoundTrigger = true;
1505 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1506 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1507 } else {
1508 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001509 }
1510 }
1511
Andy Hungf129b032015-04-07 13:45:50 -07001512 // find a compatible input profile (not necessarily identical in parameters)
1513 sp<IOProfile> profile;
1514 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001515 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001516 audio_format_t profileFormat = format;
1517 audio_channel_mask_t profileChannelMask = channelMask;
1518 audio_input_flags_t profileFlags = flags;
1519 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001520 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001521 profileSamplingRate, profileFormat, profileChannelMask,
1522 profileFlags);
1523 if (profile != 0) {
1524 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001525 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1526 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001527 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1528 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1529 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001530 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1531 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001532 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001533 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001534 }
Eric Laurente552edb2014-03-10 17:42:56 -07001535 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001536 // Pick input sampling rate if not specified by client
1537 if (samplingRate == 0) {
1538 samplingRate = profileSamplingRate;
1539 }
Eric Laurente552edb2014-03-10 17:42:56 -07001540
Eric Laurent322b4d22015-04-03 15:57:54 -07001541 if (profile->getModuleHandle() == 0) {
1542 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001543 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001544 }
1545
Eric Laurent599c7582015-12-07 18:05:55 -08001546 sp<AudioSession> audioSession = new AudioSession(session,
1547 inputSource,
1548 format,
1549 samplingRate,
1550 channelMask,
1551 flags,
1552 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001553 isSoundTrigger,
1554 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001555
Eric Laurent232f26f2016-02-17 18:06:27 -08001556// TODO enable input reuse
1557#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001558 // reuse an open input if possible
1559 for (size_t i = 0; i < mInputs.size(); i++) {
1560 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001561 // reuse input if it shares the same profile and same sound trigger attribute
1562 if (profile == desc->mProfile &&
1563 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001564
1565 sp<AudioSession> as = desc->getAudioSession(session);
1566 if (as != 0) {
1567 // do not allow unmatching properties on same session
1568 if (as->matches(audioSession)) {
1569 as->changeOpenCount(1);
1570 } else {
1571 ALOGW("getInputForDevice() record with different attributes"
1572 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001573 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001574 }
1575 } else {
1576 desc->addAudioSession(session, audioSession);
1577 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001578 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1579 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001580 }
1581 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001582#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001583
Eric Laurentcf2c0212014-07-25 16:20:43 -07001584 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001585 config.sample_rate = profileSamplingRate;
1586 config.channel_mask = profileChannelMask;
1587 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001588
Eric Laurent322b4d22015-04-03 15:57:54 -07001589 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001590 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001591 &config,
1592 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001593 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001594 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001595 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001596
1597 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001598 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001599 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001600 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001601 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001602 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1603 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001604 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001605 if (input != AUDIO_IO_HANDLE_NONE) {
1606 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001607 }
Eric Laurent599c7582015-12-07 18:05:55 -08001608 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001609 }
1610
Eric Laurent1f2f2232014-06-02 12:01:23 -07001611 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001612 inputDesc->mSamplingRate = profileSamplingRate;
1613 inputDesc->mFormat = profileFormat;
1614 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001615 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001616 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001617 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001618
Eric Laurent599c7582015-12-07 18:05:55 -08001619 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001620 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001621
Eric Laurent599c7582015-12-07 18:05:55 -08001622 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001623}
1624
Eric Laurent4dc68062014-07-28 17:26:49 -07001625status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001626 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001627{
1628 ALOGV("startInput() input %d", input);
1629 ssize_t index = mInputs.indexOfKey(input);
1630 if (index < 0) {
1631 ALOGW("startInput() unknown input %d", input);
1632 return BAD_VALUE;
1633 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001634 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001635
Eric Laurent599c7582015-12-07 18:05:55 -08001636 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1637 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001638 ALOGW("startInput() unknown session %d on input %d", session, input);
1639 return BAD_VALUE;
1640 }
1641
Eric Laurent232f26f2016-02-17 18:06:27 -08001642 // virtual input devices are compatible with other input devices
1643 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001644
Eric Laurent232f26f2016-02-17 18:06:27 -08001645 // for a non-virtual input device, check if there is another (non-virtual) active input
1646 audio_io_handle_t activeInput = mInputs.getActiveInput();
1647 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001648
Eric Laurent232f26f2016-02-17 18:06:27 -08001649 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1650 // otherwise the active input continues and the new input cannot be started.
1651 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1652 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1653 !activeDesc->hasPreemptedSession(session)) {
1654 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1655 //FIXME: consider all active sessions
1656 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1657 audio_session_t activeSession = activeSessions.keyAt(0);
1658 SortedVector<audio_session_t> sessions =
1659 activeDesc->getPreemptedSessions();
1660 sessions.add(activeSession);
1661 inputDesc->setPreemptedSessions(sessions);
1662 stopInput(activeInput, activeSession);
1663 releaseInput(activeInput, activeSession);
1664 } else {
1665 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1666 return INVALID_OPERATION;
1667 }
1668 }
1669
1670 // Do not allow capture if an active voice call is using a software patch and
1671 // the call TX source device is on the same HW module.
1672 // FIXME: would be better to refine to only inputs whose profile connects to the
1673 // call TX device but this information is not in the audio patch
1674 if (mCallTxPatch != 0 &&
1675 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1676 return INVALID_OPERATION;
1677 }
1678 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001679
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001680 // Routing?
1681 mInputRoutes.incRouteActivity(session);
1682
Eric Laurent232f26f2016-02-17 18:06:27 -08001683 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1684 // if input maps to a dynamic policy with an activity listener, notify of state change
1685 if ((inputDesc->mPolicyMix != NULL)
1686 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001687 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001688 MIX_STATE_MIXING);
1689 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001690
Eric Laurent232f26f2016-02-17 18:06:27 -08001691 if (mInputs.activeInputsCount() == 0) {
1692 SoundTrigger::setCaptureState(true);
1693 }
1694 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001695
Eric Laurent232f26f2016-02-17 18:06:27 -08001696 // automatically enable the remote submix output when input is started if not
1697 // used by a policy mix of type MIX_TYPE_RECORDERS
1698 // For remote submix (a virtual device), we open only one input per capture request.
1699 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1700 String8 address = String8("");
1701 if (inputDesc->mPolicyMix == NULL) {
1702 address = String8("0");
1703 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001704 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001705 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001706 if (address != "") {
1707 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1708 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1709 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001710 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001711 }
Eric Laurente552edb2014-03-10 17:42:56 -07001712 }
1713
Eric Laurent599c7582015-12-07 18:05:55 -08001714 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001715
Eric Laurent232f26f2016-02-17 18:06:27 -08001716 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001717 return NO_ERROR;
1718}
1719
Eric Laurent4dc68062014-07-28 17:26:49 -07001720status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1721 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001722{
1723 ALOGV("stopInput() input %d", input);
1724 ssize_t index = mInputs.indexOfKey(input);
1725 if (index < 0) {
1726 ALOGW("stopInput() unknown input %d", input);
1727 return BAD_VALUE;
1728 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001729 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001730
Eric Laurent599c7582015-12-07 18:05:55 -08001731 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001732 if (index < 0) {
1733 ALOGW("stopInput() unknown session %d on input %d", session, input);
1734 return BAD_VALUE;
1735 }
1736
Eric Laurent599c7582015-12-07 18:05:55 -08001737 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001738 ALOGW("stopInput() input %d already stopped", input);
1739 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001740 }
1741
Eric Laurent599c7582015-12-07 18:05:55 -08001742 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001743
1744 // Routing?
1745 mInputRoutes.decRouteActivity(session);
1746
Eric Laurent232f26f2016-02-17 18:06:27 -08001747 if (!inputDesc->isActive()) {
1748 // if input maps to a dynamic policy with an activity listener, notify of state change
1749 if ((inputDesc->mPolicyMix != NULL)
1750 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001751 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001752 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001753 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001754
1755 // automatically disable the remote submix output when input is stopped if not
1756 // used by a policy mix of type MIX_TYPE_RECORDERS
1757 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1758 String8 address = String8("");
1759 if (inputDesc->mPolicyMix == NULL) {
1760 address = String8("0");
1761 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001762 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001763 }
1764 if (address != "") {
1765 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1766 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1767 address, "remote-submix");
1768 }
1769 }
1770
1771 resetInputDevice(input);
1772
1773 if (mInputs.activeInputsCount() == 0) {
1774 SoundTrigger::setCaptureState(false);
1775 }
1776 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001777 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001778 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001779}
1780
Eric Laurent4dc68062014-07-28 17:26:49 -07001781void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1782 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001783{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001784
Eric Laurente552edb2014-03-10 17:42:56 -07001785 ALOGV("releaseInput() %d", input);
1786 ssize_t index = mInputs.indexOfKey(input);
1787 if (index < 0) {
1788 ALOGW("releaseInput() releasing unknown input %d", input);
1789 return;
1790 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001791
1792 // Routing
1793 mInputRoutes.removeRoute(session);
1794
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001795 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1796 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001797
Eric Laurent599c7582015-12-07 18:05:55 -08001798 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001799 if (index < 0) {
1800 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1801 return;
1802 }
Eric Laurent599c7582015-12-07 18:05:55 -08001803
1804 if (audioSession->openCount() == 0) {
1805 ALOGW("releaseInput() invalid open count %d on session %d",
1806 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001807 return;
1808 }
Eric Laurent599c7582015-12-07 18:05:55 -08001809
1810 if (audioSession->changeOpenCount(-1) == 0) {
1811 inputDesc->removeAudioSession(session);
1812 }
1813
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001814 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001815 ALOGV("releaseInput() exit > 0");
1816 return;
1817 }
1818
Eric Laurent05b90f82014-08-27 15:32:29 -07001819 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001820 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001821 ALOGV("releaseInput() exit");
1822}
1823
Eric Laurentd4692962014-05-05 18:13:44 -07001824void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001825 bool patchRemoved = false;
1826
Eric Laurentd4692962014-05-05 18:13:44 -07001827 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001828 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001829 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001830 if (patch_index >= 0) {
1831 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001832 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001833 mAudioPatches.removeItemsAt(patch_index);
1834 patchRemoved = true;
1835 }
Eric Laurentd4692962014-05-05 18:13:44 -07001836 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1837 }
1838 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001839 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001840 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001841
1842 if (patchRemoved) {
1843 mpClientInterface->onAudioPatchListUpdate();
1844 }
Eric Laurentd4692962014-05-05 18:13:44 -07001845}
1846
Eric Laurente0720872014-03-11 09:30:41 -07001847void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001848 int indexMin,
1849 int indexMax)
1850{
1851 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001852 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001853
1854 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001855 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1856 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001857 continue;
1858 }
1859 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001860 }
Eric Laurente552edb2014-03-10 17:42:56 -07001861}
1862
Eric Laurente0720872014-03-11 09:30:41 -07001863status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001864 int index,
1865 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001866{
1867
François Gaffied1ab2bd2015-12-02 18:20:06 +01001868 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1869 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001870 return BAD_VALUE;
1871 }
1872 if (!audio_is_output_device(device)) {
1873 return BAD_VALUE;
1874 }
1875
1876 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001877 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001878
Eric Laurent1fd372e2016-04-06 14:23:53 -07001879 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001880 stream, device, index);
1881
Eric Laurent28d09f02016-03-08 10:43:05 -08001882 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001883 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1884 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001885 continue;
1886 }
1887 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1888 }
Eric Laurente552edb2014-03-10 17:42:56 -07001889
Eric Laurent1fd372e2016-04-06 14:23:53 -07001890 // update volume on all outputs and streams matching the following:
1891 // - The requested stream (or a stream matching for volume control) is active on the output
1892 // - The device (or devices) selected by the strategy corresponding to this stream includes
1893 // the requested device
1894 // - For non default requested device, currently selected device on the output is either the
1895 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001896 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1897 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001898 status_t status = NO_ERROR;
1899 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001900 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1901 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001902 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1903 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001904 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001905 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001906 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1907 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001908 continue;
1909 }
Eric Laurent794fde22016-03-11 09:50:45 -08001910 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurente76f29c2016-09-14 17:17:53 -07001911 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/);
1912 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
1913 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001914 continue;
1915 }
Eric Laurente76f29c2016-09-14 17:17:53 -07001916 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001917 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001918 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07001919 applyVolume = (curDevice & curStreamDevice) != 0;
1920 } else {
1921 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
1922 stream, Volume::getDeviceForVolume(curStreamDevice));
Eric Laurent1fd372e2016-04-06 14:23:53 -07001923 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001924
Eric Laurente76f29c2016-09-14 17:17:53 -07001925 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07001926 //FIXME: workaround for truncated touch sounds
1927 // delayed volume change for system stream to be removed when the problem is
1928 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001929 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001930 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1931 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001932 if (volStatus != NO_ERROR) {
1933 status = volStatus;
1934 }
1935 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001936 }
Eric Laurente552edb2014-03-10 17:42:56 -07001937 }
1938 return status;
1939}
1940
Eric Laurente0720872014-03-11 09:30:41 -07001941status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001942 int *index,
1943 audio_devices_t device)
1944{
1945 if (index == NULL) {
1946 return BAD_VALUE;
1947 }
1948 if (!audio_is_output_device(device)) {
1949 return BAD_VALUE;
1950 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001951 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001952 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001953 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001954 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1955 }
François Gaffiedfd74092015-03-19 12:10:59 +01001956 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001957
François Gaffied1ab2bd2015-12-02 18:20:06 +01001958 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001959 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1960 return NO_ERROR;
1961}
1962
Eric Laurente0720872014-03-11 09:30:41 -07001963audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001964 const SortedVector<audio_io_handle_t>& outputs)
1965{
1966 // select one output among several suitable for global effects.
1967 // The priority is as follows:
1968 // 1: An offloaded output. If the effect ends up not being offloadable,
1969 // AudioFlinger will invalidate the track and the offloaded output
1970 // will be closed causing the effect to be moved to a PCM output.
1971 // 2: A deep buffer output
1972 // 3: the first output in the list
1973
1974 if (outputs.size() == 0) {
1975 return 0;
1976 }
1977
1978 audio_io_handle_t outputOffloaded = 0;
1979 audio_io_handle_t outputDeepBuffer = 0;
1980
1981 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001982 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001983 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001984 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1985 outputOffloaded = outputs[i];
1986 }
1987 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1988 outputDeepBuffer = outputs[i];
1989 }
1990 }
1991
1992 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1993 outputOffloaded, outputDeepBuffer);
1994 if (outputOffloaded != 0) {
1995 return outputOffloaded;
1996 }
1997 if (outputDeepBuffer != 0) {
1998 return outputDeepBuffer;
1999 }
2000
2001 return outputs[0];
2002}
2003
Eric Laurente0720872014-03-11 09:30:41 -07002004audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07002005{
2006 // apply simple rule where global effects are attached to the same output as MUSIC streams
2007
Eric Laurent3b73df72014-03-11 09:06:29 -07002008 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002009 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2010 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2011
2012 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2013 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2014 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2015
2016 return output;
2017}
2018
Eric Laurente0720872014-03-11 09:30:41 -07002019status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002020 audio_io_handle_t io,
2021 uint32_t strategy,
2022 int session,
2023 int id)
2024{
2025 ssize_t index = mOutputs.indexOfKey(io);
2026 if (index < 0) {
2027 index = mInputs.indexOfKey(io);
2028 if (index < 0) {
2029 ALOGW("registerEffect() unknown io %d", io);
2030 return INVALID_OPERATION;
2031 }
2032 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002033 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002034}
2035
Eric Laurentc75307b2015-03-17 15:29:32 -07002036bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2037{
Eric Laurent28d09f02016-03-08 10:43:05 -08002038 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002039 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2040 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002041 continue;
2042 }
2043 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2044 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002045 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002046}
2047
2048bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2049{
2050 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2051}
2052
Eric Laurente0720872014-03-11 09:30:41 -07002053bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002054{
2055 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002056 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002057 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002058 return true;
2059 }
2060 }
2061 return false;
2062}
2063
Eric Laurent275e8e92014-11-30 15:14:47 -08002064// Register a list of custom mixes with their attributes and format.
2065// When a mix is registered, corresponding input and output profiles are
2066// added to the remote submix hw module. The profile contains only the
2067// parameters (sampling rate, format...) specified by the mix.
2068// The corresponding input remote submix device is also connected.
2069//
2070// When a remote submix device is connected, the address is checked to select the
2071// appropriate profile and the corresponding input or output stream is opened.
2072//
2073// When capture starts, getInputForAttr() will:
2074// - 1 look for a mix matching the address passed in attribtutes tags if any
2075// - 2 if none found, getDeviceForInputSource() will:
2076// - 2.1 look for a mix matching the attributes source
2077// - 2.2 if none found, default to device selection by policy rules
2078// At this time, the corresponding output remote submix device is also connected
2079// and active playback use cases can be transferred to this mix if needed when reconnecting
2080// after AudioTracks are invalidated
2081//
2082// When playback starts, getOutputForAttr() will:
2083// - 1 look for a mix matching the address passed in attribtutes tags if any
2084// - 2 if none found, look for a mix matching the attributes usage
2085// - 3 if none found, default to device and output selection by policy rules.
2086
2087status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2088{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002089 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2090 status_t res = NO_ERROR;
2091
2092 sp<HwModule> rSubmixModule;
2093 // examine each mix's route type
2094 for (size_t i = 0; i < mixes.size(); i++) {
2095 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2096 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2097 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002098 break;
2099 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002100 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2101 // Loop back through "remote submix"
2102 if (rSubmixModule == 0) {
2103 for (size_t j = 0; i < mHwModules.size(); j++) {
2104 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2105 && mHwModules[j]->mHandle != 0) {
2106 rSubmixModule = mHwModules[j];
2107 break;
2108 }
2109 }
2110 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002111
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002112 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002113
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002114 if (rSubmixModule == 0) {
2115 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2116 res = INVALID_OPERATION;
2117 break;
2118 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002119
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002120 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002121
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002122 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002123 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002124 res = INVALID_OPERATION;
2125 break;
2126 }
2127 audio_config_t outputConfig = mixes[i].mFormat;
2128 audio_config_t inputConfig = mixes[i].mFormat;
2129 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2130 // stereo and let audio flinger do the channel conversion if needed.
2131 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2132 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2133 rSubmixModule->addOutputProfile(address, &outputConfig,
2134 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2135 rSubmixModule->addInputProfile(address, &inputConfig,
2136 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002137
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002138 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2139 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2140 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2141 address.string(), "remote-submix");
2142 } else {
2143 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2144 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2145 address.string(), "remote-submix");
2146 }
2147 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002148 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002149 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002150 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2151 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002152
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002153 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002154 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2155 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2156 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2157 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2158 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2159 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002160 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2161 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002162 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2163 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002164 } else {
2165 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002166 }
2167 break;
2168 }
2169 }
2170
2171 if (res != NO_ERROR) {
2172 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002173 i, device, address.string());
2174 res = INVALID_OPERATION;
2175 break;
2176 } else if (!foundOutput) {
2177 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2178 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002179 res = INVALID_OPERATION;
2180 break;
2181 }
Eric Laurentc722f302014-12-10 11:21:49 -08002182 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002183 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002184 if (res != NO_ERROR) {
2185 unregisterPolicyMixes(mixes);
2186 }
2187 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002188}
2189
2190status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2191{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002192 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002193 status_t res = NO_ERROR;
2194 sp<HwModule> rSubmixModule;
2195 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002196 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002197 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002198
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002199 if (rSubmixModule == 0) {
2200 for (size_t j = 0; i < mHwModules.size(); j++) {
2201 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2202 && mHwModules[j]->mHandle != 0) {
2203 rSubmixModule = mHwModules[j];
2204 break;
2205 }
2206 }
2207 }
2208 if (rSubmixModule == 0) {
2209 res = INVALID_OPERATION;
2210 continue;
2211 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002212
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002213 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002214
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002215 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2216 res = INVALID_OPERATION;
2217 continue;
2218 }
2219
2220 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2221 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2222 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2223 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2224 address.string(), "remote-submix");
2225 }
2226 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2227 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2228 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2229 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2230 address.string(), "remote-submix");
2231 }
2232 rSubmixModule->removeOutputProfile(address);
2233 rSubmixModule->removeInputProfile(address);
2234
2235 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2236 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2237 res = INVALID_OPERATION;
2238 continue;
2239 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002240 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002241 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002242 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002243}
2244
Eric Laurente552edb2014-03-10 17:42:56 -07002245
Eric Laurente0720872014-03-11 09:30:41 -07002246status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002247{
2248 const size_t SIZE = 256;
2249 char buffer[SIZE];
2250 String8 result;
2251
2252 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2253 result.append(buffer);
2254
Eric Laurent87ffa392015-05-22 10:32:38 -07002255 snprintf(buffer, SIZE, " Primary Output: %d\n",
2256 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002257 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002258 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002259 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002260 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002261 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002262 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002263 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002264 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002265 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002266 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002267 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002268 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002269 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002270 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002271 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002272 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002273 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002274 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2275 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2276 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002277 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2278 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002279 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2280 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002281
François Gaffie2110e042015-03-24 08:41:51 +01002282 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002283
François Gaffie112b0af2015-11-19 16:13:25 +01002284 mAvailableOutputDevices.dump(fd, String8("Available output"));
2285 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002286 mHwModules.dump(fd);
2287 mOutputs.dump(fd);
2288 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002289 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002290 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002291 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002292
2293 return NO_ERROR;
2294}
2295
2296// This function checks for the parameters which can be offloaded.
2297// This can be enhanced depending on the capability of the DSP and policy
2298// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002299bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002300{
2301 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002302 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002303 offloadInfo.sample_rate, offloadInfo.channel_mask,
2304 offloadInfo.format,
2305 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2306 offloadInfo.has_video);
2307
Andy Hung2ddee192015-12-18 17:34:44 -08002308 if (mMasterMono) {
2309 return false; // no offloading if mono is set.
2310 }
2311
Eric Laurente552edb2014-03-10 17:42:56 -07002312 // Check if offload has been disabled
2313 char propValue[PROPERTY_VALUE_MAX];
2314 if (property_get("audio.offload.disable", propValue, "0")) {
2315 if (atoi(propValue) != 0) {
2316 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2317 return false;
2318 }
2319 }
2320
2321 // Check if stream type is music, then only allow offload as of now.
2322 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2323 {
2324 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2325 return false;
2326 }
2327
2328 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002329 const bool allowOffloadWithVideo =
2330 property_get_bool("audio.offload.video", false /* default_value */);
2331 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002332 ALOGV("isOffloadSupported: has_video == true, returning false");
2333 return false;
2334 }
2335
2336 //If duration is less than minimum value defined in property, return false
2337 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2338 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2339 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2340 return false;
2341 }
2342 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2343 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2344 return false;
2345 }
2346
2347 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2348 // creating an offloaded track and tearing it down immediately after start when audioflinger
2349 // detects there is an active non offloadable effect.
2350 // FIXME: We should check the audio session here but we do not have it in this context.
2351 // This may prevent offloading in rare situations where effects are left active by apps
2352 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002353 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002354 return false;
2355 }
2356
2357 // See if there is a profile to support this.
2358 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002359 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002360 offloadInfo.sample_rate,
2361 offloadInfo.format,
2362 offloadInfo.channel_mask,
2363 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002364 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2365 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002366}
2367
Eric Laurent6a94d692014-05-20 11:18:06 -07002368status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2369 audio_port_type_t type,
2370 unsigned int *num_ports,
2371 struct audio_port *ports,
2372 unsigned int *generation)
2373{
2374 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2375 generation == NULL) {
2376 return BAD_VALUE;
2377 }
2378 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2379 if (ports == NULL) {
2380 *num_ports = 0;
2381 }
2382
2383 size_t portsWritten = 0;
2384 size_t portsMax = *num_ports;
2385 *num_ports = 0;
2386 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002387 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2388 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002389 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002390 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2391 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2392 continue;
2393 }
2394 if (portsWritten < portsMax) {
2395 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2396 }
2397 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002398 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002399 }
2400 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002401 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2402 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2403 continue;
2404 }
2405 if (portsWritten < portsMax) {
2406 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2407 }
2408 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002409 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002410 }
2411 }
2412 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2413 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2414 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2415 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2416 }
2417 *num_ports += mInputs.size();
2418 }
2419 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002420 size_t numOutputs = 0;
2421 for (size_t i = 0; i < mOutputs.size(); i++) {
2422 if (!mOutputs[i]->isDuplicated()) {
2423 numOutputs++;
2424 if (portsWritten < portsMax) {
2425 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2426 }
2427 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002428 }
Eric Laurent84c70242014-06-23 08:46:27 -07002429 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 }
2431 }
2432 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002433 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 return NO_ERROR;
2435}
2436
2437status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2438{
2439 return NO_ERROR;
2440}
2441
Eric Laurent6a94d692014-05-20 11:18:06 -07002442status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2443 audio_patch_handle_t *handle,
2444 uid_t uid)
2445{
2446 ALOGV("createAudioPatch()");
2447
2448 if (handle == NULL || patch == NULL) {
2449 return BAD_VALUE;
2450 }
2451 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2452
Eric Laurent874c42872014-08-08 15:13:39 -07002453 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2454 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2455 return BAD_VALUE;
2456 }
2457 // only one source per audio patch supported for now
2458 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002459 return INVALID_OPERATION;
2460 }
Eric Laurent874c42872014-08-08 15:13:39 -07002461
2462 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002463 return INVALID_OPERATION;
2464 }
Eric Laurent874c42872014-08-08 15:13:39 -07002465 for (size_t i = 0; i < patch->num_sinks; i++) {
2466 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2467 return INVALID_OPERATION;
2468 }
2469 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002470
2471 sp<AudioPatch> patchDesc;
2472 ssize_t index = mAudioPatches.indexOfKey(*handle);
2473
Eric Laurent6a94d692014-05-20 11:18:06 -07002474 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2475 patch->sources[0].role,
2476 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002477#if LOG_NDEBUG == 0
2478 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002479 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002480 patch->sinks[i].role,
2481 patch->sinks[i].type);
2482 }
2483#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002484
2485 if (index >= 0) {
2486 patchDesc = mAudioPatches.valueAt(index);
2487 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2488 mUidCached, patchDesc->mUid, uid);
2489 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2490 return INVALID_OPERATION;
2491 }
2492 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002493 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002494 }
2495
2496 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002497 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002498 if (outputDesc == NULL) {
2499 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2500 return BAD_VALUE;
2501 }
Eric Laurent84c70242014-06-23 08:46:27 -07002502 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2503 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002504 if (patchDesc != 0) {
2505 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2506 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2507 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2508 return BAD_VALUE;
2509 }
2510 }
Eric Laurent874c42872014-08-08 15:13:39 -07002511 DeviceVector devices;
2512 for (size_t i = 0; i < patch->num_sinks; i++) {
2513 // Only support mix to devices connection
2514 // TODO add support for mix to mix connection
2515 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2516 ALOGV("createAudioPatch() source mix but sink is not a device");
2517 return INVALID_OPERATION;
2518 }
2519 sp<DeviceDescriptor> devDesc =
2520 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2521 if (devDesc == 0) {
2522 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2523 return BAD_VALUE;
2524 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002525
François Gaffie53615e22015-03-19 09:24:12 +01002526 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002527 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002528 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002529 NULL, // updatedSamplingRate
2530 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002531 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002532 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002533 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002534 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002535 ALOGV("createAudioPatch() profile not supported for device %08x",
2536 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002537 return INVALID_OPERATION;
2538 }
2539 devices.add(devDesc);
2540 }
2541 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002542 return INVALID_OPERATION;
2543 }
Eric Laurent874c42872014-08-08 15:13:39 -07002544
Eric Laurent6a94d692014-05-20 11:18:06 -07002545 // TODO: reconfigure output format and channels here
2546 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002547 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002548 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002549 index = mAudioPatches.indexOfKey(*handle);
2550 if (index >= 0) {
2551 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2552 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2553 }
2554 patchDesc = mAudioPatches.valueAt(index);
2555 patchDesc->mUid = uid;
2556 ALOGV("createAudioPatch() success");
2557 } else {
2558 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2559 return INVALID_OPERATION;
2560 }
2561 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2562 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2563 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002564 // only one sink supported when connecting an input device to a mix
2565 if (patch->num_sinks > 1) {
2566 return INVALID_OPERATION;
2567 }
François Gaffie53615e22015-03-19 09:24:12 +01002568 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002569 if (inputDesc == NULL) {
2570 return BAD_VALUE;
2571 }
2572 if (patchDesc != 0) {
2573 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2574 return BAD_VALUE;
2575 }
2576 }
2577 sp<DeviceDescriptor> devDesc =
2578 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2579 if (devDesc == 0) {
2580 return BAD_VALUE;
2581 }
2582
François Gaffie53615e22015-03-19 09:24:12 +01002583 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002584 devDesc->mAddress,
2585 patch->sinks[0].sample_rate,
2586 NULL, /*updatedSampleRate*/
2587 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002588 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002589 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002590 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002591 // FIXME for the parameter type,
2592 // and the NONE
2593 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002594 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002595 return INVALID_OPERATION;
2596 }
2597 // TODO: reconfigure output format and channels here
2598 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002599 devDesc->type(), inputDesc->mIoHandle);
2600 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002601 index = mAudioPatches.indexOfKey(*handle);
2602 if (index >= 0) {
2603 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2604 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2605 }
2606 patchDesc = mAudioPatches.valueAt(index);
2607 patchDesc->mUid = uid;
2608 ALOGV("createAudioPatch() success");
2609 } else {
2610 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2611 return INVALID_OPERATION;
2612 }
2613 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2614 // device to device connection
2615 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002616 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002617 return BAD_VALUE;
2618 }
2619 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002620 sp<DeviceDescriptor> srcDeviceDesc =
2621 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002622 if (srcDeviceDesc == 0) {
2623 return BAD_VALUE;
2624 }
Eric Laurent874c42872014-08-08 15:13:39 -07002625
Eric Laurent6a94d692014-05-20 11:18:06 -07002626 //update source and sink with our own data as the data passed in the patch may
2627 // be incomplete.
2628 struct audio_patch newPatch = *patch;
2629 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002630
Eric Laurent874c42872014-08-08 15:13:39 -07002631 for (size_t i = 0; i < patch->num_sinks; i++) {
2632 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2633 ALOGV("createAudioPatch() source device but one sink is not a device");
2634 return INVALID_OPERATION;
2635 }
2636
2637 sp<DeviceDescriptor> sinkDeviceDesc =
2638 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2639 if (sinkDeviceDesc == 0) {
2640 return BAD_VALUE;
2641 }
2642 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2643
Eric Laurent3bcf8592015-04-03 12:13:24 -07002644 // create a software bridge in PatchPanel if:
2645 // - source and sink devices are on differnt HW modules OR
2646 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002647 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002648 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002649 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002650 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002651 return INVALID_OPERATION;
2652 }
Eric Laurent874c42872014-08-08 15:13:39 -07002653 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002654 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002655 // if the sink device is reachable via an opened output stream, request to go via
2656 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002657 audio_io_handle_t output = selectOutput(outputs,
2658 AUDIO_OUTPUT_FLAG_NONE,
2659 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002660 if (output != AUDIO_IO_HANDLE_NONE) {
2661 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2662 if (outputDesc->isDuplicated()) {
2663 return INVALID_OPERATION;
2664 }
2665 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002666 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002667 newPatch.num_sources = 2;
2668 }
Eric Laurent83b88082014-06-20 18:31:16 -07002669 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002670 }
2671 // TODO: check from routing capabilities in config file and other conflicting patches
2672
2673 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2674 if (index >= 0) {
2675 afPatchHandle = patchDesc->mAfPatchHandle;
2676 }
2677
2678 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2679 &afPatchHandle,
2680 0);
2681 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2682 status, afPatchHandle);
2683 if (status == NO_ERROR) {
2684 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002685 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002686 addAudioPatch(patchDesc->mHandle, patchDesc);
2687 } else {
2688 patchDesc->mPatch = newPatch;
2689 }
2690 patchDesc->mAfPatchHandle = afPatchHandle;
2691 *handle = patchDesc->mHandle;
2692 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002693 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002694 } else {
2695 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2696 status);
2697 return INVALID_OPERATION;
2698 }
2699 } else {
2700 return BAD_VALUE;
2701 }
2702 } else {
2703 return BAD_VALUE;
2704 }
2705 return NO_ERROR;
2706}
2707
2708status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2709 uid_t uid)
2710{
2711 ALOGV("releaseAudioPatch() patch %d", handle);
2712
2713 ssize_t index = mAudioPatches.indexOfKey(handle);
2714
2715 if (index < 0) {
2716 return BAD_VALUE;
2717 }
2718 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2719 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2720 mUidCached, patchDesc->mUid, uid);
2721 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2722 return INVALID_OPERATION;
2723 }
2724
2725 struct audio_patch *patch = &patchDesc->mPatch;
2726 patchDesc->mUid = mUidCached;
2727 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002728 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002729 if (outputDesc == NULL) {
2730 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2731 return BAD_VALUE;
2732 }
2733
Eric Laurentc75307b2015-03-17 15:29:32 -07002734 setOutputDevice(outputDesc,
2735 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002736 true,
2737 0,
2738 NULL);
2739 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2740 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002741 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002742 if (inputDesc == NULL) {
2743 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2744 return BAD_VALUE;
2745 }
2746 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002747 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002748 true,
2749 NULL);
2750 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002751 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2752 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2753 status, patchDesc->mAfPatchHandle);
2754 removeAudioPatch(patchDesc->mHandle);
2755 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002756 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002757 } else {
2758 return BAD_VALUE;
2759 }
2760 } else {
2761 return BAD_VALUE;
2762 }
2763 return NO_ERROR;
2764}
2765
2766status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2767 struct audio_patch *patches,
2768 unsigned int *generation)
2769{
François Gaffie53615e22015-03-19 09:24:12 +01002770 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002771 return BAD_VALUE;
2772 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002773 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002774 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002775}
2776
Eric Laurente1715a42014-05-20 11:30:42 -07002777status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002778{
Eric Laurente1715a42014-05-20 11:30:42 -07002779 ALOGV("setAudioPortConfig()");
2780
2781 if (config == NULL) {
2782 return BAD_VALUE;
2783 }
2784 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2785 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002786 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2787 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002788 }
2789
Eric Laurenta121f902014-06-03 13:32:54 -07002790 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002791 if (config->type == AUDIO_PORT_TYPE_MIX) {
2792 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002793 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002794 if (outputDesc == NULL) {
2795 return BAD_VALUE;
2796 }
Eric Laurent84c70242014-06-23 08:46:27 -07002797 ALOG_ASSERT(!outputDesc->isDuplicated(),
2798 "setAudioPortConfig() called on duplicated output %d",
2799 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002800 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002801 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002802 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002803 if (inputDesc == NULL) {
2804 return BAD_VALUE;
2805 }
Eric Laurenta121f902014-06-03 13:32:54 -07002806 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002807 } else {
2808 return BAD_VALUE;
2809 }
2810 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2811 sp<DeviceDescriptor> deviceDesc;
2812 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2813 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2814 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2815 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2816 } else {
2817 return BAD_VALUE;
2818 }
2819 if (deviceDesc == NULL) {
2820 return BAD_VALUE;
2821 }
Eric Laurenta121f902014-06-03 13:32:54 -07002822 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002823 } else {
2824 return BAD_VALUE;
2825 }
2826
Eric Laurenta121f902014-06-03 13:32:54 -07002827 struct audio_port_config backupConfig;
2828 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2829 if (status == NO_ERROR) {
2830 struct audio_port_config newConfig;
2831 audioPortConfig->toAudioPortConfig(&newConfig, config);
2832 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002833 }
Eric Laurenta121f902014-06-03 13:32:54 -07002834 if (status != NO_ERROR) {
2835 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002836 }
Eric Laurente1715a42014-05-20 11:30:42 -07002837
2838 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002839}
2840
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002841void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2842{
Eric Laurentd60560a2015-04-10 11:31:20 -07002843 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002844 clearAudioPatches(uid);
2845 clearSessionRoutes(uid);
2846}
2847
Eric Laurent6a94d692014-05-20 11:18:06 -07002848void AudioPolicyManager::clearAudioPatches(uid_t uid)
2849{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002850 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002851 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2852 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002853 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002854 }
2855 }
2856}
2857
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002858void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2859 audio_io_handle_t ouptutToSkip)
2860{
2861 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2862 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2863 for (size_t j = 0; j < mOutputs.size(); j++) {
2864 if (mOutputs.keyAt(j) == ouptutToSkip) {
2865 continue;
2866 }
2867 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2868 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2869 continue;
2870 }
2871 // If the default device for this strategy is on another output mix,
2872 // invalidate all tracks in this strategy to force re connection.
2873 // Otherwise select new device on the output mix.
2874 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002875 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002876 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2877 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2878 }
2879 }
2880 } else {
2881 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2882 setOutputDevice(outputDesc, newDevice, false);
2883 }
2884 }
2885}
2886
2887void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2888{
2889 // remove output routes associated with this uid
2890 SortedVector<routing_strategy> affectedStrategies;
2891 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2892 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2893 if (route->mUid == uid) {
2894 mOutputRoutes.removeItemsAt(i);
2895 if (route->mDeviceDescriptor != 0) {
2896 affectedStrategies.add(getStrategy(route->mStreamType));
2897 }
2898 }
2899 }
2900 // reroute outputs if necessary
2901 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2902 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2903 }
2904
2905 // remove input routes associated with this uid
2906 SortedVector<audio_source_t> affectedSources;
2907 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2908 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2909 if (route->mUid == uid) {
2910 mInputRoutes.removeItemsAt(i);
2911 if (route->mDeviceDescriptor != 0) {
2912 affectedSources.add(route->mSource);
2913 }
2914 }
2915 }
2916 // reroute inputs if necessary
2917 SortedVector<audio_io_handle_t> inputsToClose;
2918 for (size_t i = 0; i < mInputs.size(); i++) {
2919 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002920 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002921 inputsToClose.add(inputDesc->mIoHandle);
2922 }
2923 }
2924 for (size_t i = 0; i < inputsToClose.size(); i++) {
2925 closeInput(inputsToClose[i]);
2926 }
2927}
2928
Eric Laurentd60560a2015-04-10 11:31:20 -07002929void AudioPolicyManager::clearAudioSources(uid_t uid)
2930{
2931 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2932 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2933 if (sourceDesc->mUid == uid) {
2934 stopAudioSource(mAudioSources.keyAt(i));
2935 }
2936 }
2937}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002938
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002939status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2940 audio_io_handle_t *ioHandle,
2941 audio_devices_t *device)
2942{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002943 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2944 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002945 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002946
François Gaffiedf372692015-03-19 10:43:27 +01002947 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002948}
2949
Eric Laurentd60560a2015-04-10 11:31:20 -07002950status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2951 const audio_attributes_t *attributes,
2952 audio_io_handle_t *handle,
2953 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002954{
Eric Laurentd60560a2015-04-10 11:31:20 -07002955 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2956 if (source == NULL || attributes == NULL || handle == NULL) {
2957 return BAD_VALUE;
2958 }
2959
2960 *handle = AUDIO_IO_HANDLE_NONE;
2961
2962 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2963 source->type != AUDIO_PORT_TYPE_DEVICE) {
2964 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2965 return INVALID_OPERATION;
2966 }
2967
2968 sp<DeviceDescriptor> srcDeviceDesc =
2969 mAvailableInputDevices.getDevice(source->ext.device.type,
2970 String8(source->ext.device.address));
2971 if (srcDeviceDesc == 0) {
2972 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2973 return BAD_VALUE;
2974 }
2975 sp<AudioSourceDescriptor> sourceDesc =
2976 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2977
2978 struct audio_patch dummyPatch;
2979 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2980 sourceDesc->mPatchDesc = patchDesc;
2981
2982 status_t status = connectAudioSource(sourceDesc);
2983 if (status == NO_ERROR) {
2984 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2985 *handle = sourceDesc->getHandle();
2986 }
2987 return status;
2988}
2989
2990status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2991{
2992 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2993
2994 // make sure we only have one patch per source.
2995 disconnectAudioSource(sourceDesc);
2996
2997 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002998 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002999 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3000
3001 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3002 sp<DeviceDescriptor> sinkDeviceDesc =
3003 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3004
3005 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3006 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3007
3008 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3009 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003010 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003011 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3012 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3013 // create patch between src device and output device
3014 // create Hwoutput and add to mHwOutputs
3015 } else {
3016 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3017 audio_io_handle_t output =
3018 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3019 if (output == AUDIO_IO_HANDLE_NONE) {
3020 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3021 return INVALID_OPERATION;
3022 }
3023 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3024 if (outputDesc->isDuplicated()) {
3025 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3026 return INVALID_OPERATION;
3027 }
3028 // create a special patch with no sink and two sources:
3029 // - the second source indicates to PatchPanel through which output mix this patch should
3030 // be connected as well as the stream type for volume control
3031 // - the sink is defined by whatever output device is currently selected for the output
3032 // though which this patch is routed.
3033 patch->num_sinks = 0;
3034 patch->num_sources = 2;
3035 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3036 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3037 patch->sources[1].ext.mix.usecase.stream = stream;
3038 status_t status = mpClientInterface->createAudioPatch(patch,
3039 &afPatchHandle,
3040 0);
3041 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3042 status, afPatchHandle);
3043 if (status != NO_ERROR) {
3044 ALOGW("%s patch panel could not connect device patch, error %d",
3045 __FUNCTION__, status);
3046 return INVALID_OPERATION;
3047 }
3048 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003049 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003050
3051 if (status != NO_ERROR) {
3052 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3053 return status;
3054 }
3055 sourceDesc->mSwOutput = outputDesc;
3056 if (delayMs != 0) {
3057 usleep(delayMs * 1000);
3058 }
3059 }
3060
3061 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3062 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3063
3064 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003065}
3066
Eric Laurent493404d2015-04-21 15:07:36 -07003067status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003068{
Eric Laurentd60560a2015-04-10 11:31:20 -07003069 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3070 ALOGV("%s handle %d", __FUNCTION__, handle);
3071 if (sourceDesc == 0) {
3072 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3073 return BAD_VALUE;
3074 }
3075 status_t status = disconnectAudioSource(sourceDesc);
3076
3077 mAudioSources.removeItem(handle);
3078 return status;
3079}
3080
Andy Hung2ddee192015-12-18 17:34:44 -08003081status_t AudioPolicyManager::setMasterMono(bool mono)
3082{
3083 if (mMasterMono == mono) {
3084 return NO_ERROR;
3085 }
3086 mMasterMono = mono;
3087 // if enabling mono we close all offloaded devices, which will invalidate the
3088 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3089 // for recreating the new AudioTrack as non-offloaded PCM.
3090 //
3091 // If disabling mono, we leave all tracks as is: we don't know which clients
3092 // and tracks are able to be recreated as offloaded. The next "song" should
3093 // play back offloaded.
3094 if (mMasterMono) {
3095 Vector<audio_io_handle_t> offloaded;
3096 for (size_t i = 0; i < mOutputs.size(); ++i) {
3097 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3098 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3099 offloaded.push(desc->mIoHandle);
3100 }
3101 }
3102 for (size_t i = 0; i < offloaded.size(); ++i) {
3103 closeOutput(offloaded[i]);
3104 }
3105 }
3106 // update master mono for all remaining outputs
3107 for (size_t i = 0; i < mOutputs.size(); ++i) {
3108 updateMono(mOutputs.keyAt(i));
3109 }
3110 return NO_ERROR;
3111}
3112
3113status_t AudioPolicyManager::getMasterMono(bool *mono)
3114{
3115 *mono = mMasterMono;
3116 return NO_ERROR;
3117}
3118
Eric Laurentd60560a2015-04-10 11:31:20 -07003119status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3120{
3121 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3122
3123 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3124 if (patchDesc == 0) {
3125 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3126 sourceDesc->mPatchDesc->mHandle);
3127 return BAD_VALUE;
3128 }
3129 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3130
Eric Laurent28d09f02016-03-08 10:43:05 -08003131 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003132 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3133 if (swOutputDesc != 0) {
3134 stopSource(swOutputDesc, stream, false);
3135 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3136 } else {
3137 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3138 if (hwOutputDesc != 0) {
3139 // release patch between src device and output device
3140 // close Hwoutput and remove from mHwOutputs
3141 } else {
3142 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3143 }
3144 }
3145 return NO_ERROR;
3146}
3147
3148sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3149 audio_io_handle_t output, routing_strategy strategy)
3150{
3151 sp<AudioSourceDescriptor> source;
3152 for (size_t i = 0; i < mAudioSources.size(); i++) {
3153 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3154 routing_strategy sourceStrategy =
3155 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3156 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3157 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3158 source = sourceDesc;
3159 break;
3160 }
3161 }
3162 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003163}
3164
Eric Laurente552edb2014-03-10 17:42:56 -07003165// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003166// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003167// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003168uint32_t AudioPolicyManager::nextAudioPortGeneration()
3169{
3170 return android_atomic_inc(&mAudioPortGeneration);
3171}
3172
Eric Laurente0720872014-03-11 09:30:41 -07003173AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003174 :
3175#ifdef AUDIO_POLICY_TEST
3176 Thread(false),
3177#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003178 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003179 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003180 mAudioPortGeneration(1),
3181 mBeaconMuteRefCount(0),
3182 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003183 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003184 mTtsOutputAvailable(false),
3185 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003186{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003187 mUidCached = getuid();
3188 mpClientInterface = clientInterface;
3189
3190 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3191 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3192 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3193 bool speakerDrcEnabled = false;
3194
3195#ifdef USE_XML_AUDIO_POLICY_CONF
3196 mVolumeCurves = new VolumeCurvesCollection();
3197 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3198 mDefaultOutputDevice, speakerDrcEnabled,
3199 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3200 PolicySerializer serializer;
3201 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3202#else
3203 mVolumeCurves = new StreamDescriptorCollection();
3204 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3205 mDefaultOutputDevice, speakerDrcEnabled);
3206 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3207 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3208#endif
3209 ALOGE("could not load audio policy configuration file, setting defaults");
3210 config.setDefault();
3211 }
3212 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3213 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3214
3215 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003216 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3217 if (!engineInstance) {
3218 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3219 return;
3220 }
3221 // Retrieve the Policy Manager Interface
3222 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3223 if (mEngine == NULL) {
3224 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3225 return;
3226 }
3227 mEngine->setObserver(this);
3228 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003229 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003230 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3231
Eric Laurent3a4311c2014-03-17 12:00:47 -07003232 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003233 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003234 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3235 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003236 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003237 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003238 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003239 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003240 continue;
3241 }
3242 // open all output streams needed to access attached devices
3243 // except for direct output streams that are only opened when they are actually
3244 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003245 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003246 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3247 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003248 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003249
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003250 if (!outProfile->hasSupportedDevices()) {
3251 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003252 continue;
3253 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003254 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003255 mTtsOutputAvailable = true;
3256 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003257
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003259 continue;
3260 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003261 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003262 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3263 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003264 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003265 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003266 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003267 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003268 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003269 if ((profileType & outputDeviceTypes) == 0) {
3270 continue;
3271 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003272 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3273 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003274 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3275 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3276 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3277 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003278
3279 outputDesc->mDevice = profileType;
3280 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3281 config.sample_rate = outputDesc->mSamplingRate;
3282 config.channel_mask = outputDesc->mChannelMask;
3283 config.format = outputDesc->mFormat;
3284 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003285 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003286 &output,
3287 &config,
3288 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003289 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003290 &outputDesc->mLatency,
3291 outputDesc->mFlags);
3292
3293 if (status != NO_ERROR) {
3294 ALOGW("Cannot open output stream for device %08x on hw module %s",
3295 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003296 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003297 } else {
3298 outputDesc->mSamplingRate = config.sample_rate;
3299 outputDesc->mChannelMask = config.channel_mask;
3300 outputDesc->mFormat = config.format;
3301
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003302 for (size_t k = 0; k < supportedDevices.size(); k++) {
3303 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003304 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003305 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3306 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003307 }
3308 }
3309 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003310 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003311 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003312 }
3313 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003314 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003315 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003316 true,
3317 0,
3318 NULL,
3319 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003320 }
Eric Laurente552edb2014-03-10 17:42:56 -07003321 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003322 // open input streams needed to access attached devices to validate
3323 // mAvailableInputDevices list
3324 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3325 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003326 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003327
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003328 if (!inProfile->hasSupportedDevices()) {
3329 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003330 continue;
3331 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003332 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003333 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003334 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3335
Eric Laurentd78f1532014-09-16 16:38:20 -07003336 if ((profileType & inputDeviceTypes) == 0) {
3337 continue;
3338 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003339 sp<AudioInputDescriptor> inputDesc =
3340 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003341
Eric Laurentd78f1532014-09-16 16:38:20 -07003342 inputDesc->mDevice = profileType;
3343
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003344 // find the address
3345 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3346 // the inputs vector must be of size 1, but we don't want to crash here
3347 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3348 : String8("");
3349 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3350 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3351
Eric Laurentd78f1532014-09-16 16:38:20 -07003352 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3353 config.sample_rate = inputDesc->mSamplingRate;
3354 config.channel_mask = inputDesc->mChannelMask;
3355 config.format = inputDesc->mFormat;
3356 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003357 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003358 &input,
3359 &config,
3360 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003361 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003362 AUDIO_SOURCE_MIC,
3363 AUDIO_INPUT_FLAG_NONE);
3364
3365 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003366 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3367 for (size_t k = 0; k < supportedDevices.size(); k++) {
3368 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003369 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003370 if (index >= 0) {
3371 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3372 if (!devDesc->isAttached()) {
3373 devDesc->attach(mHwModules[i]);
3374 devDesc->importAudioPort(inProfile);
3375 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003376 }
3377 }
3378 mpClientInterface->closeInput(input);
3379 } else {
3380 ALOGW("Cannot open input stream for device %08x on hw module %s",
3381 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003382 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003383 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003384 }
3385 }
3386 // make sure all attached devices have been allocated a unique ID
3387 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003388 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003389 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003390 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3391 continue;
3392 }
François Gaffie2110e042015-03-24 08:41:51 +01003393 // The device is now validated and can be appended to the available devices of the engine
3394 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3395 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003396 i++;
3397 }
3398 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003399 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003400 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003401 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3402 continue;
3403 }
François Gaffie2110e042015-03-24 08:41:51 +01003404 // The device is now validated and can be appended to the available devices of the engine
3405 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3406 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003407 i++;
3408 }
3409 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003410 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003411 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003412 }
Eric Laurente552edb2014-03-10 17:42:56 -07003413
3414 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3415
3416 updateDevicesAndOutputs();
3417
3418#ifdef AUDIO_POLICY_TEST
3419 if (mPrimaryOutput != 0) {
3420 AudioParameter outputCmd = AudioParameter();
3421 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003422 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003423
3424 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3425 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003426 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3427 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003428 mTestLatencyMs = 0;
3429 mCurOutput = 0;
3430 mDirectOutput = false;
3431 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3432 mTestOutputs[i] = 0;
3433 }
3434
3435 const size_t SIZE = 256;
3436 char buffer[SIZE];
3437 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3438 run(buffer, ANDROID_PRIORITY_AUDIO);
3439 }
3440#endif //AUDIO_POLICY_TEST
3441}
3442
Eric Laurente0720872014-03-11 09:30:41 -07003443AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003444{
3445#ifdef AUDIO_POLICY_TEST
3446 exit();
3447#endif //AUDIO_POLICY_TEST
3448 for (size_t i = 0; i < mOutputs.size(); i++) {
3449 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003450 }
3451 for (size_t i = 0; i < mInputs.size(); i++) {
3452 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003453 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003454 mAvailableOutputDevices.clear();
3455 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003456 mOutputs.clear();
3457 mInputs.clear();
3458 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003459}
3460
Eric Laurente0720872014-03-11 09:30:41 -07003461status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003462{
Eric Laurent87ffa392015-05-22 10:32:38 -07003463 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003464}
3465
3466#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003467bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003468{
3469 ALOGV("entering threadLoop()");
3470 while (!exitPending())
3471 {
3472 String8 command;
3473 int valueInt;
3474 String8 value;
3475
3476 Mutex::Autolock _l(mLock);
3477 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3478
3479 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3480 AudioParameter param = AudioParameter(command);
3481
3482 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3483 valueInt != 0) {
3484 ALOGV("Test command %s received", command.string());
3485 String8 target;
3486 if (param.get(String8("target"), target) != NO_ERROR) {
3487 target = "Manager";
3488 }
3489 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3490 param.remove(String8("test_cmd_policy_output"));
3491 mCurOutput = valueInt;
3492 }
3493 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3494 param.remove(String8("test_cmd_policy_direct"));
3495 if (value == "false") {
3496 mDirectOutput = false;
3497 } else if (value == "true") {
3498 mDirectOutput = true;
3499 }
3500 }
3501 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3502 param.remove(String8("test_cmd_policy_input"));
3503 mTestInput = valueInt;
3504 }
3505
3506 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3507 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003508 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003509 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003510 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003511 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003512 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003513 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003514 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003515 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003516 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003517 if (target == "Manager") {
3518 mTestFormat = format;
3519 } else if (mTestOutputs[mCurOutput] != 0) {
3520 AudioParameter outputParam = AudioParameter();
3521 outputParam.addInt(String8("format"), format);
3522 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3523 }
3524 }
3525 }
3526 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3527 param.remove(String8("test_cmd_policy_channels"));
3528 int channels = 0;
3529
3530 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003531 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003532 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003533 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003534 }
3535 if (channels != 0) {
3536 if (target == "Manager") {
3537 mTestChannels = channels;
3538 } else if (mTestOutputs[mCurOutput] != 0) {
3539 AudioParameter outputParam = AudioParameter();
3540 outputParam.addInt(String8("channels"), channels);
3541 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3542 }
3543 }
3544 }
3545 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3546 param.remove(String8("test_cmd_policy_sampleRate"));
3547 if (valueInt >= 0 && valueInt <= 96000) {
3548 int samplingRate = valueInt;
3549 if (target == "Manager") {
3550 mTestSamplingRate = samplingRate;
3551 } else if (mTestOutputs[mCurOutput] != 0) {
3552 AudioParameter outputParam = AudioParameter();
3553 outputParam.addInt(String8("sampling_rate"), samplingRate);
3554 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3555 }
3556 }
3557 }
3558
3559 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3560 param.remove(String8("test_cmd_policy_reopen"));
3561
Eric Laurentc75307b2015-03-17 15:29:32 -07003562 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003563
Eric Laurentc75307b2015-03-17 15:29:32 -07003564 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003565
Eric Laurentc75307b2015-03-17 15:29:32 -07003566 removeOutput(mPrimaryOutput->mIoHandle);
3567 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3568 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003569 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003570 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3571 config.sample_rate = outputDesc->mSamplingRate;
3572 config.channel_mask = outputDesc->mChannelMask;
3573 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003574 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003575 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003576 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003577 &config,
3578 &outputDesc->mDevice,
3579 String8(""),
3580 &outputDesc->mLatency,
3581 outputDesc->mFlags);
3582 if (status != NO_ERROR) {
3583 ALOGE("Failed to reopen hardware output stream, "
3584 "samplingRate: %d, format %d, channels %d",
3585 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003586 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003587 outputDesc->mSamplingRate = config.sample_rate;
3588 outputDesc->mChannelMask = config.channel_mask;
3589 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003590 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003591 AudioParameter outputCmd = AudioParameter();
3592 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003593 mpClientInterface->setParameters(handle, outputCmd.toString());
3594 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003595 }
3596 }
3597
3598
3599 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3600 }
3601 }
3602 return false;
3603}
3604
Eric Laurente0720872014-03-11 09:30:41 -07003605void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003606{
3607 {
3608 AutoMutex _l(mLock);
3609 requestExit();
3610 mWaitWorkCV.signal();
3611 }
3612 requestExitAndWait();
3613}
3614
Eric Laurente0720872014-03-11 09:30:41 -07003615int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003616{
3617 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3618 if (output == mTestOutputs[i]) return i;
3619 }
3620 return 0;
3621}
3622#endif //AUDIO_POLICY_TEST
3623
3624// ---
3625
Eric Laurentc75307b2015-03-17 15:29:32 -07003626void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003627{
François Gaffie98cc1912015-03-18 17:52:40 +01003628 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003629 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003630 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003631 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003632}
3633
François Gaffie53615e22015-03-19 09:24:12 +01003634void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3635{
3636 mOutputs.removeItem(output);
3637}
3638
Eric Laurent1f2f2232014-06-02 12:01:23 -07003639void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003640{
François Gaffie98cc1912015-03-18 17:52:40 +01003641 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003642 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003643 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003644}
Eric Laurente552edb2014-03-10 17:42:56 -07003645
Eric Laurentc75307b2015-03-17 15:29:32 -07003646void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003647 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003648 const String8 address /*in*/,
3649 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003650 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003651 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003652 if (devDesc != 0) {
3653 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3654 desc->mIoHandle, address.string());
3655 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003656 }
3657}
3658
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003659status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003660 audio_policy_dev_state_t state,
3661 SortedVector<audio_io_handle_t>& outputs,
3662 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003663{
François Gaffie53615e22015-03-19 09:24:12 +01003664 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003665 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003666
3667 if (audio_device_is_digital(device)) {
3668 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003669 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003670 }
Eric Laurente552edb2014-03-10 17:42:56 -07003671
Eric Laurent3b73df72014-03-11 09:06:29 -07003672 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003673 // first list already open outputs that can be routed to this device
3674 for (size_t i = 0; i < mOutputs.size(); i++) {
3675 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003676 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003677 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003678 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3679 outputs.add(mOutputs.keyAt(i));
3680 } else {
3681 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003682 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003683 }
Eric Laurente552edb2014-03-10 17:42:56 -07003684 }
3685 }
3686 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003687 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003688 for (size_t i = 0; i < mHwModules.size(); i++)
3689 {
3690 if (mHwModules[i]->mHandle == 0) {
3691 continue;
3692 }
3693 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3694 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003695 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003696 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003697 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003698 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003699 profiles.add(profile);
3700 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3701 }
Eric Laurente552edb2014-03-10 17:42:56 -07003702 }
3703 }
3704 }
3705
Eric Laurent7b279bb2015-12-14 10:18:23 -08003706 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003707
Eric Laurente552edb2014-03-10 17:42:56 -07003708 if (profiles.isEmpty() && outputs.isEmpty()) {
3709 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3710 return BAD_VALUE;
3711 }
3712
3713 // open outputs for matching profiles if needed. Direct outputs are also opened to
3714 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3715 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003716 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003717
3718 // nothing to do if one output is already opened for this profile
3719 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003720 for (j = 0; j < outputs.size(); j++) {
3721 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003722 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003723 // matching profile: save the sample rates, format and channel masks supported
3724 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003725 if (audio_device_is_digital(device)) {
3726 devDesc->importAudioPort(profile);
3727 }
Eric Laurente552edb2014-03-10 17:42:56 -07003728 break;
3729 }
3730 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003731 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003732 continue;
3733 }
3734
Eric Laurent83b88082014-06-20 18:31:16 -07003735 ALOGV("opening output for device %08x with params %s profile %p",
3736 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003737 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003738 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003739 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3740 config.sample_rate = desc->mSamplingRate;
3741 config.channel_mask = desc->mChannelMask;
3742 config.format = desc->mFormat;
3743 config.offload_info.sample_rate = desc->mSamplingRate;
3744 config.offload_info.channel_mask = desc->mChannelMask;
3745 config.offload_info.format = desc->mFormat;
3746 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003747 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003748 &output,
3749 &config,
3750 &desc->mDevice,
3751 address,
3752 &desc->mLatency,
3753 desc->mFlags);
3754 if (status == NO_ERROR) {
3755 desc->mSamplingRate = config.sample_rate;
3756 desc->mChannelMask = config.channel_mask;
3757 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003758
Eric Laurentd4692962014-05-05 18:13:44 -07003759 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003760 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003761 char *param = audio_device_address_to_parameter(device, address);
3762 mpClientInterface->setParameters(output, String8(param));
3763 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003764 }
Phil Burk00eeb322016-03-31 12:41:00 -07003765 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003766 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003767 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003768 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003769 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003770 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003771 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003772 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003773 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003774 config.offload_info.sample_rate = config.sample_rate;
3775 config.offload_info.channel_mask = config.channel_mask;
3776 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003777 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003778 &output,
3779 &config,
3780 &desc->mDevice,
3781 address,
3782 &desc->mLatency,
3783 desc->mFlags);
3784 if (status == NO_ERROR) {
3785 desc->mSamplingRate = config.sample_rate;
3786 desc->mChannelMask = config.channel_mask;
3787 desc->mFormat = config.format;
3788 } else {
3789 output = AUDIO_IO_HANDLE_NONE;
3790 }
Eric Laurentd4692962014-05-05 18:13:44 -07003791 }
3792
Eric Laurentcf2c0212014-07-25 16:20:43 -07003793 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003794 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003795 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003796 sp<AudioPolicyMix> policyMix;
3797 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003798 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3799 address.string());
3800 }
François Gaffie036e1e92015-03-19 10:16:24 +01003801 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003802 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003803
Eric Laurent87ffa392015-05-22 10:32:38 -07003804 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3805 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003806 // no duplicated output for direct outputs and
3807 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003808 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003809
Eric Laurentd4692962014-05-05 18:13:44 -07003810 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003811 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003812
Eric Laurentd4692962014-05-05 18:13:44 -07003813 //TODO: configure audio effect output stage here
3814
3815 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003816 duplicatedOutput =
3817 mpClientInterface->openDuplicateOutput(output,
3818 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003819 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003820 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003821 sp<SwAudioOutputDescriptor> dupOutputDesc =
3822 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3823 dupOutputDesc->mOutput1 = mPrimaryOutput;
3824 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003825 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3826 dupOutputDesc->mFormat = desc->mFormat;
3827 dupOutputDesc->mChannelMask = desc->mChannelMask;
3828 dupOutputDesc->mLatency = desc->mLatency;
3829 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003830 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003831 } else {
3832 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003833 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003834 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003835 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003836 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003837 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003838 }
Eric Laurente552edb2014-03-10 17:42:56 -07003839 }
3840 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003841 } else {
3842 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003843 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003844 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003845 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003846 profiles.removeAt(profile_index);
3847 profile_index--;
3848 } else {
3849 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003850 // Load digital format info only for digital devices
3851 if (audio_device_is_digital(device)) {
3852 devDesc->importAudioPort(profile);
3853 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003854
François Gaffie53615e22015-03-19 09:24:12 +01003855 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003856 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3857 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003858 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003859 NULL/*patch handle*/, address.string());
3860 }
Eric Laurente552edb2014-03-10 17:42:56 -07003861 ALOGV("checkOutputsForDevice(): adding output %d", output);
3862 }
3863 }
3864
3865 if (profiles.isEmpty()) {
3866 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3867 return BAD_VALUE;
3868 }
Eric Laurentd4692962014-05-05 18:13:44 -07003869 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003870 // check if one opened output is not needed any more after disconnecting one device
3871 for (size_t i = 0; i < mOutputs.size(); i++) {
3872 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003873 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003874 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003875 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003876 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003877 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003878 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003879 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3880 mOutputs.keyAt(i));
3881 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003882 }
Eric Laurente552edb2014-03-10 17:42:56 -07003883 }
3884 }
Eric Laurentd4692962014-05-05 18:13:44 -07003885 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003886 for (size_t i = 0; i < mHwModules.size(); i++)
3887 {
3888 if (mHwModules[i]->mHandle == 0) {
3889 continue;
3890 }
3891 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3892 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003893 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003894 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003895 ALOGV("checkOutputsForDevice(): "
3896 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003897 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003898 }
3899 }
3900 }
3901 }
3902 return NO_ERROR;
3903}
3904
Paul McLean9080a4c2015-06-18 08:24:02 -07003905status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003906 audio_policy_dev_state_t state,
3907 SortedVector<audio_io_handle_t>& inputs,
3908 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003909{
Paul McLean9080a4c2015-06-18 08:24:02 -07003910 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003911 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003912
3913 if (audio_device_is_digital(device)) {
3914 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003915 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003916 }
3917
Eric Laurentd4692962014-05-05 18:13:44 -07003918 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3919 // first list already open inputs that can be routed to this device
3920 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3921 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003922 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003923 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3924 inputs.add(mInputs.keyAt(input_index));
3925 }
3926 }
3927
3928 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003929 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003930 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3931 {
3932 if (mHwModules[module_idx]->mHandle == 0) {
3933 continue;
3934 }
3935 for (size_t profile_index = 0;
3936 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3937 profile_index++)
3938 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003939 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3940
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003941 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003942 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003943 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003944 profiles.add(profile);
3945 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3946 profile_index, module_idx);
3947 }
Eric Laurentd4692962014-05-05 18:13:44 -07003948 }
3949 }
3950 }
3951
3952 if (profiles.isEmpty() && inputs.isEmpty()) {
3953 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3954 return BAD_VALUE;
3955 }
3956
3957 // open inputs for matching profiles if needed. Direct inputs are also opened to
3958 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3959 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3960
Eric Laurent1c333e22014-05-20 10:48:17 -07003961 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003962 // nothing to do if one input is already opened for this profile
3963 size_t input_index;
3964 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3965 desc = mInputs.valueAt(input_index);
3966 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003967 if (audio_device_is_digital(device)) {
3968 devDesc->importAudioPort(profile);
3969 }
Eric Laurentd4692962014-05-05 18:13:44 -07003970 break;
3971 }
3972 }
3973 if (input_index != mInputs.size()) {
3974 continue;
3975 }
3976
3977 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3978 desc = new AudioInputDescriptor(profile);
3979 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003980 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3981 config.sample_rate = desc->mSamplingRate;
3982 config.channel_mask = desc->mChannelMask;
3983 config.format = desc->mFormat;
3984 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003985 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003986 &input,
3987 &config,
3988 &desc->mDevice,
3989 address,
3990 AUDIO_SOURCE_MIC,
3991 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003992
Eric Laurentcf2c0212014-07-25 16:20:43 -07003993 if (status == NO_ERROR) {
3994 desc->mSamplingRate = config.sample_rate;
3995 desc->mChannelMask = config.channel_mask;
3996 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003997
Eric Laurentd4692962014-05-05 18:13:44 -07003998 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003999 char *param = audio_device_address_to_parameter(device, address);
4000 mpClientInterface->setParameters(input, String8(param));
4001 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004002 }
Phil Burk00eeb322016-03-31 12:41:00 -07004003 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004004 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004005 ALOGW("checkInputsForDevice() direct input missing param");
4006 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004007 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004008 }
4009
4010 if (input != 0) {
4011 addInput(input, desc);
4012 }
4013 } // endif input != 0
4014
Eric Laurentcf2c0212014-07-25 16:20:43 -07004015 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004016 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004017 profiles.removeAt(profile_index);
4018 profile_index--;
4019 } else {
4020 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004021 if (audio_device_is_digital(device)) {
4022 devDesc->importAudioPort(profile);
4023 }
Eric Laurentd4692962014-05-05 18:13:44 -07004024 ALOGV("checkInputsForDevice(): adding input %d", input);
4025 }
4026 } // end scan profiles
4027
4028 if (profiles.isEmpty()) {
4029 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4030 return BAD_VALUE;
4031 }
4032 } else {
4033 // Disconnect
4034 // check if one opened input is not needed any more after disconnecting one device
4035 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4036 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004037 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004038 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4039 mInputs.keyAt(input_index));
4040 inputs.add(mInputs.keyAt(input_index));
4041 }
4042 }
4043 // Clear any profiles associated with the disconnected device.
4044 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4045 if (mHwModules[module_index]->mHandle == 0) {
4046 continue;
4047 }
4048 for (size_t profile_index = 0;
4049 profile_index < mHwModules[module_index]->mInputProfiles.size();
4050 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004051 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004052 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004053 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004054 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004055 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004056 }
4057 }
4058 }
4059 } // end disconnect
4060
4061 return NO_ERROR;
4062}
4063
4064
Eric Laurente0720872014-03-11 09:30:41 -07004065void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004066{
4067 ALOGV("closeOutput(%d)", output);
4068
Eric Laurentc75307b2015-03-17 15:29:32 -07004069 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004070 if (outputDesc == NULL) {
4071 ALOGW("closeOutput() unknown output %d", output);
4072 return;
4073 }
François Gaffie036e1e92015-03-19 10:16:24 +01004074 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004075
Eric Laurente552edb2014-03-10 17:42:56 -07004076 // look for duplicated outputs connected to the output being removed.
4077 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004078 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004079 if (dupOutputDesc->isDuplicated() &&
4080 (dupOutputDesc->mOutput1 == outputDesc ||
4081 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004082 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004083 if (dupOutputDesc->mOutput1 == outputDesc) {
4084 outputDesc2 = dupOutputDesc->mOutput2;
4085 } else {
4086 outputDesc2 = dupOutputDesc->mOutput1;
4087 }
4088 // As all active tracks on duplicated output will be deleted,
4089 // and as they were also referenced on the other output, the reference
4090 // count for their stream type must be adjusted accordingly on
4091 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004092 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004093 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004094 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004095 }
4096 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4097 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4098
4099 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004100 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004101 }
4102 }
4103
Eric Laurent05b90f82014-08-27 15:32:29 -07004104 nextAudioPortGeneration();
4105
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004106 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004107 if (index >= 0) {
4108 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004109 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004110 mAudioPatches.removeItemsAt(index);
4111 mpClientInterface->onAudioPatchListUpdate();
4112 }
4113
Eric Laurente552edb2014-03-10 17:42:56 -07004114 AudioParameter param;
4115 param.add(String8("closing"), String8("true"));
4116 mpClientInterface->setParameters(output, param.toString());
4117
4118 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004119 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004120 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004121}
4122
4123void AudioPolicyManager::closeInput(audio_io_handle_t input)
4124{
4125 ALOGV("closeInput(%d)", input);
4126
4127 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4128 if (inputDesc == NULL) {
4129 ALOGW("closeInput() unknown input %d", input);
4130 return;
4131 }
4132
Eric Laurent6a94d692014-05-20 11:18:06 -07004133 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004134
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004135 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004136 if (index >= 0) {
4137 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004138 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004139 mAudioPatches.removeItemsAt(index);
4140 mpClientInterface->onAudioPatchListUpdate();
4141 }
4142
4143 mpClientInterface->closeInput(input);
4144 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004145}
4146
Eric Laurentc75307b2015-03-17 15:29:32 -07004147SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4148 audio_devices_t device,
4149 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004150{
4151 SortedVector<audio_io_handle_t> outputs;
4152
4153 ALOGVV("getOutputsForDevice() device %04x", device);
4154 for (size_t i = 0; i < openOutputs.size(); i++) {
4155 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004156 i, openOutputs.valueAt(i)->isDuplicated(),
4157 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004158 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4159 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4160 outputs.add(openOutputs.keyAt(i));
4161 }
4162 }
4163 return outputs;
4164}
4165
Eric Laurente0720872014-03-11 09:30:41 -07004166bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004167 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004168{
4169 if (outputs1.size() != outputs2.size()) {
4170 return false;
4171 }
4172 for (size_t i = 0; i < outputs1.size(); i++) {
4173 if (outputs1[i] != outputs2[i]) {
4174 return false;
4175 }
4176 }
4177 return true;
4178}
4179
Eric Laurente0720872014-03-11 09:30:41 -07004180void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004181{
4182 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4183 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4184 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4185 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4186
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004187 // also take into account external policy-related changes: add all outputs which are
4188 // associated with policies in the "before" and "after" output vectors
4189 ALOGVV("checkOutputForStrategy(): policy related outputs");
4190 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004191 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004192 if (desc != 0 && desc->mPolicyMix != NULL) {
4193 srcOutputs.add(desc->mIoHandle);
4194 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4195 }
4196 }
4197 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004198 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004199 if (desc != 0 && desc->mPolicyMix != NULL) {
4200 dstOutputs.add(desc->mIoHandle);
4201 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4202 }
4203 }
4204
Eric Laurente552edb2014-03-10 17:42:56 -07004205 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4206 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4207 strategy, srcOutputs[0], dstOutputs[0]);
4208 // mute strategy while moving tracks from one output to another
4209 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004210 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004211 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004212 setStrategyMute(strategy, true, desc);
4213 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004214 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004215 sp<AudioSourceDescriptor> source =
4216 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4217 if (source != 0){
4218 connectAudioSource(source);
4219 }
Eric Laurente552edb2014-03-10 17:42:56 -07004220 }
4221
4222 // Move effects associated to this strategy from previous output to new output
4223 if (strategy == STRATEGY_MEDIA) {
4224 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4225 SortedVector<audio_io_handle_t> moved;
4226 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004227 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4228 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4229 effectDesc->mIo != fxOutput) {
4230 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004231 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4232 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004233 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004234 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004235 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004236 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004237 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004238 }
4239 }
4240 }
4241 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004242 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004243 if (getStrategy((audio_stream_type_t)i) == strategy) {
4244 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004245 }
4246 }
4247 }
4248}
4249
Eric Laurente0720872014-03-11 09:30:41 -07004250void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004251{
François Gaffie2110e042015-03-24 08:41:51 +01004252 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004253 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004254 checkOutputForStrategy(STRATEGY_PHONE);
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_SONIFICATION);
4258 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004259 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004260 checkOutputForStrategy(STRATEGY_MEDIA);
4261 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004262 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004263}
4264
Eric Laurente0720872014-03-11 09:30:41 -07004265void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004266{
François Gaffie53615e22015-03-19 09:24:12 +01004267 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004268 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004269 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004270 return;
4271 }
4272
Eric Laurent3a4311c2014-03-17 12:00:47 -07004273 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004274 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4275 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4276 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004277 // suspend A2DP output if:
4278 // (NOT already suspended) &&
4279 // ((SCO device is connected &&
4280 // (forced usage for communication || for record is SCO))) ||
4281 // (phone state is ringing || in call)
4282 //
4283 // restore A2DP output if:
4284 // (Already suspended) &&
4285 // ((SCO device is NOT connected ||
4286 // (forced usage NOT for communication && NOT for record is SCO))) &&
4287 // (phone state is NOT ringing && NOT in call)
4288 //
4289 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004290 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004291 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4292 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4293 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4294 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004295
4296 mpClientInterface->restoreOutput(a2dpOutput);
4297 mA2dpSuspended = false;
4298 }
4299 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004300 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004301 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4302 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4303 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4304 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004305
4306 mpClientInterface->suspendOutput(a2dpOutput);
4307 mA2dpSuspended = true;
4308 }
4309 }
4310}
4311
Eric Laurentc75307b2015-03-17 15:29:32 -07004312audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4313 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004314{
4315 audio_devices_t device = AUDIO_DEVICE_NONE;
4316
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004317 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004318 if (index >= 0) {
4319 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4320 if (patchDesc->mUid != mUidCached) {
4321 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004322 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004323 return outputDesc->device();
4324 }
4325 }
4326
Eric Laurente552edb2014-03-10 17:42:56 -07004327 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004328 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004329 // use device for strategy enforced audible
4330 // 2: we are in call or the strategy phone is active on the output:
4331 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004332 // 3: the strategy for enforced audible is active but not enforced on the output:
4333 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004334 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004335 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004336 // 5: the strategy accessibility is active on the output:
4337 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004338 // 6: the strategy "respectful" sonification is active on the output:
4339 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004340 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004341 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004342 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004343 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004344 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004345 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004346 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004347 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004348 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4349 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004350 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004351 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004352 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004353 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004354 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004355 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004356 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4357 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004358 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004359 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004360 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004361 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004362 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004363 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004364 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004365 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004366 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004367 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004368 }
4369
Eric Laurent1c333e22014-05-20 10:48:17 -07004370 ALOGV("getNewOutputDevice() selected device %x", device);
4371 return device;
4372}
4373
Eric Laurent232f26f2016-02-17 18:06:27 -08004374audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004375{
Eric Laurent232f26f2016-02-17 18:06:27 -08004376 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004377
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004378 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004379 if (index >= 0) {
4380 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4381 if (patchDesc->mUid != mUidCached) {
4382 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004383 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004384 return inputDesc->mDevice;
4385 }
4386 }
4387
Eric Laurent232f26f2016-02-17 18:06:27 -08004388 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004389
Eric Laurente552edb2014-03-10 17:42:56 -07004390 return device;
4391}
4392
Eric Laurent794fde22016-03-11 09:50:45 -08004393bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4394 audio_stream_type_t stream2) {
4395 return ((stream1 == stream2) ||
4396 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4397 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004398}
4399
Eric Laurente0720872014-03-11 09:30:41 -07004400uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004401 return (uint32_t)getStrategy(stream);
4402}
4403
Eric Laurente0720872014-03-11 09:30:41 -07004404audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004405 // By checking the range of stream before calling getStrategy, we avoid
4406 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4407 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004408 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004409 return AUDIO_DEVICE_NONE;
4410 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004411 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004412 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4413 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004414 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004415 }
Eric Laurent794fde22016-03-11 09:50:45 -08004416 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004417 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004418 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004419 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4420 for (size_t i = 0; i < outputs.size(); i++) {
4421 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004422 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004423 curDevices |= outputDesc->device();
4424 }
4425 }
4426 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004427 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004428
4429 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4430 and doesn't really need to.*/
4431 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4432 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4433 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4434 }
Eric Laurente552edb2014-03-10 17:42:56 -07004435 return devices;
4436}
4437
François Gaffie2110e042015-03-24 08:41:51 +01004438routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4439{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004440 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004441 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004442}
4443
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004444uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4445 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004446 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4447 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4448 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004449 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4450 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4451 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004452 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004453 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004454}
4455
Eric Laurente0720872014-03-11 09:30:41 -07004456void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004457 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004458 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004459 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4460 updateDevicesAndOutputs();
4461 break;
4462 default:
4463 break;
4464 }
4465}
4466
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004467uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004468
4469 // skip beacon mute management if a dedicated TTS output is available
4470 if (mTtsOutputAvailable) {
4471 return 0;
4472 }
4473
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004474 switch(event) {
4475 case STARTING_OUTPUT:
4476 mBeaconMuteRefCount++;
4477 break;
4478 case STOPPING_OUTPUT:
4479 if (mBeaconMuteRefCount > 0) {
4480 mBeaconMuteRefCount--;
4481 }
4482 break;
4483 case STARTING_BEACON:
4484 mBeaconPlayingRefCount++;
4485 break;
4486 case STOPPING_BEACON:
4487 if (mBeaconPlayingRefCount > 0) {
4488 mBeaconPlayingRefCount--;
4489 }
4490 break;
4491 }
4492
4493 if (mBeaconMuteRefCount > 0) {
4494 // any playback causes beacon to be muted
4495 return setBeaconMute(true);
4496 } else {
4497 // no other playback: unmute when beacon starts playing, mute when it stops
4498 return setBeaconMute(mBeaconPlayingRefCount == 0);
4499 }
4500}
4501
4502uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4503 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4504 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4505 // keep track of muted state to avoid repeating mute/unmute operations
4506 if (mBeaconMuted != mute) {
4507 // mute/unmute AUDIO_STREAM_TTS on all outputs
4508 ALOGV("\t muting %d", mute);
4509 uint32_t maxLatency = 0;
4510 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004511 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004512 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004513 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004514 0 /*delay*/, AUDIO_DEVICE_NONE);
4515 const uint32_t latency = desc->latency() * 2;
4516 if (latency > maxLatency) {
4517 maxLatency = latency;
4518 }
4519 }
4520 mBeaconMuted = mute;
4521 return maxLatency;
4522 }
4523 return 0;
4524}
4525
Eric Laurente0720872014-03-11 09:30:41 -07004526audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004527 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004528{
Paul McLeanaa981192015-03-21 09:55:15 -07004529 // Routing
4530 // see if we have an explicit route
4531 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4532 // (getStrategy(stream)).
4533 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4534 // and activity count is non-zero
4535 // the device = the device from the descriptor in the RouteMap, and exit.
4536 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4537 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004538 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4539 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004540 return route->mDeviceDescriptor->type();
4541 }
4542 }
4543
Eric Laurente552edb2014-03-10 17:42:56 -07004544 if (fromCache) {
4545 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4546 strategy, mDeviceForStrategy[strategy]);
4547 return mDeviceForStrategy[strategy];
4548 }
François Gaffie2110e042015-03-24 08:41:51 +01004549 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004550}
4551
Eric Laurente0720872014-03-11 09:30:41 -07004552void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004553{
4554 for (int i = 0; i < NUM_STRATEGIES; i++) {
4555 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4556 }
4557 mPreviousOutputs = mOutputs;
4558}
4559
Eric Laurent1f2f2232014-06-02 12:01:23 -07004560uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004561 audio_devices_t prevDevice,
4562 uint32_t delayMs)
4563{
4564 // mute/unmute strategies using an incompatible device combination
4565 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4566 // if unmuting, unmute only after the specified delay
4567 if (outputDesc->isDuplicated()) {
4568 return 0;
4569 }
4570
4571 uint32_t muteWaitMs = 0;
4572 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004573 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004574
4575 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4576 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004577 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004578 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4579 bool doMute = false;
4580
4581 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4582 doMute = true;
4583 outputDesc->mStrategyMutedByDevice[i] = true;
4584 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4585 doMute = true;
4586 outputDesc->mStrategyMutedByDevice[i] = false;
4587 }
Eric Laurent99401132014-05-07 19:48:15 -07004588 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004589 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004590 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004591 // skip output if it does not share any device with current output
4592 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4593 == AUDIO_DEVICE_NONE) {
4594 continue;
4595 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004596 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4597 mute ? "muting" : "unmuting", i, curDevice);
4598 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004599 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004600 if (mute) {
4601 // FIXME: should not need to double latency if volume could be applied
4602 // immediately by the audioflinger mixer. We must account for the delay
4603 // between now and the next time the audioflinger thread for this output
4604 // will process a buffer (which corresponds to one buffer size,
4605 // usually 1/2 or 1/4 of the latency).
4606 if (muteWaitMs < desc->latency() * 2) {
4607 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004608 }
4609 }
4610 }
4611 }
4612 }
4613 }
4614
Eric Laurent99401132014-05-07 19:48:15 -07004615 // temporary mute output if device selection changes to avoid volume bursts due to
4616 // different per device volumes
4617 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004618 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4619 // temporary mute duration is conservatively set to 4 times the reported latency
4620 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4621 if (muteWaitMs < tempMuteWaitMs) {
4622 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004623 }
Eric Laurentdc462862016-07-19 12:29:53 -07004624
Eric Laurent99401132014-05-07 19:48:15 -07004625 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004626 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004627 // make sure that we do not start the temporary mute period too early in case of
4628 // delayed device change
4629 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004630 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004631 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004632 }
4633 }
4634 }
4635
Eric Laurente552edb2014-03-10 17:42:56 -07004636 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4637 if (muteWaitMs > delayMs) {
4638 muteWaitMs -= delayMs;
4639 usleep(muteWaitMs * 1000);
4640 return muteWaitMs;
4641 }
4642 return 0;
4643}
4644
Eric Laurentc75307b2015-03-17 15:29:32 -07004645uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004646 audio_devices_t device,
4647 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004648 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004649 audio_patch_handle_t *patchHandle,
4650 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004651{
Eric Laurentc75307b2015-03-17 15:29:32 -07004652 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004653 AudioParameter param;
4654 uint32_t muteWaitMs;
4655
4656 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004657 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4658 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004659 return muteWaitMs;
4660 }
4661 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4662 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004663 if ((device != AUDIO_DEVICE_NONE) &&
4664 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004665 return 0;
4666 }
4667
4668 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004669 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004670
4671 audio_devices_t prevDevice = outputDesc->mDevice;
4672
Paul McLeanaa981192015-03-21 09:55:15 -07004673 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004674
4675 if (device != AUDIO_DEVICE_NONE) {
4676 outputDesc->mDevice = device;
4677 }
4678 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4679
4680 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004681 // the requested device is AUDIO_DEVICE_NONE
4682 // OR the requested device is the same as current device
4683 // AND force is not specified
4684 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004685 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004686 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4687 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004688 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004689 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004690 return muteWaitMs;
4691 }
4692
4693 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004694
Eric Laurente552edb2014-03-10 17:42:56 -07004695 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004696 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004697 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004698 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004699 DeviceVector deviceList;
4700 if ((address == NULL) || (strlen(address) == 0)) {
4701 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4702 } else {
4703 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4704 }
4705
Eric Laurent1c333e22014-05-20 10:48:17 -07004706 if (!deviceList.isEmpty()) {
4707 struct audio_patch patch;
4708 outputDesc->toAudioPortConfig(&patch.sources[0]);
4709 patch.num_sources = 1;
4710 patch.num_sinks = 0;
4711 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4712 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004713 patch.num_sinks++;
4714 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004715 ssize_t index;
4716 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4717 index = mAudioPatches.indexOfKey(*patchHandle);
4718 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004719 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004720 }
4721 sp< AudioPatch> patchDesc;
4722 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4723 if (index >= 0) {
4724 patchDesc = mAudioPatches.valueAt(index);
4725 afPatchHandle = patchDesc->mAfPatchHandle;
4726 }
4727
Eric Laurent1c333e22014-05-20 10:48:17 -07004728 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004729 &afPatchHandle,
4730 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004731 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4732 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004733 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004735 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004736 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004737 addAudioPatch(patchDesc->mHandle, patchDesc);
4738 } else {
4739 patchDesc->mPatch = patch;
4740 }
4741 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004742 if (patchHandle) {
4743 *patchHandle = patchDesc->mHandle;
4744 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004745 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004746 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004747 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004748 }
4749 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004750
4751 // inform all input as well
4752 for (size_t i = 0; i < mInputs.size(); i++) {
4753 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004754 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004755 AudioParameter inputCmd = AudioParameter();
4756 ALOGV("%s: inform input %d of device:%d", __func__,
4757 inputDescriptor->mIoHandle, device);
4758 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4759 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4760 inputCmd.toString(),
4761 delayMs);
4762 }
4763 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004764 }
Eric Laurente552edb2014-03-10 17:42:56 -07004765
4766 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004767 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004768
4769 return muteWaitMs;
4770}
4771
Eric Laurentc75307b2015-03-17 15:29:32 -07004772status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004773 int delayMs,
4774 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004775{
Eric Laurent6a94d692014-05-20 11:18:06 -07004776 ssize_t index;
4777 if (patchHandle) {
4778 index = mAudioPatches.indexOfKey(*patchHandle);
4779 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004780 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004781 }
4782 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004783 return INVALID_OPERATION;
4784 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004785 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4786 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004787 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004788 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004789 removeAudioPatch(patchDesc->mHandle);
4790 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004791 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004792 return status;
4793}
4794
4795status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4796 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004797 bool force,
4798 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004799{
4800 status_t status = NO_ERROR;
4801
Eric Laurent1f2f2232014-06-02 12:01:23 -07004802 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004803 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4804 inputDesc->mDevice = device;
4805
4806 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4807 if (!deviceList.isEmpty()) {
4808 struct audio_patch patch;
4809 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004810 // AUDIO_SOURCE_HOTWORD is for internal use only:
4811 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004812 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004813 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004814 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4815 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004816 patch.num_sinks = 1;
4817 //only one input device for now
4818 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004819 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004820 ssize_t index;
4821 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4822 index = mAudioPatches.indexOfKey(*patchHandle);
4823 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004824 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004825 }
4826 sp< AudioPatch> patchDesc;
4827 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4828 if (index >= 0) {
4829 patchDesc = mAudioPatches.valueAt(index);
4830 afPatchHandle = patchDesc->mAfPatchHandle;
4831 }
4832
Eric Laurent1c333e22014-05-20 10:48:17 -07004833 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004834 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004835 0);
4836 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004837 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004838 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004839 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004840 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004841 addAudioPatch(patchDesc->mHandle, patchDesc);
4842 } else {
4843 patchDesc->mPatch = patch;
4844 }
4845 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004846 if (patchHandle) {
4847 *patchHandle = patchDesc->mHandle;
4848 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004849 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004850 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004851 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004852 }
4853 }
4854 }
4855 return status;
4856}
4857
Eric Laurent6a94d692014-05-20 11:18:06 -07004858status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4859 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004860{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004861 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004862 ssize_t index;
4863 if (patchHandle) {
4864 index = mAudioPatches.indexOfKey(*patchHandle);
4865 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004866 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 }
4868 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004869 return INVALID_OPERATION;
4870 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004871 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4872 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004873 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004874 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004875 removeAudioPatch(patchDesc->mHandle);
4876 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004877 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004878 return status;
4879}
4880
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004881sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004882 String8 address,
4883 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004884 audio_format_t& format,
4885 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004886 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004887{
4888 // Choose an input profile based on the requested capture parameters: select the first available
4889 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004890 //
4891 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4892 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004893
4894 for (size_t i = 0; i < mHwModules.size(); i++)
4895 {
4896 if (mHwModules[i]->mHandle == 0) {
4897 continue;
4898 }
4899 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4900 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004901 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004902 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004903 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004904 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004905 format,
4906 &format /*updatedFormat*/,
4907 channelMask,
4908 &channelMask /*updatedChannelMask*/,
4909 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004910
Eric Laurente552edb2014-03-10 17:42:56 -07004911 return profile;
4912 }
4913 }
4914 }
4915 return NULL;
4916}
4917
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004918
4919audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004920 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004921{
François Gaffie036e1e92015-03-19 10:16:24 +01004922 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4923 audio_devices_t selectedDeviceFromMix =
4924 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004925
François Gaffie036e1e92015-03-19 10:16:24 +01004926 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4927 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004928 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004929 return getDeviceForInputSource(inputSource);
4930}
4931
4932audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4933{
Paul McLean466dc8e2015-04-17 13:15:36 -06004934 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4935 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004936 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004937 return route->mDeviceDescriptor->type();
4938 }
4939 }
4940
4941 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004942}
4943
Eric Laurente0720872014-03-11 09:30:41 -07004944float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004945 int index,
4946 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004947{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004948 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07004949
4950 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
4951 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
4952 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
4953 // the ringtone volume
4954 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
4955 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
4956 && isStreamActive(AUDIO_STREAM_RING, 0)) {
4957 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
4958 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
4959 }
4960
Eric Laurente552edb2014-03-10 17:42:56 -07004961 // if a headset is connected, apply the following rules to ring tones and notifications
4962 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004963 // - always attenuate notifications volume by 6dB
4964 // - attenuate ring tones volume by 6dB unless music is not playing and
4965 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004966 // - if music is playing, always limit the volume to current music volume,
4967 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004968 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004969 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4970 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4971 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4972 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4973 ((stream_strategy == STRATEGY_SONIFICATION)
4974 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004975 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004976 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004977 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004978 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004979 // when the phone is ringing we must consider that music could have been paused just before
4980 // by the music application and behave as if music was active if the last music track was
4981 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004982 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004983 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004984 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004985 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004986 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004987 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4988 musicDevice),
4989 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004990 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4991 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004992 if (volumeDB > minVolDB) {
4993 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004994 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004995 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004996 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4997 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4998 // on A2DP, also ensure notification volume is not too low compared to media when
4999 // intended to be played
5000 if ((volumeDB > -96.0f) &&
5001 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5002 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5003 stream, device,
5004 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5005 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5006 }
5007 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005008 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5009 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005010 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005011 }
5012 }
5013
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005014 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005015}
5016
Eric Laurente0720872014-03-11 09:30:41 -07005017status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005018 int index,
5019 const sp<AudioOutputDescriptor>& outputDesc,
5020 audio_devices_t device,
5021 int delayMs,
5022 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005023{
Eric Laurente552edb2014-03-10 17:42:56 -07005024 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005025 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005026 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005027 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005028 return NO_ERROR;
5029 }
François Gaffie2110e042015-03-24 08:41:51 +01005030 audio_policy_forced_cfg_t forceUseForComm =
5031 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005032 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005033 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5034 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005035 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005036 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005037 return INVALID_OPERATION;
5038 }
5039
Eric Laurentc75307b2015-03-17 15:29:32 -07005040 if (device == AUDIO_DEVICE_NONE) {
5041 device = outputDesc->device();
5042 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005043
Eric Laurentffbc80f2015-03-18 18:30:19 -07005044 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005045 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005046 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005047 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005048
Eric Laurentffbc80f2015-03-18 18:30:19 -07005049 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005050
Eric Laurent3b73df72014-03-11 09:06:29 -07005051 if (stream == AUDIO_STREAM_VOICE_CALL ||
5052 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005053 float voiceVolume;
5054 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005055 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005056 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005057 } else {
5058 voiceVolume = 1.0;
5059 }
5060
Eric Laurent18fba842016-03-31 14:41:26 -07005061 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005062 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5063 mLastVoiceVolume = voiceVolume;
5064 }
5065 }
5066
5067 return NO_ERROR;
5068}
5069
Eric Laurentc75307b2015-03-17 15:29:32 -07005070void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5071 audio_devices_t device,
5072 int delayMs,
5073 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005074{
Eric Laurentc75307b2015-03-17 15:29:32 -07005075 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005076
Eric Laurent794fde22016-03-11 09:50:45 -08005077 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005078 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005079 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005080 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005081 device,
5082 delayMs,
5083 force);
5084 }
5085}
5086
Eric Laurente0720872014-03-11 09:30:41 -07005087void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005088 bool on,
5089 const sp<AudioOutputDescriptor>& outputDesc,
5090 int delayMs,
5091 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005092{
Eric Laurent72249d52015-06-08 11:12:15 -07005093 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5094 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005095 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005096 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005097 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005098 }
5099 }
5100}
5101
Eric Laurente0720872014-03-11 09:30:41 -07005102void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005103 bool on,
5104 const sp<AudioOutputDescriptor>& outputDesc,
5105 int delayMs,
5106 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005107{
Eric Laurente552edb2014-03-10 17:42:56 -07005108 if (device == AUDIO_DEVICE_NONE) {
5109 device = outputDesc->device();
5110 }
5111
Eric Laurentc75307b2015-03-17 15:29:32 -07005112 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5113 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005114
5115 if (on) {
5116 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005117 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005118 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005119 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005120 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005121 }
5122 }
5123 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5124 outputDesc->mMuteCount[stream]++;
5125 } else {
5126 if (outputDesc->mMuteCount[stream] == 0) {
5127 ALOGV("setStreamMute() unmuting non muted stream!");
5128 return;
5129 }
5130 if (--outputDesc->mMuteCount[stream] == 0) {
5131 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005132 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005133 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005134 device,
5135 delayMs);
5136 }
5137 }
5138}
5139
Eric Laurente0720872014-03-11 09:30:41 -07005140void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005141 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005142{
Eric Laurent87ffa392015-05-22 10:32:38 -07005143 if(!hasPrimaryOutput()) {
5144 return;
5145 }
5146
Eric Laurente552edb2014-03-10 17:42:56 -07005147 // if the stream pertains to sonification strategy and we are in call we must
5148 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5149 // in the device used for phone strategy and play the tone if the selected device does not
5150 // interfere with the device used for phone strategy
5151 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5152 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005153 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005154 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5155 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005156 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005157 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5158 stream, starting, outputDesc->mDevice, stateChange);
5159 if (outputDesc->mRefCount[stream]) {
5160 int muteCount = 1;
5161 if (stateChange) {
5162 muteCount = outputDesc->mRefCount[stream];
5163 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005164 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005165 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5166 for (int i = 0; i < muteCount; i++) {
5167 setStreamMute(stream, starting, mPrimaryOutput);
5168 }
5169 } else {
5170 ALOGV("handleIncallSonification() high visibility");
5171 if (outputDesc->device() &
5172 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5173 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5174 for (int i = 0; i < muteCount; i++) {
5175 setStreamMute(stream, starting, mPrimaryOutput);
5176 }
5177 }
5178 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005179 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5180 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005181 } else {
5182 mpClientInterface->stopTone();
5183 }
5184 }
5185 }
5186 }
5187}
5188
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005189audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5190{
5191 // flags to stream type mapping
5192 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5193 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5194 }
5195 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5196 return AUDIO_STREAM_BLUETOOTH_SCO;
5197 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005198 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5199 return AUDIO_STREAM_TTS;
5200 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005201
5202 // usage to stream type mapping
5203 switch (attr->usage) {
5204 case AUDIO_USAGE_MEDIA:
5205 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005206 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5207 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005208 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5209 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005210 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5211 return AUDIO_STREAM_SYSTEM;
5212 case AUDIO_USAGE_VOICE_COMMUNICATION:
5213 return AUDIO_STREAM_VOICE_CALL;
5214
5215 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5216 return AUDIO_STREAM_DTMF;
5217
5218 case AUDIO_USAGE_ALARM:
5219 return AUDIO_STREAM_ALARM;
5220 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5221 return AUDIO_STREAM_RING;
5222
5223 case AUDIO_USAGE_NOTIFICATION:
5224 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5225 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5226 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5227 case AUDIO_USAGE_NOTIFICATION_EVENT:
5228 return AUDIO_STREAM_NOTIFICATION;
5229
5230 case AUDIO_USAGE_UNKNOWN:
5231 default:
5232 return AUDIO_STREAM_MUSIC;
5233 }
5234}
Eric Laurente83b55d2014-11-14 10:06:21 -08005235
François Gaffie53615e22015-03-19 09:24:12 +01005236bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5237{
Eric Laurente83b55d2014-11-14 10:06:21 -08005238 // has flags that map to a strategy?
5239 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5240 return true;
5241 }
5242
5243 // has known usage?
5244 switch (paa->usage) {
5245 case AUDIO_USAGE_UNKNOWN:
5246 case AUDIO_USAGE_MEDIA:
5247 case AUDIO_USAGE_VOICE_COMMUNICATION:
5248 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5249 case AUDIO_USAGE_ALARM:
5250 case AUDIO_USAGE_NOTIFICATION:
5251 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5252 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5253 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5254 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5255 case AUDIO_USAGE_NOTIFICATION_EVENT:
5256 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5257 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5258 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5259 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005260 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005261 break;
5262 default:
5263 return false;
5264 }
5265 return true;
5266}
5267
François Gaffiead3183e2015-03-18 16:55:35 +01005268bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5269 routing_strategy strategy, uint32_t inPastMs,
5270 nsecs_t sysTime) const
5271{
5272 if ((sysTime == 0) && (inPastMs != 0)) {
5273 sysTime = systemTime();
5274 }
Eric Laurent794fde22016-03-11 09:50:45 -08005275 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005276 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5277 (NUM_STRATEGIES == strategy)) &&
5278 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5279 return true;
5280 }
5281 }
5282 return false;
5283}
5284
François Gaffie2110e042015-03-24 08:41:51 +01005285audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5286{
5287 return mEngine->getForceUse(usage);
5288}
5289
5290bool AudioPolicyManager::isInCall()
5291{
5292 return isStateInCall(mEngine->getPhoneState());
5293}
5294
5295bool AudioPolicyManager::isStateInCall(int state)
5296{
5297 return is_state_in_call(state);
5298}
5299
Eric Laurentd60560a2015-04-10 11:31:20 -07005300void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5301{
5302 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5303 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5304 if (sourceDesc->mDevice->equals(deviceDesc)) {
5305 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5306 stopAudioSource(sourceDesc->getHandle());
5307 }
5308 }
5309
5310 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5311 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5312 bool release = false;
5313 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5314 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5315 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5316 source->ext.device.type == deviceDesc->type()) {
5317 release = true;
5318 }
5319 }
5320 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5321 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5322 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5323 sink->ext.device.type == deviceDesc->type()) {
5324 release = true;
5325 }
5326 }
5327 if (release) {
5328 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5329 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5330 }
5331 }
5332}
5333
Phil Burk09bc4612016-02-24 15:58:15 -08005334// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005335void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5336 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005337 // TODO Set this based on Config properties.
5338 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005339
5340 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5341 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005342 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005343
5344 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005345 bool supportsAC3 = false;
5346 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005347 bool supportsIEC61937 = false;
5348 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5349 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005350 switch (format) {
5351 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005352 supportsAC3 = true;
5353 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005354 case AUDIO_FORMAT_E_AC3:
5355 case AUDIO_FORMAT_DTS:
5356 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005357 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005358 break;
5359 case AUDIO_FORMAT_IEC61937:
5360 supportsIEC61937 = true;
5361 break;
5362 default:
5363 break;
5364 }
5365 }
Phil Burk09bc4612016-02-24 15:58:15 -08005366
5367 // Modify formats based on surround preferences.
5368 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005369 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5370 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5371 // Remove surround sound related formats.
5372 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5373 audio_format_t format = formats[formatIndex];
5374 switch(format) {
5375 case AUDIO_FORMAT_AC3:
5376 case AUDIO_FORMAT_E_AC3:
5377 case AUDIO_FORMAT_DTS:
5378 case AUDIO_FORMAT_DTS_HD:
5379 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005380 formats.removeAt(formatIndex);
5381 break;
5382 default:
5383 formatIndex++; // keep it
5384 break;
5385 }
Phil Burk09bc4612016-02-24 15:58:15 -08005386 }
Phil Burk07ac1142016-03-25 13:39:29 -07005387 supportsAC3 = false;
5388 supportsOtherSurround = false;
5389 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005390 }
Phil Burk07ac1142016-03-25 13:39:29 -07005391 } else { // AUTO or ALWAYS
5392 // Most TVs support AC3 even if they do not report it in the EDID.
5393 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5394 && !supportsAC3) {
5395 formats.add(AUDIO_FORMAT_AC3);
5396 supportsAC3 = true;
5397 }
5398
5399 // If ALWAYS, add support for raw surround formats if all are missing.
5400 // This assumes that if any of these formats are reported by the HAL
5401 // then the report is valid and should not be modified.
5402 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5403 && !supportsOtherSurround) {
5404 formats.add(AUDIO_FORMAT_E_AC3);
5405 formats.add(AUDIO_FORMAT_DTS);
5406 formats.add(AUDIO_FORMAT_DTS_HD);
5407 supportsOtherSurround = true;
5408 }
5409
5410 // Add support for IEC61937 if any raw surround supported.
5411 // The HAL could do this but add it here, just in case.
5412 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5413 formats.add(AUDIO_FORMAT_IEC61937);
5414 supportsIEC61937 = true;
5415 }
Phil Burk09bc4612016-02-24 15:58:15 -08005416 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005417}
5418
5419// Modify the list of channel masks supported.
5420void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5421 ChannelsVector &channelMasks = *channelMasksPtr;
5422 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5423 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5424
5425 // If NEVER, then remove support for channelMasks > stereo.
5426 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5427 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5428 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5429 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5430 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5431 channelMasks.removeAt(maskIndex);
5432 } else {
5433 maskIndex++;
5434 }
5435 }
5436 // If ALWAYS, then make sure we at least support 5.1
5437 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5438 bool supports5dot1 = false;
5439 // Are there any channel masks that can be considered "surround"?
5440 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5441 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5442 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5443 supports5dot1 = true;
5444 break;
5445 }
5446 }
5447 // If not then add 5.1 support.
5448 if (!supports5dot1) {
5449 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5450 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5451 }
Phil Burk09bc4612016-02-24 15:58:15 -08005452 }
5453}
5454
Phil Burk00eeb322016-03-31 12:41:00 -07005455void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5456 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005457 AudioProfileVector &profiles)
5458{
5459 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005460
François Gaffie112b0af2015-11-19 16:13:25 +01005461 // Format MUST be checked first to update the list of AudioProfile
5462 if (profiles.hasDynamicFormat()) {
5463 reply = mpClientInterface->getParameters(ioHandle,
5464 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005465 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005466 AudioParameter repliedParameters(reply);
5467 if (repliedParameters.get(
5468 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005469 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5470 return;
5471 }
Phil Burk09bc4612016-02-24 15:58:15 -08005472 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005473 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005474 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005475 }
Phil Burk09bc4612016-02-24 15:58:15 -08005476 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005477 }
5478 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5479
Eric Laurent20eb3a42016-01-26 18:39:17 -08005480 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005481 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005482 ChannelsVector channelMasks;
5483 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005484 AudioParameter requestedParameters;
5485 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5486
5487 if (profiles.hasDynamicRateFor(format)) {
5488 reply = mpClientInterface->getParameters(ioHandle,
5489 requestedParameters.toString() + ";" +
5490 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5491 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005492 AudioParameter repliedParameters(reply);
5493 if (repliedParameters.get(
5494 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5495 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005496 }
5497 }
5498 if (profiles.hasDynamicChannelsFor(format)) {
5499 reply = mpClientInterface->getParameters(ioHandle,
5500 requestedParameters.toString() + ";" +
5501 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5502 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005503 AudioParameter repliedParameters(reply);
5504 if (repliedParameters.get(
5505 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5506 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005507 if (device == AUDIO_DEVICE_OUT_HDMI) {
5508 filterSurroundChannelMasks(&channelMasks);
5509 }
François Gaffie112b0af2015-11-19 16:13:25 +01005510 }
5511 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005512 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005513 }
5514}
Eric Laurentd60560a2015-04-10 11:31:20 -07005515
Eric Laurente552edb2014-03-10 17:42:56 -07005516}; // namespace android