blob: 1055007887b75a85fe54ee763f3b4de0ab1be6da [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
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +090027#define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128
28#define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml"
François Gaffief4ad6e52015-11-19 16:59:57 +010029
Eric Laurentd4692962014-05-05 18:13:44 -070030#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070031#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070032
François Gaffie2110e042015-03-24 08:41:51 +010033#include <AudioPolicyManagerInterface.h>
34#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070035#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070036#include <utils/Log.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070037#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080038#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070039#include <soundtrigger/SoundTrigger.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070040#include <system/audio.h>
Mikhail Naganov9ee05402016-10-13 15:58:17 -070041#include <audio_policy_conf.h>
Eric Laurentd4692962014-05-05 18:13:44 -070042#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010043#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010044#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010045#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010046#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010047#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010048#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010049#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070050
Eric Laurent3b73df72014-03-11 09:06:29 -070051namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070052
Eric Laurentdc462862016-07-19 12:29:53 -070053//FIXME: workaround for truncated touch sounds
54// to be removed when the problem is handled by system UI
55#define TOUCH_SOUND_FIXED_DELAY_MS 100
Jean-Michel Trivi719a9872017-08-05 13:51:35 -070056
57// Largest difference in dB on earpiece in call between the voice volume and another
58// media / notification / system volume.
59constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f;
60
Eric Laurente552edb2014-03-10 17:42:56 -070061// ----------------------------------------------------------------------------
62// AudioPolicyInterface implementation
63// ----------------------------------------------------------------------------
64
Eric Laurente0720872014-03-11 09:30:41 -070065status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080066 audio_policy_dev_state_t state,
67 const char *device_address,
68 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070069{
jiabina7b43792018-02-15 16:04:46 -080070 status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name);
71 nextAudioPortGeneration();
72 return status;
Eric Laurentc73ca6e2014-12-12 14:34:22 -080073}
74
François Gaffie44481e72016-04-20 07:49:57 +020075void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device,
76 audio_policy_dev_state_t state,
77 const String8 &device_address)
78{
79 AudioParameter param(device_address);
80 const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ?
Mikhail Naganov388360c2016-10-17 17:09:41 -070081 AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect);
François Gaffie44481e72016-04-20 07:49:57 +020082 param.addInt(key, device);
83 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
84}
85
Eric Laurentc73ca6e2014-12-12 14:34:22 -080086status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080087 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080088 const char *device_address,
89 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080090{
Paul McLeane743a472015-01-28 11:07:31 -080091 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
Mikhail Naganov7e22e942017-12-07 10:04:29 -080092 device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070093
94 // connect/disconnect only 1 device at a time
95 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
96
François Gaffie53615e22015-03-19 09:24:12 +010097 sp<DeviceDescriptor> devDesc =
98 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080099
Eric Laurente552edb2014-03-10 17:42:56 -0700100 // handle output devices
101 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700102 SortedVector <audio_io_handle_t> outputs;
103
Eric Laurent3a4311c2014-03-17 12:00:47 -0700104 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
105
Eric Laurente552edb2014-03-10 17:42:56 -0700106 // save a copy of the opened output descriptors before any output is opened or closed
107 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
108 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700109 switch (state)
110 {
111 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -0800112 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700113 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700114 ALOGW("setDeviceConnectionState() device already connected: %x", device);
115 return INVALID_OPERATION;
116 }
117 ALOGV("setDeviceConnectionState() connecting device %x", device);
118
Eric Laurente552edb2014-03-10 17:42:56 -0700119 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700120 index = mAvailableOutputDevices.add(devDesc);
121 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100122 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700123 if (module == 0) {
124 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
125 device);
126 mAvailableOutputDevices.remove(devDesc);
127 return INVALID_OPERATION;
128 }
Paul McLeane743a472015-01-28 11:07:31 -0800129 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700130 } else {
131 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700132 }
133
François Gaffie44481e72016-04-20 07:49:57 +0200134 // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
135 // parameters on newly connected devices (instead of opening the outputs...)
136 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
137
Eric Laurenta1d525f2015-01-29 13:36:45 -0800138 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700139 mAvailableOutputDevices.remove(devDesc);
François Gaffie44481e72016-04-20 07:49:57 +0200140
141 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
142 devDesc->mAddress);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700143 return INVALID_OPERATION;
144 }
François Gaffie2110e042015-03-24 08:41:51 +0100145 // Propagate device availability to Engine
146 mEngine->setDeviceConnectionState(devDesc, state);
147
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700148 // outputs should never be empty here
149 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
150 "checkOutputsForDevice() returned no outputs but status OK");
151 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
152 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800153
Eric Laurent3ae5f312015-02-03 17:12:08 -0800154 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700155 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700156 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700157 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700158 ALOGW("setDeviceConnectionState() device not connected: %x", device);
159 return INVALID_OPERATION;
160 }
161
Paul McLean5c477aa2014-08-20 16:47:57 -0700162 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
163
Paul McLeane743a472015-01-28 11:07:31 -0800164 // Send Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200165 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700166
Eric Laurente552edb2014-03-10 17:42:56 -0700167 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700168 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700169
Eric Laurenta1d525f2015-01-29 13:36:45 -0800170 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100171
172 // Propagate device availability to Engine
173 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700174 } break;
175
176 default:
177 ALOGE("setDeviceConnectionState() invalid state: %x", state);
178 return BAD_VALUE;
179 }
180
Eric Laurent3a4311c2014-03-17 12:00:47 -0700181 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
182 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700183 checkA2dpSuspend();
184 checkOutputForAllStrategies();
185 // outputs must be closed after checkOutputForAllStrategies() is executed
186 if (!outputs.isEmpty()) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800187 for (audio_io_handle_t output : outputs) {
188 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -0700189 // close unused outputs after device disconnection or direct outputs that have been
190 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700191 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700192 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
193 (desc->mDirectOpenCount == 0))) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800194 closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -0700195 }
196 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700197 // check again after closing A2DP output to reset mA2dpSuspended if needed
198 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700199 }
200
201 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700202 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700203 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
204 updateCallRouting(newDevice);
205 }
Eric Laurente552edb2014-03-10 17:42:56 -0700206 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700207 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
208 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
209 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700210 // do not force device change on duplicated output because if device is 0, it will
211 // also force a device 0 for the two outputs it is duplicated to which may override
212 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700213 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100214 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700215 // always force when disconnecting (a non-duplicated device)
216 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700217 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700218 }
Eric Laurente552edb2014-03-10 17:42:56 -0700219 }
220
Eric Laurentd60560a2015-04-10 11:31:20 -0700221 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
222 cleanUpForDevice(devDesc);
223 }
224
Eric Laurent72aa32f2014-05-30 18:51:48 -0700225 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700226 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700227 } // end if is output device
228
Eric Laurente552edb2014-03-10 17:42:56 -0700229 // handle input devices
230 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700231 SortedVector <audio_io_handle_t> inputs;
232
Eric Laurent3a4311c2014-03-17 12:00:47 -0700233 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700234 switch (state)
235 {
236 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device already connected: %d", device);
240 return INVALID_OPERATION;
241 }
François Gaffie53615e22015-03-19 09:24:12 +0100242 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700243 if (module == NULL) {
244 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
245 device);
246 return INVALID_OPERATION;
247 }
François Gaffie44481e72016-04-20 07:49:57 +0200248
249 // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
250 // parameters on newly connected devices (instead of opening the inputs...)
251 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
252
Paul McLean9080a4c2015-06-18 08:24:02 -0700253 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
François Gaffie44481e72016-04-20 07:49:57 +0200254 broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
255 devDesc->mAddress);
Eric Laurentd4692962014-05-05 18:13:44 -0700256 return INVALID_OPERATION;
257 }
258
Eric Laurent3a4311c2014-03-17 12:00:47 -0700259 index = mAvailableInputDevices.add(devDesc);
260 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800261 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700262 } else {
263 return NO_MEMORY;
264 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800265
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 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700271 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700272 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700273 ALOGW("setDeviceConnectionState() device not connected: %d", device);
274 return INVALID_OPERATION;
275 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700276
277 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
278
279 // Set Disconnect to HALs
François Gaffie44481e72016-04-20 07:49:57 +0200280 broadcastDeviceConnectionState(device, state, devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700281
Paul McLean9080a4c2015-06-18 08:24:02 -0700282 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700283 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700284
François Gaffie2110e042015-03-24 08:41:51 +0100285 // Propagate device availability to Engine
286 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700287 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700288
289 default:
290 ALOGE("setDeviceConnectionState() invalid state: %x", state);
291 return BAD_VALUE;
292 }
293
Eric Laurentd4692962014-05-05 18:13:44 -0700294 closeAllInputs();
Eric Laurent5f5fca52016-08-04 11:48:57 -0700295 // As the input device list can impact the output device selection, update
296 // getDeviceForStrategy() cache
297 updateDevicesAndOutputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700298
Eric Laurent87ffa392015-05-22 10:32:38 -0700299 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700300 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
301 updateCallRouting(newDevice);
302 }
303
Eric Laurentd60560a2015-04-10 11:31:20 -0700304 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
305 cleanUpForDevice(devDesc);
306 }
307
Eric Laurentb52c1522014-05-20 11:27:36 -0700308 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700309 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700310 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700311
312 ALOGW("setDeviceConnectionState() invalid device: %x", device);
313 return BAD_VALUE;
314}
315
Eric Laurente0720872014-03-11 09:30:41 -0700316audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100317 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700318{
Eric Laurent634b7142016-04-20 13:48:02 -0700319 sp<DeviceDescriptor> devDesc =
320 mHwModules.getDeviceDescriptor(device, device_address, "",
321 (strlen(device_address) != 0)/*matchAddress*/);
322
323 if (devDesc == 0) {
324 ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s",
325 device, device_address);
326 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
327 }
François Gaffie53615e22015-03-19 09:24:12 +0100328
Eric Laurent3a4311c2014-03-17 12:00:47 -0700329 DeviceVector *deviceVector;
330
Eric Laurente552edb2014-03-10 17:42:56 -0700331 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700332 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700333 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700334 deviceVector = &mAvailableInputDevices;
335 } else {
336 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
337 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700338 }
Eric Laurent634b7142016-04-20 13:48:02 -0700339
340 return (deviceVector->getDevice(device, String8(device_address)) != 0) ?
341 AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta1d525f2015-01-29 13:36:45 -0800342}
343
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800344status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device,
345 const char *device_address,
346 const char *device_name)
347{
348 status_t status;
349
350 ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s",
351 device, device_address, device_name);
352
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800353 // connect/disconnect only 1 device at a time
354 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
355
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800356 // Check if the device is currently connected
357 sp<DeviceDescriptor> devDesc =
358 mHwModules.getDeviceDescriptor(device, device_address, device_name);
359 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
360 if (index < 0) {
361 // Nothing to do: device is not connected
362 return NO_ERROR;
363 }
364
365 // Toggle the device state: UNAVAILABLE -> AVAILABLE
366 // This will force reading again the device configuration
367 status = setDeviceConnectionState(device,
368 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
369 device_address, device_name);
370 if (status != NO_ERROR) {
371 ALOGW("handleDeviceConfigChange() error disabling connection state: %d",
372 status);
373 return status;
374 }
375
376 status = setDeviceConnectionState(device,
377 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
378 device_address, device_name);
379 if (status != NO_ERROR) {
380 ALOGW("handleDeviceConfigChange() error enabling connection state: %d",
381 status);
382 return status;
383 }
384
385 return NO_ERROR;
386}
387
Eric Laurentdc462862016-07-19 12:29:53 -0700388uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700389{
390 bool createTxPatch = false;
Eric Laurentdc462862016-07-19 12:29:53 -0700391 uint32_t muteWaitMs = 0;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700392
Andy Hunga76c7de2016-12-13 19:14:31 -0800393 if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurentdc462862016-07-19 12:29:53 -0700394 return muteWaitMs;
Eric Laurent87ffa392015-05-22 10:32:38 -0700395 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800396 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700397 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
398
399 // release existing RX patch if any
400 if (mCallRxPatch != 0) {
401 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
402 mCallRxPatch.clear();
403 }
404 // release TX patch if any
405 if (mCallTxPatch != 0) {
406 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
407 mCallTxPatch.clear();
408 }
409
410 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
411 // via setOutputDevice() on primary output.
412 // Otherwise, create two audio patches for TX and RX path.
413 if (availablePrimaryOutputDevices() & rxDevice) {
Eric Laurentdc462862016-07-19 12:29:53 -0700414 muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700415 // If the TX device is also on the primary HW module, setOutputDevice() will take care
416 // of it due to legacy implementation. If not, create a patch.
417 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
418 == AUDIO_DEVICE_NONE) {
419 createTxPatch = true;
420 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700421 } else { // create RX path audio patch
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800422 mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700423 createTxPatch = true;
424 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700425 if (createTxPatch) { // create TX path audio patch
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800426 mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs);
427 }
Eric Laurent8ae73122016-04-12 10:13:29 -0700428
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800429 return muteWaitMs;
430}
Eric Laurentc2730ba2014-07-20 15:47:07 -0700431
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800432sp<AudioPatch> AudioPolicyManager::createTelephonyPatch(
433 bool isRx, audio_devices_t device, uint32_t delayMs) {
434 struct audio_patch patch;
435 patch.num_sources = 1;
436 patch.num_sinks = 1;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700437
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800438 sp<DeviceDescriptor> txSourceDeviceDesc;
439 if (isRx) {
440 fillAudioPortConfigForDevice(mAvailableOutputDevices, device, &patch.sinks[0]);
441 fillAudioPortConfigForDevice(
442 mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX, &patch.sources[0]);
443 } else {
444 txSourceDeviceDesc = fillAudioPortConfigForDevice(
445 mAvailableInputDevices, device, &patch.sources[0]);
446 fillAudioPortConfigForDevice(
447 mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX, &patch.sinks[0]);
448 }
449
450 audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX;
451 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs);
452 audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
453 // request to reuse existing output stream if one is already opened to reach the target device
454 if (output != AUDIO_IO_HANDLE_NONE) {
455 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
456 ALOG_ASSERT(!outputDesc->isDuplicated(),
457 "%s() %#x device output %d is duplicated", __func__, outputDevice, output);
458 outputDesc->toAudioPortConfig(&patch.sources[1]);
459 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
460 patch.num_sources = 2;
461 }
462
463 if (!isRx) {
Eric Laurentc0a889f2015-10-14 14:36:34 -0700464 // terminate active capture if on the same HW module as the call TX source device
465 // FIXME: would be better to refine to only inputs whose profile connects to the
466 // call TX device but this information is not in the audio patch and logic here must be
467 // symmetric to the one in startInput()
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800468 for (const auto& activeDesc : mInputs.getActiveInputs()) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800469 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
470 AudioSessionCollection activeSessions =
471 activeDesc->getAudioSessions(true /*activeOnly*/);
472 for (size_t j = 0; j < activeSessions.size(); j++) {
473 audio_session_t activeSession = activeSessions.keyAt(j);
474 stopInput(activeDesc->mIoHandle, activeSession);
475 releaseInput(activeDesc->mIoHandle, activeSession);
476 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700477 }
478 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700479 }
Eric Laurentdc462862016-07-19 12:29:53 -0700480
Mikhail Naganovb567ba02017-12-08 11:16:27 -0800481 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
482 status_t status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
483 ALOGW_IF(status != NO_ERROR,
484 "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX");
485 sp<AudioPatch> audioPatch;
486 if (status == NO_ERROR) {
487 audioPatch = new AudioPatch(&patch, mUidCached);
488 audioPatch->mAfPatchHandle = afPatchHandle;
489 audioPatch->mUid = mUidCached;
490 }
491 return audioPatch;
492}
493
494sp<DeviceDescriptor> AudioPolicyManager::fillAudioPortConfigForDevice(
495 const DeviceVector& devices, audio_devices_t device, audio_port_config *config) {
496 DeviceVector deviceList = devices.getDevicesFromType(device);
497 ALOG_ASSERT(!deviceList.isEmpty(),
498 "%s() selected device type %#x is not in devices list", __func__, device);
499 sp<DeviceDescriptor> deviceDesc = deviceList.itemAt(0);
500 deviceDesc->toAudioPortConfig(config);
501 return deviceDesc;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700502}
503
Eric Laurente0720872014-03-11 09:30:41 -0700504void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700505{
506 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100507 // store previous phone state for management of sonification strategy below
508 int oldState = mEngine->getPhoneState();
509
510 if (mEngine->setPhoneState(state) != NO_ERROR) {
511 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700512 return;
513 }
François Gaffie2110e042015-03-24 08:41:51 +0100514 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700515 // if leaving call state, handle special case of active streams
516 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700517 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700518 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800519 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700521 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800522
Eric Laurent63dea1d2015-07-02 17:10:28 -0700523 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800524 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700525 }
526
François Gaffie2110e042015-03-24 08:41:51 +0100527 /**
528 * Switching to or from incall state or switching between telephony and VoIP lead to force
529 * routing command.
530 */
531 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
532 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700533
534 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700535 checkA2dpSuspend();
536 checkOutputForAllStrategies();
537 updateDevicesAndOutputs();
538
Eric Laurente552edb2014-03-10 17:42:56 -0700539 int delayMs = 0;
540 if (isStateInCall(state)) {
541 nsecs_t sysTime = systemTime();
542 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700543 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700544 // mute media and sonification strategies and delay device switch by the largest
545 // latency of any output where either strategy is active.
546 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100547 if ((isStrategyActive(desc, STRATEGY_MEDIA,
548 SONIFICATION_HEADSET_MUSIC_DELAY,
549 sysTime) ||
550 isStrategyActive(desc, STRATEGY_SONIFICATION,
551 SONIFICATION_HEADSET_MUSIC_DELAY,
552 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700553 (delayMs < (int)desc->latency()*2)) {
554 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700555 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700556 setStrategyMute(STRATEGY_MEDIA, true, desc);
557 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700558 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700559 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
560 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700561 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
562 }
563 }
564
Eric Laurent87ffa392015-05-22 10:32:38 -0700565 if (hasPrimaryOutput()) {
566 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
567 // the device returned is not necessarily reachable via this output
568 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
569 // force routing command to audio hardware when ending call
570 // even if no device change is needed
571 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
572 rxDevice = mPrimaryOutput->device();
573 }
Eric Laurente552edb2014-03-10 17:42:56 -0700574
Eric Laurent87ffa392015-05-22 10:32:38 -0700575 if (state == AUDIO_MODE_IN_CALL) {
576 updateCallRouting(rxDevice, delayMs);
577 } else if (oldState == AUDIO_MODE_IN_CALL) {
578 if (mCallRxPatch != 0) {
579 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
580 mCallRxPatch.clear();
581 }
582 if (mCallTxPatch != 0) {
583 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
584 mCallTxPatch.clear();
585 }
586 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
587 } else {
588 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700589 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700590 }
Eric Laurente552edb2014-03-10 17:42:56 -0700591 // if entering in call state, handle special case of active streams
592 // pertaining to sonification strategy see handleIncallSonification()
593 if (isStateInCall(state)) {
594 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800595 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700596 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700598
599 // force reevaluating accessibility routing when call starts
600 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700601 }
602
603 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700604 if (state == AUDIO_MODE_RINGTONE &&
605 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700606 mLimitRingtoneVolume = true;
607 } else {
608 mLimitRingtoneVolume = false;
609 }
610}
611
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700612audio_mode_t AudioPolicyManager::getPhoneState() {
613 return mEngine->getPhoneState();
614}
615
Eric Laurente0720872014-03-11 09:30:41 -0700616void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700617 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700618{
François Gaffie2110e042015-03-24 08:41:51 +0100619 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurent8dc87a62017-05-16 19:00:40 -0700620 if (config == mEngine->getForceUse(usage)) {
621 return;
622 }
Eric Laurente552edb2014-03-10 17:42:56 -0700623
François Gaffie2110e042015-03-24 08:41:51 +0100624 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
625 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
626 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700627 }
François Gaffie2110e042015-03-24 08:41:51 +0100628 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
629 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
630 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700631
632 // check for device and output changes triggered by new force usage
633 checkA2dpSuspend();
634 checkOutputForAllStrategies();
635 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800636
Eric Laurentdc462862016-07-19 12:29:53 -0700637 //FIXME: workaround for truncated touch sounds
638 // to be removed when the problem is handled by system UI
639 uint32_t delayMs = 0;
640 uint32_t waitMs = 0;
641 if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
642 delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
643 }
Eric Laurent87ffa392015-05-22 10:32:38 -0700644 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700645 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
Eric Laurentdc462862016-07-19 12:29:53 -0700646 waitMs = updateCallRouting(newDevice, delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700647 }
Eric Laurente552edb2014-03-10 17:42:56 -0700648 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700649 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
650 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
651 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700652 waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
653 delayMs);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700654 }
Eric Laurente552edb2014-03-10 17:42:56 -0700655 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentdc462862016-07-19 12:29:53 -0700656 applyStreamVolumes(outputDesc, newDevice, waitMs, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700657 }
658 }
659
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800660 for (const auto& activeDesc : mInputs.getActiveInputs()) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800661 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700662 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800663 if (activeDesc->mProfile->getSupportedDevices().types() &
664 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
665 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700666 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800667 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700668 }
Eric Laurente552edb2014-03-10 17:42:56 -0700669 }
Eric Laurente552edb2014-03-10 17:42:56 -0700670}
671
Eric Laurente0720872014-03-11 09:30:41 -0700672void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700673{
674 ALOGV("setSystemProperty() property %s, value %s", property, value);
675}
676
677// Find a direct output profile compatible with the parameters passed, even if the input flags do
678// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800679sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700680 audio_devices_t device,
681 uint32_t samplingRate,
682 audio_format_t format,
683 audio_channel_mask_t channelMask,
684 audio_output_flags_t flags)
685{
Eric Laurent861a6282015-05-18 15:40:16 -0700686 // only retain flags that will drive the direct output profile selection
687 // if explicitly requested
688 static const uint32_t kRelevantFlags =
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700689 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
690 AUDIO_OUTPUT_FLAG_VOIP_RX);
Eric Laurent861a6282015-05-18 15:40:16 -0700691 flags =
692 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
693
694 sp<IOProfile> profile;
695
Mikhail Naganovd4120142017-12-06 15:49:22 -0800696 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -0800697 for (const auto& curProfile : hwModule->getOutputProfiles()) {
Eric Laurent861a6282015-05-18 15:40:16 -0700698 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700699 samplingRate, NULL /*updatedSamplingRate*/,
700 format, NULL /*updatedFormat*/,
701 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700702 flags)) {
703 continue;
704 }
705 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100706 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700707 continue;
708 }
709 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100710 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700711 continue;
712 }
713 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100714 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700715 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700716 }
Eric Laurente552edb2014-03-10 17:42:56 -0700717 }
718 }
Eric Laurent861a6282015-05-18 15:40:16 -0700719 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700720}
721
Eric Laurentf4e63452017-11-06 19:31:46 +0000722audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream)
Eric Laurente552edb2014-03-10 17:42:56 -0700723{
Eric Laurent3b73df72014-03-11 09:06:29 -0700724 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700725 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Andy Hungc9901522017-11-10 20:07:54 -0800726
727 // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput().
728 // We use selectOutput() here since we don't have the desired AudioTrack sample rate,
729 // format, flags, etc. This may result in some discrepancy for functions that utilize
730 // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount()
731 // and AudioSystem::getOutputSamplingRate().
732
Eric Laurentf4e63452017-11-06 19:31:46 +0000733 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
734 audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
Eric Laurente552edb2014-03-10 17:42:56 -0700735
Eric Laurentf4e63452017-11-06 19:31:46 +0000736 ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output);
737 return output;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700738}
739
Eric Laurente83b55d2014-11-14 10:06:21 -0800740status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
741 audio_io_handle_t *output,
742 audio_session_t session,
743 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700744 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800745 const audio_config_t *config,
Nadav Bar766fb022018-01-07 12:18:03 +0200746 audio_output_flags_t *flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700747 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800748 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700749{
Eric Laurente83b55d2014-11-14 10:06:21 -0800750 audio_attributes_t attributes;
751 if (attr != NULL) {
752 if (!isValidAttributes(attr)) {
753 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
754 attr->usage, attr->content_type, attr->flags,
755 attr->tags);
756 return BAD_VALUE;
757 }
758 attributes = *attr;
759 } else {
760 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
761 ALOGE("getOutputForAttr(): invalid stream type");
762 return BAD_VALUE;
763 }
764 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700765 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800766
767 // TODO: check for existing client for this port ID
768 if (*portId == AUDIO_PORT_HANDLE_NONE) {
769 *portId = AudioPort::getNextUniqueId();
770 }
771
Eric Laurentc75307b2015-03-17 15:29:32 -0700772 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800773 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100774 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Eric Laurent20b9ef02016-12-05 11:03:16 -0800775 if (!audio_has_proportional_frames(config->format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100776 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800777 }
François Gaffie036e1e92015-03-19 10:16:24 +0100778 *stream = streamTypefromAttributesInt(&attributes);
779 *output = desc->mIoHandle;
780 ALOGV("getOutputForAttr() returns output %d", *output);
781 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800782 }
783 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
784 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
785 return BAD_VALUE;
786 }
787
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700788 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
789 " session %d selectedDeviceId %d",
790 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
Eric Laurent2ac76942017-06-22 17:17:09 -0700791 session, *selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700792
793 *stream = streamTypefromAttributesInt(&attributes);
794
795 // Explicit routing?
796 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -0700797 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -0800798 deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700799 }
800 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700801
Eric Laurente83b55d2014-11-14 10:06:21 -0800802 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700803 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700804
Eric Laurente83b55d2014-11-14 10:06:21 -0800805 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Nadav Bar766fb022018-01-07 12:18:03 +0200806 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
Eric Laurent93c3d412014-08-01 14:48:35 -0700807 }
808
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800809 ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, "
810 "flags %#x",
Nadav Bar766fb022018-01-07 12:18:03 +0200811 device, config->sample_rate, config->format, config->channel_mask, *flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700812
Phil Burk2d059932018-02-15 15:55:11 -0800813 *output = getOutputForDevice(device, session, *stream, *output, config, flags);
Eric Laurente83b55d2014-11-14 10:06:21 -0800814 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700815 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800816 return INVALID_OPERATION;
817 }
Paul McLeanaa981192015-03-21 09:55:15 -0700818
Eric Laurent2ac76942017-06-22 17:17:09 -0700819 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
820 *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId()
821 : AUDIO_PORT_HANDLE_NONE;
822
823 ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId);
824
Eric Laurente83b55d2014-11-14 10:06:21 -0800825 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700826}
827
828audio_io_handle_t AudioPolicyManager::getOutputForDevice(
829 audio_devices_t device,
Kevin Rocard169753c2017-03-06 14:18:23 -0800830 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700831 audio_stream_type_t stream,
Phil Burk2d059932018-02-15 15:55:11 -0800832 audio_io_handle_t originalOutput,
Eric Laurentfe231122017-11-17 17:48:06 -0800833 const audio_config_t *config,
Nadav Bar766fb022018-01-07 12:18:03 +0200834 audio_output_flags_t *flags)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700835{
Phil Burk2d059932018-02-15 15:55:11 -0800836 audio_io_handle_t output = originalOutput;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700837 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700838
Eric Laurente552edb2014-03-10 17:42:56 -0700839 // open a direct output if required by specified parameters
840 //force direct flag if offload flag is set: offloading implies a direct output stream
841 // and all common behaviors are driven by checking only the direct flag
842 // this should normally be set appropriately in the policy configuration file
Nadav Bar766fb022018-01-07 12:18:03 +0200843 if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
844 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700845 }
Nadav Bar766fb022018-01-07 12:18:03 +0200846 if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
847 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent93c3d412014-08-01 14:48:35 -0700848 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800849 // only allow deep buffering for music stream type
850 if (stream != AUDIO_STREAM_MUSIC) {
Nadav Bar766fb022018-01-07 12:18:03 +0200851 *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700852 } else if (/* stream == AUDIO_STREAM_MUSIC && */
Nadav Bar766fb022018-01-07 12:18:03 +0200853 *flags == AUDIO_OUTPUT_FLAG_NONE &&
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700854 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
855 // use DEEP_BUFFER as default output for music stream type
Nadav Bar766fb022018-01-07 12:18:03 +0200856 *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800857 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700858 if (stream == AUDIO_STREAM_TTS) {
Nadav Bar766fb022018-01-07 12:18:03 +0200859 *flags = AUDIO_OUTPUT_FLAG_TTS;
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700860 } else if (stream == AUDIO_STREAM_VOICE_CALL &&
Eric Laurentfe231122017-11-17 17:48:06 -0800861 audio_is_linear_pcm(config->format)) {
Nadav Bar766fb022018-01-07 12:18:03 +0200862 *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
Haynes Mathew George84c621e2017-04-25 11:41:50 -0700863 AUDIO_OUTPUT_FLAG_DIRECT);
864 ALOGV("Set VoIP and Direct output flags for PCM format");
Nadav Bar766fb022018-01-07 12:18:03 +0200865 } else if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX &&
866 stream == AUDIO_STREAM_MUSIC &&
867 audio_is_linear_pcm(config->format) &&
868 isInCall()) {
869 *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC;
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700870 }
Eric Laurente552edb2014-03-10 17:42:56 -0700871
Nadav Bar766fb022018-01-07 12:18:03 +0200872
Eric Laurentb732cf52014-09-24 19:08:21 -0700873 sp<IOProfile> profile;
874
875 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700876 // and not explicitly requested
Nadav Bar766fb022018-01-07 12:18:03 +0200877 if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Eric Laurentfe231122017-11-17 17:48:06 -0800878 audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX &&
879 audio_channel_count_from_out_mask(config->channel_mask) <= 2) {
Eric Laurentb732cf52014-09-24 19:08:21 -0700880 goto non_direct_output;
881 }
882
Andy Hung2ddee192015-12-18 17:34:44 -0800883 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
884 // This prevents creating an offloaded track and tearing it down immediately after start
885 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700886 // FIXME: We should check the audio session here but we do not have it in this context.
887 // This may prevent offloading in rare situations where effects are left active by apps
888 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700889
Nadav Bar766fb022018-01-07 12:18:03 +0200890 if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800891 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700892 profile = getProfileForDirectOutput(device,
Eric Laurentfe231122017-11-17 17:48:06 -0800893 config->sample_rate,
894 config->format,
895 config->channel_mask,
Nadav Bar766fb022018-01-07 12:18:03 +0200896 (audio_output_flags_t)*flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700897 }
898
Eric Laurent1c333e22014-05-20 10:48:17 -0700899 if (profile != 0) {
Phil Burk2d059932018-02-15 15:55:11 -0800900 // exclude MMAP streams
901 if ((*flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0 || output != AUDIO_IO_HANDLE_NONE) {
902 for (size_t i = 0; i < mOutputs.size(); i++) {
903 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
904 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
905 // reuse direct output if currently open by the same client
906 // and configured with same parameters
907 if ((config->sample_rate == desc->mSamplingRate) &&
908 audio_formats_match(config->format, desc->mFormat) &&
909 (config->channel_mask == desc->mChannelMask) &&
910 (session == desc->mDirectClientSession)) {
911 desc->mDirectOpenCount++;
912 ALOGI("getOutputForDevice() reusing direct output %d for session %d",
913 mOutputs.keyAt(i), session);
914 return mOutputs.keyAt(i);
915 }
Eric Laurente552edb2014-03-10 17:42:56 -0700916 }
917 }
918 }
Eric Laurent3974e3b2017-12-07 17:58:43 -0800919
920 if (!profile->canOpenNewIo()) {
921 goto non_direct_output;
Eric Laurente552edb2014-03-10 17:42:56 -0700922 }
Eric Laurent861a6282015-05-18 15:40:16 -0700923
Eric Laurent3974e3b2017-12-07 17:58:43 -0800924 sp<SwAudioOutputDescriptor> outputDesc =
925 new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurent53b810e2017-12-10 17:25:10 -0800926
927 DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device);
928 String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress
929 : String8("");
930
Nadav Bar766fb022018-01-07 12:18:03 +0200931 status = outputDesc->open(config, device, address, stream, *flags, &output);
Eric Laurente552edb2014-03-10 17:42:56 -0700932
933 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700934 if (status != NO_ERROR ||
Eric Laurentfe231122017-11-17 17:48:06 -0800935 (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) ||
936 (config->format != AUDIO_FORMAT_DEFAULT &&
937 !audio_formats_match(config->format, outputDesc->mFormat)) ||
938 (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) {
939 ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d,"
940 "format %d %d, channel mask %04x %04x", output, config->sample_rate,
941 outputDesc->mSamplingRate, config->format, outputDesc->mFormat,
942 config->channel_mask, outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfe231122017-11-17 17:48:06 -0800944 outputDesc->close();
Eric Laurente552edb2014-03-10 17:42:56 -0700945 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800946 // fall back to mixer output if possible when the direct output could not be open
Eric Laurentfe231122017-11-17 17:48:06 -0800947 if (audio_is_linear_pcm(config->format) &&
948 config->sample_rate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800949 goto non_direct_output;
950 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700951 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700952 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700953 outputDesc->mRefCount[stream] = 0;
954 outputDesc->mStopTime[stream] = 0;
955 outputDesc->mDirectOpenCount = 1;
Kevin Rocard169753c2017-03-06 14:18:23 -0800956 outputDesc->mDirectClientSession = session;
957
Eric Laurente552edb2014-03-10 17:42:56 -0700958 addOutput(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700959 mPreviousOutputs = mOutputs;
Eric Laurentf4e63452017-11-06 19:31:46 +0000960 ALOGV("getOutputForDevice() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700961 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700962 return output;
963 }
964
Eric Laurentb732cf52014-09-24 19:08:21 -0700965non_direct_output:
Eric Laurent14cbfca2016-03-17 09:42:16 -0700966
967 // A request for HW A/V sync cannot fallback to a mixed output because time
968 // stamps are embedded in audio data
Phil Burk2d059932018-02-15 15:55:11 -0800969 if ((*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) {
Eric Laurent14cbfca2016-03-17 09:42:16 -0700970 return AUDIO_IO_HANDLE_NONE;
971 }
972
Eric Laurente552edb2014-03-10 17:42:56 -0700973 // ignoring channel mask due to downmix capability in mixer
974
975 // open a non direct output
976
977 // for non direct outputs, only PCM is supported
Eric Laurentfe231122017-11-17 17:48:06 -0800978 if (audio_is_linear_pcm(config->format)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700979 // get which output is suitable for the specified stream. The actual
980 // routing change will happen when startOutput() will be called
981 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
982
Eric Laurent8838a382014-09-08 16:44:28 -0700983 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
Nadav Bar766fb022018-01-07 12:18:03 +0200984 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
985 output = selectOutput(outputs, *flags, config->format);
Eric Laurente552edb2014-03-10 17:42:56 -0700986 }
Eric Laurentf4e63452017-11-06 19:31:46 +0000987 ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, "
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800988 "sampling rate %d, format %#x, channels %#x, flags %#x",
Nadav Bar766fb022018-01-07 12:18:03 +0200989 stream, config->sample_rate, config->format, config->channel_mask, *flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700990
Eric Laurente552edb2014-03-10 17:42:56 -0700991 return output;
992}
993
Eric Laurente0720872014-03-11 09:30:41 -0700994audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700995 audio_output_flags_t flags,
996 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700997{
998 // select one output among several that provide a path to a particular device or set of
999 // devices (the list was previously build by getOutputsForDevice()).
1000 // The priority is as follows:
1001 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -08001002 // 2: the output with the bit depth the closest to the requested one
1003 // 3: the primary output
1004 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07001005
1006 if (outputs.size() == 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001007 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001008 }
1009 if (outputs.size() == 1) {
1010 return outputs[0];
1011 }
1012
1013 int maxCommonFlags = 0;
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001014 audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE;
1015 audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE;
1016 audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE;
Eric Laurente6930022016-02-11 10:20:40 -08001017 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1018 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001019
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001020 for (audio_io_handle_t output : outputs) {
1021 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001022 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001023 // if a valid format is specified, skip output if not compatible
1024 if (format != AUDIO_FORMAT_INVALID) {
1025 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001026 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001027 continue;
1028 }
1029 } else if (!audio_is_linear_pcm(format)) {
1030 continue;
1031 }
Eric Laurente6930022016-02-11 10:20:40 -08001032 if (AudioPort::isBetterFormatMatch(
1033 outputDesc->mFormat, bestFormat, format)) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001034 outputForFormat = output;
Eric Laurente6930022016-02-11 10:20:40 -08001035 bestFormat = outputDesc->mFormat;
1036 }
Eric Laurent8838a382014-09-08 16:44:28 -07001037 }
1038
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001039 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001040 if (commonFlags >= maxCommonFlags) {
1041 if (commonFlags == maxCommonFlags) {
Andy Hungc9901522017-11-10 20:07:54 -08001042 if (format != AUDIO_FORMAT_INVALID
1043 && AudioPort::isBetterFormatMatch(
1044 outputDesc->mFormat, bestFormatForFlags, format)) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001045 outputForFlags = output;
Eric Laurente6930022016-02-11 10:20:40 -08001046 bestFormatForFlags = outputDesc->mFormat;
1047 }
1048 } else {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001049 outputForFlags = output;
Eric Laurente6930022016-02-11 10:20:40 -08001050 maxCommonFlags = commonFlags;
1051 bestFormatForFlags = outputDesc->mFormat;
1052 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001053 ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001054 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001055 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001056 outputForPrimary = output;
Eric Laurente552edb2014-03-10 17:42:56 -07001057 }
1058 }
1059 }
1060
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001061 if (outputForFlags != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001062 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001063 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001064 if (outputForFormat != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001065 return outputForFormat;
1066 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001067 if (outputForPrimary != AUDIO_IO_HANDLE_NONE) {
Eric Laurente6930022016-02-11 10:20:40 -08001068 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001069 }
1070
1071 return outputs[0];
1072}
1073
Eric Laurente0720872014-03-11 09:30:41 -07001074status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001075 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001076 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001077{
Paul McLeanaa981192015-03-21 09:55:15 -07001078 ALOGV("startOutput() output %d, stream %d, session %d",
1079 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001080 ssize_t index = mOutputs.indexOfKey(output);
1081 if (index < 0) {
1082 ALOGW("startOutput() unknown output %d", output);
1083 return BAD_VALUE;
1084 }
1085
Eric Laurentc75307b2015-03-17 15:29:32 -07001086 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1087
Eric Laurent733ce942017-12-07 12:18:25 -08001088 status_t status = outputDesc->start();
1089 if (status != NO_ERROR) {
1090 return status;
Eric Laurent3974e3b2017-12-07 17:58:43 -08001091 }
1092
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001093 // Routing?
1094 mOutputRoutes.incRouteActivity(session);
1095
Eric Laurentc75307b2015-03-17 15:29:32 -07001096 audio_devices_t newDevice;
Eric Laurentc40d9692016-04-13 19:14:13 -07001097 AudioMix *policyMix = NULL;
1098 const char *address = NULL;
Eric Laurentc75307b2015-03-17 15:29:32 -07001099 if (outputDesc->mPolicyMix != NULL) {
Eric Laurentc40d9692016-04-13 19:14:13 -07001100 policyMix = outputDesc->mPolicyMix;
1101 address = policyMix->mDeviceAddress.string();
1102 if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
1103 newDevice = policyMix->mDeviceType;
1104 } else {
1105 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1106 }
Eric Laurent493404d2015-04-21 15:07:36 -07001107 } else if (mOutputRoutes.hasRouteChanged(session)) {
1108 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001109 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001110 } else {
1111 newDevice = AUDIO_DEVICE_NONE;
1112 }
1113
1114 uint32_t delayMs = 0;
1115
Eric Laurent733ce942017-12-07 12:18:25 -08001116 status = startSource(outputDesc, stream, newDevice, address, &delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07001117
1118 if (status != NO_ERROR) {
1119 mOutputRoutes.decRouteActivity(session);
Eric Laurent733ce942017-12-07 12:18:25 -08001120 outputDesc->stop();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001121 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001122 }
1123 // Automatically enable the remote submix input when output is started on a re routing mix
1124 // of type MIX_TYPE_RECORDERS
Eric Laurentc40d9692016-04-13 19:14:13 -07001125 if (audio_is_remote_submix_device(newDevice) && policyMix != NULL &&
1126 policyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001127 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1128 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Eric Laurentc40d9692016-04-13 19:14:13 -07001129 address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001130 "remote-submix");
1131 }
1132
1133 if (delayMs != 0) {
1134 usleep(delayMs * 1000);
1135 }
1136
1137 return status;
1138}
1139
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001140status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurentc75307b2015-03-17 15:29:32 -07001141 audio_stream_type_t stream,
1142 audio_devices_t device,
Eric Laurentc40d9692016-04-13 19:14:13 -07001143 const char *address,
Eric Laurentc75307b2015-03-17 15:29:32 -07001144 uint32_t *delayMs)
1145{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001146 // cannot start playback of STREAM_TTS if any other output is being used
1147 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001148
1149 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001150 if (stream == AUDIO_STREAM_TTS) {
1151 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001152 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001153 return INVALID_OPERATION;
1154 } else {
1155 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1156 }
1157 } else {
1158 // some playback other than beacon starts
1159 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1160 }
1161
Eric Laurent77305a62016-07-25 16:39:22 -07001162 // force device change if the output is inactive and no audio patch is already present.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001163 // check active before incrementing usage count
Eric Laurent77305a62016-07-25 16:39:22 -07001164 bool force = !outputDesc->isActive() &&
1165 (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001166
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001167 // requiresMuteCheck is false when we can bypass mute strategy.
1168 // It covers a common case when there is no materially active audio
1169 // and muting would result in unnecessary delay and dropped audio.
1170 const uint32_t outputLatencyMs = outputDesc->latency();
1171 bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain
1172
Eric Laurente552edb2014-03-10 17:42:56 -07001173 // increment usage count for this stream on the requested output:
1174 // NOTE that the usage count is the same for duplicated output and hardware output which is
1175 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1176 outputDesc->changeRefCount(stream, 1);
1177
Eric Laurent36829f92017-04-07 19:04:42 -07001178 if (stream == AUDIO_STREAM_MUSIC) {
1179 selectOutputForMusicEffects();
1180 }
1181
Eric Laurent493404d2015-04-21 15:07:36 -07001182 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001183 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001184 if (device == AUDIO_DEVICE_NONE) {
1185 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001186 }
Eric Laurent36829f92017-04-07 19:04:42 -07001187
Eric Laurente552edb2014-03-10 17:42:56 -07001188 routing_strategy strategy = getStrategy(stream);
1189 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001190 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1191 (beaconMuteLatency > 0);
1192 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001193 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001194 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001195 if (desc != outputDesc) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001196 // An output has a shared device if
1197 // - managed by the same hw module
1198 // - supports the currently selected device
1199 const bool sharedDevice = outputDesc->sharesHwModuleWith(desc)
1200 && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE;
1201
Eric Laurent77305a62016-07-25 16:39:22 -07001202 // force a device change if any other output is:
1203 // - managed by the same hw module
Jean-Michel Trivi4a5b4812018-02-08 17:22:32 +00001204 // - supports currently selected device
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001205 // - has a current device selection that differs from selected device.
Eric Laurent77305a62016-07-25 16:39:22 -07001206 // - has an active audio patch
Eric Laurente552edb2014-03-10 17:42:56 -07001207 // In this case, the audio HAL must receive the new device selection so that it can
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001208 // change the device currently selected by the other output.
1209 if (sharedDevice &&
Eric Laurent77305a62016-07-25 16:39:22 -07001210 desc->device() != device &&
Eric Laurent77305a62016-07-25 16:39:22 -07001211 desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001212 force = true;
1213 }
1214 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001215 // a notification so that audio focus effect can propagate, or that a mute/unmute
1216 // event occurred for beacon
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001217 const uint32_t latencyMs = desc->latency();
1218 const bool isActive = desc->isActive(latencyMs * 2); // account for drain
1219
1220 if (shouldWait && isActive && (waitMs < latencyMs)) {
1221 waitMs = latencyMs;
Eric Laurente552edb2014-03-10 17:42:56 -07001222 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001223
1224 // Require mute check if another output is on a shared device
1225 // and currently active to have proper drain and avoid pops.
1226 // Note restoring AudioTracks onto this output needs to invoke
1227 // a volume ramp if there is no mute.
1228 requiresMuteCheck |= sharedDevice && isActive;
Eric Laurente552edb2014-03-10 17:42:56 -07001229 }
1230 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001231
1232 const uint32_t muteWaitMs =
1233 setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07001234
1235 // handle special case for sonification while in call
1236 if (isInCall()) {
1237 handleIncallSonification(stream, true, false);
1238 }
1239
1240 // apply volume rules for current stream and device if necessary
1241 checkAndSetVolume(stream,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001242 mVolumeCurves->getVolumeIndex(stream, outputDesc->device()),
Eric Laurentc75307b2015-03-17 15:29:32 -07001243 outputDesc,
Shuhei Miyazaki6fe70332017-07-31 15:21:28 +09001244 outputDesc->device());
Eric Laurente552edb2014-03-10 17:42:56 -07001245
1246 // update the outputs if starting an output with a stream that can affect notification
1247 // routing
1248 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001249
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001250 // force reevaluating accessibility routing when ringtone or alarm starts
1251 if (strategy == STRATEGY_SONIFICATION) {
1252 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1253 }
Eric Laurentdc462862016-07-19 12:29:53 -07001254
1255 if (waitMs > muteWaitMs) {
1256 *delayMs = waitMs - muteWaitMs;
1257 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00001258
1259 // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change.
1260 // A volume change enacted by APM with 0 delay is not synchronous, as it goes
1261 // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume
1262 // change occurs after the MixerThread starts and causes a stream volume
1263 // glitch.
1264 //
1265 // We do not introduce additional delay here.
Eric Laurente552edb2014-03-10 17:42:56 -07001266 }
Eric Laurentdc462862016-07-19 12:29:53 -07001267
Eric Laurente552edb2014-03-10 17:42:56 -07001268 return NO_ERROR;
1269}
1270
1271
Eric Laurente0720872014-03-11 09:30:41 -07001272status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001273 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001274 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001275{
1276 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1277 ssize_t index = mOutputs.indexOfKey(output);
1278 if (index < 0) {
1279 ALOGW("stopOutput() unknown output %d", output);
1280 return BAD_VALUE;
1281 }
1282
Eric Laurentc75307b2015-03-17 15:29:32 -07001283 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001284
Eric Laurentc75307b2015-03-17 15:29:32 -07001285 if (outputDesc->mRefCount[stream] == 1) {
1286 // Automatically disable the remote submix input when output is stopped on a
1287 // re routing mix of type MIX_TYPE_RECORDERS
1288 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1289 outputDesc->mPolicyMix != NULL &&
1290 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1291 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1292 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001293 outputDesc->mPolicyMix->mDeviceAddress,
Eric Laurentc75307b2015-03-17 15:29:32 -07001294 "remote-submix");
1295 }
1296 }
1297
1298 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001299 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001300 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001301 int activityCount = mOutputRoutes.decRouteActivity(session);
1302 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1303
1304 if (forceDeviceUpdate) {
1305 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1306 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001307 }
1308
Eric Laurent3974e3b2017-12-07 17:58:43 -08001309 status_t status = stopSource(outputDesc, stream, forceDeviceUpdate);
1310
Eric Laurent733ce942017-12-07 12:18:25 -08001311 if (status == NO_ERROR ) {
1312 outputDesc->stop();
Eric Laurent3974e3b2017-12-07 17:58:43 -08001313 }
1314 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001315}
1316
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001317status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001318 audio_stream_type_t stream,
1319 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001320{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001321 // always handle stream stop, check which stream type is stopping
1322 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1323
Eric Laurente552edb2014-03-10 17:42:56 -07001324 // handle special case for sonification while in call
1325 if (isInCall()) {
1326 handleIncallSonification(stream, false, false);
1327 }
1328
1329 if (outputDesc->mRefCount[stream] > 0) {
1330 // decrement usage count of this stream on the output
1331 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001332
Eric Laurente552edb2014-03-10 17:42:56 -07001333 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001334 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001335 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001336 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001337 // delay the device switch by twice the latency because stopOutput() is executed when
1338 // the track stop() command is received and at that time the audio track buffer can
1339 // still contain data that needs to be drained. The latency only covers the audio HAL
1340 // and kernel buffers. Also the latency does not always include additional delay in the
1341 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001342 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001343
1344 // force restoring the device selection on other active outputs if it differs from the
1345 // one being selected for this output
Eric Laurent57de36c2016-09-28 16:59:11 -07001346 uint32_t delayMs = outputDesc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -07001347 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001348 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001349 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001350 desc->isActive() &&
1351 outputDesc->sharesHwModuleWith(desc) &&
1352 (newDevice != desc->device())) {
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001353 audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/);
1354 bool force = desc->device() != newDevice2;
Eric Laurentc75307b2015-03-17 15:29:32 -07001355 setOutputDevice(desc,
Haynes Mathew George11c499a2016-08-26 12:08:25 -07001356 newDevice2,
1357 force,
Eric Laurent57de36c2016-09-28 16:59:11 -07001358 delayMs);
1359 // re-apply device specific volume if not done by setOutputDevice()
1360 if (!force) {
1361 applyStreamVolumes(desc, newDevice2, delayMs);
1362 }
Eric Laurente552edb2014-03-10 17:42:56 -07001363 }
1364 }
1365 // update the outputs if stopping one with a stream that can affect notification routing
1366 handleNotificationRoutingForStream(stream);
1367 }
Eric Laurent36829f92017-04-07 19:04:42 -07001368 if (stream == AUDIO_STREAM_MUSIC) {
1369 selectOutputForMusicEffects();
1370 }
Eric Laurente552edb2014-03-10 17:42:56 -07001371 return NO_ERROR;
1372 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001373 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001374 return INVALID_OPERATION;
1375 }
1376}
1377
Eric Laurente83b55d2014-11-14 10:06:21 -08001378void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001379 audio_stream_type_t stream __unused,
1380 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001381{
1382 ALOGV("releaseOutput() %d", output);
1383 ssize_t index = mOutputs.indexOfKey(output);
1384 if (index < 0) {
1385 ALOGW("releaseOutput() releasing unknown output %d", output);
1386 return;
1387 }
1388
Paul McLeanaa981192015-03-21 09:55:15 -07001389 // Routing
1390 mOutputRoutes.removeRoute(session);
1391
Eric Laurentc75307b2015-03-17 15:29:32 -07001392 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001393 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001394 if (desc->mDirectOpenCount <= 0) {
1395 ALOGW("releaseOutput() invalid open count %d for output %d",
1396 desc->mDirectOpenCount, output);
1397 return;
1398 }
1399 if (--desc->mDirectOpenCount == 0) {
1400 closeOutput(output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001401 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001402 }
1403 }
1404}
1405
1406
Eric Laurentcaf7f482014-11-25 17:50:47 -08001407status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1408 audio_io_handle_t *input,
1409 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001410 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001411 const audio_config_base_t *config,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001412 audio_input_flags_t flags,
Eric Laurent2ac76942017-06-22 17:17:09 -07001413 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001414 input_type_t *inputType,
1415 audio_port_handle_t *portId)
Eric Laurente552edb2014-03-10 17:42:56 -07001416{
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001417 ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x,"
Eric Laurentcaf7f482014-11-25 17:50:47 -08001418 "session %d, flags %#x",
Eric Laurent20b9ef02016-12-05 11:03:16 -08001419 attr->source, config->sample_rate, config->format, config->channel_mask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001420
Eric Laurentad2e7b92017-09-14 20:06:42 -07001421 status_t status = NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -08001422 // handle legacy remote submix case where the address was not always specified
1423 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001424 audio_source_t halInputSource;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001425 audio_source_t inputSource = attr->source;
Eric Laurentc722f302014-12-10 11:21:49 -08001426 AudioMix *policyMix = NULL;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001427 DeviceVector inputDevices;
Eric Laurent20b9ef02016-12-05 11:03:16 -08001428
Eric Laurentfe231122017-11-17 17:48:06 -08001429 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1430 inputSource = AUDIO_SOURCE_MIC;
1431 }
1432
Paul McLean466dc8e2015-04-17 13:15:36 -06001433 // Explicit routing?
1434 sp<DeviceDescriptor> deviceDesc;
Eric Laurent2ac76942017-06-22 17:17:09 -07001435 if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) {
jiabinc0c831a2018-01-29 17:55:15 -08001436 deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId);
Paul McLean466dc8e2015-04-17 13:15:36 -06001437 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001438 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001439
Eric Laurentad2e7b92017-09-14 20:06:42 -07001440 // special case for mmap capture: if an input IO handle is specified, we reuse this input if
1441 // possible
1442 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ &&
1443 *input != AUDIO_IO_HANDLE_NONE) {
1444 ssize_t index = mInputs.indexOfKey(*input);
1445 if (index < 0) {
1446 ALOGW("getInputForAttr() unknown MMAP input %d", *input);
1447 status = BAD_VALUE;
1448 goto error;
1449 }
1450 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1451 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1452 if (audioSession == 0) {
1453 ALOGW("getInputForAttr() unknown session %d on input %d", session, *input);
1454 status = BAD_VALUE;
1455 goto error;
1456 }
1457 // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger.
1458 // The second call is for the first active client and sets the UID. Any further call
1459 // corresponds to a new client and is only permitted from the same UId.
1460 if (audioSession->openCount() == 1) {
1461 audioSession->setUid(uid);
1462 } else if (audioSession->uid() != uid) {
1463 ALOGW("getInputForAttr() bad uid %d for session %d uid %d",
1464 uid, session, audioSession->uid());
1465 status = INVALID_OPERATION;
1466 goto error;
1467 }
1468 audioSession->changeOpenCount(1);
1469 *inputType = API_INPUT_LEGACY;
1470 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1471 *portId = AudioPort::getNextUniqueId();
1472 }
1473 inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice);
1474 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1475 : AUDIO_PORT_HANDLE_NONE;
1476 ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session);
1477
1478 return NO_ERROR;
1479 }
1480
1481 *input = AUDIO_IO_HANDLE_NONE;
1482 *inputType = API_INPUT_INVALID;
1483
Eric Laurentad2e7b92017-09-14 20:06:42 -07001484 halInputSource = inputSource;
1485
1486 // TODO: check for existing client for this port ID
1487 if (*portId == AUDIO_PORT_HANDLE_NONE) {
1488 *portId = AudioPort::getNextUniqueId();
1489 }
1490
1491 audio_devices_t device;
1492
Eric Laurentc447ded2015-01-06 08:47:05 -08001493 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001494 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001495 status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
1496 if (status != NO_ERROR) {
1497 goto error;
François Gaffie036e1e92015-03-19 10:16:24 +01001498 }
1499 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001500 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1501 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001502 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001503 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001504 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001505 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurentad2e7b92017-09-14 20:06:42 -07001506 status = BAD_VALUE;
1507 goto error;
Eric Laurent275e8e92014-11-30 15:14:47 -08001508 }
Eric Laurentc722f302014-12-10 11:21:49 -08001509 if (policyMix != NULL) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08001510 address = policyMix->mDeviceAddress;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001511 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1512 // there is an external policy, but this input is attached to a mix of recorders,
1513 // meaning it receives audio injected into the framework, so the recorder doesn't
1514 // know about it and is therefore considered "legacy"
1515 *inputType = API_INPUT_LEGACY;
1516 } else {
1517 // recording a mix of players defined by an external policy, we're rerouting for
1518 // an external policy
1519 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1520 }
Eric Laurentc722f302014-12-10 11:21:49 -08001521 } else if (audio_is_remote_submix_device(device)) {
1522 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001523 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001524 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1525 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001526 } else {
1527 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001528 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001529
Eric Laurent599c7582015-12-07 18:05:55 -08001530 }
1531
1532 *input = getInputForDevice(device, address, session, uid, inputSource,
Eric Laurentfe231122017-11-17 17:48:06 -08001533 config, flags,
Eric Laurent599c7582015-12-07 18:05:55 -08001534 policyMix);
1535 if (*input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001536 status = INVALID_OPERATION;
1537 goto error;
Eric Laurent599c7582015-12-07 18:05:55 -08001538 }
Eric Laurent20b9ef02016-12-05 11:03:16 -08001539
Eric Laurentad2e7b92017-09-14 20:06:42 -07001540 inputDevices = mAvailableInputDevices.getDevicesFromType(device);
Eric Laurent2ac76942017-06-22 17:17:09 -07001541 *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId()
1542 : AUDIO_PORT_HANDLE_NONE;
1543
1544 ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d",
1545 *input, *inputType, *selectedDeviceId);
1546
Eric Laurent599c7582015-12-07 18:05:55 -08001547 return NO_ERROR;
Eric Laurentad2e7b92017-09-14 20:06:42 -07001548
1549error:
1550 mInputRoutes.removeRoute(session);
1551 return status;
Eric Laurent599c7582015-12-07 18:05:55 -08001552}
1553
1554
1555audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1556 String8 address,
1557 audio_session_t session,
1558 uid_t uid,
1559 audio_source_t inputSource,
Eric Laurentfe231122017-11-17 17:48:06 -08001560 const audio_config_base_t *config,
Eric Laurent599c7582015-12-07 18:05:55 -08001561 audio_input_flags_t flags,
1562 AudioMix *policyMix)
1563{
1564 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1565 audio_source_t halInputSource = inputSource;
1566 bool isSoundTrigger = false;
1567
1568 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1569 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1570 if (index >= 0) {
1571 input = mSoundTriggerSessions.valueFor(session);
1572 isSoundTrigger = true;
1573 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1574 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1575 } else {
1576 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001577 }
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001578 } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Eric Laurentfe231122017-11-17 17:48:06 -08001579 audio_is_linear_pcm(config->format)) {
Haynes Mathew George851d3ff2017-06-19 20:01:57 -07001580 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001581 }
1582
Andy Hungf129b032015-04-07 13:45:50 -07001583 // find a compatible input profile (not necessarily identical in parameters)
1584 sp<IOProfile> profile;
Eric Laurentfe231122017-11-17 17:48:06 -08001585 // sampling rate and flags may be updated by getInputProfile
1586 uint32_t profileSamplingRate = (config->sample_rate == 0) ?
1587 SAMPLE_RATE_HZ_DEFAULT : config->sample_rate;
1588 audio_format_t profileFormat = config->format;
1589 audio_channel_mask_t profileChannelMask = config->channel_mask;
Andy Hungf129b032015-04-07 13:45:50 -07001590 audio_input_flags_t profileFlags = flags;
1591 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001592 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001593 profileSamplingRate, profileFormat, profileChannelMask,
1594 profileFlags);
1595 if (profile != 0) {
1596 break; // success
Eric Laurent05067782016-06-01 18:27:28 -07001597 } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
1598 profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
Andy Hungf129b032015-04-07 13:45:50 -07001599 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1600 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1601 } else { // fail
Glenn Kastenfaa10a62017-05-25 15:52:05 -07001602 ALOGW("getInputForDevice() could not find profile for device 0x%X, "
Eric Laurentfe231122017-11-17 17:48:06 -08001603 "sampling rate %u, format %#x, channel mask 0x%X, flags %#x",
1604 device, config->sample_rate, config->format, config->channel_mask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001605 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001606 }
Eric Laurente552edb2014-03-10 17:42:56 -07001607 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001608 // Pick input sampling rate if not specified by client
Eric Laurentfe231122017-11-17 17:48:06 -08001609 uint32_t samplingRate = config->sample_rate;
Glenn Kasten05ddca52016-02-11 08:17:12 -08001610 if (samplingRate == 0) {
1611 samplingRate = profileSamplingRate;
1612 }
Eric Laurente552edb2014-03-10 17:42:56 -07001613
Eric Laurent322b4d22015-04-03 15:57:54 -07001614 if (profile->getModuleHandle() == 0) {
1615 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001616 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001617 }
1618
Eric Laurent599c7582015-12-07 18:05:55 -08001619 sp<AudioSession> audioSession = new AudioSession(session,
Eric Laurentfe231122017-11-17 17:48:06 -08001620 inputSource,
1621 config->format,
1622 samplingRate,
1623 config->channel_mask,
1624 flags,
1625 uid,
1626 isSoundTrigger,
1627 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001628
Eric Laurent74708e72017-04-07 17:13:42 -07001629// FIXME: disable concurrent capture until UI is ready
1630#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001631 // reuse an open input if possible
Eric Laurent555530a2017-02-07 18:17:24 -08001632 sp<AudioInputDescriptor> reusedInputDesc;
Eric Laurent599c7582015-12-07 18:05:55 -08001633 for (size_t i = 0; i < mInputs.size(); i++) {
1634 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001635 // reuse input if:
1636 // - it shares the same profile
1637 // AND
1638 // - it is not a reroute submix input
1639 // AND
1640 // - it is: not used for sound trigger
1641 // OR
1642 // used for sound trigger and all clients use the same session ID
1643 //
1644 if ((profile == desc->mProfile) &&
1645 (isSoundTrigger == desc->isSoundTrigger()) &&
1646 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001647
1648 sp<AudioSession> as = desc->getAudioSession(session);
1649 if (as != 0) {
1650 // do not allow unmatching properties on same session
1651 if (as->matches(audioSession)) {
1652 as->changeOpenCount(1);
1653 } else {
1654 ALOGW("getInputForDevice() record with different attributes"
1655 " exists for session %d", session);
Eric Laurent555530a2017-02-07 18:17:24 -08001656 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001657 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001658 } else if (isSoundTrigger) {
Eric Laurent555530a2017-02-07 18:17:24 -08001659 continue;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001660 }
Eric Laurentbb948092017-01-23 18:33:30 -08001661
Eric Laurent555530a2017-02-07 18:17:24 -08001662 // Reuse the already opened input stream on this profile if:
1663 // - the new capture source is background OR
1664 // - the path requested configurations match OR
1665 // - the new source priority is less than the highest source priority on this input
1666 // If the input stream cannot be reused, close it before opening a new stream
1667 // on the same profile for the new client so that the requested path configuration
1668 // can be selected.
1669 if (!isConcurrentSource(inputSource) &&
Eric Laurentbb948092017-01-23 18:33:30 -08001670 ((desc->mSamplingRate != samplingRate ||
Eric Laurentfe231122017-11-17 17:48:06 -08001671 desc->mChannelMask != config->channel_mask ||
1672 !audio_formats_match(desc->mFormat, config->format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001673 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
Eric Laurentbb948092017-01-23 18:33:30 -08001674 source_priority(inputSource)))) {
Eric Laurent555530a2017-02-07 18:17:24 -08001675 reusedInputDesc = desc;
1676 continue;
Eric Laurent599c7582015-12-07 18:05:55 -08001677 } else {
1678 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001679 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1680 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001681 }
Eric Laurent599c7582015-12-07 18:05:55 -08001682 }
1683 }
Eric Laurent599c7582015-12-07 18:05:55 -08001684
Eric Laurent555530a2017-02-07 18:17:24 -08001685 if (reusedInputDesc != 0) {
1686 AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/);
1687 for (size_t j = 0; j < sessions.size(); j++) {
1688 audio_session_t currentSession = sessions.keyAt(j);
1689 stopInput(reusedInputDesc->mIoHandle, currentSession);
1690 releaseInput(reusedInputDesc->mIoHandle, currentSession);
1691 }
1692 }
Eric Laurent74708e72017-04-07 17:13:42 -07001693#endif
Eric Laurent555530a2017-02-07 18:17:24 -08001694
Eric Laurent3974e3b2017-12-07 17:58:43 -08001695 if (!profile->canOpenNewIo()) {
1696 return AUDIO_IO_HANDLE_NONE;
1697 }
1698
Eric Laurentfe231122017-11-17 17:48:06 -08001699 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001700
Eric Laurentfe231122017-11-17 17:48:06 -08001701 audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER;
1702 lConfig.sample_rate = profileSamplingRate;
1703 lConfig.channel_mask = profileChannelMask;
1704 lConfig.format = profileFormat;
Eric Laurente3014102017-05-03 11:15:43 -07001705
Eric Laurent53b810e2017-12-10 17:25:10 -08001706 if (address == "") {
1707 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device);
1708 // the inputs vector must be of size >= 1, but we don't want to crash here
1709 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8("");
1710 }
1711
Eric Laurentfe231122017-11-17 17:48:06 -08001712 status_t status = inputDesc->open(&lConfig, device, address,
1713 halInputSource, profileFlags, &input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001714
1715 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001716 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentfe231122017-11-17 17:48:06 -08001717 (profileSamplingRate != lConfig.sample_rate) ||
1718 !audio_formats_match(profileFormat, lConfig.format) ||
1719 (profileChannelMask != lConfig.channel_mask)) {
1720 ALOGW("getInputForAttr() failed opening input: sampling rate %d"
Glenn Kasten49f36ba2017-12-06 13:02:02 -08001721 ", format %#x, channel mask %#x",
Eric Laurentfe231122017-11-17 17:48:06 -08001722 profileSamplingRate, profileFormat, profileChannelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001723 if (input != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfe231122017-11-17 17:48:06 -08001724 inputDesc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001725 }
Eric Laurent599c7582015-12-07 18:05:55 -08001726 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001727 }
1728
Eric Laurentc722f302014-12-10 11:21:49 -08001729 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001730 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001731
Eric Laurent599c7582015-12-07 18:05:55 -08001732 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001733 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001734
Eric Laurent599c7582015-12-07 18:05:55 -08001735 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001736}
1737
Eric Laurentbb948092017-01-23 18:33:30 -08001738//static
1739bool AudioPolicyManager::isConcurrentSource(audio_source_t source)
1740{
1741 return (source == AUDIO_SOURCE_HOTWORD) ||
1742 (source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
1743 (source == AUDIO_SOURCE_FM_TUNER);
1744}
1745
Eric Laurentfb66dd92016-01-28 18:32:03 -08001746bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1747 const sp<AudioSession>& audioSession)
1748{
1749 // Do not allow capture if an active voice call is using a software patch and
1750 // the call TX source device is on the same HW module.
1751 // FIXME: would be better to refine to only inputs whose profile connects to the
1752 // call TX device but this information is not in the audio patch
1753 if (mCallTxPatch != 0 &&
1754 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1755 return false;
1756 }
1757
1758 // starting concurrent capture is enabled if:
1759 // 1) capturing for re-routing
1760 // 2) capturing for HOTWORD source
1761 // 3) capturing for FM TUNER source
1762 // 3) All other active captures are either for re-routing or HOTWORD
1763
1764 if (is_virtual_input_device(inputDesc->mDevice) ||
Eric Laurentbb948092017-01-23 18:33:30 -08001765 isConcurrentSource(audioSession->inputSource())) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001766 return true;
1767 }
1768
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001769 for (const auto& activeInput : mInputs.getActiveInputs()) {
Eric Laurentbb948092017-01-23 18:33:30 -08001770 if (!isConcurrentSource(activeInput->inputSource(true)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001771 !is_virtual_input_device(activeInput->mDevice)) {
1772 return false;
1773 }
1774 }
1775
1776 return true;
1777}
1778
Chris Thornton2b864642017-06-27 21:26:07 -07001779// FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537.
1780bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() {
1781 if (!mHasComputedSoundTriggerSupportsConcurrentCapture) {
1782 bool soundTriggerSupportsConcurrentCapture = false;
1783 unsigned int numModules = 0;
1784 struct sound_trigger_module_descriptor* nModules = NULL;
1785
1786 status_t status = SoundTrigger::listModules(nModules, &numModules);
1787 if (status == NO_ERROR && numModules != 0) {
1788 nModules = (struct sound_trigger_module_descriptor*) calloc(
1789 numModules, sizeof(struct sound_trigger_module_descriptor));
1790 if (nModules == NULL) {
1791 // We failed to malloc the buffer, so just say no for now, and hope that we have more
1792 // ram the next time this function is called.
1793 ALOGE("Failed to allocate buffer for module descriptors");
1794 return false;
1795 }
1796
1797 status = SoundTrigger::listModules(nModules, &numModules);
1798 if (status == NO_ERROR) {
1799 soundTriggerSupportsConcurrentCapture = true;
1800 for (size_t i = 0; i < numModules; ++i) {
1801 soundTriggerSupportsConcurrentCapture &=
1802 nModules[i].properties.concurrent_capture;
1803 }
1804 }
1805 free(nModules);
1806 }
1807 mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture;
1808 mHasComputedSoundTriggerSupportsConcurrentCapture = true;
1809 }
1810 return mSoundTriggerSupportsConcurrentCapture;
1811}
1812
Eric Laurentfb66dd92016-01-28 18:32:03 -08001813
Eric Laurent4dc68062014-07-28 17:26:49 -07001814status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001815 audio_session_t session,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001816 bool silenced,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001817 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001818{
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001819
1820 ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)",
1821 input, session, silenced, *concurrency);
1822
Eric Laurentfb66dd92016-01-28 18:32:03 -08001823 *concurrency = API_INPUT_CONCURRENCY_NONE;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001824
Eric Laurente552edb2014-03-10 17:42:56 -07001825 ssize_t index = mInputs.indexOfKey(input);
1826 if (index < 0) {
1827 ALOGW("startInput() unknown input %d", input);
1828 return BAD_VALUE;
1829 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001830 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001831
Eric Laurent599c7582015-12-07 18:05:55 -08001832 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1833 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001834 ALOGW("startInput() unknown session %d on input %d", session, input);
1835 return BAD_VALUE;
1836 }
1837
Eric Laurent74708e72017-04-07 17:13:42 -07001838// FIXME: disable concurrent capture until UI is ready
1839#if 0
Eric Laurentfb66dd92016-01-28 18:32:03 -08001840 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1841 ALOGW("startInput(%d) failed: other input already started", input);
1842 return INVALID_OPERATION;
1843 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001844
Eric Laurentfb66dd92016-01-28 18:32:03 -08001845 if (isInCall()) {
1846 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1847 }
Eric Laurentf7c50102016-09-30 15:19:43 -07001848 if (mInputs.activeInputsCountOnDevices() != 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001849 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001850 }
Eric Laurent74708e72017-04-07 17:13:42 -07001851#else
1852 if (!is_virtual_input_device(inputDesc->mDevice)) {
1853 if (mCallTxPatch != 0 &&
1854 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1855 ALOGW("startInput(%d) failed: call in progress", input);
1856 return INVALID_OPERATION;
1857 }
1858
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001859 Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs();
Eric Laurent74708e72017-04-07 17:13:42 -07001860
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001861 // If a UID is idle and records silence and another not silenced recording starts
1862 // from another UID (idle or active) we stop the current idle UID recording in
1863 // favor of the new one - "There can be only one" TM
1864 if (!silenced) {
1865 for (const auto& activeDesc : activeInputs) {
1866 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1867 activeDesc->getId() == inputDesc->getId()) {
1868 continue;
1869 }
1870
1871 AudioSessionCollection activeSessions = activeDesc->getAudioSessions(
1872 true /*activeOnly*/);
1873 sp<AudioSession> activeSession = activeSessions.valueAt(0);
1874 if (activeSession->isSilenced()) {
1875 audio_io_handle_t activeInput = activeDesc->mIoHandle;
1876 audio_session_t activeSessionId = activeSession->session();
1877 stopInput(activeInput, activeSessionId);
1878 releaseInput(activeInput, activeSessionId);
1879 ALOGV("startInput(%d) stopping silenced input %d", input, activeInput);
1880 activeInputs = mInputs.getActiveInputs();
1881 }
1882 }
1883 }
1884
1885 for (const auto& activeDesc : activeInputs) {
Eric Laurentcb4dae22017-07-01 19:39:32 -07001886 if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 &&
1887 activeDesc->getId() == inputDesc->getId()) {
1888 continue;
1889 }
1890
Eric Laurent74708e72017-04-07 17:13:42 -07001891 audio_source_t activeSource = activeDesc->inputSource(true);
1892 if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) {
1893 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1894 if (activeDesc->hasPreemptedSession(session)) {
1895 ALOGW("startInput(%d) failed for HOTWORD: "
1896 "other input %d already started for HOTWORD",
1897 input, activeDesc->mIoHandle);
1898 return INVALID_OPERATION;
1899 }
1900 } else {
1901 ALOGV("startInput(%d) failed for HOTWORD: other input %d already started",
1902 input, activeDesc->mIoHandle);
1903 return INVALID_OPERATION;
1904 }
1905 } else {
1906 if (activeSource != AUDIO_SOURCE_HOTWORD) {
1907 ALOGW("startInput(%d) failed: other input %d already started",
1908 input, activeDesc->mIoHandle);
1909 return INVALID_OPERATION;
1910 }
1911 }
1912 }
1913
Chris Thornton2b864642017-06-27 21:26:07 -07001914 // We only need to check if the sound trigger session supports concurrent capture if the
1915 // input is also a sound trigger input. Otherwise, we should preempt any hotword stream
1916 // that's running.
1917 const bool allowConcurrentWithSoundTrigger =
1918 inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false;
1919
Eric Laurent74708e72017-04-07 17:13:42 -07001920 // if capture is allowed, preempt currently active HOTWORD captures
Mikhail Naganovcf84e592017-12-07 11:25:11 -08001921 for (const auto& activeDesc : activeInputs) {
Chris Thornton2b864642017-06-27 21:26:07 -07001922 if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) {
1923 continue;
1924 }
1925
Eric Laurent74708e72017-04-07 17:13:42 -07001926 audio_source_t activeSource = activeDesc->inputSource(true);
1927 if (activeSource == AUDIO_SOURCE_HOTWORD) {
1928 AudioSessionCollection activeSessions =
1929 activeDesc->getAudioSessions(true /*activeOnly*/);
1930 audio_session_t activeSession = activeSessions.keyAt(0);
1931 audio_io_handle_t activeHandle = activeDesc->mIoHandle;
1932 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1933 sessions.add(activeSession);
1934 inputDesc->setPreemptedSessions(sessions);
1935 stopInput(activeHandle, activeSession);
1936 releaseInput(activeHandle, activeSession);
1937 ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d",
1938 input, activeDesc->mIoHandle);
1939 }
1940 }
1941 }
1942#endif
Eric Laurente552edb2014-03-10 17:42:56 -07001943
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001944 // Make sure we start with the correct silence state
1945 audioSession->setSilenced(silenced);
1946
Eric Laurent313d1e72016-01-29 09:56:57 -08001947 // increment activity count before calling getNewInputDevice() below as only active sessions
1948 // are considered for device selection
1949 audioSession->changeActiveCount(1);
1950
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001951 // Routing?
1952 mInputRoutes.incRouteActivity(session);
1953
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001954 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Eric Laurent271a93e2016-09-29 14:47:06 -07001955 // indicate active capture to sound trigger service if starting capture from a mic on
1956 // primary HW module
Eric Laurentf7c50102016-09-30 15:19:43 -07001957 audio_devices_t device = getNewInputDevice(inputDesc);
Eric Laurent271a93e2016-09-29 14:47:06 -07001958 setInputDevice(input, device, true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001959
Eric Laurent733ce942017-12-07 12:18:25 -08001960 status_t status = inputDesc->start();
1961 if (status != NO_ERROR) {
1962 mInputRoutes.decRouteActivity(session);
1963 audioSession->changeActiveCount(-1);
1964 return status;
1965 }
Eric Laurent3974e3b2017-12-07 17:58:43 -08001966
Eric Laurent733ce942017-12-07 12:18:25 -08001967 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001968 // if input maps to a dynamic policy with an activity listener, notify of state change
1969 if ((inputDesc->mPolicyMix != NULL)
1970 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1971 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
1972 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001973 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001974
Eric Laurentf7c50102016-09-30 15:19:43 -07001975 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
1976 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Eric Laurent05b345e2016-11-18 12:36:27 -08001977 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) {
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07001978 SoundTrigger::setCaptureState(true);
1979 }
1980
1981 // automatically enable the remote submix output when input is started if not
1982 // used by a policy mix of type MIX_TYPE_RECORDERS
1983 // For remote submix (a virtual device), we open only one input per capture request.
1984 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1985 String8 address = String8("");
1986 if (inputDesc->mPolicyMix == NULL) {
1987 address = String8("0");
1988 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1989 address = inputDesc->mPolicyMix->mDeviceAddress;
1990 }
1991 if (address != "") {
1992 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1993 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1994 address, "remote-submix");
1995 }
Eric Laurentc722f302014-12-10 11:21:49 -08001996 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001997 }
Eric Laurente552edb2014-03-10 17:42:56 -07001998 }
1999
Eric Laurent599c7582015-12-07 18:05:55 -08002000 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07002001
Eric Laurente552edb2014-03-10 17:42:56 -07002002 return NO_ERROR;
2003}
2004
Eric Laurent4dc68062014-07-28 17:26:49 -07002005status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
2006 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002007{
2008 ALOGV("stopInput() input %d", input);
2009 ssize_t index = mInputs.indexOfKey(input);
2010 if (index < 0) {
2011 ALOGW("stopInput() unknown input %d", input);
2012 return BAD_VALUE;
2013 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002014 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07002015
Eric Laurent599c7582015-12-07 18:05:55 -08002016 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07002017 if (index < 0) {
2018 ALOGW("stopInput() unknown session %d on input %d", session, input);
2019 return BAD_VALUE;
2020 }
2021
Eric Laurent599c7582015-12-07 18:05:55 -08002022 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002023 ALOGW("stopInput() input %d already stopped", input);
2024 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002025 }
2026
Eric Laurent599c7582015-12-07 18:05:55 -08002027 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06002028
2029 // Routing?
2030 mInputRoutes.decRouteActivity(session);
2031
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002032 if (audioSession->activeCount() == 0) {
Eric Laurent733ce942017-12-07 12:18:25 -08002033 inputDesc->stop();
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002034 if (inputDesc->isActive()) {
2035 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
2036 } else {
2037 // if input maps to a dynamic policy with an activity listener, notify of state change
2038 if ((inputDesc->mPolicyMix != NULL)
2039 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
2040 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
2041 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00002042 }
Jean-Michel Trivieb6421d2016-03-17 12:32:52 -07002043
2044 // automatically disable the remote submix output when input is stopped if not
2045 // used by a policy mix of type MIX_TYPE_RECORDERS
2046 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
2047 String8 address = String8("");
2048 if (inputDesc->mPolicyMix == NULL) {
2049 address = String8("0");
2050 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
2051 address = inputDesc->mPolicyMix->mDeviceAddress;
2052 }
2053 if (address != "") {
2054 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2055 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2056 address, "remote-submix");
2057 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002058 }
Eric Laurent84332aa2016-01-28 22:19:18 +00002059
Eric Laurentf7c50102016-09-30 15:19:43 -07002060 audio_devices_t device = inputDesc->mDevice;
Eric Laurentfb66dd92016-01-28 18:32:03 -08002061 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00002062
Eric Laurentf7c50102016-09-30 15:19:43 -07002063 // indicate inactive capture to sound trigger service if stopping capture from a mic on
2064 // primary HW module
2065 audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
2066 if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
2067 mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08002068 SoundTrigger::setCaptureState(false);
2069 }
2070 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00002071 }
Eric Laurente552edb2014-03-10 17:42:56 -07002072 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002073 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07002074}
2075
Eric Laurent4dc68062014-07-28 17:26:49 -07002076void AudioPolicyManager::releaseInput(audio_io_handle_t input,
2077 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07002078{
2079 ALOGV("releaseInput() %d", input);
2080 ssize_t index = mInputs.indexOfKey(input);
2081 if (index < 0) {
2082 ALOGW("releaseInput() releasing unknown input %d", input);
2083 return;
2084 }
Paul McLean466dc8e2015-04-17 13:15:36 -06002085
2086 // Routing
2087 mInputRoutes.removeRoute(session);
2088
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002089 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
2090 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07002091
Eric Laurent599c7582015-12-07 18:05:55 -08002092 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
vivek mehta587b8df2017-01-31 14:58:44 -08002093 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07002094 ALOGW("releaseInput() unknown session %d on input %d", session, input);
2095 return;
2096 }
Eric Laurent599c7582015-12-07 18:05:55 -08002097
2098 if (audioSession->openCount() == 0) {
2099 ALOGW("releaseInput() invalid open count %d on session %d",
2100 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002101 return;
2102 }
Eric Laurent599c7582015-12-07 18:05:55 -08002103
2104 if (audioSession->changeOpenCount(-1) == 0) {
2105 inputDesc->removeAudioSession(session);
2106 }
2107
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08002108 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002109 ALOGV("releaseInput() exit > 0");
2110 return;
2111 }
2112
Eric Laurent05b90f82014-08-27 15:32:29 -07002113 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07002114 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07002115 ALOGV("releaseInput() exit");
2116}
2117
Eric Laurentd4692962014-05-05 18:13:44 -07002118void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07002119 bool patchRemoved = false;
2120
Mikhail Naganov7e22e942017-12-07 10:04:29 -08002121 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07002122 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002123 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07002124 if (patch_index >= 0) {
2125 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07002126 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07002127 mAudioPatches.removeItemsAt(patch_index);
2128 patchRemoved = true;
2129 }
Eric Laurentfe231122017-11-17 17:48:06 -08002130 inputDesc->close();
Eric Laurentd4692962014-05-05 18:13:44 -07002131 }
2132 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08002133 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07002134 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07002135
2136 if (patchRemoved) {
2137 mpClientInterface->onAudioPatchListUpdate();
2138 }
Eric Laurentd4692962014-05-05 18:13:44 -07002139}
2140
Eric Laurente0720872014-03-11 09:30:41 -07002141void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002142 int indexMin,
2143 int indexMax)
2144{
2145 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002146 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08002147
2148 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002149 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2150 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002151 continue;
2152 }
2153 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08002154 }
Eric Laurente552edb2014-03-10 17:42:56 -07002155}
2156
Eric Laurente0720872014-03-11 09:30:41 -07002157status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01002158 int index,
2159 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07002160{
2161
Nadav Bar7c605f62017-12-25 00:01:52 +02002162 // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an
2163 // app that has MODIFY_PHONE_STATE permission.
2164 if (((index < mVolumeCurves->getVolumeIndexMin(stream)) &&
2165 !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) ||
François Gaffied1ab2bd2015-12-02 18:20:06 +01002166 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002167 return BAD_VALUE;
2168 }
2169 if (!audio_is_output_device(device)) {
2170 return BAD_VALUE;
2171 }
2172
2173 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01002174 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002175
Eric Laurent1fd372e2016-04-06 14:23:53 -07002176 ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d",
Eric Laurente552edb2014-03-10 17:42:56 -07002177 stream, device, index);
2178
Eric Laurent28d09f02016-03-08 10:43:05 -08002179 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08002180 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2181 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002182 continue;
2183 }
2184 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
2185 }
Eric Laurente552edb2014-03-10 17:42:56 -07002186
Eric Laurent1fd372e2016-04-06 14:23:53 -07002187 // update volume on all outputs and streams matching the following:
2188 // - The requested stream (or a stream matching for volume control) is active on the output
2189 // - The device (or devices) selected by the strategy corresponding to this stream includes
2190 // the requested device
2191 // - For non default requested device, currently selected device on the output is either the
2192 // requested device or one of the devices selected by the strategy
Eric Laurent5a2b6292016-04-14 18:05:57 -07002193 // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if
2194 // no specific device volume value exists for currently selected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002195 status_t status = NO_ERROR;
2196 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002197 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2198 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08002199 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
2200 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002201 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07002202 }
Eric Laurentd3926fe2016-04-15 18:10:07 -07002203 if (!(desc->isStreamActive((audio_stream_type_t)curStream) ||
2204 (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002205 continue;
2206 }
Eric Laurent794fde22016-03-11 09:50:45 -08002207 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002208 audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy(
2209 curStrategy, false /*fromCache*/));
Eric Laurente76f29c2016-09-14 17:17:53 -07002210 if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) &&
2211 ((curStreamDevice & device) == 0)) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002212 continue;
2213 }
Eric Laurente76f29c2016-09-14 17:17:53 -07002214 bool applyVolume;
Eric Laurent5a2b6292016-04-14 18:05:57 -07002215 if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurent1fd372e2016-04-06 14:23:53 -07002216 curStreamDevice |= device;
Eric Laurente76f29c2016-09-14 17:17:53 -07002217 applyVolume = (curDevice & curStreamDevice) != 0;
2218 } else {
2219 applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
Jean-Michel Trivib7fdce62017-06-27 19:38:42 -07002220 stream, curStreamDevice);
Eric Laurent1fd372e2016-04-06 14:23:53 -07002221 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002222
Eric Laurente76f29c2016-09-14 17:17:53 -07002223 if (applyVolume) {
Eric Laurentdc462862016-07-19 12:29:53 -07002224 //FIXME: workaround for truncated touch sounds
2225 // delayed volume change for system stream to be removed when the problem is
2226 // handled by system UI
Eric Laurent28d09f02016-03-08 10:43:05 -08002227 status_t volStatus =
Eric Laurentdc462862016-07-19 12:29:53 -07002228 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
2229 (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
Eric Laurent28d09f02016-03-08 10:43:05 -08002230 if (volStatus != NO_ERROR) {
2231 status = volStatus;
2232 }
2233 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08002234 }
Eric Laurente552edb2014-03-10 17:42:56 -07002235 }
2236 return status;
2237}
2238
Eric Laurente0720872014-03-11 09:30:41 -07002239status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07002240 int *index,
2241 audio_devices_t device)
2242{
2243 if (index == NULL) {
2244 return BAD_VALUE;
2245 }
2246 if (!audio_is_output_device(device)) {
2247 return BAD_VALUE;
2248 }
Eric Laurent5a2b6292016-04-14 18:05:57 -07002249 // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to
Eric Laurente552edb2014-03-10 17:42:56 -07002250 // the strategy the stream belongs to.
Eric Laurent5a2b6292016-04-14 18:05:57 -07002251 if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
Eric Laurente552edb2014-03-10 17:42:56 -07002252 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
2253 }
François Gaffiedfd74092015-03-19 12:10:59 +01002254 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07002255
François Gaffied1ab2bd2015-12-02 18:20:06 +01002256 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07002257 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
2258 return NO_ERROR;
2259}
2260
Eric Laurent36829f92017-04-07 19:04:42 -07002261audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects()
Eric Laurente552edb2014-03-10 17:42:56 -07002262{
2263 // select one output among several suitable for global effects.
2264 // The priority is as follows:
2265 // 1: An offloaded output. If the effect ends up not being offloadable,
2266 // AudioFlinger will invalidate the track and the offloaded output
2267 // will be closed causing the effect to be moved to a PCM output.
2268 // 2: A deep buffer output
Eric Laurent36829f92017-04-07 19:04:42 -07002269 // 3: The primary output
2270 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -07002271
Eric Laurent3b73df72014-03-11 09:06:29 -07002272 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07002273 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent36829f92017-04-07 19:04:42 -07002274 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -07002275
Eric Laurent36829f92017-04-07 19:04:42 -07002276 if (outputs.size() == 0) {
2277 return AUDIO_IO_HANDLE_NONE;
2278 }
Eric Laurente552edb2014-03-10 17:42:56 -07002279
Eric Laurent36829f92017-04-07 19:04:42 -07002280 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2281 bool activeOnly = true;
2282
2283 while (output == AUDIO_IO_HANDLE_NONE) {
2284 audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE;
2285 audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE;
2286 audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE;
2287
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002288 for (audio_io_handle_t output : outputs) {
2289 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output);
Eric Laurent36829f92017-04-07 19:04:42 -07002290 if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) {
2291 continue;
2292 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002293 ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x",
2294 activeOnly, output, desc->mFlags);
Eric Laurent36829f92017-04-07 19:04:42 -07002295 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002296 outputOffloaded = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002297 }
2298 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002299 outputDeepBuffer = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002300 }
2301 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002302 outputPrimary = output;
Eric Laurent36829f92017-04-07 19:04:42 -07002303 }
2304 }
2305 if (outputOffloaded != AUDIO_IO_HANDLE_NONE) {
2306 output = outputOffloaded;
2307 } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) {
2308 output = outputDeepBuffer;
2309 } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) {
2310 output = outputPrimary;
2311 } else {
2312 output = outputs[0];
2313 }
2314 activeOnly = false;
2315 }
2316
2317 if (output != mMusicEffectOutput) {
2318 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output);
2319 mMusicEffectOutput = output;
2320 }
2321
2322 ALOGV("selectOutputForMusicEffects selected output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -07002323 return output;
2324}
2325
Eric Laurent36829f92017-04-07 19:04:42 -07002326audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused)
2327{
2328 return selectOutputForMusicEffects();
2329}
2330
Eric Laurente0720872014-03-11 09:30:41 -07002331status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07002332 audio_io_handle_t io,
2333 uint32_t strategy,
2334 int session,
2335 int id)
2336{
2337 ssize_t index = mOutputs.indexOfKey(io);
2338 if (index < 0) {
2339 index = mInputs.indexOfKey(io);
2340 if (index < 0) {
2341 ALOGW("registerEffect() unknown io %d", io);
2342 return INVALID_OPERATION;
2343 }
2344 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002345 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002346}
2347
Eric Laurentc75307b2015-03-17 15:29:32 -07002348bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2349{
Eric Laurent28d09f02016-03-08 10:43:05 -08002350 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08002351 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
2352 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08002353 continue;
2354 }
2355 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
2356 }
Eric Laurent28d09f02016-03-08 10:43:05 -08002357 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07002358}
2359
2360bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2361{
2362 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2363}
2364
Eric Laurente0720872014-03-11 09:30:41 -07002365bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002366{
2367 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002368 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002369 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002370 return true;
2371 }
2372 }
2373 return false;
2374}
2375
Eric Laurent275e8e92014-11-30 15:14:47 -08002376// Register a list of custom mixes with their attributes and format.
2377// When a mix is registered, corresponding input and output profiles are
2378// added to the remote submix hw module. The profile contains only the
2379// parameters (sampling rate, format...) specified by the mix.
2380// The corresponding input remote submix device is also connected.
2381//
2382// When a remote submix device is connected, the address is checked to select the
2383// appropriate profile and the corresponding input or output stream is opened.
2384//
2385// When capture starts, getInputForAttr() will:
2386// - 1 look for a mix matching the address passed in attribtutes tags if any
2387// - 2 if none found, getDeviceForInputSource() will:
2388// - 2.1 look for a mix matching the attributes source
2389// - 2.2 if none found, default to device selection by policy rules
2390// At this time, the corresponding output remote submix device is also connected
2391// and active playback use cases can be transferred to this mix if needed when reconnecting
2392// after AudioTracks are invalidated
2393//
2394// When playback starts, getOutputForAttr() will:
2395// - 1 look for a mix matching the address passed in attribtutes tags if any
2396// - 2 if none found, look for a mix matching the attributes usage
2397// - 3 if none found, default to device and output selection by policy rules.
2398
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002399status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes)
Eric Laurent275e8e92014-11-30 15:14:47 -08002400{
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002401 ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size());
2402 status_t res = NO_ERROR;
2403
2404 sp<HwModule> rSubmixModule;
2405 // examine each mix's route type
2406 for (size_t i = 0; i < mixes.size(); i++) {
2407 // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination
2408 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) {
2409 res = INVALID_OPERATION;
Eric Laurent275e8e92014-11-30 15:14:47 -08002410 break;
2411 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002412 if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002413 ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002414 if (rSubmixModule == 0) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08002415 rSubmixModule = mHwModules.getModuleFromName(
2416 AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX);
2417 if (rSubmixModule == 0) {
2418 ALOGE(" Unable to find audio module for submix, aborting mix %zu registration",
2419 i);
2420 res = INVALID_OPERATION;
2421 break;
2422 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002423 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002424
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002425 String8 address = mixes[i].mDeviceAddress;
François Gaffie036e1e92015-03-19 10:16:24 +01002426
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002427 if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) {
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002428 ALOGE(" Error registering mix %zu for address %s", i, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002429 res = INVALID_OPERATION;
2430 break;
2431 }
2432 audio_config_t outputConfig = mixes[i].mFormat;
2433 audio_config_t inputConfig = mixes[i].mFormat;
2434 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2435 // stereo and let audio flinger do the channel conversion if needed.
2436 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2437 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2438 rSubmixModule->addOutputProfile(address, &outputConfig,
2439 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2440 rSubmixModule->addInputProfile(address, &inputConfig,
2441 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002442
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002443 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
2444 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2445 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2446 address.string(), "remote-submix");
2447 } else {
2448 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2449 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2450 address.string(), "remote-submix");
2451 }
2452 } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002453 String8 address = mixes[i].mDeviceAddress;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002454 audio_devices_t device = mixes[i].mDeviceType;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002455 ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s",
2456 i, mixes.size(), device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002457
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002458 bool foundOutput = false;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002459 for (size_t j = 0 ; j < mOutputs.size() ; j++) {
2460 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j);
2461 sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle());
2462 if ((patch != 0) && (patch->mPatch.num_sinks != 0)
2463 && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE)
2464 && (patch->mPatch.sinks[0].ext.device.type == device)
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002465 && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(),
2466 AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002467 if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) {
2468 res = INVALID_OPERATION;
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002469 } else {
2470 foundOutput = true;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002471 }
2472 break;
2473 }
2474 }
2475
2476 if (res != NO_ERROR) {
2477 ALOGE(" Error registering mix %zu for device 0x%X addr %s",
Jean-Michel Trivi5ac8cd42016-03-24 16:35:36 -07002478 i, device, address.string());
2479 res = INVALID_OPERATION;
2480 break;
2481 } else if (!foundOutput) {
2482 ALOGE(" Output not found for mix %zu for device 0x%X addr %s",
2483 i, device, address.string());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002484 res = INVALID_OPERATION;
2485 break;
2486 }
Eric Laurentc722f302014-12-10 11:21:49 -08002487 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002488 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002489 if (res != NO_ERROR) {
2490 unregisterPolicyMixes(mixes);
2491 }
2492 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002493}
2494
2495status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2496{
Eric Laurent7b279bb2015-12-14 10:18:23 -08002497 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002498 status_t res = NO_ERROR;
2499 sp<HwModule> rSubmixModule;
2500 // examine each mix's route type
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002501 for (const auto& mix : mixes) {
2502 if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) {
François Gaffie036e1e92015-03-19 10:16:24 +01002503
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002504 if (rSubmixModule == 0) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08002505 rSubmixModule = mHwModules.getModuleFromName(
2506 AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX);
2507 if (rSubmixModule == 0) {
2508 res = INVALID_OPERATION;
2509 continue;
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002510 }
2511 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002512
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002513 String8 address = mix.mDeviceAddress;
Eric Laurent275e8e92014-11-30 15:14:47 -08002514
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002515 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
2516 res = INVALID_OPERATION;
2517 continue;
2518 }
2519
2520 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2521 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2522 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2523 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2524 address.string(), "remote-submix");
2525 }
2526 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2527 AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2528 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2529 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2530 address.string(), "remote-submix");
2531 }
2532 rSubmixModule->removeOutputProfile(address);
2533 rSubmixModule->removeInputProfile(address);
2534
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002535 } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) {
2536 if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) {
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002537 res = INVALID_OPERATION;
2538 continue;
2539 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002540 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002541 }
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -08002542 return res;
Eric Laurent275e8e92014-11-30 15:14:47 -08002543}
2544
Eric Laurente552edb2014-03-10 17:42:56 -07002545
Eric Laurente0720872014-03-11 09:30:41 -07002546status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002547{
2548 const size_t SIZE = 256;
2549 char buffer[SIZE];
2550 String8 result;
2551
2552 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2553 result.append(buffer);
2554
Eric Laurent87ffa392015-05-22 10:32:38 -07002555 snprintf(buffer, SIZE, " Primary Output: %d\n",
2556 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002557 result.append(buffer);
Mikhail Naganov0d6a0332016-04-19 17:12:38 -07002558 std::string stateLiteral;
2559 AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral);
2560 snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str());
Eric Laurente552edb2014-03-10 17:42:56 -07002561 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002562 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002563 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002564 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002565 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002566 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002567 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002568 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002569 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002570 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002571 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002572 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002573 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002574 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002575 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002576 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2577 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2578 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002579 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2580 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002581 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2582 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002583
François Gaffie2110e042015-03-24 08:41:51 +01002584 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002585
François Gaffie112b0af2015-11-19 16:13:25 +01002586 mAvailableOutputDevices.dump(fd, String8("Available output"));
2587 mAvailableInputDevices.dump(fd, String8("Available input"));
Mikhail Naganovd4120142017-12-06 15:49:22 -08002588 mHwModulesAll.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002589 mOutputs.dump(fd);
2590 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002591 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002592 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002593 mAudioPatches.dump(fd);
Mikhail Naganov44344b02016-12-13 11:21:02 -08002594 mPolicyMixes.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002595
2596 return NO_ERROR;
2597}
2598
2599// This function checks for the parameters which can be offloaded.
2600// This can be enhanced depending on the capability of the DSP and policy
2601// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002602bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002603{
2604 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002605 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002606 offloadInfo.sample_rate, offloadInfo.channel_mask,
2607 offloadInfo.format,
2608 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2609 offloadInfo.has_video);
2610
Andy Hung2ddee192015-12-18 17:34:44 -08002611 if (mMasterMono) {
2612 return false; // no offloading if mono is set.
2613 }
2614
Eric Laurente552edb2014-03-10 17:42:56 -07002615 // Check if offload has been disabled
2616 char propValue[PROPERTY_VALUE_MAX];
2617 if (property_get("audio.offload.disable", propValue, "0")) {
2618 if (atoi(propValue) != 0) {
2619 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2620 return false;
2621 }
2622 }
2623
2624 // Check if stream type is music, then only allow offload as of now.
2625 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2626 {
2627 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2628 return false;
2629 }
2630
2631 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002632 const bool allowOffloadWithVideo =
2633 property_get_bool("audio.offload.video", false /* default_value */);
2634 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002635 ALOGV("isOffloadSupported: has_video == true, returning false");
2636 return false;
2637 }
2638
2639 //If duration is less than minimum value defined in property, return false
2640 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2641 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2642 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2643 return false;
2644 }
2645 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2646 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2647 return false;
2648 }
2649
2650 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2651 // creating an offloaded track and tearing it down immediately after start when audioflinger
2652 // detects there is an active non offloadable effect.
2653 // FIXME: We should check the audio session here but we do not have it in this context.
2654 // This may prevent offloading in rare situations where effects are left active by apps
2655 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002656 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002657 return false;
2658 }
2659
2660 // See if there is a profile to support this.
2661 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002662 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002663 offloadInfo.sample_rate,
2664 offloadInfo.format,
2665 offloadInfo.channel_mask,
2666 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002667 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2668 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002669}
2670
Eric Laurent6a94d692014-05-20 11:18:06 -07002671status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2672 audio_port_type_t type,
2673 unsigned int *num_ports,
2674 struct audio_port *ports,
2675 unsigned int *generation)
2676{
2677 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2678 generation == NULL) {
2679 return BAD_VALUE;
2680 }
2681 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2682 if (ports == NULL) {
2683 *num_ports = 0;
2684 }
2685
2686 size_t portsWritten = 0;
2687 size_t portsMax = *num_ports;
2688 *num_ports = 0;
2689 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002690 // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB
2691 // as they are used by stub HALs by convention
Eric Laurent6a94d692014-05-20 11:18:06 -07002692 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002693 for (const auto& dev : mAvailableOutputDevices) {
2694 if (dev->type() == AUDIO_DEVICE_OUT_STUB) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002695 continue;
2696 }
2697 if (portsWritten < portsMax) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002698 dev->toAudioPort(&ports[portsWritten++]);
Eric Laurent5a2b6292016-04-14 18:05:57 -07002699 }
2700 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002701 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002702 }
2703 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002704 for (const auto& dev : mAvailableInputDevices) {
2705 if (dev->type() == AUDIO_DEVICE_IN_STUB) {
Eric Laurent5a2b6292016-04-14 18:05:57 -07002706 continue;
2707 }
2708 if (portsWritten < portsMax) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08002709 dev->toAudioPort(&ports[portsWritten++]);
Eric Laurent5a2b6292016-04-14 18:05:57 -07002710 }
2711 (*num_ports)++;
Eric Laurent6a94d692014-05-20 11:18:06 -07002712 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002713 }
2714 }
2715 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2716 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2717 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2718 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2719 }
2720 *num_ports += mInputs.size();
2721 }
2722 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002723 size_t numOutputs = 0;
2724 for (size_t i = 0; i < mOutputs.size(); i++) {
2725 if (!mOutputs[i]->isDuplicated()) {
2726 numOutputs++;
2727 if (portsWritten < portsMax) {
2728 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2729 }
2730 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002731 }
Eric Laurent84c70242014-06-23 08:46:27 -07002732 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002733 }
2734 }
2735 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002736 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002737 return NO_ERROR;
2738}
2739
2740status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2741{
2742 return NO_ERROR;
2743}
2744
Eric Laurent6a94d692014-05-20 11:18:06 -07002745status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2746 audio_patch_handle_t *handle,
2747 uid_t uid)
2748{
2749 ALOGV("createAudioPatch()");
2750
2751 if (handle == NULL || patch == NULL) {
2752 return BAD_VALUE;
2753 }
2754 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2755
Eric Laurent874c42872014-08-08 15:13:39 -07002756 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2757 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2758 return BAD_VALUE;
2759 }
2760 // only one source per audio patch supported for now
2761 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002762 return INVALID_OPERATION;
2763 }
Eric Laurent874c42872014-08-08 15:13:39 -07002764
2765 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002766 return INVALID_OPERATION;
2767 }
Eric Laurent874c42872014-08-08 15:13:39 -07002768 for (size_t i = 0; i < patch->num_sinks; i++) {
2769 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2770 return INVALID_OPERATION;
2771 }
2772 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002773
2774 sp<AudioPatch> patchDesc;
2775 ssize_t index = mAudioPatches.indexOfKey(*handle);
2776
Eric Laurent6a94d692014-05-20 11:18:06 -07002777 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2778 patch->sources[0].role,
2779 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002780#if LOG_NDEBUG == 0
2781 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002782 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002783 patch->sinks[i].role,
2784 patch->sinks[i].type);
2785 }
2786#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002787
2788 if (index >= 0) {
2789 patchDesc = mAudioPatches.valueAt(index);
2790 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2791 mUidCached, patchDesc->mUid, uid);
2792 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2793 return INVALID_OPERATION;
2794 }
2795 } else {
Glenn Kastena13cde92016-03-28 15:26:02 -07002796 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07002797 }
2798
2799 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002800 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002801 if (outputDesc == NULL) {
2802 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2803 return BAD_VALUE;
2804 }
Eric Laurent84c70242014-06-23 08:46:27 -07002805 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2806 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002807 if (patchDesc != 0) {
2808 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2809 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2810 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2811 return BAD_VALUE;
2812 }
2813 }
Eric Laurent874c42872014-08-08 15:13:39 -07002814 DeviceVector devices;
2815 for (size_t i = 0; i < patch->num_sinks; i++) {
2816 // Only support mix to devices connection
2817 // TODO add support for mix to mix connection
2818 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2819 ALOGV("createAudioPatch() source mix but sink is not a device");
2820 return INVALID_OPERATION;
2821 }
2822 sp<DeviceDescriptor> devDesc =
2823 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2824 if (devDesc == 0) {
2825 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2826 return BAD_VALUE;
2827 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002828
François Gaffie53615e22015-03-19 09:24:12 +01002829 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002830 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002831 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002832 NULL, // updatedSamplingRate
2833 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002834 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002835 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002836 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002837 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002838 ALOGV("createAudioPatch() profile not supported for device %08x",
2839 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002840 return INVALID_OPERATION;
2841 }
2842 devices.add(devDesc);
2843 }
2844 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002845 return INVALID_OPERATION;
2846 }
Eric Laurent874c42872014-08-08 15:13:39 -07002847
Eric Laurent6a94d692014-05-20 11:18:06 -07002848 // TODO: reconfigure output format and channels here
2849 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002850 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002851 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002852 index = mAudioPatches.indexOfKey(*handle);
2853 if (index >= 0) {
2854 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2855 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2856 }
2857 patchDesc = mAudioPatches.valueAt(index);
2858 patchDesc->mUid = uid;
2859 ALOGV("createAudioPatch() success");
2860 } else {
2861 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2862 return INVALID_OPERATION;
2863 }
2864 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2865 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2866 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002867 // only one sink supported when connecting an input device to a mix
2868 if (patch->num_sinks > 1) {
2869 return INVALID_OPERATION;
2870 }
François Gaffie53615e22015-03-19 09:24:12 +01002871 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002872 if (inputDesc == NULL) {
2873 return BAD_VALUE;
2874 }
2875 if (patchDesc != 0) {
2876 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2877 return BAD_VALUE;
2878 }
2879 }
2880 sp<DeviceDescriptor> devDesc =
2881 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2882 if (devDesc == 0) {
2883 return BAD_VALUE;
2884 }
2885
François Gaffie53615e22015-03-19 09:24:12 +01002886 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002887 devDesc->mAddress,
2888 patch->sinks[0].sample_rate,
2889 NULL, /*updatedSampleRate*/
2890 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002891 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002892 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002893 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002894 // FIXME for the parameter type,
2895 // and the NONE
2896 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002897 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002898 return INVALID_OPERATION;
2899 }
2900 // TODO: reconfigure output format and channels here
2901 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002902 devDesc->type(), inputDesc->mIoHandle);
2903 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002904 index = mAudioPatches.indexOfKey(*handle);
2905 if (index >= 0) {
2906 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2907 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2908 }
2909 patchDesc = mAudioPatches.valueAt(index);
2910 patchDesc->mUid = uid;
2911 ALOGV("createAudioPatch() success");
2912 } else {
2913 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2914 return INVALID_OPERATION;
2915 }
2916 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2917 // device to device connection
2918 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002919 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002920 return BAD_VALUE;
2921 }
2922 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002923 sp<DeviceDescriptor> srcDeviceDesc =
2924 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002925 if (srcDeviceDesc == 0) {
2926 return BAD_VALUE;
2927 }
Eric Laurent874c42872014-08-08 15:13:39 -07002928
Eric Laurent6a94d692014-05-20 11:18:06 -07002929 //update source and sink with our own data as the data passed in the patch may
2930 // be incomplete.
2931 struct audio_patch newPatch = *patch;
2932 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002933
Eric Laurent874c42872014-08-08 15:13:39 -07002934 for (size_t i = 0; i < patch->num_sinks; i++) {
2935 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2936 ALOGV("createAudioPatch() source device but one sink is not a device");
2937 return INVALID_OPERATION;
2938 }
2939
2940 sp<DeviceDescriptor> sinkDeviceDesc =
2941 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2942 if (sinkDeviceDesc == 0) {
2943 return BAD_VALUE;
2944 }
2945 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2946
Eric Laurent3bcf8592015-04-03 12:13:24 -07002947 // create a software bridge in PatchPanel if:
Scott Randolphf3172402018-01-23 17:06:53 -08002948 // - source and sink devices are on different HW modules OR
Eric Laurent3bcf8592015-04-03 12:13:24 -07002949 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002950 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -07002951 (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002952 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002953 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002954 return INVALID_OPERATION;
2955 }
Eric Laurent874c42872014-08-08 15:13:39 -07002956 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002957 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002958 // if the sink device is reachable via an opened output stream, request to go via
2959 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002960 audio_io_handle_t output = selectOutput(outputs,
2961 AUDIO_OUTPUT_FLAG_NONE,
2962 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002963 if (output != AUDIO_IO_HANDLE_NONE) {
2964 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2965 if (outputDesc->isDuplicated()) {
2966 return INVALID_OPERATION;
2967 }
2968 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002969 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002970 newPatch.num_sources = 2;
2971 }
Eric Laurent83b88082014-06-20 18:31:16 -07002972 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002973 }
2974 // TODO: check from routing capabilities in config file and other conflicting patches
2975
2976 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2977 if (index >= 0) {
2978 afPatchHandle = patchDesc->mAfPatchHandle;
2979 }
2980
2981 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2982 &afPatchHandle,
2983 0);
2984 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2985 status, afPatchHandle);
2986 if (status == NO_ERROR) {
2987 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002988 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002989 addAudioPatch(patchDesc->mHandle, patchDesc);
2990 } else {
2991 patchDesc->mPatch = newPatch;
2992 }
2993 patchDesc->mAfPatchHandle = afPatchHandle;
2994 *handle = patchDesc->mHandle;
2995 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002996 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002997 } else {
2998 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2999 status);
3000 return INVALID_OPERATION;
3001 }
3002 } else {
3003 return BAD_VALUE;
3004 }
3005 } else {
3006 return BAD_VALUE;
3007 }
3008 return NO_ERROR;
3009}
3010
3011status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
3012 uid_t uid)
3013{
3014 ALOGV("releaseAudioPatch() patch %d", handle);
3015
3016 ssize_t index = mAudioPatches.indexOfKey(handle);
3017
3018 if (index < 0) {
3019 return BAD_VALUE;
3020 }
3021 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3022 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
3023 mUidCached, patchDesc->mUid, uid);
3024 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
3025 return INVALID_OPERATION;
3026 }
3027
3028 struct audio_patch *patch = &patchDesc->mPatch;
3029 patchDesc->mUid = mUidCached;
3030 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003031 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003032 if (outputDesc == NULL) {
3033 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
3034 return BAD_VALUE;
3035 }
3036
Eric Laurentc75307b2015-03-17 15:29:32 -07003037 setOutputDevice(outputDesc,
3038 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07003039 true,
3040 0,
3041 NULL);
3042 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
3043 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01003044 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07003045 if (inputDesc == NULL) {
3046 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
3047 return BAD_VALUE;
3048 }
3049 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08003050 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07003051 true,
3052 NULL);
3053 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003054 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3055 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
3056 status, patchDesc->mAfPatchHandle);
3057 removeAudioPatch(patchDesc->mHandle);
3058 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003059 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07003060 } else {
3061 return BAD_VALUE;
3062 }
3063 } else {
3064 return BAD_VALUE;
3065 }
3066 return NO_ERROR;
3067}
3068
3069status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
3070 struct audio_patch *patches,
3071 unsigned int *generation)
3072{
François Gaffie53615e22015-03-19 09:24:12 +01003073 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003074 return BAD_VALUE;
3075 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003076 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01003077 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07003078}
3079
Eric Laurente1715a42014-05-20 11:30:42 -07003080status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07003081{
Eric Laurente1715a42014-05-20 11:30:42 -07003082 ALOGV("setAudioPortConfig()");
3083
3084 if (config == NULL) {
3085 return BAD_VALUE;
3086 }
3087 ALOGV("setAudioPortConfig() on port handle %d", config->id);
3088 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07003089 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
3090 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07003091 }
3092
Eric Laurenta121f902014-06-03 13:32:54 -07003093 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07003094 if (config->type == AUDIO_PORT_TYPE_MIX) {
3095 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003096 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003097 if (outputDesc == NULL) {
3098 return BAD_VALUE;
3099 }
Eric Laurent84c70242014-06-23 08:46:27 -07003100 ALOG_ASSERT(!outputDesc->isDuplicated(),
3101 "setAudioPortConfig() called on duplicated output %d",
3102 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07003103 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003104 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01003105 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07003106 if (inputDesc == NULL) {
3107 return BAD_VALUE;
3108 }
Eric Laurenta121f902014-06-03 13:32:54 -07003109 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003110 } else {
3111 return BAD_VALUE;
3112 }
3113 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
3114 sp<DeviceDescriptor> deviceDesc;
3115 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
3116 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
3117 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
3118 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
3119 } else {
3120 return BAD_VALUE;
3121 }
3122 if (deviceDesc == NULL) {
3123 return BAD_VALUE;
3124 }
Eric Laurenta121f902014-06-03 13:32:54 -07003125 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07003126 } else {
3127 return BAD_VALUE;
3128 }
3129
Eric Laurenta121f902014-06-03 13:32:54 -07003130 struct audio_port_config backupConfig;
3131 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
3132 if (status == NO_ERROR) {
3133 struct audio_port_config newConfig;
3134 audioPortConfig->toAudioPortConfig(&newConfig, config);
3135 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07003136 }
Eric Laurenta121f902014-06-03 13:32:54 -07003137 if (status != NO_ERROR) {
3138 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07003139 }
Eric Laurente1715a42014-05-20 11:30:42 -07003140
3141 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07003142}
3143
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003144void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
3145{
Eric Laurentd60560a2015-04-10 11:31:20 -07003146 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003147 clearAudioPatches(uid);
3148 clearSessionRoutes(uid);
3149}
3150
Eric Laurent6a94d692014-05-20 11:18:06 -07003151void AudioPolicyManager::clearAudioPatches(uid_t uid)
3152{
Eric Laurent0add0fd2014-12-04 18:58:14 -08003153 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003154 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
3155 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08003156 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07003157 }
3158 }
3159}
3160
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003161void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
3162 audio_io_handle_t ouptutToSkip)
3163{
3164 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
3165 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
3166 for (size_t j = 0; j < mOutputs.size(); j++) {
3167 if (mOutputs.keyAt(j) == ouptutToSkip) {
3168 continue;
3169 }
3170 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
3171 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
3172 continue;
3173 }
3174 // If the default device for this strategy is on another output mix,
3175 // invalidate all tracks in this strategy to force re connection.
3176 // Otherwise select new device on the output mix.
3177 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08003178 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003179 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3180 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
3181 }
3182 }
3183 } else {
3184 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
3185 setOutputDevice(outputDesc, newDevice, false);
3186 }
3187 }
3188}
3189
3190void AudioPolicyManager::clearSessionRoutes(uid_t uid)
3191{
3192 // remove output routes associated with this uid
3193 SortedVector<routing_strategy> affectedStrategies;
3194 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
3195 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
3196 if (route->mUid == uid) {
3197 mOutputRoutes.removeItemsAt(i);
3198 if (route->mDeviceDescriptor != 0) {
3199 affectedStrategies.add(getStrategy(route->mStreamType));
3200 }
3201 }
3202 }
3203 // reroute outputs if necessary
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003204 for (const auto& strategy : affectedStrategies) {
3205 checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003206 }
3207
3208 // remove input routes associated with this uid
3209 SortedVector<audio_source_t> affectedSources;
3210 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
3211 sp<SessionRoute> route = mInputRoutes.valueAt(i);
3212 if (route->mUid == uid) {
3213 mInputRoutes.removeItemsAt(i);
3214 if (route->mDeviceDescriptor != 0) {
3215 affectedSources.add(route->mSource);
3216 }
3217 }
3218 }
3219 // reroute inputs if necessary
3220 SortedVector<audio_io_handle_t> inputsToClose;
3221 for (size_t i = 0; i < mInputs.size(); i++) {
3222 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08003223 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003224 inputsToClose.add(inputDesc->mIoHandle);
3225 }
3226 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003227 for (const auto& input : inputsToClose) {
3228 closeInput(input);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003229 }
3230}
3231
Eric Laurentd60560a2015-04-10 11:31:20 -07003232void AudioPolicyManager::clearAudioSources(uid_t uid)
3233{
3234 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
3235 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3236 if (sourceDesc->mUid == uid) {
3237 stopAudioSource(mAudioSources.keyAt(i));
3238 }
3239 }
3240}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003241
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003242status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
3243 audio_io_handle_t *ioHandle,
3244 audio_devices_t *device)
3245{
Glenn Kastenf0c6d7d2016-02-26 10:44:04 -08003246 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
3247 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003248 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003249
François Gaffiedf372692015-03-19 10:43:27 +01003250 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07003251}
3252
Eric Laurentd60560a2015-04-10 11:31:20 -07003253status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
3254 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07003255 audio_patch_handle_t *handle,
Eric Laurentd60560a2015-04-10 11:31:20 -07003256 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07003257{
Eric Laurentd60560a2015-04-10 11:31:20 -07003258 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
3259 if (source == NULL || attributes == NULL || handle == NULL) {
3260 return BAD_VALUE;
3261 }
3262
Glenn Kasten559d4392016-03-29 13:42:57 -07003263 *handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -07003264
3265 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
3266 source->type != AUDIO_PORT_TYPE_DEVICE) {
3267 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
3268 return INVALID_OPERATION;
3269 }
3270
3271 sp<DeviceDescriptor> srcDeviceDesc =
3272 mAvailableInputDevices.getDevice(source->ext.device.type,
3273 String8(source->ext.device.address));
3274 if (srcDeviceDesc == 0) {
3275 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
3276 return BAD_VALUE;
3277 }
3278 sp<AudioSourceDescriptor> sourceDesc =
3279 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
3280
3281 struct audio_patch dummyPatch;
3282 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
3283 sourceDesc->mPatchDesc = patchDesc;
3284
3285 status_t status = connectAudioSource(sourceDesc);
3286 if (status == NO_ERROR) {
3287 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
3288 *handle = sourceDesc->getHandle();
3289 }
3290 return status;
3291}
3292
3293status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3294{
3295 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3296
3297 // make sure we only have one patch per source.
3298 disconnectAudioSource(sourceDesc);
3299
3300 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08003301 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003302 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
3303
3304 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
3305 sp<DeviceDescriptor> sinkDeviceDesc =
3306 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
3307
3308 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3309 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
3310
3311 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
3312 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -07003313 srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07003314 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
3315 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
3316 // create patch between src device and output device
3317 // create Hwoutput and add to mHwOutputs
3318 } else {
3319 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
3320 audio_io_handle_t output =
3321 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
3322 if (output == AUDIO_IO_HANDLE_NONE) {
3323 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
3324 return INVALID_OPERATION;
3325 }
3326 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
3327 if (outputDesc->isDuplicated()) {
3328 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
3329 return INVALID_OPERATION;
3330 }
Eric Laurent733ce942017-12-07 12:18:25 -08003331 status_t status = outputDesc->start();
3332 if (status != NO_ERROR) {
3333 return status;
3334 }
3335
Eric Laurentd60560a2015-04-10 11:31:20 -07003336 // create a special patch with no sink and two sources:
3337 // - the second source indicates to PatchPanel through which output mix this patch should
3338 // be connected as well as the stream type for volume control
3339 // - the sink is defined by whatever output device is currently selected for the output
3340 // though which this patch is routed.
3341 patch->num_sinks = 0;
3342 patch->num_sources = 2;
3343 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
3344 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
3345 patch->sources[1].ext.mix.usecase.stream = stream;
Eric Laurent733ce942017-12-07 12:18:25 -08003346 status = mpClientInterface->createAudioPatch(patch,
Eric Laurentd60560a2015-04-10 11:31:20 -07003347 &afPatchHandle,
3348 0);
3349 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
3350 status, afPatchHandle);
3351 if (status != NO_ERROR) {
3352 ALOGW("%s patch panel could not connect device patch, error %d",
3353 __FUNCTION__, status);
3354 return INVALID_OPERATION;
3355 }
3356 uint32_t delayMs = 0;
Eric Laurentc40d9692016-04-13 19:14:13 -07003357 status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs);
Eric Laurentd60560a2015-04-10 11:31:20 -07003358
3359 if (status != NO_ERROR) {
3360 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
3361 return status;
3362 }
3363 sourceDesc->mSwOutput = outputDesc;
3364 if (delayMs != 0) {
3365 usleep(delayMs * 1000);
3366 }
3367 }
3368
3369 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
3370 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
3371
3372 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07003373}
3374
Glenn Kasten559d4392016-03-29 13:42:57 -07003375status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07003376{
Eric Laurentd60560a2015-04-10 11:31:20 -07003377 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
3378 ALOGV("%s handle %d", __FUNCTION__, handle);
3379 if (sourceDesc == 0) {
3380 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
3381 return BAD_VALUE;
3382 }
3383 status_t status = disconnectAudioSource(sourceDesc);
3384
3385 mAudioSources.removeItem(handle);
3386 return status;
3387}
3388
Andy Hung2ddee192015-12-18 17:34:44 -08003389status_t AudioPolicyManager::setMasterMono(bool mono)
3390{
3391 if (mMasterMono == mono) {
3392 return NO_ERROR;
3393 }
3394 mMasterMono = mono;
3395 // if enabling mono we close all offloaded devices, which will invalidate the
3396 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
3397 // for recreating the new AudioTrack as non-offloaded PCM.
3398 //
3399 // If disabling mono, we leave all tracks as is: we don't know which clients
3400 // and tracks are able to be recreated as offloaded. The next "song" should
3401 // play back offloaded.
3402 if (mMasterMono) {
3403 Vector<audio_io_handle_t> offloaded;
3404 for (size_t i = 0; i < mOutputs.size(); ++i) {
3405 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
3406 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3407 offloaded.push(desc->mIoHandle);
3408 }
3409 }
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003410 for (const auto& handle : offloaded) {
3411 closeOutput(handle);
Andy Hung2ddee192015-12-18 17:34:44 -08003412 }
3413 }
3414 // update master mono for all remaining outputs
3415 for (size_t i = 0; i < mOutputs.size(); ++i) {
3416 updateMono(mOutputs.keyAt(i));
3417 }
3418 return NO_ERROR;
3419}
3420
3421status_t AudioPolicyManager::getMasterMono(bool *mono)
3422{
3423 *mono = mMasterMono;
3424 return NO_ERROR;
3425}
3426
Eric Laurentac9cef52017-06-09 15:46:26 -07003427float AudioPolicyManager::getStreamVolumeDB(
3428 audio_stream_type_t stream, int index, audio_devices_t device)
3429{
3430 return computeVolume(stream, index, device);
3431}
3432
Svet Ganovf4ddfef2018-01-16 07:37:58 -08003433void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced)
3434{
3435 ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced);
3436
3437 Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
3438 for (size_t i = 0; i < activeInputs.size(); i++) {
3439 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
3440 AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true);
3441 for (size_t j = 0; j < activeSessions.size(); j++) {
3442 sp<AudioSession> activeSession = activeSessions.valueAt(j);
3443 if (activeSession->uid() == uid) {
3444 activeSession->setSilenced(silenced);
3445 }
3446 }
3447 }
3448}
3449
Eric Laurentd60560a2015-04-10 11:31:20 -07003450status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3451{
3452 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3453
3454 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3455 if (patchDesc == 0) {
3456 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3457 sourceDesc->mPatchDesc->mHandle);
3458 return BAD_VALUE;
3459 }
3460 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3461
Eric Laurent28d09f02016-03-08 10:43:05 -08003462 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07003463 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3464 if (swOutputDesc != 0) {
Eric Laurent733ce942017-12-07 12:18:25 -08003465 status_t status = stopSource(swOutputDesc, stream, false);
3466 if (status == NO_ERROR) {
3467 swOutputDesc->stop();
3468 }
Eric Laurentd60560a2015-04-10 11:31:20 -07003469 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3470 } else {
3471 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3472 if (hwOutputDesc != 0) {
3473 // release patch between src device and output device
3474 // close Hwoutput and remove from mHwOutputs
3475 } else {
3476 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3477 }
3478 }
3479 return NO_ERROR;
3480}
3481
3482sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3483 audio_io_handle_t output, routing_strategy strategy)
3484{
3485 sp<AudioSourceDescriptor> source;
3486 for (size_t i = 0; i < mAudioSources.size(); i++) {
3487 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3488 routing_strategy sourceStrategy =
3489 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3490 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3491 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3492 source = sourceDesc;
3493 break;
3494 }
3495 }
3496 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003497}
3498
Eric Laurente552edb2014-03-10 17:42:56 -07003499// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003500// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003501// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003502uint32_t AudioPolicyManager::nextAudioPortGeneration()
3503{
Mikhail Naganov2773dd72017-12-08 10:12:11 -08003504 return mAudioPortGeneration++;
Eric Laurent6a94d692014-05-20 11:18:06 -07003505}
3506
Jaekyun Seok0d4a6af2017-02-17 17:10:17 +09003507#ifdef USE_XML_AUDIO_POLICY_CONF
3508// Treblized audio policy xml config will be located in /odm/etc or /vendor/etc.
3509static const char *kConfigLocationList[] =
3510 {"/odm/etc", "/vendor/etc", "/system/etc"};
3511static const int kConfigLocationListSize =
3512 (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0]));
3513
3514static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) {
3515 char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH];
3516 status_t ret;
3517
3518 for (int i = 0; i < kConfigLocationListSize; i++) {
3519 PolicySerializer serializer;
3520 snprintf(audioPolicyXmlConfigFile,
3521 sizeof(audioPolicyXmlConfigFile),
3522 "%s/%s",
3523 kConfigLocationList[i],
3524 AUDIO_POLICY_XML_CONFIG_FILE_NAME);
3525 ret = serializer.deserialize(audioPolicyXmlConfigFile, config);
3526 if (ret == NO_ERROR) {
3527 break;
3528 }
3529 }
3530 return ret;
3531}
3532#endif
3533
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003534AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface,
3535 bool /*forTesting*/)
Eric Laurente552edb2014-03-10 17:42:56 -07003536 :
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003537 mUidCached(getuid()),
3538 mpClientInterface(clientInterface),
Eric Laurente552edb2014-03-10 17:42:56 -07003539 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003540 mA2dpSuspended(false),
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003541#ifdef USE_XML_AUDIO_POLICY_CONF
3542 mVolumeCurves(new VolumeCurvesCollection()),
3543 mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices,
Mikhail Naganovbcbcb1b2017-12-13 13:03:35 -08003544 mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())),
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003545#else
3546 mVolumeCurves(new StreamDescriptorCollection()),
3547 mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices,
3548 mDefaultOutputDevice),
3549#endif
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003550 mAudioPortGeneration(1),
3551 mBeaconMuteRefCount(0),
3552 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003553 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003554 mTtsOutputAvailable(false),
Eric Laurent36829f92017-04-07 19:04:42 -07003555 mMasterMono(false),
Chris Thornton2b864642017-06-27 21:26:07 -07003556 mMusicEffectOutput(AUDIO_IO_HANDLE_NONE),
3557 mHasComputedSoundTriggerSupportsConcurrentCapture(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003558{
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003559}
François Gaffied1ab2bd2015-12-02 18:20:06 +01003560
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003561AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
3562 : AudioPolicyManager(clientInterface, false /*forTesting*/)
3563{
3564 loadConfig();
3565 initialize();
3566}
François Gaffied1ab2bd2015-12-02 18:20:06 +01003567
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003568void AudioPolicyManager::loadConfig() {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003569#ifdef USE_XML_AUDIO_POLICY_CONF
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003570 if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003571#else
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003572 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR)
3573 && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01003574#endif
3575 ALOGE("could not load audio policy configuration file, setting defaults");
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003576 getConfig().setDefault();
François Gaffied1ab2bd2015-12-02 18:20:06 +01003577 }
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003578}
3579
3580status_t AudioPolicyManager::initialize() {
3581 mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled());
François Gaffied1ab2bd2015-12-02 18:20:06 +01003582
3583 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003584 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3585 if (!engineInstance) {
3586 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003587 return NO_INIT;
François Gaffie2110e042015-03-24 08:41:51 +01003588 }
3589 // Retrieve the Policy Manager Interface
3590 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3591 if (mEngine == NULL) {
3592 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003593 return NO_INIT;
François Gaffie2110e042015-03-24 08:41:51 +01003594 }
3595 mEngine->setObserver(this);
3596 status_t status = mEngine->initCheck();
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003597 if (status != NO_ERROR) {
3598 LOG_FATAL("Policy engine not initialized(err=%d)", status);
3599 return status;
3600 }
François Gaffie2110e042015-03-24 08:41:51 +01003601
Eric Laurent3a4311c2014-03-17 12:00:47 -07003602 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003603 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003604 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3605 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Mikhail Naganovd4120142017-12-06 15:49:22 -08003606 for (const auto& hwModule : mHwModulesAll) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003607 hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName()));
Mikhail Naganovd4120142017-12-06 15:49:22 -08003608 if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) {
3609 ALOGW("could not open HW module %s", hwModule->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003610 continue;
3611 }
Mikhail Naganovd4120142017-12-06 15:49:22 -08003612 mHwModules.push_back(hwModule);
Eric Laurente552edb2014-03-10 17:42:56 -07003613 // open all output streams needed to access attached devices
3614 // except for direct output streams that are only opened when they are actually
3615 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003616 // This also validates mAvailableOutputDevices list
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003617 for (const auto& outProfile : hwModule->getOutputProfiles()) {
Eric Laurent3974e3b2017-12-07 17:58:43 -08003618 if (!outProfile->canOpenNewIo()) {
3619 ALOGE("Invalid Output profile max open count %u for profile %s",
3620 outProfile->maxOpenCount, outProfile->getTagName().c_str());
3621 continue;
3622 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003623 if (!outProfile->hasSupportedDevices()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003624 ALOGW("Output profile contains no device on module %s", hwModule->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003625 continue;
3626 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003627 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003628 mTtsOutputAvailable = true;
3629 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003630
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003631 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003632 continue;
3633 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003634 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003635 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3636 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003637 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003638 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003639 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003640 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003641 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003642 if ((profileType & outputDeviceTypes) == 0) {
3643 continue;
3644 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003645 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3646 mpClientInterface);
Eric Laurentc40d9692016-04-13 19:14:13 -07003647 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3648 const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType);
3649 String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress
3650 : String8("");
Eric Laurentd78f1532014-09-16 16:38:20 -07003651 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08003652 status_t status = outputDesc->open(nullptr, profileType, address,
3653 AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output);
Eric Laurentd78f1532014-09-16 16:38:20 -07003654
3655 if (status != NO_ERROR) {
3656 ALOGW("Cannot open output stream for device %08x on hw module %s",
3657 outputDesc->mDevice,
Mikhail Naganovd4120142017-12-06 15:49:22 -08003658 hwModule->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003659 } else {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003660 for (const auto& dev : supportedDevices) {
3661 ssize_t index = mAvailableOutputDevices.indexOf(dev);
Eric Laurentd78f1532014-09-16 16:38:20 -07003662 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003663 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003664 mAvailableOutputDevices[index]->attach(hwModule);
Eric Laurentd78f1532014-09-16 16:38:20 -07003665 }
3666 }
3667 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003668 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003669 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003670 }
3671 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003672 setOutputDevice(outputDesc,
Eric Laurentfe231122017-11-17 17:48:06 -08003673 profileType,
Eric Laurentc40d9692016-04-13 19:14:13 -07003674 true,
3675 0,
3676 NULL,
Eric Laurentfe231122017-11-17 17:48:06 -08003677 address);
Eric Laurentd78f1532014-09-16 16:38:20 -07003678 }
Eric Laurente552edb2014-03-10 17:42:56 -07003679 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003680 // open input streams needed to access attached devices to validate
3681 // mAvailableInputDevices list
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003682 for (const auto& inProfile : hwModule->getInputProfiles()) {
Eric Laurent3974e3b2017-12-07 17:58:43 -08003683 if (!inProfile->canOpenNewIo()) {
3684 ALOGE("Invalid Input profile max open count %u for profile %s",
3685 inProfile->maxOpenCount, inProfile->getTagName().c_str());
3686 continue;
3687 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003688 if (!inProfile->hasSupportedDevices()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003689 ALOGW("Input profile contains no device on module %s", hwModule->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003690 continue;
3691 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003692 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003693 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003694 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3695
Eric Laurentd78f1532014-09-16 16:38:20 -07003696 if ((profileType & inputDeviceTypes) == 0) {
3697 continue;
3698 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003699 sp<AudioInputDescriptor> inputDesc =
Eric Laurentfe231122017-11-17 17:48:06 -08003700 new AudioInputDescriptor(inProfile, mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07003701
Eric Laurent53b810e2017-12-10 17:25:10 -08003702 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3703 // the inputs vector must be of size >= 1, but we don't want to crash here
3704 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3705 : String8("");
3706 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3707 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3708
Eric Laurentd78f1532014-09-16 16:38:20 -07003709 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08003710 status_t status = inputDesc->open(nullptr,
3711 profileType,
Eric Laurent53b810e2017-12-10 17:25:10 -08003712 address,
Eric Laurentfe231122017-11-17 17:48:06 -08003713 AUDIO_SOURCE_MIC,
3714 AUDIO_INPUT_FLAG_NONE,
3715 &input);
Eric Laurentd78f1532014-09-16 16:38:20 -07003716
3717 if (status == NO_ERROR) {
Mikhail Naganovcf84e592017-12-07 11:25:11 -08003718 for (const auto& dev : inProfile->getSupportedDevices()) {
3719 ssize_t index = mAvailableInputDevices.indexOf(dev);
Eric Laurentd78f1532014-09-16 16:38:20 -07003720 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003721 if (index >= 0) {
3722 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3723 if (!devDesc->isAttached()) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08003724 devDesc->attach(hwModule);
Eric Laurent83efe1c2017-07-09 16:51:08 -07003725 devDesc->importAudioPort(inProfile, true);
Eric Laurent45aabc32015-08-06 09:11:13 -07003726 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003727 }
3728 }
Eric Laurentfe231122017-11-17 17:48:06 -08003729 inputDesc->close();
Eric Laurentd78f1532014-09-16 16:38:20 -07003730 } else {
3731 ALOGW("Cannot open input stream for device %08x on hw module %s",
Eric Laurentfe231122017-11-17 17:48:06 -08003732 profileType,
Mikhail Naganovd4120142017-12-06 15:49:22 -08003733 hwModule->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003734 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003735 }
3736 }
3737 // make sure all attached devices have been allocated a unique ID
3738 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003739 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003740 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003741 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3742 continue;
3743 }
François Gaffie2110e042015-03-24 08:41:51 +01003744 // The device is now validated and can be appended to the available devices of the engine
3745 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3746 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003747 i++;
3748 }
3749 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003750 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003751 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003752 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3753 continue;
3754 }
François Gaffie2110e042015-03-24 08:41:51 +01003755 // The device is now validated and can be appended to the available devices of the engine
3756 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3757 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003758 i++;
3759 }
3760 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003761 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003762 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003763 status = NO_INIT;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003764 }
Eric Laurente552edb2014-03-10 17:42:56 -07003765
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003766 if (mPrimaryOutput == 0) {
3767 ALOGE("Failed to open primary output");
3768 status = NO_INIT;
3769 }
Eric Laurente552edb2014-03-10 17:42:56 -07003770
3771 updateDevicesAndOutputs();
Mikhail Naganovad3f8a12017-12-12 13:24:23 -08003772 return status;
Eric Laurente552edb2014-03-10 17:42:56 -07003773}
3774
Eric Laurente0720872014-03-11 09:30:41 -07003775AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003776{
Eric Laurente552edb2014-03-10 17:42:56 -07003777 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentfe231122017-11-17 17:48:06 -08003778 mOutputs.valueAt(i)->close();
Eric Laurente552edb2014-03-10 17:42:56 -07003779 }
3780 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurentfe231122017-11-17 17:48:06 -08003781 mInputs.valueAt(i)->close();
Eric Laurente552edb2014-03-10 17:42:56 -07003782 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003783 mAvailableOutputDevices.clear();
3784 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003785 mOutputs.clear();
3786 mInputs.clear();
3787 mHwModules.clear();
Mikhail Naganovd4120142017-12-06 15:49:22 -08003788 mHwModulesAll.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003789}
3790
Eric Laurente0720872014-03-11 09:30:41 -07003791status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003792{
Eric Laurent87ffa392015-05-22 10:32:38 -07003793 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003794}
3795
Eric Laurente552edb2014-03-10 17:42:56 -07003796// ---
3797
Eric Laurent98e38192018-02-15 18:31:53 -08003798void AudioPolicyManager::addOutput(audio_io_handle_t output,
3799 const sp<SwAudioOutputDescriptor>& outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003800{
Eric Laurent1c333e22014-05-20 10:48:17 -07003801 mOutputs.add(output, outputDesc);
Eric Laurent98e38192018-02-15 18:31:53 -08003802 applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */);
Andy Hung2ddee192015-12-18 17:34:44 -08003803 updateMono(output); // update mono status when adding to output list
Eric Laurent36829f92017-04-07 19:04:42 -07003804 selectOutputForMusicEffects();
Eric Laurent6a94d692014-05-20 11:18:06 -07003805 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003806}
3807
François Gaffie53615e22015-03-19 09:24:12 +01003808void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3809{
3810 mOutputs.removeItem(output);
Eric Laurent36829f92017-04-07 19:04:42 -07003811 selectOutputForMusicEffects();
François Gaffie53615e22015-03-19 09:24:12 +01003812}
3813
Eric Laurent98e38192018-02-15 18:31:53 -08003814void AudioPolicyManager::addInput(audio_io_handle_t input,
3815 const sp<AudioInputDescriptor>& inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003816{
Eric Laurent1c333e22014-05-20 10:48:17 -07003817 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003818 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003819}
Eric Laurente552edb2014-03-10 17:42:56 -07003820
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003821void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003822 const audio_devices_t device /*in*/,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003823 const String8& address /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003824 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003825 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003826 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003827 if (devDesc != 0) {
3828 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3829 desc->mIoHandle, address.string());
3830 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003831 }
3832}
3833
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003834status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003835 audio_policy_dev_state_t state,
3836 SortedVector<audio_io_handle_t>& outputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07003837 const String8& address)
Eric Laurente552edb2014-03-10 17:42:56 -07003838{
François Gaffie53615e22015-03-19 09:24:12 +01003839 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003840 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003841
3842 if (audio_device_is_digital(device)) {
3843 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003844 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003845 }
Eric Laurente552edb2014-03-10 17:42:56 -07003846
Eric Laurent3b73df72014-03-11 09:06:29 -07003847 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003848 // first list already open outputs that can be routed to this device
3849 for (size_t i = 0; i < mOutputs.size(); i++) {
3850 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003851 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003852 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003853 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3854 outputs.add(mOutputs.keyAt(i));
3855 } else {
3856 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003857 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003858 }
Eric Laurente552edb2014-03-10 17:42:56 -07003859 }
3860 }
3861 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003862 SortedVector< sp<IOProfile> > profiles;
Mikhail Naganov7e22e942017-12-07 10:04:29 -08003863 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08003864 for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) {
3865 sp<IOProfile> profile = hwModule->getOutputProfiles()[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003866 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003867 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003868 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003869 profiles.add(profile);
Mikhail Naganovd4120142017-12-06 15:49:22 -08003870 ALOGV("checkOutputsForDevice(): adding profile %zu from module %s",
3871 j, hwModule->getName());
Eric Laurent275e8e92014-11-30 15:14:47 -08003872 }
Eric Laurente552edb2014-03-10 17:42:56 -07003873 }
3874 }
3875 }
3876
Eric Laurent7b279bb2015-12-14 10:18:23 -08003877 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003878
Eric Laurente552edb2014-03-10 17:42:56 -07003879 if (profiles.isEmpty() && outputs.isEmpty()) {
3880 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3881 return BAD_VALUE;
3882 }
3883
3884 // open outputs for matching profiles if needed. Direct outputs are also opened to
3885 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3886 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003887 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003888
3889 // nothing to do if one output is already opened for this profile
3890 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003891 for (j = 0; j < outputs.size(); j++) {
3892 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003893 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003894 // matching profile: save the sample rates, format and channel masks supported
3895 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003896 if (audio_device_is_digital(device)) {
3897 devDesc->importAudioPort(profile);
3898 }
Eric Laurente552edb2014-03-10 17:42:56 -07003899 break;
3900 }
3901 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003902 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003903 continue;
3904 }
3905
Eric Laurent3974e3b2017-12-07 17:58:43 -08003906 if (!profile->canOpenNewIo()) {
3907 ALOGW("Max Output number %u already opened for this profile %s",
3908 profile->maxOpenCount, profile->getTagName().c_str());
3909 continue;
3910 }
3911
Eric Laurent83efe1c2017-07-09 16:51:08 -07003912 ALOGV("opening output for device %08x with params %s profile %p name %s",
3913 device, address.string(), profile.get(), profile->getName().string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003914 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003915 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08003916 status_t status = desc->open(nullptr, device, address,
3917 AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output);
Eric Laurente552edb2014-03-10 17:42:56 -07003918
Eric Laurentfe231122017-11-17 17:48:06 -08003919 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07003920 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003921 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003922 char *param = audio_device_address_to_parameter(device, address);
3923 mpClientInterface->setParameters(output, String8(param));
3924 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003925 }
Phil Burk00eeb322016-03-31 12:41:00 -07003926 updateAudioProfiles(device, output, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01003927 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003928 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentfe231122017-11-17 17:48:06 -08003929 desc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003930 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003931 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentfe231122017-11-17 17:48:06 -08003932 desc->close();
Phil Burk702b1052016-03-02 16:38:26 -08003933 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08003934 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3935 profile->pickAudioProfile(
3936 config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003937 config.offload_info.sample_rate = config.sample_rate;
3938 config.offload_info.channel_mask = config.channel_mask;
3939 config.offload_info.format = config.format;
Eric Laurentfe231122017-11-17 17:48:06 -08003940
3941 status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT,
3942 AUDIO_OUTPUT_FLAG_NONE, &output);
3943 if (status != NO_ERROR) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003944 output = AUDIO_IO_HANDLE_NONE;
3945 }
Eric Laurentd4692962014-05-05 18:13:44 -07003946 }
3947
Eric Laurentcf2c0212014-07-25 16:20:43 -07003948 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003949 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003950 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003951 sp<AudioPolicyMix> policyMix;
3952 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003953 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3954 address.string());
3955 }
François Gaffie036e1e92015-03-19 10:16:24 +01003956 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003957 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003958
Eric Laurent87ffa392015-05-22 10:32:38 -07003959 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3960 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003961 // no duplicated output for direct outputs and
3962 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003963 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003964
Eric Laurentd4692962014-05-05 18:13:44 -07003965 //TODO: configure audio effect output stage here
3966
3967 // open a duplicating output thread for the new output and the primary output
Eric Laurent5babc4f2018-02-15 12:33:44 -08003968 sp<SwAudioOutputDescriptor> dupOutputDesc =
3969 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3970 status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc,
3971 &duplicatedOutput);
3972 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07003973 // add duplicated output descriptor
Eric Laurentd4692962014-05-05 18:13:44 -07003974 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentd4692962014-05-05 18:13:44 -07003975 } else {
3976 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003977 mPrimaryOutput->mIoHandle, output);
Eric Laurentfe231122017-11-17 17:48:06 -08003978 desc->close();
François Gaffie53615e22015-03-19 09:24:12 +01003979 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003980 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003981 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003982 }
Eric Laurente552edb2014-03-10 17:42:56 -07003983 }
3984 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003985 } else {
3986 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003987 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003988 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003989 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003990 profiles.removeAt(profile_index);
3991 profile_index--;
3992 } else {
3993 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003994 // Load digital format info only for digital devices
3995 if (audio_device_is_digital(device)) {
3996 devDesc->importAudioPort(profile);
3997 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003998
François Gaffie53615e22015-03-19 09:24:12 +01003999 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004000 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
4001 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07004002 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004003 NULL/*patch handle*/, address.string());
4004 }
Eric Laurente552edb2014-03-10 17:42:56 -07004005 ALOGV("checkOutputsForDevice(): adding output %d", output);
4006 }
4007 }
4008
4009 if (profiles.isEmpty()) {
4010 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
4011 return BAD_VALUE;
4012 }
Eric Laurentd4692962014-05-05 18:13:44 -07004013 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07004014 // check if one opened output is not needed any more after disconnecting one device
4015 for (size_t i = 0; i < mOutputs.size(); i++) {
4016 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004017 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004018 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01004019 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07004020 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08004021 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004022 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004023 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
4024 mOutputs.keyAt(i));
4025 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004026 }
Eric Laurente552edb2014-03-10 17:42:56 -07004027 }
4028 }
Eric Laurentd4692962014-05-05 18:13:44 -07004029 // Clear any profiles associated with the disconnected device.
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004030 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004031 for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) {
4032 sp<IOProfile> profile = hwModule->getOutputProfiles()[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004033 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004034 ALOGV("checkOutputsForDevice(): "
Mikhail Naganovd4120142017-12-06 15:49:22 -08004035 "clearing direct output profile %zu on module %s",
4036 j, hwModule->getName());
François Gaffie112b0af2015-11-19 16:13:25 +01004037 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07004038 }
4039 }
4040 }
4041 }
4042 return NO_ERROR;
4043}
4044
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004045status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01004046 audio_policy_dev_state_t state,
4047 SortedVector<audio_io_handle_t>& inputs,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004048 const String8& address)
Eric Laurentd4692962014-05-05 18:13:44 -07004049{
Paul McLean9080a4c2015-06-18 08:24:02 -07004050 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07004051 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07004052
4053 if (audio_device_is_digital(device)) {
4054 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08004055 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07004056 }
4057
Eric Laurentd4692962014-05-05 18:13:44 -07004058 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
4059 // first list already open inputs that can be routed to this device
4060 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4061 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004062 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07004063 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
4064 inputs.add(mInputs.keyAt(input_index));
4065 }
4066 }
4067
4068 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07004069 SortedVector< sp<IOProfile> > profiles;
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004070 for (const auto& hwModule : mHwModules) {
Eric Laurentd4692962014-05-05 18:13:44 -07004071 for (size_t profile_index = 0;
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004072 profile_index < hwModule->getInputProfiles().size();
Mikhail Naganov7e22e942017-12-07 10:04:29 -08004073 profile_index++) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004074 sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index];
Eric Laurent275e8e92014-11-30 15:14:47 -08004075
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004076 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01004077 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004078 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004079 profiles.add(profile);
Mikhail Naganovd4120142017-12-06 15:49:22 -08004080 ALOGV("checkInputsForDevice(): adding profile %zu from module %s",
4081 profile_index, hwModule->getName());
Eric Laurent275e8e92014-11-30 15:14:47 -08004082 }
Eric Laurentd4692962014-05-05 18:13:44 -07004083 }
4084 }
4085 }
4086
4087 if (profiles.isEmpty() && inputs.isEmpty()) {
4088 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4089 return BAD_VALUE;
4090 }
4091
4092 // open inputs for matching profiles if needed. Direct inputs are also opened to
4093 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
4094 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
4095
Eric Laurent1c333e22014-05-20 10:48:17 -07004096 sp<IOProfile> profile = profiles[profile_index];
Eric Laurent3974e3b2017-12-07 17:58:43 -08004097
Eric Laurentd4692962014-05-05 18:13:44 -07004098 // nothing to do if one input is already opened for this profile
4099 size_t input_index;
4100 for (input_index = 0; input_index < mInputs.size(); input_index++) {
4101 desc = mInputs.valueAt(input_index);
4102 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07004103 if (audio_device_is_digital(device)) {
4104 devDesc->importAudioPort(profile);
4105 }
Eric Laurentd4692962014-05-05 18:13:44 -07004106 break;
4107 }
4108 }
4109 if (input_index != mInputs.size()) {
4110 continue;
4111 }
4112
Eric Laurent3974e3b2017-12-07 17:58:43 -08004113 if (!profile->canOpenNewIo()) {
4114 ALOGW("Max Input number %u already opened for this profile %s",
4115 profile->maxOpenCount, profile->getTagName().c_str());
4116 continue;
4117 }
4118
Eric Laurentfe231122017-11-17 17:48:06 -08004119 desc = new AudioInputDescriptor(profile, mpClientInterface);
Eric Laurentcf2c0212014-07-25 16:20:43 -07004120 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurentfe231122017-11-17 17:48:06 -08004121 status_t status = desc->open(nullptr,
4122 device,
4123 address,
4124 AUDIO_SOURCE_MIC,
4125 AUDIO_INPUT_FLAG_NONE,
4126 &input);
Eric Laurentd4692962014-05-05 18:13:44 -07004127
Eric Laurentcf2c0212014-07-25 16:20:43 -07004128 if (status == NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -07004129 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07004130 char *param = audio_device_address_to_parameter(device, address);
4131 mpClientInterface->setParameters(input, String8(param));
4132 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07004133 }
Phil Burk00eeb322016-03-31 12:41:00 -07004134 updateAudioProfiles(device, input, profile->getAudioProfiles());
François Gaffie112b0af2015-11-19 16:13:25 +01004135 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07004136 ALOGW("checkInputsForDevice() direct input missing param");
Eric Laurentfe231122017-11-17 17:48:06 -08004137 desc->close();
Eric Laurentcf2c0212014-07-25 16:20:43 -07004138 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07004139 }
4140
4141 if (input != 0) {
4142 addInput(input, desc);
4143 }
4144 } // endif input != 0
4145
Eric Laurentcf2c0212014-07-25 16:20:43 -07004146 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07004147 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07004148 profiles.removeAt(profile_index);
4149 profile_index--;
4150 } else {
4151 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07004152 if (audio_device_is_digital(device)) {
4153 devDesc->importAudioPort(profile);
4154 }
Eric Laurentd4692962014-05-05 18:13:44 -07004155 ALOGV("checkInputsForDevice(): adding input %d", input);
4156 }
4157 } // end scan profiles
4158
4159 if (profiles.isEmpty()) {
4160 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
4161 return BAD_VALUE;
4162 }
4163 } else {
4164 // Disconnect
4165 // check if one opened input is not needed any more after disconnecting one device
4166 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
4167 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004168 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07004169 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
4170 mInputs.keyAt(input_index));
4171 inputs.add(mInputs.keyAt(input_index));
4172 }
4173 }
4174 // Clear any profiles associated with the disconnected device.
Mikhail Naganovd4120142017-12-06 15:49:22 -08004175 for (const auto& hwModule : mHwModules) {
Eric Laurentd4692962014-05-05 18:13:44 -07004176 for (size_t profile_index = 0;
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004177 profile_index < hwModule->getInputProfiles().size();
Eric Laurentd4692962014-05-05 18:13:44 -07004178 profile_index++) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08004179 sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01004180 if (profile->supportDevice(device)) {
Mikhail Naganovd4120142017-12-06 15:49:22 -08004181 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s",
4182 profile_index, hwModule->getName());
François Gaffie112b0af2015-11-19 16:13:25 +01004183 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07004184 }
4185 }
4186 }
4187 } // end disconnect
4188
4189 return NO_ERROR;
4190}
4191
4192
Eric Laurente0720872014-03-11 09:30:41 -07004193void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07004194{
4195 ALOGV("closeOutput(%d)", output);
4196
Eric Laurentc75307b2015-03-17 15:29:32 -07004197 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004198 if (outputDesc == NULL) {
4199 ALOGW("closeOutput() unknown output %d", output);
4200 return;
4201 }
François Gaffie036e1e92015-03-19 10:16:24 +01004202 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08004203
Eric Laurente552edb2014-03-10 17:42:56 -07004204 // look for duplicated outputs connected to the output being removed.
4205 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004206 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004207 if (dupOutputDesc->isDuplicated() &&
4208 (dupOutputDesc->mOutput1 == outputDesc ||
4209 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent733ce942017-12-07 12:18:25 -08004210 sp<SwAudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07004211 if (dupOutputDesc->mOutput1 == outputDesc) {
4212 outputDesc2 = dupOutputDesc->mOutput2;
4213 } else {
4214 outputDesc2 = dupOutputDesc->mOutput1;
4215 }
4216 // As all active tracks on duplicated output will be deleted,
4217 // and as they were also referenced on the other output, the reference
4218 // count for their stream type must be adjusted accordingly on
4219 // the other output.
Eric Laurent733ce942017-12-07 12:18:25 -08004220 bool wasActive = outputDesc2->isActive();
Eric Laurent3b73df72014-03-11 09:06:29 -07004221 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004222 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07004223 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07004224 }
Eric Laurent733ce942017-12-07 12:18:25 -08004225 // stop() will be a no op if the output is still active but is needed in case all
4226 // active streams refcounts where cleared above
4227 if (wasActive) {
4228 outputDesc2->stop();
4229 }
Eric Laurente552edb2014-03-10 17:42:56 -07004230 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
4231 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
4232
4233 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01004234 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004235 }
4236 }
4237
Eric Laurent05b90f82014-08-27 15:32:29 -07004238 nextAudioPortGeneration();
4239
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004240 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004241 if (index >= 0) {
4242 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004243 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004244 mAudioPatches.removeItemsAt(index);
4245 mpClientInterface->onAudioPatchListUpdate();
4246 }
4247
Eric Laurentfe231122017-11-17 17:48:06 -08004248 outputDesc->close();
Eric Laurente552edb2014-03-10 17:42:56 -07004249
François Gaffie53615e22015-03-19 09:24:12 +01004250 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004251 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004252}
4253
4254void AudioPolicyManager::closeInput(audio_io_handle_t input)
4255{
4256 ALOGV("closeInput(%d)", input);
4257
4258 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4259 if (inputDesc == NULL) {
4260 ALOGW("closeInput() unknown input %d", input);
4261 return;
4262 }
4263
Eric Laurent6a94d692014-05-20 11:18:06 -07004264 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004265
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004266 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07004267 if (index >= 0) {
4268 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
Glenn Kastenfcddb0b2016-07-08 17:19:25 -07004269 (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent05b90f82014-08-27 15:32:29 -07004270 mAudioPatches.removeItemsAt(index);
4271 mpClientInterface->onAudioPatchListUpdate();
4272 }
4273
Eric Laurentfe231122017-11-17 17:48:06 -08004274 inputDesc->close();
Eric Laurent05b90f82014-08-27 15:32:29 -07004275 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004276}
4277
Eric Laurentc75307b2015-03-17 15:29:32 -07004278SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4279 audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004280 const SwAudioOutputCollection& openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004281{
4282 SortedVector<audio_io_handle_t> outputs;
4283
4284 ALOGVV("getOutputsForDevice() device %04x", device);
4285 for (size_t i = 0; i < openOutputs.size(); i++) {
Eric Laurent37ddbb42016-08-10 16:19:14 -07004286 ALOGVV("output %zu isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004287 i, openOutputs.valueAt(i)->isDuplicated(),
4288 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004289 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4290 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4291 outputs.add(openOutputs.keyAt(i));
4292 }
4293 }
4294 return outputs;
4295}
4296
Eric Laurente0720872014-03-11 09:30:41 -07004297bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004298 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004299{
4300 if (outputs1.size() != outputs2.size()) {
4301 return false;
4302 }
4303 for (size_t i = 0; i < outputs1.size(); i++) {
4304 if (outputs1[i] != outputs2[i]) {
4305 return false;
4306 }
4307 }
4308 return true;
4309}
4310
Eric Laurente0720872014-03-11 09:30:41 -07004311void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004312{
4313 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4314 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4315 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4316 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4317
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004318 // also take into account external policy-related changes: add all outputs which are
4319 // associated with policies in the "before" and "after" output vectors
4320 ALOGVV("checkOutputForStrategy(): policy related outputs");
4321 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004322 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004323 if (desc != 0 && desc->mPolicyMix != NULL) {
4324 srcOutputs.add(desc->mIoHandle);
4325 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4326 }
4327 }
4328 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004329 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004330 if (desc != 0 && desc->mPolicyMix != NULL) {
4331 dstOutputs.add(desc->mIoHandle);
4332 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4333 }
4334 }
4335
Eric Laurente552edb2014-03-10 17:42:56 -07004336 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4337 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4338 strategy, srcOutputs[0], dstOutputs[0]);
4339 // mute strategy while moving tracks from one output to another
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004340 for (audio_io_handle_t srcOut : srcOutputs) {
4341 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOut);
François Gaffiead3183e2015-03-18 16:55:35 +01004342 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004343 setStrategyMute(strategy, true, desc);
4344 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004345 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004346 sp<AudioSourceDescriptor> source =
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004347 getSourceForStrategyOnOutput(srcOut, strategy);
Eric Laurentd60560a2015-04-10 11:31:20 -07004348 if (source != 0){
4349 connectAudioSource(source);
4350 }
Eric Laurente552edb2014-03-10 17:42:56 -07004351 }
4352
4353 // Move effects associated to this strategy from previous output to new output
4354 if (strategy == STRATEGY_MEDIA) {
Eric Laurent36829f92017-04-07 19:04:42 -07004355 selectOutputForMusicEffects();
Eric Laurente552edb2014-03-10 17:42:56 -07004356 }
4357 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004358 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004359 if (getStrategy((audio_stream_type_t)i) == strategy) {
4360 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004361 }
4362 }
4363 }
4364}
4365
Eric Laurente0720872014-03-11 09:30:41 -07004366void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004367{
François Gaffie2110e042015-03-24 08:41:51 +01004368 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004369 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004370 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004371 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004372 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004373 checkOutputForStrategy(STRATEGY_SONIFICATION);
4374 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004375 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004376 checkOutputForStrategy(STRATEGY_MEDIA);
4377 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004378 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004379}
4380
Eric Laurente0720872014-03-11 09:30:41 -07004381void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004382{
François Gaffie53615e22015-03-19 09:24:12 +01004383 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -08004384 if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004385 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004386 return;
4387 }
4388
Eric Laurent3a4311c2014-03-17 12:00:47 -07004389 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004390 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4391 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4392 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurentf732e072016-08-03 19:30:28 -07004393
4394 // if suspended, restore A2DP output if:
4395 // ((SCO device is NOT connected) ||
4396 // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) &&
4397 // (phone state is NOT in call) && (phone state is NOT ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004398 //
Eric Laurentf732e072016-08-03 19:30:28 -07004399 // if not suspended, suspend A2DP output if:
4400 // (SCO device is connected) &&
4401 // ((forced usage for communication is SCO) || (forced usage for record is SCO) ||
4402 // ((phone state is in call) || (phone state is ringing)))
Eric Laurente552edb2014-03-10 17:42:56 -07004403 //
4404 if (mA2dpSuspended) {
Eric Laurentf732e072016-08-03 19:30:28 -07004405 if (!isScoConnected ||
4406 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) !=
4407 AUDIO_POLICY_FORCE_BT_SCO) &&
4408 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) !=
4409 AUDIO_POLICY_FORCE_BT_SCO) &&
4410 (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
François Gaffie2110e042015-03-24 08:41:51 +01004411 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004412
4413 mpClientInterface->restoreOutput(a2dpOutput);
4414 mA2dpSuspended = false;
4415 }
4416 } else {
Eric Laurentf732e072016-08-03 19:30:28 -07004417 if (isScoConnected &&
4418 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ==
4419 AUDIO_POLICY_FORCE_BT_SCO) ||
4420 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) ==
4421 AUDIO_POLICY_FORCE_BT_SCO) ||
4422 (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
François Gaffie2110e042015-03-24 08:41:51 +01004423 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004424
4425 mpClientInterface->suspendOutput(a2dpOutput);
4426 mA2dpSuspended = true;
4427 }
4428 }
4429}
4430
Eric Laurentc75307b2015-03-17 15:29:32 -07004431audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4432 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004433{
4434 audio_devices_t device = AUDIO_DEVICE_NONE;
4435
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004436 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004437 if (index >= 0) {
4438 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4439 if (patchDesc->mUid != mUidCached) {
4440 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004441 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004442 return outputDesc->device();
4443 }
4444 }
4445
Eric Laurente552edb2014-03-10 17:42:56 -07004446 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004447 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004448 // use device for strategy enforced audible
4449 // 2: we are in call or the strategy phone is active on the output:
4450 // use device for strategy phone
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004451 // 3: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004452 // use device for strategy sonification
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004453 // 4: the strategy for enforced audible is active but not enforced on the output:
4454 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004455 // 5: the strategy accessibility is active on the output:
4456 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004457 // 6: the strategy "respectful" sonification is active on the output:
4458 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004459 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004460 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004461 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004462 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004463 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004464 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004465 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004466 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004467 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4468 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004469 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004470 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004471 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004472 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Jean-Michel Trivi178683b2017-08-30 18:07:06 -07004473 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
4474 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004475 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4476 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004477 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004478 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004479 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004480 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004481 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004482 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004483 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004484 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004485 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004486 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004487 }
4488
Eric Laurent1c333e22014-05-20 10:48:17 -07004489 ALOGV("getNewOutputDevice() selected device %x", device);
4490 return device;
4491}
4492
Eric Laurentfb66dd92016-01-28 18:32:03 -08004493audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004494{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004495 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004496
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004497 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004498 if (index >= 0) {
4499 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4500 if (patchDesc->mUid != mUidCached) {
4501 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004502 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004503 return inputDesc->mDevice;
4504 }
4505 }
4506
Eric Laurentfb66dd92016-01-28 18:32:03 -08004507 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4508 if (isInCall()) {
4509 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4510 } else if (source != AUDIO_SOURCE_DEFAULT) {
4511 device = getDeviceAndMixForInputSource(source);
4512 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004513
Eric Laurente552edb2014-03-10 17:42:56 -07004514 return device;
4515}
4516
Eric Laurent794fde22016-03-11 09:50:45 -08004517bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4518 audio_stream_type_t stream2) {
Jean-Michel Trivi99bb2f92016-11-23 15:52:07 -08004519 return (stream1 == stream2);
Eric Laurent28d09f02016-03-08 10:43:05 -08004520}
4521
Eric Laurente0720872014-03-11 09:30:41 -07004522uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004523 return (uint32_t)getStrategy(stream);
4524}
4525
Eric Laurente0720872014-03-11 09:30:41 -07004526audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004527 // By checking the range of stream before calling getStrategy, we avoid
4528 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4529 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004530 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004531 return AUDIO_DEVICE_NONE;
4532 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004533 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004534 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4535 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004536 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004537 }
Eric Laurent794fde22016-03-11 09:50:45 -08004538 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004539 audio_devices_t curDevices =
Eric Laurent447a87b2016-07-07 17:32:10 -07004540 getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
Mikhail Naganovcf84e592017-12-07 11:25:11 -08004541 for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) {
4542 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent794fde22016-03-11 09:50:45 -08004543 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004544 curDevices |= outputDesc->device();
4545 }
4546 }
4547 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004548 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004549
4550 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4551 and doesn't really need to.*/
4552 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4553 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4554 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4555 }
Eric Laurente552edb2014-03-10 17:42:56 -07004556 return devices;
4557}
4558
François Gaffie2110e042015-03-24 08:41:51 +01004559routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4560{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004561 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004562 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004563}
4564
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004565uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4566 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004567 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4568 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4569 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004570 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4571 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4572 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004573 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004574 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004575}
4576
Eric Laurente0720872014-03-11 09:30:41 -07004577void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004578 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004579 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004580 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4581 updateDevicesAndOutputs();
4582 break;
4583 default:
4584 break;
4585 }
4586}
4587
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004588uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004589
4590 // skip beacon mute management if a dedicated TTS output is available
4591 if (mTtsOutputAvailable) {
4592 return 0;
4593 }
4594
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004595 switch(event) {
4596 case STARTING_OUTPUT:
4597 mBeaconMuteRefCount++;
4598 break;
4599 case STOPPING_OUTPUT:
4600 if (mBeaconMuteRefCount > 0) {
4601 mBeaconMuteRefCount--;
4602 }
4603 break;
4604 case STARTING_BEACON:
4605 mBeaconPlayingRefCount++;
4606 break;
4607 case STOPPING_BEACON:
4608 if (mBeaconPlayingRefCount > 0) {
4609 mBeaconPlayingRefCount--;
4610 }
4611 break;
4612 }
4613
4614 if (mBeaconMuteRefCount > 0) {
4615 // any playback causes beacon to be muted
4616 return setBeaconMute(true);
4617 } else {
4618 // no other playback: unmute when beacon starts playing, mute when it stops
4619 return setBeaconMute(mBeaconPlayingRefCount == 0);
4620 }
4621}
4622
4623uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4624 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4625 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4626 // keep track of muted state to avoid repeating mute/unmute operations
4627 if (mBeaconMuted != mute) {
4628 // mute/unmute AUDIO_STREAM_TTS on all outputs
4629 ALOGV("\t muting %d", mute);
4630 uint32_t maxLatency = 0;
4631 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004632 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004633 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004634 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004635 0 /*delay*/, AUDIO_DEVICE_NONE);
4636 const uint32_t latency = desc->latency() * 2;
4637 if (latency > maxLatency) {
4638 maxLatency = latency;
4639 }
4640 }
4641 mBeaconMuted = mute;
4642 return maxLatency;
4643 }
4644 return 0;
4645}
4646
Eric Laurente0720872014-03-11 09:30:41 -07004647audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004648 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004649{
Paul McLeanaa981192015-03-21 09:55:15 -07004650 // Routing
4651 // see if we have an explicit route
4652 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4653 // (getStrategy(stream)).
4654 // if the strategy from the stream type in the RouteMap is the same as the argument above,
Eric Laurentad2e7b92017-09-14 20:06:42 -07004655 // and activity count is non-zero and the device in the route descriptor is available
4656 // then select this device.
Paul McLeanaa981192015-03-21 09:55:15 -07004657 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4658 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004659 routing_strategy routeStrategy = getStrategy(route->mStreamType);
Eric Laurentad2e7b92017-09-14 20:06:42 -07004660 if ((routeStrategy == strategy) && route->isActive() &&
4661 (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLeanaa981192015-03-21 09:55:15 -07004662 return route->mDeviceDescriptor->type();
4663 }
4664 }
4665
Eric Laurente552edb2014-03-10 17:42:56 -07004666 if (fromCache) {
4667 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4668 strategy, mDeviceForStrategy[strategy]);
4669 return mDeviceForStrategy[strategy];
4670 }
François Gaffie2110e042015-03-24 08:41:51 +01004671 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004672}
4673
Eric Laurente0720872014-03-11 09:30:41 -07004674void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004675{
4676 for (int i = 0; i < NUM_STRATEGIES; i++) {
4677 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4678 }
4679 mPreviousOutputs = mOutputs;
4680}
4681
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07004682uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004683 audio_devices_t prevDevice,
4684 uint32_t delayMs)
4685{
4686 // mute/unmute strategies using an incompatible device combination
4687 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4688 // if unmuting, unmute only after the specified delay
4689 if (outputDesc->isDuplicated()) {
4690 return 0;
4691 }
4692
4693 uint32_t muteWaitMs = 0;
4694 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004695 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004696
4697 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4698 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004699 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004700 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4701 bool doMute = false;
4702
4703 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4704 doMute = true;
4705 outputDesc->mStrategyMutedByDevice[i] = true;
4706 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4707 doMute = true;
4708 outputDesc->mStrategyMutedByDevice[i] = false;
4709 }
Eric Laurent99401132014-05-07 19:48:15 -07004710 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004711 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004712 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004713 // skip output if it does not share any device with current output
4714 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4715 == AUDIO_DEVICE_NONE) {
4716 continue;
4717 }
Eric Laurent37ddbb42016-08-10 16:19:14 -07004718 ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)",
Eric Laurentc75307b2015-03-17 15:29:32 -07004719 mute ? "muting" : "unmuting", i, curDevice);
4720 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004721 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004722 if (mute) {
4723 // FIXME: should not need to double latency if volume could be applied
4724 // immediately by the audioflinger mixer. We must account for the delay
4725 // between now and the next time the audioflinger thread for this output
4726 // will process a buffer (which corresponds to one buffer size,
4727 // usually 1/2 or 1/4 of the latency).
4728 if (muteWaitMs < desc->latency() * 2) {
4729 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004730 }
4731 }
4732 }
4733 }
4734 }
4735 }
4736
Eric Laurent99401132014-05-07 19:48:15 -07004737 // temporary mute output if device selection changes to avoid volume bursts due to
4738 // different per device volumes
4739 if (outputDesc->isActive() && (device != prevDevice)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004740 uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
4741 // temporary mute duration is conservatively set to 4 times the reported latency
4742 uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
4743 if (muteWaitMs < tempMuteWaitMs) {
4744 muteWaitMs = tempMuteWaitMs;
Eric Laurent99401132014-05-07 19:48:15 -07004745 }
Eric Laurentdc462862016-07-19 12:29:53 -07004746
Eric Laurent99401132014-05-07 19:48:15 -07004747 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004748 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentdc462862016-07-19 12:29:53 -07004749 // make sure that we do not start the temporary mute period too early in case of
4750 // delayed device change
4751 setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
Eric Laurentc75307b2015-03-17 15:29:32 -07004752 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurentdc462862016-07-19 12:29:53 -07004753 delayMs + tempMuteDurationMs, device);
Eric Laurent99401132014-05-07 19:48:15 -07004754 }
4755 }
4756 }
4757
Eric Laurente552edb2014-03-10 17:42:56 -07004758 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4759 if (muteWaitMs > delayMs) {
4760 muteWaitMs -= delayMs;
4761 usleep(muteWaitMs * 1000);
4762 return muteWaitMs;
4763 }
4764 return 0;
4765}
4766
Eric Laurentc75307b2015-03-17 15:29:32 -07004767uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004768 audio_devices_t device,
4769 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004770 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004771 audio_patch_handle_t *patchHandle,
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004772 const char *address,
4773 bool requiresMuteCheck)
Eric Laurente552edb2014-03-10 17:42:56 -07004774{
Eric Laurentc75307b2015-03-17 15:29:32 -07004775 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004776 AudioParameter param;
4777 uint32_t muteWaitMs;
4778
4779 if (outputDesc->isDuplicated()) {
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004780 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs,
4781 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
4782 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs,
4783 nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck);
Eric Laurente552edb2014-03-10 17:42:56 -07004784 return muteWaitMs;
4785 }
4786 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4787 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004788 if ((device != AUDIO_DEVICE_NONE) &&
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004789 ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004790 return 0;
4791 }
4792
4793 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004794 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004795
4796 audio_devices_t prevDevice = outputDesc->mDevice;
4797
Paul McLeanaa981192015-03-21 09:55:15 -07004798 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004799
4800 if (device != AUDIO_DEVICE_NONE) {
4801 outputDesc->mDevice = device;
4802 }
Jean-Michel Trivib3733cf2018-02-15 19:17:50 +00004803
4804 // if the outputs are not materially active, there is no need to mute.
4805 if (requiresMuteCheck) {
4806 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4807 } else {
4808 ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__);
4809 muteWaitMs = 0;
4810 }
Eric Laurente552edb2014-03-10 17:42:56 -07004811
4812 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004813 // the requested device is AUDIO_DEVICE_NONE
4814 // OR the requested device is the same as current device
4815 // AND force is not specified
4816 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004817 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004818 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4819 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004820 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004821 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004822 return muteWaitMs;
4823 }
4824
4825 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004826
Eric Laurente552edb2014-03-10 17:42:56 -07004827 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004828 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004829 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004830 } else {
Eric Laurentc40d9692016-04-13 19:14:13 -07004831 DeviceVector deviceList;
4832 if ((address == NULL) || (strlen(address) == 0)) {
4833 deviceList = mAvailableOutputDevices.getDevicesFromType(device);
4834 } else {
4835 deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
4836 }
4837
Eric Laurent1c333e22014-05-20 10:48:17 -07004838 if (!deviceList.isEmpty()) {
4839 struct audio_patch patch;
4840 outputDesc->toAudioPortConfig(&patch.sources[0]);
4841 patch.num_sources = 1;
4842 patch.num_sinks = 0;
4843 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4844 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004845 patch.num_sinks++;
4846 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004847 ssize_t index;
4848 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4849 index = mAudioPatches.indexOfKey(*patchHandle);
4850 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004851 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004852 }
4853 sp< AudioPatch> patchDesc;
4854 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4855 if (index >= 0) {
4856 patchDesc = mAudioPatches.valueAt(index);
4857 afPatchHandle = patchDesc->mAfPatchHandle;
4858 }
4859
Eric Laurent1c333e22014-05-20 10:48:17 -07004860 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004861 &afPatchHandle,
4862 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004863 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4864 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004865 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004867 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004868 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004869 addAudioPatch(patchDesc->mHandle, patchDesc);
4870 } else {
4871 patchDesc->mPatch = patch;
4872 }
4873 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004874 if (patchHandle) {
4875 *patchHandle = patchDesc->mHandle;
4876 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004877 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004878 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004879 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004880 }
4881 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004882
4883 // inform all input as well
4884 for (size_t i = 0; i < mInputs.size(); i++) {
4885 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004886 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004887 AudioParameter inputCmd = AudioParameter();
4888 ALOGV("%s: inform input %d of device:%d", __func__,
4889 inputDescriptor->mIoHandle, device);
4890 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4891 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4892 inputCmd.toString(),
4893 delayMs);
4894 }
4895 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004896 }
Eric Laurente552edb2014-03-10 17:42:56 -07004897
4898 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004899 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004900
4901 return muteWaitMs;
4902}
4903
Eric Laurentc75307b2015-03-17 15:29:32 -07004904status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004905 int delayMs,
4906 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004907{
Eric Laurent6a94d692014-05-20 11:18:06 -07004908 ssize_t index;
4909 if (patchHandle) {
4910 index = mAudioPatches.indexOfKey(*patchHandle);
4911 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004912 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004913 }
4914 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004915 return INVALID_OPERATION;
4916 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004917 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4918 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004919 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07004920 outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07004921 removeAudioPatch(patchDesc->mHandle);
4922 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004923 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004924 return status;
4925}
4926
4927status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4928 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004929 bool force,
4930 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004931{
4932 status_t status = NO_ERROR;
4933
Eric Laurent1f2f2232014-06-02 12:01:23 -07004934 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004935 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4936 inputDesc->mDevice = device;
4937
4938 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4939 if (!deviceList.isEmpty()) {
4940 struct audio_patch patch;
4941 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004942 // AUDIO_SOURCE_HOTWORD is for internal use only:
4943 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004944 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004945 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004946 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4947 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004948 patch.num_sinks = 1;
4949 //only one input device for now
4950 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004951 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004952 ssize_t index;
4953 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4954 index = mAudioPatches.indexOfKey(*patchHandle);
4955 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004956 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004957 }
4958 sp< AudioPatch> patchDesc;
4959 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4960 if (index >= 0) {
4961 patchDesc = mAudioPatches.valueAt(index);
4962 afPatchHandle = patchDesc->mAfPatchHandle;
4963 }
4964
Eric Laurent1c333e22014-05-20 10:48:17 -07004965 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004966 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004967 0);
4968 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004969 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004970 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004971 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004972 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004973 addAudioPatch(patchDesc->mHandle, patchDesc);
4974 } else {
4975 patchDesc->mPatch = patch;
4976 }
4977 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004978 if (patchHandle) {
4979 *patchHandle = patchDesc->mHandle;
4980 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004981 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004982 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004983 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004984 }
4985 }
4986 }
4987 return status;
4988}
4989
Eric Laurent6a94d692014-05-20 11:18:06 -07004990status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4991 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004992{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004993 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004994 ssize_t index;
4995 if (patchHandle) {
4996 index = mAudioPatches.indexOfKey(*patchHandle);
4997 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004998 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004999 }
5000 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005001 return INVALID_OPERATION;
5002 }
Eric Laurent6a94d692014-05-20 11:18:06 -07005003 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
5004 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07005005 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Glenn Kastena13cde92016-03-28 15:26:02 -07005006 inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE);
Eric Laurent6a94d692014-05-20 11:18:06 -07005007 removeAudioPatch(patchDesc->mHandle);
5008 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07005009 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07005010 return status;
5011}
5012
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005013sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005014 const String8& address,
François Gaffie53615e22015-03-19 09:24:12 +01005015 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07005016 audio_format_t& format,
5017 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01005018 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07005019{
5020 // Choose an input profile based on the requested capture parameters: select the first available
5021 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07005022 //
5023 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
5024 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07005025
Mikhail Naganov7e22e942017-12-07 10:04:29 -08005026 for (const auto& hwModule : mHwModules) {
Mikhail Naganova5e165d2017-12-07 17:08:02 -08005027 for (const auto& profile : hwModule->getInputProfiles()) {
Eric Laurentd4692962014-05-05 18:13:44 -07005028 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005029 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005030 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07005031 format,
5032 &format /*updatedFormat*/,
5033 channelMask,
5034 &channelMask /*updatedChannelMask*/,
5035 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005036
Eric Laurente552edb2014-03-10 17:42:56 -07005037 return profile;
5038 }
5039 }
5040 }
5041 return NULL;
5042}
5043
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005044
5045audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01005046 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005047{
François Gaffie036e1e92015-03-19 10:16:24 +01005048 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
5049 audio_devices_t selectedDeviceFromMix =
5050 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08005051
François Gaffie036e1e92015-03-19 10:16:24 +01005052 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
5053 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005054 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005055 return getDeviceForInputSource(inputSource);
5056}
5057
5058audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5059{
Eric Laurentad2e7b92017-09-14 20:06:42 -07005060 // Routing
5061 // Scan the whole RouteMap to see if we have an explicit route:
5062 // if the input source in the RouteMap is the same as the argument above,
5063 // and activity count is non-zero and the device in the route descriptor is available
5064 // then select this device.
Paul McLean466dc8e2015-04-17 13:15:36 -06005065 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
5066 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurentad2e7b92017-09-14 20:06:42 -07005067 if ((inputSource == route->mSource) && route->isActive() &&
5068 (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) {
Paul McLean466dc8e2015-04-17 13:15:36 -06005069 return route->mDeviceDescriptor->type();
5070 }
5071 }
5072
5073 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07005074}
5075
Eric Laurente0720872014-03-11 09:30:41 -07005076float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005077 int index,
5078 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005079{
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005080 float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Jean-Michel Trivi3d8b4a42016-09-14 18:37:46 -07005081
5082 // handle the case of accessibility active while a ringtone is playing: if the ringtone is much
5083 // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch
5084 // exploration of the dialer UI. In this situation, bring the accessibility volume closer to
5085 // the ringtone volume
5086 if ((stream == AUDIO_STREAM_ACCESSIBILITY)
5087 && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState())
5088 && isStreamActive(AUDIO_STREAM_RING, 0)) {
5089 const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device);
5090 return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB;
5091 }
5092
Jean-Michel Trivi719a9872017-08-05 13:51:35 -07005093 // in-call: always cap earpiece volume by voice volume + some low headroom
5094 if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && isInCall()) {
5095 switch (stream) {
5096 case AUDIO_STREAM_SYSTEM:
5097 case AUDIO_STREAM_RING:
5098 case AUDIO_STREAM_MUSIC:
5099 case AUDIO_STREAM_ALARM:
5100 case AUDIO_STREAM_NOTIFICATION:
5101 case AUDIO_STREAM_ENFORCED_AUDIBLE:
5102 case AUDIO_STREAM_DTMF:
5103 case AUDIO_STREAM_ACCESSIBILITY: {
5104 const float maxVoiceVolDb = computeVolume(AUDIO_STREAM_VOICE_CALL, index, device)
5105 + IN_CALL_EARPIECE_HEADROOM_DB;
5106 if (volumeDB > maxVoiceVolDb) {
5107 ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f",
5108 stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb);
5109 volumeDB = maxVoiceVolDb;
5110 }
5111 } break;
5112 default:
5113 break;
5114 }
5115 }
5116
Eric Laurente552edb2014-03-10 17:42:56 -07005117 // if a headset is connected, apply the following rules to ring tones and notifications
5118 // to avoid sound level bursts in user's ears:
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005119 // - always attenuate notifications volume by 6dB
5120 // - attenuate ring tones volume by 6dB unless music is not playing and
5121 // speaker is part of the select devices
Eric Laurente552edb2014-03-10 17:42:56 -07005122 // - if music is playing, always limit the volume to current music volume,
5123 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005124 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005125 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5126 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5127 AUDIO_DEVICE_OUT_WIRED_HEADSET |
Eric Laurent904d6322017-03-17 17:20:47 -07005128 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
5129 AUDIO_DEVICE_OUT_USB_HEADSET)) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005130 ((stream_strategy == STRATEGY_SONIFICATION)
5131 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005132 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005133 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01005134 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01005135 mVolumeCurves->canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005136 // when the phone is ringing we must consider that music could have been paused just before
5137 // by the music application and behave as if music was active if the last music track was
5138 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005139 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005140 mLimitRingtoneVolume) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005141 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005142 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005143 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005144 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
5145 musicDevice),
5146 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07005147 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
5148 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005149 if (volumeDB > minVolDB) {
5150 volumeDB = minVolDB;
Eric Laurentffbc80f2015-03-18 18:30:19 -07005151 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07005152 }
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005153 if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5154 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) {
5155 // on A2DP, also ensure notification volume is not too low compared to media when
5156 // intended to be played
5157 if ((volumeDB > -96.0f) &&
5158 (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) {
5159 ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f",
5160 stream, device,
5161 volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB);
5162 volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB;
5163 }
5164 }
Eric Laurent6af1c1d2016-04-14 11:20:44 -07005165 } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) ||
5166 stream_strategy != STRATEGY_SONIFICATION) {
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005167 volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07005168 }
5169 }
5170
Jean-Michel Trivi00a20962016-05-25 19:11:01 -07005171 return volumeDB;
Eric Laurente552edb2014-03-10 17:42:56 -07005172}
5173
Eric Laurente0720872014-03-11 09:30:41 -07005174status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005175 int index,
5176 const sp<AudioOutputDescriptor>& outputDesc,
5177 audio_devices_t device,
5178 int delayMs,
5179 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005180{
Eric Laurente552edb2014-03-10 17:42:56 -07005181 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07005182 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07005183 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07005184 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005185 return NO_ERROR;
5186 }
François Gaffie2110e042015-03-24 08:41:51 +01005187 audio_policy_forced_cfg_t forceUseForComm =
5188 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07005189 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01005190 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
5191 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005192 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01005193 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07005194 return INVALID_OPERATION;
5195 }
5196
Eric Laurentc75307b2015-03-17 15:29:32 -07005197 if (device == AUDIO_DEVICE_NONE) {
5198 device = outputDesc->device();
5199 }
Eric Laurent275e8e92014-11-30 15:14:47 -08005200
Eric Laurentffbc80f2015-03-18 18:30:19 -07005201 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07005202 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07005203 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08005204 }
Eric Laurentc75307b2015-03-17 15:29:32 -07005205
Eric Laurentffbc80f2015-03-18 18:30:19 -07005206 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07005207
Eric Laurent3b73df72014-03-11 09:06:29 -07005208 if (stream == AUDIO_STREAM_VOICE_CALL ||
5209 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005210 float voiceVolume;
5211 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005212 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005213 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005214 } else {
5215 voiceVolume = 1.0;
5216 }
5217
Eric Laurent18fba842016-03-31 14:41:26 -07005218 if (voiceVolume != mLastVoiceVolume) {
Eric Laurente552edb2014-03-10 17:42:56 -07005219 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5220 mLastVoiceVolume = voiceVolume;
5221 }
5222 }
5223
5224 return NO_ERROR;
5225}
5226
Eric Laurentc75307b2015-03-17 15:29:32 -07005227void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
5228 audio_devices_t device,
5229 int delayMs,
5230 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07005231{
Eric Laurentc75307b2015-03-17 15:29:32 -07005232 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07005233
Eric Laurent794fde22016-03-11 09:50:45 -08005234 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005235 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005236 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005237 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005238 device,
5239 delayMs,
5240 force);
5241 }
5242}
5243
Eric Laurente0720872014-03-11 09:30:41 -07005244void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07005245 bool on,
5246 const sp<AudioOutputDescriptor>& outputDesc,
5247 int delayMs,
5248 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005249{
Eric Laurent72249d52015-06-08 11:12:15 -07005250 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
5251 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08005252 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005253 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005254 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005255 }
5256 }
5257}
5258
Eric Laurente0720872014-03-11 09:30:41 -07005259void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07005260 bool on,
5261 const sp<AudioOutputDescriptor>& outputDesc,
5262 int delayMs,
5263 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005264{
Eric Laurente552edb2014-03-10 17:42:56 -07005265 if (device == AUDIO_DEVICE_NONE) {
5266 device = outputDesc->device();
5267 }
5268
Eric Laurentc75307b2015-03-17 15:29:32 -07005269 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
5270 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07005271
5272 if (on) {
5273 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01005274 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005275 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01005276 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005277 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005278 }
5279 }
5280 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5281 outputDesc->mMuteCount[stream]++;
5282 } else {
5283 if (outputDesc->mMuteCount[stream] == 0) {
5284 ALOGV("setStreamMute() unmuting non muted stream!");
5285 return;
5286 }
5287 if (--outputDesc->mMuteCount[stream] == 0) {
5288 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01005289 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07005290 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005291 device,
5292 delayMs);
5293 }
5294 }
5295}
5296
Eric Laurente0720872014-03-11 09:30:41 -07005297void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005298 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005299{
Eric Laurent87ffa392015-05-22 10:32:38 -07005300 if(!hasPrimaryOutput()) {
5301 return;
5302 }
5303
Eric Laurente552edb2014-03-10 17:42:56 -07005304 // if the stream pertains to sonification strategy and we are in call we must
5305 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5306 // in the device used for phone strategy and play the tone if the selected device does not
5307 // interfere with the device used for phone strategy
5308 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5309 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005310 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005311 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5312 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005313 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005314 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5315 stream, starting, outputDesc->mDevice, stateChange);
5316 if (outputDesc->mRefCount[stream]) {
5317 int muteCount = 1;
5318 if (stateChange) {
5319 muteCount = outputDesc->mRefCount[stream];
5320 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005321 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005322 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5323 for (int i = 0; i < muteCount; i++) {
5324 setStreamMute(stream, starting, mPrimaryOutput);
5325 }
5326 } else {
5327 ALOGV("handleIncallSonification() high visibility");
5328 if (outputDesc->device() &
5329 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5330 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5331 for (int i = 0; i < muteCount; i++) {
5332 setStreamMute(stream, starting, mPrimaryOutput);
5333 }
5334 }
5335 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005336 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5337 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005338 } else {
5339 mpClientInterface->stopTone();
5340 }
5341 }
5342 }
5343 }
5344}
5345
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005346audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5347{
5348 // flags to stream type mapping
5349 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5350 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5351 }
5352 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5353 return AUDIO_STREAM_BLUETOOTH_SCO;
5354 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005355 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5356 return AUDIO_STREAM_TTS;
5357 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005358
5359 // usage to stream type mapping
5360 switch (attr->usage) {
5361 case AUDIO_USAGE_MEDIA:
5362 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005363 case AUDIO_USAGE_ASSISTANT:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005364 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5365 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005366 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5367 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005368 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5369 return AUDIO_STREAM_SYSTEM;
5370 case AUDIO_USAGE_VOICE_COMMUNICATION:
5371 return AUDIO_STREAM_VOICE_CALL;
5372
5373 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5374 return AUDIO_STREAM_DTMF;
5375
5376 case AUDIO_USAGE_ALARM:
5377 return AUDIO_STREAM_ALARM;
5378 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5379 return AUDIO_STREAM_RING;
5380
5381 case AUDIO_USAGE_NOTIFICATION:
5382 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5383 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5384 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5385 case AUDIO_USAGE_NOTIFICATION_EVENT:
5386 return AUDIO_STREAM_NOTIFICATION;
5387
5388 case AUDIO_USAGE_UNKNOWN:
5389 default:
5390 return AUDIO_STREAM_MUSIC;
5391 }
5392}
Eric Laurente83b55d2014-11-14 10:06:21 -08005393
François Gaffie53615e22015-03-19 09:24:12 +01005394bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5395{
Eric Laurente83b55d2014-11-14 10:06:21 -08005396 // has flags that map to a strategy?
5397 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5398 return true;
5399 }
5400
5401 // has known usage?
5402 switch (paa->usage) {
5403 case AUDIO_USAGE_UNKNOWN:
5404 case AUDIO_USAGE_MEDIA:
5405 case AUDIO_USAGE_VOICE_COMMUNICATION:
5406 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5407 case AUDIO_USAGE_ALARM:
5408 case AUDIO_USAGE_NOTIFICATION:
5409 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5410 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5411 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5412 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5413 case AUDIO_USAGE_NOTIFICATION_EVENT:
5414 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5415 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5416 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5417 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005418 case AUDIO_USAGE_VIRTUAL_SOURCE:
Jean-Michel Trivi36867762016-12-29 12:03:28 -08005419 case AUDIO_USAGE_ASSISTANT:
Eric Laurente83b55d2014-11-14 10:06:21 -08005420 break;
5421 default:
5422 return false;
5423 }
5424 return true;
5425}
5426
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07005427bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc,
François Gaffiead3183e2015-03-18 16:55:35 +01005428 routing_strategy strategy, uint32_t inPastMs,
5429 nsecs_t sysTime) const
5430{
5431 if ((sysTime == 0) && (inPastMs != 0)) {
5432 sysTime = systemTime();
5433 }
Eric Laurent794fde22016-03-11 09:50:45 -08005434 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005435 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5436 (NUM_STRATEGIES == strategy)) &&
5437 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5438 return true;
5439 }
5440 }
5441 return false;
5442}
5443
François Gaffie2110e042015-03-24 08:41:51 +01005444audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5445{
5446 return mEngine->getForceUse(usage);
5447}
5448
5449bool AudioPolicyManager::isInCall()
5450{
5451 return isStateInCall(mEngine->getPhoneState());
5452}
5453
5454bool AudioPolicyManager::isStateInCall(int state)
5455{
5456 return is_state_in_call(state);
5457}
5458
Eric Laurentd60560a2015-04-10 11:31:20 -07005459void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5460{
5461 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5462 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5463 if (sourceDesc->mDevice->equals(deviceDesc)) {
5464 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5465 stopAudioSource(sourceDesc->getHandle());
5466 }
5467 }
5468
5469 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5470 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5471 bool release = false;
5472 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5473 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5474 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5475 source->ext.device.type == deviceDesc->type()) {
5476 release = true;
5477 }
5478 }
5479 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5480 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5481 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5482 sink->ext.device.type == deviceDesc->type()) {
5483 release = true;
5484 }
5485 }
5486 if (release) {
5487 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5488 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5489 }
5490 }
5491}
5492
Phil Burk09bc4612016-02-24 15:58:15 -08005493// Modify the list of surround sound formats supported.
Phil Burk0709b0a2016-03-31 12:54:57 -07005494void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
5495 FormatVector &formats = *formatsPtr;
Phil Burk07ac1142016-03-25 13:39:29 -07005496 // TODO Set this based on Config properties.
5497 const bool alwaysForceAC3 = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005498
5499 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5500 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
Phil Burk0709b0a2016-03-31 12:54:57 -07005501 ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
Phil Burk09bc4612016-02-24 15:58:15 -08005502
5503 // Analyze original support for various formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005504 bool supportsAC3 = false;
5505 bool supportsOtherSurround = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005506 bool supportsIEC61937 = false;
Mikhail Naganovcf84e592017-12-07 11:25:11 -08005507 for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) {
Phil Burk09bc4612016-02-24 15:58:15 -08005508 audio_format_t format = formats[formatIndex];
Phil Burk09bc4612016-02-24 15:58:15 -08005509 switch (format) {
5510 case AUDIO_FORMAT_AC3:
Phil Burk07ac1142016-03-25 13:39:29 -07005511 supportsAC3 = true;
5512 break;
Phil Burk09bc4612016-02-24 15:58:15 -08005513 case AUDIO_FORMAT_E_AC3:
5514 case AUDIO_FORMAT_DTS:
5515 case AUDIO_FORMAT_DTS_HD:
jiabindd601152017-11-27 14:53:37 -08005516 // If ALWAYS, remove all other surround formats here since we will add them later.
5517 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5518 formats.removeAt(formatIndex);
5519 formatIndex--;
5520 }
Phil Burk07ac1142016-03-25 13:39:29 -07005521 supportsOtherSurround = true;
Phil Burk09bc4612016-02-24 15:58:15 -08005522 break;
5523 case AUDIO_FORMAT_IEC61937:
5524 supportsIEC61937 = true;
5525 break;
5526 default:
5527 break;
5528 }
5529 }
Phil Burk09bc4612016-02-24 15:58:15 -08005530
5531 // Modify formats based on surround preferences.
5532 // If NEVER, remove support for surround formats.
Phil Burk07ac1142016-03-25 13:39:29 -07005533 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5534 if (supportsAC3 || supportsOtherSurround || supportsIEC61937) {
5535 // Remove surround sound related formats.
5536 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5537 audio_format_t format = formats[formatIndex];
5538 switch(format) {
5539 case AUDIO_FORMAT_AC3:
5540 case AUDIO_FORMAT_E_AC3:
5541 case AUDIO_FORMAT_DTS:
5542 case AUDIO_FORMAT_DTS_HD:
5543 case AUDIO_FORMAT_IEC61937:
Phil Burk07ac1142016-03-25 13:39:29 -07005544 formats.removeAt(formatIndex);
5545 break;
5546 default:
5547 formatIndex++; // keep it
5548 break;
5549 }
Phil Burk09bc4612016-02-24 15:58:15 -08005550 }
Phil Burk07ac1142016-03-25 13:39:29 -07005551 supportsAC3 = false;
5552 supportsOtherSurround = false;
5553 supportsIEC61937 = false;
Phil Burk09bc4612016-02-24 15:58:15 -08005554 }
Phil Burk07ac1142016-03-25 13:39:29 -07005555 } else { // AUTO or ALWAYS
5556 // Most TVs support AC3 even if they do not report it in the EDID.
5557 if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS))
5558 && !supportsAC3) {
5559 formats.add(AUDIO_FORMAT_AC3);
5560 supportsAC3 = true;
5561 }
5562
5563 // If ALWAYS, add support for raw surround formats if all are missing.
5564 // This assumes that if any of these formats are reported by the HAL
5565 // then the report is valid and should not be modified.
jiabindd601152017-11-27 14:53:37 -08005566 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
Phil Burk07ac1142016-03-25 13:39:29 -07005567 formats.add(AUDIO_FORMAT_E_AC3);
5568 formats.add(AUDIO_FORMAT_DTS);
5569 formats.add(AUDIO_FORMAT_DTS_HD);
5570 supportsOtherSurround = true;
5571 }
5572
5573 // Add support for IEC61937 if any raw surround supported.
5574 // The HAL could do this but add it here, just in case.
5575 if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) {
5576 formats.add(AUDIO_FORMAT_IEC61937);
5577 supportsIEC61937 = true;
5578 }
Phil Burk09bc4612016-02-24 15:58:15 -08005579 }
Phil Burk0709b0a2016-03-31 12:54:57 -07005580}
5581
5582// Modify the list of channel masks supported.
5583void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
5584 ChannelsVector &channelMasks = *channelMasksPtr;
5585 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5586 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5587
5588 // If NEVER, then remove support for channelMasks > stereo.
5589 if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
5590 for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
5591 audio_channel_mask_t channelMask = channelMasks[maskIndex];
5592 if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
5593 ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
5594 channelMasks.removeAt(maskIndex);
5595 } else {
5596 maskIndex++;
5597 }
5598 }
5599 // If ALWAYS, then make sure we at least support 5.1
5600 } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
5601 bool supports5dot1 = false;
5602 // Are there any channel masks that can be considered "surround"?
Mikhail Naganovcf84e592017-12-07 11:25:11 -08005603 for (audio_channel_mask_t channelMask : channelMasks) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005604 if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
5605 supports5dot1 = true;
5606 break;
5607 }
5608 }
5609 // If not then add 5.1 support.
5610 if (!supports5dot1) {
5611 channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
5612 ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
5613 }
Phil Burk09bc4612016-02-24 15:58:15 -08005614 }
5615}
5616
Phil Burk00eeb322016-03-31 12:41:00 -07005617void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
5618 audio_io_handle_t ioHandle,
François Gaffie112b0af2015-11-19 16:13:25 +01005619 AudioProfileVector &profiles)
5620{
5621 String8 reply;
Phil Burk0709b0a2016-03-31 12:54:57 -07005622
François Gaffie112b0af2015-11-19 16:13:25 +01005623 // Format MUST be checked first to update the list of AudioProfile
5624 if (profiles.hasDynamicFormat()) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005625 reply = mpClientInterface->getParameters(
5626 ioHandle, String8(AudioParameter::keyStreamSupportedFormats));
Phil Burk0709b0a2016-03-31 12:54:57 -07005627 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005628 AudioParameter repliedParameters(reply);
5629 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005630 String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005631 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5632 return;
5633 }
Phil Burk09bc4612016-02-24 15:58:15 -08005634 FormatVector formats = formatsFromString(reply.string());
Phil Burk00eeb322016-03-31 12:41:00 -07005635 if (device == AUDIO_DEVICE_OUT_HDMI) {
Phil Burk0709b0a2016-03-31 12:54:57 -07005636 filterSurroundFormats(&formats);
Phil Burk00eeb322016-03-31 12:41:00 -07005637 }
Phil Burk09bc4612016-02-24 15:58:15 -08005638 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005639 }
François Gaffie112b0af2015-11-19 16:13:25 +01005640
Mikhail Naganovcf84e592017-12-07 11:25:11 -08005641 for (audio_format_t format : profiles.getSupportedFormats()) {
Eric Laurent20eb3a42016-01-26 18:39:17 -08005642 ChannelsVector channelMasks;
5643 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005644 AudioParameter requestedParameters;
Mikhail Naganov388360c2016-10-17 17:09:41 -07005645 requestedParameters.addInt(String8(AudioParameter::keyFormat), format);
François Gaffie112b0af2015-11-19 16:13:25 +01005646
5647 if (profiles.hasDynamicRateFor(format)) {
Mikhail Naganov388360c2016-10-17 17:09:41 -07005648 reply = mpClientInterface->getParameters(
5649 ioHandle,
5650 requestedParameters.toString() + ";" +
5651 AudioParameter::keyStreamSupportedSamplingRates);
François Gaffie112b0af2015-11-19 16:13:25 +01005652 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005653 AudioParameter repliedParameters(reply);
5654 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005655 String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005656 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005657 }
5658 }
5659 if (profiles.hasDynamicChannelsFor(format)) {
5660 reply = mpClientInterface->getParameters(ioHandle,
5661 requestedParameters.toString() + ";" +
Mikhail Naganov388360c2016-10-17 17:09:41 -07005662 AudioParameter::keyStreamSupportedChannels);
François Gaffie112b0af2015-11-19 16:13:25 +01005663 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005664 AudioParameter repliedParameters(reply);
5665 if (repliedParameters.get(
Mikhail Naganov388360c2016-10-17 17:09:41 -07005666 String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) {
Eric Laurent62e4bc52016-02-02 18:37:28 -08005667 channelMasks = channelMasksFromString(reply.string());
Phil Burk0709b0a2016-03-31 12:54:57 -07005668 if (device == AUDIO_DEVICE_OUT_HDMI) {
5669 filterSurroundChannelMasks(&channelMasks);
5670 }
François Gaffie112b0af2015-11-19 16:13:25 +01005671 }
5672 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005673 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005674 }
5675}
Eric Laurentd60560a2015-04-10 11:31:20 -07005676
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08005677} // namespace android