blob: 9095cdec946cc5560662294d62553bb049323377 [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())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001324 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1325 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001326 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001327 newDevice2,
1328 force,
Eric Laurentc75307b2015-03-17 15:29:32 -07001329 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001330 }
1331 }
1332 // update the outputs if stopping one with a stream that can affect notification routing
1333 handleNotificationRoutingForStream(stream);
1334 }
1335 return NO_ERROR;
1336 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001337 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001338 return INVALID_OPERATION;
1339 }
1340}
1341
Eric Laurente83b55d2014-11-14 10:06:21 -08001342void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001343 audio_stream_type_t stream __unused,
1344 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001345{
1346 ALOGV("releaseOutput() %d", output);
1347 ssize_t index = mOutputs.indexOfKey(output);
1348 if (index < 0) {
1349 ALOGW("releaseOutput() releasing unknown output %d", output);
1350 return;
1351 }
1352
1353#ifdef AUDIO_POLICY_TEST
1354 int testIndex = testOutputIndex(output);
1355 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001356 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001357 if (outputDesc->isActive()) {
1358 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001359 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001360 mTestOutputs[testIndex] = 0;
1361 }
1362 return;
1363 }
1364#endif //AUDIO_POLICY_TEST
1365
Paul McLeanaa981192015-03-21 09:55:15 -07001366 // Routing
1367 mOutputRoutes.removeRoute(session);
1368
Eric Laurentc75307b2015-03-17 15:29:32 -07001369 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001370 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001371 if (desc->mDirectOpenCount <= 0) {
1372 ALOGW("releaseOutput() invalid open count %d for output %d",
1373 desc->mDirectOpenCount, output);
1374 return;
1375 }
1376 if (--desc->mDirectOpenCount == 0) {
1377 closeOutput(output);
1378 // If effects where present on the output, audioflinger moved them to the primary
1379 // output by default: move them back to the appropriate output.
1380 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001381 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001382 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1383 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001384 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001385 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001386 }
1387 }
1388}
1389
1390
Eric Laurentcaf7f482014-11-25 17:50:47 -08001391status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1392 audio_io_handle_t *input,
1393 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001394 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001395 uint32_t samplingRate,
1396 audio_format_t format,
1397 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001398 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001399 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001400 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001401{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001402 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1403 "session %d, flags %#x",
1404 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001405
Eric Laurentcaf7f482014-11-25 17:50:47 -08001406 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001407 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001408 audio_devices_t device;
1409 // handle legacy remote submix case where the address was not always specified
1410 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001411 audio_source_t inputSource = attr->source;
1412 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001413 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001414
Eric Laurentc447ded2015-01-06 08:47:05 -08001415 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1416 inputSource = AUDIO_SOURCE_MIC;
1417 }
1418 halInputSource = inputSource;
1419
Paul McLean466dc8e2015-04-17 13:15:36 -06001420 // Explicit routing?
1421 sp<DeviceDescriptor> deviceDesc;
1422 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1423 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1424 deviceDesc = mAvailableInputDevices[i];
1425 break;
1426 }
1427 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001428 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001429
Eric Laurentc447ded2015-01-06 08:47:05 -08001430 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001431 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001432 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001433 if (ret != NO_ERROR) {
1434 return ret;
1435 }
1436 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001437 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1438 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001439 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001440 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001441 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001442 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001443 return BAD_VALUE;
1444 }
Eric Laurentc722f302014-12-10 11:21:49 -08001445 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001446 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001447 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1448 // there is an external policy, but this input is attached to a mix of recorders,
1449 // meaning it receives audio injected into the framework, so the recorder doesn't
1450 // know about it and is therefore considered "legacy"
1451 *inputType = API_INPUT_LEGACY;
1452 } else {
1453 // recording a mix of players defined by an external policy, we're rerouting for
1454 // an external policy
1455 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1456 }
Eric Laurentc722f302014-12-10 11:21:49 -08001457 } else if (audio_is_remote_submix_device(device)) {
1458 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001459 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001460 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1461 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001462 } else {
1463 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001464 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001465
Eric Laurent599c7582015-12-07 18:05:55 -08001466 }
1467
1468 *input = getInputForDevice(device, address, session, uid, inputSource,
1469 samplingRate, format, channelMask, flags,
1470 policyMix);
1471 if (*input == AUDIO_IO_HANDLE_NONE) {
1472 mInputRoutes.removeRoute(session);
1473 return INVALID_OPERATION;
1474 }
1475 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1476 return NO_ERROR;
1477}
1478
1479
1480audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1481 String8 address,
1482 audio_session_t session,
1483 uid_t uid,
1484 audio_source_t inputSource,
1485 uint32_t samplingRate,
1486 audio_format_t format,
1487 audio_channel_mask_t channelMask,
1488 audio_input_flags_t flags,
1489 AudioMix *policyMix)
1490{
1491 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1492 audio_source_t halInputSource = inputSource;
1493 bool isSoundTrigger = false;
1494
1495 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1496 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1497 if (index >= 0) {
1498 input = mSoundTriggerSessions.valueFor(session);
1499 isSoundTrigger = true;
1500 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1501 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1502 } else {
1503 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001504 }
1505 }
1506
Andy Hungf129b032015-04-07 13:45:50 -07001507 // find a compatible input profile (not necessarily identical in parameters)
1508 sp<IOProfile> profile;
1509 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001510 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001511 audio_format_t profileFormat = format;
1512 audio_channel_mask_t profileChannelMask = channelMask;
1513 audio_input_flags_t profileFlags = flags;
1514 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001515 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001516 profileSamplingRate, profileFormat, profileChannelMask,
1517 profileFlags);
1518 if (profile != 0) {
1519 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001520 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1521 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001522 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1523 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1524 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001525 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1526 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001527 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001528 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001529 }
Eric Laurente552edb2014-03-10 17:42:56 -07001530 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001531 // Pick input sampling rate if not specified by client
1532 if (samplingRate == 0) {
1533 samplingRate = profileSamplingRate;
1534 }
Eric Laurente552edb2014-03-10 17:42:56 -07001535
Eric Laurent322b4d22015-04-03 15:57:54 -07001536 if (profile->getModuleHandle() == 0) {
1537 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001538 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001539 }
1540
Eric Laurent599c7582015-12-07 18:05:55 -08001541 sp<AudioSession> audioSession = new AudioSession(session,
1542 inputSource,
1543 format,
1544 samplingRate,
1545 channelMask,
1546 flags,
1547 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001548 isSoundTrigger,
1549 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001550
Eric Laurent232f26f2016-02-17 18:06:27 -08001551// TODO enable input reuse
1552#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001553 // reuse an open input if possible
1554 for (size_t i = 0; i < mInputs.size(); i++) {
1555 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001556 // reuse input if it shares the same profile and same sound trigger attribute
1557 if (profile == desc->mProfile &&
1558 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001559
1560 sp<AudioSession> as = desc->getAudioSession(session);
1561 if (as != 0) {
1562 // do not allow unmatching properties on same session
1563 if (as->matches(audioSession)) {
1564 as->changeOpenCount(1);
1565 } else {
1566 ALOGW("getInputForDevice() record with different attributes"
1567 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001568 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001569 }
1570 } else {
1571 desc->addAudioSession(session, audioSession);
1572 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001573 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1574 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001575 }
1576 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001577#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001578
Eric Laurentcf2c0212014-07-25 16:20:43 -07001579 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001580 config.sample_rate = profileSamplingRate;
1581 config.channel_mask = profileChannelMask;
1582 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001583
Eric Laurent322b4d22015-04-03 15:57:54 -07001584 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001585 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001586 &config,
1587 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001588 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001589 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001590 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001591
1592 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001593 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001594 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001595 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001596 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001597 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1598 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001599 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001600 if (input != AUDIO_IO_HANDLE_NONE) {
1601 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001602 }
Eric Laurent599c7582015-12-07 18:05:55 -08001603 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001604 }
1605
Eric Laurent1f2f2232014-06-02 12:01:23 -07001606 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001607 inputDesc->mSamplingRate = profileSamplingRate;
1608 inputDesc->mFormat = profileFormat;
1609 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001610 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001611 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001612 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001613
Eric Laurent599c7582015-12-07 18:05:55 -08001614 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001615 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001616
Eric Laurent599c7582015-12-07 18:05:55 -08001617 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001618}
1619
Eric Laurent4dc68062014-07-28 17:26:49 -07001620status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001621 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001622{
1623 ALOGV("startInput() input %d", input);
1624 ssize_t index = mInputs.indexOfKey(input);
1625 if (index < 0) {
1626 ALOGW("startInput() unknown input %d", input);
1627 return BAD_VALUE;
1628 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001629 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001630
Eric Laurent599c7582015-12-07 18:05:55 -08001631 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1632 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001633 ALOGW("startInput() unknown session %d on input %d", session, input);
1634 return BAD_VALUE;
1635 }
1636
Eric Laurent232f26f2016-02-17 18:06:27 -08001637 // virtual input devices are compatible with other input devices
1638 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001639
Eric Laurent232f26f2016-02-17 18:06:27 -08001640 // for a non-virtual input device, check if there is another (non-virtual) active input
1641 audio_io_handle_t activeInput = mInputs.getActiveInput();
1642 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001643
Eric Laurent232f26f2016-02-17 18:06:27 -08001644 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1645 // otherwise the active input continues and the new input cannot be started.
1646 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1647 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1648 !activeDesc->hasPreemptedSession(session)) {
1649 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1650 //FIXME: consider all active sessions
1651 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1652 audio_session_t activeSession = activeSessions.keyAt(0);
1653 SortedVector<audio_session_t> sessions =
1654 activeDesc->getPreemptedSessions();
1655 sessions.add(activeSession);
1656 inputDesc->setPreemptedSessions(sessions);
1657 stopInput(activeInput, activeSession);
1658 releaseInput(activeInput, activeSession);
1659 } else {
1660 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1661 return INVALID_OPERATION;
1662 }
1663 }
1664
1665 // Do not allow capture if an active voice call is using a software patch and
1666 // the call TX source device is on the same HW module.
1667 // FIXME: would be better to refine to only inputs whose profile connects to the
1668 // call TX device but this information is not in the audio patch
1669 if (mCallTxPatch != 0 &&
1670 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1671 return INVALID_OPERATION;
1672 }
1673 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001674
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001675 // Routing?
1676 mInputRoutes.incRouteActivity(session);
1677
Eric Laurent232f26f2016-02-17 18:06:27 -08001678 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1679 // if input maps to a dynamic policy with an activity listener, notify of state change
1680 if ((inputDesc->mPolicyMix != NULL)
1681 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001682 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001683 MIX_STATE_MIXING);
1684 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001685
Eric Laurent232f26f2016-02-17 18:06:27 -08001686 if (mInputs.activeInputsCount() == 0) {
1687 SoundTrigger::setCaptureState(true);
1688 }
1689 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001690
Eric Laurent232f26f2016-02-17 18:06:27 -08001691 // automatically enable the remote submix output when input is started if not
1692 // used by a policy mix of type MIX_TYPE_RECORDERS
1693 // For remote submix (a virtual device), we open only one input per capture request.
1694 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1695 String8 address = String8("");
1696 if (inputDesc->mPolicyMix == NULL) {
1697 address = String8("0");
1698 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001699 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001700 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001701 if (address != "") {
1702 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1703 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1704 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001705 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001706 }
Eric Laurente552edb2014-03-10 17:42:56 -07001707 }
1708
Eric Laurent599c7582015-12-07 18:05:55 -08001709 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001710
Eric Laurent232f26f2016-02-17 18:06:27 -08001711 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001712 return NO_ERROR;
1713}
1714
Eric Laurent4dc68062014-07-28 17:26:49 -07001715status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1716 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001717{
1718 ALOGV("stopInput() input %d", input);
1719 ssize_t index = mInputs.indexOfKey(input);
1720 if (index < 0) {
1721 ALOGW("stopInput() unknown input %d", input);
1722 return BAD_VALUE;
1723 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001724 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001725
Eric Laurent599c7582015-12-07 18:05:55 -08001726 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001727 if (index < 0) {
1728 ALOGW("stopInput() unknown session %d on input %d", session, input);
1729 return BAD_VALUE;
1730 }
1731
Eric Laurent599c7582015-12-07 18:05:55 -08001732 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001733 ALOGW("stopInput() input %d already stopped", input);
1734 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001735 }
1736
Eric Laurent599c7582015-12-07 18:05:55 -08001737 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001738
1739 // Routing?
1740 mInputRoutes.decRouteActivity(session);
1741
Eric Laurent232f26f2016-02-17 18:06:27 -08001742 if (!inputDesc->isActive()) {
1743 // if input maps to a dynamic policy with an activity listener, notify of state change
1744 if ((inputDesc->mPolicyMix != NULL)
1745 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001746 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001747 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001748 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001749
1750 // automatically disable the remote submix output when input is stopped if not
1751 // used by a policy mix of type MIX_TYPE_RECORDERS
1752 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1753 String8 address = String8("");
1754 if (inputDesc->mPolicyMix == NULL) {
1755 address = String8("0");
1756 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001757 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001758 }
1759 if (address != "") {
1760 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1761 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1762 address, "remote-submix");
1763 }
1764 }
1765
1766 resetInputDevice(input);
1767
1768 if (mInputs.activeInputsCount() == 0) {
1769 SoundTrigger::setCaptureState(false);
1770 }
1771 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001772 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001773 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001774}
1775
Eric Laurent4dc68062014-07-28 17:26:49 -07001776void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1777 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001778{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001779
Eric Laurente552edb2014-03-10 17:42:56 -07001780 ALOGV("releaseInput() %d", input);
1781 ssize_t index = mInputs.indexOfKey(input);
1782 if (index < 0) {
1783 ALOGW("releaseInput() releasing unknown input %d", input);
1784 return;
1785 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001786
1787 // Routing
1788 mInputRoutes.removeRoute(session);
1789
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001790 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1791 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001792
Eric Laurent599c7582015-12-07 18:05:55 -08001793 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001794 if (index < 0) {
1795 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1796 return;
1797 }
Eric Laurent599c7582015-12-07 18:05:55 -08001798
1799 if (audioSession->openCount() == 0) {
1800 ALOGW("releaseInput() invalid open count %d on session %d",
1801 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001802 return;
1803 }
Eric Laurent599c7582015-12-07 18:05:55 -08001804
1805 if (audioSession->changeOpenCount(-1) == 0) {
1806 inputDesc->removeAudioSession(session);
1807 }
1808
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001809 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001810 ALOGV("releaseInput() exit > 0");
1811 return;
1812 }
1813
Eric Laurent05b90f82014-08-27 15:32:29 -07001814 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001815 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001816 ALOGV("releaseInput() exit");
1817}
1818
Eric Laurentd4692962014-05-05 18:13:44 -07001819void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001820 bool patchRemoved = false;
1821
Eric Laurentd4692962014-05-05 18:13:44 -07001822 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001823 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001824 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001825 if (patch_index >= 0) {
1826 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001827 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001828 mAudioPatches.removeItemsAt(patch_index);
1829 patchRemoved = true;
1830 }
Eric Laurentd4692962014-05-05 18:13:44 -07001831 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1832 }
1833 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001834 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001835 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001836
1837 if (patchRemoved) {
1838 mpClientInterface->onAudioPatchListUpdate();
1839 }
Eric Laurentd4692962014-05-05 18:13:44 -07001840}
1841
Eric Laurente0720872014-03-11 09:30:41 -07001842void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001843 int indexMin,
1844 int indexMax)
1845{
1846 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001847 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001848
1849 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001850 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1851 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001852 continue;
1853 }
1854 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001855 }
Eric Laurente552edb2014-03-10 17:42:56 -07001856}
1857
Eric Laurente0720872014-03-11 09:30:41 -07001858status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001859 int index,
1860 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001861{
1862
François Gaffied1ab2bd2015-12-02 18:20:06 +01001863 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1864 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001865 return BAD_VALUE;
1866 }
1867 if (!audio_is_output_device(device)) {
1868 return BAD_VALUE;
1869 }
1870
1871 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001872 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001873
Eric Laurent1fd372e2016-04-06 14:23:53 -07001874 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001875 stream, device, index);
1876
Eric Laurent28d09f02016-03-08 10:43:05 -08001877 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001878 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1879 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001880 continue;
1881 }
1882 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1883 }
Eric Laurente552edb2014-03-10 17:42:56 -07001884
Eric Laurent1fd372e2016-04-06 14:23:53 -07001885 // update volume on all outputs and streams matching the following:
1886 // - The requested stream (or a stream matching for volume control) is active on the output
1887 // - The device (or devices) selected by the strategy corresponding to this stream includes
1888 // the requested device
1889 // - For non default requested device, currently selected device on the output is either the
1890 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001891 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1892 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001893 status_t status = NO_ERROR;
1894 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001895 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1896 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001897 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1898 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001899 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001900 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001901 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1902 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001903 continue;
1904 }
Eric Laurent794fde22016-03-11 09:50:45 -08001905 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001906 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001907 if ((curStreamDevice & device) == 0) {
1908 continue;
1909 }
1910 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001911 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001912 curStreamDevice |= device;
1913 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1914 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1915 applyDefault = true;
1916 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001917
Eric Laurent1fd372e2016-04-06 14:23:53 -07001918 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurentdc462862016-07-19 12:29:53 -07001919 //FIXME: workaround for truncated touch sounds
1920 // delayed volume change for system stream to be removed when the problem is
1921 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001922 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001923 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1924 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001925 if (volStatus != NO_ERROR) {
1926 status = volStatus;
1927 }
1928 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001929 }
Eric Laurente552edb2014-03-10 17:42:56 -07001930 }
1931 return status;
1932}
1933
Eric Laurente0720872014-03-11 09:30:41 -07001934status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001935 int *index,
1936 audio_devices_t device)
1937{
1938 if (index == NULL) {
1939 return BAD_VALUE;
1940 }
1941 if (!audio_is_output_device(device)) {
1942 return BAD_VALUE;
1943 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001944 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001945 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001946 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001947 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1948 }
François Gaffiedfd74092015-03-19 12:10:59 +01001949 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001950
François Gaffied1ab2bd2015-12-02 18:20:06 +01001951 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001952 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1953 return NO_ERROR;
1954}
1955
Eric Laurente0720872014-03-11 09:30:41 -07001956audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001957 const SortedVector<audio_io_handle_t>& outputs)
1958{
1959 // select one output among several suitable for global effects.
1960 // The priority is as follows:
1961 // 1: An offloaded output. If the effect ends up not being offloadable,
1962 // AudioFlinger will invalidate the track and the offloaded output
1963 // will be closed causing the effect to be moved to a PCM output.
1964 // 2: A deep buffer output
1965 // 3: the first output in the list
1966
1967 if (outputs.size() == 0) {
1968 return 0;
1969 }
1970
1971 audio_io_handle_t outputOffloaded = 0;
1972 audio_io_handle_t outputDeepBuffer = 0;
1973
1974 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001975 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001976 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001977 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1978 outputOffloaded = outputs[i];
1979 }
1980 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1981 outputDeepBuffer = outputs[i];
1982 }
1983 }
1984
1985 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1986 outputOffloaded, outputDeepBuffer);
1987 if (outputOffloaded != 0) {
1988 return outputOffloaded;
1989 }
1990 if (outputDeepBuffer != 0) {
1991 return outputDeepBuffer;
1992 }
1993
1994 return outputs[0];
1995}
1996
Eric Laurente0720872014-03-11 09:30:41 -07001997audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001998{
1999 // apply simple rule where global effects are attached to the same output as MUSIC streams
2000
Eric Laurent3b73df72014-03-11 09:06:29 -07002001 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002002 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2003 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
2004
2005 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2006 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2007 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2008
2009 return output;
2010}
2011
Eric Laurente0720872014-03-11 09:30:41 -07002012status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002013 audio_io_handle_t io,
2014 uint32_t strategy,
2015 int session,
2016 int id)
2017{
2018 ssize_t index = mOutputs.indexOfKey(io);
2019 if (index < 0) {
2020 index = mInputs.indexOfKey(io);
2021 if (index < 0) {
2022 ALOGW("registerEffect() unknown io %d", io);
2023 return INVALID_OPERATION;
2024 }
2025 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002026 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002027}
2028
Eric Laurentc75307b2015-03-17 15:29:32 -07002029bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2030{
Eric Laurent28d09f02016-03-08 10:43:05 -08002031 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002032 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2033 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002034 continue;
2035 }
2036 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2037 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002038 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002039}
2040
2041bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2042{
2043 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2044}
2045
Eric Laurente0720872014-03-11 09:30:41 -07002046bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002047{
2048 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002049 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002050 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002051 return true;
2052 }
2053 }
2054 return false;
2055}
2056
Eric Laurent275e8e92014-11-30 15:14:47 -08002057// Register a list of custom mixes with their attributes and format.
2058// When a mix is registered, corresponding input and output profiles are
2059// added to the remote submix hw module. The profile contains only the
2060// parameters (sampling rate, format...) specified by the mix.
2061// The corresponding input remote submix device is also connected.
2062//
2063// When a remote submix device is connected, the address is checked to select the
2064// appropriate profile and the corresponding input or output stream is opened.
2065//
2066// When capture starts, getInputForAttr() will:
2067// - 1 look for a mix matching the address passed in attribtutes tags if any
2068// - 2 if none found, getDeviceForInputSource() will:
2069// - 2.1 look for a mix matching the attributes source
2070// - 2.2 if none found, default to device selection by policy rules
2071// At this time, the corresponding output remote submix device is also connected
2072// and active playback use cases can be transferred to this mix if needed when reconnecting
2073// after AudioTracks are invalidated
2074//
2075// When playback starts, getOutputForAttr() will:
2076// - 1 look for a mix matching the address passed in attribtutes tags if any
2077// - 2 if none found, look for a mix matching the attributes usage
2078// - 3 if none found, default to device and output selection by policy rules.
2079
2080status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2081{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002082 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2083 status_t res = NO_ERROR;
2084
2085 sp<HwModule> rSubmixModule;
2086 // examine each mix's route type
2087 for (size_t i = 0; i < mixes.size(); i++) {
2088 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2089 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2090 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002091 break;
2092 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002093 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2094 // Loop back through "remote submix"
2095 if (rSubmixModule == 0) {
2096 for (size_t j = 0; i < mHwModules.size(); j++) {
2097 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2098 && mHwModules[j]->mHandle != 0) {
2099 rSubmixModule = mHwModules[j];
2100 break;
2101 }
2102 }
2103 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002104
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002105 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002106
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002107 if (rSubmixModule == 0) {
2108 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2109 res = INVALID_OPERATION;
2110 break;
2111 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002112
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002113 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002114
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002115 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002116 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002117 res = INVALID_OPERATION;
2118 break;
2119 }
2120 audio_config_t outputConfig = mixes[i].mFormat;
2121 audio_config_t inputConfig = mixes[i].mFormat;
2122 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2123 // stereo and let audio flinger do the channel conversion if needed.
2124 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2125 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2126 rSubmixModule->addOutputProfile(address, &outputConfig,
2127 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2128 rSubmixModule->addInputProfile(address, &inputConfig,
2129 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002130
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002131 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2132 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2133 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2134 address.string(), "remote-submix");
2135 } else {
2136 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2137 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2138 address.string(), "remote-submix");
2139 }
2140 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002141 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002142 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002143 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2144 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002145
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002146 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002147 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2148 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2149 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2150 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2151 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2152 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002153 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2154 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002155 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2156 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002157 } else {
2158 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002159 }
2160 break;
2161 }
2162 }
2163
2164 if (res != NO_ERROR) {
2165 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002166 i, device, address.string());
2167 res = INVALID_OPERATION;
2168 break;
2169 } else if (!foundOutput) {
2170 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2171 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002172 res = INVALID_OPERATION;
2173 break;
2174 }
Eric Laurentc722f302014-12-10 11:21:49 -08002175 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002176 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002177 if (res != NO_ERROR) {
2178 unregisterPolicyMixes(mixes);
2179 }
2180 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002181}
2182
2183status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2184{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002185 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002186 status_t res = NO_ERROR;
2187 sp<HwModule> rSubmixModule;
2188 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002189 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002190 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002191
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002192 if (rSubmixModule == 0) {
2193 for (size_t j = 0; i < mHwModules.size(); j++) {
2194 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2195 && mHwModules[j]->mHandle != 0) {
2196 rSubmixModule = mHwModules[j];
2197 break;
2198 }
2199 }
2200 }
2201 if (rSubmixModule == 0) {
2202 res = INVALID_OPERATION;
2203 continue;
2204 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002205
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002206 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002207
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002208 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2209 res = INVALID_OPERATION;
2210 continue;
2211 }
2212
2213 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2214 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2215 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2216 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2217 address.string(), "remote-submix");
2218 }
2219 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2220 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2221 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2222 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2223 address.string(), "remote-submix");
2224 }
2225 rSubmixModule->removeOutputProfile(address);
2226 rSubmixModule->removeInputProfile(address);
2227
2228 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2229 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2230 res = INVALID_OPERATION;
2231 continue;
2232 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002233 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002234 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002235 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002236}
2237
Eric Laurente552edb2014-03-10 17:42:56 -07002238
Eric Laurente0720872014-03-11 09:30:41 -07002239status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002240{
2241 const size_t SIZE = 256;
2242 char buffer[SIZE];
2243 String8 result;
2244
2245 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2246 result.append(buffer);
2247
Eric Laurent87ffa392015-05-22 10:32:38 -07002248 snprintf(buffer, SIZE, " Primary Output: %d\n",
2249 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002250 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002251 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002252 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002253 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002254 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
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 media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
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 record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
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 dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002261 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002262 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002263 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002264 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002265 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002266 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002267 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2268 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2269 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002270 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2271 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002272 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2273 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002274
François Gaffie2110e042015-03-24 08:41:51 +01002275 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002276
François Gaffie112b0af2015-11-19 16:13:25 +01002277 mAvailableOutputDevices.dump(fd, String8("Available output"));
2278 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002279 mHwModules.dump(fd);
2280 mOutputs.dump(fd);
2281 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002282 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002283 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002284 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002285
2286 return NO_ERROR;
2287}
2288
2289// This function checks for the parameters which can be offloaded.
2290// This can be enhanced depending on the capability of the DSP and policy
2291// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002292bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002293{
2294 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002295 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002296 offloadInfo.sample_rate, offloadInfo.channel_mask,
2297 offloadInfo.format,
2298 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2299 offloadInfo.has_video);
2300
Andy Hung2ddee192015-12-18 17:34:44 -08002301 if (mMasterMono) {
2302 return false; // no offloading if mono is set.
2303 }
2304
Eric Laurente552edb2014-03-10 17:42:56 -07002305 // Check if offload has been disabled
2306 char propValue[PROPERTY_VALUE_MAX];
2307 if (property_get("audio.offload.disable", propValue, "0")) {
2308 if (atoi(propValue) != 0) {
2309 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2310 return false;
2311 }
2312 }
2313
2314 // Check if stream type is music, then only allow offload as of now.
2315 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2316 {
2317 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2318 return false;
2319 }
2320
2321 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002322 const bool allowOffloadWithVideo =
2323 property_get_bool("audio.offload.video", false /* default_value */);
2324 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002325 ALOGV("isOffloadSupported: has_video == true, returning false");
2326 return false;
2327 }
2328
2329 //If duration is less than minimum value defined in property, return false
2330 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2331 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2332 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2333 return false;
2334 }
2335 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2336 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2337 return false;
2338 }
2339
2340 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2341 // creating an offloaded track and tearing it down immediately after start when audioflinger
2342 // detects there is an active non offloadable effect.
2343 // FIXME: We should check the audio session here but we do not have it in this context.
2344 // This may prevent offloading in rare situations where effects are left active by apps
2345 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002346 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002347 return false;
2348 }
2349
2350 // See if there is a profile to support this.
2351 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002352 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002353 offloadInfo.sample_rate,
2354 offloadInfo.format,
2355 offloadInfo.channel_mask,
2356 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002357 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2358 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002359}
2360
Eric Laurent6a94d692014-05-20 11:18:06 -07002361status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2362 audio_port_type_t type,
2363 unsigned int *num_ports,
2364 struct audio_port *ports,
2365 unsigned int *generation)
2366{
2367 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2368 generation == NULL) {
2369 return BAD_VALUE;
2370 }
2371 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2372 if (ports == NULL) {
2373 *num_ports = 0;
2374 }
2375
2376 size_t portsWritten = 0;
2377 size_t portsMax = *num_ports;
2378 *num_ports = 0;
2379 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002380 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2381 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002382 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002383 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2384 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2385 continue;
2386 }
2387 if (portsWritten < portsMax) {
2388 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2389 }
2390 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002391 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002392 }
2393 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002394 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2395 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2396 continue;
2397 }
2398 if (portsWritten < portsMax) {
2399 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2400 }
2401 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002402 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002403 }
2404 }
2405 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2406 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2407 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2408 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2409 }
2410 *num_ports += mInputs.size();
2411 }
2412 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002413 size_t numOutputs = 0;
2414 for (size_t i = 0; i < mOutputs.size(); i++) {
2415 if (!mOutputs[i]->isDuplicated()) {
2416 numOutputs++;
2417 if (portsWritten < portsMax) {
2418 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2419 }
2420 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002421 }
Eric Laurent84c70242014-06-23 08:46:27 -07002422 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002423 }
2424 }
2425 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002426 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002427 return NO_ERROR;
2428}
2429
2430status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2431{
2432 return NO_ERROR;
2433}
2434
Eric Laurent6a94d692014-05-20 11:18:06 -07002435status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2436 audio_patch_handle_t *handle,
2437 uid_t uid)
2438{
2439 ALOGV("createAudioPatch()");
2440
2441 if (handle == NULL || patch == NULL) {
2442 return BAD_VALUE;
2443 }
2444 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2445
Eric Laurent874c42872014-08-08 15:13:39 -07002446 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2447 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2448 return BAD_VALUE;
2449 }
2450 // only one source per audio patch supported for now
2451 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002452 return INVALID_OPERATION;
2453 }
Eric Laurent874c42872014-08-08 15:13:39 -07002454
2455 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002456 return INVALID_OPERATION;
2457 }
Eric Laurent874c42872014-08-08 15:13:39 -07002458 for (size_t i = 0; i < patch->num_sinks; i++) {
2459 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2460 return INVALID_OPERATION;
2461 }
2462 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002463
2464 sp<AudioPatch> patchDesc;
2465 ssize_t index = mAudioPatches.indexOfKey(*handle);
2466
Eric Laurent6a94d692014-05-20 11:18:06 -07002467 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2468 patch->sources[0].role,
2469 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002470#if LOG_NDEBUG == 0
2471 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002472 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002473 patch->sinks[i].role,
2474 patch->sinks[i].type);
2475 }
2476#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002477
2478 if (index >= 0) {
2479 patchDesc = mAudioPatches.valueAt(index);
2480 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2481 mUidCached, patchDesc->mUid, uid);
2482 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2483 return INVALID_OPERATION;
2484 }
2485 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002486 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002487 }
2488
2489 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002490 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002491 if (outputDesc == NULL) {
2492 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2493 return BAD_VALUE;
2494 }
Eric Laurent84c70242014-06-23 08:46:27 -07002495 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2496 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002497 if (patchDesc != 0) {
2498 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2499 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2500 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2501 return BAD_VALUE;
2502 }
2503 }
Eric Laurent874c42872014-08-08 15:13:39 -07002504 DeviceVector devices;
2505 for (size_t i = 0; i < patch->num_sinks; i++) {
2506 // Only support mix to devices connection
2507 // TODO add support for mix to mix connection
2508 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2509 ALOGV("createAudioPatch() source mix but sink is not a device");
2510 return INVALID_OPERATION;
2511 }
2512 sp<DeviceDescriptor> devDesc =
2513 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2514 if (devDesc == 0) {
2515 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2516 return BAD_VALUE;
2517 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002518
François Gaffie53615e22015-03-19 09:24:12 +01002519 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002520 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002521 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002522 NULL, // updatedSamplingRate
2523 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002524 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002525 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002526 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002527 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002528 ALOGV("createAudioPatch() profile not supported for device %08x",
2529 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002530 return INVALID_OPERATION;
2531 }
2532 devices.add(devDesc);
2533 }
2534 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002535 return INVALID_OPERATION;
2536 }
Eric Laurent874c42872014-08-08 15:13:39 -07002537
Eric Laurent6a94d692014-05-20 11:18:06 -07002538 // TODO: reconfigure output format and channels here
2539 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002540 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002541 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002542 index = mAudioPatches.indexOfKey(*handle);
2543 if (index >= 0) {
2544 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2545 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2546 }
2547 patchDesc = mAudioPatches.valueAt(index);
2548 patchDesc->mUid = uid;
2549 ALOGV("createAudioPatch() success");
2550 } else {
2551 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2552 return INVALID_OPERATION;
2553 }
2554 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2555 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2556 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002557 // only one sink supported when connecting an input device to a mix
2558 if (patch->num_sinks > 1) {
2559 return INVALID_OPERATION;
2560 }
François Gaffie53615e22015-03-19 09:24:12 +01002561 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002562 if (inputDesc == NULL) {
2563 return BAD_VALUE;
2564 }
2565 if (patchDesc != 0) {
2566 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2567 return BAD_VALUE;
2568 }
2569 }
2570 sp<DeviceDescriptor> devDesc =
2571 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2572 if (devDesc == 0) {
2573 return BAD_VALUE;
2574 }
2575
François Gaffie53615e22015-03-19 09:24:12 +01002576 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002577 devDesc->mAddress,
2578 patch->sinks[0].sample_rate,
2579 NULL, /*updatedSampleRate*/
2580 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002581 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002582 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002583 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002584 // FIXME for the parameter type,
2585 // and the NONE
2586 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002587 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002588 return INVALID_OPERATION;
2589 }
2590 // TODO: reconfigure output format and channels here
2591 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002592 devDesc->type(), inputDesc->mIoHandle);
2593 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002594 index = mAudioPatches.indexOfKey(*handle);
2595 if (index >= 0) {
2596 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2597 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2598 }
2599 patchDesc = mAudioPatches.valueAt(index);
2600 patchDesc->mUid = uid;
2601 ALOGV("createAudioPatch() success");
2602 } else {
2603 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2604 return INVALID_OPERATION;
2605 }
2606 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2607 // device to device connection
2608 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002609 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002610 return BAD_VALUE;
2611 }
2612 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002613 sp<DeviceDescriptor> srcDeviceDesc =
2614 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002615 if (srcDeviceDesc == 0) {
2616 return BAD_VALUE;
2617 }
Eric Laurent874c42872014-08-08 15:13:39 -07002618
Eric Laurent6a94d692014-05-20 11:18:06 -07002619 //update source and sink with our own data as the data passed in the patch may
2620 // be incomplete.
2621 struct audio_patch newPatch = *patch;
2622 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002623
Eric Laurent874c42872014-08-08 15:13:39 -07002624 for (size_t i = 0; i < patch->num_sinks; i++) {
2625 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2626 ALOGV("createAudioPatch() source device but one sink is not a device");
2627 return INVALID_OPERATION;
2628 }
2629
2630 sp<DeviceDescriptor> sinkDeviceDesc =
2631 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2632 if (sinkDeviceDesc == 0) {
2633 return BAD_VALUE;
2634 }
2635 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2636
Eric Laurent3bcf8592015-04-03 12:13:24 -07002637 // create a software bridge in PatchPanel if:
2638 // - source and sink devices are on differnt HW modules OR
2639 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002640 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002641 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002642 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002643 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002644 return INVALID_OPERATION;
2645 }
Eric Laurent874c42872014-08-08 15:13:39 -07002646 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002647 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002648 // if the sink device is reachable via an opened output stream, request to go via
2649 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002650 audio_io_handle_t output = selectOutput(outputs,
2651 AUDIO_OUTPUT_FLAG_NONE,
2652 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002653 if (output != AUDIO_IO_HANDLE_NONE) {
2654 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2655 if (outputDesc->isDuplicated()) {
2656 return INVALID_OPERATION;
2657 }
2658 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002659 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002660 newPatch.num_sources = 2;
2661 }
Eric Laurent83b88082014-06-20 18:31:16 -07002662 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002663 }
2664 // TODO: check from routing capabilities in config file and other conflicting patches
2665
2666 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2667 if (index >= 0) {
2668 afPatchHandle = patchDesc->mAfPatchHandle;
2669 }
2670
2671 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2672 &afPatchHandle,
2673 0);
2674 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2675 status, afPatchHandle);
2676 if (status == NO_ERROR) {
2677 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002678 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002679 addAudioPatch(patchDesc->mHandle, patchDesc);
2680 } else {
2681 patchDesc->mPatch = newPatch;
2682 }
2683 patchDesc->mAfPatchHandle = afPatchHandle;
2684 *handle = patchDesc->mHandle;
2685 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002686 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002687 } else {
2688 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2689 status);
2690 return INVALID_OPERATION;
2691 }
2692 } else {
2693 return BAD_VALUE;
2694 }
2695 } else {
2696 return BAD_VALUE;
2697 }
2698 return NO_ERROR;
2699}
2700
2701status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2702 uid_t uid)
2703{
2704 ALOGV("releaseAudioPatch() patch %d", handle);
2705
2706 ssize_t index = mAudioPatches.indexOfKey(handle);
2707
2708 if (index < 0) {
2709 return BAD_VALUE;
2710 }
2711 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2712 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2713 mUidCached, patchDesc->mUid, uid);
2714 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2715 return INVALID_OPERATION;
2716 }
2717
2718 struct audio_patch *patch = &patchDesc->mPatch;
2719 patchDesc->mUid = mUidCached;
2720 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002721 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002722 if (outputDesc == NULL) {
2723 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2724 return BAD_VALUE;
2725 }
2726
Eric Laurentc75307b2015-03-17 15:29:32 -07002727 setOutputDevice(outputDesc,
2728 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002729 true,
2730 0,
2731 NULL);
2732 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2733 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002734 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002735 if (inputDesc == NULL) {
2736 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2737 return BAD_VALUE;
2738 }
2739 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002740 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002741 true,
2742 NULL);
2743 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002744 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2745 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2746 status, patchDesc->mAfPatchHandle);
2747 removeAudioPatch(patchDesc->mHandle);
2748 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002749 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 } else {
2751 return BAD_VALUE;
2752 }
2753 } else {
2754 return BAD_VALUE;
2755 }
2756 return NO_ERROR;
2757}
2758
2759status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2760 struct audio_patch *patches,
2761 unsigned int *generation)
2762{
François Gaffie53615e22015-03-19 09:24:12 +01002763 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002764 return BAD_VALUE;
2765 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002766 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002767 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002768}
2769
Eric Laurente1715a42014-05-20 11:30:42 -07002770status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002771{
Eric Laurente1715a42014-05-20 11:30:42 -07002772 ALOGV("setAudioPortConfig()");
2773
2774 if (config == NULL) {
2775 return BAD_VALUE;
2776 }
2777 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2778 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002779 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2780 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002781 }
2782
Eric Laurenta121f902014-06-03 13:32:54 -07002783 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002784 if (config->type == AUDIO_PORT_TYPE_MIX) {
2785 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002786 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002787 if (outputDesc == NULL) {
2788 return BAD_VALUE;
2789 }
Eric Laurent84c70242014-06-23 08:46:27 -07002790 ALOG_ASSERT(!outputDesc->isDuplicated(),
2791 "setAudioPortConfig() called on duplicated output %d",
2792 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002793 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002794 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002795 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002796 if (inputDesc == NULL) {
2797 return BAD_VALUE;
2798 }
Eric Laurenta121f902014-06-03 13:32:54 -07002799 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002800 } else {
2801 return BAD_VALUE;
2802 }
2803 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2804 sp<DeviceDescriptor> deviceDesc;
2805 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2806 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2807 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2808 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2809 } else {
2810 return BAD_VALUE;
2811 }
2812 if (deviceDesc == NULL) {
2813 return BAD_VALUE;
2814 }
Eric Laurenta121f902014-06-03 13:32:54 -07002815 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002816 } else {
2817 return BAD_VALUE;
2818 }
2819
Eric Laurenta121f902014-06-03 13:32:54 -07002820 struct audio_port_config backupConfig;
2821 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2822 if (status == NO_ERROR) {
2823 struct audio_port_config newConfig;
2824 audioPortConfig->toAudioPortConfig(&newConfig, config);
2825 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002826 }
Eric Laurenta121f902014-06-03 13:32:54 -07002827 if (status != NO_ERROR) {
2828 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002829 }
Eric Laurente1715a42014-05-20 11:30:42 -07002830
2831 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002832}
2833
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002834void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2835{
Eric Laurentd60560a2015-04-10 11:31:20 -07002836 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002837 clearAudioPatches(uid);
2838 clearSessionRoutes(uid);
2839}
2840
Eric Laurent6a94d692014-05-20 11:18:06 -07002841void AudioPolicyManager::clearAudioPatches(uid_t uid)
2842{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002843 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002844 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2845 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002846 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002847 }
2848 }
2849}
2850
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002851void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2852 audio_io_handle_t ouptutToSkip)
2853{
2854 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2855 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2856 for (size_t j = 0; j < mOutputs.size(); j++) {
2857 if (mOutputs.keyAt(j) == ouptutToSkip) {
2858 continue;
2859 }
2860 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2861 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2862 continue;
2863 }
2864 // If the default device for this strategy is on another output mix,
2865 // invalidate all tracks in this strategy to force re connection.
2866 // Otherwise select new device on the output mix.
2867 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002868 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002869 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2870 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2871 }
2872 }
2873 } else {
2874 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2875 setOutputDevice(outputDesc, newDevice, false);
2876 }
2877 }
2878}
2879
2880void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2881{
2882 // remove output routes associated with this uid
2883 SortedVector<routing_strategy> affectedStrategies;
2884 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2885 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2886 if (route->mUid == uid) {
2887 mOutputRoutes.removeItemsAt(i);
2888 if (route->mDeviceDescriptor != 0) {
2889 affectedStrategies.add(getStrategy(route->mStreamType));
2890 }
2891 }
2892 }
2893 // reroute outputs if necessary
2894 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2895 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2896 }
2897
2898 // remove input routes associated with this uid
2899 SortedVector<audio_source_t> affectedSources;
2900 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2901 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2902 if (route->mUid == uid) {
2903 mInputRoutes.removeItemsAt(i);
2904 if (route->mDeviceDescriptor != 0) {
2905 affectedSources.add(route->mSource);
2906 }
2907 }
2908 }
2909 // reroute inputs if necessary
2910 SortedVector<audio_io_handle_t> inputsToClose;
2911 for (size_t i = 0; i < mInputs.size(); i++) {
2912 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002913 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002914 inputsToClose.add(inputDesc->mIoHandle);
2915 }
2916 }
2917 for (size_t i = 0; i < inputsToClose.size(); i++) {
2918 closeInput(inputsToClose[i]);
2919 }
2920}
2921
Eric Laurentd60560a2015-04-10 11:31:20 -07002922void AudioPolicyManager::clearAudioSources(uid_t uid)
2923{
2924 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2925 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2926 if (sourceDesc->mUid == uid) {
2927 stopAudioSource(mAudioSources.keyAt(i));
2928 }
2929 }
2930}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002931
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002932status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2933 audio_io_handle_t *ioHandle,
2934 audio_devices_t *device)
2935{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002936 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2937 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002938 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002939
François Gaffiedf372692015-03-19 10:43:27 +01002940 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002941}
2942
Eric Laurentd60560a2015-04-10 11:31:20 -07002943status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2944 const audio_attributes_t *attributes,
2945 audio_io_handle_t *handle,
2946 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002947{
Eric Laurentd60560a2015-04-10 11:31:20 -07002948 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2949 if (source == NULL || attributes == NULL || handle == NULL) {
2950 return BAD_VALUE;
2951 }
2952
2953 *handle = AUDIO_IO_HANDLE_NONE;
2954
2955 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2956 source->type != AUDIO_PORT_TYPE_DEVICE) {
2957 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2958 return INVALID_OPERATION;
2959 }
2960
2961 sp<DeviceDescriptor> srcDeviceDesc =
2962 mAvailableInputDevices.getDevice(source->ext.device.type,
2963 String8(source->ext.device.address));
2964 if (srcDeviceDesc == 0) {
2965 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2966 return BAD_VALUE;
2967 }
2968 sp<AudioSourceDescriptor> sourceDesc =
2969 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2970
2971 struct audio_patch dummyPatch;
2972 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2973 sourceDesc->mPatchDesc = patchDesc;
2974
2975 status_t status = connectAudioSource(sourceDesc);
2976 if (status == NO_ERROR) {
2977 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2978 *handle = sourceDesc->getHandle();
2979 }
2980 return status;
2981}
2982
2983status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2984{
2985 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2986
2987 // make sure we only have one patch per source.
2988 disconnectAudioSource(sourceDesc);
2989
2990 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002991 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002992 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2993
2994 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2995 sp<DeviceDescriptor> sinkDeviceDesc =
2996 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2997
2998 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2999 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3000
3001 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3002 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003003 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003004 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3005 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3006 // create patch between src device and output device
3007 // create Hwoutput and add to mHwOutputs
3008 } else {
3009 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3010 audio_io_handle_t output =
3011 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3012 if (output == AUDIO_IO_HANDLE_NONE) {
3013 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3014 return INVALID_OPERATION;
3015 }
3016 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3017 if (outputDesc->isDuplicated()) {
3018 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3019 return INVALID_OPERATION;
3020 }
3021 // create a special patch with no sink and two sources:
3022 // - the second source indicates to PatchPanel through which output mix this patch should
3023 // be connected as well as the stream type for volume control
3024 // - the sink is defined by whatever output device is currently selected for the output
3025 // though which this patch is routed.
3026 patch->num_sinks = 0;
3027 patch->num_sources = 2;
3028 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3029 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3030 patch->sources[1].ext.mix.usecase.stream = stream;
3031 status_t status = mpClientInterface->createAudioPatch(patch,
3032 &afPatchHandle,
3033 0);
3034 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3035 status, afPatchHandle);
3036 if (status != NO_ERROR) {
3037 ALOGW("%s patch panel could not connect device patch, error %d",
3038 __FUNCTION__, status);
3039 return INVALID_OPERATION;
3040 }
3041 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003042 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003043
3044 if (status != NO_ERROR) {
3045 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3046 return status;
3047 }
3048 sourceDesc->mSwOutput = outputDesc;
3049 if (delayMs != 0) {
3050 usleep(delayMs * 1000);
3051 }
3052 }
3053
3054 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3055 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3056
3057 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003058}
3059
Eric Laurent493404d2015-04-21 15:07:36 -07003060status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003061{
Eric Laurentd60560a2015-04-10 11:31:20 -07003062 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3063 ALOGV("%s handle %d", __FUNCTION__, handle);
3064 if (sourceDesc == 0) {
3065 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3066 return BAD_VALUE;
3067 }
3068 status_t status = disconnectAudioSource(sourceDesc);
3069
3070 mAudioSources.removeItem(handle);
3071 return status;
3072}
3073
Andy Hung2ddee192015-12-18 17:34:44 -08003074status_t AudioPolicyManager::setMasterMono(bool mono)
3075{
3076 if (mMasterMono == mono) {
3077 return NO_ERROR;
3078 }
3079 mMasterMono = mono;
3080 // if enabling mono we close all offloaded devices, which will invalidate the
3081 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3082 // for recreating the new AudioTrack as non-offloaded PCM.
3083 //
3084 // If disabling mono, we leave all tracks as is: we don't know which clients
3085 // and tracks are able to be recreated as offloaded. The next "song" should
3086 // play back offloaded.
3087 if (mMasterMono) {
3088 Vector<audio_io_handle_t> offloaded;
3089 for (size_t i = 0; i < mOutputs.size(); ++i) {
3090 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3091 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3092 offloaded.push(desc->mIoHandle);
3093 }
3094 }
3095 for (size_t i = 0; i < offloaded.size(); ++i) {
3096 closeOutput(offloaded[i]);
3097 }
3098 }
3099 // update master mono for all remaining outputs
3100 for (size_t i = 0; i < mOutputs.size(); ++i) {
3101 updateMono(mOutputs.keyAt(i));
3102 }
3103 return NO_ERROR;
3104}
3105
3106status_t AudioPolicyManager::getMasterMono(bool *mono)
3107{
3108 *mono = mMasterMono;
3109 return NO_ERROR;
3110}
3111
Eric Laurentd60560a2015-04-10 11:31:20 -07003112status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3113{
3114 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3115
3116 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3117 if (patchDesc == 0) {
3118 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3119 sourceDesc->mPatchDesc->mHandle);
3120 return BAD_VALUE;
3121 }
3122 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3123
Eric Laurent28d09f02016-03-08 10:43:05 -08003124 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003125 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3126 if (swOutputDesc != 0) {
3127 stopSource(swOutputDesc, stream, false);
3128 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3129 } else {
3130 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3131 if (hwOutputDesc != 0) {
3132 // release patch between src device and output device
3133 // close Hwoutput and remove from mHwOutputs
3134 } else {
3135 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3136 }
3137 }
3138 return NO_ERROR;
3139}
3140
3141sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3142 audio_io_handle_t output, routing_strategy strategy)
3143{
3144 sp<AudioSourceDescriptor> source;
3145 for (size_t i = 0; i < mAudioSources.size(); i++) {
3146 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3147 routing_strategy sourceStrategy =
3148 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3149 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3150 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3151 source = sourceDesc;
3152 break;
3153 }
3154 }
3155 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003156}
3157
Eric Laurente552edb2014-03-10 17:42:56 -07003158// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003159// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003160// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003161uint32_t AudioPolicyManager::nextAudioPortGeneration()
3162{
3163 return android_atomic_inc(&mAudioPortGeneration);
3164}
3165
Eric Laurente0720872014-03-11 09:30:41 -07003166AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003167 :
3168#ifdef AUDIO_POLICY_TEST
3169 Thread(false),
3170#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003171 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003172 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003173 mAudioPortGeneration(1),
3174 mBeaconMuteRefCount(0),
3175 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003176 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003177 mTtsOutputAvailable(false),
3178 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003179{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003180 mUidCached = getuid();
3181 mpClientInterface = clientInterface;
3182
3183 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3184 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3185 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3186 bool speakerDrcEnabled = false;
3187
3188#ifdef USE_XML_AUDIO_POLICY_CONF
3189 mVolumeCurves = new VolumeCurvesCollection();
3190 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3191 mDefaultOutputDevice, speakerDrcEnabled,
3192 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3193 PolicySerializer serializer;
3194 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3195#else
3196 mVolumeCurves = new StreamDescriptorCollection();
3197 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3198 mDefaultOutputDevice, speakerDrcEnabled);
3199 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3200 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3201#endif
3202 ALOGE("could not load audio policy configuration file, setting defaults");
3203 config.setDefault();
3204 }
3205 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3206 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3207
3208 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003209 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3210 if (!engineInstance) {
3211 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3212 return;
3213 }
3214 // Retrieve the Policy Manager Interface
3215 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3216 if (mEngine == NULL) {
3217 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3218 return;
3219 }
3220 mEngine->setObserver(this);
3221 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003222 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003223 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3224
Eric Laurent3a4311c2014-03-17 12:00:47 -07003225 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003226 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003227 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3228 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003229 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003230 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003231 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003232 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003233 continue;
3234 }
3235 // open all output streams needed to access attached devices
3236 // except for direct output streams that are only opened when they are actually
3237 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003238 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003239 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3240 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003241 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003242
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003243 if (!outProfile->hasSupportedDevices()) {
3244 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003245 continue;
3246 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003247 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003248 mTtsOutputAvailable = true;
3249 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003250
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003251 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003252 continue;
3253 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003254 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003255 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3256 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003257 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003259 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003260 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003261 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003262 if ((profileType & outputDeviceTypes) == 0) {
3263 continue;
3264 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003265 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3266 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003267 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3268 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3269 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3270 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003271
3272 outputDesc->mDevice = profileType;
3273 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3274 config.sample_rate = outputDesc->mSamplingRate;
3275 config.channel_mask = outputDesc->mChannelMask;
3276 config.format = outputDesc->mFormat;
3277 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003278 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003279 &output,
3280 &config,
3281 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003282 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003283 &outputDesc->mLatency,
3284 outputDesc->mFlags);
3285
3286 if (status != NO_ERROR) {
3287 ALOGW("Cannot open output stream for device %08x on hw module %s",
3288 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003289 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003290 } else {
3291 outputDesc->mSamplingRate = config.sample_rate;
3292 outputDesc->mChannelMask = config.channel_mask;
3293 outputDesc->mFormat = config.format;
3294
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003295 for (size_t k = 0; k < supportedDevices.size(); k++) {
3296 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003297 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003298 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3299 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003300 }
3301 }
3302 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003303 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003304 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003305 }
3306 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003307 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003308 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003309 true,
3310 0,
3311 NULL,
3312 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003313 }
Eric Laurente552edb2014-03-10 17:42:56 -07003314 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003315 // open input streams needed to access attached devices to validate
3316 // mAvailableInputDevices list
3317 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3318 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003319 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003320
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003321 if (!inProfile->hasSupportedDevices()) {
3322 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003323 continue;
3324 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003325 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003326 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003327 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3328
Eric Laurentd78f1532014-09-16 16:38:20 -07003329 if ((profileType & inputDeviceTypes) == 0) {
3330 continue;
3331 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003332 sp<AudioInputDescriptor> inputDesc =
3333 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003334
Eric Laurentd78f1532014-09-16 16:38:20 -07003335 inputDesc->mDevice = profileType;
3336
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003337 // find the address
3338 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3339 // the inputs vector must be of size 1, but we don't want to crash here
3340 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3341 : String8("");
3342 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3343 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3344
Eric Laurentd78f1532014-09-16 16:38:20 -07003345 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3346 config.sample_rate = inputDesc->mSamplingRate;
3347 config.channel_mask = inputDesc->mChannelMask;
3348 config.format = inputDesc->mFormat;
3349 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003350 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003351 &input,
3352 &config,
3353 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003354 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003355 AUDIO_SOURCE_MIC,
3356 AUDIO_INPUT_FLAG_NONE);
3357
3358 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003359 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3360 for (size_t k = 0; k < supportedDevices.size(); k++) {
3361 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003362 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003363 if (index >= 0) {
3364 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3365 if (!devDesc->isAttached()) {
3366 devDesc->attach(mHwModules[i]);
3367 devDesc->importAudioPort(inProfile);
3368 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003369 }
3370 }
3371 mpClientInterface->closeInput(input);
3372 } else {
3373 ALOGW("Cannot open input stream for device %08x on hw module %s",
3374 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003375 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003376 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003377 }
3378 }
3379 // make sure all attached devices have been allocated a unique ID
3380 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003381 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003382 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003383 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3384 continue;
3385 }
François Gaffie2110e042015-03-24 08:41:51 +01003386 // The device is now validated and can be appended to the available devices of the engine
3387 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3388 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003389 i++;
3390 }
3391 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003392 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003393 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003394 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3395 continue;
3396 }
François Gaffie2110e042015-03-24 08:41:51 +01003397 // The device is now validated and can be appended to the available devices of the engine
3398 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3399 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003400 i++;
3401 }
3402 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003403 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003404 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003405 }
Eric Laurente552edb2014-03-10 17:42:56 -07003406
3407 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3408
3409 updateDevicesAndOutputs();
3410
3411#ifdef AUDIO_POLICY_TEST
3412 if (mPrimaryOutput != 0) {
3413 AudioParameter outputCmd = AudioParameter();
3414 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003415 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003416
3417 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3418 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003419 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3420 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003421 mTestLatencyMs = 0;
3422 mCurOutput = 0;
3423 mDirectOutput = false;
3424 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3425 mTestOutputs[i] = 0;
3426 }
3427
3428 const size_t SIZE = 256;
3429 char buffer[SIZE];
3430 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3431 run(buffer, ANDROID_PRIORITY_AUDIO);
3432 }
3433#endif //AUDIO_POLICY_TEST
3434}
3435
Eric Laurente0720872014-03-11 09:30:41 -07003436AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003437{
3438#ifdef AUDIO_POLICY_TEST
3439 exit();
3440#endif //AUDIO_POLICY_TEST
3441 for (size_t i = 0; i < mOutputs.size(); i++) {
3442 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003443 }
3444 for (size_t i = 0; i < mInputs.size(); i++) {
3445 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003446 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003447 mAvailableOutputDevices.clear();
3448 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003449 mOutputs.clear();
3450 mInputs.clear();
3451 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003452}
3453
Eric Laurente0720872014-03-11 09:30:41 -07003454status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003455{
Eric Laurent87ffa392015-05-22 10:32:38 -07003456 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003457}
3458
3459#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003460bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003461{
3462 ALOGV("entering threadLoop()");
3463 while (!exitPending())
3464 {
3465 String8 command;
3466 int valueInt;
3467 String8 value;
3468
3469 Mutex::Autolock _l(mLock);
3470 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3471
3472 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3473 AudioParameter param = AudioParameter(command);
3474
3475 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3476 valueInt != 0) {
3477 ALOGV("Test command %s received", command.string());
3478 String8 target;
3479 if (param.get(String8("target"), target) != NO_ERROR) {
3480 target = "Manager";
3481 }
3482 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3483 param.remove(String8("test_cmd_policy_output"));
3484 mCurOutput = valueInt;
3485 }
3486 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3487 param.remove(String8("test_cmd_policy_direct"));
3488 if (value == "false") {
3489 mDirectOutput = false;
3490 } else if (value == "true") {
3491 mDirectOutput = true;
3492 }
3493 }
3494 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3495 param.remove(String8("test_cmd_policy_input"));
3496 mTestInput = valueInt;
3497 }
3498
3499 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3500 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003501 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003502 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003503 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003504 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003505 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003506 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003507 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003508 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003509 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003510 if (target == "Manager") {
3511 mTestFormat = format;
3512 } else if (mTestOutputs[mCurOutput] != 0) {
3513 AudioParameter outputParam = AudioParameter();
3514 outputParam.addInt(String8("format"), format);
3515 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3516 }
3517 }
3518 }
3519 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3520 param.remove(String8("test_cmd_policy_channels"));
3521 int channels = 0;
3522
3523 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003524 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003525 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003526 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003527 }
3528 if (channels != 0) {
3529 if (target == "Manager") {
3530 mTestChannels = channels;
3531 } else if (mTestOutputs[mCurOutput] != 0) {
3532 AudioParameter outputParam = AudioParameter();
3533 outputParam.addInt(String8("channels"), channels);
3534 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3535 }
3536 }
3537 }
3538 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3539 param.remove(String8("test_cmd_policy_sampleRate"));
3540 if (valueInt >= 0 && valueInt <= 96000) {
3541 int samplingRate = valueInt;
3542 if (target == "Manager") {
3543 mTestSamplingRate = samplingRate;
3544 } else if (mTestOutputs[mCurOutput] != 0) {
3545 AudioParameter outputParam = AudioParameter();
3546 outputParam.addInt(String8("sampling_rate"), samplingRate);
3547 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3548 }
3549 }
3550 }
3551
3552 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3553 param.remove(String8("test_cmd_policy_reopen"));
3554
Eric Laurentc75307b2015-03-17 15:29:32 -07003555 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003556
Eric Laurentc75307b2015-03-17 15:29:32 -07003557 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003558
Eric Laurentc75307b2015-03-17 15:29:32 -07003559 removeOutput(mPrimaryOutput->mIoHandle);
3560 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3561 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003562 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003563 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3564 config.sample_rate = outputDesc->mSamplingRate;
3565 config.channel_mask = outputDesc->mChannelMask;
3566 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003567 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003568 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003569 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003570 &config,
3571 &outputDesc->mDevice,
3572 String8(""),
3573 &outputDesc->mLatency,
3574 outputDesc->mFlags);
3575 if (status != NO_ERROR) {
3576 ALOGE("Failed to reopen hardware output stream, "
3577 "samplingRate: %d, format %d, channels %d",
3578 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003579 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003580 outputDesc->mSamplingRate = config.sample_rate;
3581 outputDesc->mChannelMask = config.channel_mask;
3582 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003583 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003584 AudioParameter outputCmd = AudioParameter();
3585 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003586 mpClientInterface->setParameters(handle, outputCmd.toString());
3587 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003588 }
3589 }
3590
3591
3592 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3593 }
3594 }
3595 return false;
3596}
3597
Eric Laurente0720872014-03-11 09:30:41 -07003598void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003599{
3600 {
3601 AutoMutex _l(mLock);
3602 requestExit();
3603 mWaitWorkCV.signal();
3604 }
3605 requestExitAndWait();
3606}
3607
Eric Laurente0720872014-03-11 09:30:41 -07003608int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003609{
3610 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3611 if (output == mTestOutputs[i]) return i;
3612 }
3613 return 0;
3614}
3615#endif //AUDIO_POLICY_TEST
3616
3617// ---
3618
Eric Laurentc75307b2015-03-17 15:29:32 -07003619void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003620{
François Gaffie98cc1912015-03-18 17:52:40 +01003621 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003622 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003623 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003624 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003625}
3626
François Gaffie53615e22015-03-19 09:24:12 +01003627void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3628{
3629 mOutputs.removeItem(output);
3630}
3631
Eric Laurent1f2f2232014-06-02 12:01:23 -07003632void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003633{
François Gaffie98cc1912015-03-18 17:52:40 +01003634 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003635 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003636 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003637}
Eric Laurente552edb2014-03-10 17:42:56 -07003638
Eric Laurentc75307b2015-03-17 15:29:32 -07003639void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003640 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003641 const String8 address /*in*/,
3642 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003643 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003644 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003645 if (devDesc != 0) {
3646 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3647 desc->mIoHandle, address.string());
3648 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003649 }
3650}
3651
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003652status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003653 audio_policy_dev_state_t state,
3654 SortedVector<audio_io_handle_t>& outputs,
3655 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003656{
François Gaffie53615e22015-03-19 09:24:12 +01003657 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003658 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003659
3660 if (audio_device_is_digital(device)) {
3661 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003662 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003663 }
Eric Laurente552edb2014-03-10 17:42:56 -07003664
Eric Laurent3b73df72014-03-11 09:06:29 -07003665 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003666 // first list already open outputs that can be routed to this device
3667 for (size_t i = 0; i < mOutputs.size(); i++) {
3668 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003669 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003670 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003671 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3672 outputs.add(mOutputs.keyAt(i));
3673 } else {
3674 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003675 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003676 }
Eric Laurente552edb2014-03-10 17:42:56 -07003677 }
3678 }
3679 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003680 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003681 for (size_t i = 0; i < mHwModules.size(); i++)
3682 {
3683 if (mHwModules[i]->mHandle == 0) {
3684 continue;
3685 }
3686 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3687 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003688 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003689 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003690 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003691 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003692 profiles.add(profile);
3693 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3694 }
Eric Laurente552edb2014-03-10 17:42:56 -07003695 }
3696 }
3697 }
3698
Eric Laurent7b279bb2015-12-14 10:18:23 -08003699 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003700
Eric Laurente552edb2014-03-10 17:42:56 -07003701 if (profiles.isEmpty() && outputs.isEmpty()) {
3702 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3703 return BAD_VALUE;
3704 }
3705
3706 // open outputs for matching profiles if needed. Direct outputs are also opened to
3707 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3708 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003709 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003710
3711 // nothing to do if one output is already opened for this profile
3712 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003713 for (j = 0; j < outputs.size(); j++) {
3714 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003715 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003716 // matching profile: save the sample rates, format and channel masks supported
3717 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003718 if (audio_device_is_digital(device)) {
3719 devDesc->importAudioPort(profile);
3720 }
Eric Laurente552edb2014-03-10 17:42:56 -07003721 break;
3722 }
3723 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003724 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003725 continue;
3726 }
3727
Eric Laurent83b88082014-06-20 18:31:16 -07003728 ALOGV("opening output for device %08x with params %s profile %p",
3729 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003730 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003731 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003732 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3733 config.sample_rate = desc->mSamplingRate;
3734 config.channel_mask = desc->mChannelMask;
3735 config.format = desc->mFormat;
3736 config.offload_info.sample_rate = desc->mSamplingRate;
3737 config.offload_info.channel_mask = desc->mChannelMask;
3738 config.offload_info.format = desc->mFormat;
3739 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003740 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003741 &output,
3742 &config,
3743 &desc->mDevice,
3744 address,
3745 &desc->mLatency,
3746 desc->mFlags);
3747 if (status == NO_ERROR) {
3748 desc->mSamplingRate = config.sample_rate;
3749 desc->mChannelMask = config.channel_mask;
3750 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003751
Eric Laurentd4692962014-05-05 18:13:44 -07003752 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003753 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003754 char *param = audio_device_address_to_parameter(device, address);
3755 mpClientInterface->setParameters(output, String8(param));
3756 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003757 }
Phil Burk00eeb322016-03-31 12:41:00 -07003758 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003759 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003760 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003761 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003762 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003763 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003764 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003765 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003766 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003767 config.offload_info.sample_rate = config.sample_rate;
3768 config.offload_info.channel_mask = config.channel_mask;
3769 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003770 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003771 &output,
3772 &config,
3773 &desc->mDevice,
3774 address,
3775 &desc->mLatency,
3776 desc->mFlags);
3777 if (status == NO_ERROR) {
3778 desc->mSamplingRate = config.sample_rate;
3779 desc->mChannelMask = config.channel_mask;
3780 desc->mFormat = config.format;
3781 } else {
3782 output = AUDIO_IO_HANDLE_NONE;
3783 }
Eric Laurentd4692962014-05-05 18:13:44 -07003784 }
3785
Eric Laurentcf2c0212014-07-25 16:20:43 -07003786 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003787 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003788 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003789 sp<AudioPolicyMix> policyMix;
3790 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003791 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3792 address.string());
3793 }
François Gaffie036e1e92015-03-19 10:16:24 +01003794 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003795 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003796
Eric Laurent87ffa392015-05-22 10:32:38 -07003797 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3798 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003799 // no duplicated output for direct outputs and
3800 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003801 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003802
Eric Laurentd4692962014-05-05 18:13:44 -07003803 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003804 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003805
Eric Laurentd4692962014-05-05 18:13:44 -07003806 //TODO: configure audio effect output stage here
3807
3808 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003809 duplicatedOutput =
3810 mpClientInterface->openDuplicateOutput(output,
3811 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003812 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003813 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003814 sp<SwAudioOutputDescriptor> dupOutputDesc =
3815 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3816 dupOutputDesc->mOutput1 = mPrimaryOutput;
3817 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003818 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3819 dupOutputDesc->mFormat = desc->mFormat;
3820 dupOutputDesc->mChannelMask = desc->mChannelMask;
3821 dupOutputDesc->mLatency = desc->mLatency;
3822 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003823 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003824 } else {
3825 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003826 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003827 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003828 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003829 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003830 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003831 }
Eric Laurente552edb2014-03-10 17:42:56 -07003832 }
3833 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003834 } else {
3835 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003836 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003837 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003838 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003839 profiles.removeAt(profile_index);
3840 profile_index--;
3841 } else {
3842 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003843 // Load digital format info only for digital devices
3844 if (audio_device_is_digital(device)) {
3845 devDesc->importAudioPort(profile);
3846 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003847
François Gaffie53615e22015-03-19 09:24:12 +01003848 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003849 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3850 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003851 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003852 NULL/*patch handle*/, address.string());
3853 }
Eric Laurente552edb2014-03-10 17:42:56 -07003854 ALOGV("checkOutputsForDevice(): adding output %d", output);
3855 }
3856 }
3857
3858 if (profiles.isEmpty()) {
3859 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3860 return BAD_VALUE;
3861 }
Eric Laurentd4692962014-05-05 18:13:44 -07003862 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003863 // check if one opened output is not needed any more after disconnecting one device
3864 for (size_t i = 0; i < mOutputs.size(); i++) {
3865 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003866 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003867 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003868 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003869 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003870 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003871 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003872 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3873 mOutputs.keyAt(i));
3874 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003875 }
Eric Laurente552edb2014-03-10 17:42:56 -07003876 }
3877 }
Eric Laurentd4692962014-05-05 18:13:44 -07003878 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003879 for (size_t i = 0; i < mHwModules.size(); i++)
3880 {
3881 if (mHwModules[i]->mHandle == 0) {
3882 continue;
3883 }
3884 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3885 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003886 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003887 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003888 ALOGV("checkOutputsForDevice(): "
3889 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003890 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003891 }
3892 }
3893 }
3894 }
3895 return NO_ERROR;
3896}
3897
Paul McLean9080a4c2015-06-18 08:24:02 -07003898status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003899 audio_policy_dev_state_t state,
3900 SortedVector<audio_io_handle_t>& inputs,
3901 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003902{
Paul McLean9080a4c2015-06-18 08:24:02 -07003903 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003904 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003905
3906 if (audio_device_is_digital(device)) {
3907 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003908 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003909 }
3910
Eric Laurentd4692962014-05-05 18:13:44 -07003911 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3912 // first list already open inputs that can be routed to this device
3913 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3914 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003915 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003916 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3917 inputs.add(mInputs.keyAt(input_index));
3918 }
3919 }
3920
3921 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003922 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003923 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3924 {
3925 if (mHwModules[module_idx]->mHandle == 0) {
3926 continue;
3927 }
3928 for (size_t profile_index = 0;
3929 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3930 profile_index++)
3931 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003932 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3933
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003934 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003935 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003936 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003937 profiles.add(profile);
3938 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3939 profile_index, module_idx);
3940 }
Eric Laurentd4692962014-05-05 18:13:44 -07003941 }
3942 }
3943 }
3944
3945 if (profiles.isEmpty() && inputs.isEmpty()) {
3946 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3947 return BAD_VALUE;
3948 }
3949
3950 // open inputs for matching profiles if needed. Direct inputs are also opened to
3951 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3952 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3953
Eric Laurent1c333e22014-05-20 10:48:17 -07003954 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003955 // nothing to do if one input is already opened for this profile
3956 size_t input_index;
3957 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3958 desc = mInputs.valueAt(input_index);
3959 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003960 if (audio_device_is_digital(device)) {
3961 devDesc->importAudioPort(profile);
3962 }
Eric Laurentd4692962014-05-05 18:13:44 -07003963 break;
3964 }
3965 }
3966 if (input_index != mInputs.size()) {
3967 continue;
3968 }
3969
3970 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3971 desc = new AudioInputDescriptor(profile);
3972 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003973 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3974 config.sample_rate = desc->mSamplingRate;
3975 config.channel_mask = desc->mChannelMask;
3976 config.format = desc->mFormat;
3977 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003978 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003979 &input,
3980 &config,
3981 &desc->mDevice,
3982 address,
3983 AUDIO_SOURCE_MIC,
3984 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003985
Eric Laurentcf2c0212014-07-25 16:20:43 -07003986 if (status == NO_ERROR) {
3987 desc->mSamplingRate = config.sample_rate;
3988 desc->mChannelMask = config.channel_mask;
3989 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003990
Eric Laurentd4692962014-05-05 18:13:44 -07003991 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003992 char *param = audio_device_address_to_parameter(device, address);
3993 mpClientInterface->setParameters(input, String8(param));
3994 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003995 }
Phil Burk00eeb322016-03-31 12:41:00 -07003996 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003997 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003998 ALOGW("checkInputsForDevice() direct input missing param");
3999 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004000 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004001 }
4002
4003 if (input != 0) {
4004 addInput(input, desc);
4005 }
4006 } // endif input != 0
4007
Eric Laurentcf2c0212014-07-25 16:20:43 -07004008 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004009 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004010 profiles.removeAt(profile_index);
4011 profile_index--;
4012 } else {
4013 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004014 if (audio_device_is_digital(device)) {
4015 devDesc->importAudioPort(profile);
4016 }
Eric Laurentd4692962014-05-05 18:13:44 -07004017 ALOGV("checkInputsForDevice(): adding input %d", input);
4018 }
4019 } // end scan profiles
4020
4021 if (profiles.isEmpty()) {
4022 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4023 return BAD_VALUE;
4024 }
4025 } else {
4026 // Disconnect
4027 // check if one opened input is not needed any more after disconnecting one device
4028 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4029 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004030 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004031 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4032 mInputs.keyAt(input_index));
4033 inputs.add(mInputs.keyAt(input_index));
4034 }
4035 }
4036 // Clear any profiles associated with the disconnected device.
4037 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4038 if (mHwModules[module_index]->mHandle == 0) {
4039 continue;
4040 }
4041 for (size_t profile_index = 0;
4042 profile_index < mHwModules[module_index]->mInputProfiles.size();
4043 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004044 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004045 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004046 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004047 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004048 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004049 }
4050 }
4051 }
4052 } // end disconnect
4053
4054 return NO_ERROR;
4055}
4056
4057
Eric Laurente0720872014-03-11 09:30:41 -07004058void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004059{
4060 ALOGV("closeOutput(%d)", output);
4061
Eric Laurentc75307b2015-03-17 15:29:32 -07004062 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004063 if (outputDesc == NULL) {
4064 ALOGW("closeOutput() unknown output %d", output);
4065 return;
4066 }
François Gaffie036e1e92015-03-19 10:16:24 +01004067 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004068
Eric Laurente552edb2014-03-10 17:42:56 -07004069 // look for duplicated outputs connected to the output being removed.
4070 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004071 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004072 if (dupOutputDesc->isDuplicated() &&
4073 (dupOutputDesc->mOutput1 == outputDesc ||
4074 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004075 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004076 if (dupOutputDesc->mOutput1 == outputDesc) {
4077 outputDesc2 = dupOutputDesc->mOutput2;
4078 } else {
4079 outputDesc2 = dupOutputDesc->mOutput1;
4080 }
4081 // As all active tracks on duplicated output will be deleted,
4082 // and as they were also referenced on the other output, the reference
4083 // count for their stream type must be adjusted accordingly on
4084 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004085 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004086 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004087 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004088 }
4089 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4090 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4091
4092 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004093 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004094 }
4095 }
4096
Eric Laurent05b90f82014-08-27 15:32:29 -07004097 nextAudioPortGeneration();
4098
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004099 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004100 if (index >= 0) {
4101 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004102 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004103 mAudioPatches.removeItemsAt(index);
4104 mpClientInterface->onAudioPatchListUpdate();
4105 }
4106
Eric Laurente552edb2014-03-10 17:42:56 -07004107 AudioParameter param;
4108 param.add(String8("closing"), String8("true"));
4109 mpClientInterface->setParameters(output, param.toString());
4110
4111 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004112 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004113 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004114}
4115
4116void AudioPolicyManager::closeInput(audio_io_handle_t input)
4117{
4118 ALOGV("closeInput(%d)", input);
4119
4120 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4121 if (inputDesc == NULL) {
4122 ALOGW("closeInput() unknown input %d", input);
4123 return;
4124 }
4125
Eric Laurent6a94d692014-05-20 11:18:06 -07004126 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004127
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004128 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004129 if (index >= 0) {
4130 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004131 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004132 mAudioPatches.removeItemsAt(index);
4133 mpClientInterface->onAudioPatchListUpdate();
4134 }
4135
4136 mpClientInterface->closeInput(input);
4137 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004138}
4139
Eric Laurentc75307b2015-03-17 15:29:32 -07004140SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4141 audio_devices_t device,
4142 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004143{
4144 SortedVector<audio_io_handle_t> outputs;
4145
4146 ALOGVV("getOutputsForDevice() device %04x", device);
4147 for (size_t i = 0; i < openOutputs.size(); i++) {
4148 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004149 i, openOutputs.valueAt(i)->isDuplicated(),
4150 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004151 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4152 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4153 outputs.add(openOutputs.keyAt(i));
4154 }
4155 }
4156 return outputs;
4157}
4158
Eric Laurente0720872014-03-11 09:30:41 -07004159bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004160 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004161{
4162 if (outputs1.size() != outputs2.size()) {
4163 return false;
4164 }
4165 for (size_t i = 0; i < outputs1.size(); i++) {
4166 if (outputs1[i] != outputs2[i]) {
4167 return false;
4168 }
4169 }
4170 return true;
4171}
4172
Eric Laurente0720872014-03-11 09:30:41 -07004173void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004174{
4175 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4176 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4177 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4178 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4179
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004180 // also take into account external policy-related changes: add all outputs which are
4181 // associated with policies in the "before" and "after" output vectors
4182 ALOGVV("checkOutputForStrategy(): policy related outputs");
4183 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004184 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004185 if (desc != 0 && desc->mPolicyMix != NULL) {
4186 srcOutputs.add(desc->mIoHandle);
4187 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4188 }
4189 }
4190 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004191 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004192 if (desc != 0 && desc->mPolicyMix != NULL) {
4193 dstOutputs.add(desc->mIoHandle);
4194 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4195 }
4196 }
4197
Eric Laurente552edb2014-03-10 17:42:56 -07004198 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4199 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4200 strategy, srcOutputs[0], dstOutputs[0]);
4201 // mute strategy while moving tracks from one output to another
4202 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004203 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004204 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004205 setStrategyMute(strategy, true, desc);
4206 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004207 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004208 sp<AudioSourceDescriptor> source =
4209 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4210 if (source != 0){
4211 connectAudioSource(source);
4212 }
Eric Laurente552edb2014-03-10 17:42:56 -07004213 }
4214
4215 // Move effects associated to this strategy from previous output to new output
4216 if (strategy == STRATEGY_MEDIA) {
4217 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4218 SortedVector<audio_io_handle_t> moved;
4219 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004220 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4221 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4222 effectDesc->mIo != fxOutput) {
4223 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004224 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4225 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004226 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004227 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004228 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004229 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004230 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004231 }
4232 }
4233 }
4234 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004235 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004236 if (getStrategy((audio_stream_type_t)i) == strategy) {
4237 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004238 }
4239 }
4240 }
4241}
4242
Eric Laurente0720872014-03-11 09:30:41 -07004243void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004244{
François Gaffie2110e042015-03-24 08:41:51 +01004245 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004246 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004247 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004248 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004249 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004250 checkOutputForStrategy(STRATEGY_SONIFICATION);
4251 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004252 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004253 checkOutputForStrategy(STRATEGY_MEDIA);
4254 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004255 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004256}
4257
Eric Laurente0720872014-03-11 09:30:41 -07004258void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004259{
François Gaffie53615e22015-03-19 09:24:12 +01004260 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004261 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004262 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004263 return;
4264 }
4265
Eric Laurent3a4311c2014-03-17 12:00:47 -07004266 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004267 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4268 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4269 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004270 // suspend A2DP output if:
4271 // (NOT already suspended) &&
4272 // ((SCO device is connected &&
4273 // (forced usage for communication || for record is SCO))) ||
4274 // (phone state is ringing || in call)
4275 //
4276 // restore A2DP output if:
4277 // (Already suspended) &&
4278 // ((SCO device is NOT connected ||
4279 // (forced usage NOT for communication && NOT for record is SCO))) &&
4280 // (phone state is NOT ringing && NOT in call)
4281 //
4282 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004283 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004284 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4285 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4286 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4287 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004288
4289 mpClientInterface->restoreOutput(a2dpOutput);
4290 mA2dpSuspended = false;
4291 }
4292 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004293 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004294 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4295 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4296 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4297 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004298
4299 mpClientInterface->suspendOutput(a2dpOutput);
4300 mA2dpSuspended = true;
4301 }
4302 }
4303}
4304
Eric Laurentc75307b2015-03-17 15:29:32 -07004305audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4306 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004307{
4308 audio_devices_t device = AUDIO_DEVICE_NONE;
4309
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004310 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004311 if (index >= 0) {
4312 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4313 if (patchDesc->mUid != mUidCached) {
4314 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004315 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004316 return outputDesc->device();
4317 }
4318 }
4319
Eric Laurente552edb2014-03-10 17:42:56 -07004320 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004321 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004322 // use device for strategy enforced audible
4323 // 2: we are in call or the strategy phone is active on the output:
4324 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004325 // 3: the strategy for enforced audible is active but not enforced on the output:
4326 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004327 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004328 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004329 // 5: the strategy accessibility is active on the output:
4330 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004331 // 6: the strategy "respectful" sonification is active on the output:
4332 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004333 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004334 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004335 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004336 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004337 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004338 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004339 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004340 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004341 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4342 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004343 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004344 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004345 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004346 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004347 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004348 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004349 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4350 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004351 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004352 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004353 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004354 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004355 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004356 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004357 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004358 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004359 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004360 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004361 }
4362
Eric Laurent1c333e22014-05-20 10:48:17 -07004363 ALOGV("getNewOutputDevice() selected device %x", device);
4364 return device;
4365}
4366
Eric Laurent232f26f2016-02-17 18:06:27 -08004367audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004368{
Eric Laurent232f26f2016-02-17 18:06:27 -08004369 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004370
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004371 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004372 if (index >= 0) {
4373 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4374 if (patchDesc->mUid != mUidCached) {
4375 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004376 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004377 return inputDesc->mDevice;
4378 }
4379 }
4380
Eric Laurent232f26f2016-02-17 18:06:27 -08004381 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004382
Eric Laurente552edb2014-03-10 17:42:56 -07004383 return device;
4384}
4385
Eric Laurent794fde22016-03-11 09:50:45 -08004386bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4387 audio_stream_type_t stream2) {
4388 return ((stream1 == stream2) ||
4389 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4390 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004391}
4392
Eric Laurente0720872014-03-11 09:30:41 -07004393uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004394 return (uint32_t)getStrategy(stream);
4395}
4396
Eric Laurente0720872014-03-11 09:30:41 -07004397audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004398 // By checking the range of stream before calling getStrategy, we avoid
4399 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4400 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004401 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004402 return AUDIO_DEVICE_NONE;
4403 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004404 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004405 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4406 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004407 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004408 }
Eric Laurent794fde22016-03-11 09:50:45 -08004409 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004410 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004411 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004412 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4413 for (size_t i = 0; i < outputs.size(); i++) {
4414 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004415 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004416 curDevices |= outputDesc->device();
4417 }
4418 }
4419 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004420 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004421
4422 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4423 and doesn't really need to.*/
4424 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4425 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4426 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4427 }
Eric Laurente552edb2014-03-10 17:42:56 -07004428 return devices;
4429}
4430
François Gaffie2110e042015-03-24 08:41:51 +01004431routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4432{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004433 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004434 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004435}
4436
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004437uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4438 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004439 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4440 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4441 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004442 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4443 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4444 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004445 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004446 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004447}
4448
Eric Laurente0720872014-03-11 09:30:41 -07004449void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004450 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004451 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004452 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4453 updateDevicesAndOutputs();
4454 break;
4455 default:
4456 break;
4457 }
4458}
4459
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004460uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004461
4462 // skip beacon mute management if a dedicated TTS output is available
4463 if (mTtsOutputAvailable) {
4464 return 0;
4465 }
4466
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004467 switch(event) {
4468 case STARTING_OUTPUT:
4469 mBeaconMuteRefCount++;
4470 break;
4471 case STOPPING_OUTPUT:
4472 if (mBeaconMuteRefCount > 0) {
4473 mBeaconMuteRefCount--;
4474 }
4475 break;
4476 case STARTING_BEACON:
4477 mBeaconPlayingRefCount++;
4478 break;
4479 case STOPPING_BEACON:
4480 if (mBeaconPlayingRefCount > 0) {
4481 mBeaconPlayingRefCount--;
4482 }
4483 break;
4484 }
4485
4486 if (mBeaconMuteRefCount > 0) {
4487 // any playback causes beacon to be muted
4488 return setBeaconMute(true);
4489 } else {
4490 // no other playback: unmute when beacon starts playing, mute when it stops
4491 return setBeaconMute(mBeaconPlayingRefCount == 0);
4492 }
4493}
4494
4495uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4496 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4497 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4498 // keep track of muted state to avoid repeating mute/unmute operations
4499 if (mBeaconMuted != mute) {
4500 // mute/unmute AUDIO_STREAM_TTS on all outputs
4501 ALOGV("\t muting %d", mute);
4502 uint32_t maxLatency = 0;
4503 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004504 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004505 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004506 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004507 0 /*delay*/, AUDIO_DEVICE_NONE);
4508 const uint32_t latency = desc->latency() * 2;
4509 if (latency > maxLatency) {
4510 maxLatency = latency;
4511 }
4512 }
4513 mBeaconMuted = mute;
4514 return maxLatency;
4515 }
4516 return 0;
4517}
4518
Eric Laurente0720872014-03-11 09:30:41 -07004519audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004520 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004521{
Paul McLeanaa981192015-03-21 09:55:15 -07004522 // Routing
4523 // see if we have an explicit route
4524 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4525 // (getStrategy(stream)).
4526 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4527 // and activity count is non-zero
4528 // the device = the device from the descriptor in the RouteMap, and exit.
4529 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4530 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004531 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4532 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004533 return route->mDeviceDescriptor->type();
4534 }
4535 }
4536
Eric Laurente552edb2014-03-10 17:42:56 -07004537 if (fromCache) {
4538 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4539 strategy, mDeviceForStrategy[strategy]);
4540 return mDeviceForStrategy[strategy];
4541 }
François Gaffie2110e042015-03-24 08:41:51 +01004542 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004543}
4544
Eric Laurente0720872014-03-11 09:30:41 -07004545void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004546{
4547 for (int i = 0; i < NUM_STRATEGIES; i++) {
4548 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4549 }
4550 mPreviousOutputs = mOutputs;
4551}
4552
Eric Laurent1f2f2232014-06-02 12:01:23 -07004553uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004554 audio_devices_t prevDevice,
4555 uint32_t delayMs)
4556{
4557 // mute/unmute strategies using an incompatible device combination
4558 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4559 // if unmuting, unmute only after the specified delay
4560 if (outputDesc->isDuplicated()) {
4561 return 0;
4562 }
4563
4564 uint32_t muteWaitMs = 0;
4565 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004566 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004567
4568 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4569 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004570 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004571 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4572 bool doMute = false;
4573
4574 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4575 doMute = true;
4576 outputDesc->mStrategyMutedByDevice[i] = true;
4577 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4578 doMute = true;
4579 outputDesc->mStrategyMutedByDevice[i] = false;
4580 }
Eric Laurent99401132014-05-07 19:48:15 -07004581 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004582 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004583 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004584 // skip output if it does not share any device with current output
4585 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4586 == AUDIO_DEVICE_NONE) {
4587 continue;
4588 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004589 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4590 mute ? "muting" : "unmuting", i, curDevice);
4591 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004592 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004593 if (mute) {
4594 // FIXME: should not need to double latency if volume could be applied
4595 // immediately by the audioflinger mixer. We must account for the delay
4596 // between now and the next time the audioflinger thread for this output
4597 // will process a buffer (which corresponds to one buffer size,
4598 // usually 1/2 or 1/4 of the latency).
4599 if (muteWaitMs < desc->latency() * 2) {
4600 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004601 }
4602 }
4603 }
4604 }
4605 }
4606 }
4607
Eric Laurent99401132014-05-07 19:48:15 -07004608 // temporary mute output if device selection changes to avoid volume bursts due to
4609 // different per device volumes
4610 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004611 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4612 // temporary mute duration is conservatively set to 4 times the reported latency
4613 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4614 if (muteWaitMs < tempMuteWaitMs) {
4615 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004616 }
Eric Laurentdc462862016-07-19 12:29:53 -07004617
Eric Laurent99401132014-05-07 19:48:15 -07004618 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004619 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004620 // make sure that we do not start the temporary mute period too early in case of
4621 // delayed device change
4622 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004623 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004624 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004625 }
4626 }
4627 }
4628
Eric Laurente552edb2014-03-10 17:42:56 -07004629 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4630 if (muteWaitMs > delayMs) {
4631 muteWaitMs -= delayMs;
4632 usleep(muteWaitMs * 1000);
4633 return muteWaitMs;
4634 }
4635 return 0;
4636}
4637
Eric Laurentc75307b2015-03-17 15:29:32 -07004638uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004639 audio_devices_t device,
4640 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004641 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004642 audio_patch_handle_t *patchHandle,
4643 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004644{
Eric Laurentc75307b2015-03-17 15:29:32 -07004645 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004646 AudioParameter param;
4647 uint32_t muteWaitMs;
4648
4649 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004650 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4651 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004652 return muteWaitMs;
4653 }
4654 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4655 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004656 if ((device != AUDIO_DEVICE_NONE) &&
4657 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004658 return 0;
4659 }
4660
4661 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004662 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004663
4664 audio_devices_t prevDevice = outputDesc->mDevice;
4665
Paul McLeanaa981192015-03-21 09:55:15 -07004666 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004667
4668 if (device != AUDIO_DEVICE_NONE) {
4669 outputDesc->mDevice = device;
4670 }
4671 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4672
4673 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004674 // the requested device is AUDIO_DEVICE_NONE
4675 // OR the requested device is the same as current device
4676 // AND force is not specified
4677 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004678 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004679 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4680 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004681 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004682 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004683 return muteWaitMs;
4684 }
4685
4686 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004687
Eric Laurente552edb2014-03-10 17:42:56 -07004688 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004689 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004690 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004691 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004692 DeviceVector deviceList;
4693 if ((address == NULL) || (strlen(address) == 0)) {
4694 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4695 } else {
4696 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4697 }
4698
Eric Laurent1c333e22014-05-20 10:48:17 -07004699 if (!deviceList.isEmpty()) {
4700 struct audio_patch patch;
4701 outputDesc->toAudioPortConfig(&patch.sources[0]);
4702 patch.num_sources = 1;
4703 patch.num_sinks = 0;
4704 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4705 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004706 patch.num_sinks++;
4707 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004708 ssize_t index;
4709 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4710 index = mAudioPatches.indexOfKey(*patchHandle);
4711 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004712 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004713 }
4714 sp< AudioPatch> patchDesc;
4715 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4716 if (index >= 0) {
4717 patchDesc = mAudioPatches.valueAt(index);
4718 afPatchHandle = patchDesc->mAfPatchHandle;
4719 }
4720
Eric Laurent1c333e22014-05-20 10:48:17 -07004721 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004722 &afPatchHandle,
4723 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004724 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4725 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004726 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004727 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004728 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004729 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004730 addAudioPatch(patchDesc->mHandle, patchDesc);
4731 } else {
4732 patchDesc->mPatch = patch;
4733 }
4734 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004735 if (patchHandle) {
4736 *patchHandle = patchDesc->mHandle;
4737 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004738 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004739 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004740 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004741 }
4742 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004743
4744 // inform all input as well
4745 for (size_t i = 0; i < mInputs.size(); i++) {
4746 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004747 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004748 AudioParameter inputCmd = AudioParameter();
4749 ALOGV("%s: inform input %d of device:%d", __func__,
4750 inputDescriptor->mIoHandle, device);
4751 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4752 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4753 inputCmd.toString(),
4754 delayMs);
4755 }
4756 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004757 }
Eric Laurente552edb2014-03-10 17:42:56 -07004758
4759 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004760 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004761
4762 return muteWaitMs;
4763}
4764
Eric Laurentc75307b2015-03-17 15:29:32 -07004765status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004766 int delayMs,
4767 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004768{
Eric Laurent6a94d692014-05-20 11:18:06 -07004769 ssize_t index;
4770 if (patchHandle) {
4771 index = mAudioPatches.indexOfKey(*patchHandle);
4772 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004773 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004774 }
4775 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004776 return INVALID_OPERATION;
4777 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004778 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4779 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004780 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004781 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004782 removeAudioPatch(patchDesc->mHandle);
4783 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004784 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004785 return status;
4786}
4787
4788status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4789 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004790 bool force,
4791 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004792{
4793 status_t status = NO_ERROR;
4794
Eric Laurent1f2f2232014-06-02 12:01:23 -07004795 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004796 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4797 inputDesc->mDevice = device;
4798
4799 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4800 if (!deviceList.isEmpty()) {
4801 struct audio_patch patch;
4802 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004803 // AUDIO_SOURCE_HOTWORD is for internal use only:
4804 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004805 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004806 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004807 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4808 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004809 patch.num_sinks = 1;
4810 //only one input device for now
4811 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004812 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004813 ssize_t index;
4814 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4815 index = mAudioPatches.indexOfKey(*patchHandle);
4816 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004817 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004818 }
4819 sp< AudioPatch> patchDesc;
4820 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4821 if (index >= 0) {
4822 patchDesc = mAudioPatches.valueAt(index);
4823 afPatchHandle = patchDesc->mAfPatchHandle;
4824 }
4825
Eric Laurent1c333e22014-05-20 10:48:17 -07004826 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004827 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004828 0);
4829 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004830 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004831 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004832 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004833 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004834 addAudioPatch(patchDesc->mHandle, patchDesc);
4835 } else {
4836 patchDesc->mPatch = patch;
4837 }
4838 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004839 if (patchHandle) {
4840 *patchHandle = patchDesc->mHandle;
4841 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004842 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004843 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004844 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004845 }
4846 }
4847 }
4848 return status;
4849}
4850
Eric Laurent6a94d692014-05-20 11:18:06 -07004851status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4852 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004853{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004854 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004855 ssize_t index;
4856 if (patchHandle) {
4857 index = mAudioPatches.indexOfKey(*patchHandle);
4858 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004859 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004860 }
4861 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004862 return INVALID_OPERATION;
4863 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004864 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4865 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004867 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004868 removeAudioPatch(patchDesc->mHandle);
4869 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004870 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004871 return status;
4872}
4873
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004874sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004875 String8 address,
4876 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004877 audio_format_t& format,
4878 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004879 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004880{
4881 // Choose an input profile based on the requested capture parameters: select the first available
4882 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004883 //
4884 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4885 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004886
4887 for (size_t i = 0; i < mHwModules.size(); i++)
4888 {
4889 if (mHwModules[i]->mHandle == 0) {
4890 continue;
4891 }
4892 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4893 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004894 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004895 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004896 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004897 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004898 format,
4899 &format /*updatedFormat*/,
4900 channelMask,
4901 &channelMask /*updatedChannelMask*/,
4902 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004903
Eric Laurente552edb2014-03-10 17:42:56 -07004904 return profile;
4905 }
4906 }
4907 }
4908 return NULL;
4909}
4910
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004911
4912audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004913 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004914{
François Gaffie036e1e92015-03-19 10:16:24 +01004915 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4916 audio_devices_t selectedDeviceFromMix =
4917 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004918
François Gaffie036e1e92015-03-19 10:16:24 +01004919 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4920 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004921 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004922 return getDeviceForInputSource(inputSource);
4923}
4924
4925audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4926{
Paul McLean466dc8e2015-04-17 13:15:36 -06004927 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4928 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004929 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004930 return route->mDeviceDescriptor->type();
4931 }
4932 }
4933
4934 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004935}
4936
Eric Laurente0720872014-03-11 09:30:41 -07004937float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004938 int index,
4939 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004940{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004941 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07004942
4943 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
4944 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
4945 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
4946 // the ringtone volume
4947 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
4948 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
4949 && isStreamActive(AUDIO_STREAM_RING, 0)) {
4950 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
4951 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
4952 }
4953
Eric Laurente552edb2014-03-10 17:42:56 -07004954 // if a headset is connected, apply the following rules to ring tones and notifications
4955 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004956 // - always attenuate notifications volume by 6dB
4957 // - attenuate ring tones volume by 6dB unless music is not playing and
4958 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004959 // - if music is playing, always limit the volume to current music volume,
4960 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004961 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004962 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4963 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4964 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4965 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4966 ((stream_strategy == STRATEGY_SONIFICATION)
4967 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004968 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004969 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004970 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004971 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004972 // when the phone is ringing we must consider that music could have been paused just before
4973 // by the music application and behave as if music was active if the last music track was
4974 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004975 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004976 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004977 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004978 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004979 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004980 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4981 musicDevice),
4982 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004983 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4984 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004985 if (volumeDB > minVolDB) {
4986 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004987 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004988 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004989 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4990 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4991 // on A2DP, also ensure notification volume is not too low compared to media when
4992 // intended to be played
4993 if ((volumeDB > -96.0f) &&
4994 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4995 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4996 stream, device,
4997 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4998 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4999 }
5000 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005001 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5002 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005003 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005004 }
5005 }
5006
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005007 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005008}
5009
Eric Laurente0720872014-03-11 09:30:41 -07005010status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005011 int index,
5012 const sp<AudioOutputDescriptor>& outputDesc,
5013 audio_devices_t device,
5014 int delayMs,
5015 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005016{
Eric Laurente552edb2014-03-10 17:42:56 -07005017 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005018 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005019 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005020 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005021 return NO_ERROR;
5022 }
François Gaffie2110e042015-03-24 08:41:51 +01005023 audio_policy_forced_cfg_t forceUseForComm =
5024 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005025 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005026 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5027 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005028 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005029 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005030 return INVALID_OPERATION;
5031 }
5032
Eric Laurentc75307b2015-03-17 15:29:32 -07005033 if (device == AUDIO_DEVICE_NONE) {
5034 device = outputDesc->device();
5035 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005036
Eric Laurentffbc80f2015-03-18 18:30:19 -07005037 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005038 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005039 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005040 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005041
Eric Laurentffbc80f2015-03-18 18:30:19 -07005042 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005043
Eric Laurent3b73df72014-03-11 09:06:29 -07005044 if (stream == AUDIO_STREAM_VOICE_CALL ||
5045 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005046 float voiceVolume;
5047 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005048 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005049 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005050 } else {
5051 voiceVolume = 1.0;
5052 }
5053
Eric Laurent18fba842016-03-31 14:41:26 -07005054 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005055 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5056 mLastVoiceVolume = voiceVolume;
5057 }
5058 }
5059
5060 return NO_ERROR;
5061}
5062
Eric Laurentc75307b2015-03-17 15:29:32 -07005063void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5064 audio_devices_t device,
5065 int delayMs,
5066 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005067{
Eric Laurentc75307b2015-03-17 15:29:32 -07005068 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005069
Eric Laurent794fde22016-03-11 09:50:45 -08005070 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005071 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005072 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005073 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005074 device,
5075 delayMs,
5076 force);
5077 }
5078}
5079
Eric Laurente0720872014-03-11 09:30:41 -07005080void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005081 bool on,
5082 const sp<AudioOutputDescriptor>& outputDesc,
5083 int delayMs,
5084 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005085{
Eric Laurent72249d52015-06-08 11:12:15 -07005086 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5087 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005088 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005089 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005090 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005091 }
5092 }
5093}
5094
Eric Laurente0720872014-03-11 09:30:41 -07005095void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005096 bool on,
5097 const sp<AudioOutputDescriptor>& outputDesc,
5098 int delayMs,
5099 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005100{
Eric Laurente552edb2014-03-10 17:42:56 -07005101 if (device == AUDIO_DEVICE_NONE) {
5102 device = outputDesc->device();
5103 }
5104
Eric Laurentc75307b2015-03-17 15:29:32 -07005105 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5106 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005107
5108 if (on) {
5109 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005110 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005111 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005112 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005113 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005114 }
5115 }
5116 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5117 outputDesc->mMuteCount[stream]++;
5118 } else {
5119 if (outputDesc->mMuteCount[stream] == 0) {
5120 ALOGV("setStreamMute() unmuting non muted stream!");
5121 return;
5122 }
5123 if (--outputDesc->mMuteCount[stream] == 0) {
5124 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005125 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005126 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005127 device,
5128 delayMs);
5129 }
5130 }
5131}
5132
Eric Laurente0720872014-03-11 09:30:41 -07005133void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005134 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005135{
Eric Laurent87ffa392015-05-22 10:32:38 -07005136 if(!hasPrimaryOutput()) {
5137 return;
5138 }
5139
Eric Laurente552edb2014-03-10 17:42:56 -07005140 // if the stream pertains to sonification strategy and we are in call we must
5141 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5142 // in the device used for phone strategy and play the tone if the selected device does not
5143 // interfere with the device used for phone strategy
5144 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5145 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005146 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005147 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5148 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005149 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005150 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5151 stream, starting, outputDesc->mDevice, stateChange);
5152 if (outputDesc->mRefCount[stream]) {
5153 int muteCount = 1;
5154 if (stateChange) {
5155 muteCount = outputDesc->mRefCount[stream];
5156 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005157 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005158 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5159 for (int i = 0; i < muteCount; i++) {
5160 setStreamMute(stream, starting, mPrimaryOutput);
5161 }
5162 } else {
5163 ALOGV("handleIncallSonification() high visibility");
5164 if (outputDesc->device() &
5165 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5166 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5167 for (int i = 0; i < muteCount; i++) {
5168 setStreamMute(stream, starting, mPrimaryOutput);
5169 }
5170 }
5171 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005172 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5173 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005174 } else {
5175 mpClientInterface->stopTone();
5176 }
5177 }
5178 }
5179 }
5180}
5181
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005182audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5183{
5184 // flags to stream type mapping
5185 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5186 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5187 }
5188 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5189 return AUDIO_STREAM_BLUETOOTH_SCO;
5190 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005191 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5192 return AUDIO_STREAM_TTS;
5193 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005194
5195 // usage to stream type mapping
5196 switch (attr->usage) {
5197 case AUDIO_USAGE_MEDIA:
5198 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005199 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5200 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005201 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5202 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005203 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5204 return AUDIO_STREAM_SYSTEM;
5205 case AUDIO_USAGE_VOICE_COMMUNICATION:
5206 return AUDIO_STREAM_VOICE_CALL;
5207
5208 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5209 return AUDIO_STREAM_DTMF;
5210
5211 case AUDIO_USAGE_ALARM:
5212 return AUDIO_STREAM_ALARM;
5213 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5214 return AUDIO_STREAM_RING;
5215
5216 case AUDIO_USAGE_NOTIFICATION:
5217 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5218 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5219 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5220 case AUDIO_USAGE_NOTIFICATION_EVENT:
5221 return AUDIO_STREAM_NOTIFICATION;
5222
5223 case AUDIO_USAGE_UNKNOWN:
5224 default:
5225 return AUDIO_STREAM_MUSIC;
5226 }
5227}
Eric Laurente83b55d2014-11-14 10:06:21 -08005228
François Gaffie53615e22015-03-19 09:24:12 +01005229bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5230{
Eric Laurente83b55d2014-11-14 10:06:21 -08005231 // has flags that map to a strategy?
5232 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5233 return true;
5234 }
5235
5236 // has known usage?
5237 switch (paa->usage) {
5238 case AUDIO_USAGE_UNKNOWN:
5239 case AUDIO_USAGE_MEDIA:
5240 case AUDIO_USAGE_VOICE_COMMUNICATION:
5241 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5242 case AUDIO_USAGE_ALARM:
5243 case AUDIO_USAGE_NOTIFICATION:
5244 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5245 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5246 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5247 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5248 case AUDIO_USAGE_NOTIFICATION_EVENT:
5249 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5250 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5251 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5252 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005253 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005254 break;
5255 default:
5256 return false;
5257 }
5258 return true;
5259}
5260
François Gaffiead3183e2015-03-18 16:55:35 +01005261bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5262 routing_strategy strategy, uint32_t inPastMs,
5263 nsecs_t sysTime) const
5264{
5265 if ((sysTime == 0) && (inPastMs != 0)) {
5266 sysTime = systemTime();
5267 }
Eric Laurent794fde22016-03-11 09:50:45 -08005268 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005269 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5270 (NUM_STRATEGIES == strategy)) &&
5271 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5272 return true;
5273 }
5274 }
5275 return false;
5276}
5277
François Gaffie2110e042015-03-24 08:41:51 +01005278audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5279{
5280 return mEngine->getForceUse(usage);
5281}
5282
5283bool AudioPolicyManager::isInCall()
5284{
5285 return isStateInCall(mEngine->getPhoneState());
5286}
5287
5288bool AudioPolicyManager::isStateInCall(int state)
5289{
5290 return is_state_in_call(state);
5291}
5292
Eric Laurentd60560a2015-04-10 11:31:20 -07005293void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5294{
5295 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5296 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5297 if (sourceDesc->mDevice->equals(deviceDesc)) {
5298 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5299 stopAudioSource(sourceDesc->getHandle());
5300 }
5301 }
5302
5303 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5304 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5305 bool release = false;
5306 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5307 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5308 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5309 source->ext.device.type == deviceDesc->type()) {
5310 release = true;
5311 }
5312 }
5313 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5314 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5315 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5316 sink->ext.device.type == deviceDesc->type()) {
5317 release = true;
5318 }
5319 }
5320 if (release) {
5321 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5322 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5323 }
5324 }
5325}
5326
Phil Burk09bc4612016-02-24 15:58:15 -08005327// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005328void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5329 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005330 // TODO Set this based on Config properties.
5331 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005332
5333 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5334 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005335 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005336
5337 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005338 bool supportsAC3 = false;
5339 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005340 bool supportsIEC61937 = false;
5341 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5342 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005343 switch (format) {
5344 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005345 supportsAC3 = true;
5346 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005347 case AUDIO_FORMAT_E_AC3:
5348 case AUDIO_FORMAT_DTS:
5349 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005350 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005351 break;
5352 case AUDIO_FORMAT_IEC61937:
5353 supportsIEC61937 = true;
5354 break;
5355 default:
5356 break;
5357 }
5358 }
Phil Burk09bc4612016-02-24 15:58:15 -08005359
5360 // Modify formats based on surround preferences.
5361 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005362 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5363 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5364 // Remove surround sound related formats.
5365 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5366 audio_format_t format = formats[formatIndex];
5367 switch(format) {
5368 case AUDIO_FORMAT_AC3:
5369 case AUDIO_FORMAT_E_AC3:
5370 case AUDIO_FORMAT_DTS:
5371 case AUDIO_FORMAT_DTS_HD:
5372 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005373 formats.removeAt(formatIndex);
5374 break;
5375 default:
5376 formatIndex++; // keep it
5377 break;
5378 }
Phil Burk09bc4612016-02-24 15:58:15 -08005379 }
Phil Burk07ac1142016-03-25 13:39:29 -07005380 supportsAC3 = false;
5381 supportsOtherSurround = false;
5382 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005383 }
Phil Burk07ac1142016-03-25 13:39:29 -07005384 } else { // AUTO or ALWAYS
5385 // Most TVs support AC3 even if they do not report it in the EDID.
5386 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5387 && !supportsAC3) {
5388 formats.add(AUDIO_FORMAT_AC3);
5389 supportsAC3 = true;
5390 }
5391
5392 // If ALWAYS, add support for raw surround formats if all are missing.
5393 // This assumes that if any of these formats are reported by the HAL
5394 // then the report is valid and should not be modified.
5395 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5396 && !supportsOtherSurround) {
5397 formats.add(AUDIO_FORMAT_E_AC3);
5398 formats.add(AUDIO_FORMAT_DTS);
5399 formats.add(AUDIO_FORMAT_DTS_HD);
5400 supportsOtherSurround = true;
5401 }
5402
5403 // Add support for IEC61937 if any raw surround supported.
5404 // The HAL could do this but add it here, just in case.
5405 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5406 formats.add(AUDIO_FORMAT_IEC61937);
5407 supportsIEC61937 = true;
5408 }
Phil Burk09bc4612016-02-24 15:58:15 -08005409 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005410}
5411
5412// Modify the list of channel masks supported.
5413void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5414 ChannelsVector &channelMasks = *channelMasksPtr;
5415 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5416 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5417
5418 // If NEVER, then remove support for channelMasks > stereo.
5419 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5420 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5421 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5422 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5423 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5424 channelMasks.removeAt(maskIndex);
5425 } else {
5426 maskIndex++;
5427 }
5428 }
5429 // If ALWAYS, then make sure we at least support 5.1
5430 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5431 bool supports5dot1 = false;
5432 // Are there any channel masks that can be considered "surround"?
5433 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5434 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5435 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5436 supports5dot1 = true;
5437 break;
5438 }
5439 }
5440 // If not then add 5.1 support.
5441 if (!supports5dot1) {
5442 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5443 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5444 }
Phil Burk09bc4612016-02-24 15:58:15 -08005445 }
5446}
5447
Phil Burk00eeb322016-03-31 12:41:00 -07005448void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5449 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005450 AudioProfileVector &profiles)
5451{
5452 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005453
François Gaffie112b0af2015-11-19 16:13:25 +01005454 // Format MUST be checked first to update the list of AudioProfile
5455 if (profiles.hasDynamicFormat()) {
5456 reply = mpClientInterface->getParameters(ioHandle,
5457 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005458 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005459 AudioParameter repliedParameters(reply);
5460 if (repliedParameters.get(
5461 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005462 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5463 return;
5464 }
Phil Burk09bc4612016-02-24 15:58:15 -08005465 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005466 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005467 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005468 }
Phil Burk09bc4612016-02-24 15:58:15 -08005469 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005470 }
5471 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5472
Eric Laurent20eb3a42016-01-26 18:39:17 -08005473 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005474 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005475 ChannelsVector channelMasks;
5476 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005477 AudioParameter requestedParameters;
5478 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5479
5480 if (profiles.hasDynamicRateFor(format)) {
5481 reply = mpClientInterface->getParameters(ioHandle,
5482 requestedParameters.toString() + ";" +
5483 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5484 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005485 AudioParameter repliedParameters(reply);
5486 if (repliedParameters.get(
5487 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5488 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005489 }
5490 }
5491 if (profiles.hasDynamicChannelsFor(format)) {
5492 reply = mpClientInterface->getParameters(ioHandle,
5493 requestedParameters.toString() + ";" +
5494 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5495 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005496 AudioParameter repliedParameters(reply);
5497 if (repliedParameters.get(
5498 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5499 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005500 if (device == AUDIO_DEVICE_OUT_HDMI) {
5501 filterSurroundChannelMasks(&channelMasks);
5502 }
François Gaffie112b0af2015-11-19 16:13:25 +01005503 }
5504 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005505 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005506 }
5507}
Eric Laurentd60560a2015-04-10 11:31:20 -07005508
Eric Laurente552edb2014-03-10 17:42:56 -07005509}; // namespace android