blob: c0bb4e7cfc00ed13a45e51cb888d293708f39bf6 [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 Laurente552edb2014-03-10 17:42:56 -0700276
Eric Laurent87ffa392015-05-22 10:32:38 -0700277 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700278 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
279 updateCallRouting(newDevice);
280 }
281
Eric Laurentd60560a2015-04-10 11:31:20 -0700282 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
283 cleanUpForDevice(devDesc);
284 }
285
Eric Laurentb52c1522014-05-20 11:27:36 -0700286 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700287 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700288 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700289
290 ALOGW("setDeviceConnectionState() invalid device: %x", device);
291 return BAD_VALUE;
292}
293
Eric Laurente0720872014-03-11 09:30:41 -0700294audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100295 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700296{
Eric Laurent634b7142016-04-20 13:48:02 -0700297 sp<DeviceDescriptor> devDesc =
298 mHwModules.getDeviceDescriptor(device, device_address, "",
299 (strlen(device_address) != 0)/*matchAddress*/);
300
301 if (devDesc == 0) {
302 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
303 device, device_address);
304 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
305 }
François Gaffie53615e22015-03-19 09:24:12 +0100306
Eric Laurent3a4311c2014-03-17 12:00:47 -0700307 DeviceVector *deviceVector;
308
Eric Laurente552edb2014-03-10 17:42:56 -0700309 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700310 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700311 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700312 deviceVector = &mAvailableInputDevices;
313 } else {
314 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
315 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700316 }
Eric Laurent634b7142016-04-20 13:48:02 -0700317
318 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
319 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800320}
321
Eric Laurentdc462862016-07-19 12:29:53 -0700322uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700323{
324 bool createTxPatch = false;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700325 status_t status;
326 audio_patch_handle_t afPatchHandle;
327 DeviceVector deviceList;
Eric Laurentdc462862016-07-19 12:29:53 -0700328 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700329
Eric Laurent87ffa392015-05-22 10:32:38 -0700330 if(!hasPrimaryOutput()) {
Eric Laurentdc462862016-07-19 12:29:53 -0700331 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700332 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800333 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700334 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
335
336 // release existing RX patch if any
337 if (mCallRxPatch != 0) {
338 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
339 mCallRxPatch.clear();
340 }
341 // release TX patch if any
342 if (mCallTxPatch != 0) {
343 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
344 mCallTxPatch.clear();
345 }
346
347 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
348 // via setOutputDevice() on primary output.
349 // Otherwise, create two audio patches for TX and RX path.
350 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700351 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 // If the TX device is also on the primary HW module, setOutputDevice() will take care
353 // of it due to legacy implementation. If not, create a patch.
354 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
355 == AUDIO_DEVICE_NONE) {
356 createTxPatch = true;
357 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700358 } else { // create RX path audio patch
359 struct audio_patch patch;
360
361 patch.num_sources = 1;
362 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700363 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
364 ALOG_ASSERT(!deviceList.isEmpty(),
365 "updateCallRouting() selected device not in output device list");
366 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
367 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
368 ALOG_ASSERT(!deviceList.isEmpty(),
369 "updateCallRouting() no telephony RX device");
370 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
371
372 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
373 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
374
375 // request to reuse existing output stream if one is already opened to reach the RX device
376 SortedVector<audio_io_handle_t> outputs =
377 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700378 audio_io_handle_t output = selectOutput(outputs,
379 AUDIO_OUTPUT_FLAG_NONE,
380 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700381 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700382 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700383 ALOG_ASSERT(!outputDesc->isDuplicated(),
384 "updateCallRouting() RX device output is duplicated");
385 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700386 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700387 patch.num_sources = 2;
388 }
389
390 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700391 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700392 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
393 status);
394 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100395 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700396 mCallRxPatch->mAfPatchHandle = afPatchHandle;
397 mCallRxPatch->mUid = mUidCached;
398 }
399 createTxPatch = true;
400 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700401 if (createTxPatch) { // create TX path audio patch
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 struct audio_patch patch;
Eric Laurent8ae73122016-04-12 10:13:29 -0700403
Eric Laurentc2730ba2014-07-20 15:47:07 -0700404 patch.num_sources = 1;
405 patch.num_sinks = 1;
406 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
407 ALOG_ASSERT(!deviceList.isEmpty(),
408 "updateCallRouting() selected device not in input device list");
409 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
410 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
411 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
412 ALOG_ASSERT(!deviceList.isEmpty(),
413 "updateCallRouting() no telephony TX device");
414 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
415 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
416
417 SortedVector<audio_io_handle_t> outputs =
418 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700419 audio_io_handle_t output = selectOutput(outputs,
420 AUDIO_OUTPUT_FLAG_NONE,
421 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700422 // request to reuse existing output stream if one is already opened to reach the TX
423 // path output device
424 if (output != AUDIO_IO_HANDLE_NONE) {
425 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
426 ALOG_ASSERT(!outputDesc->isDuplicated(),
427 "updateCallRouting() RX device output is duplicated");
428 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700429 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700430 patch.num_sources = 2;
431 }
432
Eric Laurentc0a889f2015-10-14 14:36:34 -0700433 // terminate active capture if on the same HW module as the call TX source device
434 // FIXME: would be better to refine to only inputs whose profile connects to the
435 // call TX device but this information is not in the audio patch and logic here must be
436 // symmetric to the one in startInput()
Eric Laurent232f26f2016-02-17 18:06:27 -0800437 audio_io_handle_t activeInput = mInputs.getActiveInput();
438 if (activeInput != 0) {
439 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
440 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
441 //FIXME: consider all active sessions
442 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
443 audio_session_t activeSession = activeSessions.keyAt(0);
444 stopInput(activeInput, activeSession);
445 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700446 }
447 }
448
Eric Laurentc2730ba2014-07-20 15:47:07 -0700449 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentdc462862016-07-19 12:29:53 -0700450 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700451 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
452 status);
453 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100454 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700455 mCallTxPatch->mAfPatchHandle = afPatchHandle;
456 mCallTxPatch->mUid = mUidCached;
457 }
458 }
Eric Laurentdc462862016-07-19 12:29:53 -0700459
460 return muteWaitMs;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700461}
462
Eric Laurente0720872014-03-11 09:30:41 -0700463void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700464{
465 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100466 // store previous phone state for management of sonification strategy below
467 int oldState = mEngine->getPhoneState();
468
469 if (mEngine->setPhoneState(state) != NO_ERROR) {
470 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700471 return;
472 }
François Gaffie2110e042015-03-24 08:41:51 +0100473 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700474 // if leaving call state, handle special case of active streams
475 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700476 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700477 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800478 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700479 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700480 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800481
Eric Laurent63dea1d2015-07-02 17:10:28 -0700482 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800483 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700484 }
485
François Gaffie2110e042015-03-24 08:41:51 +0100486 /**
487 * Switching to or from incall state or switching between telephony and VoIP lead to force
488 * routing command.
489 */
490 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
491 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700492
493 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700494 checkA2dpSuspend();
495 checkOutputForAllStrategies();
496 updateDevicesAndOutputs();
497
Eric Laurente552edb2014-03-10 17:42:56 -0700498 int delayMs = 0;
499 if (isStateInCall(state)) {
500 nsecs_t sysTime = systemTime();
501 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700502 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700503 // mute media and sonification strategies and delay device switch by the largest
504 // latency of any output where either strategy is active.
505 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100506 if ((isStrategyActive(desc, STRATEGY_MEDIA,
507 SONIFICATION_HEADSET_MUSIC_DELAY,
508 sysTime) ||
509 isStrategyActive(desc, STRATEGY_SONIFICATION,
510 SONIFICATION_HEADSET_MUSIC_DELAY,
511 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700512 (delayMs < (int)desc->latency()*2)) {
513 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700514 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700515 setStrategyMute(STRATEGY_MEDIA, true, desc);
516 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700517 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700518 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
519 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700520 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
521 }
522 }
523
Eric Laurent87ffa392015-05-22 10:32:38 -0700524 if (hasPrimaryOutput()) {
525 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
526 // the device returned is not necessarily reachable via this output
527 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
528 // force routing command to audio hardware when ending call
529 // even if no device change is needed
530 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
531 rxDevice = mPrimaryOutput->device();
532 }
Eric Laurente552edb2014-03-10 17:42:56 -0700533
Eric Laurent87ffa392015-05-22 10:32:38 -0700534 if (state == AUDIO_MODE_IN_CALL) {
535 updateCallRouting(rxDevice, delayMs);
536 } else if (oldState == AUDIO_MODE_IN_CALL) {
537 if (mCallRxPatch != 0) {
538 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
539 mCallRxPatch.clear();
540 }
541 if (mCallTxPatch != 0) {
542 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
543 mCallTxPatch.clear();
544 }
545 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
546 } else {
547 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700548 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 // if entering in call state, handle special case of active streams
551 // pertaining to sonification strategy see handleIncallSonification()
552 if (isStateInCall(state)) {
553 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800554 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700555 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700556 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700557
558 // force reevaluating accessibility routing when call starts
559 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700560 }
561
562 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700563 if (state == AUDIO_MODE_RINGTONE &&
564 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700565 mLimitRingtoneVolume = true;
566 } else {
567 mLimitRingtoneVolume = false;
568 }
569}
570
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700571audio_mode_t AudioPolicyManager::getPhoneState() {
572 return mEngine->getPhoneState();
573}
574
Eric Laurente0720872014-03-11 09:30:41 -0700575void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700576 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700577{
François Gaffie2110e042015-03-24 08:41:51 +0100578 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700579
François Gaffie2110e042015-03-24 08:41:51 +0100580 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
581 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
582 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700583 }
François Gaffie2110e042015-03-24 08:41:51 +0100584 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
585 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
586 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700587
588 // check for device and output changes triggered by new force usage
589 checkA2dpSuspend();
590 checkOutputForAllStrategies();
591 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800592
Eric Laurentdc462862016-07-19 12:29:53 -0700593 //FIXME: workaround for truncated touch sounds
594 // to be removed when the problem is handled by system UI
595 uint32_t delayMs = 0;
596 uint32_t waitMs = 0;
597 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
598 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
599 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700600 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700601 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700602 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700603 }
Eric Laurente552edb2014-03-10 17:42:56 -0700604 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700605 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
606 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
607 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700608 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
609 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700610 }
Eric Laurente552edb2014-03-10 17:42:56 -0700611 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700612 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700613 }
614 }
615
Eric Laurent232f26f2016-02-17 18:06:27 -0800616 audio_io_handle_t activeInput = mInputs.getActiveInput();
617 if (activeInput != 0) {
618 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
619 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700620 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800621 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
622 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700623 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800624 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700625 }
Eric Laurente552edb2014-03-10 17:42:56 -0700626 }
Eric Laurente552edb2014-03-10 17:42:56 -0700627}
628
Eric Laurente0720872014-03-11 09:30:41 -0700629void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700630{
631 ALOGV("setSystemProperty() property %s, value %s", property, value);
632}
633
634// Find a direct output profile compatible with the parameters passed, even if the input flags do
635// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800636sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700637 audio_devices_t device,
638 uint32_t samplingRate,
639 audio_format_t format,
640 audio_channel_mask_t channelMask,
641 audio_output_flags_t flags)
642{
Eric Laurent861a6282015-05-18 15:40:16 -0700643 // only retain flags that will drive the direct output profile selection
644 // if explicitly requested
645 static const uint32_t kRelevantFlags =
646 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
647 flags =
648 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
649
650 sp<IOProfile> profile;
651
Eric Laurente552edb2014-03-10 17:42:56 -0700652 for (size_t i = 0; i < mHwModules.size(); i++) {
653 if (mHwModules[i]->mHandle == 0) {
654 continue;
655 }
656 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700657 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
658 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700659 samplingRate, NULL /*updatedSamplingRate*/,
660 format, NULL /*updatedFormat*/,
661 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700662 flags)) {
663 continue;
664 }
665 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100666 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700667 continue;
668 }
669 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100670 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700671 continue;
672 }
673 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100674 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700675 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700676 }
Eric Laurente552edb2014-03-10 17:42:56 -0700677 }
678 }
Eric Laurent861a6282015-05-18 15:40:16 -0700679 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700680}
681
Eric Laurente0720872014-03-11 09:30:41 -0700682audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100683 uint32_t samplingRate,
684 audio_format_t format,
685 audio_channel_mask_t channelMask,
686 audio_output_flags_t flags,
687 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700688{
Eric Laurent3b73df72014-03-11 09:06:29 -0700689 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700690 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
691 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
692 device, stream, samplingRate, format, channelMask, flags);
693
Eric Laurente83b55d2014-11-14 10:06:21 -0800694 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
695 stream, samplingRate,format, channelMask,
696 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700697}
698
Eric Laurente83b55d2014-11-14 10:06:21 -0800699status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
700 audio_io_handle_t *output,
701 audio_session_t session,
702 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700703 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800704 uint32_t samplingRate,
705 audio_format_t format,
706 audio_channel_mask_t channelMask,
707 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700708 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800709 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710{
Eric Laurente83b55d2014-11-14 10:06:21 -0800711 audio_attributes_t attributes;
712 if (attr != NULL) {
713 if (!isValidAttributes(attr)) {
714 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
715 attr->usage, attr->content_type, attr->flags,
716 attr->tags);
717 return BAD_VALUE;
718 }
719 attributes = *attr;
720 } else {
721 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
722 ALOGE("getOutputForAttr(): invalid stream type");
723 return BAD_VALUE;
724 }
725 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700727 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800728 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100729 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800730 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100731 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800732 }
François Gaffie036e1e92015-03-19 10:16:24 +0100733 *stream = streamTypefromAttributesInt(&attributes);
734 *output = desc->mIoHandle;
735 ALOGV("getOutputForAttr() returns output %d", *output);
736 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800737 }
738 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
739 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
740 return BAD_VALUE;
741 }
742
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700743 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
744 " session %d selectedDeviceId %d",
745 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
746 session, selectedDeviceId);
747
748 *stream = streamTypefromAttributesInt(&attributes);
749
750 // Explicit routing?
751 sp<DeviceDescriptor> deviceDesc;
752 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
753 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
754 deviceDesc = mAvailableOutputDevices[i];
755 break;
756 }
757 }
758 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700759
Eric Laurente83b55d2014-11-14 10:06:21 -0800760 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700761 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700762
Eric Laurente83b55d2014-11-14 10:06:21 -0800763 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700764 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
765 }
766
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700767 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700768 device, samplingRate, format, channelMask, flags);
769
Eric Laurente83b55d2014-11-14 10:06:21 -0800770 *output = getOutputForDevice(device, session, *stream,
771 samplingRate, format, channelMask,
772 flags, offloadInfo);
773 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700774 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800775 return INVALID_OPERATION;
776 }
Paul McLeanaa981192015-03-21 09:55:15 -0700777
Eric Laurente83b55d2014-11-14 10:06:21 -0800778 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700779}
780
781audio_io_handle_t AudioPolicyManager::getOutputForDevice(
782 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800783 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700784 audio_stream_type_t stream,
785 uint32_t samplingRate,
786 audio_format_t format,
787 audio_channel_mask_t channelMask,
788 audio_output_flags_t flags,
789 const audio_offload_info_t *offloadInfo)
790{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700791 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700792 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700793
Eric Laurente552edb2014-03-10 17:42:56 -0700794#ifdef AUDIO_POLICY_TEST
795 if (mCurOutput != 0) {
796 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
797 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
798
799 if (mTestOutputs[mCurOutput] == 0) {
800 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700801 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
802 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700803 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700804 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700805 outputDesc->mFlags =
806 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700807 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700808 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
809 config.sample_rate = mTestSamplingRate;
810 config.channel_mask = mTestChannels;
811 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700812 if (offloadInfo != NULL) {
813 config.offload_info = *offloadInfo;
814 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700815 status = mpClientInterface->openOutput(0,
816 &mTestOutputs[mCurOutput],
817 &config,
818 &outputDesc->mDevice,
819 String8(""),
820 &outputDesc->mLatency,
821 outputDesc->mFlags);
822 if (status == NO_ERROR) {
823 outputDesc->mSamplingRate = config.sample_rate;
824 outputDesc->mFormat = config.format;
825 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700826 AudioParameter outputCmd = AudioParameter();
827 outputCmd.addInt(String8("set_id"),mCurOutput);
828 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
829 addOutput(mTestOutputs[mCurOutput], outputDesc);
830 }
831 }
832 return mTestOutputs[mCurOutput];
833 }
834#endif //AUDIO_POLICY_TEST
835
836 // open a direct output if required by specified parameters
837 //force direct flag if offload flag is set: offloading implies a direct output stream
838 // and all common behaviors are driven by checking only the direct flag
839 // this should normally be set appropriately in the policy configuration file
840 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700841 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700842 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700843 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
844 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
845 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800846 // only allow deep buffering for music stream type
847 if (stream != AUDIO_STREAM_MUSIC) {
848 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700849 } else if (/* stream == AUDIO_STREAM_MUSIC && */
850 flags == AUDIO_OUTPUT_FLAG_NONE &&
851 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
852 // use DEEP_BUFFER as default output for music stream type
853 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800854 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700855 if (stream == AUDIO_STREAM_TTS) {
856 flags = AUDIO_OUTPUT_FLAG_TTS;
857 }
Eric Laurente552edb2014-03-10 17:42:56 -0700858
Eric Laurentb732cf52014-09-24 19:08:21 -0700859 sp<IOProfile> profile;
860
861 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700862 // and not explicitly requested
863 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800864 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700865 audio_channel_count_from_out_mask(channelMask) <= 2) {
866 goto non_direct_output;
867 }
868
Andy Hung2ddee192015-12-18 17:34:44 -0800869 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
870 // This prevents creating an offloaded track and tearing it down immediately after start
871 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700872 // FIXME: We should check the audio session here but we do not have it in this context.
873 // This may prevent offloading in rare situations where effects are left active by apps
874 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700875
Eric Laurente552edb2014-03-10 17:42:56 -0700876 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800877 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700878 profile = getProfileForDirectOutput(device,
879 samplingRate,
880 format,
881 channelMask,
882 (audio_output_flags_t)flags);
883 }
884
Eric Laurent1c333e22014-05-20 10:48:17 -0700885 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700886 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700887
888 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700889 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700890 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
891 outputDesc = desc;
892 // reuse direct output if currently open and configured with same parameters
893 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800894 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700895 (channelMask == outputDesc->mChannelMask)) {
896 outputDesc->mDirectOpenCount++;
897 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
898 return mOutputs.keyAt(i);
899 }
900 }
901 }
902 // close direct output if currently open and configured with different parameters
903 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700904 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700905 }
Eric Laurent861a6282015-05-18 15:40:16 -0700906
907 // if the selected profile is offloaded and no offload info was specified,
908 // create a default one
909 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100910 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700911 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
912 defaultOffloadInfo.sample_rate = samplingRate;
913 defaultOffloadInfo.channel_mask = channelMask;
914 defaultOffloadInfo.format = format;
915 defaultOffloadInfo.stream_type = stream;
916 defaultOffloadInfo.bit_rate = 0;
917 defaultOffloadInfo.duration_us = -1;
918 defaultOffloadInfo.has_video = true; // conservative
919 defaultOffloadInfo.is_streaming = true; // likely
920 offloadInfo = &defaultOffloadInfo;
921 }
922
Eric Laurentc75307b2015-03-17 15:29:32 -0700923 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700924 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700925 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700926 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700927 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
928 config.sample_rate = samplingRate;
929 config.channel_mask = channelMask;
930 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700931 if (offloadInfo != NULL) {
932 config.offload_info = *offloadInfo;
933 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700934 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700935 &output,
936 &config,
937 &outputDesc->mDevice,
938 String8(""),
939 &outputDesc->mLatency,
940 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700941
942 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 if (status != NO_ERROR ||
944 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800945 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700946 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700947 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
948 "format %d %d, channelMask %04x %04x", output, samplingRate,
949 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
950 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700951 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700952 mpClientInterface->closeOutput(output);
953 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800954 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800955 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800956 goto non_direct_output;
957 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700958 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700959 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700960 outputDesc->mSamplingRate = config.sample_rate;
961 outputDesc->mChannelMask = config.channel_mask;
962 outputDesc->mFormat = config.format;
963 outputDesc->mRefCount[stream] = 0;
964 outputDesc->mStopTime[stream] = 0;
965 outputDesc->mDirectOpenCount = 1;
966
Eric Laurente552edb2014-03-10 17:42:56 -0700967 audio_io_handle_t srcOutput = getOutputForEffect();
968 addOutput(output, outputDesc);
969 audio_io_handle_t dstOutput = getOutputForEffect();
970 if (dstOutput == output) {
971 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
972 }
973 mPreviousOutputs = mOutputs;
974 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700975 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700976 return output;
977 }
978
Eric Laurentb732cf52014-09-24 19:08:21 -0700979non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700980
981 // A request for HW A/V sync cannot fallback to a mixed output because time
982 // stamps are embedded in audio data
983 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
984 return AUDIO_IO_HANDLE_NONE;
985 }
986
Eric Laurente552edb2014-03-10 17:42:56 -0700987 // ignoring channel mask due to downmix capability in mixer
988
989 // open a non direct output
990
991 // for non direct outputs, only PCM is supported
992 if (audio_is_linear_pcm(format)) {
993 // get which output is suitable for the specified stream. The actual
994 // routing change will happen when startOutput() will be called
995 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
996
Eric Laurent8838a382014-09-08 16:44:28 -0700997 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
998 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
999 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001000 }
1001 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1002 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1003
Paul McLeanaa981192015-03-21 09:55:15 -07001004 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07001005
1006 return output;
1007}
1008
Eric Laurente0720872014-03-11 09:30:41 -07001009audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001010 audio_output_flags_t flags,
1011 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001012{
1013 // select one output among several that provide a path to a particular device or set of
1014 // devices (the list was previously build by getOutputsForDevice()).
1015 // The priority is as follows:
1016 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001017 // 2: the output with the bit depth the closest to the requested one
1018 // 3: the primary output
1019 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001020
1021 if (outputs.size() == 0) {
1022 return 0;
1023 }
1024 if (outputs.size() == 1) {
1025 return outputs[0];
1026 }
1027
1028 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001029 audio_io_handle_t outputForFlags = 0;
1030 audio_io_handle_t outputForPrimary = 0;
1031 audio_io_handle_t outputForFormat = 0;
1032 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1033 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001034
1035 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001036 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001037 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001038 // if a valid format is specified, skip output if not compatible
1039 if (format != AUDIO_FORMAT_INVALID) {
1040 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001041 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001042 continue;
1043 }
1044 } else if (!audio_is_linear_pcm(format)) {
1045 continue;
1046 }
Eric Laurente6930022016-02-11 10:20:40 -08001047 if (AudioPort::isBetterFormatMatch(
1048 outputDesc->mFormat, bestFormat, format)) {
1049 outputForFormat = outputs[i];
1050 bestFormat = outputDesc->mFormat;
1051 }
Eric Laurent8838a382014-09-08 16:44:28 -07001052 }
1053
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001054 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001055 if (commonFlags >= maxCommonFlags) {
1056 if (commonFlags == maxCommonFlags) {
1057 if (AudioPort::isBetterFormatMatch(
1058 outputDesc->mFormat, bestFormatForFlags, format)) {
1059 outputForFlags = outputs[i];
1060 bestFormatForFlags = outputDesc->mFormat;
1061 }
1062 } else {
1063 outputForFlags = outputs[i];
1064 maxCommonFlags = commonFlags;
1065 bestFormatForFlags = outputDesc->mFormat;
1066 }
Eric Laurente552edb2014-03-10 17:42:56 -07001067 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1068 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001069 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001070 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001071 }
1072 }
1073 }
1074
Eric Laurente6930022016-02-11 10:20:40 -08001075 if (outputForFlags != 0) {
1076 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001077 }
Eric Laurente6930022016-02-11 10:20:40 -08001078 if (outputForFormat != 0) {
1079 return outputForFormat;
1080 }
1081 if (outputForPrimary != 0) {
1082 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001083 }
1084
1085 return outputs[0];
1086}
1087
Eric Laurente0720872014-03-11 09:30:41 -07001088status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001089 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001090 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001091{
Paul McLeanaa981192015-03-21 09:55:15 -07001092 ALOGV("startOutput() output %d, stream %d, session %d",
1093 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001094 ssize_t index = mOutputs.indexOfKey(output);
1095 if (index < 0) {
1096 ALOGW("startOutput() unknown output %d", output);
1097 return BAD_VALUE;
1098 }
1099
Eric Laurentc75307b2015-03-17 15:29:32 -07001100 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1101
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001102 // Routing?
1103 mOutputRoutes.incRouteActivity(session);
1104
Eric Laurentc75307b2015-03-17 15:29:32 -07001105 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001106 AudioMix *policyMix = NULL;
1107 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001108 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001109 policyMix = outputDesc->mPolicyMix;
1110 address = policyMix->mDeviceAddress.string();
1111 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1112 newDevice = policyMix->mDeviceType;
1113 } else {
1114 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1115 }
Eric Laurent493404d2015-04-21 15:07:36 -07001116 } else if (mOutputRoutes.hasRouteChanged(session)) {
1117 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001118 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001119 } else {
1120 newDevice = AUDIO_DEVICE_NONE;
1121 }
1122
1123 uint32_t delayMs = 0;
1124
Eric Laurentc40d9692016-04-13 19:14:13 -07001125 status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001126
1127 if (status != NO_ERROR) {
1128 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001129 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001130 }
1131 // Automatically enable the remote submix input when output is started on a re routing mix
1132 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001133 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1134 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001135 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1136 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001137 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001138 "remote-submix");
1139 }
1140
1141 if (delayMs != 0) {
1142 usleep(delayMs * 1000);
1143 }
1144
1145 return status;
1146}
1147
1148status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1149 audio_stream_type_t stream,
1150 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001151 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001152 uint32_t *delayMs)
1153{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001154 // cannot start playback of STREAM_TTS if any other output is being used
1155 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001156
1157 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001158 if (stream == AUDIO_STREAM_TTS) {
1159 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001160 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001161 return INVALID_OPERATION;
1162 } else {
1163 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1164 }
1165 } else {
1166 // some playback other than beacon starts
1167 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1168 }
1169
Eric Laurent77305a62016-07-25 16:39:22 -07001170 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001171 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001172 bool force = !outputDesc->isActive() &&
1173 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001174
Eric Laurente552edb2014-03-10 17:42:56 -07001175 // increment usage count for this stream on the requested output:
1176 // NOTE that the usage count is the same for duplicated output and hardware output which is
1177 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1178 outputDesc->changeRefCount(stream, 1);
1179
Eric Laurent493404d2015-04-21 15:07:36 -07001180 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001181 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001182 if (device == AUDIO_DEVICE_NONE) {
1183 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001184 }
Eric Laurente552edb2014-03-10 17:42:56 -07001185 routing_strategy strategy = getStrategy(stream);
1186 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001187 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1188 (beaconMuteLatency > 0);
1189 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001190 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001191 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001192 if (desc != outputDesc) {
Eric Laurent77305a62016-07-25 16:39:22 -07001193 // force a device change if any other output is:
1194 // - managed by the same hw module
1195 // - has a current device selection that differs from selected device.
1196 // - supports currently selected device
1197 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001198 // In this case, the audio HAL must receive the new device selection so that it can
1199 // change the device currently selected by the other active output.
1200 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurent77305a62016-07-25 16:39:22 -07001201 desc->device() != device &&
1202 desc->supportedDevices() & device &&
1203 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001204 force = true;
1205 }
1206 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001207 // a notification so that audio focus effect can propagate, or that a mute/unmute
1208 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001209 uint32_t latency = desc->latency();
1210 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1211 waitMs = latency;
1212 }
1213 }
1214 }
Eric Laurentdc462862016-07-19 12:29:53 -07001215 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address);
Eric Laurente552edb2014-03-10 17:42:56 -07001216
1217 // handle special case for sonification while in call
1218 if (isInCall()) {
1219 handleIncallSonification(stream, true, false);
1220 }
1221
1222 // apply volume rules for current stream and device if necessary
1223 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001224 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001225 outputDesc,
1226 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001227
1228 // update the outputs if starting an output with a stream that can affect notification
1229 // routing
1230 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001231
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001232 // force reevaluating accessibility routing when ringtone or alarm starts
1233 if (strategy == STRATEGY_SONIFICATION) {
1234 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1235 }
Eric Laurentdc462862016-07-19 12:29:53 -07001236
1237 if (waitMs > muteWaitMs) {
1238 *delayMs = waitMs - muteWaitMs;
1239 }
Eric Laurente552edb2014-03-10 17:42:56 -07001240 }
Eric Laurentdc462862016-07-19 12:29:53 -07001241
Eric Laurente552edb2014-03-10 17:42:56 -07001242 return NO_ERROR;
1243}
1244
1245
Eric Laurente0720872014-03-11 09:30:41 -07001246status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001247 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001248 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001249{
1250 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1251 ssize_t index = mOutputs.indexOfKey(output);
1252 if (index < 0) {
1253 ALOGW("stopOutput() unknown output %d", output);
1254 return BAD_VALUE;
1255 }
1256
Eric Laurentc75307b2015-03-17 15:29:32 -07001257 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001258
Eric Laurentc75307b2015-03-17 15:29:32 -07001259 if (outputDesc->mRefCount[stream] == 1) {
1260 // Automatically disable the remote submix input when output is stopped on a
1261 // re routing mix of type MIX_TYPE_RECORDERS
1262 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1263 outputDesc->mPolicyMix != NULL &&
1264 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1265 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1266 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001267 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001268 "remote-submix");
1269 }
1270 }
1271
1272 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001273 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001274 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001275 int activityCount = mOutputRoutes.decRouteActivity(session);
1276 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1277
1278 if (forceDeviceUpdate) {
1279 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1280 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001281 }
1282
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001283 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001284}
1285
1286status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001287 audio_stream_type_t stream,
1288 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001289{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001290 // always handle stream stop, check which stream type is stopping
1291 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1292
Eric Laurente552edb2014-03-10 17:42:56 -07001293 // handle special case for sonification while in call
1294 if (isInCall()) {
1295 handleIncallSonification(stream, false, false);
1296 }
1297
1298 if (outputDesc->mRefCount[stream] > 0) {
1299 // decrement usage count of this stream on the output
1300 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001301
Eric Laurente552edb2014-03-10 17:42:56 -07001302 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001303 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001304 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001305 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001306 // delay the device switch by twice the latency because stopOutput() is executed when
1307 // the track stop() command is received and at that time the audio track buffer can
1308 // still contain data that needs to be drained. The latency only covers the audio HAL
1309 // and kernel buffers. Also the latency does not always include additional delay in the
1310 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001311 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001312
1313 // force restoring the device selection on other active outputs if it differs from the
1314 // one being selected for this output
1315 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001316 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001317 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001318 desc->isActive() &&
1319 outputDesc->sharesHwModuleWith(desc) &&
1320 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001321 setOutputDevice(desc,
1322 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001323 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001324 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001325 }
1326 }
1327 // update the outputs if stopping one with a stream that can affect notification routing
1328 handleNotificationRoutingForStream(stream);
1329 }
1330 return NO_ERROR;
1331 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001332 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001333 return INVALID_OPERATION;
1334 }
1335}
1336
Eric Laurente83b55d2014-11-14 10:06:21 -08001337void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001338 audio_stream_type_t stream __unused,
1339 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001340{
1341 ALOGV("releaseOutput() %d", output);
1342 ssize_t index = mOutputs.indexOfKey(output);
1343 if (index < 0) {
1344 ALOGW("releaseOutput() releasing unknown output %d", output);
1345 return;
1346 }
1347
1348#ifdef AUDIO_POLICY_TEST
1349 int testIndex = testOutputIndex(output);
1350 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001351 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001352 if (outputDesc->isActive()) {
1353 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001354 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001355 mTestOutputs[testIndex] = 0;
1356 }
1357 return;
1358 }
1359#endif //AUDIO_POLICY_TEST
1360
Paul McLeanaa981192015-03-21 09:55:15 -07001361 // Routing
1362 mOutputRoutes.removeRoute(session);
1363
Eric Laurentc75307b2015-03-17 15:29:32 -07001364 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001365 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001366 if (desc->mDirectOpenCount <= 0) {
1367 ALOGW("releaseOutput() invalid open count %d for output %d",
1368 desc->mDirectOpenCount, output);
1369 return;
1370 }
1371 if (--desc->mDirectOpenCount == 0) {
1372 closeOutput(output);
1373 // If effects where present on the output, audioflinger moved them to the primary
1374 // output by default: move them back to the appropriate output.
1375 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001376 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001377 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1378 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001379 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001380 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001381 }
1382 }
1383}
1384
1385
Eric Laurentcaf7f482014-11-25 17:50:47 -08001386status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1387 audio_io_handle_t *input,
1388 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001389 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001390 uint32_t samplingRate,
1391 audio_format_t format,
1392 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001393 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001394 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001395 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001396{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001397 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1398 "session %d, flags %#x",
1399 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001400
Eric Laurentcaf7f482014-11-25 17:50:47 -08001401 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001402 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001403 audio_devices_t device;
1404 // handle legacy remote submix case where the address was not always specified
1405 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001406 audio_source_t inputSource = attr->source;
1407 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001408 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001409
Eric Laurentc447ded2015-01-06 08:47:05 -08001410 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1411 inputSource = AUDIO_SOURCE_MIC;
1412 }
1413 halInputSource = inputSource;
1414
Paul McLean466dc8e2015-04-17 13:15:36 -06001415 // Explicit routing?
1416 sp<DeviceDescriptor> deviceDesc;
1417 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1418 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1419 deviceDesc = mAvailableInputDevices[i];
1420 break;
1421 }
1422 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001423 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001424
Eric Laurentc447ded2015-01-06 08:47:05 -08001425 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001426 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001427 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001428 if (ret != NO_ERROR) {
1429 return ret;
1430 }
1431 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001432 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1433 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001434 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001435 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001436 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001437 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001438 return BAD_VALUE;
1439 }
Eric Laurentc722f302014-12-10 11:21:49 -08001440 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001441 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001442 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1443 // there is an external policy, but this input is attached to a mix of recorders,
1444 // meaning it receives audio injected into the framework, so the recorder doesn't
1445 // know about it and is therefore considered "legacy"
1446 *inputType = API_INPUT_LEGACY;
1447 } else {
1448 // recording a mix of players defined by an external policy, we're rerouting for
1449 // an external policy
1450 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1451 }
Eric Laurentc722f302014-12-10 11:21:49 -08001452 } else if (audio_is_remote_submix_device(device)) {
1453 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001454 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001455 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1456 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001457 } else {
1458 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001459 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001460
Eric Laurent599c7582015-12-07 18:05:55 -08001461 }
1462
1463 *input = getInputForDevice(device, address, session, uid, inputSource,
1464 samplingRate, format, channelMask, flags,
1465 policyMix);
1466 if (*input == AUDIO_IO_HANDLE_NONE) {
1467 mInputRoutes.removeRoute(session);
1468 return INVALID_OPERATION;
1469 }
1470 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1471 return NO_ERROR;
1472}
1473
1474
1475audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1476 String8 address,
1477 audio_session_t session,
1478 uid_t uid,
1479 audio_source_t inputSource,
1480 uint32_t samplingRate,
1481 audio_format_t format,
1482 audio_channel_mask_t channelMask,
1483 audio_input_flags_t flags,
1484 AudioMix *policyMix)
1485{
1486 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1487 audio_source_t halInputSource = inputSource;
1488 bool isSoundTrigger = false;
1489
1490 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1491 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1492 if (index >= 0) {
1493 input = mSoundTriggerSessions.valueFor(session);
1494 isSoundTrigger = true;
1495 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1496 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1497 } else {
1498 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001499 }
1500 }
1501
Andy Hungf129b032015-04-07 13:45:50 -07001502 // find a compatible input profile (not necessarily identical in parameters)
1503 sp<IOProfile> profile;
1504 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001505 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001506 audio_format_t profileFormat = format;
1507 audio_channel_mask_t profileChannelMask = channelMask;
1508 audio_input_flags_t profileFlags = flags;
1509 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001510 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001511 profileSamplingRate, profileFormat, profileChannelMask,
1512 profileFlags);
1513 if (profile != 0) {
1514 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001515 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1516 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001517 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1518 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1519 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001520 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1521 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001522 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001523 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001524 }
Eric Laurente552edb2014-03-10 17:42:56 -07001525 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001526 // Pick input sampling rate if not specified by client
1527 if (samplingRate == 0) {
1528 samplingRate = profileSamplingRate;
1529 }
Eric Laurente552edb2014-03-10 17:42:56 -07001530
Eric Laurent322b4d22015-04-03 15:57:54 -07001531 if (profile->getModuleHandle() == 0) {
1532 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001533 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001534 }
1535
Eric Laurent599c7582015-12-07 18:05:55 -08001536 sp<AudioSession> audioSession = new AudioSession(session,
1537 inputSource,
1538 format,
1539 samplingRate,
1540 channelMask,
1541 flags,
1542 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001543 isSoundTrigger,
1544 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001545
Eric Laurent232f26f2016-02-17 18:06:27 -08001546// TODO enable input reuse
1547#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001548 // reuse an open input if possible
1549 for (size_t i = 0; i < mInputs.size(); i++) {
1550 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001551 // reuse input if it shares the same profile and same sound trigger attribute
1552 if (profile == desc->mProfile &&
1553 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001554
1555 sp<AudioSession> as = desc->getAudioSession(session);
1556 if (as != 0) {
1557 // do not allow unmatching properties on same session
1558 if (as->matches(audioSession)) {
1559 as->changeOpenCount(1);
1560 } else {
1561 ALOGW("getInputForDevice() record with different attributes"
1562 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001563 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001564 }
1565 } else {
1566 desc->addAudioSession(session, audioSession);
1567 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001568 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1569 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001570 }
1571 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001572#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001573
Eric Laurentcf2c0212014-07-25 16:20:43 -07001574 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001575 config.sample_rate = profileSamplingRate;
1576 config.channel_mask = profileChannelMask;
1577 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001578
Eric Laurent322b4d22015-04-03 15:57:54 -07001579 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001580 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001581 &config,
1582 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001583 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001584 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001585 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001586
1587 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001588 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001589 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001590 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001591 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001592 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1593 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001594 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001595 if (input != AUDIO_IO_HANDLE_NONE) {
1596 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001597 }
Eric Laurent599c7582015-12-07 18:05:55 -08001598 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001599 }
1600
Eric Laurent1f2f2232014-06-02 12:01:23 -07001601 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001602 inputDesc->mSamplingRate = profileSamplingRate;
1603 inputDesc->mFormat = profileFormat;
1604 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001605 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001606 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001607 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001608
Eric Laurent599c7582015-12-07 18:05:55 -08001609 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001610 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001611
Eric Laurent599c7582015-12-07 18:05:55 -08001612 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001613}
1614
Eric Laurent4dc68062014-07-28 17:26:49 -07001615status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001616 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001617{
1618 ALOGV("startInput() input %d", input);
1619 ssize_t index = mInputs.indexOfKey(input);
1620 if (index < 0) {
1621 ALOGW("startInput() unknown input %d", input);
1622 return BAD_VALUE;
1623 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001624 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001625
Eric Laurent599c7582015-12-07 18:05:55 -08001626 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1627 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001628 ALOGW("startInput() unknown session %d on input %d", session, input);
1629 return BAD_VALUE;
1630 }
1631
Eric Laurent232f26f2016-02-17 18:06:27 -08001632 // virtual input devices are compatible with other input devices
1633 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001634
Eric Laurent232f26f2016-02-17 18:06:27 -08001635 // for a non-virtual input device, check if there is another (non-virtual) active input
1636 audio_io_handle_t activeInput = mInputs.getActiveInput();
1637 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001638
Eric Laurent232f26f2016-02-17 18:06:27 -08001639 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1640 // otherwise the active input continues and the new input cannot be started.
1641 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1642 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1643 !activeDesc->hasPreemptedSession(session)) {
1644 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1645 //FIXME: consider all active sessions
1646 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1647 audio_session_t activeSession = activeSessions.keyAt(0);
1648 SortedVector<audio_session_t> sessions =
1649 activeDesc->getPreemptedSessions();
1650 sessions.add(activeSession);
1651 inputDesc->setPreemptedSessions(sessions);
1652 stopInput(activeInput, activeSession);
1653 releaseInput(activeInput, activeSession);
1654 } else {
1655 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1656 return INVALID_OPERATION;
1657 }
1658 }
1659
1660 // Do not allow capture if an active voice call is using a software patch and
1661 // the call TX source device is on the same HW module.
1662 // FIXME: would be better to refine to only inputs whose profile connects to the
1663 // call TX device but this information is not in the audio patch
1664 if (mCallTxPatch != 0 &&
1665 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1666 return INVALID_OPERATION;
1667 }
1668 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001669
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001670 // Routing?
1671 mInputRoutes.incRouteActivity(session);
1672
Eric Laurent232f26f2016-02-17 18:06:27 -08001673 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1674 // if input maps to a dynamic policy with an activity listener, notify of state change
1675 if ((inputDesc->mPolicyMix != NULL)
1676 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001677 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001678 MIX_STATE_MIXING);
1679 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001680
Eric Laurent232f26f2016-02-17 18:06:27 -08001681 if (mInputs.activeInputsCount() == 0) {
1682 SoundTrigger::setCaptureState(true);
1683 }
1684 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001685
Eric Laurent232f26f2016-02-17 18:06:27 -08001686 // automatically enable the remote submix output when input is started if not
1687 // used by a policy mix of type MIX_TYPE_RECORDERS
1688 // For remote submix (a virtual device), we open only one input per capture request.
1689 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1690 String8 address = String8("");
1691 if (inputDesc->mPolicyMix == NULL) {
1692 address = String8("0");
1693 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001694 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurentc722f302014-12-10 11:21:49 -08001695 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001696 if (address != "") {
1697 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1698 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1699 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001700 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001701 }
Eric Laurente552edb2014-03-10 17:42:56 -07001702 }
1703
Eric Laurent599c7582015-12-07 18:05:55 -08001704 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001705
Eric Laurent232f26f2016-02-17 18:06:27 -08001706 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001707 return NO_ERROR;
1708}
1709
Eric Laurent4dc68062014-07-28 17:26:49 -07001710status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1711 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001712{
1713 ALOGV("stopInput() input %d", input);
1714 ssize_t index = mInputs.indexOfKey(input);
1715 if (index < 0) {
1716 ALOGW("stopInput() unknown input %d", input);
1717 return BAD_VALUE;
1718 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001719 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001720
Eric Laurent599c7582015-12-07 18:05:55 -08001721 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001722 if (index < 0) {
1723 ALOGW("stopInput() unknown session %d on input %d", session, input);
1724 return BAD_VALUE;
1725 }
1726
Eric Laurent599c7582015-12-07 18:05:55 -08001727 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001728 ALOGW("stopInput() input %d already stopped", input);
1729 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001730 }
1731
Eric Laurent599c7582015-12-07 18:05:55 -08001732 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001733
1734 // Routing?
1735 mInputRoutes.decRouteActivity(session);
1736
Eric Laurent232f26f2016-02-17 18:06:27 -08001737 if (!inputDesc->isActive()) {
1738 // if input maps to a dynamic policy with an activity listener, notify of state change
1739 if ((inputDesc->mPolicyMix != NULL)
1740 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001741 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
Eric Laurent232f26f2016-02-17 18:06:27 -08001742 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001743 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001744
1745 // automatically disable the remote submix output when input is stopped if not
1746 // used by a policy mix of type MIX_TYPE_RECORDERS
1747 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1748 String8 address = String8("");
1749 if (inputDesc->mPolicyMix == NULL) {
1750 address = String8("0");
1751 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001752 address = inputDesc->mPolicyMix->mDeviceAddress;
Eric Laurent232f26f2016-02-17 18:06:27 -08001753 }
1754 if (address != "") {
1755 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1756 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1757 address, "remote-submix");
1758 }
1759 }
1760
1761 resetInputDevice(input);
1762
1763 if (mInputs.activeInputsCount() == 0) {
1764 SoundTrigger::setCaptureState(false);
1765 }
1766 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001767 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001768 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001769}
1770
Eric Laurent4dc68062014-07-28 17:26:49 -07001771void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1772 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001773{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001774
Eric Laurente552edb2014-03-10 17:42:56 -07001775 ALOGV("releaseInput() %d", input);
1776 ssize_t index = mInputs.indexOfKey(input);
1777 if (index < 0) {
1778 ALOGW("releaseInput() releasing unknown input %d", input);
1779 return;
1780 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001781
1782 // Routing
1783 mInputRoutes.removeRoute(session);
1784
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001785 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1786 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001787
Eric Laurent599c7582015-12-07 18:05:55 -08001788 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001789 if (index < 0) {
1790 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1791 return;
1792 }
Eric Laurent599c7582015-12-07 18:05:55 -08001793
1794 if (audioSession->openCount() == 0) {
1795 ALOGW("releaseInput() invalid open count %d on session %d",
1796 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001797 return;
1798 }
Eric Laurent599c7582015-12-07 18:05:55 -08001799
1800 if (audioSession->changeOpenCount(-1) == 0) {
1801 inputDesc->removeAudioSession(session);
1802 }
1803
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001804 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001805 ALOGV("releaseInput() exit > 0");
1806 return;
1807 }
1808
Eric Laurent05b90f82014-08-27 15:32:29 -07001809 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001810 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001811 ALOGV("releaseInput() exit");
1812}
1813
Eric Laurentd4692962014-05-05 18:13:44 -07001814void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001815 bool patchRemoved = false;
1816
Eric Laurentd4692962014-05-05 18:13:44 -07001817 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001818 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001819 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001820 if (patch_index >= 0) {
1821 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07001822 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07001823 mAudioPatches.removeItemsAt(patch_index);
1824 patchRemoved = true;
1825 }
Eric Laurentd4692962014-05-05 18:13:44 -07001826 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1827 }
1828 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001829 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001830 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001831
1832 if (patchRemoved) {
1833 mpClientInterface->onAudioPatchListUpdate();
1834 }
Eric Laurentd4692962014-05-05 18:13:44 -07001835}
1836
Eric Laurente0720872014-03-11 09:30:41 -07001837void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001838 int indexMin,
1839 int indexMax)
1840{
1841 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001842 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001843
1844 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001845 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1846 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001847 continue;
1848 }
1849 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001850 }
Eric Laurente552edb2014-03-10 17:42:56 -07001851}
1852
Eric Laurente0720872014-03-11 09:30:41 -07001853status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001854 int index,
1855 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001856{
1857
François Gaffied1ab2bd2015-12-02 18:20:06 +01001858 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1859 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001860 return BAD_VALUE;
1861 }
1862 if (!audio_is_output_device(device)) {
1863 return BAD_VALUE;
1864 }
1865
1866 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001867 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001868
Eric Laurent1fd372e2016-04-06 14:23:53 -07001869 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07001870 stream, device, index);
1871
Eric Laurent28d09f02016-03-08 10:43:05 -08001872 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001873 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1874 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001875 continue;
1876 }
1877 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1878 }
Eric Laurente552edb2014-03-10 17:42:56 -07001879
Eric Laurent1fd372e2016-04-06 14:23:53 -07001880 // update volume on all outputs and streams matching the following:
1881 // - The requested stream (or a stream matching for volume control) is active on the output
1882 // - The device (or devices) selected by the strategy corresponding to this stream includes
1883 // the requested device
1884 // - For non default requested device, currently selected device on the output is either the
1885 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07001886 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
1887 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07001888 status_t status = NO_ERROR;
1889 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001890 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1891 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001892 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1893 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001894 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001895 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07001896 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
1897 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001898 continue;
1899 }
Eric Laurent794fde22016-03-11 09:50:45 -08001900 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001901 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
Eric Laurent1fd372e2016-04-06 14:23:53 -07001902 if ((curStreamDevice & device) == 0) {
1903 continue;
1904 }
1905 bool applyDefault = false;
Eric Laurent5a2b6292016-04-14 18:05:57 -07001906 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07001907 curStreamDevice |= device;
1908 } else if (!mVolumeCurves->hasVolumeIndexForDevice(
1909 stream, Volume::getDeviceForVolume(curStreamDevice))) {
1910 applyDefault = true;
1911 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001912
Eric Laurent1fd372e2016-04-06 14:23:53 -07001913 if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
Eric Laurentdc462862016-07-19 12:29:53 -07001914 //FIXME: workaround for truncated touch sounds
1915 // delayed volume change for system stream to be removed when the problem is
1916 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08001917 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07001918 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
1919 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08001920 if (volStatus != NO_ERROR) {
1921 status = volStatus;
1922 }
1923 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001924 }
Eric Laurente552edb2014-03-10 17:42:56 -07001925 }
1926 return status;
1927}
1928
Eric Laurente0720872014-03-11 09:30:41 -07001929status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001930 int *index,
1931 audio_devices_t device)
1932{
1933 if (index == NULL) {
1934 return BAD_VALUE;
1935 }
1936 if (!audio_is_output_device(device)) {
1937 return BAD_VALUE;
1938 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07001939 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07001940 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07001941 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07001942 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1943 }
François Gaffiedfd74092015-03-19 12:10:59 +01001944 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001945
François Gaffied1ab2bd2015-12-02 18:20:06 +01001946 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001947 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1948 return NO_ERROR;
1949}
1950
Eric Laurente0720872014-03-11 09:30:41 -07001951audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001952 const SortedVector<audio_io_handle_t>& outputs)
1953{
1954 // select one output among several suitable for global effects.
1955 // The priority is as follows:
1956 // 1: An offloaded output. If the effect ends up not being offloadable,
1957 // AudioFlinger will invalidate the track and the offloaded output
1958 // will be closed causing the effect to be moved to a PCM output.
1959 // 2: A deep buffer output
1960 // 3: the first output in the list
1961
1962 if (outputs.size() == 0) {
1963 return 0;
1964 }
1965
1966 audio_io_handle_t outputOffloaded = 0;
1967 audio_io_handle_t outputDeepBuffer = 0;
1968
1969 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001970 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001971 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001972 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1973 outputOffloaded = outputs[i];
1974 }
1975 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1976 outputDeepBuffer = outputs[i];
1977 }
1978 }
1979
1980 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1981 outputOffloaded, outputDeepBuffer);
1982 if (outputOffloaded != 0) {
1983 return outputOffloaded;
1984 }
1985 if (outputDeepBuffer != 0) {
1986 return outputDeepBuffer;
1987 }
1988
1989 return outputs[0];
1990}
1991
Eric Laurente0720872014-03-11 09:30:41 -07001992audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001993{
1994 // apply simple rule where global effects are attached to the same output as MUSIC streams
1995
Eric Laurent3b73df72014-03-11 09:06:29 -07001996 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001997 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1998 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1999
2000 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
2001 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
2002 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
2003
2004 return output;
2005}
2006
Eric Laurente0720872014-03-11 09:30:41 -07002007status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002008 audio_io_handle_t io,
2009 uint32_t strategy,
2010 int session,
2011 int id)
2012{
2013 ssize_t index = mOutputs.indexOfKey(io);
2014 if (index < 0) {
2015 index = mInputs.indexOfKey(io);
2016 if (index < 0) {
2017 ALOGW("registerEffect() unknown io %d", io);
2018 return INVALID_OPERATION;
2019 }
2020 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002021 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002022}
2023
Eric Laurentc75307b2015-03-17 15:29:32 -07002024bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2025{
Eric Laurent28d09f02016-03-08 10:43:05 -08002026 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002027 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2028 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002029 continue;
2030 }
2031 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2032 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002033 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002034}
2035
2036bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2037{
2038 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2039}
2040
Eric Laurente0720872014-03-11 09:30:41 -07002041bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002042{
2043 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002044 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002045 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002046 return true;
2047 }
2048 }
2049 return false;
2050}
2051
Eric Laurent275e8e92014-11-30 15:14:47 -08002052// Register a list of custom mixes with their attributes and format.
2053// When a mix is registered, corresponding input and output profiles are
2054// added to the remote submix hw module. The profile contains only the
2055// parameters (sampling rate, format...) specified by the mix.
2056// The corresponding input remote submix device is also connected.
2057//
2058// When a remote submix device is connected, the address is checked to select the
2059// appropriate profile and the corresponding input or output stream is opened.
2060//
2061// When capture starts, getInputForAttr() will:
2062// - 1 look for a mix matching the address passed in attribtutes tags if any
2063// - 2 if none found, getDeviceForInputSource() will:
2064// - 2.1 look for a mix matching the attributes source
2065// - 2.2 if none found, default to device selection by policy rules
2066// At this time, the corresponding output remote submix device is also connected
2067// and active playback use cases can be transferred to this mix if needed when reconnecting
2068// after AudioTracks are invalidated
2069//
2070// When playback starts, getOutputForAttr() will:
2071// - 1 look for a mix matching the address passed in attribtutes tags if any
2072// - 2 if none found, look for a mix matching the attributes usage
2073// - 3 if none found, default to device and output selection by policy rules.
2074
2075status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2076{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002077 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2078 status_t res = NO_ERROR;
2079
2080 sp<HwModule> rSubmixModule;
2081 // examine each mix's route type
2082 for (size_t i = 0; i < mixes.size(); i++) {
2083 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2084 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2085 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002086 break;
2087 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002088 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
2089 // Loop back through "remote submix"
2090 if (rSubmixModule == 0) {
2091 for (size_t j = 0; i < mHwModules.size(); j++) {
2092 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2093 && mHwModules[j]->mHandle != 0) {
2094 rSubmixModule = mHwModules[j];
2095 break;
2096 }
2097 }
2098 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002099
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002100 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002101
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002102 if (rSubmixModule == 0) {
2103 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i);
2104 res = INVALID_OPERATION;
2105 break;
2106 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002107
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002108 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002109
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002110 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002111 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002112 res = INVALID_OPERATION;
2113 break;
2114 }
2115 audio_config_t outputConfig = mixes[i].mFormat;
2116 audio_config_t inputConfig = mixes[i].mFormat;
2117 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2118 // stereo and let audio flinger do the channel conversion if needed.
2119 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2120 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2121 rSubmixModule->addOutputProfile(address, &outputConfig,
2122 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2123 rSubmixModule->addInputProfile(address, &inputConfig,
2124 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002125
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002126 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2127 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2128 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2129 address.string(), "remote-submix");
2130 } else {
2131 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2132 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2133 address.string(), "remote-submix");
2134 }
2135 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002136 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002137 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002138 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2139 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002140
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002141 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002142 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2143 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2144 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2145 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2146 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2147 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002148 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2149 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002150 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2151 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002152 } else {
2153 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002154 }
2155 break;
2156 }
2157 }
2158
2159 if (res != NO_ERROR) {
2160 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002161 i, device, address.string());
2162 res = INVALID_OPERATION;
2163 break;
2164 } else if (!foundOutput) {
2165 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2166 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002167 res = INVALID_OPERATION;
2168 break;
2169 }
Eric Laurentc722f302014-12-10 11:21:49 -08002170 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002171 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002172 if (res != NO_ERROR) {
2173 unregisterPolicyMixes(mixes);
2174 }
2175 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002176}
2177
2178status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2179{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002180 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002181 status_t res = NO_ERROR;
2182 sp<HwModule> rSubmixModule;
2183 // examine each mix's route type
Eric Laurent275e8e92014-11-30 15:14:47 -08002184 for (size_t i = 0; i < mixes.size(); i++) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002185 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002186
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002187 if (rSubmixModule == 0) {
2188 for (size_t j = 0; i < mHwModules.size(); j++) {
2189 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0
2190 && mHwModules[j]->mHandle != 0) {
2191 rSubmixModule = mHwModules[j];
2192 break;
2193 }
2194 }
2195 }
2196 if (rSubmixModule == 0) {
2197 res = INVALID_OPERATION;
2198 continue;
2199 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002200
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002201 String8 address = mixes[i].mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002202
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002203 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2204 res = INVALID_OPERATION;
2205 continue;
2206 }
2207
2208 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2209 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2210 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2211 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2212 address.string(), "remote-submix");
2213 }
2214 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2215 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2216 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2217 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2218 address.string(), "remote-submix");
2219 }
2220 rSubmixModule->removeOutputProfile(address);
2221 rSubmixModule->removeInputProfile(address);
2222
2223 } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2224 if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) {
2225 res = INVALID_OPERATION;
2226 continue;
2227 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002228 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002229 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002230 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002231}
2232
Eric Laurente552edb2014-03-10 17:42:56 -07002233
Eric Laurente0720872014-03-11 09:30:41 -07002234status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002235{
2236 const size_t SIZE = 256;
2237 char buffer[SIZE];
2238 String8 result;
2239
2240 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2241 result.append(buffer);
2242
Eric Laurent87ffa392015-05-22 10:32:38 -07002243 snprintf(buffer, SIZE, " Primary Output: %d\n",
2244 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002245 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002246 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002247 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002248 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002249 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002250 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002251 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002252 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002253 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002254 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002255 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002256 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002257 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002258 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002259 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002260 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002261 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002262 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2263 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2264 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002265 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2266 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002267 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2268 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002269
François Gaffie2110e042015-03-24 08:41:51 +01002270 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002271
François Gaffie112b0af2015-11-19 16:13:25 +01002272 mAvailableOutputDevices.dump(fd, String8("Available output"));
2273 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002274 mHwModules.dump(fd);
2275 mOutputs.dump(fd);
2276 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002277 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002278 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002279 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002280
2281 return NO_ERROR;
2282}
2283
2284// This function checks for the parameters which can be offloaded.
2285// This can be enhanced depending on the capability of the DSP and policy
2286// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002287bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002288{
2289 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002290 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002291 offloadInfo.sample_rate, offloadInfo.channel_mask,
2292 offloadInfo.format,
2293 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2294 offloadInfo.has_video);
2295
Andy Hung2ddee192015-12-18 17:34:44 -08002296 if (mMasterMono) {
2297 return false; // no offloading if mono is set.
2298 }
2299
Eric Laurente552edb2014-03-10 17:42:56 -07002300 // Check if offload has been disabled
2301 char propValue[PROPERTY_VALUE_MAX];
2302 if (property_get("audio.offload.disable", propValue, "0")) {
2303 if (atoi(propValue) != 0) {
2304 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2305 return false;
2306 }
2307 }
2308
2309 // Check if stream type is music, then only allow offload as of now.
2310 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2311 {
2312 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2313 return false;
2314 }
2315
2316 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002317 const bool allowOffloadWithVideo =
2318 property_get_bool("audio.offload.video", false /* default_value */);
2319 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002320 ALOGV("isOffloadSupported: has_video == true, returning false");
2321 return false;
2322 }
2323
2324 //If duration is less than minimum value defined in property, return false
2325 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2326 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2327 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2328 return false;
2329 }
2330 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2331 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2332 return false;
2333 }
2334
2335 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2336 // creating an offloaded track and tearing it down immediately after start when audioflinger
2337 // detects there is an active non offloadable effect.
2338 // FIXME: We should check the audio session here but we do not have it in this context.
2339 // This may prevent offloading in rare situations where effects are left active by apps
2340 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002341 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002342 return false;
2343 }
2344
2345 // See if there is a profile to support this.
2346 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002347 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002348 offloadInfo.sample_rate,
2349 offloadInfo.format,
2350 offloadInfo.channel_mask,
2351 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002352 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2353 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002354}
2355
Eric Laurent6a94d692014-05-20 11:18:06 -07002356status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2357 audio_port_type_t type,
2358 unsigned int *num_ports,
2359 struct audio_port *ports,
2360 unsigned int *generation)
2361{
2362 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2363 generation == NULL) {
2364 return BAD_VALUE;
2365 }
2366 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2367 if (ports == NULL) {
2368 *num_ports = 0;
2369 }
2370
2371 size_t portsWritten = 0;
2372 size_t portsMax = *num_ports;
2373 *num_ports = 0;
2374 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002375 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2376 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002377 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002378 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
2379 if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) {
2380 continue;
2381 }
2382 if (portsWritten < portsMax) {
2383 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2384 }
2385 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002386 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002387 }
2388 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002389 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2390 if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) {
2391 continue;
2392 }
2393 if (portsWritten < portsMax) {
2394 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2395 }
2396 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002397 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002398 }
2399 }
2400 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2401 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2402 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2403 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2404 }
2405 *num_ports += mInputs.size();
2406 }
2407 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002408 size_t numOutputs = 0;
2409 for (size_t i = 0; i < mOutputs.size(); i++) {
2410 if (!mOutputs[i]->isDuplicated()) {
2411 numOutputs++;
2412 if (portsWritten < portsMax) {
2413 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2414 }
2415 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002416 }
Eric Laurent84c70242014-06-23 08:46:27 -07002417 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002418 }
2419 }
2420 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002421 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002422 return NO_ERROR;
2423}
2424
2425status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2426{
2427 return NO_ERROR;
2428}
2429
Eric Laurent6a94d692014-05-20 11:18:06 -07002430status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2431 audio_patch_handle_t *handle,
2432 uid_t uid)
2433{
2434 ALOGV("createAudioPatch()");
2435
2436 if (handle == NULL || patch == NULL) {
2437 return BAD_VALUE;
2438 }
2439 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2440
Eric Laurent874c42872014-08-08 15:13:39 -07002441 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2442 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2443 return BAD_VALUE;
2444 }
2445 // only one source per audio patch supported for now
2446 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002447 return INVALID_OPERATION;
2448 }
Eric Laurent874c42872014-08-08 15:13:39 -07002449
2450 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002451 return INVALID_OPERATION;
2452 }
Eric Laurent874c42872014-08-08 15:13:39 -07002453 for (size_t i = 0; i < patch->num_sinks; i++) {
2454 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2455 return INVALID_OPERATION;
2456 }
2457 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002458
2459 sp<AudioPatch> patchDesc;
2460 ssize_t index = mAudioPatches.indexOfKey(*handle);
2461
Eric Laurent6a94d692014-05-20 11:18:06 -07002462 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2463 patch->sources[0].role,
2464 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002465#if LOG_NDEBUG == 0
2466 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002467 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002468 patch->sinks[i].role,
2469 patch->sinks[i].type);
2470 }
2471#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002472
2473 if (index >= 0) {
2474 patchDesc = mAudioPatches.valueAt(index);
2475 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2476 mUidCached, patchDesc->mUid, uid);
2477 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2478 return INVALID_OPERATION;
2479 }
2480 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002481 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002482 }
2483
2484 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002485 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002486 if (outputDesc == NULL) {
2487 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2488 return BAD_VALUE;
2489 }
Eric Laurent84c70242014-06-23 08:46:27 -07002490 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2491 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002492 if (patchDesc != 0) {
2493 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2494 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2495 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2496 return BAD_VALUE;
2497 }
2498 }
Eric Laurent874c42872014-08-08 15:13:39 -07002499 DeviceVector devices;
2500 for (size_t i = 0; i < patch->num_sinks; i++) {
2501 // Only support mix to devices connection
2502 // TODO add support for mix to mix connection
2503 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2504 ALOGV("createAudioPatch() source mix but sink is not a device");
2505 return INVALID_OPERATION;
2506 }
2507 sp<DeviceDescriptor> devDesc =
2508 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2509 if (devDesc == 0) {
2510 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2511 return BAD_VALUE;
2512 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002513
François Gaffie53615e22015-03-19 09:24:12 +01002514 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002515 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002516 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002517 NULL, // updatedSamplingRate
2518 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002519 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002520 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002521 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002522 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002523 ALOGV("createAudioPatch() profile not supported for device %08x",
2524 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002525 return INVALID_OPERATION;
2526 }
2527 devices.add(devDesc);
2528 }
2529 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002530 return INVALID_OPERATION;
2531 }
Eric Laurent874c42872014-08-08 15:13:39 -07002532
Eric Laurent6a94d692014-05-20 11:18:06 -07002533 // TODO: reconfigure output format and channels here
2534 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002535 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002536 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002537 index = mAudioPatches.indexOfKey(*handle);
2538 if (index >= 0) {
2539 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2540 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2541 }
2542 patchDesc = mAudioPatches.valueAt(index);
2543 patchDesc->mUid = uid;
2544 ALOGV("createAudioPatch() success");
2545 } else {
2546 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2547 return INVALID_OPERATION;
2548 }
2549 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2550 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2551 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002552 // only one sink supported when connecting an input device to a mix
2553 if (patch->num_sinks > 1) {
2554 return INVALID_OPERATION;
2555 }
François Gaffie53615e22015-03-19 09:24:12 +01002556 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002557 if (inputDesc == NULL) {
2558 return BAD_VALUE;
2559 }
2560 if (patchDesc != 0) {
2561 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2562 return BAD_VALUE;
2563 }
2564 }
2565 sp<DeviceDescriptor> devDesc =
2566 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2567 if (devDesc == 0) {
2568 return BAD_VALUE;
2569 }
2570
François Gaffie53615e22015-03-19 09:24:12 +01002571 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002572 devDesc->mAddress,
2573 patch->sinks[0].sample_rate,
2574 NULL, /*updatedSampleRate*/
2575 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002576 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002577 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002578 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002579 // FIXME for the parameter type,
2580 // and the NONE
2581 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002582 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002583 return INVALID_OPERATION;
2584 }
2585 // TODO: reconfigure output format and channels here
2586 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002587 devDesc->type(), inputDesc->mIoHandle);
2588 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002589 index = mAudioPatches.indexOfKey(*handle);
2590 if (index >= 0) {
2591 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2592 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2593 }
2594 patchDesc = mAudioPatches.valueAt(index);
2595 patchDesc->mUid = uid;
2596 ALOGV("createAudioPatch() success");
2597 } else {
2598 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2599 return INVALID_OPERATION;
2600 }
2601 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2602 // device to device connection
2603 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002604 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002605 return BAD_VALUE;
2606 }
2607 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002608 sp<DeviceDescriptor> srcDeviceDesc =
2609 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002610 if (srcDeviceDesc == 0) {
2611 return BAD_VALUE;
2612 }
Eric Laurent874c42872014-08-08 15:13:39 -07002613
Eric Laurent6a94d692014-05-20 11:18:06 -07002614 //update source and sink with our own data as the data passed in the patch may
2615 // be incomplete.
2616 struct audio_patch newPatch = *patch;
2617 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002618
Eric Laurent874c42872014-08-08 15:13:39 -07002619 for (size_t i = 0; i < patch->num_sinks; i++) {
2620 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2621 ALOGV("createAudioPatch() source device but one sink is not a device");
2622 return INVALID_OPERATION;
2623 }
2624
2625 sp<DeviceDescriptor> sinkDeviceDesc =
2626 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2627 if (sinkDeviceDesc == 0) {
2628 return BAD_VALUE;
2629 }
2630 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2631
Eric Laurent3bcf8592015-04-03 12:13:24 -07002632 // create a software bridge in PatchPanel if:
2633 // - source and sink devices are on differnt HW modules OR
2634 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002635 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002636 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002637 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002638 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002639 return INVALID_OPERATION;
2640 }
Eric Laurent874c42872014-08-08 15:13:39 -07002641 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002642 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002643 // if the sink device is reachable via an opened output stream, request to go via
2644 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002645 audio_io_handle_t output = selectOutput(outputs,
2646 AUDIO_OUTPUT_FLAG_NONE,
2647 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002648 if (output != AUDIO_IO_HANDLE_NONE) {
2649 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2650 if (outputDesc->isDuplicated()) {
2651 return INVALID_OPERATION;
2652 }
2653 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002654 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002655 newPatch.num_sources = 2;
2656 }
Eric Laurent83b88082014-06-20 18:31:16 -07002657 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002658 }
2659 // TODO: check from routing capabilities in config file and other conflicting patches
2660
2661 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2662 if (index >= 0) {
2663 afPatchHandle = patchDesc->mAfPatchHandle;
2664 }
2665
2666 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2667 &afPatchHandle,
2668 0);
2669 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2670 status, afPatchHandle);
2671 if (status == NO_ERROR) {
2672 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002673 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002674 addAudioPatch(patchDesc->mHandle, patchDesc);
2675 } else {
2676 patchDesc->mPatch = newPatch;
2677 }
2678 patchDesc->mAfPatchHandle = afPatchHandle;
2679 *handle = patchDesc->mHandle;
2680 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002681 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002682 } else {
2683 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2684 status);
2685 return INVALID_OPERATION;
2686 }
2687 } else {
2688 return BAD_VALUE;
2689 }
2690 } else {
2691 return BAD_VALUE;
2692 }
2693 return NO_ERROR;
2694}
2695
2696status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2697 uid_t uid)
2698{
2699 ALOGV("releaseAudioPatch() patch %d", handle);
2700
2701 ssize_t index = mAudioPatches.indexOfKey(handle);
2702
2703 if (index < 0) {
2704 return BAD_VALUE;
2705 }
2706 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2707 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2708 mUidCached, patchDesc->mUid, uid);
2709 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2710 return INVALID_OPERATION;
2711 }
2712
2713 struct audio_patch *patch = &patchDesc->mPatch;
2714 patchDesc->mUid = mUidCached;
2715 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002716 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002717 if (outputDesc == NULL) {
2718 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2719 return BAD_VALUE;
2720 }
2721
Eric Laurentc75307b2015-03-17 15:29:32 -07002722 setOutputDevice(outputDesc,
2723 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002724 true,
2725 0,
2726 NULL);
2727 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2728 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002729 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002730 if (inputDesc == NULL) {
2731 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2732 return BAD_VALUE;
2733 }
2734 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002735 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002736 true,
2737 NULL);
2738 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002739 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2740 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2741 status, patchDesc->mAfPatchHandle);
2742 removeAudioPatch(patchDesc->mHandle);
2743 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002744 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002745 } else {
2746 return BAD_VALUE;
2747 }
2748 } else {
2749 return BAD_VALUE;
2750 }
2751 return NO_ERROR;
2752}
2753
2754status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2755 struct audio_patch *patches,
2756 unsigned int *generation)
2757{
François Gaffie53615e22015-03-19 09:24:12 +01002758 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002759 return BAD_VALUE;
2760 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002761 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002762 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002763}
2764
Eric Laurente1715a42014-05-20 11:30:42 -07002765status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002766{
Eric Laurente1715a42014-05-20 11:30:42 -07002767 ALOGV("setAudioPortConfig()");
2768
2769 if (config == NULL) {
2770 return BAD_VALUE;
2771 }
2772 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2773 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002774 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2775 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002776 }
2777
Eric Laurenta121f902014-06-03 13:32:54 -07002778 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002779 if (config->type == AUDIO_PORT_TYPE_MIX) {
2780 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002781 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002782 if (outputDesc == NULL) {
2783 return BAD_VALUE;
2784 }
Eric Laurent84c70242014-06-23 08:46:27 -07002785 ALOG_ASSERT(!outputDesc->isDuplicated(),
2786 "setAudioPortConfig() called on duplicated output %d",
2787 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002788 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002789 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002790 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002791 if (inputDesc == NULL) {
2792 return BAD_VALUE;
2793 }
Eric Laurenta121f902014-06-03 13:32:54 -07002794 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002795 } else {
2796 return BAD_VALUE;
2797 }
2798 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2799 sp<DeviceDescriptor> deviceDesc;
2800 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2801 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2802 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2803 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2804 } else {
2805 return BAD_VALUE;
2806 }
2807 if (deviceDesc == NULL) {
2808 return BAD_VALUE;
2809 }
Eric Laurenta121f902014-06-03 13:32:54 -07002810 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002811 } else {
2812 return BAD_VALUE;
2813 }
2814
Eric Laurenta121f902014-06-03 13:32:54 -07002815 struct audio_port_config backupConfig;
2816 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2817 if (status == NO_ERROR) {
2818 struct audio_port_config newConfig;
2819 audioPortConfig->toAudioPortConfig(&newConfig, config);
2820 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002821 }
Eric Laurenta121f902014-06-03 13:32:54 -07002822 if (status != NO_ERROR) {
2823 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002824 }
Eric Laurente1715a42014-05-20 11:30:42 -07002825
2826 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002827}
2828
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002829void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2830{
Eric Laurentd60560a2015-04-10 11:31:20 -07002831 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002832 clearAudioPatches(uid);
2833 clearSessionRoutes(uid);
2834}
2835
Eric Laurent6a94d692014-05-20 11:18:06 -07002836void AudioPolicyManager::clearAudioPatches(uid_t uid)
2837{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002838 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002839 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2840 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002841 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002842 }
2843 }
2844}
2845
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002846void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2847 audio_io_handle_t ouptutToSkip)
2848{
2849 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2850 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2851 for (size_t j = 0; j < mOutputs.size(); j++) {
2852 if (mOutputs.keyAt(j) == ouptutToSkip) {
2853 continue;
2854 }
2855 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2856 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2857 continue;
2858 }
2859 // If the default device for this strategy is on another output mix,
2860 // invalidate all tracks in this strategy to force re connection.
2861 // Otherwise select new device on the output mix.
2862 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002863 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002864 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2865 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2866 }
2867 }
2868 } else {
2869 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2870 setOutputDevice(outputDesc, newDevice, false);
2871 }
2872 }
2873}
2874
2875void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2876{
2877 // remove output routes associated with this uid
2878 SortedVector<routing_strategy> affectedStrategies;
2879 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2880 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2881 if (route->mUid == uid) {
2882 mOutputRoutes.removeItemsAt(i);
2883 if (route->mDeviceDescriptor != 0) {
2884 affectedStrategies.add(getStrategy(route->mStreamType));
2885 }
2886 }
2887 }
2888 // reroute outputs if necessary
2889 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2890 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2891 }
2892
2893 // remove input routes associated with this uid
2894 SortedVector<audio_source_t> affectedSources;
2895 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2896 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2897 if (route->mUid == uid) {
2898 mInputRoutes.removeItemsAt(i);
2899 if (route->mDeviceDescriptor != 0) {
2900 affectedSources.add(route->mSource);
2901 }
2902 }
2903 }
2904 // reroute inputs if necessary
2905 SortedVector<audio_io_handle_t> inputsToClose;
2906 for (size_t i = 0; i < mInputs.size(); i++) {
2907 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002908 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002909 inputsToClose.add(inputDesc->mIoHandle);
2910 }
2911 }
2912 for (size_t i = 0; i < inputsToClose.size(); i++) {
2913 closeInput(inputsToClose[i]);
2914 }
2915}
2916
Eric Laurentd60560a2015-04-10 11:31:20 -07002917void AudioPolicyManager::clearAudioSources(uid_t uid)
2918{
2919 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2920 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2921 if (sourceDesc->mUid == uid) {
2922 stopAudioSource(mAudioSources.keyAt(i));
2923 }
2924 }
2925}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002926
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002927status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2928 audio_io_handle_t *ioHandle,
2929 audio_devices_t *device)
2930{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002931 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2932 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002933 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002934
François Gaffiedf372692015-03-19 10:43:27 +01002935 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002936}
2937
Eric Laurentd60560a2015-04-10 11:31:20 -07002938status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2939 const audio_attributes_t *attributes,
2940 audio_io_handle_t *handle,
2941 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002942{
Eric Laurentd60560a2015-04-10 11:31:20 -07002943 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2944 if (source == NULL || attributes == NULL || handle == NULL) {
2945 return BAD_VALUE;
2946 }
2947
2948 *handle = AUDIO_IO_HANDLE_NONE;
2949
2950 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2951 source->type != AUDIO_PORT_TYPE_DEVICE) {
2952 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2953 return INVALID_OPERATION;
2954 }
2955
2956 sp<DeviceDescriptor> srcDeviceDesc =
2957 mAvailableInputDevices.getDevice(source->ext.device.type,
2958 String8(source->ext.device.address));
2959 if (srcDeviceDesc == 0) {
2960 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2961 return BAD_VALUE;
2962 }
2963 sp<AudioSourceDescriptor> sourceDesc =
2964 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2965
2966 struct audio_patch dummyPatch;
2967 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2968 sourceDesc->mPatchDesc = patchDesc;
2969
2970 status_t status = connectAudioSource(sourceDesc);
2971 if (status == NO_ERROR) {
2972 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2973 *handle = sourceDesc->getHandle();
2974 }
2975 return status;
2976}
2977
2978status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2979{
2980 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2981
2982 // make sure we only have one patch per source.
2983 disconnectAudioSource(sourceDesc);
2984
2985 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002986 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002987 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2988
2989 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2990 sp<DeviceDescriptor> sinkDeviceDesc =
2991 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2992
2993 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2994 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2995
2996 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2997 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002998 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002999 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3000 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3001 // create patch between src device and output device
3002 // create Hwoutput and add to mHwOutputs
3003 } else {
3004 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3005 audio_io_handle_t output =
3006 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3007 if (output == AUDIO_IO_HANDLE_NONE) {
3008 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3009 return INVALID_OPERATION;
3010 }
3011 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3012 if (outputDesc->isDuplicated()) {
3013 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3014 return INVALID_OPERATION;
3015 }
3016 // create a special patch with no sink and two sources:
3017 // - the second source indicates to PatchPanel through which output mix this patch should
3018 // be connected as well as the stream type for volume control
3019 // - the sink is defined by whatever output device is currently selected for the output
3020 // though which this patch is routed.
3021 patch->num_sinks = 0;
3022 patch->num_sources = 2;
3023 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3024 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3025 patch->sources[1].ext.mix.usecase.stream = stream;
3026 status_t status = mpClientInterface->createAudioPatch(patch,
3027 &afPatchHandle,
3028 0);
3029 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3030 status, afPatchHandle);
3031 if (status != NO_ERROR) {
3032 ALOGW("%s patch panel could not connect device patch, error %d",
3033 __FUNCTION__, status);
3034 return INVALID_OPERATION;
3035 }
3036 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003037 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003038
3039 if (status != NO_ERROR) {
3040 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3041 return status;
3042 }
3043 sourceDesc->mSwOutput = outputDesc;
3044 if (delayMs != 0) {
3045 usleep(delayMs * 1000);
3046 }
3047 }
3048
3049 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3050 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3051
3052 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003053}
3054
Eric Laurent493404d2015-04-21 15:07:36 -07003055status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003056{
Eric Laurentd60560a2015-04-10 11:31:20 -07003057 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3058 ALOGV("%s handle %d", __FUNCTION__, handle);
3059 if (sourceDesc == 0) {
3060 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3061 return BAD_VALUE;
3062 }
3063 status_t status = disconnectAudioSource(sourceDesc);
3064
3065 mAudioSources.removeItem(handle);
3066 return status;
3067}
3068
Andy Hung2ddee192015-12-18 17:34:44 -08003069status_t AudioPolicyManager::setMasterMono(bool mono)
3070{
3071 if (mMasterMono == mono) {
3072 return NO_ERROR;
3073 }
3074 mMasterMono = mono;
3075 // if enabling mono we close all offloaded devices, which will invalidate the
3076 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3077 // for recreating the new AudioTrack as non-offloaded PCM.
3078 //
3079 // If disabling mono, we leave all tracks as is: we don't know which clients
3080 // and tracks are able to be recreated as offloaded. The next "song" should
3081 // play back offloaded.
3082 if (mMasterMono) {
3083 Vector<audio_io_handle_t> offloaded;
3084 for (size_t i = 0; i < mOutputs.size(); ++i) {
3085 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3086 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3087 offloaded.push(desc->mIoHandle);
3088 }
3089 }
3090 for (size_t i = 0; i < offloaded.size(); ++i) {
3091 closeOutput(offloaded[i]);
3092 }
3093 }
3094 // update master mono for all remaining outputs
3095 for (size_t i = 0; i < mOutputs.size(); ++i) {
3096 updateMono(mOutputs.keyAt(i));
3097 }
3098 return NO_ERROR;
3099}
3100
3101status_t AudioPolicyManager::getMasterMono(bool *mono)
3102{
3103 *mono = mMasterMono;
3104 return NO_ERROR;
3105}
3106
Eric Laurentd60560a2015-04-10 11:31:20 -07003107status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3108{
3109 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3110
3111 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3112 if (patchDesc == 0) {
3113 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3114 sourceDesc->mPatchDesc->mHandle);
3115 return BAD_VALUE;
3116 }
3117 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3118
Eric Laurent28d09f02016-03-08 10:43:05 -08003119 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003120 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3121 if (swOutputDesc != 0) {
3122 stopSource(swOutputDesc, stream, false);
3123 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3124 } else {
3125 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3126 if (hwOutputDesc != 0) {
3127 // release patch between src device and output device
3128 // close Hwoutput and remove from mHwOutputs
3129 } else {
3130 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3131 }
3132 }
3133 return NO_ERROR;
3134}
3135
3136sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3137 audio_io_handle_t output, routing_strategy strategy)
3138{
3139 sp<AudioSourceDescriptor> source;
3140 for (size_t i = 0; i < mAudioSources.size(); i++) {
3141 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3142 routing_strategy sourceStrategy =
3143 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3144 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3145 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3146 source = sourceDesc;
3147 break;
3148 }
3149 }
3150 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003151}
3152
Eric Laurente552edb2014-03-10 17:42:56 -07003153// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003154// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003155// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003156uint32_t AudioPolicyManager::nextAudioPortGeneration()
3157{
3158 return android_atomic_inc(&mAudioPortGeneration);
3159}
3160
Eric Laurente0720872014-03-11 09:30:41 -07003161AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003162 :
3163#ifdef AUDIO_POLICY_TEST
3164 Thread(false),
3165#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003166 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003167 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003168 mAudioPortGeneration(1),
3169 mBeaconMuteRefCount(0),
3170 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003171 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003172 mTtsOutputAvailable(false),
3173 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003174{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003175 mUidCached = getuid();
3176 mpClientInterface = clientInterface;
3177
3178 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3179 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3180 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3181 bool speakerDrcEnabled = false;
3182
3183#ifdef USE_XML_AUDIO_POLICY_CONF
3184 mVolumeCurves = new VolumeCurvesCollection();
3185 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3186 mDefaultOutputDevice, speakerDrcEnabled,
3187 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3188 PolicySerializer serializer;
3189 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3190#else
3191 mVolumeCurves = new StreamDescriptorCollection();
3192 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3193 mDefaultOutputDevice, speakerDrcEnabled);
3194 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3195 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3196#endif
3197 ALOGE("could not load audio policy configuration file, setting defaults");
3198 config.setDefault();
3199 }
3200 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3201 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3202
3203 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003204 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3205 if (!engineInstance) {
3206 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3207 return;
3208 }
3209 // Retrieve the Policy Manager Interface
3210 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3211 if (mEngine == NULL) {
3212 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3213 return;
3214 }
3215 mEngine->setObserver(this);
3216 status_t status = mEngine->initCheck();
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07003217 (void) status;
François Gaffie2110e042015-03-24 08:41:51 +01003218 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3219
Eric Laurent3a4311c2014-03-17 12:00:47 -07003220 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003221 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003222 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3223 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003224 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003225 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003226 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003227 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003228 continue;
3229 }
3230 // open all output streams needed to access attached devices
3231 // except for direct output streams that are only opened when they are actually
3232 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003233 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003234 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3235 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003236 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003237
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003238 if (!outProfile->hasSupportedDevices()) {
3239 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003240 continue;
3241 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003242 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003243 mTtsOutputAvailable = true;
3244 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003245
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003246 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003247 continue;
3248 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003249 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003250 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3251 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003252 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003253 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003254 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003255 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003256 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003257 if ((profileType & outputDeviceTypes) == 0) {
3258 continue;
3259 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003260 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3261 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003262 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3263 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3264 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3265 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003266
3267 outputDesc->mDevice = profileType;
3268 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3269 config.sample_rate = outputDesc->mSamplingRate;
3270 config.channel_mask = outputDesc->mChannelMask;
3271 config.format = outputDesc->mFormat;
3272 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003273 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003274 &output,
3275 &config,
3276 &outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003277 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003278 &outputDesc->mLatency,
3279 outputDesc->mFlags);
3280
3281 if (status != NO_ERROR) {
3282 ALOGW("Cannot open output stream for device %08x on hw module %s",
3283 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003284 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003285 } else {
3286 outputDesc->mSamplingRate = config.sample_rate;
3287 outputDesc->mChannelMask = config.channel_mask;
3288 outputDesc->mFormat = config.format;
3289
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003290 for (size_t k = 0; k < supportedDevices.size(); k++) {
3291 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003292 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003293 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3294 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003295 }
3296 }
3297 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003298 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003299 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003300 }
3301 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003302 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003303 outputDesc->mDevice,
Eric Laurentc40d9692016-04-13 19:14:13 -07003304 true,
3305 0,
3306 NULL,
3307 address.string());
Eric Laurentd78f1532014-09-16 16:38:20 -07003308 }
Eric Laurente552edb2014-03-10 17:42:56 -07003309 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003310 // open input streams needed to access attached devices to validate
3311 // mAvailableInputDevices list
3312 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3313 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003314 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003315
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003316 if (!inProfile->hasSupportedDevices()) {
3317 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003318 continue;
3319 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003320 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003321 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003322 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3323
Eric Laurentd78f1532014-09-16 16:38:20 -07003324 if ((profileType & inputDeviceTypes) == 0) {
3325 continue;
3326 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003327 sp<AudioInputDescriptor> inputDesc =
3328 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003329
Eric Laurentd78f1532014-09-16 16:38:20 -07003330 inputDesc->mDevice = profileType;
3331
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003332 // find the address
3333 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3334 // the inputs vector must be of size 1, but we don't want to crash here
3335 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3336 : String8("");
3337 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3338 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3339
Eric Laurentd78f1532014-09-16 16:38:20 -07003340 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3341 config.sample_rate = inputDesc->mSamplingRate;
3342 config.channel_mask = inputDesc->mChannelMask;
3343 config.format = inputDesc->mFormat;
3344 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003345 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003346 &input,
3347 &config,
3348 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003349 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003350 AUDIO_SOURCE_MIC,
3351 AUDIO_INPUT_FLAG_NONE);
3352
3353 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003354 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3355 for (size_t k = 0; k < supportedDevices.size(); k++) {
3356 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003357 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003358 if (index >= 0) {
3359 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3360 if (!devDesc->isAttached()) {
3361 devDesc->attach(mHwModules[i]);
3362 devDesc->importAudioPort(inProfile);
3363 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003364 }
3365 }
3366 mpClientInterface->closeInput(input);
3367 } else {
3368 ALOGW("Cannot open input stream for device %08x on hw module %s",
3369 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003370 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003371 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003372 }
3373 }
3374 // make sure all attached devices have been allocated a unique ID
3375 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003376 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003377 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003378 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3379 continue;
3380 }
François Gaffie2110e042015-03-24 08:41:51 +01003381 // The device is now validated and can be appended to the available devices of the engine
3382 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3383 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003384 i++;
3385 }
3386 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003387 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003388 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003389 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3390 continue;
3391 }
François Gaffie2110e042015-03-24 08:41:51 +01003392 // The device is now validated and can be appended to the available devices of the engine
3393 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3394 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003395 i++;
3396 }
3397 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003398 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003399 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003400 }
Eric Laurente552edb2014-03-10 17:42:56 -07003401
3402 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3403
3404 updateDevicesAndOutputs();
3405
3406#ifdef AUDIO_POLICY_TEST
3407 if (mPrimaryOutput != 0) {
3408 AudioParameter outputCmd = AudioParameter();
3409 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003410 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003411
3412 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3413 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003414 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3415 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003416 mTestLatencyMs = 0;
3417 mCurOutput = 0;
3418 mDirectOutput = false;
3419 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3420 mTestOutputs[i] = 0;
3421 }
3422
3423 const size_t SIZE = 256;
3424 char buffer[SIZE];
3425 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3426 run(buffer, ANDROID_PRIORITY_AUDIO);
3427 }
3428#endif //AUDIO_POLICY_TEST
3429}
3430
Eric Laurente0720872014-03-11 09:30:41 -07003431AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003432{
3433#ifdef AUDIO_POLICY_TEST
3434 exit();
3435#endif //AUDIO_POLICY_TEST
3436 for (size_t i = 0; i < mOutputs.size(); i++) {
3437 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003438 }
3439 for (size_t i = 0; i < mInputs.size(); i++) {
3440 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003441 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003442 mAvailableOutputDevices.clear();
3443 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003444 mOutputs.clear();
3445 mInputs.clear();
3446 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003447}
3448
Eric Laurente0720872014-03-11 09:30:41 -07003449status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003450{
Eric Laurent87ffa392015-05-22 10:32:38 -07003451 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003452}
3453
3454#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003455bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003456{
3457 ALOGV("entering threadLoop()");
3458 while (!exitPending())
3459 {
3460 String8 command;
3461 int valueInt;
3462 String8 value;
3463
3464 Mutex::Autolock _l(mLock);
3465 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3466
3467 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3468 AudioParameter param = AudioParameter(command);
3469
3470 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3471 valueInt != 0) {
3472 ALOGV("Test command %s received", command.string());
3473 String8 target;
3474 if (param.get(String8("target"), target) != NO_ERROR) {
3475 target = "Manager";
3476 }
3477 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3478 param.remove(String8("test_cmd_policy_output"));
3479 mCurOutput = valueInt;
3480 }
3481 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3482 param.remove(String8("test_cmd_policy_direct"));
3483 if (value == "false") {
3484 mDirectOutput = false;
3485 } else if (value == "true") {
3486 mDirectOutput = true;
3487 }
3488 }
3489 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3490 param.remove(String8("test_cmd_policy_input"));
3491 mTestInput = valueInt;
3492 }
3493
3494 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3495 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003496 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003497 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003498 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003499 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003500 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003501 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003502 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003503 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003504 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003505 if (target == "Manager") {
3506 mTestFormat = format;
3507 } else if (mTestOutputs[mCurOutput] != 0) {
3508 AudioParameter outputParam = AudioParameter();
3509 outputParam.addInt(String8("format"), format);
3510 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3511 }
3512 }
3513 }
3514 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3515 param.remove(String8("test_cmd_policy_channels"));
3516 int channels = 0;
3517
3518 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003519 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003520 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003521 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003522 }
3523 if (channels != 0) {
3524 if (target == "Manager") {
3525 mTestChannels = channels;
3526 } else if (mTestOutputs[mCurOutput] != 0) {
3527 AudioParameter outputParam = AudioParameter();
3528 outputParam.addInt(String8("channels"), channels);
3529 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3530 }
3531 }
3532 }
3533 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3534 param.remove(String8("test_cmd_policy_sampleRate"));
3535 if (valueInt >= 0 && valueInt <= 96000) {
3536 int samplingRate = valueInt;
3537 if (target == "Manager") {
3538 mTestSamplingRate = samplingRate;
3539 } else if (mTestOutputs[mCurOutput] != 0) {
3540 AudioParameter outputParam = AudioParameter();
3541 outputParam.addInt(String8("sampling_rate"), samplingRate);
3542 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3543 }
3544 }
3545 }
3546
3547 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3548 param.remove(String8("test_cmd_policy_reopen"));
3549
Eric Laurentc75307b2015-03-17 15:29:32 -07003550 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003551
Eric Laurentc75307b2015-03-17 15:29:32 -07003552 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003553
Eric Laurentc75307b2015-03-17 15:29:32 -07003554 removeOutput(mPrimaryOutput->mIoHandle);
3555 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3556 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003557 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003558 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3559 config.sample_rate = outputDesc->mSamplingRate;
3560 config.channel_mask = outputDesc->mChannelMask;
3561 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003562 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003563 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003564 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003565 &config,
3566 &outputDesc->mDevice,
3567 String8(""),
3568 &outputDesc->mLatency,
3569 outputDesc->mFlags);
3570 if (status != NO_ERROR) {
3571 ALOGE("Failed to reopen hardware output stream, "
3572 "samplingRate: %d, format %d, channels %d",
3573 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003574 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003575 outputDesc->mSamplingRate = config.sample_rate;
3576 outputDesc->mChannelMask = config.channel_mask;
3577 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003578 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003579 AudioParameter outputCmd = AudioParameter();
3580 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003581 mpClientInterface->setParameters(handle, outputCmd.toString());
3582 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003583 }
3584 }
3585
3586
3587 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3588 }
3589 }
3590 return false;
3591}
3592
Eric Laurente0720872014-03-11 09:30:41 -07003593void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003594{
3595 {
3596 AutoMutex _l(mLock);
3597 requestExit();
3598 mWaitWorkCV.signal();
3599 }
3600 requestExitAndWait();
3601}
3602
Eric Laurente0720872014-03-11 09:30:41 -07003603int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003604{
3605 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3606 if (output == mTestOutputs[i]) return i;
3607 }
3608 return 0;
3609}
3610#endif //AUDIO_POLICY_TEST
3611
3612// ---
3613
Eric Laurentc75307b2015-03-17 15:29:32 -07003614void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003615{
François Gaffie98cc1912015-03-18 17:52:40 +01003616 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003617 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003618 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003619 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003620}
3621
François Gaffie53615e22015-03-19 09:24:12 +01003622void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3623{
3624 mOutputs.removeItem(output);
3625}
3626
Eric Laurent1f2f2232014-06-02 12:01:23 -07003627void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003628{
François Gaffie98cc1912015-03-18 17:52:40 +01003629 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003630 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003631 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003632}
Eric Laurente552edb2014-03-10 17:42:56 -07003633
Eric Laurentc75307b2015-03-17 15:29:32 -07003634void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003635 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003636 const String8 address /*in*/,
3637 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003638 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003639 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003640 if (devDesc != 0) {
3641 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3642 desc->mIoHandle, address.string());
3643 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003644 }
3645}
3646
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003647status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003648 audio_policy_dev_state_t state,
3649 SortedVector<audio_io_handle_t>& outputs,
3650 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003651{
François Gaffie53615e22015-03-19 09:24:12 +01003652 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003653 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003654
3655 if (audio_device_is_digital(device)) {
3656 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003657 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003658 }
Eric Laurente552edb2014-03-10 17:42:56 -07003659
Eric Laurent3b73df72014-03-11 09:06:29 -07003660 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003661 // first list already open outputs that can be routed to this device
3662 for (size_t i = 0; i < mOutputs.size(); i++) {
3663 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003664 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003665 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003666 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3667 outputs.add(mOutputs.keyAt(i));
3668 } else {
3669 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003670 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003671 }
Eric Laurente552edb2014-03-10 17:42:56 -07003672 }
3673 }
3674 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003675 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003676 for (size_t i = 0; i < mHwModules.size(); i++)
3677 {
3678 if (mHwModules[i]->mHandle == 0) {
3679 continue;
3680 }
3681 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3682 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003683 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003684 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003685 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003686 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003687 profiles.add(profile);
3688 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3689 }
Eric Laurente552edb2014-03-10 17:42:56 -07003690 }
3691 }
3692 }
3693
Eric Laurent7b279bb2015-12-14 10:18:23 -08003694 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003695
Eric Laurente552edb2014-03-10 17:42:56 -07003696 if (profiles.isEmpty() && outputs.isEmpty()) {
3697 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3698 return BAD_VALUE;
3699 }
3700
3701 // open outputs for matching profiles if needed. Direct outputs are also opened to
3702 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3703 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003704 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003705
3706 // nothing to do if one output is already opened for this profile
3707 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003708 for (j = 0; j < outputs.size(); j++) {
3709 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003710 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003711 // matching profile: save the sample rates, format and channel masks supported
3712 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003713 if (audio_device_is_digital(device)) {
3714 devDesc->importAudioPort(profile);
3715 }
Eric Laurente552edb2014-03-10 17:42:56 -07003716 break;
3717 }
3718 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003719 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003720 continue;
3721 }
3722
Eric Laurent83b88082014-06-20 18:31:16 -07003723 ALOGV("opening output for device %08x with params %s profile %p",
3724 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003725 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003726 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003727 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3728 config.sample_rate = desc->mSamplingRate;
3729 config.channel_mask = desc->mChannelMask;
3730 config.format = desc->mFormat;
3731 config.offload_info.sample_rate = desc->mSamplingRate;
3732 config.offload_info.channel_mask = desc->mChannelMask;
3733 config.offload_info.format = desc->mFormat;
3734 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003735 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003736 &output,
3737 &config,
3738 &desc->mDevice,
3739 address,
3740 &desc->mLatency,
3741 desc->mFlags);
3742 if (status == NO_ERROR) {
3743 desc->mSamplingRate = config.sample_rate;
3744 desc->mChannelMask = config.channel_mask;
3745 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003746
Eric Laurentd4692962014-05-05 18:13:44 -07003747 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003748 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003749 char *param = audio_device_address_to_parameter(device, address);
3750 mpClientInterface->setParameters(output, String8(param));
3751 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003752 }
Phil Burk00eeb322016-03-31 12:41:00 -07003753 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003754 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003755 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003756 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003757 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003758 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003759 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003760 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003761 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003762 config.offload_info.sample_rate = config.sample_rate;
3763 config.offload_info.channel_mask = config.channel_mask;
3764 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003765 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003766 &output,
3767 &config,
3768 &desc->mDevice,
3769 address,
3770 &desc->mLatency,
3771 desc->mFlags);
3772 if (status == NO_ERROR) {
3773 desc->mSamplingRate = config.sample_rate;
3774 desc->mChannelMask = config.channel_mask;
3775 desc->mFormat = config.format;
3776 } else {
3777 output = AUDIO_IO_HANDLE_NONE;
3778 }
Eric Laurentd4692962014-05-05 18:13:44 -07003779 }
3780
Eric Laurentcf2c0212014-07-25 16:20:43 -07003781 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003782 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003783 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003784 sp<AudioPolicyMix> policyMix;
3785 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003786 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3787 address.string());
3788 }
François Gaffie036e1e92015-03-19 10:16:24 +01003789 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003790 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003791
Eric Laurent87ffa392015-05-22 10:32:38 -07003792 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3793 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003794 // no duplicated output for direct outputs and
3795 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003796 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003797
Eric Laurentd4692962014-05-05 18:13:44 -07003798 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003799 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003800
Eric Laurentd4692962014-05-05 18:13:44 -07003801 //TODO: configure audio effect output stage here
3802
3803 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003804 duplicatedOutput =
3805 mpClientInterface->openDuplicateOutput(output,
3806 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003807 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003808 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003809 sp<SwAudioOutputDescriptor> dupOutputDesc =
3810 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3811 dupOutputDesc->mOutput1 = mPrimaryOutput;
3812 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003813 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3814 dupOutputDesc->mFormat = desc->mFormat;
3815 dupOutputDesc->mChannelMask = desc->mChannelMask;
3816 dupOutputDesc->mLatency = desc->mLatency;
3817 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003818 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003819 } else {
3820 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003821 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003822 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003823 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003824 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003825 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003826 }
Eric Laurente552edb2014-03-10 17:42:56 -07003827 }
3828 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003829 } else {
3830 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003831 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003832 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003833 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003834 profiles.removeAt(profile_index);
3835 profile_index--;
3836 } else {
3837 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003838 // Load digital format info only for digital devices
3839 if (audio_device_is_digital(device)) {
3840 devDesc->importAudioPort(profile);
3841 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003842
François Gaffie53615e22015-03-19 09:24:12 +01003843 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003844 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3845 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003846 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003847 NULL/*patch handle*/, address.string());
3848 }
Eric Laurente552edb2014-03-10 17:42:56 -07003849 ALOGV("checkOutputsForDevice(): adding output %d", output);
3850 }
3851 }
3852
3853 if (profiles.isEmpty()) {
3854 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3855 return BAD_VALUE;
3856 }
Eric Laurentd4692962014-05-05 18:13:44 -07003857 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003858 // check if one opened output is not needed any more after disconnecting one device
3859 for (size_t i = 0; i < mOutputs.size(); i++) {
3860 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003861 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003862 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003863 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003864 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003865 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003866 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003867 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3868 mOutputs.keyAt(i));
3869 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003870 }
Eric Laurente552edb2014-03-10 17:42:56 -07003871 }
3872 }
Eric Laurentd4692962014-05-05 18:13:44 -07003873 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003874 for (size_t i = 0; i < mHwModules.size(); i++)
3875 {
3876 if (mHwModules[i]->mHandle == 0) {
3877 continue;
3878 }
3879 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3880 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003881 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003882 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003883 ALOGV("checkOutputsForDevice(): "
3884 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003885 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003886 }
3887 }
3888 }
3889 }
3890 return NO_ERROR;
3891}
3892
Paul McLean9080a4c2015-06-18 08:24:02 -07003893status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003894 audio_policy_dev_state_t state,
3895 SortedVector<audio_io_handle_t>& inputs,
3896 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003897{
Paul McLean9080a4c2015-06-18 08:24:02 -07003898 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003899 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003900
3901 if (audio_device_is_digital(device)) {
3902 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003903 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003904 }
3905
Eric Laurentd4692962014-05-05 18:13:44 -07003906 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3907 // first list already open inputs that can be routed to this device
3908 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3909 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003910 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003911 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3912 inputs.add(mInputs.keyAt(input_index));
3913 }
3914 }
3915
3916 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003917 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003918 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3919 {
3920 if (mHwModules[module_idx]->mHandle == 0) {
3921 continue;
3922 }
3923 for (size_t profile_index = 0;
3924 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3925 profile_index++)
3926 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003927 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3928
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003929 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003930 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003931 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003932 profiles.add(profile);
3933 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3934 profile_index, module_idx);
3935 }
Eric Laurentd4692962014-05-05 18:13:44 -07003936 }
3937 }
3938 }
3939
3940 if (profiles.isEmpty() && inputs.isEmpty()) {
3941 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3942 return BAD_VALUE;
3943 }
3944
3945 // open inputs for matching profiles if needed. Direct inputs are also opened to
3946 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3947 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3948
Eric Laurent1c333e22014-05-20 10:48:17 -07003949 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003950 // nothing to do if one input is already opened for this profile
3951 size_t input_index;
3952 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3953 desc = mInputs.valueAt(input_index);
3954 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003955 if (audio_device_is_digital(device)) {
3956 devDesc->importAudioPort(profile);
3957 }
Eric Laurentd4692962014-05-05 18:13:44 -07003958 break;
3959 }
3960 }
3961 if (input_index != mInputs.size()) {
3962 continue;
3963 }
3964
3965 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3966 desc = new AudioInputDescriptor(profile);
3967 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003968 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3969 config.sample_rate = desc->mSamplingRate;
3970 config.channel_mask = desc->mChannelMask;
3971 config.format = desc->mFormat;
3972 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003973 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003974 &input,
3975 &config,
3976 &desc->mDevice,
3977 address,
3978 AUDIO_SOURCE_MIC,
3979 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003980
Eric Laurentcf2c0212014-07-25 16:20:43 -07003981 if (status == NO_ERROR) {
3982 desc->mSamplingRate = config.sample_rate;
3983 desc->mChannelMask = config.channel_mask;
3984 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003985
Eric Laurentd4692962014-05-05 18:13:44 -07003986 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003987 char *param = audio_device_address_to_parameter(device, address);
3988 mpClientInterface->setParameters(input, String8(param));
3989 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003990 }
Phil Burk00eeb322016-03-31 12:41:00 -07003991 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003992 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003993 ALOGW("checkInputsForDevice() direct input missing param");
3994 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003995 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003996 }
3997
3998 if (input != 0) {
3999 addInput(input, desc);
4000 }
4001 } // endif input != 0
4002
Eric Laurentcf2c0212014-07-25 16:20:43 -07004003 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004004 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004005 profiles.removeAt(profile_index);
4006 profile_index--;
4007 } else {
4008 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004009 if (audio_device_is_digital(device)) {
4010 devDesc->importAudioPort(profile);
4011 }
Eric Laurentd4692962014-05-05 18:13:44 -07004012 ALOGV("checkInputsForDevice(): adding input %d", input);
4013 }
4014 } // end scan profiles
4015
4016 if (profiles.isEmpty()) {
4017 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4018 return BAD_VALUE;
4019 }
4020 } else {
4021 // Disconnect
4022 // check if one opened input is not needed any more after disconnecting one device
4023 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4024 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004025 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004026 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4027 mInputs.keyAt(input_index));
4028 inputs.add(mInputs.keyAt(input_index));
4029 }
4030 }
4031 // Clear any profiles associated with the disconnected device.
4032 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
4033 if (mHwModules[module_index]->mHandle == 0) {
4034 continue;
4035 }
4036 for (size_t profile_index = 0;
4037 profile_index < mHwModules[module_index]->mInputProfiles.size();
4038 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004039 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004040 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07004041 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07004042 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01004043 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004044 }
4045 }
4046 }
4047 } // end disconnect
4048
4049 return NO_ERROR;
4050}
4051
4052
Eric Laurente0720872014-03-11 09:30:41 -07004053void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004054{
4055 ALOGV("closeOutput(%d)", output);
4056
Eric Laurentc75307b2015-03-17 15:29:32 -07004057 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004058 if (outputDesc == NULL) {
4059 ALOGW("closeOutput() unknown output %d", output);
4060 return;
4061 }
François Gaffie036e1e92015-03-19 10:16:24 +01004062 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004063
Eric Laurente552edb2014-03-10 17:42:56 -07004064 // look for duplicated outputs connected to the output being removed.
4065 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004066 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004067 if (dupOutputDesc->isDuplicated() &&
4068 (dupOutputDesc->mOutput1 == outputDesc ||
4069 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004070 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004071 if (dupOutputDesc->mOutput1 == outputDesc) {
4072 outputDesc2 = dupOutputDesc->mOutput2;
4073 } else {
4074 outputDesc2 = dupOutputDesc->mOutput1;
4075 }
4076 // As all active tracks on duplicated output will be deleted,
4077 // and as they were also referenced on the other output, the reference
4078 // count for their stream type must be adjusted accordingly on
4079 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07004080 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004081 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004082 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004083 }
4084 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4085 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4086
4087 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004088 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004089 }
4090 }
4091
Eric Laurent05b90f82014-08-27 15:32:29 -07004092 nextAudioPortGeneration();
4093
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004094 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004095 if (index >= 0) {
4096 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004097 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004098 mAudioPatches.removeItemsAt(index);
4099 mpClientInterface->onAudioPatchListUpdate();
4100 }
4101
Eric Laurente552edb2014-03-10 17:42:56 -07004102 AudioParameter param;
4103 param.add(String8("closing"), String8("true"));
4104 mpClientInterface->setParameters(output, param.toString());
4105
4106 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004107 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004108 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004109}
4110
4111void AudioPolicyManager::closeInput(audio_io_handle_t input)
4112{
4113 ALOGV("closeInput(%d)", input);
4114
4115 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4116 if (inputDesc == NULL) {
4117 ALOGW("closeInput() unknown input %d", input);
4118 return;
4119 }
4120
Eric Laurent6a94d692014-05-20 11:18:06 -07004121 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004122
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004123 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004124 if (index >= 0) {
4125 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004126 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004127 mAudioPatches.removeItemsAt(index);
4128 mpClientInterface->onAudioPatchListUpdate();
4129 }
4130
4131 mpClientInterface->closeInput(input);
4132 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004133}
4134
Eric Laurentc75307b2015-03-17 15:29:32 -07004135SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4136 audio_devices_t device,
4137 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004138{
4139 SortedVector<audio_io_handle_t> outputs;
4140
4141 ALOGVV("getOutputsForDevice() device %04x", device);
4142 for (size_t i = 0; i < openOutputs.size(); i++) {
4143 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004144 i, openOutputs.valueAt(i)->isDuplicated(),
4145 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004146 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4147 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4148 outputs.add(openOutputs.keyAt(i));
4149 }
4150 }
4151 return outputs;
4152}
4153
Eric Laurente0720872014-03-11 09:30:41 -07004154bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004155 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004156{
4157 if (outputs1.size() != outputs2.size()) {
4158 return false;
4159 }
4160 for (size_t i = 0; i < outputs1.size(); i++) {
4161 if (outputs1[i] != outputs2[i]) {
4162 return false;
4163 }
4164 }
4165 return true;
4166}
4167
Eric Laurente0720872014-03-11 09:30:41 -07004168void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004169{
4170 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4171 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4172 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4173 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4174
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004175 // also take into account external policy-related changes: add all outputs which are
4176 // associated with policies in the "before" and "after" output vectors
4177 ALOGVV("checkOutputForStrategy(): policy related outputs");
4178 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004179 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004180 if (desc != 0 && desc->mPolicyMix != NULL) {
4181 srcOutputs.add(desc->mIoHandle);
4182 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4183 }
4184 }
4185 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004186 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004187 if (desc != 0 && desc->mPolicyMix != NULL) {
4188 dstOutputs.add(desc->mIoHandle);
4189 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4190 }
4191 }
4192
Eric Laurente552edb2014-03-10 17:42:56 -07004193 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4194 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4195 strategy, srcOutputs[0], dstOutputs[0]);
4196 // mute strategy while moving tracks from one output to another
4197 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004198 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004199 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004200 setStrategyMute(strategy, true, desc);
4201 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004202 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004203 sp<AudioSourceDescriptor> source =
4204 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4205 if (source != 0){
4206 connectAudioSource(source);
4207 }
Eric Laurente552edb2014-03-10 17:42:56 -07004208 }
4209
4210 // Move effects associated to this strategy from previous output to new output
4211 if (strategy == STRATEGY_MEDIA) {
4212 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4213 SortedVector<audio_io_handle_t> moved;
4214 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004215 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4216 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4217 effectDesc->mIo != fxOutput) {
4218 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004219 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4220 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004221 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004222 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004223 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004224 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004225 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004226 }
4227 }
4228 }
4229 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004230 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004231 if (getStrategy((audio_stream_type_t)i) == strategy) {
4232 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004233 }
4234 }
4235 }
4236}
4237
Eric Laurente0720872014-03-11 09:30:41 -07004238void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004239{
François Gaffie2110e042015-03-24 08:41:51 +01004240 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004241 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004242 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004243 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004244 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004245 checkOutputForStrategy(STRATEGY_SONIFICATION);
4246 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004247 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004248 checkOutputForStrategy(STRATEGY_MEDIA);
4249 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004250 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004251}
4252
Eric Laurente0720872014-03-11 09:30:41 -07004253void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004254{
François Gaffie53615e22015-03-19 09:24:12 +01004255 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004256 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004257 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004258 return;
4259 }
4260
Eric Laurent3a4311c2014-03-17 12:00:47 -07004261 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004262 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4263 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4264 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004265 // suspend A2DP output if:
4266 // (NOT already suspended) &&
4267 // ((SCO device is connected &&
4268 // (forced usage for communication || for record is SCO))) ||
4269 // (phone state is ringing || in call)
4270 //
4271 // restore A2DP output if:
4272 // (Already suspended) &&
4273 // ((SCO device is NOT connected ||
4274 // (forced usage NOT for communication && NOT for record is SCO))) &&
4275 // (phone state is NOT ringing && NOT in call)
4276 //
4277 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004278 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004279 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4280 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4281 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4282 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004283
4284 mpClientInterface->restoreOutput(a2dpOutput);
4285 mA2dpSuspended = false;
4286 }
4287 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004288 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004289 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4290 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4291 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4292 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004293
4294 mpClientInterface->suspendOutput(a2dpOutput);
4295 mA2dpSuspended = true;
4296 }
4297 }
4298}
4299
Eric Laurentc75307b2015-03-17 15:29:32 -07004300audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4301 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004302{
4303 audio_devices_t device = AUDIO_DEVICE_NONE;
4304
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004305 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004306 if (index >= 0) {
4307 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4308 if (patchDesc->mUid != mUidCached) {
4309 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004310 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004311 return outputDesc->device();
4312 }
4313 }
4314
Eric Laurente552edb2014-03-10 17:42:56 -07004315 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004316 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004317 // use device for strategy enforced audible
4318 // 2: we are in call or the strategy phone is active on the output:
4319 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004320 // 3: the strategy for enforced audible is active but not enforced on the output:
4321 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004322 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004323 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004324 // 5: the strategy accessibility is active on the output:
4325 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004326 // 6: the strategy "respectful" sonification is active on the output:
4327 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004328 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004329 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004330 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004331 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004332 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004333 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004334 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004335 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004336 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4337 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004338 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004339 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004340 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004341 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004342 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004343 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004344 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4345 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004346 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004347 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004348 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004349 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004350 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004351 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004352 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004353 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004354 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004355 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004356 }
4357
Eric Laurent1c333e22014-05-20 10:48:17 -07004358 ALOGV("getNewOutputDevice() selected device %x", device);
4359 return device;
4360}
4361
Eric Laurent232f26f2016-02-17 18:06:27 -08004362audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004363{
Eric Laurent232f26f2016-02-17 18:06:27 -08004364 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004365
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004366 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004367 if (index >= 0) {
4368 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4369 if (patchDesc->mUid != mUidCached) {
4370 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004371 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004372 return inputDesc->mDevice;
4373 }
4374 }
4375
Eric Laurent232f26f2016-02-17 18:06:27 -08004376 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004377
Eric Laurente552edb2014-03-10 17:42:56 -07004378 return device;
4379}
4380
Eric Laurent794fde22016-03-11 09:50:45 -08004381bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4382 audio_stream_type_t stream2) {
4383 return ((stream1 == stream2) ||
4384 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4385 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004386}
4387
Eric Laurente0720872014-03-11 09:30:41 -07004388uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004389 return (uint32_t)getStrategy(stream);
4390}
4391
Eric Laurente0720872014-03-11 09:30:41 -07004392audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004393 // By checking the range of stream before calling getStrategy, we avoid
4394 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4395 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004396 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004397 return AUDIO_DEVICE_NONE;
4398 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004399 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004400 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4401 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004402 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004403 }
Eric Laurent794fde22016-03-11 09:50:45 -08004404 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004405 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004406 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Eric Laurent28d09f02016-03-08 10:43:05 -08004407 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4408 for (size_t i = 0; i < outputs.size(); i++) {
4409 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004410 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004411 curDevices |= outputDesc->device();
4412 }
4413 }
4414 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004415 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004416
4417 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4418 and doesn't really need to.*/
4419 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4420 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4421 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4422 }
Eric Laurente552edb2014-03-10 17:42:56 -07004423 return devices;
4424}
4425
François Gaffie2110e042015-03-24 08:41:51 +01004426routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4427{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004428 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004429 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004430}
4431
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004432uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4433 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004434 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4435 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4436 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004437 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4438 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4439 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004440 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004441 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004442}
4443
Eric Laurente0720872014-03-11 09:30:41 -07004444void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004445 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004446 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004447 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4448 updateDevicesAndOutputs();
4449 break;
4450 default:
4451 break;
4452 }
4453}
4454
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004455uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004456
4457 // skip beacon mute management if a dedicated TTS output is available
4458 if (mTtsOutputAvailable) {
4459 return 0;
4460 }
4461
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004462 switch(event) {
4463 case STARTING_OUTPUT:
4464 mBeaconMuteRefCount++;
4465 break;
4466 case STOPPING_OUTPUT:
4467 if (mBeaconMuteRefCount > 0) {
4468 mBeaconMuteRefCount--;
4469 }
4470 break;
4471 case STARTING_BEACON:
4472 mBeaconPlayingRefCount++;
4473 break;
4474 case STOPPING_BEACON:
4475 if (mBeaconPlayingRefCount > 0) {
4476 mBeaconPlayingRefCount--;
4477 }
4478 break;
4479 }
4480
4481 if (mBeaconMuteRefCount > 0) {
4482 // any playback causes beacon to be muted
4483 return setBeaconMute(true);
4484 } else {
4485 // no other playback: unmute when beacon starts playing, mute when it stops
4486 return setBeaconMute(mBeaconPlayingRefCount == 0);
4487 }
4488}
4489
4490uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4491 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4492 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4493 // keep track of muted state to avoid repeating mute/unmute operations
4494 if (mBeaconMuted != mute) {
4495 // mute/unmute AUDIO_STREAM_TTS on all outputs
4496 ALOGV("\t muting %d", mute);
4497 uint32_t maxLatency = 0;
4498 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004499 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004500 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004501 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004502 0 /*delay*/, AUDIO_DEVICE_NONE);
4503 const uint32_t latency = desc->latency() * 2;
4504 if (latency > maxLatency) {
4505 maxLatency = latency;
4506 }
4507 }
4508 mBeaconMuted = mute;
4509 return maxLatency;
4510 }
4511 return 0;
4512}
4513
Eric Laurente0720872014-03-11 09:30:41 -07004514audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004515 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004516{
Paul McLeanaa981192015-03-21 09:55:15 -07004517 // Routing
4518 // see if we have an explicit route
4519 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4520 // (getStrategy(stream)).
4521 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4522 // and activity count is non-zero
4523 // the device = the device from the descriptor in the RouteMap, and exit.
4524 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4525 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004526 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4527 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004528 return route->mDeviceDescriptor->type();
4529 }
4530 }
4531
Eric Laurente552edb2014-03-10 17:42:56 -07004532 if (fromCache) {
4533 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4534 strategy, mDeviceForStrategy[strategy]);
4535 return mDeviceForStrategy[strategy];
4536 }
François Gaffie2110e042015-03-24 08:41:51 +01004537 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004538}
4539
Eric Laurente0720872014-03-11 09:30:41 -07004540void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004541{
4542 for (int i = 0; i < NUM_STRATEGIES; i++) {
4543 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4544 }
4545 mPreviousOutputs = mOutputs;
4546}
4547
Eric Laurent1f2f2232014-06-02 12:01:23 -07004548uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004549 audio_devices_t prevDevice,
4550 uint32_t delayMs)
4551{
4552 // mute/unmute strategies using an incompatible device combination
4553 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4554 // if unmuting, unmute only after the specified delay
4555 if (outputDesc->isDuplicated()) {
4556 return 0;
4557 }
4558
4559 uint32_t muteWaitMs = 0;
4560 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004561 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004562
4563 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4564 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004565 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004566 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4567 bool doMute = false;
4568
4569 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4570 doMute = true;
4571 outputDesc->mStrategyMutedByDevice[i] = true;
4572 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4573 doMute = true;
4574 outputDesc->mStrategyMutedByDevice[i] = false;
4575 }
Eric Laurent99401132014-05-07 19:48:15 -07004576 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004577 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004578 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004579 // skip output if it does not share any device with current output
4580 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4581 == AUDIO_DEVICE_NONE) {
4582 continue;
4583 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004584 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4585 mute ? "muting" : "unmuting", i, curDevice);
4586 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004587 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004588 if (mute) {
4589 // FIXME: should not need to double latency if volume could be applied
4590 // immediately by the audioflinger mixer. We must account for the delay
4591 // between now and the next time the audioflinger thread for this output
4592 // will process a buffer (which corresponds to one buffer size,
4593 // usually 1/2 or 1/4 of the latency).
4594 if (muteWaitMs < desc->latency() * 2) {
4595 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004596 }
4597 }
4598 }
4599 }
4600 }
4601 }
4602
Eric Laurent99401132014-05-07 19:48:15 -07004603 // temporary mute output if device selection changes to avoid volume bursts due to
4604 // different per device volumes
4605 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004606 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4607 // temporary mute duration is conservatively set to 4 times the reported latency
4608 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4609 if (muteWaitMs < tempMuteWaitMs) {
4610 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004611 }
Eric Laurentdc462862016-07-19 12:29:53 -07004612
Eric Laurent99401132014-05-07 19:48:15 -07004613 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004614 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004615 // make sure that we do not start the temporary mute period too early in case of
4616 // delayed device change
4617 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004618 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004619 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004620 }
4621 }
4622 }
4623
Eric Laurente552edb2014-03-10 17:42:56 -07004624 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4625 if (muteWaitMs > delayMs) {
4626 muteWaitMs -= delayMs;
4627 usleep(muteWaitMs * 1000);
4628 return muteWaitMs;
4629 }
4630 return 0;
4631}
4632
Eric Laurentc75307b2015-03-17 15:29:32 -07004633uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004634 audio_devices_t device,
4635 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004636 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004637 audio_patch_handle_t *patchHandle,
4638 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004639{
Eric Laurentc75307b2015-03-17 15:29:32 -07004640 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004641 AudioParameter param;
4642 uint32_t muteWaitMs;
4643
4644 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004645 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4646 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004647 return muteWaitMs;
4648 }
4649 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4650 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004651 if ((device != AUDIO_DEVICE_NONE) &&
4652 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004653 return 0;
4654 }
4655
4656 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004657 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004658
4659 audio_devices_t prevDevice = outputDesc->mDevice;
4660
Paul McLeanaa981192015-03-21 09:55:15 -07004661 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004662
4663 if (device != AUDIO_DEVICE_NONE) {
4664 outputDesc->mDevice = device;
4665 }
4666 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4667
4668 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004669 // the requested device is AUDIO_DEVICE_NONE
4670 // OR the requested device is the same as current device
4671 // AND force is not specified
4672 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004673 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004674 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4675 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004676 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004677 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004678 return muteWaitMs;
4679 }
4680
4681 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004682
Eric Laurente552edb2014-03-10 17:42:56 -07004683 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004684 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004685 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004686 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004687 DeviceVector deviceList;
4688 if ((address == NULL) || (strlen(address) == 0)) {
4689 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4690 } else {
4691 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4692 }
4693
Eric Laurent1c333e22014-05-20 10:48:17 -07004694 if (!deviceList.isEmpty()) {
4695 struct audio_patch patch;
4696 outputDesc->toAudioPortConfig(&patch.sources[0]);
4697 patch.num_sources = 1;
4698 patch.num_sinks = 0;
4699 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4700 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004701 patch.num_sinks++;
4702 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004703 ssize_t index;
4704 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4705 index = mAudioPatches.indexOfKey(*patchHandle);
4706 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004707 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004708 }
4709 sp< AudioPatch> patchDesc;
4710 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4711 if (index >= 0) {
4712 patchDesc = mAudioPatches.valueAt(index);
4713 afPatchHandle = patchDesc->mAfPatchHandle;
4714 }
4715
Eric Laurent1c333e22014-05-20 10:48:17 -07004716 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004717 &afPatchHandle,
4718 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004719 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4720 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004721 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004722 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004723 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004724 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004725 addAudioPatch(patchDesc->mHandle, patchDesc);
4726 } else {
4727 patchDesc->mPatch = patch;
4728 }
4729 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004730 if (patchHandle) {
4731 *patchHandle = patchDesc->mHandle;
4732 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004733 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004734 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004735 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004736 }
4737 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004738
4739 // inform all input as well
4740 for (size_t i = 0; i < mInputs.size(); i++) {
4741 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004742 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004743 AudioParameter inputCmd = AudioParameter();
4744 ALOGV("%s: inform input %d of device:%d", __func__,
4745 inputDescriptor->mIoHandle, device);
4746 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4747 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4748 inputCmd.toString(),
4749 delayMs);
4750 }
4751 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004752 }
Eric Laurente552edb2014-03-10 17:42:56 -07004753
4754 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004755 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004756
4757 return muteWaitMs;
4758}
4759
Eric Laurentc75307b2015-03-17 15:29:32 -07004760status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004761 int delayMs,
4762 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004763{
Eric Laurent6a94d692014-05-20 11:18:06 -07004764 ssize_t index;
4765 if (patchHandle) {
4766 index = mAudioPatches.indexOfKey(*patchHandle);
4767 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004768 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004769 }
4770 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004771 return INVALID_OPERATION;
4772 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004773 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4774 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004775 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004776 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004777 removeAudioPatch(patchDesc->mHandle);
4778 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004779 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004780 return status;
4781}
4782
4783status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4784 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004785 bool force,
4786 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004787{
4788 status_t status = NO_ERROR;
4789
Eric Laurent1f2f2232014-06-02 12:01:23 -07004790 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004791 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4792 inputDesc->mDevice = device;
4793
4794 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4795 if (!deviceList.isEmpty()) {
4796 struct audio_patch patch;
4797 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004798 // AUDIO_SOURCE_HOTWORD is for internal use only:
4799 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004800 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004801 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004802 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4803 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004804 patch.num_sinks = 1;
4805 //only one input device for now
4806 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004807 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004808 ssize_t index;
4809 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4810 index = mAudioPatches.indexOfKey(*patchHandle);
4811 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004812 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004813 }
4814 sp< AudioPatch> patchDesc;
4815 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4816 if (index >= 0) {
4817 patchDesc = mAudioPatches.valueAt(index);
4818 afPatchHandle = patchDesc->mAfPatchHandle;
4819 }
4820
Eric Laurent1c333e22014-05-20 10:48:17 -07004821 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004822 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004823 0);
4824 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004825 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004826 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004827 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004828 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004829 addAudioPatch(patchDesc->mHandle, patchDesc);
4830 } else {
4831 patchDesc->mPatch = patch;
4832 }
4833 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004834 if (patchHandle) {
4835 *patchHandle = patchDesc->mHandle;
4836 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004837 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004838 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004839 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004840 }
4841 }
4842 }
4843 return status;
4844}
4845
Eric Laurent6a94d692014-05-20 11:18:06 -07004846status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4847 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004848{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004849 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004850 ssize_t index;
4851 if (patchHandle) {
4852 index = mAudioPatches.indexOfKey(*patchHandle);
4853 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004854 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004855 }
4856 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004857 return INVALID_OPERATION;
4858 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004859 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4860 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004861 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004862 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004863 removeAudioPatch(patchDesc->mHandle);
4864 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004865 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 return status;
4867}
4868
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004869sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004870 String8 address,
4871 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004872 audio_format_t& format,
4873 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004874 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004875{
4876 // Choose an input profile based on the requested capture parameters: select the first available
4877 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004878 //
4879 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4880 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004881
4882 for (size_t i = 0; i < mHwModules.size(); i++)
4883 {
4884 if (mHwModules[i]->mHandle == 0) {
4885 continue;
4886 }
4887 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4888 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004889 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004890 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004891 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004892 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004893 format,
4894 &format /*updatedFormat*/,
4895 channelMask,
4896 &channelMask /*updatedChannelMask*/,
4897 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004898
Eric Laurente552edb2014-03-10 17:42:56 -07004899 return profile;
4900 }
4901 }
4902 }
4903 return NULL;
4904}
4905
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004906
4907audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004908 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004909{
François Gaffie036e1e92015-03-19 10:16:24 +01004910 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4911 audio_devices_t selectedDeviceFromMix =
4912 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004913
François Gaffie036e1e92015-03-19 10:16:24 +01004914 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4915 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004916 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004917 return getDeviceForInputSource(inputSource);
4918}
4919
4920audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4921{
Paul McLean466dc8e2015-04-17 13:15:36 -06004922 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4923 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004924 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004925 return route->mDeviceDescriptor->type();
4926 }
4927 }
4928
4929 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004930}
4931
Eric Laurente0720872014-03-11 09:30:41 -07004932float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004933 int index,
4934 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004935{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004936 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004937 // if a headset is connected, apply the following rules to ring tones and notifications
4938 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004939 // - always attenuate notifications volume by 6dB
4940 // - attenuate ring tones volume by 6dB unless music is not playing and
4941 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07004942 // - if music is playing, always limit the volume to current music volume,
4943 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004944 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004945 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4946 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4947 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4948 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4949 ((stream_strategy == STRATEGY_SONIFICATION)
4950 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004951 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004952 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004953 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004954 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004955 // when the phone is ringing we must consider that music could have been paused just before
4956 // by the music application and behave as if music was active if the last music track was
4957 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004958 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004959 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004960 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004961 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004962 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004963 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4964 musicDevice),
4965 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004966 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4967 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004968 if (volumeDB > minVolDB) {
4969 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07004970 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004971 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004972 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4973 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
4974 // on A2DP, also ensure notification volume is not too low compared to media when
4975 // intended to be played
4976 if ((volumeDB > -96.0f) &&
4977 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
4978 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
4979 stream, device,
4980 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
4981 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
4982 }
4983 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07004984 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
4985 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004986 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004987 }
4988 }
4989
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07004990 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07004991}
4992
Eric Laurente0720872014-03-11 09:30:41 -07004993status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004994 int index,
4995 const sp<AudioOutputDescriptor>& outputDesc,
4996 audio_devices_t device,
4997 int delayMs,
4998 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004999{
Eric Laurente552edb2014-03-10 17:42:56 -07005000 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005001 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005002 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005003 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005004 return NO_ERROR;
5005 }
François Gaffie2110e042015-03-24 08:41:51 +01005006 audio_policy_forced_cfg_t forceUseForComm =
5007 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005008 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005009 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5010 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005011 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005012 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005013 return INVALID_OPERATION;
5014 }
5015
Eric Laurentc75307b2015-03-17 15:29:32 -07005016 if (device == AUDIO_DEVICE_NONE) {
5017 device = outputDesc->device();
5018 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005019
Eric Laurentffbc80f2015-03-18 18:30:19 -07005020 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005021 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005022 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005023 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005024
Eric Laurentffbc80f2015-03-18 18:30:19 -07005025 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005026
Eric Laurent3b73df72014-03-11 09:06:29 -07005027 if (stream == AUDIO_STREAM_VOICE_CALL ||
5028 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005029 float voiceVolume;
5030 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005031 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005032 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005033 } else {
5034 voiceVolume = 1.0;
5035 }
5036
Eric Laurent18fba842016-03-31 14:41:26 -07005037 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005038 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5039 mLastVoiceVolume = voiceVolume;
5040 }
5041 }
5042
5043 return NO_ERROR;
5044}
5045
Eric Laurentc75307b2015-03-17 15:29:32 -07005046void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5047 audio_devices_t device,
5048 int delayMs,
5049 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005050{
Eric Laurentc75307b2015-03-17 15:29:32 -07005051 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005052
Eric Laurent794fde22016-03-11 09:50:45 -08005053 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005054 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005055 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005056 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005057 device,
5058 delayMs,
5059 force);
5060 }
5061}
5062
Eric Laurente0720872014-03-11 09:30:41 -07005063void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005064 bool on,
5065 const sp<AudioOutputDescriptor>& outputDesc,
5066 int delayMs,
5067 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005068{
Eric Laurent72249d52015-06-08 11:12:15 -07005069 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5070 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005071 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005072 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005073 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005074 }
5075 }
5076}
5077
Eric Laurente0720872014-03-11 09:30:41 -07005078void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005079 bool on,
5080 const sp<AudioOutputDescriptor>& outputDesc,
5081 int delayMs,
5082 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005083{
Eric Laurente552edb2014-03-10 17:42:56 -07005084 if (device == AUDIO_DEVICE_NONE) {
5085 device = outputDesc->device();
5086 }
5087
Eric Laurentc75307b2015-03-17 15:29:32 -07005088 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5089 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005090
5091 if (on) {
5092 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005093 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005094 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005095 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005096 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005097 }
5098 }
5099 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5100 outputDesc->mMuteCount[stream]++;
5101 } else {
5102 if (outputDesc->mMuteCount[stream] == 0) {
5103 ALOGV("setStreamMute() unmuting non muted stream!");
5104 return;
5105 }
5106 if (--outputDesc->mMuteCount[stream] == 0) {
5107 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005108 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005109 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005110 device,
5111 delayMs);
5112 }
5113 }
5114}
5115
Eric Laurente0720872014-03-11 09:30:41 -07005116void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005117 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005118{
Eric Laurent87ffa392015-05-22 10:32:38 -07005119 if(!hasPrimaryOutput()) {
5120 return;
5121 }
5122
Eric Laurente552edb2014-03-10 17:42:56 -07005123 // if the stream pertains to sonification strategy and we are in call we must
5124 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5125 // in the device used for phone strategy and play the tone if the selected device does not
5126 // interfere with the device used for phone strategy
5127 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5128 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005129 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005130 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5131 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005132 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005133 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5134 stream, starting, outputDesc->mDevice, stateChange);
5135 if (outputDesc->mRefCount[stream]) {
5136 int muteCount = 1;
5137 if (stateChange) {
5138 muteCount = outputDesc->mRefCount[stream];
5139 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005140 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005141 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5142 for (int i = 0; i < muteCount; i++) {
5143 setStreamMute(stream, starting, mPrimaryOutput);
5144 }
5145 } else {
5146 ALOGV("handleIncallSonification() high visibility");
5147 if (outputDesc->device() &
5148 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5149 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5150 for (int i = 0; i < muteCount; i++) {
5151 setStreamMute(stream, starting, mPrimaryOutput);
5152 }
5153 }
5154 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005155 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5156 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005157 } else {
5158 mpClientInterface->stopTone();
5159 }
5160 }
5161 }
5162 }
5163}
5164
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005165audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5166{
5167 // flags to stream type mapping
5168 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5169 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5170 }
5171 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5172 return AUDIO_STREAM_BLUETOOTH_SCO;
5173 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005174 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5175 return AUDIO_STREAM_TTS;
5176 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005177
5178 // usage to stream type mapping
5179 switch (attr->usage) {
5180 case AUDIO_USAGE_MEDIA:
5181 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005182 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5183 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005184 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5185 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005186 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5187 return AUDIO_STREAM_SYSTEM;
5188 case AUDIO_USAGE_VOICE_COMMUNICATION:
5189 return AUDIO_STREAM_VOICE_CALL;
5190
5191 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5192 return AUDIO_STREAM_DTMF;
5193
5194 case AUDIO_USAGE_ALARM:
5195 return AUDIO_STREAM_ALARM;
5196 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5197 return AUDIO_STREAM_RING;
5198
5199 case AUDIO_USAGE_NOTIFICATION:
5200 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5201 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5202 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5203 case AUDIO_USAGE_NOTIFICATION_EVENT:
5204 return AUDIO_STREAM_NOTIFICATION;
5205
5206 case AUDIO_USAGE_UNKNOWN:
5207 default:
5208 return AUDIO_STREAM_MUSIC;
5209 }
5210}
Eric Laurente83b55d2014-11-14 10:06:21 -08005211
François Gaffie53615e22015-03-19 09:24:12 +01005212bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5213{
Eric Laurente83b55d2014-11-14 10:06:21 -08005214 // has flags that map to a strategy?
5215 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5216 return true;
5217 }
5218
5219 // has known usage?
5220 switch (paa->usage) {
5221 case AUDIO_USAGE_UNKNOWN:
5222 case AUDIO_USAGE_MEDIA:
5223 case AUDIO_USAGE_VOICE_COMMUNICATION:
5224 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5225 case AUDIO_USAGE_ALARM:
5226 case AUDIO_USAGE_NOTIFICATION:
5227 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5228 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5229 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5230 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5231 case AUDIO_USAGE_NOTIFICATION_EVENT:
5232 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5233 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5234 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5235 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005236 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005237 break;
5238 default:
5239 return false;
5240 }
5241 return true;
5242}
5243
François Gaffiead3183e2015-03-18 16:55:35 +01005244bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5245 routing_strategy strategy, uint32_t inPastMs,
5246 nsecs_t sysTime) const
5247{
5248 if ((sysTime == 0) && (inPastMs != 0)) {
5249 sysTime = systemTime();
5250 }
Eric Laurent794fde22016-03-11 09:50:45 -08005251 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005252 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5253 (NUM_STRATEGIES == strategy)) &&
5254 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5255 return true;
5256 }
5257 }
5258 return false;
5259}
5260
François Gaffie2110e042015-03-24 08:41:51 +01005261audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5262{
5263 return mEngine->getForceUse(usage);
5264}
5265
5266bool AudioPolicyManager::isInCall()
5267{
5268 return isStateInCall(mEngine->getPhoneState());
5269}
5270
5271bool AudioPolicyManager::isStateInCall(int state)
5272{
5273 return is_state_in_call(state);
5274}
5275
Eric Laurentd60560a2015-04-10 11:31:20 -07005276void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5277{
5278 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5279 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5280 if (sourceDesc->mDevice->equals(deviceDesc)) {
5281 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5282 stopAudioSource(sourceDesc->getHandle());
5283 }
5284 }
5285
5286 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5287 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5288 bool release = false;
5289 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5290 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5291 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5292 source->ext.device.type == deviceDesc->type()) {
5293 release = true;
5294 }
5295 }
5296 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5297 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5298 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5299 sink->ext.device.type == deviceDesc->type()) {
5300 release = true;
5301 }
5302 }
5303 if (release) {
5304 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5305 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5306 }
5307 }
5308}
5309
Phil Burk09bc4612016-02-24 15:58:15 -08005310// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005311void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5312 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005313 // TODO Set this based on Config properties.
5314 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005315
5316 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5317 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005318 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005319
5320 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005321 bool supportsAC3 = false;
5322 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005323 bool supportsIEC61937 = false;
5324 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5325 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005326 switch (format) {
5327 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005328 supportsAC3 = true;
5329 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005330 case AUDIO_FORMAT_E_AC3:
5331 case AUDIO_FORMAT_DTS:
5332 case AUDIO_FORMAT_DTS_HD:
Phil Burk07ac1142016-03-25 13:39:29 -07005333 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005334 break;
5335 case AUDIO_FORMAT_IEC61937:
5336 supportsIEC61937 = true;
5337 break;
5338 default:
5339 break;
5340 }
5341 }
Phil Burk09bc4612016-02-24 15:58:15 -08005342
5343 // Modify formats based on surround preferences.
5344 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005345 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5346 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5347 // Remove surround sound related formats.
5348 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5349 audio_format_t format = formats[formatIndex];
5350 switch(format) {
5351 case AUDIO_FORMAT_AC3:
5352 case AUDIO_FORMAT_E_AC3:
5353 case AUDIO_FORMAT_DTS:
5354 case AUDIO_FORMAT_DTS_HD:
5355 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005356 formats.removeAt(formatIndex);
5357 break;
5358 default:
5359 formatIndex++; // keep it
5360 break;
5361 }
Phil Burk09bc4612016-02-24 15:58:15 -08005362 }
Phil Burk07ac1142016-03-25 13:39:29 -07005363 supportsAC3 = false;
5364 supportsOtherSurround = false;
5365 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005366 }
Phil Burk07ac1142016-03-25 13:39:29 -07005367 } else { // AUTO or ALWAYS
5368 // Most TVs support AC3 even if they do not report it in the EDID.
5369 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5370 && !supportsAC3) {
5371 formats.add(AUDIO_FORMAT_AC3);
5372 supportsAC3 = true;
5373 }
5374
5375 // If ALWAYS, add support for raw surround formats if all are missing.
5376 // This assumes that if any of these formats are reported by the HAL
5377 // then the report is valid and should not be modified.
5378 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5379 && !supportsOtherSurround) {
5380 formats.add(AUDIO_FORMAT_E_AC3);
5381 formats.add(AUDIO_FORMAT_DTS);
5382 formats.add(AUDIO_FORMAT_DTS_HD);
5383 supportsOtherSurround = true;
5384 }
5385
5386 // Add support for IEC61937 if any raw surround supported.
5387 // The HAL could do this but add it here, just in case.
5388 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5389 formats.add(AUDIO_FORMAT_IEC61937);
5390 supportsIEC61937 = true;
5391 }
Phil Burk09bc4612016-02-24 15:58:15 -08005392 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005393}
5394
5395// Modify the list of channel masks supported.
5396void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5397 ChannelsVector &channelMasks = *channelMasksPtr;
5398 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5399 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5400
5401 // If NEVER, then remove support for channelMasks > stereo.
5402 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5403 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5404 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5405 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5406 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5407 channelMasks.removeAt(maskIndex);
5408 } else {
5409 maskIndex++;
5410 }
5411 }
5412 // If ALWAYS, then make sure we at least support 5.1
5413 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5414 bool supports5dot1 = false;
5415 // Are there any channel masks that can be considered "surround"?
5416 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
5417 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5418 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5419 supports5dot1 = true;
5420 break;
5421 }
5422 }
5423 // If not then add 5.1 support.
5424 if (!supports5dot1) {
5425 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5426 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5427 }
Phil Burk09bc4612016-02-24 15:58:15 -08005428 }
5429}
5430
Phil Burk00eeb322016-03-31 12:41:00 -07005431void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5432 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005433 AudioProfileVector &profiles)
5434{
5435 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005436
François Gaffie112b0af2015-11-19 16:13:25 +01005437 // Format MUST be checked first to update the list of AudioProfile
5438 if (profiles.hasDynamicFormat()) {
5439 reply = mpClientInterface->getParameters(ioHandle,
5440 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk0709b0a2016-03-31 12:54:57 -07005441 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005442 AudioParameter repliedParameters(reply);
5443 if (repliedParameters.get(
5444 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005445 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5446 return;
5447 }
Phil Burk09bc4612016-02-24 15:58:15 -08005448 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005449 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005450 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005451 }
Phil Burk09bc4612016-02-24 15:58:15 -08005452 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005453 }
5454 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5455
Eric Laurent20eb3a42016-01-26 18:39:17 -08005456 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005457 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005458 ChannelsVector channelMasks;
5459 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005460 AudioParameter requestedParameters;
5461 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5462
5463 if (profiles.hasDynamicRateFor(format)) {
5464 reply = mpClientInterface->getParameters(ioHandle,
5465 requestedParameters.toString() + ";" +
5466 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5467 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005468 AudioParameter repliedParameters(reply);
5469 if (repliedParameters.get(
5470 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5471 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005472 }
5473 }
5474 if (profiles.hasDynamicChannelsFor(format)) {
5475 reply = mpClientInterface->getParameters(ioHandle,
5476 requestedParameters.toString() + ";" +
5477 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5478 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005479 AudioParameter repliedParameters(reply);
5480 if (repliedParameters.get(
5481 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5482 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005483 if (device == AUDIO_DEVICE_OUT_HDMI) {
5484 filterSurroundChannelMasks(&channelMasks);
5485 }
François Gaffie112b0af2015-11-19 16:13:25 +01005486 }
5487 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005488 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005489 }
5490}
Eric Laurentd60560a2015-04-10 11:31:20 -07005491
Eric Laurente552edb2014-03-10 17:42:56 -07005492}; // namespace android