blob: b75254157b668f556f3d6423368618da53692de6 [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
1318 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001319 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001320 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001321 desc->isActive() &&
1322 outputDesc->sharesHwModuleWith(desc) &&
1323 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001324 setOutputDevice(desc,
1325 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001326 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001327 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001328 }
1329 }
1330 // update the outputs if stopping one with a stream that can affect notification routing
1331 handleNotificationRoutingForStream(stream);
1332 }
1333 return NO_ERROR;
1334 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001335 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001336 return INVALID_OPERATION;
1337 }
1338}
1339
Eric Laurente83b55d2014-11-14 10:06:21 -08001340void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001341 audio_stream_type_t stream __unused,
1342 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001343{
1344 ALOGV("releaseOutput() %d", output);
1345 ssize_t index = mOutputs.indexOfKey(output);
1346 if (index < 0) {
1347 ALOGW("releaseOutput() releasing unknown output %d", output);
1348 return;
1349 }
1350
1351#ifdef AUDIO_POLICY_TEST
1352 int testIndex = testOutputIndex(output);
1353 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001354 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001355 if (outputDesc->isActive()) {
1356 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001357 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001358 mTestOutputs[testIndex] = 0;
1359 }
1360 return;
1361 }
1362#endif //AUDIO_POLICY_TEST
1363
Paul McLeanaa981192015-03-21 09:55:15 -07001364 // Routing
1365 mOutputRoutes.removeRoute(session);
1366
Eric Laurentc75307b2015-03-17 15:29:32 -07001367 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001368 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001369 if (desc->mDirectOpenCount <= 0) {
1370 ALOGW("releaseOutput() invalid open count %d for output %d",
1371 desc->mDirectOpenCount, output);
1372 return;
1373 }
1374 if (--desc->mDirectOpenCount == 0) {
1375 closeOutput(output);
1376 // If effects where present on the output, audioflinger moved them to the primary
1377 // output by default: move them back to the appropriate output.
1378 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001379 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001380 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1381 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001382 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001383 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001384 }
1385 }
1386}
1387
1388
Eric Laurentcaf7f482014-11-25 17:50:47 -08001389status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1390 audio_io_handle_t *input,
1391 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001392 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001393 uint32_t samplingRate,
1394 audio_format_t format,
1395 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001396 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001397 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001398 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001399{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001400 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1401 "session %d, flags %#x",
1402 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001403
Eric Laurentcaf7f482014-11-25 17:50:47 -08001404 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001405 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001406 audio_devices_t device;
1407 // handle legacy remote submix case where the address was not always specified
1408 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001409 audio_source_t inputSource = attr->source;
1410 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001411 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001412
Eric Laurentc447ded2015-01-06 08:47:05 -08001413 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1414 inputSource = AUDIO_SOURCE_MIC;
1415 }
1416 halInputSource = inputSource;
1417
Paul McLean466dc8e2015-04-17 13:15:36 -06001418 // Explicit routing?
1419 sp<DeviceDescriptor> deviceDesc;
1420 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1421 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1422 deviceDesc = mAvailableInputDevices[i];
1423 break;
1424 }
1425 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001426 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001427
Eric Laurentc447ded2015-01-06 08:47:05 -08001428 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001429 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001430 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001431 if (ret != NO_ERROR) {
1432 return ret;
1433 }
1434 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001435 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1436 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001437 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001438 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001439 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001440 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001441 return BAD_VALUE;
1442 }
Eric Laurentc722f302014-12-10 11:21:49 -08001443 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001444 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001445 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1446 // there is an external policy, but this input is attached to a mix of recorders,
1447 // meaning it receives audio injected into the framework, so the recorder doesn't
1448 // know about it and is therefore considered "legacy"
1449 *inputType = API_INPUT_LEGACY;
1450 } else {
1451 // recording a mix of players defined by an external policy, we're rerouting for
1452 // an external policy
1453 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1454 }
Eric Laurentc722f302014-12-10 11:21:49 -08001455 } else if (audio_is_remote_submix_device(device)) {
1456 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001457 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001458 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1459 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001460 } else {
1461 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001462 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001463
Eric Laurent599c7582015-12-07 18:05:55 -08001464 }
1465
1466 *input = getInputForDevice(device, address, session, uid, inputSource,
1467 samplingRate, format, channelMask, flags,
1468 policyMix);
1469 if (*input == AUDIO_IO_HANDLE_NONE) {
1470 mInputRoutes.removeRoute(session);
1471 return INVALID_OPERATION;
1472 }
1473 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1474 return NO_ERROR;
1475}
1476
1477
1478audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1479 String8 address,
1480 audio_session_t session,
1481 uid_t uid,
1482 audio_source_t inputSource,
1483 uint32_t samplingRate,
1484 audio_format_t format,
1485 audio_channel_mask_t channelMask,
1486 audio_input_flags_t flags,
1487 AudioMix *policyMix)
1488{
1489 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1490 audio_source_t halInputSource = inputSource;
1491 bool isSoundTrigger = false;
1492
1493 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1494 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1495 if (index >= 0) {
1496 input = mSoundTriggerSessions.valueFor(session);
1497 isSoundTrigger = true;
1498 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1499 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1500 } else {
1501 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001502 }
1503 }
1504
Andy Hungf129b032015-04-07 13:45:50 -07001505 // find a compatible input profile (not necessarily identical in parameters)
1506 sp<IOProfile> profile;
1507 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001508 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001509 audio_format_t profileFormat = format;
1510 audio_channel_mask_t profileChannelMask = channelMask;
1511 audio_input_flags_t profileFlags = flags;
1512 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001513 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001514 profileSamplingRate, profileFormat, profileChannelMask,
1515 profileFlags);
1516 if (profile != 0) {
1517 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001518 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1519 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001520 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1521 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1522 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001523 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1524 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001525 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001526 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001527 }
Eric Laurente552edb2014-03-10 17:42:56 -07001528 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001529 // Pick input sampling rate if not specified by client
1530 if (samplingRate == 0) {
1531 samplingRate = profileSamplingRate;
1532 }
Eric Laurente552edb2014-03-10 17:42:56 -07001533
Eric Laurent322b4d22015-04-03 15:57:54 -07001534 if (profile->getModuleHandle() == 0) {
1535 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001536 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001537 }
1538
Eric Laurent599c7582015-12-07 18:05:55 -08001539 sp<AudioSession> audioSession = new AudioSession(session,
1540 inputSource,
1541 format,
1542 samplingRate,
1543 channelMask,
1544 flags,
1545 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001546 isSoundTrigger,
1547 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001548
Eric Laurent232f26f2016-02-17 18:06:27 -08001549// TODO enable input reuse
1550#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001551 // reuse an open input if possible
1552 for (size_t i = 0; i < mInputs.size(); i++) {
1553 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001554 // reuse input if it shares the same profile and same sound trigger attribute
1555 if (profile == desc->mProfile &&
1556 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001557
1558 sp<AudioSession> as = desc->getAudioSession(session);
1559 if (as != 0) {
1560 // do not allow unmatching properties on same session
1561 if (as->matches(audioSession)) {
1562 as->changeOpenCount(1);
1563 } else {
1564 ALOGW("getInputForDevice() record with different attributes"
1565 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001566 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001567 }
1568 } else {
1569 desc->addAudioSession(session, audioSession);
1570 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001571 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1572 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001573 }
1574 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001575#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001576
Eric Laurentcf2c0212014-07-25 16:20:43 -07001577 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001578 config.sample_rate = profileSamplingRate;
1579 config.channel_mask = profileChannelMask;
1580 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001581
Eric Laurent322b4d22015-04-03 15:57:54 -07001582 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001583 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001584 &config,
1585 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001586 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001587 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001588 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001589
1590 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001591 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001592 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001593 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001594 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001595 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1596 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001597 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001598 if (input != AUDIO_IO_HANDLE_NONE) {
1599 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001600 }
Eric Laurent599c7582015-12-07 18:05:55 -08001601 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001602 }
1603
Eric Laurent1f2f2232014-06-02 12:01:23 -07001604 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001605 inputDesc->mSamplingRate = profileSamplingRate;
1606 inputDesc->mFormat = profileFormat;
1607 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001608 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001609 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001610 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001611
Eric Laurent599c7582015-12-07 18:05:55 -08001612 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001613 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001614
Eric Laurent599c7582015-12-07 18:05:55 -08001615 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001616}
1617
Eric Laurent4dc68062014-07-28 17:26:49 -07001618status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001619 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001620{
1621 ALOGV("startInput() input %d", input);
1622 ssize_t index = mInputs.indexOfKey(input);
1623 if (index < 0) {
1624 ALOGW("startInput() unknown input %d", input);
1625 return BAD_VALUE;
1626 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001627 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001628
Eric Laurent599c7582015-12-07 18:05:55 -08001629 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1630 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001631 ALOGW("startInput() unknown session %d on input %d", session, input);
1632 return BAD_VALUE;
1633 }
1634
Eric Laurent232f26f2016-02-17 18:06:27 -08001635 // virtual input devices are compatible with other input devices
1636 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001637
Eric Laurent232f26f2016-02-17 18:06:27 -08001638 // for a non-virtual input device, check if there is another (non-virtual) active input
1639 audio_io_handle_t activeInput = mInputs.getActiveInput();
1640 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001641
Eric Laurent232f26f2016-02-17 18:06:27 -08001642 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1643 // otherwise the active input continues and the new input cannot be started.
1644 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1645 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1646 !activeDesc->hasPreemptedSession(session)) {
1647 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1648 //FIXME: consider all active sessions
1649 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1650 audio_session_t activeSession = activeSessions.keyAt(0);
1651 SortedVector<audio_session_t> sessions =
1652 activeDesc->getPreemptedSessions();
1653 sessions.add(activeSession);
1654 inputDesc->setPreemptedSessions(sessions);
1655 stopInput(activeInput, activeSession);
1656 releaseInput(activeInput, activeSession);
1657 } else {
1658 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1659 return INVALID_OPERATION;
1660 }
1661 }
1662
1663 // Do not allow capture if an active voice call is using a software patch and
1664 // the call TX source device is on the same HW module.
1665 // FIXME: would be better to refine to only inputs whose profile connects to the
1666 // call TX device but this information is not in the audio patch
1667 if (mCallTxPatch != 0 &&
1668 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1669 return INVALID_OPERATION;
1670 }
1671 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001672
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001673 // Routing?
1674 mInputRoutes.incRouteActivity(session);
1675
Eric Laurent232f26f2016-02-17 18:06:27 -08001676 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1677 // if input maps to a dynamic policy with an activity listener, notify of state change
1678 if ((inputDesc->mPolicyMix != NULL)
1679 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001680 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001681 MIX_STATE_MIXING);
1682 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001683
Eric Laurent232f26f2016-02-17 18:06:27 -08001684 if (mInputs.activeInputsCount() == 0) {
1685 SoundTrigger::setCaptureState(true);
1686 }
1687 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001688
Eric Laurent232f26f2016-02-17 18:06:27 -08001689 // automatically enable the remote submix output when input is started if not
1690 // used by a policy mix of type MIX_TYPE_RECORDERS
1691 // For remote submix (a virtual device), we open only one input per capture request.
1692 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1693 String8 address = String8("");
1694 if (inputDesc->mPolicyMix == NULL) {
1695 address = String8("0");
1696 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001697 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001698 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001699 if (address != "") {
1700 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1701 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1702 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001703 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001704 }
Eric Laurente552edb2014-03-10 17:42:56 -07001705 }
1706
Eric Laurent599c7582015-12-07 18:05:55 -08001707 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001708
Eric Laurent232f26f2016-02-17 18:06:27 -08001709 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001710 return NO_ERROR;
1711}
1712
Eric Laurent4dc68062014-07-28 17:26:49 -07001713status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1714 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001715{
1716 ALOGV("stopInput() input %d", input);
1717 ssize_t index = mInputs.indexOfKey(input);
1718 if (index < 0) {
1719 ALOGW("stopInput() unknown input %d", input);
1720 return BAD_VALUE;
1721 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001722 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001723
Eric Laurent599c7582015-12-07 18:05:55 -08001724 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001725 if (index < 0) {
1726 ALOGW("stopInput() unknown session %d on input %d", session, input);
1727 return BAD_VALUE;
1728 }
1729
Eric Laurent599c7582015-12-07 18:05:55 -08001730 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001731 ALOGW("stopInput() input %d already stopped", input);
1732 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001733 }
1734
Eric Laurent599c7582015-12-07 18:05:55 -08001735 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001736
1737 // Routing?
1738 mInputRoutes.decRouteActivity(session);
1739
Eric Laurent232f26f2016-02-17 18:06:27 -08001740 if (!inputDesc->isActive()) {
1741 // if input maps to a dynamic policy with an activity listener, notify of state change
1742 if ((inputDesc->mPolicyMix != NULL)
1743 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001744 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001745 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001746 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001747
1748 // automatically disable the remote submix output when input is stopped if not
1749 // used by a policy mix of type MIX_TYPE_RECORDERS
1750 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1751 String8 address = String8("");
1752 if (inputDesc->mPolicyMix == NULL) {
1753 address = String8("0");
1754 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001755 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001756 }
1757 if (address != "") {
1758 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1759 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1760 address, "remote-submix");
1761 }
1762 }
1763
1764 resetInputDevice(input);
1765
1766 if (mInputs.activeInputsCount() == 0) {
1767 SoundTrigger::setCaptureState(false);
1768 }
1769 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001770 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001771 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001772}
1773
Eric Laurent4dc68062014-07-28 17:26:49 -07001774void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1775 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001776{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001777
Eric Laurente552edb2014-03-10 17:42:56 -07001778 ALOGV("releaseInput() %d", input);
1779 ssize_t index = mInputs.indexOfKey(input);
1780 if (index < 0) {
1781 ALOGW("releaseInput() releasing unknown input %d", input);
1782 return;
1783 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001784
1785 // Routing
1786 mInputRoutes.removeRoute(session);
1787
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001788 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1789 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001790
Eric Laurent599c7582015-12-07 18:05:55 -08001791 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001792 if (index < 0) {
1793 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1794 return;
1795 }
Eric Laurent599c7582015-12-07 18:05:55 -08001796
1797 if (audioSession->openCount() == 0) {
1798 ALOGW("releaseInput() invalid open count %d on session %d",
1799 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001800 return;
1801 }
Eric Laurent599c7582015-12-07 18:05:55 -08001802
1803 if (audioSession->changeOpenCount(-1) == 0) {
1804 inputDesc->removeAudioSession(session);
1805 }
1806
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001807 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001808 ALOGV("releaseInput() exit > 0");
1809 return;
1810 }
1811
Eric Laurent05b90f82014-08-27 15:32:29 -07001812 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001813 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001814 ALOGV("releaseInput() exit");
1815}
1816
Eric Laurentd4692962014-05-05 18:13:44 -07001817void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001818 bool patchRemoved = false;
1819
Eric Laurentd4692962014-05-05 18:13:44 -07001820 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001821 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001822 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001823 if (patch_index >= 0) {
1824 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001825 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001826 mAudioPatches.removeItemsAt(patch_index);
1827 patchRemoved = true;
1828 }
Eric Laurentd4692962014-05-05 18:13:44 -07001829 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1830 }
1831 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001832 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001833 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001834
1835 if (patchRemoved) {
1836 mpClientInterface->onAudioPatchListUpdate();
1837 }
Eric Laurentd4692962014-05-05 18:13:44 -07001838}
1839
Eric Laurente0720872014-03-11 09:30:41 -07001840void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001841 int indexMin,
1842 int indexMax)
1843{
1844 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001845 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001846
1847 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001848 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1849 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001850 continue;
1851 }
1852 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001853 }
Eric Laurente552edb2014-03-10 17:42:56 -07001854}
1855
Eric Laurente0720872014-03-11 09:30:41 -07001856status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001857 int index,
1858 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001859{
1860
François Gaffied1ab2bd2015-12-02 18:20:06 +01001861 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1862 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001863 return BAD_VALUE;
1864 }
1865 if (!audio_is_output_device(device)) {
1866 return BAD_VALUE;
1867 }
1868
1869 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001870 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001871
Eric Laurent1fd372e2016-04-06 14:23:53 -07001872 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001873 stream, device, index);
1874
Eric Laurent28d09f02016-03-08 10:43:05 -08001875 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001876 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1877 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001878 continue;
1879 }
1880 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1881 }
Eric Laurente552edb2014-03-10 17:42:56 -07001882
Eric Laurent1fd372e2016-04-06 14:23:53 -07001883 // update volume on all outputs and streams matching the following:
1884 // - The requested stream (or a stream matching for volume control) is active on the output
1885 // - The device (or devices) selected by the strategy corresponding to this stream includes
1886 // the requested device
1887 // - For non default requested device, currently selected device on the output is either the
1888 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001889 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1890 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001891 status_t status = NO_ERROR;
1892 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001893 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1894 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001895 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1896 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001897 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001898 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001899 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1900 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001901 continue;
1902 }
Eric Laurent794fde22016-03-11 09:50:45 -08001903 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001904 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001905 if ((curStreamDevice & device) == 0) {
1906 continue;
1907 }
1908 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001909 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001910 curStreamDevice |= device;
1911 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1912 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1913 applyDefault = true;
1914 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001915
Eric Laurent1fd372e2016-04-06 14:23:53 -07001916 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurentdc462862016-07-19 12:29:53 -07001917 //FIXME: workaround for truncated touch sounds
1918 // delayed volume change for system stream to be removed when the problem is
1919 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001920 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001921 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1922 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001923 if (volStatus != NO_ERROR) {
1924 status = volStatus;
1925 }
1926 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001927 }
Eric Laurente552edb2014-03-10 17:42:56 -07001928 }
1929 return status;
1930}
1931
Eric Laurente0720872014-03-11 09:30:41 -07001932status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001933 int *index,
1934 audio_devices_t device)
1935{
1936 if (index == NULL) {
1937 return BAD_VALUE;
1938 }
1939 if (!audio_is_output_device(device)) {
1940 return BAD_VALUE;
1941 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001942 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001943 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001944 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001945 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1946 }
François Gaffiedfd74092015-03-19 12:10:59 +01001947 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001948
François Gaffied1ab2bd2015-12-02 18:20:06 +01001949 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001950 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1951 return NO_ERROR;
1952}
1953
Eric Laurente0720872014-03-11 09:30:41 -07001954audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001955 const SortedVector<audio_io_handle_t>& outputs)
1956{
1957 // select one output among several suitable for global effects.
1958 // The priority is as follows:
1959 // 1: An offloaded output. If the effect ends up not being offloadable,
1960 // AudioFlinger will invalidate the track and the offloaded output
1961 // will be closed causing the effect to be moved to a PCM output.
1962 // 2: A deep buffer output
1963 // 3: the first output in the list
1964
1965 if (outputs.size() == 0) {
1966 return 0;
1967 }
1968
1969 audio_io_handle_t outputOffloaded = 0;
1970 audio_io_handle_t outputDeepBuffer = 0;
1971
1972 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001973 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001974 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001975 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1976 outputOffloaded = outputs[i];
1977 }
1978 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1979 outputDeepBuffer = outputs[i];
1980 }
1981 }
1982
1983 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1984 outputOffloaded, outputDeepBuffer);
1985 if (outputOffloaded != 0) {
1986 return outputOffloaded;
1987 }
1988 if (outputDeepBuffer != 0) {
1989 return outputDeepBuffer;
1990 }
1991
1992 return outputs[0];
1993}
1994
Eric Laurente0720872014-03-11 09:30:41 -07001995audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001996{
1997 // apply simple rule where global effects are attached to the same output as MUSIC streams
1998
Eric Laurent3b73df72014-03-11 09:06:29 -07001999 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002000 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2001 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2002
2003 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2004 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2005 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2006
2007 return output;
2008}
2009
Eric Laurente0720872014-03-11 09:30:41 -07002010status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002011 audio_io_handle_t io,
2012 uint32_t strategy,
2013 int session,
2014 int id)
2015{
2016 ssize_t index = mOutputs.indexOfKey(io);
2017 if (index < 0) {
2018 index = mInputs.indexOfKey(io);
2019 if (index < 0) {
2020 ALOGW("registerEffect() unknown io %d", io);
2021 return INVALID_OPERATION;
2022 }
2023 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002024 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002025}
2026
Eric Laurentc75307b2015-03-17 15:29:32 -07002027bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2028{
Eric Laurent28d09f02016-03-08 10:43:05 -08002029 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002030 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2031 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002032 continue;
2033 }
2034 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2035 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002036 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002037}
2038
2039bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2040{
2041 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2042}
2043
Eric Laurente0720872014-03-11 09:30:41 -07002044bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002045{
2046 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002047 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002048 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002049 return true;
2050 }
2051 }
2052 return false;
2053}
2054
Eric Laurent275e8e92014-11-30 15:14:47 -08002055// Register a list of custom mixes with their attributes and format.
2056// When a mix is registered, corresponding input and output profiles are
2057// added to the remote submix hw module. The profile contains only the
2058// parameters (sampling rate, format...) specified by the mix.
2059// The corresponding input remote submix device is also connected.
2060//
2061// When a remote submix device is connected, the address is checked to select the
2062// appropriate profile and the corresponding input or output stream is opened.
2063//
2064// When capture starts, getInputForAttr() will:
2065// - 1 look for a mix matching the address passed in attribtutes tags if any
2066// - 2 if none found, getDeviceForInputSource() will:
2067// - 2.1 look for a mix matching the attributes source
2068// - 2.2 if none found, default to device selection by policy rules
2069// At this time, the corresponding output remote submix device is also connected
2070// and active playback use cases can be transferred to this mix if needed when reconnecting
2071// after AudioTracks are invalidated
2072//
2073// When playback starts, getOutputForAttr() will:
2074// - 1 look for a mix matching the address passed in attribtutes tags if any
2075// - 2 if none found, look for a mix matching the attributes usage
2076// - 3 if none found, default to device and output selection by policy rules.
2077
2078status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2079{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002080 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2081 status_t res = NO_ERROR;
2082
2083 sp<HwModule> rSubmixModule;
2084 // examine each mix's route type
2085 for (size_t i = 0; i < mixes.size(); i++) {
2086 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2087 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2088 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002089 break;
2090 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002091 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2092 // Loop back through "remote submix"
2093 if (rSubmixModule == 0) {
2094 for (size_t j = 0; i < mHwModules.size(); j++) {
2095 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2096 && mHwModules[j]->mHandle != 0) {
2097 rSubmixModule = mHwModules[j];
2098 break;
2099 }
2100 }
2101 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002102
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002103 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002104
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002105 if (rSubmixModule == 0) {
2106 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2107 res = INVALID_OPERATION;
2108 break;
2109 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002110
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002111 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002112
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002113 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002114 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002115 res = INVALID_OPERATION;
2116 break;
2117 }
2118 audio_config_t outputConfig = mixes[i].mFormat;
2119 audio_config_t inputConfig = mixes[i].mFormat;
2120 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2121 // stereo and let audio flinger do the channel conversion if needed.
2122 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2123 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2124 rSubmixModule->addOutputProfile(address, &outputConfig,
2125 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2126 rSubmixModule->addInputProfile(address, &inputConfig,
2127 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002128
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002129 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2130 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2131 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2132 address.string(), "remote-submix");
2133 } else {
2134 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2135 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2136 address.string(), "remote-submix");
2137 }
2138 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002139 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002140 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002141 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2142 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002143
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002144 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002145 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2146 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2147 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2148 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2149 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2150 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002151 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2152 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002153 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2154 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002155 } else {
2156 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002157 }
2158 break;
2159 }
2160 }
2161
2162 if (res != NO_ERROR) {
2163 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002164 i, device, address.string());
2165 res = INVALID_OPERATION;
2166 break;
2167 } else if (!foundOutput) {
2168 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2169 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002170 res = INVALID_OPERATION;
2171 break;
2172 }
Eric Laurentc722f302014-12-10 11:21:49 -08002173 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002174 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002175 if (res != NO_ERROR) {
2176 unregisterPolicyMixes(mixes);
2177 }
2178 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002179}
2180
2181status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2182{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002183 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002184 status_t res = NO_ERROR;
2185 sp<HwModule> rSubmixModule;
2186 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002187 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002188 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002189
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002190 if (rSubmixModule == 0) {
2191 for (size_t j = 0; i < mHwModules.size(); j++) {
2192 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2193 && mHwModules[j]->mHandle != 0) {
2194 rSubmixModule = mHwModules[j];
2195 break;
2196 }
2197 }
2198 }
2199 if (rSubmixModule == 0) {
2200 res = INVALID_OPERATION;
2201 continue;
2202 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002203
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002204 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002205
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002206 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2207 res = INVALID_OPERATION;
2208 continue;
2209 }
2210
2211 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2212 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2213 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2214 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2215 address.string(), "remote-submix");
2216 }
2217 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2218 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2219 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2220 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2221 address.string(), "remote-submix");
2222 }
2223 rSubmixModule->removeOutputProfile(address);
2224 rSubmixModule->removeInputProfile(address);
2225
2226 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2227 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2228 res = INVALID_OPERATION;
2229 continue;
2230 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002231 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002232 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002233 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002234}
2235
Eric Laurente552edb2014-03-10 17:42:56 -07002236
Eric Laurente0720872014-03-11 09:30:41 -07002237status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002238{
2239 const size_t SIZE = 256;
2240 char buffer[SIZE];
2241 String8 result;
2242
2243 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2244 result.append(buffer);
2245
Eric Laurent87ffa392015-05-22 10:32:38 -07002246 snprintf(buffer, SIZE, " Primary Output: %d\n",
2247 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002248 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002249 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002250 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002251 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002252 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002253 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002254 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002255 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002256 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002257 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002258 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002259 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002260 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002261 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002262 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002263 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002264 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002265 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2266 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2267 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002268 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2269 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002270 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2271 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002272
François Gaffie2110e042015-03-24 08:41:51 +01002273 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002274
François Gaffie112b0af2015-11-19 16:13:25 +01002275 mAvailableOutputDevices.dump(fd, String8("Available output"));
2276 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002277 mHwModules.dump(fd);
2278 mOutputs.dump(fd);
2279 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002280 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002281 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002282 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002283
2284 return NO_ERROR;
2285}
2286
2287// This function checks for the parameters which can be offloaded.
2288// This can be enhanced depending on the capability of the DSP and policy
2289// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002290bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002291{
2292 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002293 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002294 offloadInfo.sample_rate, offloadInfo.channel_mask,
2295 offloadInfo.format,
2296 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2297 offloadInfo.has_video);
2298
Andy Hung2ddee192015-12-18 17:34:44 -08002299 if (mMasterMono) {
2300 return false; // no offloading if mono is set.
2301 }
2302
Eric Laurente552edb2014-03-10 17:42:56 -07002303 // Check if offload has been disabled
2304 char propValue[PROPERTY_VALUE_MAX];
2305 if (property_get("audio.offload.disable", propValue, "0")) {
2306 if (atoi(propValue) != 0) {
2307 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2308 return false;
2309 }
2310 }
2311
2312 // Check if stream type is music, then only allow offload as of now.
2313 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2314 {
2315 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2316 return false;
2317 }
2318
2319 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002320 const bool allowOffloadWithVideo =
2321 property_get_bool("audio.offload.video", false /* default_value */);
2322 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002323 ALOGV("isOffloadSupported: has_video == true, returning false");
2324 return false;
2325 }
2326
2327 //If duration is less than minimum value defined in property, return false
2328 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2329 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2330 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2331 return false;
2332 }
2333 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2334 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2335 return false;
2336 }
2337
2338 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2339 // creating an offloaded track and tearing it down immediately after start when audioflinger
2340 // detects there is an active non offloadable effect.
2341 // FIXME: We should check the audio session here but we do not have it in this context.
2342 // This may prevent offloading in rare situations where effects are left active by apps
2343 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002344 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002345 return false;
2346 }
2347
2348 // See if there is a profile to support this.
2349 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002350 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002351 offloadInfo.sample_rate,
2352 offloadInfo.format,
2353 offloadInfo.channel_mask,
2354 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002355 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2356 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002357}
2358
Eric Laurent6a94d692014-05-20 11:18:06 -07002359status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2360 audio_port_type_t type,
2361 unsigned int *num_ports,
2362 struct audio_port *ports,
2363 unsigned int *generation)
2364{
2365 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2366 generation == NULL) {
2367 return BAD_VALUE;
2368 }
2369 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2370 if (ports == NULL) {
2371 *num_ports = 0;
2372 }
2373
2374 size_t portsWritten = 0;
2375 size_t portsMax = *num_ports;
2376 *num_ports = 0;
2377 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002378 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2379 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002380 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002381 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2382 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2383 continue;
2384 }
2385 if (portsWritten < portsMax) {
2386 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2387 }
2388 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002389 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002390 }
2391 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002392 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2393 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2394 continue;
2395 }
2396 if (portsWritten < portsMax) {
2397 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2398 }
2399 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002400 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002401 }
2402 }
2403 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2404 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2405 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2406 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2407 }
2408 *num_ports += mInputs.size();
2409 }
2410 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002411 size_t numOutputs = 0;
2412 for (size_t i = 0; i < mOutputs.size(); i++) {
2413 if (!mOutputs[i]->isDuplicated()) {
2414 numOutputs++;
2415 if (portsWritten < portsMax) {
2416 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2417 }
2418 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002419 }
Eric Laurent84c70242014-06-23 08:46:27 -07002420 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002421 }
2422 }
2423 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002424 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002425 return NO_ERROR;
2426}
2427
2428status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2429{
2430 return NO_ERROR;
2431}
2432
Eric Laurent6a94d692014-05-20 11:18:06 -07002433status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2434 audio_patch_handle_t *handle,
2435 uid_t uid)
2436{
2437 ALOGV("createAudioPatch()");
2438
2439 if (handle == NULL || patch == NULL) {
2440 return BAD_VALUE;
2441 }
2442 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2443
Eric Laurent874c42872014-08-08 15:13:39 -07002444 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2445 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2446 return BAD_VALUE;
2447 }
2448 // only one source per audio patch supported for now
2449 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002450 return INVALID_OPERATION;
2451 }
Eric Laurent874c42872014-08-08 15:13:39 -07002452
2453 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002454 return INVALID_OPERATION;
2455 }
Eric Laurent874c42872014-08-08 15:13:39 -07002456 for (size_t i = 0; i < patch->num_sinks; i++) {
2457 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2458 return INVALID_OPERATION;
2459 }
2460 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002461
2462 sp<AudioPatch> patchDesc;
2463 ssize_t index = mAudioPatches.indexOfKey(*handle);
2464
Eric Laurent6a94d692014-05-20 11:18:06 -07002465 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2466 patch->sources[0].role,
2467 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002468#if LOG_NDEBUG == 0
2469 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002470 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002471 patch->sinks[i].role,
2472 patch->sinks[i].type);
2473 }
2474#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002475
2476 if (index >= 0) {
2477 patchDesc = mAudioPatches.valueAt(index);
2478 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2479 mUidCached, patchDesc->mUid, uid);
2480 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2481 return INVALID_OPERATION;
2482 }
2483 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002484 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002485 }
2486
2487 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002488 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002489 if (outputDesc == NULL) {
2490 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2491 return BAD_VALUE;
2492 }
Eric Laurent84c70242014-06-23 08:46:27 -07002493 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2494 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002495 if (patchDesc != 0) {
2496 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2497 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2498 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2499 return BAD_VALUE;
2500 }
2501 }
Eric Laurent874c42872014-08-08 15:13:39 -07002502 DeviceVector devices;
2503 for (size_t i = 0; i < patch->num_sinks; i++) {
2504 // Only support mix to devices connection
2505 // TODO add support for mix to mix connection
2506 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2507 ALOGV("createAudioPatch() source mix but sink is not a device");
2508 return INVALID_OPERATION;
2509 }
2510 sp<DeviceDescriptor> devDesc =
2511 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2512 if (devDesc == 0) {
2513 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2514 return BAD_VALUE;
2515 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002516
François Gaffie53615e22015-03-19 09:24:12 +01002517 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002518 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002519 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002520 NULL, // updatedSamplingRate
2521 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002522 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002523 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002524 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002525 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002526 ALOGV("createAudioPatch() profile not supported for device %08x",
2527 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002528 return INVALID_OPERATION;
2529 }
2530 devices.add(devDesc);
2531 }
2532 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002533 return INVALID_OPERATION;
2534 }
Eric Laurent874c42872014-08-08 15:13:39 -07002535
Eric Laurent6a94d692014-05-20 11:18:06 -07002536 // TODO: reconfigure output format and channels here
2537 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002538 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002539 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002540 index = mAudioPatches.indexOfKey(*handle);
2541 if (index >= 0) {
2542 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2543 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2544 }
2545 patchDesc = mAudioPatches.valueAt(index);
2546 patchDesc->mUid = uid;
2547 ALOGV("createAudioPatch() success");
2548 } else {
2549 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2550 return INVALID_OPERATION;
2551 }
2552 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2553 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2554 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002555 // only one sink supported when connecting an input device to a mix
2556 if (patch->num_sinks > 1) {
2557 return INVALID_OPERATION;
2558 }
François Gaffie53615e22015-03-19 09:24:12 +01002559 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002560 if (inputDesc == NULL) {
2561 return BAD_VALUE;
2562 }
2563 if (patchDesc != 0) {
2564 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2565 return BAD_VALUE;
2566 }
2567 }
2568 sp<DeviceDescriptor> devDesc =
2569 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2570 if (devDesc == 0) {
2571 return BAD_VALUE;
2572 }
2573
François Gaffie53615e22015-03-19 09:24:12 +01002574 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002575 devDesc->mAddress,
2576 patch->sinks[0].sample_rate,
2577 NULL, /*updatedSampleRate*/
2578 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002579 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002580 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002581 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002582 // FIXME for the parameter type,
2583 // and the NONE
2584 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002585 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002586 return INVALID_OPERATION;
2587 }
2588 // TODO: reconfigure output format and channels here
2589 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002590 devDesc->type(), inputDesc->mIoHandle);
2591 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002592 index = mAudioPatches.indexOfKey(*handle);
2593 if (index >= 0) {
2594 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2595 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2596 }
2597 patchDesc = mAudioPatches.valueAt(index);
2598 patchDesc->mUid = uid;
2599 ALOGV("createAudioPatch() success");
2600 } else {
2601 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2602 return INVALID_OPERATION;
2603 }
2604 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2605 // device to device connection
2606 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002607 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002608 return BAD_VALUE;
2609 }
2610 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002611 sp<DeviceDescriptor> srcDeviceDesc =
2612 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002613 if (srcDeviceDesc == 0) {
2614 return BAD_VALUE;
2615 }
Eric Laurent874c42872014-08-08 15:13:39 -07002616
Eric Laurent6a94d692014-05-20 11:18:06 -07002617 //update source and sink with our own data as the data passed in the patch may
2618 // be incomplete.
2619 struct audio_patch newPatch = *patch;
2620 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002621
Eric Laurent874c42872014-08-08 15:13:39 -07002622 for (size_t i = 0; i < patch->num_sinks; i++) {
2623 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2624 ALOGV("createAudioPatch() source device but one sink is not a device");
2625 return INVALID_OPERATION;
2626 }
2627
2628 sp<DeviceDescriptor> sinkDeviceDesc =
2629 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2630 if (sinkDeviceDesc == 0) {
2631 return BAD_VALUE;
2632 }
2633 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2634
Eric Laurent3bcf8592015-04-03 12:13:24 -07002635 // create a software bridge in PatchPanel if:
2636 // - source and sink devices are on differnt HW modules OR
2637 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002638 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002639 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002640 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002641 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002642 return INVALID_OPERATION;
2643 }
Eric Laurent874c42872014-08-08 15:13:39 -07002644 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002645 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002646 // if the sink device is reachable via an opened output stream, request to go via
2647 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002648 audio_io_handle_t output = selectOutput(outputs,
2649 AUDIO_OUTPUT_FLAG_NONE,
2650 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002651 if (output != AUDIO_IO_HANDLE_NONE) {
2652 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2653 if (outputDesc->isDuplicated()) {
2654 return INVALID_OPERATION;
2655 }
2656 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002657 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002658 newPatch.num_sources = 2;
2659 }
Eric Laurent83b88082014-06-20 18:31:16 -07002660 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002661 }
2662 // TODO: check from routing capabilities in config file and other conflicting patches
2663
2664 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2665 if (index >= 0) {
2666 afPatchHandle = patchDesc->mAfPatchHandle;
2667 }
2668
2669 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2670 &afPatchHandle,
2671 0);
2672 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2673 status, afPatchHandle);
2674 if (status == NO_ERROR) {
2675 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002676 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002677 addAudioPatch(patchDesc->mHandle, patchDesc);
2678 } else {
2679 patchDesc->mPatch = newPatch;
2680 }
2681 patchDesc->mAfPatchHandle = afPatchHandle;
2682 *handle = patchDesc->mHandle;
2683 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002684 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002685 } else {
2686 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2687 status);
2688 return INVALID_OPERATION;
2689 }
2690 } else {
2691 return BAD_VALUE;
2692 }
2693 } else {
2694 return BAD_VALUE;
2695 }
2696 return NO_ERROR;
2697}
2698
2699status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2700 uid_t uid)
2701{
2702 ALOGV("releaseAudioPatch() patch %d", handle);
2703
2704 ssize_t index = mAudioPatches.indexOfKey(handle);
2705
2706 if (index < 0) {
2707 return BAD_VALUE;
2708 }
2709 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2710 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2711 mUidCached, patchDesc->mUid, uid);
2712 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2713 return INVALID_OPERATION;
2714 }
2715
2716 struct audio_patch *patch = &patchDesc->mPatch;
2717 patchDesc->mUid = mUidCached;
2718 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002719 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002720 if (outputDesc == NULL) {
2721 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2722 return BAD_VALUE;
2723 }
2724
Eric Laurentc75307b2015-03-17 15:29:32 -07002725 setOutputDevice(outputDesc,
2726 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002727 true,
2728 0,
2729 NULL);
2730 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2731 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002732 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002733 if (inputDesc == NULL) {
2734 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2735 return BAD_VALUE;
2736 }
2737 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002738 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002739 true,
2740 NULL);
2741 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002742 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2743 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2744 status, patchDesc->mAfPatchHandle);
2745 removeAudioPatch(patchDesc->mHandle);
2746 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002747 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002748 } else {
2749 return BAD_VALUE;
2750 }
2751 } else {
2752 return BAD_VALUE;
2753 }
2754 return NO_ERROR;
2755}
2756
2757status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2758 struct audio_patch *patches,
2759 unsigned int *generation)
2760{
François Gaffie53615e22015-03-19 09:24:12 +01002761 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002762 return BAD_VALUE;
2763 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002764 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002765 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002766}
2767
Eric Laurente1715a42014-05-20 11:30:42 -07002768status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002769{
Eric Laurente1715a42014-05-20 11:30:42 -07002770 ALOGV("setAudioPortConfig()");
2771
2772 if (config == NULL) {
2773 return BAD_VALUE;
2774 }
2775 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2776 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002777 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2778 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002779 }
2780
Eric Laurenta121f902014-06-03 13:32:54 -07002781 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002782 if (config->type == AUDIO_PORT_TYPE_MIX) {
2783 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002784 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002785 if (outputDesc == NULL) {
2786 return BAD_VALUE;
2787 }
Eric Laurent84c70242014-06-23 08:46:27 -07002788 ALOG_ASSERT(!outputDesc->isDuplicated(),
2789 "setAudioPortConfig() called on duplicated output %d",
2790 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002791 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002792 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002793 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002794 if (inputDesc == NULL) {
2795 return BAD_VALUE;
2796 }
Eric Laurenta121f902014-06-03 13:32:54 -07002797 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002798 } else {
2799 return BAD_VALUE;
2800 }
2801 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2802 sp<DeviceDescriptor> deviceDesc;
2803 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2804 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2805 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2806 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2807 } else {
2808 return BAD_VALUE;
2809 }
2810 if (deviceDesc == NULL) {
2811 return BAD_VALUE;
2812 }
Eric Laurenta121f902014-06-03 13:32:54 -07002813 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002814 } else {
2815 return BAD_VALUE;
2816 }
2817
Eric Laurenta121f902014-06-03 13:32:54 -07002818 struct audio_port_config backupConfig;
2819 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2820 if (status == NO_ERROR) {
2821 struct audio_port_config newConfig;
2822 audioPortConfig->toAudioPortConfig(&newConfig, config);
2823 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002824 }
Eric Laurenta121f902014-06-03 13:32:54 -07002825 if (status != NO_ERROR) {
2826 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002827 }
Eric Laurente1715a42014-05-20 11:30:42 -07002828
2829 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002830}
2831
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002832void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2833{
Eric Laurentd60560a2015-04-10 11:31:20 -07002834 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002835 clearAudioPatches(uid);
2836 clearSessionRoutes(uid);
2837}
2838
Eric Laurent6a94d692014-05-20 11:18:06 -07002839void AudioPolicyManager::clearAudioPatches(uid_t uid)
2840{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002841 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002842 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2843 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002844 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002845 }
2846 }
2847}
2848
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002849void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2850 audio_io_handle_t ouptutToSkip)
2851{
2852 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2853 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2854 for (size_t j = 0; j < mOutputs.size(); j++) {
2855 if (mOutputs.keyAt(j) == ouptutToSkip) {
2856 continue;
2857 }
2858 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2859 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2860 continue;
2861 }
2862 // If the default device for this strategy is on another output mix,
2863 // invalidate all tracks in this strategy to force re connection.
2864 // Otherwise select new device on the output mix.
2865 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002866 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002867 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2868 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2869 }
2870 }
2871 } else {
2872 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2873 setOutputDevice(outputDesc, newDevice, false);
2874 }
2875 }
2876}
2877
2878void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2879{
2880 // remove output routes associated with this uid
2881 SortedVector<routing_strategy> affectedStrategies;
2882 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2883 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2884 if (route->mUid == uid) {
2885 mOutputRoutes.removeItemsAt(i);
2886 if (route->mDeviceDescriptor != 0) {
2887 affectedStrategies.add(getStrategy(route->mStreamType));
2888 }
2889 }
2890 }
2891 // reroute outputs if necessary
2892 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2893 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2894 }
2895
2896 // remove input routes associated with this uid
2897 SortedVector<audio_source_t> affectedSources;
2898 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2899 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2900 if (route->mUid == uid) {
2901 mInputRoutes.removeItemsAt(i);
2902 if (route->mDeviceDescriptor != 0) {
2903 affectedSources.add(route->mSource);
2904 }
2905 }
2906 }
2907 // reroute inputs if necessary
2908 SortedVector<audio_io_handle_t> inputsToClose;
2909 for (size_t i = 0; i < mInputs.size(); i++) {
2910 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002911 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002912 inputsToClose.add(inputDesc->mIoHandle);
2913 }
2914 }
2915 for (size_t i = 0; i < inputsToClose.size(); i++) {
2916 closeInput(inputsToClose[i]);
2917 }
2918}
2919
Eric Laurentd60560a2015-04-10 11:31:20 -07002920void AudioPolicyManager::clearAudioSources(uid_t uid)
2921{
2922 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2923 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2924 if (sourceDesc->mUid == uid) {
2925 stopAudioSource(mAudioSources.keyAt(i));
2926 }
2927 }
2928}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002929
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002930status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2931 audio_io_handle_t *ioHandle,
2932 audio_devices_t *device)
2933{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002934 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2935 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002936 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002937
François Gaffiedf372692015-03-19 10:43:27 +01002938 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002939}
2940
Eric Laurentd60560a2015-04-10 11:31:20 -07002941status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2942 const audio_attributes_t *attributes,
2943 audio_io_handle_t *handle,
2944 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002945{
Eric Laurentd60560a2015-04-10 11:31:20 -07002946 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2947 if (source == NULL || attributes == NULL || handle == NULL) {
2948 return BAD_VALUE;
2949 }
2950
2951 *handle = AUDIO_IO_HANDLE_NONE;
2952
2953 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2954 source->type != AUDIO_PORT_TYPE_DEVICE) {
2955 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2956 return INVALID_OPERATION;
2957 }
2958
2959 sp<DeviceDescriptor> srcDeviceDesc =
2960 mAvailableInputDevices.getDevice(source->ext.device.type,
2961 String8(source->ext.device.address));
2962 if (srcDeviceDesc == 0) {
2963 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2964 return BAD_VALUE;
2965 }
2966 sp<AudioSourceDescriptor> sourceDesc =
2967 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2968
2969 struct audio_patch dummyPatch;
2970 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2971 sourceDesc->mPatchDesc = patchDesc;
2972
2973 status_t status = connectAudioSource(sourceDesc);
2974 if (status == NO_ERROR) {
2975 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2976 *handle = sourceDesc->getHandle();
2977 }
2978 return status;
2979}
2980
2981status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2982{
2983 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2984
2985 // make sure we only have one patch per source.
2986 disconnectAudioSource(sourceDesc);
2987
2988 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002989 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002990 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2991
2992 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2993 sp<DeviceDescriptor> sinkDeviceDesc =
2994 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2995
2996 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2997 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2998
2999 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3000 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003001 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003002 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3003 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3004 // create patch between src device and output device
3005 // create Hwoutput and add to mHwOutputs
3006 } else {
3007 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3008 audio_io_handle_t output =
3009 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3010 if (output == AUDIO_IO_HANDLE_NONE) {
3011 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3012 return INVALID_OPERATION;
3013 }
3014 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3015 if (outputDesc->isDuplicated()) {
3016 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3017 return INVALID_OPERATION;
3018 }
3019 // create a special patch with no sink and two sources:
3020 // - the second source indicates to PatchPanel through which output mix this patch should
3021 // be connected as well as the stream type for volume control
3022 // - the sink is defined by whatever output device is currently selected for the output
3023 // though which this patch is routed.
3024 patch->num_sinks = 0;
3025 patch->num_sources = 2;
3026 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3027 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3028 patch->sources[1].ext.mix.usecase.stream = stream;
3029 status_t status = mpClientInterface->createAudioPatch(patch,
3030 &afPatchHandle,
3031 0);
3032 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3033 status, afPatchHandle);
3034 if (status != NO_ERROR) {
3035 ALOGW("%s patch panel could not connect device patch, error %d",
3036 __FUNCTION__, status);
3037 return INVALID_OPERATION;
3038 }
3039 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003040 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003041
3042 if (status != NO_ERROR) {
3043 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3044 return status;
3045 }
3046 sourceDesc->mSwOutput = outputDesc;
3047 if (delayMs != 0) {
3048 usleep(delayMs * 1000);
3049 }
3050 }
3051
3052 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3053 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3054
3055 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003056}
3057
Eric Laurent493404d2015-04-21 15:07:36 -07003058status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003059{
Eric Laurentd60560a2015-04-10 11:31:20 -07003060 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3061 ALOGV("%s handle %d", __FUNCTION__, handle);
3062 if (sourceDesc == 0) {
3063 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3064 return BAD_VALUE;
3065 }
3066 status_t status = disconnectAudioSource(sourceDesc);
3067
3068 mAudioSources.removeItem(handle);
3069 return status;
3070}
3071
Andy Hung2ddee192015-12-18 17:34:44 -08003072status_t AudioPolicyManager::setMasterMono(bool mono)
3073{
3074 if (mMasterMono == mono) {
3075 return NO_ERROR;
3076 }
3077 mMasterMono = mono;
3078 // if enabling mono we close all offloaded devices, which will invalidate the
3079 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3080 // for recreating the new AudioTrack as non-offloaded PCM.
3081 //
3082 // If disabling mono, we leave all tracks as is: we don't know which clients
3083 // and tracks are able to be recreated as offloaded. The next "song" should
3084 // play back offloaded.
3085 if (mMasterMono) {
3086 Vector<audio_io_handle_t> offloaded;
3087 for (size_t i = 0; i < mOutputs.size(); ++i) {
3088 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3089 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3090 offloaded.push(desc->mIoHandle);
3091 }
3092 }
3093 for (size_t i = 0; i < offloaded.size(); ++i) {
3094 closeOutput(offloaded[i]);
3095 }
3096 }
3097 // update master mono for all remaining outputs
3098 for (size_t i = 0; i < mOutputs.size(); ++i) {
3099 updateMono(mOutputs.keyAt(i));
3100 }
3101 return NO_ERROR;
3102}
3103
3104status_t AudioPolicyManager::getMasterMono(bool *mono)
3105{
3106 *mono = mMasterMono;
3107 return NO_ERROR;
3108}
3109
Eric Laurentd60560a2015-04-10 11:31:20 -07003110status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3111{
3112 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3113
3114 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3115 if (patchDesc == 0) {
3116 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3117 sourceDesc->mPatchDesc->mHandle);
3118 return BAD_VALUE;
3119 }
3120 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3121
Eric Laurent28d09f02016-03-08 10:43:05 -08003122 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003123 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3124 if (swOutputDesc != 0) {
3125 stopSource(swOutputDesc, stream, false);
3126 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3127 } else {
3128 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3129 if (hwOutputDesc != 0) {
3130 // release patch between src device and output device
3131 // close Hwoutput and remove from mHwOutputs
3132 } else {
3133 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3134 }
3135 }
3136 return NO_ERROR;
3137}
3138
3139sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3140 audio_io_handle_t output, routing_strategy strategy)
3141{
3142 sp<AudioSourceDescriptor> source;
3143 for (size_t i = 0; i < mAudioSources.size(); i++) {
3144 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3145 routing_strategy sourceStrategy =
3146 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3147 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3148 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3149 source = sourceDesc;
3150 break;
3151 }
3152 }
3153 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003154}
3155
Eric Laurente552edb2014-03-10 17:42:56 -07003156// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003157// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003158// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003159uint32_t AudioPolicyManager::nextAudioPortGeneration()
3160{
3161 return android_atomic_inc(&mAudioPortGeneration);
3162}
3163
Eric Laurente0720872014-03-11 09:30:41 -07003164AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003165 :
3166#ifdef AUDIO_POLICY_TEST
3167 Thread(false),
3168#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003169 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003170 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003171 mAudioPortGeneration(1),
3172 mBeaconMuteRefCount(0),
3173 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003174 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003175 mTtsOutputAvailable(false),
3176 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003177{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003178 mUidCached = getuid();
3179 mpClientInterface = clientInterface;
3180
3181 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3182 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3183 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3184 bool speakerDrcEnabled = false;
3185
3186#ifdef USE_XML_AUDIO_POLICY_CONF
3187 mVolumeCurves = new VolumeCurvesCollection();
3188 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3189 mDefaultOutputDevice, speakerDrcEnabled,
3190 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3191 PolicySerializer serializer;
3192 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3193#else
3194 mVolumeCurves = new StreamDescriptorCollection();
3195 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3196 mDefaultOutputDevice, speakerDrcEnabled);
3197 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3198 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3199#endif
3200 ALOGE("could not load audio policy configuration file, setting defaults");
3201 config.setDefault();
3202 }
3203 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3204 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3205
3206 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003207 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3208 if (!engineInstance) {
3209 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3210 return;
3211 }
3212 // Retrieve the Policy Manager Interface
3213 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3214 if (mEngine == NULL) {
3215 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3216 return;
3217 }
3218 mEngine->setObserver(this);
3219 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003220 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003221 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3222
Eric Laurent3a4311c2014-03-17 12:00:47 -07003223 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003224 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003225 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3226 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003227 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003228 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003229 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003230 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003231 continue;
3232 }
3233 // open all output streams needed to access attached devices
3234 // except for direct output streams that are only opened when they are actually
3235 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003236 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003237 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3238 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003239 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003240
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003241 if (!outProfile->hasSupportedDevices()) {
3242 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003243 continue;
3244 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003245 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003246 mTtsOutputAvailable = true;
3247 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003248
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003249 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003250 continue;
3251 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003252 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003253 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3254 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003255 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003256 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003257 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003259 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003260 if ((profileType & outputDeviceTypes) == 0) {
3261 continue;
3262 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003263 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3264 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003265 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3266 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3267 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3268 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003269
3270 outputDesc->mDevice = profileType;
3271 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3272 config.sample_rate = outputDesc->mSamplingRate;
3273 config.channel_mask = outputDesc->mChannelMask;
3274 config.format = outputDesc->mFormat;
3275 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003276 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003277 &output,
3278 &config,
3279 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003280 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003281 &outputDesc->mLatency,
3282 outputDesc->mFlags);
3283
3284 if (status != NO_ERROR) {
3285 ALOGW("Cannot open output stream for device %08x on hw module %s",
3286 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003287 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003288 } else {
3289 outputDesc->mSamplingRate = config.sample_rate;
3290 outputDesc->mChannelMask = config.channel_mask;
3291 outputDesc->mFormat = config.format;
3292
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003293 for (size_t k = 0; k < supportedDevices.size(); k++) {
3294 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003295 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003296 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3297 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003298 }
3299 }
3300 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003301 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003302 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003303 }
3304 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003305 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003306 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003307 true,
3308 0,
3309 NULL,
3310 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003311 }
Eric Laurente552edb2014-03-10 17:42:56 -07003312 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003313 // open input streams needed to access attached devices to validate
3314 // mAvailableInputDevices list
3315 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3316 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003317 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003318
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003319 if (!inProfile->hasSupportedDevices()) {
3320 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003321 continue;
3322 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003323 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003324 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003325 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3326
Eric Laurentd78f1532014-09-16 16:38:20 -07003327 if ((profileType & inputDeviceTypes) == 0) {
3328 continue;
3329 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003330 sp<AudioInputDescriptor> inputDesc =
3331 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003332
Eric Laurentd78f1532014-09-16 16:38:20 -07003333 inputDesc->mDevice = profileType;
3334
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003335 // find the address
3336 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3337 // the inputs vector must be of size 1, but we don't want to crash here
3338 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3339 : String8("");
3340 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3341 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3342
Eric Laurentd78f1532014-09-16 16:38:20 -07003343 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3344 config.sample_rate = inputDesc->mSamplingRate;
3345 config.channel_mask = inputDesc->mChannelMask;
3346 config.format = inputDesc->mFormat;
3347 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003348 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003349 &input,
3350 &config,
3351 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003352 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003353 AUDIO_SOURCE_MIC,
3354 AUDIO_INPUT_FLAG_NONE);
3355
3356 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003357 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3358 for (size_t k = 0; k < supportedDevices.size(); k++) {
3359 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003360 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003361 if (index >= 0) {
3362 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3363 if (!devDesc->isAttached()) {
3364 devDesc->attach(mHwModules[i]);
3365 devDesc->importAudioPort(inProfile);
3366 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003367 }
3368 }
3369 mpClientInterface->closeInput(input);
3370 } else {
3371 ALOGW("Cannot open input stream for device %08x on hw module %s",
3372 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003373 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003374 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003375 }
3376 }
3377 // make sure all attached devices have been allocated a unique ID
3378 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003379 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003380 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003381 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3382 continue;
3383 }
François Gaffie2110e042015-03-24 08:41:51 +01003384 // The device is now validated and can be appended to the available devices of the engine
3385 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3386 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003387 i++;
3388 }
3389 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003390 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003391 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003392 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3393 continue;
3394 }
François Gaffie2110e042015-03-24 08:41:51 +01003395 // The device is now validated and can be appended to the available devices of the engine
3396 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3397 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003398 i++;
3399 }
3400 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003401 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003402 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003403 }
Eric Laurente552edb2014-03-10 17:42:56 -07003404
3405 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3406
3407 updateDevicesAndOutputs();
3408
3409#ifdef AUDIO_POLICY_TEST
3410 if (mPrimaryOutput != 0) {
3411 AudioParameter outputCmd = AudioParameter();
3412 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003413 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003414
3415 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3416 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003417 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3418 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003419 mTestLatencyMs = 0;
3420 mCurOutput = 0;
3421 mDirectOutput = false;
3422 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3423 mTestOutputs[i] = 0;
3424 }
3425
3426 const size_t SIZE = 256;
3427 char buffer[SIZE];
3428 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3429 run(buffer, ANDROID_PRIORITY_AUDIO);
3430 }
3431#endif //AUDIO_POLICY_TEST
3432}
3433
Eric Laurente0720872014-03-11 09:30:41 -07003434AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003435{
3436#ifdef AUDIO_POLICY_TEST
3437 exit();
3438#endif //AUDIO_POLICY_TEST
3439 for (size_t i = 0; i < mOutputs.size(); i++) {
3440 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003441 }
3442 for (size_t i = 0; i < mInputs.size(); i++) {
3443 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003444 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003445 mAvailableOutputDevices.clear();
3446 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003447 mOutputs.clear();
3448 mInputs.clear();
3449 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003450}
3451
Eric Laurente0720872014-03-11 09:30:41 -07003452status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003453{
Eric Laurent87ffa392015-05-22 10:32:38 -07003454 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003455}
3456
3457#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003458bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003459{
3460 ALOGV("entering threadLoop()");
3461 while (!exitPending())
3462 {
3463 String8 command;
3464 int valueInt;
3465 String8 value;
3466
3467 Mutex::Autolock _l(mLock);
3468 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3469
3470 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3471 AudioParameter param = AudioParameter(command);
3472
3473 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3474 valueInt != 0) {
3475 ALOGV("Test command %s received", command.string());
3476 String8 target;
3477 if (param.get(String8("target"), target) != NO_ERROR) {
3478 target = "Manager";
3479 }
3480 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3481 param.remove(String8("test_cmd_policy_output"));
3482 mCurOutput = valueInt;
3483 }
3484 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3485 param.remove(String8("test_cmd_policy_direct"));
3486 if (value == "false") {
3487 mDirectOutput = false;
3488 } else if (value == "true") {
3489 mDirectOutput = true;
3490 }
3491 }
3492 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3493 param.remove(String8("test_cmd_policy_input"));
3494 mTestInput = valueInt;
3495 }
3496
3497 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3498 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003499 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003500 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003501 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003502 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003503 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003504 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003505 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003506 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003507 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003508 if (target == "Manager") {
3509 mTestFormat = format;
3510 } else if (mTestOutputs[mCurOutput] != 0) {
3511 AudioParameter outputParam = AudioParameter();
3512 outputParam.addInt(String8("format"), format);
3513 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3514 }
3515 }
3516 }
3517 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3518 param.remove(String8("test_cmd_policy_channels"));
3519 int channels = 0;
3520
3521 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003522 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003523 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003524 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003525 }
3526 if (channels != 0) {
3527 if (target == "Manager") {
3528 mTestChannels = channels;
3529 } else if (mTestOutputs[mCurOutput] != 0) {
3530 AudioParameter outputParam = AudioParameter();
3531 outputParam.addInt(String8("channels"), channels);
3532 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3533 }
3534 }
3535 }
3536 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3537 param.remove(String8("test_cmd_policy_sampleRate"));
3538 if (valueInt >= 0 && valueInt <= 96000) {
3539 int samplingRate = valueInt;
3540 if (target == "Manager") {
3541 mTestSamplingRate = samplingRate;
3542 } else if (mTestOutputs[mCurOutput] != 0) {
3543 AudioParameter outputParam = AudioParameter();
3544 outputParam.addInt(String8("sampling_rate"), samplingRate);
3545 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3546 }
3547 }
3548 }
3549
3550 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3551 param.remove(String8("test_cmd_policy_reopen"));
3552
Eric Laurentc75307b2015-03-17 15:29:32 -07003553 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003554
Eric Laurentc75307b2015-03-17 15:29:32 -07003555 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003556
Eric Laurentc75307b2015-03-17 15:29:32 -07003557 removeOutput(mPrimaryOutput->mIoHandle);
3558 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3559 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003560 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003561 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3562 config.sample_rate = outputDesc->mSamplingRate;
3563 config.channel_mask = outputDesc->mChannelMask;
3564 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003565 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003566 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003567 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003568 &config,
3569 &outputDesc->mDevice,
3570 String8(""),
3571 &outputDesc->mLatency,
3572 outputDesc->mFlags);
3573 if (status != NO_ERROR) {
3574 ALOGE("Failed to reopen hardware output stream, "
3575 "samplingRate: %d, format %d, channels %d",
3576 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003577 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003578 outputDesc->mSamplingRate = config.sample_rate;
3579 outputDesc->mChannelMask = config.channel_mask;
3580 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003581 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003582 AudioParameter outputCmd = AudioParameter();
3583 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003584 mpClientInterface->setParameters(handle, outputCmd.toString());
3585 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003586 }
3587 }
3588
3589
3590 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3591 }
3592 }
3593 return false;
3594}
3595
Eric Laurente0720872014-03-11 09:30:41 -07003596void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003597{
3598 {
3599 AutoMutex _l(mLock);
3600 requestExit();
3601 mWaitWorkCV.signal();
3602 }
3603 requestExitAndWait();
3604}
3605
Eric Laurente0720872014-03-11 09:30:41 -07003606int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003607{
3608 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3609 if (output == mTestOutputs[i]) return i;
3610 }
3611 return 0;
3612}
3613#endif //AUDIO_POLICY_TEST
3614
3615// ---
3616
Eric Laurentc75307b2015-03-17 15:29:32 -07003617void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003618{
François Gaffie98cc1912015-03-18 17:52:40 +01003619 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003620 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003621 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003622 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003623}
3624
François Gaffie53615e22015-03-19 09:24:12 +01003625void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3626{
3627 mOutputs.removeItem(output);
3628}
3629
Eric Laurent1f2f2232014-06-02 12:01:23 -07003630void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003631{
François Gaffie98cc1912015-03-18 17:52:40 +01003632 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003633 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003634 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003635}
Eric Laurente552edb2014-03-10 17:42:56 -07003636
Eric Laurentc75307b2015-03-17 15:29:32 -07003637void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003638 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003639 const String8 address /*in*/,
3640 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003641 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003642 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003643 if (devDesc != 0) {
3644 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3645 desc->mIoHandle, address.string());
3646 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003647 }
3648}
3649
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003650status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003651 audio_policy_dev_state_t state,
3652 SortedVector<audio_io_handle_t>& outputs,
3653 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003654{
François Gaffie53615e22015-03-19 09:24:12 +01003655 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003656 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003657
3658 if (audio_device_is_digital(device)) {
3659 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003660 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003661 }
Eric Laurente552edb2014-03-10 17:42:56 -07003662
Eric Laurent3b73df72014-03-11 09:06:29 -07003663 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003664 // first list already open outputs that can be routed to this device
3665 for (size_t i = 0; i < mOutputs.size(); i++) {
3666 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003667 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003668 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003669 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3670 outputs.add(mOutputs.keyAt(i));
3671 } else {
3672 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003673 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003674 }
Eric Laurente552edb2014-03-10 17:42:56 -07003675 }
3676 }
3677 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003678 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003679 for (size_t i = 0; i < mHwModules.size(); i++)
3680 {
3681 if (mHwModules[i]->mHandle == 0) {
3682 continue;
3683 }
3684 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3685 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003686 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003687 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003688 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003689 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003690 profiles.add(profile);
3691 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3692 }
Eric Laurente552edb2014-03-10 17:42:56 -07003693 }
3694 }
3695 }
3696
Eric Laurent7b279bb2015-12-14 10:18:23 -08003697 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003698
Eric Laurente552edb2014-03-10 17:42:56 -07003699 if (profiles.isEmpty() && outputs.isEmpty()) {
3700 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3701 return BAD_VALUE;
3702 }
3703
3704 // open outputs for matching profiles if needed. Direct outputs are also opened to
3705 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3706 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003707 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003708
3709 // nothing to do if one output is already opened for this profile
3710 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003711 for (j = 0; j < outputs.size(); j++) {
3712 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003713 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003714 // matching profile: save the sample rates, format and channel masks supported
3715 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003716 if (audio_device_is_digital(device)) {
3717 devDesc->importAudioPort(profile);
3718 }
Eric Laurente552edb2014-03-10 17:42:56 -07003719 break;
3720 }
3721 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003722 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003723 continue;
3724 }
3725
Eric Laurent83b88082014-06-20 18:31:16 -07003726 ALOGV("opening output for device %08x with params %s profile %p",
3727 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003728 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003729 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003730 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3731 config.sample_rate = desc->mSamplingRate;
3732 config.channel_mask = desc->mChannelMask;
3733 config.format = desc->mFormat;
3734 config.offload_info.sample_rate = desc->mSamplingRate;
3735 config.offload_info.channel_mask = desc->mChannelMask;
3736 config.offload_info.format = desc->mFormat;
3737 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003738 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003739 &output,
3740 &config,
3741 &desc->mDevice,
3742 address,
3743 &desc->mLatency,
3744 desc->mFlags);
3745 if (status == NO_ERROR) {
3746 desc->mSamplingRate = config.sample_rate;
3747 desc->mChannelMask = config.channel_mask;
3748 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003749
Eric Laurentd4692962014-05-05 18:13:44 -07003750 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003751 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003752 char *param = audio_device_address_to_parameter(device, address);
3753 mpClientInterface->setParameters(output, String8(param));
3754 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003755 }
Phil Burk00eeb322016-03-31 12:41:00 -07003756 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003757 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003758 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003759 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003760 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003761 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003762 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003763 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003764 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003765 config.offload_info.sample_rate = config.sample_rate;
3766 config.offload_info.channel_mask = config.channel_mask;
3767 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003768 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003769 &output,
3770 &config,
3771 &desc->mDevice,
3772 address,
3773 &desc->mLatency,
3774 desc->mFlags);
3775 if (status == NO_ERROR) {
3776 desc->mSamplingRate = config.sample_rate;
3777 desc->mChannelMask = config.channel_mask;
3778 desc->mFormat = config.format;
3779 } else {
3780 output = AUDIO_IO_HANDLE_NONE;
3781 }
Eric Laurentd4692962014-05-05 18:13:44 -07003782 }
3783
Eric Laurentcf2c0212014-07-25 16:20:43 -07003784 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003785 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003786 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003787 sp<AudioPolicyMix> policyMix;
3788 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003789 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3790 address.string());
3791 }
François Gaffie036e1e92015-03-19 10:16:24 +01003792 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003793 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003794
Eric Laurent87ffa392015-05-22 10:32:38 -07003795 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3796 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003797 // no duplicated output for direct outputs and
3798 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003799 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003800
Eric Laurentd4692962014-05-05 18:13:44 -07003801 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003802 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003803
Eric Laurentd4692962014-05-05 18:13:44 -07003804 //TODO: configure audio effect output stage here
3805
3806 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003807 duplicatedOutput =
3808 mpClientInterface->openDuplicateOutput(output,
3809 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003810 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003811 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003812 sp<SwAudioOutputDescriptor> dupOutputDesc =
3813 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3814 dupOutputDesc->mOutput1 = mPrimaryOutput;
3815 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003816 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3817 dupOutputDesc->mFormat = desc->mFormat;
3818 dupOutputDesc->mChannelMask = desc->mChannelMask;
3819 dupOutputDesc->mLatency = desc->mLatency;
3820 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003821 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003822 } else {
3823 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003824 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003825 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003826 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003827 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003828 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003829 }
Eric Laurente552edb2014-03-10 17:42:56 -07003830 }
3831 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003832 } else {
3833 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003834 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003835 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003836 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003837 profiles.removeAt(profile_index);
3838 profile_index--;
3839 } else {
3840 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003841 // Load digital format info only for digital devices
3842 if (audio_device_is_digital(device)) {
3843 devDesc->importAudioPort(profile);
3844 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003845
François Gaffie53615e22015-03-19 09:24:12 +01003846 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003847 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3848 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003849 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003850 NULL/*patch handle*/, address.string());
3851 }
Eric Laurente552edb2014-03-10 17:42:56 -07003852 ALOGV("checkOutputsForDevice(): adding output %d", output);
3853 }
3854 }
3855
3856 if (profiles.isEmpty()) {
3857 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3858 return BAD_VALUE;
3859 }
Eric Laurentd4692962014-05-05 18:13:44 -07003860 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003861 // check if one opened output is not needed any more after disconnecting one device
3862 for (size_t i = 0; i < mOutputs.size(); i++) {
3863 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003864 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003865 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003866 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003867 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003868 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003869 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003870 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3871 mOutputs.keyAt(i));
3872 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003873 }
Eric Laurente552edb2014-03-10 17:42:56 -07003874 }
3875 }
Eric Laurentd4692962014-05-05 18:13:44 -07003876 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003877 for (size_t i = 0; i < mHwModules.size(); i++)
3878 {
3879 if (mHwModules[i]->mHandle == 0) {
3880 continue;
3881 }
3882 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3883 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003884 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003885 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003886 ALOGV("checkOutputsForDevice(): "
3887 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003888 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003889 }
3890 }
3891 }
3892 }
3893 return NO_ERROR;
3894}
3895
Paul McLean9080a4c2015-06-18 08:24:02 -07003896status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003897 audio_policy_dev_state_t state,
3898 SortedVector<audio_io_handle_t>& inputs,
3899 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003900{
Paul McLean9080a4c2015-06-18 08:24:02 -07003901 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003902 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003903
3904 if (audio_device_is_digital(device)) {
3905 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003906 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003907 }
3908
Eric Laurentd4692962014-05-05 18:13:44 -07003909 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3910 // first list already open inputs that can be routed to this device
3911 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3912 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003913 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003914 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3915 inputs.add(mInputs.keyAt(input_index));
3916 }
3917 }
3918
3919 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003920 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003921 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3922 {
3923 if (mHwModules[module_idx]->mHandle == 0) {
3924 continue;
3925 }
3926 for (size_t profile_index = 0;
3927 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3928 profile_index++)
3929 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003930 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3931
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003932 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003933 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003934 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003935 profiles.add(profile);
3936 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3937 profile_index, module_idx);
3938 }
Eric Laurentd4692962014-05-05 18:13:44 -07003939 }
3940 }
3941 }
3942
3943 if (profiles.isEmpty() && inputs.isEmpty()) {
3944 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3945 return BAD_VALUE;
3946 }
3947
3948 // open inputs for matching profiles if needed. Direct inputs are also opened to
3949 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3950 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3951
Eric Laurent1c333e22014-05-20 10:48:17 -07003952 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003953 // nothing to do if one input is already opened for this profile
3954 size_t input_index;
3955 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3956 desc = mInputs.valueAt(input_index);
3957 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003958 if (audio_device_is_digital(device)) {
3959 devDesc->importAudioPort(profile);
3960 }
Eric Laurentd4692962014-05-05 18:13:44 -07003961 break;
3962 }
3963 }
3964 if (input_index != mInputs.size()) {
3965 continue;
3966 }
3967
3968 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3969 desc = new AudioInputDescriptor(profile);
3970 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003971 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3972 config.sample_rate = desc->mSamplingRate;
3973 config.channel_mask = desc->mChannelMask;
3974 config.format = desc->mFormat;
3975 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003976 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003977 &input,
3978 &config,
3979 &desc->mDevice,
3980 address,
3981 AUDIO_SOURCE_MIC,
3982 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003983
Eric Laurentcf2c0212014-07-25 16:20:43 -07003984 if (status == NO_ERROR) {
3985 desc->mSamplingRate = config.sample_rate;
3986 desc->mChannelMask = config.channel_mask;
3987 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003988
Eric Laurentd4692962014-05-05 18:13:44 -07003989 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003990 char *param = audio_device_address_to_parameter(device, address);
3991 mpClientInterface->setParameters(input, String8(param));
3992 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003993 }
Phil Burk00eeb322016-03-31 12:41:00 -07003994 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003995 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003996 ALOGW("checkInputsForDevice() direct input missing param");
3997 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003998 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003999 }
4000
4001 if (input != 0) {
4002 addInput(input, desc);
4003 }
4004 } // endif input != 0
4005
Eric Laurentcf2c0212014-07-25 16:20:43 -07004006 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004007 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004008 profiles.removeAt(profile_index);
4009 profile_index--;
4010 } else {
4011 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004012 if (audio_device_is_digital(device)) {
4013 devDesc->importAudioPort(profile);
4014 }
Eric Laurentd4692962014-05-05 18:13:44 -07004015 ALOGV("checkInputsForDevice(): adding input %d", input);
4016 }
4017 } // end scan profiles
4018
4019 if (profiles.isEmpty()) {
4020 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4021 return BAD_VALUE;
4022 }
4023 } else {
4024 // Disconnect
4025 // check if one opened input is not needed any more after disconnecting one device
4026 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4027 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004028 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004029 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4030 mInputs.keyAt(input_index));
4031 inputs.add(mInputs.keyAt(input_index));
4032 }
4033 }
4034 // Clear any profiles associated with the disconnected device.
4035 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4036 if (mHwModules[module_index]->mHandle == 0) {
4037 continue;
4038 }
4039 for (size_t profile_index = 0;
4040 profile_index < mHwModules[module_index]->mInputProfiles.size();
4041 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004042 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004043 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004044 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004045 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004046 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004047 }
4048 }
4049 }
4050 } // end disconnect
4051
4052 return NO_ERROR;
4053}
4054
4055
Eric Laurente0720872014-03-11 09:30:41 -07004056void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004057{
4058 ALOGV("closeOutput(%d)", output);
4059
Eric Laurentc75307b2015-03-17 15:29:32 -07004060 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004061 if (outputDesc == NULL) {
4062 ALOGW("closeOutput() unknown output %d", output);
4063 return;
4064 }
François Gaffie036e1e92015-03-19 10:16:24 +01004065 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004066
Eric Laurente552edb2014-03-10 17:42:56 -07004067 // look for duplicated outputs connected to the output being removed.
4068 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004069 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004070 if (dupOutputDesc->isDuplicated() &&
4071 (dupOutputDesc->mOutput1 == outputDesc ||
4072 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004073 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004074 if (dupOutputDesc->mOutput1 == outputDesc) {
4075 outputDesc2 = dupOutputDesc->mOutput2;
4076 } else {
4077 outputDesc2 = dupOutputDesc->mOutput1;
4078 }
4079 // As all active tracks on duplicated output will be deleted,
4080 // and as they were also referenced on the other output, the reference
4081 // count for their stream type must be adjusted accordingly on
4082 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004083 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004084 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004085 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004086 }
4087 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4088 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4089
4090 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004091 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004092 }
4093 }
4094
Eric Laurent05b90f82014-08-27 15:32:29 -07004095 nextAudioPortGeneration();
4096
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004097 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004098 if (index >= 0) {
4099 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004100 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004101 mAudioPatches.removeItemsAt(index);
4102 mpClientInterface->onAudioPatchListUpdate();
4103 }
4104
Eric Laurente552edb2014-03-10 17:42:56 -07004105 AudioParameter param;
4106 param.add(String8("closing"), String8("true"));
4107 mpClientInterface->setParameters(output, param.toString());
4108
4109 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004110 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004111 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004112}
4113
4114void AudioPolicyManager::closeInput(audio_io_handle_t input)
4115{
4116 ALOGV("closeInput(%d)", input);
4117
4118 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4119 if (inputDesc == NULL) {
4120 ALOGW("closeInput() unknown input %d", input);
4121 return;
4122 }
4123
Eric Laurent6a94d692014-05-20 11:18:06 -07004124 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004125
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004126 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004127 if (index >= 0) {
4128 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004129 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004130 mAudioPatches.removeItemsAt(index);
4131 mpClientInterface->onAudioPatchListUpdate();
4132 }
4133
4134 mpClientInterface->closeInput(input);
4135 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004136}
4137
Eric Laurentc75307b2015-03-17 15:29:32 -07004138SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4139 audio_devices_t device,
4140 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004141{
4142 SortedVector<audio_io_handle_t> outputs;
4143
4144 ALOGVV("getOutputsForDevice() device %04x", device);
4145 for (size_t i = 0; i < openOutputs.size(); i++) {
4146 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004147 i, openOutputs.valueAt(i)->isDuplicated(),
4148 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004149 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4150 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4151 outputs.add(openOutputs.keyAt(i));
4152 }
4153 }
4154 return outputs;
4155}
4156
Eric Laurente0720872014-03-11 09:30:41 -07004157bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004158 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004159{
4160 if (outputs1.size() != outputs2.size()) {
4161 return false;
4162 }
4163 for (size_t i = 0; i < outputs1.size(); i++) {
4164 if (outputs1[i] != outputs2[i]) {
4165 return false;
4166 }
4167 }
4168 return true;
4169}
4170
Eric Laurente0720872014-03-11 09:30:41 -07004171void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004172{
4173 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4174 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4175 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4176 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4177
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004178 // also take into account external policy-related changes: add all outputs which are
4179 // associated with policies in the "before" and "after" output vectors
4180 ALOGVV("checkOutputForStrategy(): policy related outputs");
4181 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004182 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004183 if (desc != 0 && desc->mPolicyMix != NULL) {
4184 srcOutputs.add(desc->mIoHandle);
4185 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4186 }
4187 }
4188 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004189 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004190 if (desc != 0 && desc->mPolicyMix != NULL) {
4191 dstOutputs.add(desc->mIoHandle);
4192 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4193 }
4194 }
4195
Eric Laurente552edb2014-03-10 17:42:56 -07004196 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4197 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4198 strategy, srcOutputs[0], dstOutputs[0]);
4199 // mute strategy while moving tracks from one output to another
4200 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004201 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004202 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004203 setStrategyMute(strategy, true, desc);
4204 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004205 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004206 sp<AudioSourceDescriptor> source =
4207 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4208 if (source != 0){
4209 connectAudioSource(source);
4210 }
Eric Laurente552edb2014-03-10 17:42:56 -07004211 }
4212
4213 // Move effects associated to this strategy from previous output to new output
4214 if (strategy == STRATEGY_MEDIA) {
4215 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4216 SortedVector<audio_io_handle_t> moved;
4217 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004218 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4219 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4220 effectDesc->mIo != fxOutput) {
4221 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004222 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4223 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004224 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004225 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004226 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004227 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004228 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004229 }
4230 }
4231 }
4232 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004233 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004234 if (getStrategy((audio_stream_type_t)i) == strategy) {
4235 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004236 }
4237 }
4238 }
4239}
4240
Eric Laurente0720872014-03-11 09:30:41 -07004241void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004242{
François Gaffie2110e042015-03-24 08:41:51 +01004243 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004244 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004245 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004246 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004247 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004248 checkOutputForStrategy(STRATEGY_SONIFICATION);
4249 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004250 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004251 checkOutputForStrategy(STRATEGY_MEDIA);
4252 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004253 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004254}
4255
Eric Laurente0720872014-03-11 09:30:41 -07004256void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004257{
François Gaffie53615e22015-03-19 09:24:12 +01004258 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004259 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004260 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004261 return;
4262 }
4263
Eric Laurent3a4311c2014-03-17 12:00:47 -07004264 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004265 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4266 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4267 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004268 // suspend A2DP output if:
4269 // (NOT already suspended) &&
4270 // ((SCO device is connected &&
4271 // (forced usage for communication || for record is SCO))) ||
4272 // (phone state is ringing || in call)
4273 //
4274 // restore A2DP output if:
4275 // (Already suspended) &&
4276 // ((SCO device is NOT connected ||
4277 // (forced usage NOT for communication && NOT for record is SCO))) &&
4278 // (phone state is NOT ringing && NOT in call)
4279 //
4280 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004281 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004282 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4283 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4284 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4285 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004286
4287 mpClientInterface->restoreOutput(a2dpOutput);
4288 mA2dpSuspended = false;
4289 }
4290 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004291 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004292 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4293 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4294 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4295 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004296
4297 mpClientInterface->suspendOutput(a2dpOutput);
4298 mA2dpSuspended = true;
4299 }
4300 }
4301}
4302
Eric Laurentc75307b2015-03-17 15:29:32 -07004303audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4304 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004305{
4306 audio_devices_t device = AUDIO_DEVICE_NONE;
4307
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004308 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004309 if (index >= 0) {
4310 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4311 if (patchDesc->mUid != mUidCached) {
4312 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004313 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004314 return outputDesc->device();
4315 }
4316 }
4317
Eric Laurente552edb2014-03-10 17:42:56 -07004318 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004319 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004320 // use device for strategy enforced audible
4321 // 2: we are in call or the strategy phone is active on the output:
4322 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004323 // 3: the strategy for enforced audible is active but not enforced on the output:
4324 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004325 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004326 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004327 // 5: the strategy accessibility is active on the output:
4328 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004329 // 6: the strategy "respectful" sonification is active on the output:
4330 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004331 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004332 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004333 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004334 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004335 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004336 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004337 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004338 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004339 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4340 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004341 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004342 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004343 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004344 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004345 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004346 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004347 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4348 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004349 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004350 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004351 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004352 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004353 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004354 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004355 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004356 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004357 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004358 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004359 }
4360
Eric Laurent1c333e22014-05-20 10:48:17 -07004361 ALOGV("getNewOutputDevice() selected device %x", device);
4362 return device;
4363}
4364
Eric Laurent232f26f2016-02-17 18:06:27 -08004365audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004366{
Eric Laurent232f26f2016-02-17 18:06:27 -08004367 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004368
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004369 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004370 if (index >= 0) {
4371 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4372 if (patchDesc->mUid != mUidCached) {
4373 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004374 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004375 return inputDesc->mDevice;
4376 }
4377 }
4378
Eric Laurent232f26f2016-02-17 18:06:27 -08004379 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004380
Eric Laurente552edb2014-03-10 17:42:56 -07004381 return device;
4382}
4383
Eric Laurent794fde22016-03-11 09:50:45 -08004384bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4385 audio_stream_type_t stream2) {
4386 return ((stream1 == stream2) ||
4387 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4388 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004389}
4390
Eric Laurente0720872014-03-11 09:30:41 -07004391uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004392 return (uint32_t)getStrategy(stream);
4393}
4394
Eric Laurente0720872014-03-11 09:30:41 -07004395audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004396 // By checking the range of stream before calling getStrategy, we avoid
4397 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4398 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004399 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004400 return AUDIO_DEVICE_NONE;
4401 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004402 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004403 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4404 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004405 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004406 }
Eric Laurent794fde22016-03-11 09:50:45 -08004407 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004408 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004409 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004410 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4411 for (size_t i = 0; i < outputs.size(); i++) {
4412 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004413 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004414 curDevices |= outputDesc->device();
4415 }
4416 }
4417 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004418 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004419
4420 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4421 and doesn't really need to.*/
4422 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4423 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4424 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4425 }
Eric Laurente552edb2014-03-10 17:42:56 -07004426 return devices;
4427}
4428
François Gaffie2110e042015-03-24 08:41:51 +01004429routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4430{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004431 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004432 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004433}
4434
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004435uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4436 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004437 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4438 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4439 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004440 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4441 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4442 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004443 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004444 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004445}
4446
Eric Laurente0720872014-03-11 09:30:41 -07004447void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004448 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004449 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004450 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4451 updateDevicesAndOutputs();
4452 break;
4453 default:
4454 break;
4455 }
4456}
4457
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004458uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004459
4460 // skip beacon mute management if a dedicated TTS output is available
4461 if (mTtsOutputAvailable) {
4462 return 0;
4463 }
4464
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004465 switch(event) {
4466 case STARTING_OUTPUT:
4467 mBeaconMuteRefCount++;
4468 break;
4469 case STOPPING_OUTPUT:
4470 if (mBeaconMuteRefCount > 0) {
4471 mBeaconMuteRefCount--;
4472 }
4473 break;
4474 case STARTING_BEACON:
4475 mBeaconPlayingRefCount++;
4476 break;
4477 case STOPPING_BEACON:
4478 if (mBeaconPlayingRefCount > 0) {
4479 mBeaconPlayingRefCount--;
4480 }
4481 break;
4482 }
4483
4484 if (mBeaconMuteRefCount > 0) {
4485 // any playback causes beacon to be muted
4486 return setBeaconMute(true);
4487 } else {
4488 // no other playback: unmute when beacon starts playing, mute when it stops
4489 return setBeaconMute(mBeaconPlayingRefCount == 0);
4490 }
4491}
4492
4493uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4494 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4495 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4496 // keep track of muted state to avoid repeating mute/unmute operations
4497 if (mBeaconMuted != mute) {
4498 // mute/unmute AUDIO_STREAM_TTS on all outputs
4499 ALOGV("\t muting %d", mute);
4500 uint32_t maxLatency = 0;
4501 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004502 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004503 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004504 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004505 0 /*delay*/, AUDIO_DEVICE_NONE);
4506 const uint32_t latency = desc->latency() * 2;
4507 if (latency > maxLatency) {
4508 maxLatency = latency;
4509 }
4510 }
4511 mBeaconMuted = mute;
4512 return maxLatency;
4513 }
4514 return 0;
4515}
4516
Eric Laurente0720872014-03-11 09:30:41 -07004517audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004518 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004519{
Paul McLeanaa981192015-03-21 09:55:15 -07004520 // Routing
4521 // see if we have an explicit route
4522 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4523 // (getStrategy(stream)).
4524 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4525 // and activity count is non-zero
4526 // the device = the device from the descriptor in the RouteMap, and exit.
4527 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4528 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004529 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4530 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004531 return route->mDeviceDescriptor->type();
4532 }
4533 }
4534
Eric Laurente552edb2014-03-10 17:42:56 -07004535 if (fromCache) {
4536 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4537 strategy, mDeviceForStrategy[strategy]);
4538 return mDeviceForStrategy[strategy];
4539 }
François Gaffie2110e042015-03-24 08:41:51 +01004540 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004541}
4542
Eric Laurente0720872014-03-11 09:30:41 -07004543void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004544{
4545 for (int i = 0; i < NUM_STRATEGIES; i++) {
4546 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4547 }
4548 mPreviousOutputs = mOutputs;
4549}
4550
Eric Laurent1f2f2232014-06-02 12:01:23 -07004551uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004552 audio_devices_t prevDevice,
4553 uint32_t delayMs)
4554{
4555 // mute/unmute strategies using an incompatible device combination
4556 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4557 // if unmuting, unmute only after the specified delay
4558 if (outputDesc->isDuplicated()) {
4559 return 0;
4560 }
4561
4562 uint32_t muteWaitMs = 0;
4563 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004564 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004565
4566 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4567 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004568 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004569 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4570 bool doMute = false;
4571
4572 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4573 doMute = true;
4574 outputDesc->mStrategyMutedByDevice[i] = true;
4575 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4576 doMute = true;
4577 outputDesc->mStrategyMutedByDevice[i] = false;
4578 }
Eric Laurent99401132014-05-07 19:48:15 -07004579 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004580 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004581 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004582 // skip output if it does not share any device with current output
4583 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4584 == AUDIO_DEVICE_NONE) {
4585 continue;
4586 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004587 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4588 mute ? "muting" : "unmuting", i, curDevice);
4589 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004590 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004591 if (mute) {
4592 // FIXME: should not need to double latency if volume could be applied
4593 // immediately by the audioflinger mixer. We must account for the delay
4594 // between now and the next time the audioflinger thread for this output
4595 // will process a buffer (which corresponds to one buffer size,
4596 // usually 1/2 or 1/4 of the latency).
4597 if (muteWaitMs < desc->latency() * 2) {
4598 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004599 }
4600 }
4601 }
4602 }
4603 }
4604 }
4605
Eric Laurent99401132014-05-07 19:48:15 -07004606 // temporary mute output if device selection changes to avoid volume bursts due to
4607 // different per device volumes
4608 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004609 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4610 // temporary mute duration is conservatively set to 4 times the reported latency
4611 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4612 if (muteWaitMs < tempMuteWaitMs) {
4613 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004614 }
Eric Laurentdc462862016-07-19 12:29:53 -07004615
Eric Laurent99401132014-05-07 19:48:15 -07004616 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004617 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004618 // make sure that we do not start the temporary mute period too early in case of
4619 // delayed device change
4620 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004621 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004622 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004623 }
4624 }
4625 }
4626
Eric Laurente552edb2014-03-10 17:42:56 -07004627 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4628 if (muteWaitMs > delayMs) {
4629 muteWaitMs -= delayMs;
4630 usleep(muteWaitMs * 1000);
4631 return muteWaitMs;
4632 }
4633 return 0;
4634}
4635
Eric Laurentc75307b2015-03-17 15:29:32 -07004636uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004637 audio_devices_t device,
4638 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004639 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004640 audio_patch_handle_t *patchHandle,
4641 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004642{
Eric Laurentc75307b2015-03-17 15:29:32 -07004643 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004644 AudioParameter param;
4645 uint32_t muteWaitMs;
4646
4647 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004648 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4649 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004650 return muteWaitMs;
4651 }
4652 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4653 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004654 if ((device != AUDIO_DEVICE_NONE) &&
4655 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004656 return 0;
4657 }
4658
4659 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004660 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004661
4662 audio_devices_t prevDevice = outputDesc->mDevice;
4663
Paul McLeanaa981192015-03-21 09:55:15 -07004664 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004665
4666 if (device != AUDIO_DEVICE_NONE) {
4667 outputDesc->mDevice = device;
4668 }
4669 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4670
4671 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004672 // the requested device is AUDIO_DEVICE_NONE
4673 // OR the requested device is the same as current device
4674 // AND force is not specified
4675 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004676 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004677 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4678 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004679 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004680 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004681 return muteWaitMs;
4682 }
4683
4684 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004685
Eric Laurente552edb2014-03-10 17:42:56 -07004686 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004687 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004688 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004689 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004690 DeviceVector deviceList;
4691 if ((address == NULL) || (strlen(address) == 0)) {
4692 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4693 } else {
4694 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4695 }
4696
Eric Laurent1c333e22014-05-20 10:48:17 -07004697 if (!deviceList.isEmpty()) {
4698 struct audio_patch patch;
4699 outputDesc->toAudioPortConfig(&patch.sources[0]);
4700 patch.num_sources = 1;
4701 patch.num_sinks = 0;
4702 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4703 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004704 patch.num_sinks++;
4705 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004706 ssize_t index;
4707 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4708 index = mAudioPatches.indexOfKey(*patchHandle);
4709 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004710 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004711 }
4712 sp< AudioPatch> patchDesc;
4713 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4714 if (index >= 0) {
4715 patchDesc = mAudioPatches.valueAt(index);
4716 afPatchHandle = patchDesc->mAfPatchHandle;
4717 }
4718
Eric Laurent1c333e22014-05-20 10:48:17 -07004719 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004720 &afPatchHandle,
4721 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004722 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4723 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004724 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004725 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004726 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004727 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004728 addAudioPatch(patchDesc->mHandle, patchDesc);
4729 } else {
4730 patchDesc->mPatch = patch;
4731 }
4732 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004733 if (patchHandle) {
4734 *patchHandle = patchDesc->mHandle;
4735 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004736 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004737 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004738 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004739 }
4740 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004741
4742 // inform all input as well
4743 for (size_t i = 0; i < mInputs.size(); i++) {
4744 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004745 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004746 AudioParameter inputCmd = AudioParameter();
4747 ALOGV("%s: inform input %d of device:%d", __func__,
4748 inputDescriptor->mIoHandle, device);
4749 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4750 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4751 inputCmd.toString(),
4752 delayMs);
4753 }
4754 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004755 }
Eric Laurente552edb2014-03-10 17:42:56 -07004756
4757 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004758 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004759
4760 return muteWaitMs;
4761}
4762
Eric Laurentc75307b2015-03-17 15:29:32 -07004763status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004764 int delayMs,
4765 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004766{
Eric Laurent6a94d692014-05-20 11:18:06 -07004767 ssize_t index;
4768 if (patchHandle) {
4769 index = mAudioPatches.indexOfKey(*patchHandle);
4770 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004771 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004772 }
4773 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004774 return INVALID_OPERATION;
4775 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004776 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4777 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004778 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004779 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004780 removeAudioPatch(patchDesc->mHandle);
4781 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004782 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004783 return status;
4784}
4785
4786status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4787 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004788 bool force,
4789 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004790{
4791 status_t status = NO_ERROR;
4792
Eric Laurent1f2f2232014-06-02 12:01:23 -07004793 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004794 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4795 inputDesc->mDevice = device;
4796
4797 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4798 if (!deviceList.isEmpty()) {
4799 struct audio_patch patch;
4800 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004801 // AUDIO_SOURCE_HOTWORD is for internal use only:
4802 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004803 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004804 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004805 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4806 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004807 patch.num_sinks = 1;
4808 //only one input device for now
4809 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004810 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004811 ssize_t index;
4812 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4813 index = mAudioPatches.indexOfKey(*patchHandle);
4814 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004815 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004816 }
4817 sp< AudioPatch> patchDesc;
4818 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4819 if (index >= 0) {
4820 patchDesc = mAudioPatches.valueAt(index);
4821 afPatchHandle = patchDesc->mAfPatchHandle;
4822 }
4823
Eric Laurent1c333e22014-05-20 10:48:17 -07004824 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004825 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004826 0);
4827 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004828 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004829 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004830 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004831 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004832 addAudioPatch(patchDesc->mHandle, patchDesc);
4833 } else {
4834 patchDesc->mPatch = patch;
4835 }
4836 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004837 if (patchHandle) {
4838 *patchHandle = patchDesc->mHandle;
4839 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004840 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004841 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004842 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004843 }
4844 }
4845 }
4846 return status;
4847}
4848
Eric Laurent6a94d692014-05-20 11:18:06 -07004849status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4850 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004851{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004852 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004853 ssize_t index;
4854 if (patchHandle) {
4855 index = mAudioPatches.indexOfKey(*patchHandle);
4856 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004857 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004858 }
4859 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004860 return INVALID_OPERATION;
4861 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004862 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4863 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004864 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004865 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004866 removeAudioPatch(patchDesc->mHandle);
4867 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004868 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004869 return status;
4870}
4871
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004872sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004873 String8 address,
4874 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004875 audio_format_t& format,
4876 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004877 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004878{
4879 // Choose an input profile based on the requested capture parameters: select the first available
4880 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004881 //
4882 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4883 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004884
4885 for (size_t i = 0; i < mHwModules.size(); i++)
4886 {
4887 if (mHwModules[i]->mHandle == 0) {
4888 continue;
4889 }
4890 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4891 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004892 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004893 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004894 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004895 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004896 format,
4897 &format /*updatedFormat*/,
4898 channelMask,
4899 &channelMask /*updatedChannelMask*/,
4900 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004901
Eric Laurente552edb2014-03-10 17:42:56 -07004902 return profile;
4903 }
4904 }
4905 }
4906 return NULL;
4907}
4908
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004909
4910audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004911 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004912{
François Gaffie036e1e92015-03-19 10:16:24 +01004913 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4914 audio_devices_t selectedDeviceFromMix =
4915 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004916
François Gaffie036e1e92015-03-19 10:16:24 +01004917 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4918 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004919 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004920 return getDeviceForInputSource(inputSource);
4921}
4922
4923audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4924{
Paul McLean466dc8e2015-04-17 13:15:36 -06004925 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4926 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004927 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004928 return route->mDeviceDescriptor->type();
4929 }
4930 }
4931
4932 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004933}
4934
Eric Laurente0720872014-03-11 09:30:41 -07004935float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004936 int index,
4937 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004938{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004939 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004940 // if a headset is connected, apply the following rules to ring tones and notifications
4941 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004942 // - always attenuate notifications volume by 6dB
4943 // - attenuate ring tones volume by 6dB unless music is not playing and
4944 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004945 // - if music is playing, always limit the volume to current music volume,
4946 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004947 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004948 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4949 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4950 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4951 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4952 ((stream_strategy == STRATEGY_SONIFICATION)
4953 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004954 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004955 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004956 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004957 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004958 // when the phone is ringing we must consider that music could have been paused just before
4959 // by the music application and behave as if music was active if the last music track was
4960 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004961 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004962 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004963 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004964 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004965 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004966 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4967 musicDevice),
4968 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004969 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4970 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004971 if (volumeDB > minVolDB) {
4972 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004973 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004974 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004975 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4976 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4977 // on A2DP, also ensure notification volume is not too low compared to media when
4978 // intended to be played
4979 if ((volumeDB > -96.0f) &&
4980 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4981 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4982 stream, device,
4983 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4984 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4985 }
4986 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004987 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4988 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004989 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004990 }
4991 }
4992
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004993 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07004994}
4995
Eric Laurente0720872014-03-11 09:30:41 -07004996status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004997 int index,
4998 const sp<AudioOutputDescriptor>& outputDesc,
4999 audio_devices_t device,
5000 int delayMs,
5001 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005002{
Eric Laurente552edb2014-03-10 17:42:56 -07005003 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005004 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005005 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005006 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005007 return NO_ERROR;
5008 }
François Gaffie2110e042015-03-24 08:41:51 +01005009 audio_policy_forced_cfg_t forceUseForComm =
5010 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005011 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005012 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5013 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005014 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005015 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005016 return INVALID_OPERATION;
5017 }
5018
Eric Laurentc75307b2015-03-17 15:29:32 -07005019 if (device == AUDIO_DEVICE_NONE) {
5020 device = outputDesc->device();
5021 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005022
Eric Laurentffbc80f2015-03-18 18:30:19 -07005023 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005024 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005025 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005026 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005027
Eric Laurentffbc80f2015-03-18 18:30:19 -07005028 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005029
Eric Laurent3b73df72014-03-11 09:06:29 -07005030 if (stream == AUDIO_STREAM_VOICE_CALL ||
5031 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005032 float voiceVolume;
5033 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005034 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005035 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005036 } else {
5037 voiceVolume = 1.0;
5038 }
5039
Eric Laurent18fba842016-03-31 14:41:26 -07005040 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005041 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5042 mLastVoiceVolume = voiceVolume;
5043 }
5044 }
5045
5046 return NO_ERROR;
5047}
5048
Eric Laurentc75307b2015-03-17 15:29:32 -07005049void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5050 audio_devices_t device,
5051 int delayMs,
5052 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005053{
Eric Laurentc75307b2015-03-17 15:29:32 -07005054 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005055
Eric Laurent794fde22016-03-11 09:50:45 -08005056 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005057 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005058 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005059 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005060 device,
5061 delayMs,
5062 force);
5063 }
5064}
5065
Eric Laurente0720872014-03-11 09:30:41 -07005066void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005067 bool on,
5068 const sp<AudioOutputDescriptor>& outputDesc,
5069 int delayMs,
5070 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005071{
Eric Laurent72249d52015-06-08 11:12:15 -07005072 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5073 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005074 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005075 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005076 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005077 }
5078 }
5079}
5080
Eric Laurente0720872014-03-11 09:30:41 -07005081void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005082 bool on,
5083 const sp<AudioOutputDescriptor>& outputDesc,
5084 int delayMs,
5085 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005086{
Eric Laurente552edb2014-03-10 17:42:56 -07005087 if (device == AUDIO_DEVICE_NONE) {
5088 device = outputDesc->device();
5089 }
5090
Eric Laurentc75307b2015-03-17 15:29:32 -07005091 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5092 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005093
5094 if (on) {
5095 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005096 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005097 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005098 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005099 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005100 }
5101 }
5102 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5103 outputDesc->mMuteCount[stream]++;
5104 } else {
5105 if (outputDesc->mMuteCount[stream] == 0) {
5106 ALOGV("setStreamMute() unmuting non muted stream!");
5107 return;
5108 }
5109 if (--outputDesc->mMuteCount[stream] == 0) {
5110 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005111 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005112 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005113 device,
5114 delayMs);
5115 }
5116 }
5117}
5118
Eric Laurente0720872014-03-11 09:30:41 -07005119void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005120 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005121{
Eric Laurent87ffa392015-05-22 10:32:38 -07005122 if(!hasPrimaryOutput()) {
5123 return;
5124 }
5125
Eric Laurente552edb2014-03-10 17:42:56 -07005126 // if the stream pertains to sonification strategy and we are in call we must
5127 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5128 // in the device used for phone strategy and play the tone if the selected device does not
5129 // interfere with the device used for phone strategy
5130 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5131 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005132 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005133 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5134 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005135 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005136 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5137 stream, starting, outputDesc->mDevice, stateChange);
5138 if (outputDesc->mRefCount[stream]) {
5139 int muteCount = 1;
5140 if (stateChange) {
5141 muteCount = outputDesc->mRefCount[stream];
5142 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005143 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005144 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5145 for (int i = 0; i < muteCount; i++) {
5146 setStreamMute(stream, starting, mPrimaryOutput);
5147 }
5148 } else {
5149 ALOGV("handleIncallSonification() high visibility");
5150 if (outputDesc->device() &
5151 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5152 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5153 for (int i = 0; i < muteCount; i++) {
5154 setStreamMute(stream, starting, mPrimaryOutput);
5155 }
5156 }
5157 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005158 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5159 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005160 } else {
5161 mpClientInterface->stopTone();
5162 }
5163 }
5164 }
5165 }
5166}
5167
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005168audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5169{
5170 // flags to stream type mapping
5171 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5172 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5173 }
5174 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5175 return AUDIO_STREAM_BLUETOOTH_SCO;
5176 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005177 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5178 return AUDIO_STREAM_TTS;
5179 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005180
5181 // usage to stream type mapping
5182 switch (attr->usage) {
5183 case AUDIO_USAGE_MEDIA:
5184 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005185 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5186 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005187 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5188 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005189 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5190 return AUDIO_STREAM_SYSTEM;
5191 case AUDIO_USAGE_VOICE_COMMUNICATION:
5192 return AUDIO_STREAM_VOICE_CALL;
5193
5194 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5195 return AUDIO_STREAM_DTMF;
5196
5197 case AUDIO_USAGE_ALARM:
5198 return AUDIO_STREAM_ALARM;
5199 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5200 return AUDIO_STREAM_RING;
5201
5202 case AUDIO_USAGE_NOTIFICATION:
5203 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5204 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5205 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5206 case AUDIO_USAGE_NOTIFICATION_EVENT:
5207 return AUDIO_STREAM_NOTIFICATION;
5208
5209 case AUDIO_USAGE_UNKNOWN:
5210 default:
5211 return AUDIO_STREAM_MUSIC;
5212 }
5213}
Eric Laurente83b55d2014-11-14 10:06:21 -08005214
François Gaffie53615e22015-03-19 09:24:12 +01005215bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5216{
Eric Laurente83b55d2014-11-14 10:06:21 -08005217 // has flags that map to a strategy?
5218 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5219 return true;
5220 }
5221
5222 // has known usage?
5223 switch (paa->usage) {
5224 case AUDIO_USAGE_UNKNOWN:
5225 case AUDIO_USAGE_MEDIA:
5226 case AUDIO_USAGE_VOICE_COMMUNICATION:
5227 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5228 case AUDIO_USAGE_ALARM:
5229 case AUDIO_USAGE_NOTIFICATION:
5230 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5231 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5232 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5233 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5234 case AUDIO_USAGE_NOTIFICATION_EVENT:
5235 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5236 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5237 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5238 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005239 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005240 break;
5241 default:
5242 return false;
5243 }
5244 return true;
5245}
5246
François Gaffiead3183e2015-03-18 16:55:35 +01005247bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5248 routing_strategy strategy, uint32_t inPastMs,
5249 nsecs_t sysTime) const
5250{
5251 if ((sysTime == 0) && (inPastMs != 0)) {
5252 sysTime = systemTime();
5253 }
Eric Laurent794fde22016-03-11 09:50:45 -08005254 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005255 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5256 (NUM_STRATEGIES == strategy)) &&
5257 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5258 return true;
5259 }
5260 }
5261 return false;
5262}
5263
François Gaffie2110e042015-03-24 08:41:51 +01005264audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5265{
5266 return mEngine->getForceUse(usage);
5267}
5268
5269bool AudioPolicyManager::isInCall()
5270{
5271 return isStateInCall(mEngine->getPhoneState());
5272}
5273
5274bool AudioPolicyManager::isStateInCall(int state)
5275{
5276 return is_state_in_call(state);
5277}
5278
Eric Laurentd60560a2015-04-10 11:31:20 -07005279void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5280{
5281 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5282 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5283 if (sourceDesc->mDevice->equals(deviceDesc)) {
5284 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5285 stopAudioSource(sourceDesc->getHandle());
5286 }
5287 }
5288
5289 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5290 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5291 bool release = false;
5292 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5293 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5294 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5295 source->ext.device.type == deviceDesc->type()) {
5296 release = true;
5297 }
5298 }
5299 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5300 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5301 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5302 sink->ext.device.type == deviceDesc->type()) {
5303 release = true;
5304 }
5305 }
5306 if (release) {
5307 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5308 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5309 }
5310 }
5311}
5312
Phil Burk09bc4612016-02-24 15:58:15 -08005313// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005314void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5315 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005316 // TODO Set this based on Config properties.
5317 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005318
5319 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5320 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005321 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005322
5323 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005324 bool supportsAC3 = false;
5325 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005326 bool supportsIEC61937 = false;
5327 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5328 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005329 switch (format) {
5330 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005331 supportsAC3 = true;
5332 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005333 case AUDIO_FORMAT_E_AC3:
5334 case AUDIO_FORMAT_DTS:
5335 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005336 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005337 break;
5338 case AUDIO_FORMAT_IEC61937:
5339 supportsIEC61937 = true;
5340 break;
5341 default:
5342 break;
5343 }
5344 }
Phil Burk09bc4612016-02-24 15:58:15 -08005345
5346 // Modify formats based on surround preferences.
5347 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005348 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5349 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5350 // Remove surround sound related formats.
5351 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5352 audio_format_t format = formats[formatIndex];
5353 switch(format) {
5354 case AUDIO_FORMAT_AC3:
5355 case AUDIO_FORMAT_E_AC3:
5356 case AUDIO_FORMAT_DTS:
5357 case AUDIO_FORMAT_DTS_HD:
5358 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005359 formats.removeAt(formatIndex);
5360 break;
5361 default:
5362 formatIndex++; // keep it
5363 break;
5364 }
Phil Burk09bc4612016-02-24 15:58:15 -08005365 }
Phil Burk07ac1142016-03-25 13:39:29 -07005366 supportsAC3 = false;
5367 supportsOtherSurround = false;
5368 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005369 }
Phil Burk07ac1142016-03-25 13:39:29 -07005370 } else { // AUTO or ALWAYS
5371 // Most TVs support AC3 even if they do not report it in the EDID.
5372 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5373 && !supportsAC3) {
5374 formats.add(AUDIO_FORMAT_AC3);
5375 supportsAC3 = true;
5376 }
5377
5378 // If ALWAYS, add support for raw surround formats if all are missing.
5379 // This assumes that if any of these formats are reported by the HAL
5380 // then the report is valid and should not be modified.
5381 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5382 && !supportsOtherSurround) {
5383 formats.add(AUDIO_FORMAT_E_AC3);
5384 formats.add(AUDIO_FORMAT_DTS);
5385 formats.add(AUDIO_FORMAT_DTS_HD);
5386 supportsOtherSurround = true;
5387 }
5388
5389 // Add support for IEC61937 if any raw surround supported.
5390 // The HAL could do this but add it here, just in case.
5391 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5392 formats.add(AUDIO_FORMAT_IEC61937);
5393 supportsIEC61937 = true;
5394 }
Phil Burk09bc4612016-02-24 15:58:15 -08005395 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005396}
5397
5398// Modify the list of channel masks supported.
5399void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5400 ChannelsVector &channelMasks = *channelMasksPtr;
5401 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5402 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5403
5404 // If NEVER, then remove support for channelMasks > stereo.
5405 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5406 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5407 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5408 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5409 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5410 channelMasks.removeAt(maskIndex);
5411 } else {
5412 maskIndex++;
5413 }
5414 }
5415 // If ALWAYS, then make sure we at least support 5.1
5416 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5417 bool supports5dot1 = false;
5418 // Are there any channel masks that can be considered "surround"?
5419 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5420 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5421 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5422 supports5dot1 = true;
5423 break;
5424 }
5425 }
5426 // If not then add 5.1 support.
5427 if (!supports5dot1) {
5428 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5429 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5430 }
Phil Burk09bc4612016-02-24 15:58:15 -08005431 }
5432}
5433
Phil Burk00eeb322016-03-31 12:41:00 -07005434void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5435 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005436 AudioProfileVector &profiles)
5437{
5438 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005439
François Gaffie112b0af2015-11-19 16:13:25 +01005440 // Format MUST be checked first to update the list of AudioProfile
5441 if (profiles.hasDynamicFormat()) {
5442 reply = mpClientInterface->getParameters(ioHandle,
5443 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005444 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005445 AudioParameter repliedParameters(reply);
5446 if (repliedParameters.get(
5447 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005448 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5449 return;
5450 }
Phil Burk09bc4612016-02-24 15:58:15 -08005451 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005452 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005453 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005454 }
Phil Burk09bc4612016-02-24 15:58:15 -08005455 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005456 }
5457 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5458
Eric Laurent20eb3a42016-01-26 18:39:17 -08005459 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005460 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005461 ChannelsVector channelMasks;
5462 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005463 AudioParameter requestedParameters;
5464 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5465
5466 if (profiles.hasDynamicRateFor(format)) {
5467 reply = mpClientInterface->getParameters(ioHandle,
5468 requestedParameters.toString() + ";" +
5469 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5470 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005471 AudioParameter repliedParameters(reply);
5472 if (repliedParameters.get(
5473 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5474 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005475 }
5476 }
5477 if (profiles.hasDynamicChannelsFor(format)) {
5478 reply = mpClientInterface->getParameters(ioHandle,
5479 requestedParameters.toString() + ";" +
5480 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5481 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005482 AudioParameter repliedParameters(reply);
5483 if (repliedParameters.get(
5484 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5485 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005486 if (device == AUDIO_DEVICE_OUT_HDMI) {
5487 filterSurroundChannelMasks(&channelMasks);
5488 }
François Gaffie112b0af2015-11-19 16:13:25 +01005489 }
5490 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005491 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005492 }
5493}
Eric Laurentd60560a2015-04-10 11:31:20 -07005494
Eric Laurente552edb2014-03-10 17:42:56 -07005495}; // namespace android