blob: a7b90f4fee9b61abc4272b76d7fff0feaeda4048 [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
Phil Burk09bc4612016-02-24 15:58:15 -080017#define LOG_TAG "AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
François Gaffief4ad6e52015-11-19 16:59:57 +010027#define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml"
28
Eric Laurentd4692962014-05-05 18:13:44 -070029#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070030#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070031
François Gaffie2110e042015-03-24 08:41:51 +010032#include <AudioPolicyManagerInterface.h>
33#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070034#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070035#include <utils/Log.h>
36#include <hardware/audio.h>
37#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070041#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010042#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010043#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010045#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010047#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010048#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070049
Eric Laurent3b73df72014-03-11 09:06:29 -070050namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070051
52// ----------------------------------------------------------------------------
53// AudioPolicyInterface implementation
54// ----------------------------------------------------------------------------
55
Eric Laurente0720872014-03-11 09:30:41 -070056status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080057 audio_policy_dev_state_t state,
58 const char *device_address,
59 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070060{
Paul McLeane743a472015-01-28 11:07:31 -080061 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062}
63
64status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080065 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080066 const char *device_address,
67 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080068{
Paul McLeane743a472015-01-28 11:07:31 -080069 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
70- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070071
72 // connect/disconnect only 1 device at a time
73 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
74
François Gaffie53615e22015-03-19 09:24:12 +010075 sp<DeviceDescriptor> devDesc =
76 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080077
Eric Laurente552edb2014-03-10 17:42:56 -070078 // handle output devices
79 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070080 SortedVector <audio_io_handle_t> outputs;
81
Eric Laurent3a4311c2014-03-17 12:00:47 -070082 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
83
Eric Laurente552edb2014-03-10 17:42:56 -070084 // save a copy of the opened output descriptors before any output is opened or closed
85 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
86 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070087 switch (state)
88 {
89 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080090 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070091 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070092 ALOGW("setDeviceConnectionState() device already connected: %x", device);
93 return INVALID_OPERATION;
94 }
95 ALOGV("setDeviceConnectionState() connecting device %x", device);
96
Eric Laurente552edb2014-03-10 17:42:56 -070097 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070098 index = mAvailableOutputDevices.add(devDesc);
99 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +0100100 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700101 if (module == 0) {
102 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
103 device);
104 mAvailableOutputDevices.remove(devDesc);
105 return INVALID_OPERATION;
106 }
Paul McLeane743a472015-01-28 11:07:31 -0800107 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700108 } else {
109 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700110 }
111
Eric Laurenta1d525f2015-01-29 13:36:45 -0800112 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 mAvailableOutputDevices.remove(devDesc);
114 return INVALID_OPERATION;
115 }
François Gaffie2110e042015-03-24 08:41:51 +0100116 // Propagate device availability to Engine
117 mEngine->setDeviceConnectionState(devDesc, state);
118
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700119 // outputs should never be empty here
120 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
121 "checkOutputsForDevice() returned no outputs but status OK");
122 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
123 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800124
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800125 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800126 AudioParameter param = AudioParameter(devDesc->mAddress);
127 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
128 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
129
130 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700131 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700132 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700133 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700134 ALOGW("setDeviceConnectionState() device not connected: %x", device);
135 return INVALID_OPERATION;
136 }
137
Paul McLean5c477aa2014-08-20 16:47:57 -0700138 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
139
Paul McLeane743a472015-01-28 11:07:31 -0800140 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800141 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700142 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
143 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
144
Eric Laurente552edb2014-03-10 17:42:56 -0700145 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700146 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700147
Eric Laurenta1d525f2015-01-29 13:36:45 -0800148 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100149
150 // Propagate device availability to Engine
151 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700152 } break;
153
154 default:
155 ALOGE("setDeviceConnectionState() invalid state: %x", state);
156 return BAD_VALUE;
157 }
158
Eric Laurent3a4311c2014-03-17 12:00:47 -0700159 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
160 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700161 checkA2dpSuspend();
162 checkOutputForAllStrategies();
163 // outputs must be closed after checkOutputForAllStrategies() is executed
164 if (!outputs.isEmpty()) {
165 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700166 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700167 // close unused outputs after device disconnection or direct outputs that have been
168 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700169 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700170 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
171 (desc->mDirectOpenCount == 0))) {
172 closeOutput(outputs[i]);
173 }
174 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700175 // check again after closing A2DP output to reset mA2dpSuspended if needed
176 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700177 }
178
179 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700180 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700181 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
182 updateCallRouting(newDevice);
183 }
Eric Laurente552edb2014-03-10 17:42:56 -0700184 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
186 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
187 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700188 // do not force device change on duplicated output because if device is 0, it will
189 // also force a device 0 for the two outputs it is duplicated to which may override
190 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700191 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100192 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700193 // always force when disconnecting (a non-duplicated device)
194 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700195 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700196 }
Eric Laurente552edb2014-03-10 17:42:56 -0700197 }
198
Eric Laurentd60560a2015-04-10 11:31:20 -0700199 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
200 cleanUpForDevice(devDesc);
201 }
202
Eric Laurent72aa32f2014-05-30 18:51:48 -0700203 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700204 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700205 } // end if is output device
206
Eric Laurente552edb2014-03-10 17:42:56 -0700207 // handle input devices
208 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700209 SortedVector <audio_io_handle_t> inputs;
210
Eric Laurent3a4311c2014-03-17 12:00:47 -0700211 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700212 switch (state)
213 {
214 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700215 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700216 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700217 ALOGW("setDeviceConnectionState() device already connected: %d", device);
218 return INVALID_OPERATION;
219 }
François Gaffie53615e22015-03-19 09:24:12 +0100220 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700221 if (module == NULL) {
222 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
223 device);
224 return INVALID_OPERATION;
225 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700226 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700227 return INVALID_OPERATION;
228 }
229
Eric Laurent3a4311c2014-03-17 12:00:47 -0700230 index = mAvailableInputDevices.add(devDesc);
231 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800232 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700233 } else {
234 return NO_MEMORY;
235 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800236
237 // Set connect to HALs
238 AudioParameter param = AudioParameter(devDesc->mAddress);
239 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
240 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
241
François Gaffie2110e042015-03-24 08:41:51 +0100242 // Propagate device availability to Engine
243 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700244 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700245
246 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700247 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700248 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700249 ALOGW("setDeviceConnectionState() device not connected: %d", device);
250 return INVALID_OPERATION;
251 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
253 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
254
255 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800256 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700257 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
258 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
259
Paul McLean9080a4c2015-06-18 08:24:02 -0700260 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700262
François Gaffie2110e042015-03-24 08:41:51 +0100263 // Propagate device availability to Engine
264 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700265 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700266
267 default:
268 ALOGE("setDeviceConnectionState() invalid state: %x", state);
269 return BAD_VALUE;
270 }
271
Eric Laurentd4692962014-05-05 18:13:44 -0700272 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700273
Eric Laurent87ffa392015-05-22 10:32:38 -0700274 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700275 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
276 updateCallRouting(newDevice);
277 }
278
Eric Laurentd60560a2015-04-10 11:31:20 -0700279 if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
280 cleanUpForDevice(devDesc);
281 }
282
Eric Laurentb52c1522014-05-20 11:27:36 -0700283 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700284 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700285 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700286
287 ALOGW("setDeviceConnectionState() invalid device: %x", device);
288 return BAD_VALUE;
289}
290
Eric Laurente0720872014-03-11 09:30:41 -0700291audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100292 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700293{
François Gaffie53615e22015-03-19 09:24:12 +0100294 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
295
Eric Laurent3a4311c2014-03-17 12:00:47 -0700296 DeviceVector *deviceVector;
297
Eric Laurente552edb2014-03-10 17:42:56 -0700298 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700299 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700300 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700301 deviceVector = &mAvailableInputDevices;
302 } else {
303 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
304 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700305 }
François Gaffie53615e22015-03-19 09:24:12 +0100306 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800307}
308
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
310{
311 bool createTxPatch = false;
312 struct audio_patch patch;
313 patch.num_sources = 1;
314 patch.num_sinks = 1;
315 status_t status;
316 audio_patch_handle_t afPatchHandle;
317 DeviceVector deviceList;
318
Eric Laurent87ffa392015-05-22 10:32:38 -0700319 if(!hasPrimaryOutput()) {
320 return;
321 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800322 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700323 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
324
325 // release existing RX patch if any
326 if (mCallRxPatch != 0) {
327 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
328 mCallRxPatch.clear();
329 }
330 // release TX patch if any
331 if (mCallTxPatch != 0) {
332 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
333 mCallTxPatch.clear();
334 }
335
336 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
337 // via setOutputDevice() on primary output.
338 // Otherwise, create two audio patches for TX and RX path.
339 if (availablePrimaryOutputDevices() & rxDevice) {
340 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
341 // If the TX device is also on the primary HW module, setOutputDevice() will take care
342 // of it due to legacy implementation. If not, create a patch.
343 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
344 == AUDIO_DEVICE_NONE) {
345 createTxPatch = true;
346 }
347 } else {
348 // create RX path audio patch
349 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
350 ALOG_ASSERT(!deviceList.isEmpty(),
351 "updateCallRouting() selected device not in output device list");
352 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
353 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
354 ALOG_ASSERT(!deviceList.isEmpty(),
355 "updateCallRouting() no telephony RX device");
356 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
357
358 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
359 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
360
361 // request to reuse existing output stream if one is already opened to reach the RX device
362 SortedVector<audio_io_handle_t> outputs =
363 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700364 audio_io_handle_t output = selectOutput(outputs,
365 AUDIO_OUTPUT_FLAG_NONE,
366 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700367 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700368 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700369 ALOG_ASSERT(!outputDesc->isDuplicated(),
370 "updateCallRouting() RX device output is duplicated");
371 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700372 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700373 patch.num_sources = 2;
374 }
375
376 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
377 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
378 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
379 status);
380 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100381 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700382 mCallRxPatch->mAfPatchHandle = afPatchHandle;
383 mCallRxPatch->mUid = mUidCached;
384 }
385 createTxPatch = true;
386 }
387 if (createTxPatch) {
388
389 struct audio_patch patch;
390 patch.num_sources = 1;
391 patch.num_sinks = 1;
392 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
393 ALOG_ASSERT(!deviceList.isEmpty(),
394 "updateCallRouting() selected device not in input device list");
395 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
396 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
397 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
398 ALOG_ASSERT(!deviceList.isEmpty(),
399 "updateCallRouting() no telephony TX device");
400 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
401 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
402
403 SortedVector<audio_io_handle_t> outputs =
404 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700405 audio_io_handle_t output = selectOutput(outputs,
406 AUDIO_OUTPUT_FLAG_NONE,
407 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 // request to reuse existing output stream if one is already opened to reach the TX
409 // path output device
410 if (output != AUDIO_IO_HANDLE_NONE) {
411 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
412 ALOG_ASSERT(!outputDesc->isDuplicated(),
413 "updateCallRouting() RX device output is duplicated");
414 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700415 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700416 patch.num_sources = 2;
417 }
418
Eric Laurentc0a889f2015-10-14 14:36:34 -0700419 // terminate active capture if on the same HW module as the call TX source device
420 // FIXME: would be better to refine to only inputs whose profile connects to the
421 // call TX device but this information is not in the audio patch and logic here must be
422 // symmetric to the one in startInput()
Eric Laurent232f26f2016-02-17 18:06:27 -0800423 audio_io_handle_t activeInput = mInputs.getActiveInput();
424 if (activeInput != 0) {
425 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
426 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
427 //FIXME: consider all active sessions
428 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
429 audio_session_t activeSession = activeSessions.keyAt(0);
430 stopInput(activeInput, activeSession);
431 releaseInput(activeInput, activeSession);
Eric Laurentc0a889f2015-10-14 14:36:34 -0700432 }
433 }
434
Eric Laurentc2730ba2014-07-20 15:47:07 -0700435 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
436 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
437 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
438 status);
439 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100440 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700441 mCallTxPatch->mAfPatchHandle = afPatchHandle;
442 mCallTxPatch->mUid = mUidCached;
443 }
444 }
445}
446
Eric Laurente0720872014-03-11 09:30:41 -0700447void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700448{
449 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100450 // store previous phone state for management of sonification strategy below
451 int oldState = mEngine->getPhoneState();
452
453 if (mEngine->setPhoneState(state) != NO_ERROR) {
454 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700455 return;
456 }
François Gaffie2110e042015-03-24 08:41:51 +0100457 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700458 // if leaving call state, handle special case of active streams
459 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700460 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700461 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700462 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800463 if (stream == AUDIO_STREAM_PATCH) {
464 continue;
465 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700466 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700467 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800468
Eric Laurent63dea1d2015-07-02 17:10:28 -0700469 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800470 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
472
François Gaffie2110e042015-03-24 08:41:51 +0100473 /**
474 * Switching to or from incall state or switching between telephony and VoIP lead to force
475 * routing command.
476 */
477 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
478 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700479
480 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700481 checkA2dpSuspend();
482 checkOutputForAllStrategies();
483 updateDevicesAndOutputs();
484
Eric Laurente552edb2014-03-10 17:42:56 -0700485 int delayMs = 0;
486 if (isStateInCall(state)) {
487 nsecs_t sysTime = systemTime();
488 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700489 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700490 // mute media and sonification strategies and delay device switch by the largest
491 // latency of any output where either strategy is active.
492 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100493 if ((isStrategyActive(desc, STRATEGY_MEDIA,
494 SONIFICATION_HEADSET_MUSIC_DELAY,
495 sysTime) ||
496 isStrategyActive(desc, STRATEGY_SONIFICATION,
497 SONIFICATION_HEADSET_MUSIC_DELAY,
498 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700499 (delayMs < (int)desc->latency()*2)) {
500 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700501 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700502 setStrategyMute(STRATEGY_MEDIA, true, desc);
503 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700504 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700505 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
506 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700507 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
508 }
509 }
510
Eric Laurent87ffa392015-05-22 10:32:38 -0700511 if (hasPrimaryOutput()) {
512 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
513 // the device returned is not necessarily reachable via this output
514 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
515 // force routing command to audio hardware when ending call
516 // even if no device change is needed
517 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
518 rxDevice = mPrimaryOutput->device();
519 }
Eric Laurente552edb2014-03-10 17:42:56 -0700520
Eric Laurent87ffa392015-05-22 10:32:38 -0700521 if (state == AUDIO_MODE_IN_CALL) {
522 updateCallRouting(rxDevice, delayMs);
523 } else if (oldState == AUDIO_MODE_IN_CALL) {
524 if (mCallRxPatch != 0) {
525 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
526 mCallRxPatch.clear();
527 }
528 if (mCallTxPatch != 0) {
529 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
530 mCallTxPatch.clear();
531 }
532 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
533 } else {
534 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700535 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700536 }
Eric Laurente552edb2014-03-10 17:42:56 -0700537 // if entering in call state, handle special case of active streams
538 // pertaining to sonification strategy see handleIncallSonification()
539 if (isStateInCall(state)) {
540 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700541 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800542 if (stream == AUDIO_STREAM_PATCH) {
543 continue;
544 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700545 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700546 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700547
548 // force reevaluating accessibility routing when call starts
549 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700550 }
551
552 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700553 if (state == AUDIO_MODE_RINGTONE &&
554 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700555 mLimitRingtoneVolume = true;
556 } else {
557 mLimitRingtoneVolume = false;
558 }
559}
560
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700561audio_mode_t AudioPolicyManager::getPhoneState() {
562 return mEngine->getPhoneState();
563}
564
Eric Laurente0720872014-03-11 09:30:41 -0700565void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700566 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700567{
François Gaffie2110e042015-03-24 08:41:51 +0100568 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700569
François Gaffie2110e042015-03-24 08:41:51 +0100570 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
571 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
572 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700573 }
François Gaffie2110e042015-03-24 08:41:51 +0100574 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
575 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
576 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700577
578 // check for device and output changes triggered by new force usage
579 checkA2dpSuspend();
580 checkOutputForAllStrategies();
581 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800582
Eric Laurent87ffa392015-05-22 10:32:38 -0700583 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700584 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
585 updateCallRouting(newDevice);
586 }
Eric Laurente552edb2014-03-10 17:42:56 -0700587 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700588 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
589 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
590 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
591 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700592 }
Eric Laurente552edb2014-03-10 17:42:56 -0700593 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700594 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700595 }
596 }
597
Eric Laurent232f26f2016-02-17 18:06:27 -0800598 audio_io_handle_t activeInput = mInputs.getActiveInput();
599 if (activeInput != 0) {
600 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
601 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700602 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800603 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
604 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700605 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800606 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700607 }
Eric Laurente552edb2014-03-10 17:42:56 -0700608 }
Eric Laurente552edb2014-03-10 17:42:56 -0700609}
610
Eric Laurente0720872014-03-11 09:30:41 -0700611void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700612{
613 ALOGV("setSystemProperty() property %s, value %s", property, value);
614}
615
616// Find a direct output profile compatible with the parameters passed, even if the input flags do
617// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800618sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700619 audio_devices_t device,
620 uint32_t samplingRate,
621 audio_format_t format,
622 audio_channel_mask_t channelMask,
623 audio_output_flags_t flags)
624{
Eric Laurent861a6282015-05-18 15:40:16 -0700625 // only retain flags that will drive the direct output profile selection
626 // if explicitly requested
627 static const uint32_t kRelevantFlags =
628 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
629 flags =
630 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
631
632 sp<IOProfile> profile;
633
Eric Laurente552edb2014-03-10 17:42:56 -0700634 for (size_t i = 0; i < mHwModules.size(); i++) {
635 if (mHwModules[i]->mHandle == 0) {
636 continue;
637 }
638 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700639 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
640 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700641 samplingRate, NULL /*updatedSamplingRate*/,
642 format, NULL /*updatedFormat*/,
643 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700644 flags)) {
645 continue;
646 }
647 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100648 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700649 continue;
650 }
651 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100652 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700653 continue;
654 }
655 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100656 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700657 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700658 }
Eric Laurente552edb2014-03-10 17:42:56 -0700659 }
660 }
Eric Laurent861a6282015-05-18 15:40:16 -0700661 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700662}
663
Eric Laurente0720872014-03-11 09:30:41 -0700664audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100665 uint32_t samplingRate,
666 audio_format_t format,
667 audio_channel_mask_t channelMask,
668 audio_output_flags_t flags,
669 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700670{
Eric Laurent3b73df72014-03-11 09:06:29 -0700671 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700672 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
673 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
674 device, stream, samplingRate, format, channelMask, flags);
675
Eric Laurente83b55d2014-11-14 10:06:21 -0800676 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
677 stream, samplingRate,format, channelMask,
678 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700679}
680
Eric Laurente83b55d2014-11-14 10:06:21 -0800681status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
682 audio_io_handle_t *output,
683 audio_session_t session,
684 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700685 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800686 uint32_t samplingRate,
687 audio_format_t format,
688 audio_channel_mask_t channelMask,
689 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700690 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800691 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700692{
Eric Laurente83b55d2014-11-14 10:06:21 -0800693 audio_attributes_t attributes;
694 if (attr != NULL) {
695 if (!isValidAttributes(attr)) {
696 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
697 attr->usage, attr->content_type, attr->flags,
698 attr->tags);
699 return BAD_VALUE;
700 }
701 attributes = *attr;
702 } else {
703 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
704 ALOGE("getOutputForAttr(): invalid stream type");
705 return BAD_VALUE;
706 }
707 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700708 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700709 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800710 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100711 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800712 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100713 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800714 }
François Gaffie036e1e92015-03-19 10:16:24 +0100715 *stream = streamTypefromAttributesInt(&attributes);
716 *output = desc->mIoHandle;
717 ALOGV("getOutputForAttr() returns output %d", *output);
718 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800719 }
720 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
721 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
722 return BAD_VALUE;
723 }
724
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700725 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
726 " session %d selectedDeviceId %d",
727 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
728 session, selectedDeviceId);
729
730 *stream = streamTypefromAttributesInt(&attributes);
731
732 // Explicit routing?
733 sp<DeviceDescriptor> deviceDesc;
734 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
735 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
736 deviceDesc = mAvailableOutputDevices[i];
737 break;
738 }
739 }
740 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700741
Eric Laurente83b55d2014-11-14 10:06:21 -0800742 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700743 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700744
Eric Laurente83b55d2014-11-14 10:06:21 -0800745 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700746 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
747 }
748
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700749 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700750 device, samplingRate, format, channelMask, flags);
751
Eric Laurente83b55d2014-11-14 10:06:21 -0800752 *output = getOutputForDevice(device, session, *stream,
753 samplingRate, format, channelMask,
754 flags, offloadInfo);
755 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700756 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800757 return INVALID_OPERATION;
758 }
Paul McLeanaa981192015-03-21 09:55:15 -0700759
Eric Laurente83b55d2014-11-14 10:06:21 -0800760 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700761}
762
763audio_io_handle_t AudioPolicyManager::getOutputForDevice(
764 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800765 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700766 audio_stream_type_t stream,
767 uint32_t samplingRate,
768 audio_format_t format,
769 audio_channel_mask_t channelMask,
770 audio_output_flags_t flags,
771 const audio_offload_info_t *offloadInfo)
772{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700773 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700774 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700775 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700776
Eric Laurente552edb2014-03-10 17:42:56 -0700777#ifdef AUDIO_POLICY_TEST
778 if (mCurOutput != 0) {
779 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
780 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
781
782 if (mTestOutputs[mCurOutput] == 0) {
783 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700784 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
785 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700786 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700787 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700788 outputDesc->mFlags =
789 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700790 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700791 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
792 config.sample_rate = mTestSamplingRate;
793 config.channel_mask = mTestChannels;
794 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700795 if (offloadInfo != NULL) {
796 config.offload_info = *offloadInfo;
797 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700798 status = mpClientInterface->openOutput(0,
799 &mTestOutputs[mCurOutput],
800 &config,
801 &outputDesc->mDevice,
802 String8(""),
803 &outputDesc->mLatency,
804 outputDesc->mFlags);
805 if (status == NO_ERROR) {
806 outputDesc->mSamplingRate = config.sample_rate;
807 outputDesc->mFormat = config.format;
808 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700809 AudioParameter outputCmd = AudioParameter();
810 outputCmd.addInt(String8("set_id"),mCurOutput);
811 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
812 addOutput(mTestOutputs[mCurOutput], outputDesc);
813 }
814 }
815 return mTestOutputs[mCurOutput];
816 }
817#endif //AUDIO_POLICY_TEST
818
819 // open a direct output if required by specified parameters
820 //force direct flag if offload flag is set: offloading implies a direct output stream
821 // and all common behaviors are driven by checking only the direct flag
822 // this should normally be set appropriately in the policy configuration file
823 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700824 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700825 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700826 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
827 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
828 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800829 // only allow deep buffering for music stream type
830 if (stream != AUDIO_STREAM_MUSIC) {
831 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700832 } else if (/* stream == AUDIO_STREAM_MUSIC && */
833 flags == AUDIO_OUTPUT_FLAG_NONE &&
834 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
835 // use DEEP_BUFFER as default output for music stream type
836 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800837 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700838 if (stream == AUDIO_STREAM_TTS) {
839 flags = AUDIO_OUTPUT_FLAG_TTS;
840 }
Eric Laurente552edb2014-03-10 17:42:56 -0700841
Eric Laurentb732cf52014-09-24 19:08:21 -0700842 sp<IOProfile> profile;
843
844 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700845 // and not explicitly requested
846 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800847 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700848 audio_channel_count_from_out_mask(channelMask) <= 2) {
849 goto non_direct_output;
850 }
851
Andy Hung2ddee192015-12-18 17:34:44 -0800852 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
853 // This prevents creating an offloaded track and tearing it down immediately after start
854 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700855 // FIXME: We should check the audio session here but we do not have it in this context.
856 // This may prevent offloading in rare situations where effects are left active by apps
857 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700858
Eric Laurente552edb2014-03-10 17:42:56 -0700859 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800860 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700861 profile = getProfileForDirectOutput(device,
862 samplingRate,
863 format,
864 channelMask,
865 (audio_output_flags_t)flags);
866 }
867
Eric Laurent1c333e22014-05-20 10:48:17 -0700868 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700869 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700870
871 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700872 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700873 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
874 outputDesc = desc;
875 // reuse direct output if currently open and configured with same parameters
876 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800877 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700878 (channelMask == outputDesc->mChannelMask)) {
879 outputDesc->mDirectOpenCount++;
880 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
881 return mOutputs.keyAt(i);
882 }
883 }
884 }
885 // close direct output if currently open and configured with different parameters
886 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700887 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700888 }
Eric Laurent861a6282015-05-18 15:40:16 -0700889
890 // if the selected profile is offloaded and no offload info was specified,
891 // create a default one
892 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100893 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700894 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
895 defaultOffloadInfo.sample_rate = samplingRate;
896 defaultOffloadInfo.channel_mask = channelMask;
897 defaultOffloadInfo.format = format;
898 defaultOffloadInfo.stream_type = stream;
899 defaultOffloadInfo.bit_rate = 0;
900 defaultOffloadInfo.duration_us = -1;
901 defaultOffloadInfo.has_video = true; // conservative
902 defaultOffloadInfo.is_streaming = true; // likely
903 offloadInfo = &defaultOffloadInfo;
904 }
905
Eric Laurentc75307b2015-03-17 15:29:32 -0700906 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700907 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700908 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700909 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700910 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
911 config.sample_rate = samplingRate;
912 config.channel_mask = channelMask;
913 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700914 if (offloadInfo != NULL) {
915 config.offload_info = *offloadInfo;
916 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700917 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700918 &output,
919 &config,
920 &outputDesc->mDevice,
921 String8(""),
922 &outputDesc->mLatency,
923 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700924
925 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700926 if (status != NO_ERROR ||
927 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800928 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700929 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700930 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
931 "format %d %d, channelMask %04x %04x", output, samplingRate,
932 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
933 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700934 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700935 mpClientInterface->closeOutput(output);
936 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800937 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800938 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800939 goto non_direct_output;
940 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700941 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700942 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 outputDesc->mSamplingRate = config.sample_rate;
944 outputDesc->mChannelMask = config.channel_mask;
945 outputDesc->mFormat = config.format;
946 outputDesc->mRefCount[stream] = 0;
947 outputDesc->mStopTime[stream] = 0;
948 outputDesc->mDirectOpenCount = 1;
949
Eric Laurente552edb2014-03-10 17:42:56 -0700950 audio_io_handle_t srcOutput = getOutputForEffect();
951 addOutput(output, outputDesc);
952 audio_io_handle_t dstOutput = getOutputForEffect();
953 if (dstOutput == output) {
954 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
955 }
956 mPreviousOutputs = mOutputs;
957 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700958 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700959 return output;
960 }
961
Eric Laurentb732cf52014-09-24 19:08:21 -0700962non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700963 // ignoring channel mask due to downmix capability in mixer
964
965 // open a non direct output
966
967 // for non direct outputs, only PCM is supported
968 if (audio_is_linear_pcm(format)) {
969 // get which output is suitable for the specified stream. The actual
970 // routing change will happen when startOutput() will be called
971 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
972
Eric Laurent8838a382014-09-08 16:44:28 -0700973 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
974 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
975 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700976 }
977 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
978 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
979
Paul McLeanaa981192015-03-21 09:55:15 -0700980 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700981
982 return output;
983}
984
Eric Laurente0720872014-03-11 09:30:41 -0700985audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700986 audio_output_flags_t flags,
987 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700988{
989 // select one output among several that provide a path to a particular device or set of
990 // devices (the list was previously build by getOutputsForDevice()).
991 // The priority is as follows:
992 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -0800993 // 2: the output with the bit depth the closest to the requested one
994 // 3: the primary output
995 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -0700996
997 if (outputs.size() == 0) {
998 return 0;
999 }
1000 if (outputs.size() == 1) {
1001 return outputs[0];
1002 }
1003
1004 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001005 audio_io_handle_t outputForFlags = 0;
1006 audio_io_handle_t outputForPrimary = 0;
1007 audio_io_handle_t outputForFormat = 0;
1008 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1009 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001010
1011 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001012 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001013 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001014 // if a valid format is specified, skip output if not compatible
1015 if (format != AUDIO_FORMAT_INVALID) {
1016 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001017 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001018 continue;
1019 }
1020 } else if (!audio_is_linear_pcm(format)) {
1021 continue;
1022 }
Eric Laurente6930022016-02-11 10:20:40 -08001023 if (AudioPort::isBetterFormatMatch(
1024 outputDesc->mFormat, bestFormat, format)) {
1025 outputForFormat = outputs[i];
1026 bestFormat = outputDesc->mFormat;
1027 }
Eric Laurent8838a382014-09-08 16:44:28 -07001028 }
1029
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001030 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001031 if (commonFlags >= maxCommonFlags) {
1032 if (commonFlags == maxCommonFlags) {
1033 if (AudioPort::isBetterFormatMatch(
1034 outputDesc->mFormat, bestFormatForFlags, format)) {
1035 outputForFlags = outputs[i];
1036 bestFormatForFlags = outputDesc->mFormat;
1037 }
1038 } else {
1039 outputForFlags = outputs[i];
1040 maxCommonFlags = commonFlags;
1041 bestFormatForFlags = outputDesc->mFormat;
1042 }
Eric Laurente552edb2014-03-10 17:42:56 -07001043 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1044 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001045 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001046 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001047 }
1048 }
1049 }
1050
Eric Laurente6930022016-02-11 10:20:40 -08001051 if (outputForFlags != 0) {
1052 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001053 }
Eric Laurente6930022016-02-11 10:20:40 -08001054 if (outputForFormat != 0) {
1055 return outputForFormat;
1056 }
1057 if (outputForPrimary != 0) {
1058 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001059 }
1060
1061 return outputs[0];
1062}
1063
Eric Laurente0720872014-03-11 09:30:41 -07001064status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001065 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001066 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001067{
Paul McLeanaa981192015-03-21 09:55:15 -07001068 ALOGV("startOutput() output %d, stream %d, session %d",
1069 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001070 ssize_t index = mOutputs.indexOfKey(output);
1071 if (index < 0) {
1072 ALOGW("startOutput() unknown output %d", output);
1073 return BAD_VALUE;
1074 }
1075
Eric Laurentc75307b2015-03-17 15:29:32 -07001076 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1077
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001078 // Routing?
1079 mOutputRoutes.incRouteActivity(session);
1080
Eric Laurentc75307b2015-03-17 15:29:32 -07001081 audio_devices_t newDevice;
1082 if (outputDesc->mPolicyMix != NULL) {
1083 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001084 } else if (mOutputRoutes.hasRouteChanged(session)) {
1085 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001086 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001087 } else {
1088 newDevice = AUDIO_DEVICE_NONE;
1089 }
1090
1091 uint32_t delayMs = 0;
1092
Eric Laurentc75307b2015-03-17 15:29:32 -07001093 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1094
1095 if (status != NO_ERROR) {
1096 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001097 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001098 }
1099 // Automatically enable the remote submix input when output is started on a re routing mix
1100 // of type MIX_TYPE_RECORDERS
1101 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1102 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1103 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1104 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1105 outputDesc->mPolicyMix->mRegistrationId,
1106 "remote-submix");
1107 }
1108
1109 if (delayMs != 0) {
1110 usleep(delayMs * 1000);
1111 }
1112
1113 return status;
1114}
1115
1116status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1117 audio_stream_type_t stream,
1118 audio_devices_t device,
1119 uint32_t *delayMs)
1120{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001121 // cannot start playback of STREAM_TTS if any other output is being used
1122 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001123
1124 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001125 if (stream == AUDIO_STREAM_TTS) {
1126 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001127 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001128 return INVALID_OPERATION;
1129 } else {
1130 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1131 }
1132 } else {
1133 // some playback other than beacon starts
1134 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1135 }
1136
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001137 // check active before incrementing usage count
1138 bool force = !outputDesc->isActive();
1139
Eric Laurente552edb2014-03-10 17:42:56 -07001140 // increment usage count for this stream on the requested output:
1141 // NOTE that the usage count is the same for duplicated output and hardware output which is
1142 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1143 outputDesc->changeRefCount(stream, 1);
1144
Eric Laurent493404d2015-04-21 15:07:36 -07001145 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001146 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001147 if (device == AUDIO_DEVICE_NONE) {
1148 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001149 }
Eric Laurente552edb2014-03-10 17:42:56 -07001150 routing_strategy strategy = getStrategy(stream);
1151 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001152 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1153 (beaconMuteLatency > 0);
1154 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001155 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001156 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001157 if (desc != outputDesc) {
1158 // force a device change if any other output is managed by the same hw
1159 // module and has a current device selection that differs from selected device.
1160 // In this case, the audio HAL must receive the new device selection so that it can
1161 // change the device currently selected by the other active output.
1162 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001163 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001164 force = true;
1165 }
1166 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001167 // a notification so that audio focus effect can propagate, or that a mute/unmute
1168 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001169 uint32_t latency = desc->latency();
1170 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1171 waitMs = latency;
1172 }
1173 }
1174 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001175 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001176
1177 // handle special case for sonification while in call
1178 if (isInCall()) {
1179 handleIncallSonification(stream, true, false);
1180 }
1181
1182 // apply volume rules for current stream and device if necessary
1183 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001184 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001185 outputDesc,
1186 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001187
1188 // update the outputs if starting an output with a stream that can affect notification
1189 // routing
1190 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001191
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001192 // force reevaluating accessibility routing when ringtone or alarm starts
1193 if (strategy == STRATEGY_SONIFICATION) {
1194 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1195 }
Eric Laurente552edb2014-03-10 17:42:56 -07001196 }
1197 return NO_ERROR;
1198}
1199
1200
Eric Laurente0720872014-03-11 09:30:41 -07001201status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001202 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001203 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001204{
1205 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1206 ssize_t index = mOutputs.indexOfKey(output);
1207 if (index < 0) {
1208 ALOGW("stopOutput() unknown output %d", output);
1209 return BAD_VALUE;
1210 }
1211
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001213
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 if (outputDesc->mRefCount[stream] == 1) {
1215 // Automatically disable the remote submix input when output is stopped on a
1216 // re routing mix of type MIX_TYPE_RECORDERS
1217 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1218 outputDesc->mPolicyMix != NULL &&
1219 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1220 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1221 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1222 outputDesc->mPolicyMix->mRegistrationId,
1223 "remote-submix");
1224 }
1225 }
1226
1227 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001228 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001229 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001230 int activityCount = mOutputRoutes.decRouteActivity(session);
1231 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1232
1233 if (forceDeviceUpdate) {
1234 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1235 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001236 }
1237
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001238 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001239}
1240
1241status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001242 audio_stream_type_t stream,
1243 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001244{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001245 // always handle stream stop, check which stream type is stopping
1246 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1247
Eric Laurente552edb2014-03-10 17:42:56 -07001248 // handle special case for sonification while in call
1249 if (isInCall()) {
1250 handleIncallSonification(stream, false, false);
1251 }
1252
1253 if (outputDesc->mRefCount[stream] > 0) {
1254 // decrement usage count of this stream on the output
1255 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001256
Eric Laurente552edb2014-03-10 17:42:56 -07001257 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001258 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001259 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001260 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001261 // delay the device switch by twice the latency because stopOutput() is executed when
1262 // the track stop() command is received and at that time the audio track buffer can
1263 // still contain data that needs to be drained. The latency only covers the audio HAL
1264 // and kernel buffers. Also the latency does not always include additional delay in the
1265 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001266 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001267
1268 // force restoring the device selection on other active outputs if it differs from the
1269 // one being selected for this output
1270 for (size_t i = 0; i < mOutputs.size(); i++) {
1271 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001272 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001273 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001274 desc->isActive() &&
1275 outputDesc->sharesHwModuleWith(desc) &&
1276 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001277 setOutputDevice(desc,
1278 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001279 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001280 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001281 }
1282 }
1283 // update the outputs if stopping one with a stream that can affect notification routing
1284 handleNotificationRoutingForStream(stream);
1285 }
1286 return NO_ERROR;
1287 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001288 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001289 return INVALID_OPERATION;
1290 }
1291}
1292
Eric Laurente83b55d2014-11-14 10:06:21 -08001293void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001294 audio_stream_type_t stream __unused,
1295 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001296{
1297 ALOGV("releaseOutput() %d", output);
1298 ssize_t index = mOutputs.indexOfKey(output);
1299 if (index < 0) {
1300 ALOGW("releaseOutput() releasing unknown output %d", output);
1301 return;
1302 }
1303
1304#ifdef AUDIO_POLICY_TEST
1305 int testIndex = testOutputIndex(output);
1306 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001307 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001308 if (outputDesc->isActive()) {
1309 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001310 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001311 mTestOutputs[testIndex] = 0;
1312 }
1313 return;
1314 }
1315#endif //AUDIO_POLICY_TEST
1316
Paul McLeanaa981192015-03-21 09:55:15 -07001317 // Routing
1318 mOutputRoutes.removeRoute(session);
1319
Eric Laurentc75307b2015-03-17 15:29:32 -07001320 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001321 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001322 if (desc->mDirectOpenCount <= 0) {
1323 ALOGW("releaseOutput() invalid open count %d for output %d",
1324 desc->mDirectOpenCount, output);
1325 return;
1326 }
1327 if (--desc->mDirectOpenCount == 0) {
1328 closeOutput(output);
1329 // If effects where present on the output, audioflinger moved them to the primary
1330 // output by default: move them back to the appropriate output.
1331 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001332 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001333 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1334 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001335 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001336 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001337 }
1338 }
1339}
1340
1341
Eric Laurentcaf7f482014-11-25 17:50:47 -08001342status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1343 audio_io_handle_t *input,
1344 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001345 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001346 uint32_t samplingRate,
1347 audio_format_t format,
1348 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001349 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001350 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001351 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001352{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001353 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1354 "session %d, flags %#x",
1355 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001356
Eric Laurentcaf7f482014-11-25 17:50:47 -08001357 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001358 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001359 audio_devices_t device;
1360 // handle legacy remote submix case where the address was not always specified
1361 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001362 audio_source_t inputSource = attr->source;
1363 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001364 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001365
Eric Laurentc447ded2015-01-06 08:47:05 -08001366 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1367 inputSource = AUDIO_SOURCE_MIC;
1368 }
1369 halInputSource = inputSource;
1370
Paul McLean466dc8e2015-04-17 13:15:36 -06001371 // Explicit routing?
1372 sp<DeviceDescriptor> deviceDesc;
1373 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1374 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1375 deviceDesc = mAvailableInputDevices[i];
1376 break;
1377 }
1378 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001379 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001380
Eric Laurentc447ded2015-01-06 08:47:05 -08001381 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001382 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001383 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001384 if (ret != NO_ERROR) {
1385 return ret;
1386 }
1387 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001388 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1389 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001390 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001391 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001392 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001393 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001394 return BAD_VALUE;
1395 }
Eric Laurentc722f302014-12-10 11:21:49 -08001396 if (policyMix != NULL) {
1397 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001398 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1399 // there is an external policy, but this input is attached to a mix of recorders,
1400 // meaning it receives audio injected into the framework, so the recorder doesn't
1401 // know about it and is therefore considered "legacy"
1402 *inputType = API_INPUT_LEGACY;
1403 } else {
1404 // recording a mix of players defined by an external policy, we're rerouting for
1405 // an external policy
1406 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1407 }
Eric Laurentc722f302014-12-10 11:21:49 -08001408 } else if (audio_is_remote_submix_device(device)) {
1409 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001410 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001411 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1412 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001413 } else {
1414 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001415 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001416
Eric Laurent599c7582015-12-07 18:05:55 -08001417 }
1418
1419 *input = getInputForDevice(device, address, session, uid, inputSource,
1420 samplingRate, format, channelMask, flags,
1421 policyMix);
1422 if (*input == AUDIO_IO_HANDLE_NONE) {
1423 mInputRoutes.removeRoute(session);
1424 return INVALID_OPERATION;
1425 }
1426 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1427 return NO_ERROR;
1428}
1429
1430
1431audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1432 String8 address,
1433 audio_session_t session,
1434 uid_t uid,
1435 audio_source_t inputSource,
1436 uint32_t samplingRate,
1437 audio_format_t format,
1438 audio_channel_mask_t channelMask,
1439 audio_input_flags_t flags,
1440 AudioMix *policyMix)
1441{
1442 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1443 audio_source_t halInputSource = inputSource;
1444 bool isSoundTrigger = false;
1445
1446 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1447 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1448 if (index >= 0) {
1449 input = mSoundTriggerSessions.valueFor(session);
1450 isSoundTrigger = true;
1451 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1452 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1453 } else {
1454 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001455 }
1456 }
1457
Andy Hungf129b032015-04-07 13:45:50 -07001458 // find a compatible input profile (not necessarily identical in parameters)
1459 sp<IOProfile> profile;
1460 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001461 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001462 audio_format_t profileFormat = format;
1463 audio_channel_mask_t profileChannelMask = channelMask;
1464 audio_input_flags_t profileFlags = flags;
1465 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001466 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001467 profileSamplingRate, profileFormat, profileChannelMask,
1468 profileFlags);
1469 if (profile != 0) {
1470 break; // success
1471 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1472 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1473 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001474 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1475 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001476 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001477 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001478 }
Eric Laurente552edb2014-03-10 17:42:56 -07001479 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001480 // Pick input sampling rate if not specified by client
1481 if (samplingRate == 0) {
1482 samplingRate = profileSamplingRate;
1483 }
Eric Laurente552edb2014-03-10 17:42:56 -07001484
Eric Laurent322b4d22015-04-03 15:57:54 -07001485 if (profile->getModuleHandle() == 0) {
1486 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001487 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001488 }
1489
Eric Laurent599c7582015-12-07 18:05:55 -08001490 sp<AudioSession> audioSession = new AudioSession(session,
1491 inputSource,
1492 format,
1493 samplingRate,
1494 channelMask,
1495 flags,
1496 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001497 isSoundTrigger,
1498 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001499
Eric Laurent232f26f2016-02-17 18:06:27 -08001500// TODO enable input reuse
1501#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001502 // reuse an open input if possible
1503 for (size_t i = 0; i < mInputs.size(); i++) {
1504 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001505 // reuse input if it shares the same profile and same sound trigger attribute
1506 if (profile == desc->mProfile &&
1507 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001508
1509 sp<AudioSession> as = desc->getAudioSession(session);
1510 if (as != 0) {
1511 // do not allow unmatching properties on same session
1512 if (as->matches(audioSession)) {
1513 as->changeOpenCount(1);
1514 } else {
1515 ALOGW("getInputForDevice() record with different attributes"
1516 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001517 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001518 }
1519 } else {
1520 desc->addAudioSession(session, audioSession);
1521 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001522 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1523 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001524 }
1525 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001526#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001527
Eric Laurentcf2c0212014-07-25 16:20:43 -07001528 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001529 config.sample_rate = profileSamplingRate;
1530 config.channel_mask = profileChannelMask;
1531 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001532
Eric Laurent322b4d22015-04-03 15:57:54 -07001533 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001534 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001535 &config,
1536 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001537 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001538 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001539 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001540
1541 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001542 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001543 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001544 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001545 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001546 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1547 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001548 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001549 if (input != AUDIO_IO_HANDLE_NONE) {
1550 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001551 }
Eric Laurent599c7582015-12-07 18:05:55 -08001552 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001553 }
1554
Eric Laurent1f2f2232014-06-02 12:01:23 -07001555 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001556 inputDesc->mSamplingRate = profileSamplingRate;
1557 inputDesc->mFormat = profileFormat;
1558 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001559 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001560 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001561 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001562
Eric Laurent599c7582015-12-07 18:05:55 -08001563 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001564 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001565
Eric Laurent599c7582015-12-07 18:05:55 -08001566 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001567}
1568
Eric Laurent4dc68062014-07-28 17:26:49 -07001569status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001570 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001571{
1572 ALOGV("startInput() input %d", input);
1573 ssize_t index = mInputs.indexOfKey(input);
1574 if (index < 0) {
1575 ALOGW("startInput() unknown input %d", input);
1576 return BAD_VALUE;
1577 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001578 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001579
Eric Laurent599c7582015-12-07 18:05:55 -08001580 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1581 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001582 ALOGW("startInput() unknown session %d on input %d", session, input);
1583 return BAD_VALUE;
1584 }
1585
Eric Laurent232f26f2016-02-17 18:06:27 -08001586 // virtual input devices are compatible with other input devices
1587 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001588
Eric Laurent232f26f2016-02-17 18:06:27 -08001589 // for a non-virtual input device, check if there is another (non-virtual) active input
1590 audio_io_handle_t activeInput = mInputs.getActiveInput();
1591 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001592
Eric Laurent232f26f2016-02-17 18:06:27 -08001593 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1594 // otherwise the active input continues and the new input cannot be started.
1595 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1596 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1597 !activeDesc->hasPreemptedSession(session)) {
1598 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1599 //FIXME: consider all active sessions
1600 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1601 audio_session_t activeSession = activeSessions.keyAt(0);
1602 SortedVector<audio_session_t> sessions =
1603 activeDesc->getPreemptedSessions();
1604 sessions.add(activeSession);
1605 inputDesc->setPreemptedSessions(sessions);
1606 stopInput(activeInput, activeSession);
1607 releaseInput(activeInput, activeSession);
1608 } else {
1609 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1610 return INVALID_OPERATION;
1611 }
1612 }
1613
1614 // Do not allow capture if an active voice call is using a software patch and
1615 // the call TX source device is on the same HW module.
1616 // FIXME: would be better to refine to only inputs whose profile connects to the
1617 // call TX device but this information is not in the audio patch
1618 if (mCallTxPatch != 0 &&
1619 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1620 return INVALID_OPERATION;
1621 }
1622 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001623
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001624 // Routing?
1625 mInputRoutes.incRouteActivity(session);
1626
Eric Laurent232f26f2016-02-17 18:06:27 -08001627 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1628 // if input maps to a dynamic policy with an activity listener, notify of state change
1629 if ((inputDesc->mPolicyMix != NULL)
1630 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1631 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1632 MIX_STATE_MIXING);
1633 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001634
Eric Laurent232f26f2016-02-17 18:06:27 -08001635 if (mInputs.activeInputsCount() == 0) {
1636 SoundTrigger::setCaptureState(true);
1637 }
1638 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001639
Eric Laurent232f26f2016-02-17 18:06:27 -08001640 // automatically enable the remote submix output when input is started if not
1641 // used by a policy mix of type MIX_TYPE_RECORDERS
1642 // For remote submix (a virtual device), we open only one input per capture request.
1643 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1644 String8 address = String8("");
1645 if (inputDesc->mPolicyMix == NULL) {
1646 address = String8("0");
1647 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1648 address = inputDesc->mPolicyMix->mRegistrationId;
Eric Laurentc722f302014-12-10 11:21:49 -08001649 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001650 if (address != "") {
1651 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1652 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1653 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001654 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001655 }
Eric Laurente552edb2014-03-10 17:42:56 -07001656 }
1657
Eric Laurent599c7582015-12-07 18:05:55 -08001658 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001659
Eric Laurent232f26f2016-02-17 18:06:27 -08001660 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001661 return NO_ERROR;
1662}
1663
Eric Laurent4dc68062014-07-28 17:26:49 -07001664status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1665 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001666{
1667 ALOGV("stopInput() input %d", input);
1668 ssize_t index = mInputs.indexOfKey(input);
1669 if (index < 0) {
1670 ALOGW("stopInput() unknown input %d", input);
1671 return BAD_VALUE;
1672 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001673 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001674
Eric Laurent599c7582015-12-07 18:05:55 -08001675 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001676 if (index < 0) {
1677 ALOGW("stopInput() unknown session %d on input %d", session, input);
1678 return BAD_VALUE;
1679 }
1680
Eric Laurent599c7582015-12-07 18:05:55 -08001681 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001682 ALOGW("stopInput() input %d already stopped", input);
1683 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001684 }
1685
Eric Laurent599c7582015-12-07 18:05:55 -08001686 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001687
1688 // Routing?
1689 mInputRoutes.decRouteActivity(session);
1690
Eric Laurent232f26f2016-02-17 18:06:27 -08001691 if (!inputDesc->isActive()) {
1692 // if input maps to a dynamic policy with an activity listener, notify of state change
1693 if ((inputDesc->mPolicyMix != NULL)
1694 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1695 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1696 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001697 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001698
1699 // automatically disable the remote submix output when input is stopped if not
1700 // used by a policy mix of type MIX_TYPE_RECORDERS
1701 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1702 String8 address = String8("");
1703 if (inputDesc->mPolicyMix == NULL) {
1704 address = String8("0");
1705 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1706 address = inputDesc->mPolicyMix->mRegistrationId;
1707 }
1708 if (address != "") {
1709 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1710 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1711 address, "remote-submix");
1712 }
1713 }
1714
1715 resetInputDevice(input);
1716
1717 if (mInputs.activeInputsCount() == 0) {
1718 SoundTrigger::setCaptureState(false);
1719 }
1720 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001721 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001722 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001723}
1724
Eric Laurent4dc68062014-07-28 17:26:49 -07001725void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1726 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001727{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001728
Eric Laurente552edb2014-03-10 17:42:56 -07001729 ALOGV("releaseInput() %d", input);
1730 ssize_t index = mInputs.indexOfKey(input);
1731 if (index < 0) {
1732 ALOGW("releaseInput() releasing unknown input %d", input);
1733 return;
1734 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001735
1736 // Routing
1737 mInputRoutes.removeRoute(session);
1738
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001739 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1740 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001741
Eric Laurent599c7582015-12-07 18:05:55 -08001742 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001743 if (index < 0) {
1744 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1745 return;
1746 }
Eric Laurent599c7582015-12-07 18:05:55 -08001747
1748 if (audioSession->openCount() == 0) {
1749 ALOGW("releaseInput() invalid open count %d on session %d",
1750 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001751 return;
1752 }
Eric Laurent599c7582015-12-07 18:05:55 -08001753
1754 if (audioSession->changeOpenCount(-1) == 0) {
1755 inputDesc->removeAudioSession(session);
1756 }
1757
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001758 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001759 ALOGV("releaseInput() exit > 0");
1760 return;
1761 }
1762
Eric Laurent05b90f82014-08-27 15:32:29 -07001763 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001764 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001765 ALOGV("releaseInput() exit");
1766}
1767
Eric Laurentd4692962014-05-05 18:13:44 -07001768void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001769 bool patchRemoved = false;
1770
Eric Laurentd4692962014-05-05 18:13:44 -07001771 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001772 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001773 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001774 if (patch_index >= 0) {
1775 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1776 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1777 mAudioPatches.removeItemsAt(patch_index);
1778 patchRemoved = true;
1779 }
Eric Laurentd4692962014-05-05 18:13:44 -07001780 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1781 }
1782 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001783 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001784 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001785
1786 if (patchRemoved) {
1787 mpClientInterface->onAudioPatchListUpdate();
1788 }
Eric Laurentd4692962014-05-05 18:13:44 -07001789}
1790
Eric Laurente0720872014-03-11 09:30:41 -07001791void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001792 int indexMin,
1793 int indexMax)
1794{
1795 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001796 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001797 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001798 mVolumeCurves->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001799 }
Eric Laurente552edb2014-03-10 17:42:56 -07001800}
1801
Eric Laurente0720872014-03-11 09:30:41 -07001802status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001803 int index,
1804 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001805{
1806
François Gaffied1ab2bd2015-12-02 18:20:06 +01001807 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1808 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001809 return BAD_VALUE;
1810 }
1811 if (!audio_is_output_device(device)) {
1812 return BAD_VALUE;
1813 }
1814
1815 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001816 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001817
1818 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1819 stream, device, index);
1820
1821 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1822 // clear all device specific values
1823 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001824 mVolumeCurves->clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001825 }
François Gaffied1ab2bd2015-12-02 18:20:06 +01001826 mVolumeCurves->addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001827
Eric Laurent31551f82014-10-10 18:21:56 -07001828 // update volume on all outputs whose current device is also selected by the same
1829 // strategy as the device specified by the caller
Eric Laurente04e62b2016-01-29 18:25:01 -08001830 audio_devices_t selectedDevices = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1831 // it is possible that the requested device is not selected by the strategy (e.g an explicit
1832 // audio patch is active causing getDevicesForStream() to return this device. We must make
1833 // sure that the device passed is part of the devices considered when applying volume below.
1834 selectedDevices |= device;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001835
1836 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1837 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1838 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001839 mVolumeCurves->addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001840 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1841 }
Eric Laurente04e62b2016-01-29 18:25:01 -08001842
Eric Laurente552edb2014-03-10 17:42:56 -07001843 status_t status = NO_ERROR;
1844 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001845 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1846 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurente04e62b2016-01-29 18:25:01 -08001847 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & selectedDevices) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001848 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001849 if (volStatus != NO_ERROR) {
1850 status = volStatus;
1851 }
1852 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001853 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1854 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1855 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001856 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001857 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001858 }
Eric Laurente552edb2014-03-10 17:42:56 -07001859 }
1860 return status;
1861}
1862
Eric Laurente0720872014-03-11 09:30:41 -07001863status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001864 int *index,
1865 audio_devices_t device)
1866{
1867 if (index == NULL) {
1868 return BAD_VALUE;
1869 }
1870 if (!audio_is_output_device(device)) {
1871 return BAD_VALUE;
1872 }
1873 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1874 // the strategy the stream belongs to.
1875 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1876 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1877 }
François Gaffiedfd74092015-03-19 12:10:59 +01001878 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001879
François Gaffied1ab2bd2015-12-02 18:20:06 +01001880 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001881 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1882 return NO_ERROR;
1883}
1884
Eric Laurente0720872014-03-11 09:30:41 -07001885audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001886 const SortedVector<audio_io_handle_t>& outputs)
1887{
1888 // select one output among several suitable for global effects.
1889 // The priority is as follows:
1890 // 1: An offloaded output. If the effect ends up not being offloadable,
1891 // AudioFlinger will invalidate the track and the offloaded output
1892 // will be closed causing the effect to be moved to a PCM output.
1893 // 2: A deep buffer output
1894 // 3: the first output in the list
1895
1896 if (outputs.size() == 0) {
1897 return 0;
1898 }
1899
1900 audio_io_handle_t outputOffloaded = 0;
1901 audio_io_handle_t outputDeepBuffer = 0;
1902
1903 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001904 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001905 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001906 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1907 outputOffloaded = outputs[i];
1908 }
1909 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1910 outputDeepBuffer = outputs[i];
1911 }
1912 }
1913
1914 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1915 outputOffloaded, outputDeepBuffer);
1916 if (outputOffloaded != 0) {
1917 return outputOffloaded;
1918 }
1919 if (outputDeepBuffer != 0) {
1920 return outputDeepBuffer;
1921 }
1922
1923 return outputs[0];
1924}
1925
Eric Laurente0720872014-03-11 09:30:41 -07001926audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001927{
1928 // apply simple rule where global effects are attached to the same output as MUSIC streams
1929
Eric Laurent3b73df72014-03-11 09:06:29 -07001930 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001931 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1932 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1933
1934 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1935 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1936 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1937
1938 return output;
1939}
1940
Eric Laurente0720872014-03-11 09:30:41 -07001941status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001942 audio_io_handle_t io,
1943 uint32_t strategy,
1944 int session,
1945 int id)
1946{
1947 ssize_t index = mOutputs.indexOfKey(io);
1948 if (index < 0) {
1949 index = mInputs.indexOfKey(io);
1950 if (index < 0) {
1951 ALOGW("registerEffect() unknown io %d", io);
1952 return INVALID_OPERATION;
1953 }
1954 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001955 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001956}
1957
Eric Laurentc75307b2015-03-17 15:29:32 -07001958bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1959{
1960 return mOutputs.isStreamActive(stream, inPastMs);
1961}
1962
1963bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1964{
1965 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1966}
1967
Eric Laurente0720872014-03-11 09:30:41 -07001968bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001969{
1970 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001971 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001972 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001973 return true;
1974 }
1975 }
1976 return false;
1977}
1978
Eric Laurent275e8e92014-11-30 15:14:47 -08001979// Register a list of custom mixes with their attributes and format.
1980// When a mix is registered, corresponding input and output profiles are
1981// added to the remote submix hw module. The profile contains only the
1982// parameters (sampling rate, format...) specified by the mix.
1983// The corresponding input remote submix device is also connected.
1984//
1985// When a remote submix device is connected, the address is checked to select the
1986// appropriate profile and the corresponding input or output stream is opened.
1987//
1988// When capture starts, getInputForAttr() will:
1989// - 1 look for a mix matching the address passed in attribtutes tags if any
1990// - 2 if none found, getDeviceForInputSource() will:
1991// - 2.1 look for a mix matching the attributes source
1992// - 2.2 if none found, default to device selection by policy rules
1993// At this time, the corresponding output remote submix device is also connected
1994// and active playback use cases can be transferred to this mix if needed when reconnecting
1995// after AudioTracks are invalidated
1996//
1997// When playback starts, getOutputForAttr() will:
1998// - 1 look for a mix matching the address passed in attribtutes tags if any
1999// - 2 if none found, look for a mix matching the attributes usage
2000// - 3 if none found, default to device and output selection by policy rules.
2001
2002status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2003{
2004 sp<HwModule> module;
2005 for (size_t i = 0; i < mHwModules.size(); i++) {
2006 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2007 mHwModules[i]->mHandle != 0) {
2008 module = mHwModules[i];
2009 break;
2010 }
2011 }
2012
2013 if (module == 0) {
2014 return INVALID_OPERATION;
2015 }
2016
Eric Laurent7b279bb2015-12-14 10:18:23 -08002017 ALOGV("registerPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002018
2019 for (size_t i = 0; i < mixes.size(); i++) {
2020 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002021
2022 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002023 continue;
2024 }
2025 audio_config_t outputConfig = mixes[i].mFormat;
2026 audio_config_t inputConfig = mixes[i].mFormat;
2027 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2028 // stereo and let audio flinger do the channel conversion if needed.
2029 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2030 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2031 module->addOutputProfile(address, &outputConfig,
2032 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2033 module->addInputProfile(address, &inputConfig,
2034 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002035
Eric Laurentc722f302014-12-10 11:21:49 -08002036 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002037 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002038 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002039 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002040 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002041 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002042 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002043 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002044 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002045 }
2046 return NO_ERROR;
2047}
2048
2049status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2050{
2051 sp<HwModule> module;
2052 for (size_t i = 0; i < mHwModules.size(); i++) {
2053 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2054 mHwModules[i]->mHandle != 0) {
2055 module = mHwModules[i];
2056 break;
2057 }
2058 }
2059
2060 if (module == 0) {
2061 return INVALID_OPERATION;
2062 }
2063
Eric Laurent7b279bb2015-12-14 10:18:23 -08002064 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002065
2066 for (size_t i = 0; i < mixes.size(); i++) {
2067 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002068
2069 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002070 continue;
2071 }
2072
Eric Laurentc722f302014-12-10 11:21:49 -08002073 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2074 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2075 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002076 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002077 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002078 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002079 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002080
2081 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2082 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2083 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002084 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002085 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002086 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002087 }
2088 module->removeOutputProfile(address);
2089 module->removeInputProfile(address);
2090 }
2091 return NO_ERROR;
2092}
2093
Eric Laurente552edb2014-03-10 17:42:56 -07002094
Eric Laurente0720872014-03-11 09:30:41 -07002095status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002096{
2097 const size_t SIZE = 256;
2098 char buffer[SIZE];
2099 String8 result;
2100
2101 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2102 result.append(buffer);
2103
Eric Laurent87ffa392015-05-22 10:32:38 -07002104 snprintf(buffer, SIZE, " Primary Output: %d\n",
2105 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002106 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002107 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002108 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002109 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002110 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002111 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002112 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002113 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002114 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002115 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002116 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002117 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002118 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002119 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002120 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002121 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002122 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002123 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2124 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2125 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002126 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2127 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002128 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2129 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002130
François Gaffie2110e042015-03-24 08:41:51 +01002131 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002132
François Gaffie112b0af2015-11-19 16:13:25 +01002133 mAvailableOutputDevices.dump(fd, String8("Available output"));
2134 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002135 mHwModules.dump(fd);
2136 mOutputs.dump(fd);
2137 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002138 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002139 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002140 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002141
2142 return NO_ERROR;
2143}
2144
2145// This function checks for the parameters which can be offloaded.
2146// This can be enhanced depending on the capability of the DSP and policy
2147// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002148bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002149{
2150 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002151 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002152 offloadInfo.sample_rate, offloadInfo.channel_mask,
2153 offloadInfo.format,
2154 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2155 offloadInfo.has_video);
2156
Andy Hung2ddee192015-12-18 17:34:44 -08002157 if (mMasterMono) {
2158 return false; // no offloading if mono is set.
2159 }
2160
Eric Laurente552edb2014-03-10 17:42:56 -07002161 // Check if offload has been disabled
2162 char propValue[PROPERTY_VALUE_MAX];
2163 if (property_get("audio.offload.disable", propValue, "0")) {
2164 if (atoi(propValue) != 0) {
2165 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2166 return false;
2167 }
2168 }
2169
2170 // Check if stream type is music, then only allow offload as of now.
2171 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2172 {
2173 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2174 return false;
2175 }
2176
2177 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002178 const bool allowOffloadWithVideo =
2179 property_get_bool("audio.offload.video", false /* default_value */);
2180 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002181 ALOGV("isOffloadSupported: has_video == true, returning false");
2182 return false;
2183 }
2184
2185 //If duration is less than minimum value defined in property, return false
2186 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2187 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2188 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2189 return false;
2190 }
2191 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2192 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2193 return false;
2194 }
2195
2196 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2197 // creating an offloaded track and tearing it down immediately after start when audioflinger
2198 // detects there is an active non offloadable effect.
2199 // FIXME: We should check the audio session here but we do not have it in this context.
2200 // This may prevent offloading in rare situations where effects are left active by apps
2201 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002202 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002203 return false;
2204 }
2205
2206 // See if there is a profile to support this.
2207 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002208 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002209 offloadInfo.sample_rate,
2210 offloadInfo.format,
2211 offloadInfo.channel_mask,
2212 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002213 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2214 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002215}
2216
Eric Laurent6a94d692014-05-20 11:18:06 -07002217status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2218 audio_port_type_t type,
2219 unsigned int *num_ports,
2220 struct audio_port *ports,
2221 unsigned int *generation)
2222{
2223 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2224 generation == NULL) {
2225 return BAD_VALUE;
2226 }
2227 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2228 if (ports == NULL) {
2229 *num_ports = 0;
2230 }
2231
2232 size_t portsWritten = 0;
2233 size_t portsMax = *num_ports;
2234 *num_ports = 0;
2235 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2236 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2237 for (size_t i = 0;
2238 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2239 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2240 }
2241 *num_ports += mAvailableOutputDevices.size();
2242 }
2243 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2244 for (size_t i = 0;
2245 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2246 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2247 }
2248 *num_ports += mAvailableInputDevices.size();
2249 }
2250 }
2251 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2252 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2253 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2254 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2255 }
2256 *num_ports += mInputs.size();
2257 }
2258 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002259 size_t numOutputs = 0;
2260 for (size_t i = 0; i < mOutputs.size(); i++) {
2261 if (!mOutputs[i]->isDuplicated()) {
2262 numOutputs++;
2263 if (portsWritten < portsMax) {
2264 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2265 }
2266 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002267 }
Eric Laurent84c70242014-06-23 08:46:27 -07002268 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002269 }
2270 }
2271 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002272 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002273 return NO_ERROR;
2274}
2275
2276status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2277{
2278 return NO_ERROR;
2279}
2280
Eric Laurent6a94d692014-05-20 11:18:06 -07002281status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2282 audio_patch_handle_t *handle,
2283 uid_t uid)
2284{
2285 ALOGV("createAudioPatch()");
2286
2287 if (handle == NULL || patch == NULL) {
2288 return BAD_VALUE;
2289 }
2290 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2291
Eric Laurent874c42872014-08-08 15:13:39 -07002292 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2293 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2294 return BAD_VALUE;
2295 }
2296 // only one source per audio patch supported for now
2297 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002298 return INVALID_OPERATION;
2299 }
Eric Laurent874c42872014-08-08 15:13:39 -07002300
2301 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002302 return INVALID_OPERATION;
2303 }
Eric Laurent874c42872014-08-08 15:13:39 -07002304 for (size_t i = 0; i < patch->num_sinks; i++) {
2305 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2306 return INVALID_OPERATION;
2307 }
2308 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002309
2310 sp<AudioPatch> patchDesc;
2311 ssize_t index = mAudioPatches.indexOfKey(*handle);
2312
Eric Laurent6a94d692014-05-20 11:18:06 -07002313 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2314 patch->sources[0].role,
2315 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002316#if LOG_NDEBUG == 0
2317 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002318 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002319 patch->sinks[i].role,
2320 patch->sinks[i].type);
2321 }
2322#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002323
2324 if (index >= 0) {
2325 patchDesc = mAudioPatches.valueAt(index);
2326 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2327 mUidCached, patchDesc->mUid, uid);
2328 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2329 return INVALID_OPERATION;
2330 }
2331 } else {
2332 *handle = 0;
2333 }
2334
2335 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002336 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002337 if (outputDesc == NULL) {
2338 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2339 return BAD_VALUE;
2340 }
Eric Laurent84c70242014-06-23 08:46:27 -07002341 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2342 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002343 if (patchDesc != 0) {
2344 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2345 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2346 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2347 return BAD_VALUE;
2348 }
2349 }
Eric Laurent874c42872014-08-08 15:13:39 -07002350 DeviceVector devices;
2351 for (size_t i = 0; i < patch->num_sinks; i++) {
2352 // Only support mix to devices connection
2353 // TODO add support for mix to mix connection
2354 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2355 ALOGV("createAudioPatch() source mix but sink is not a device");
2356 return INVALID_OPERATION;
2357 }
2358 sp<DeviceDescriptor> devDesc =
2359 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2360 if (devDesc == 0) {
2361 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2362 return BAD_VALUE;
2363 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002364
François Gaffie53615e22015-03-19 09:24:12 +01002365 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002366 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002367 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002368 NULL, // updatedSamplingRate
2369 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002370 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002371 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002372 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002373 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002374 ALOGV("createAudioPatch() profile not supported for device %08x",
2375 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002376 return INVALID_OPERATION;
2377 }
2378 devices.add(devDesc);
2379 }
2380 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002381 return INVALID_OPERATION;
2382 }
Eric Laurent874c42872014-08-08 15:13:39 -07002383
Eric Laurent6a94d692014-05-20 11:18:06 -07002384 // TODO: reconfigure output format and channels here
2385 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002386 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002387 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002388 index = mAudioPatches.indexOfKey(*handle);
2389 if (index >= 0) {
2390 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2391 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2392 }
2393 patchDesc = mAudioPatches.valueAt(index);
2394 patchDesc->mUid = uid;
2395 ALOGV("createAudioPatch() success");
2396 } else {
2397 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2398 return INVALID_OPERATION;
2399 }
2400 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2401 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2402 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002403 // only one sink supported when connecting an input device to a mix
2404 if (patch->num_sinks > 1) {
2405 return INVALID_OPERATION;
2406 }
François Gaffie53615e22015-03-19 09:24:12 +01002407 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002408 if (inputDesc == NULL) {
2409 return BAD_VALUE;
2410 }
2411 if (patchDesc != 0) {
2412 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2413 return BAD_VALUE;
2414 }
2415 }
2416 sp<DeviceDescriptor> devDesc =
2417 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2418 if (devDesc == 0) {
2419 return BAD_VALUE;
2420 }
2421
François Gaffie53615e22015-03-19 09:24:12 +01002422 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002423 devDesc->mAddress,
2424 patch->sinks[0].sample_rate,
2425 NULL, /*updatedSampleRate*/
2426 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002427 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002428 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002429 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002430 // FIXME for the parameter type,
2431 // and the NONE
2432 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002433 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 return INVALID_OPERATION;
2435 }
2436 // TODO: reconfigure output format and channels here
2437 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002438 devDesc->type(), inputDesc->mIoHandle);
2439 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002440 index = mAudioPatches.indexOfKey(*handle);
2441 if (index >= 0) {
2442 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2443 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2444 }
2445 patchDesc = mAudioPatches.valueAt(index);
2446 patchDesc->mUid = uid;
2447 ALOGV("createAudioPatch() success");
2448 } else {
2449 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2450 return INVALID_OPERATION;
2451 }
2452 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2453 // device to device connection
2454 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002455 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002456 return BAD_VALUE;
2457 }
2458 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002459 sp<DeviceDescriptor> srcDeviceDesc =
2460 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002461 if (srcDeviceDesc == 0) {
2462 return BAD_VALUE;
2463 }
Eric Laurent874c42872014-08-08 15:13:39 -07002464
Eric Laurent6a94d692014-05-20 11:18:06 -07002465 //update source and sink with our own data as the data passed in the patch may
2466 // be incomplete.
2467 struct audio_patch newPatch = *patch;
2468 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002469
Eric Laurent874c42872014-08-08 15:13:39 -07002470 for (size_t i = 0; i < patch->num_sinks; i++) {
2471 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2472 ALOGV("createAudioPatch() source device but one sink is not a device");
2473 return INVALID_OPERATION;
2474 }
2475
2476 sp<DeviceDescriptor> sinkDeviceDesc =
2477 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2478 if (sinkDeviceDesc == 0) {
2479 return BAD_VALUE;
2480 }
2481 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2482
Eric Laurent3bcf8592015-04-03 12:13:24 -07002483 // create a software bridge in PatchPanel if:
2484 // - source and sink devices are on differnt HW modules OR
2485 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002486 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002487 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002488 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002489 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002490 return INVALID_OPERATION;
2491 }
Eric Laurent874c42872014-08-08 15:13:39 -07002492 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002493 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002494 // if the sink device is reachable via an opened output stream, request to go via
2495 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002496 audio_io_handle_t output = selectOutput(outputs,
2497 AUDIO_OUTPUT_FLAG_NONE,
2498 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002499 if (output != AUDIO_IO_HANDLE_NONE) {
2500 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2501 if (outputDesc->isDuplicated()) {
2502 return INVALID_OPERATION;
2503 }
2504 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002505 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002506 newPatch.num_sources = 2;
2507 }
Eric Laurent83b88082014-06-20 18:31:16 -07002508 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002509 }
2510 // TODO: check from routing capabilities in config file and other conflicting patches
2511
2512 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2513 if (index >= 0) {
2514 afPatchHandle = patchDesc->mAfPatchHandle;
2515 }
2516
2517 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2518 &afPatchHandle,
2519 0);
2520 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2521 status, afPatchHandle);
2522 if (status == NO_ERROR) {
2523 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002524 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002525 addAudioPatch(patchDesc->mHandle, patchDesc);
2526 } else {
2527 patchDesc->mPatch = newPatch;
2528 }
2529 patchDesc->mAfPatchHandle = afPatchHandle;
2530 *handle = patchDesc->mHandle;
2531 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002532 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002533 } else {
2534 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2535 status);
2536 return INVALID_OPERATION;
2537 }
2538 } else {
2539 return BAD_VALUE;
2540 }
2541 } else {
2542 return BAD_VALUE;
2543 }
2544 return NO_ERROR;
2545}
2546
2547status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2548 uid_t uid)
2549{
2550 ALOGV("releaseAudioPatch() patch %d", handle);
2551
2552 ssize_t index = mAudioPatches.indexOfKey(handle);
2553
2554 if (index < 0) {
2555 return BAD_VALUE;
2556 }
2557 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2558 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2559 mUidCached, patchDesc->mUid, uid);
2560 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2561 return INVALID_OPERATION;
2562 }
2563
2564 struct audio_patch *patch = &patchDesc->mPatch;
2565 patchDesc->mUid = mUidCached;
2566 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002567 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002568 if (outputDesc == NULL) {
2569 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2570 return BAD_VALUE;
2571 }
2572
Eric Laurentc75307b2015-03-17 15:29:32 -07002573 setOutputDevice(outputDesc,
2574 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002575 true,
2576 0,
2577 NULL);
2578 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2579 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002580 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002581 if (inputDesc == NULL) {
2582 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2583 return BAD_VALUE;
2584 }
2585 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002586 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002587 true,
2588 NULL);
2589 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2590 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2591 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2592 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2593 status, patchDesc->mAfPatchHandle);
2594 removeAudioPatch(patchDesc->mHandle);
2595 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002596 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002597 } else {
2598 return BAD_VALUE;
2599 }
2600 } else {
2601 return BAD_VALUE;
2602 }
2603 return NO_ERROR;
2604}
2605
2606status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2607 struct audio_patch *patches,
2608 unsigned int *generation)
2609{
François Gaffie53615e22015-03-19 09:24:12 +01002610 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002611 return BAD_VALUE;
2612 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002613 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002614 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002615}
2616
Eric Laurente1715a42014-05-20 11:30:42 -07002617status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002618{
Eric Laurente1715a42014-05-20 11:30:42 -07002619 ALOGV("setAudioPortConfig()");
2620
2621 if (config == NULL) {
2622 return BAD_VALUE;
2623 }
2624 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2625 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002626 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2627 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002628 }
2629
Eric Laurenta121f902014-06-03 13:32:54 -07002630 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002631 if (config->type == AUDIO_PORT_TYPE_MIX) {
2632 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002633 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002634 if (outputDesc == NULL) {
2635 return BAD_VALUE;
2636 }
Eric Laurent84c70242014-06-23 08:46:27 -07002637 ALOG_ASSERT(!outputDesc->isDuplicated(),
2638 "setAudioPortConfig() called on duplicated output %d",
2639 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002640 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002641 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002642 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002643 if (inputDesc == NULL) {
2644 return BAD_VALUE;
2645 }
Eric Laurenta121f902014-06-03 13:32:54 -07002646 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002647 } else {
2648 return BAD_VALUE;
2649 }
2650 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2651 sp<DeviceDescriptor> deviceDesc;
2652 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2653 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2654 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2655 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2656 } else {
2657 return BAD_VALUE;
2658 }
2659 if (deviceDesc == NULL) {
2660 return BAD_VALUE;
2661 }
Eric Laurenta121f902014-06-03 13:32:54 -07002662 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002663 } else {
2664 return BAD_VALUE;
2665 }
2666
Eric Laurenta121f902014-06-03 13:32:54 -07002667 struct audio_port_config backupConfig;
2668 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2669 if (status == NO_ERROR) {
2670 struct audio_port_config newConfig;
2671 audioPortConfig->toAudioPortConfig(&newConfig, config);
2672 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002673 }
Eric Laurenta121f902014-06-03 13:32:54 -07002674 if (status != NO_ERROR) {
2675 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002676 }
Eric Laurente1715a42014-05-20 11:30:42 -07002677
2678 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002679}
2680
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002681void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2682{
Eric Laurentd60560a2015-04-10 11:31:20 -07002683 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002684 clearAudioPatches(uid);
2685 clearSessionRoutes(uid);
2686}
2687
Eric Laurent6a94d692014-05-20 11:18:06 -07002688void AudioPolicyManager::clearAudioPatches(uid_t uid)
2689{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002690 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002691 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2692 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002693 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002694 }
2695 }
2696}
2697
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002698void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2699 audio_io_handle_t ouptutToSkip)
2700{
2701 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2702 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2703 for (size_t j = 0; j < mOutputs.size(); j++) {
2704 if (mOutputs.keyAt(j) == ouptutToSkip) {
2705 continue;
2706 }
2707 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2708 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2709 continue;
2710 }
2711 // If the default device for this strategy is on another output mix,
2712 // invalidate all tracks in this strategy to force re connection.
2713 // Otherwise select new device on the output mix.
2714 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2715 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2716 if (stream == AUDIO_STREAM_PATCH) {
2717 continue;
2718 }
2719 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2720 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2721 }
2722 }
2723 } else {
2724 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2725 setOutputDevice(outputDesc, newDevice, false);
2726 }
2727 }
2728}
2729
2730void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2731{
2732 // remove output routes associated with this uid
2733 SortedVector<routing_strategy> affectedStrategies;
2734 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2735 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2736 if (route->mUid == uid) {
2737 mOutputRoutes.removeItemsAt(i);
2738 if (route->mDeviceDescriptor != 0) {
2739 affectedStrategies.add(getStrategy(route->mStreamType));
2740 }
2741 }
2742 }
2743 // reroute outputs if necessary
2744 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2745 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2746 }
2747
2748 // remove input routes associated with this uid
2749 SortedVector<audio_source_t> affectedSources;
2750 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2751 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2752 if (route->mUid == uid) {
2753 mInputRoutes.removeItemsAt(i);
2754 if (route->mDeviceDescriptor != 0) {
2755 affectedSources.add(route->mSource);
2756 }
2757 }
2758 }
2759 // reroute inputs if necessary
2760 SortedVector<audio_io_handle_t> inputsToClose;
2761 for (size_t i = 0; i < mInputs.size(); i++) {
2762 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002763 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002764 inputsToClose.add(inputDesc->mIoHandle);
2765 }
2766 }
2767 for (size_t i = 0; i < inputsToClose.size(); i++) {
2768 closeInput(inputsToClose[i]);
2769 }
2770}
2771
Eric Laurentd60560a2015-04-10 11:31:20 -07002772void AudioPolicyManager::clearAudioSources(uid_t uid)
2773{
2774 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2775 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2776 if (sourceDesc->mUid == uid) {
2777 stopAudioSource(mAudioSources.keyAt(i));
2778 }
2779 }
2780}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002781
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002782status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2783 audio_io_handle_t *ioHandle,
2784 audio_devices_t *device)
2785{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002786 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2787 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002788 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002789
François Gaffiedf372692015-03-19 10:43:27 +01002790 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002791}
2792
Eric Laurentd60560a2015-04-10 11:31:20 -07002793status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2794 const audio_attributes_t *attributes,
2795 audio_io_handle_t *handle,
2796 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002797{
Eric Laurentd60560a2015-04-10 11:31:20 -07002798 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2799 if (source == NULL || attributes == NULL || handle == NULL) {
2800 return BAD_VALUE;
2801 }
2802
2803 *handle = AUDIO_IO_HANDLE_NONE;
2804
2805 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2806 source->type != AUDIO_PORT_TYPE_DEVICE) {
2807 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2808 return INVALID_OPERATION;
2809 }
2810
2811 sp<DeviceDescriptor> srcDeviceDesc =
2812 mAvailableInputDevices.getDevice(source->ext.device.type,
2813 String8(source->ext.device.address));
2814 if (srcDeviceDesc == 0) {
2815 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2816 return BAD_VALUE;
2817 }
2818 sp<AudioSourceDescriptor> sourceDesc =
2819 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2820
2821 struct audio_patch dummyPatch;
2822 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2823 sourceDesc->mPatchDesc = patchDesc;
2824
2825 status_t status = connectAudioSource(sourceDesc);
2826 if (status == NO_ERROR) {
2827 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2828 *handle = sourceDesc->getHandle();
2829 }
2830 return status;
2831}
2832
2833status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2834{
2835 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2836
2837 // make sure we only have one patch per source.
2838 disconnectAudioSource(sourceDesc);
2839
2840 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
2841 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
2842 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2843
2844 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2845 sp<DeviceDescriptor> sinkDeviceDesc =
2846 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2847
2848 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2849 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2850
2851 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2852 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002853 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002854 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2855 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2856 // create patch between src device and output device
2857 // create Hwoutput and add to mHwOutputs
2858 } else {
2859 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2860 audio_io_handle_t output =
2861 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2862 if (output == AUDIO_IO_HANDLE_NONE) {
2863 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2864 return INVALID_OPERATION;
2865 }
2866 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2867 if (outputDesc->isDuplicated()) {
2868 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2869 return INVALID_OPERATION;
2870 }
2871 // create a special patch with no sink and two sources:
2872 // - the second source indicates to PatchPanel through which output mix this patch should
2873 // be connected as well as the stream type for volume control
2874 // - the sink is defined by whatever output device is currently selected for the output
2875 // though which this patch is routed.
2876 patch->num_sinks = 0;
2877 patch->num_sources = 2;
2878 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
2879 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
2880 patch->sources[1].ext.mix.usecase.stream = stream;
2881 status_t status = mpClientInterface->createAudioPatch(patch,
2882 &afPatchHandle,
2883 0);
2884 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
2885 status, afPatchHandle);
2886 if (status != NO_ERROR) {
2887 ALOGW("%s patch panel could not connect device patch, error %d",
2888 __FUNCTION__, status);
2889 return INVALID_OPERATION;
2890 }
2891 uint32_t delayMs = 0;
2892 status = startSource(outputDesc, stream, sinkDevice, &delayMs);
2893
2894 if (status != NO_ERROR) {
2895 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
2896 return status;
2897 }
2898 sourceDesc->mSwOutput = outputDesc;
2899 if (delayMs != 0) {
2900 usleep(delayMs * 1000);
2901 }
2902 }
2903
2904 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
2905 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
2906
2907 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07002908}
2909
Eric Laurent493404d2015-04-21 15:07:36 -07002910status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002911{
Eric Laurentd60560a2015-04-10 11:31:20 -07002912 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
2913 ALOGV("%s handle %d", __FUNCTION__, handle);
2914 if (sourceDesc == 0) {
2915 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
2916 return BAD_VALUE;
2917 }
2918 status_t status = disconnectAudioSource(sourceDesc);
2919
2920 mAudioSources.removeItem(handle);
2921 return status;
2922}
2923
Andy Hung2ddee192015-12-18 17:34:44 -08002924status_t AudioPolicyManager::setMasterMono(bool mono)
2925{
2926 if (mMasterMono == mono) {
2927 return NO_ERROR;
2928 }
2929 mMasterMono = mono;
2930 // if enabling mono we close all offloaded devices, which will invalidate the
2931 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
2932 // for recreating the new AudioTrack as non-offloaded PCM.
2933 //
2934 // If disabling mono, we leave all tracks as is: we don't know which clients
2935 // and tracks are able to be recreated as offloaded. The next "song" should
2936 // play back offloaded.
2937 if (mMasterMono) {
2938 Vector<audio_io_handle_t> offloaded;
2939 for (size_t i = 0; i < mOutputs.size(); ++i) {
2940 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2941 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2942 offloaded.push(desc->mIoHandle);
2943 }
2944 }
2945 for (size_t i = 0; i < offloaded.size(); ++i) {
2946 closeOutput(offloaded[i]);
2947 }
2948 }
2949 // update master mono for all remaining outputs
2950 for (size_t i = 0; i < mOutputs.size(); ++i) {
2951 updateMono(mOutputs.keyAt(i));
2952 }
2953 return NO_ERROR;
2954}
2955
2956status_t AudioPolicyManager::getMasterMono(bool *mono)
2957{
2958 *mono = mMasterMono;
2959 return NO_ERROR;
2960}
2961
Eric Laurentd60560a2015-04-10 11:31:20 -07002962status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2963{
2964 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2965
2966 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
2967 if (patchDesc == 0) {
2968 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
2969 sourceDesc->mPatchDesc->mHandle);
2970 return BAD_VALUE;
2971 }
2972 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
2973
2974 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
2975 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
2976 if (swOutputDesc != 0) {
2977 stopSource(swOutputDesc, stream, false);
2978 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2979 } else {
2980 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
2981 if (hwOutputDesc != 0) {
2982 // release patch between src device and output device
2983 // close Hwoutput and remove from mHwOutputs
2984 } else {
2985 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
2986 }
2987 }
2988 return NO_ERROR;
2989}
2990
2991sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
2992 audio_io_handle_t output, routing_strategy strategy)
2993{
2994 sp<AudioSourceDescriptor> source;
2995 for (size_t i = 0; i < mAudioSources.size(); i++) {
2996 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2997 routing_strategy sourceStrategy =
2998 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
2999 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3000 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3001 source = sourceDesc;
3002 break;
3003 }
3004 }
3005 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003006}
3007
Eric Laurente552edb2014-03-10 17:42:56 -07003008// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003009// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003010// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003011uint32_t AudioPolicyManager::nextAudioPortGeneration()
3012{
3013 return android_atomic_inc(&mAudioPortGeneration);
3014}
3015
Eric Laurente0720872014-03-11 09:30:41 -07003016AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003017 :
3018#ifdef AUDIO_POLICY_TEST
3019 Thread(false),
3020#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003021 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003022 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003023 mAudioPortGeneration(1),
3024 mBeaconMuteRefCount(0),
3025 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003026 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003027 mTtsOutputAvailable(false),
3028 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003029{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003030 mUidCached = getuid();
3031 mpClientInterface = clientInterface;
3032
3033 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3034 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3035 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3036 bool speakerDrcEnabled = false;
3037
3038#ifdef USE_XML_AUDIO_POLICY_CONF
3039 mVolumeCurves = new VolumeCurvesCollection();
3040 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3041 mDefaultOutputDevice, speakerDrcEnabled,
3042 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3043 PolicySerializer serializer;
3044 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3045#else
3046 mVolumeCurves = new StreamDescriptorCollection();
3047 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3048 mDefaultOutputDevice, speakerDrcEnabled);
3049 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3050 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3051#endif
3052 ALOGE("could not load audio policy configuration file, setting defaults");
3053 config.setDefault();
3054 }
3055 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3056 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3057
3058 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003059 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3060 if (!engineInstance) {
3061 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3062 return;
3063 }
3064 // Retrieve the Policy Manager Interface
3065 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3066 if (mEngine == NULL) {
3067 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3068 return;
3069 }
3070 mEngine->setObserver(this);
3071 status_t status = mEngine->initCheck();
3072 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3073
Eric Laurent3a4311c2014-03-17 12:00:47 -07003074 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003075 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003076 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3077 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003078 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003079 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003080 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003081 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003082 continue;
3083 }
3084 // open all output streams needed to access attached devices
3085 // except for direct output streams that are only opened when they are actually
3086 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003087 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003088 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3089 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003090 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003091
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003092 if (!outProfile->hasSupportedDevices()) {
3093 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003094 continue;
3095 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003096 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003097 mTtsOutputAvailable = true;
3098 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003099
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003100 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003101 continue;
3102 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003103 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003104 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3105 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003106 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003107 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003108 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003109 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003110 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003111 if ((profileType & outputDeviceTypes) == 0) {
3112 continue;
3113 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003114 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3115 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07003116
3117 outputDesc->mDevice = profileType;
3118 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3119 config.sample_rate = outputDesc->mSamplingRate;
3120 config.channel_mask = outputDesc->mChannelMask;
3121 config.format = outputDesc->mFormat;
3122 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003123 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003124 &output,
3125 &config,
3126 &outputDesc->mDevice,
3127 String8(""),
3128 &outputDesc->mLatency,
3129 outputDesc->mFlags);
3130
3131 if (status != NO_ERROR) {
3132 ALOGW("Cannot open output stream for device %08x on hw module %s",
3133 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003134 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003135 } else {
3136 outputDesc->mSamplingRate = config.sample_rate;
3137 outputDesc->mChannelMask = config.channel_mask;
3138 outputDesc->mFormat = config.format;
3139
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003140 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3141 for (size_t k = 0; k < supportedDevices.size(); k++) {
3142 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003143 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003144 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3145 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003146 }
3147 }
3148 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003149 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003150 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003151 }
3152 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003153 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003154 outputDesc->mDevice,
3155 true);
3156 }
Eric Laurente552edb2014-03-10 17:42:56 -07003157 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003158 // open input streams needed to access attached devices to validate
3159 // mAvailableInputDevices list
3160 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3161 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003162 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003163
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003164 if (!inProfile->hasSupportedDevices()) {
3165 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003166 continue;
3167 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003168 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003169 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003170 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3171
Eric Laurentd78f1532014-09-16 16:38:20 -07003172 if ((profileType & inputDeviceTypes) == 0) {
3173 continue;
3174 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003175 sp<AudioInputDescriptor> inputDesc =
3176 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003177
Eric Laurentd78f1532014-09-16 16:38:20 -07003178 inputDesc->mDevice = profileType;
3179
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003180 // find the address
3181 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3182 // the inputs vector must be of size 1, but we don't want to crash here
3183 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3184 : String8("");
3185 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3186 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3187
Eric Laurentd78f1532014-09-16 16:38:20 -07003188 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3189 config.sample_rate = inputDesc->mSamplingRate;
3190 config.channel_mask = inputDesc->mChannelMask;
3191 config.format = inputDesc->mFormat;
3192 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003193 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003194 &input,
3195 &config,
3196 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003197 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003198 AUDIO_SOURCE_MIC,
3199 AUDIO_INPUT_FLAG_NONE);
3200
3201 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003202 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3203 for (size_t k = 0; k < supportedDevices.size(); k++) {
3204 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003205 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003206 if (index >= 0) {
3207 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3208 if (!devDesc->isAttached()) {
3209 devDesc->attach(mHwModules[i]);
3210 devDesc->importAudioPort(inProfile);
3211 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003212 }
3213 }
3214 mpClientInterface->closeInput(input);
3215 } else {
3216 ALOGW("Cannot open input stream for device %08x on hw module %s",
3217 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003218 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003219 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003220 }
3221 }
3222 // make sure all attached devices have been allocated a unique ID
3223 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003224 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003225 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003226 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3227 continue;
3228 }
François Gaffie2110e042015-03-24 08:41:51 +01003229 // The device is now validated and can be appended to the available devices of the engine
3230 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3231 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003232 i++;
3233 }
3234 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003235 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003236 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003237 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3238 continue;
3239 }
François Gaffie2110e042015-03-24 08:41:51 +01003240 // The device is now validated and can be appended to the available devices of the engine
3241 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3242 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003243 i++;
3244 }
3245 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003246 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003247 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003248 }
Eric Laurente552edb2014-03-10 17:42:56 -07003249
3250 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3251
3252 updateDevicesAndOutputs();
3253
3254#ifdef AUDIO_POLICY_TEST
3255 if (mPrimaryOutput != 0) {
3256 AudioParameter outputCmd = AudioParameter();
3257 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003258 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003259
3260 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3261 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003262 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3263 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003264 mTestLatencyMs = 0;
3265 mCurOutput = 0;
3266 mDirectOutput = false;
3267 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3268 mTestOutputs[i] = 0;
3269 }
3270
3271 const size_t SIZE = 256;
3272 char buffer[SIZE];
3273 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3274 run(buffer, ANDROID_PRIORITY_AUDIO);
3275 }
3276#endif //AUDIO_POLICY_TEST
3277}
3278
Eric Laurente0720872014-03-11 09:30:41 -07003279AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003280{
3281#ifdef AUDIO_POLICY_TEST
3282 exit();
3283#endif //AUDIO_POLICY_TEST
3284 for (size_t i = 0; i < mOutputs.size(); i++) {
3285 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003286 }
3287 for (size_t i = 0; i < mInputs.size(); i++) {
3288 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003289 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003290 mAvailableOutputDevices.clear();
3291 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003292 mOutputs.clear();
3293 mInputs.clear();
3294 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003295}
3296
Eric Laurente0720872014-03-11 09:30:41 -07003297status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003298{
Eric Laurent87ffa392015-05-22 10:32:38 -07003299 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003300}
3301
3302#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003303bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003304{
3305 ALOGV("entering threadLoop()");
3306 while (!exitPending())
3307 {
3308 String8 command;
3309 int valueInt;
3310 String8 value;
3311
3312 Mutex::Autolock _l(mLock);
3313 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3314
3315 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3316 AudioParameter param = AudioParameter(command);
3317
3318 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3319 valueInt != 0) {
3320 ALOGV("Test command %s received", command.string());
3321 String8 target;
3322 if (param.get(String8("target"), target) != NO_ERROR) {
3323 target = "Manager";
3324 }
3325 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3326 param.remove(String8("test_cmd_policy_output"));
3327 mCurOutput = valueInt;
3328 }
3329 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3330 param.remove(String8("test_cmd_policy_direct"));
3331 if (value == "false") {
3332 mDirectOutput = false;
3333 } else if (value == "true") {
3334 mDirectOutput = true;
3335 }
3336 }
3337 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3338 param.remove(String8("test_cmd_policy_input"));
3339 mTestInput = valueInt;
3340 }
3341
3342 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3343 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003344 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003345 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003346 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003347 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003348 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003349 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003350 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003351 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003352 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003353 if (target == "Manager") {
3354 mTestFormat = format;
3355 } else if (mTestOutputs[mCurOutput] != 0) {
3356 AudioParameter outputParam = AudioParameter();
3357 outputParam.addInt(String8("format"), format);
3358 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3359 }
3360 }
3361 }
3362 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3363 param.remove(String8("test_cmd_policy_channels"));
3364 int channels = 0;
3365
3366 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003367 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003368 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003369 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003370 }
3371 if (channels != 0) {
3372 if (target == "Manager") {
3373 mTestChannels = channels;
3374 } else if (mTestOutputs[mCurOutput] != 0) {
3375 AudioParameter outputParam = AudioParameter();
3376 outputParam.addInt(String8("channels"), channels);
3377 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3378 }
3379 }
3380 }
3381 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3382 param.remove(String8("test_cmd_policy_sampleRate"));
3383 if (valueInt >= 0 && valueInt <= 96000) {
3384 int samplingRate = valueInt;
3385 if (target == "Manager") {
3386 mTestSamplingRate = samplingRate;
3387 } else if (mTestOutputs[mCurOutput] != 0) {
3388 AudioParameter outputParam = AudioParameter();
3389 outputParam.addInt(String8("sampling_rate"), samplingRate);
3390 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3391 }
3392 }
3393 }
3394
3395 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3396 param.remove(String8("test_cmd_policy_reopen"));
3397
Eric Laurentc75307b2015-03-17 15:29:32 -07003398 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003399
Eric Laurentc75307b2015-03-17 15:29:32 -07003400 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003401
Eric Laurentc75307b2015-03-17 15:29:32 -07003402 removeOutput(mPrimaryOutput->mIoHandle);
3403 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3404 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003405 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003406 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3407 config.sample_rate = outputDesc->mSamplingRate;
3408 config.channel_mask = outputDesc->mChannelMask;
3409 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003410 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003411 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003412 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003413 &config,
3414 &outputDesc->mDevice,
3415 String8(""),
3416 &outputDesc->mLatency,
3417 outputDesc->mFlags);
3418 if (status != NO_ERROR) {
3419 ALOGE("Failed to reopen hardware output stream, "
3420 "samplingRate: %d, format %d, channels %d",
3421 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003422 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003423 outputDesc->mSamplingRate = config.sample_rate;
3424 outputDesc->mChannelMask = config.channel_mask;
3425 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003426 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003427 AudioParameter outputCmd = AudioParameter();
3428 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003429 mpClientInterface->setParameters(handle, outputCmd.toString());
3430 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003431 }
3432 }
3433
3434
3435 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3436 }
3437 }
3438 return false;
3439}
3440
Eric Laurente0720872014-03-11 09:30:41 -07003441void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003442{
3443 {
3444 AutoMutex _l(mLock);
3445 requestExit();
3446 mWaitWorkCV.signal();
3447 }
3448 requestExitAndWait();
3449}
3450
Eric Laurente0720872014-03-11 09:30:41 -07003451int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003452{
3453 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3454 if (output == mTestOutputs[i]) return i;
3455 }
3456 return 0;
3457}
3458#endif //AUDIO_POLICY_TEST
3459
3460// ---
3461
Eric Laurentc75307b2015-03-17 15:29:32 -07003462void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003463{
François Gaffie98cc1912015-03-18 17:52:40 +01003464 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003465 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003466 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003467 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003468}
3469
François Gaffie53615e22015-03-19 09:24:12 +01003470void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3471{
3472 mOutputs.removeItem(output);
3473}
3474
Eric Laurent1f2f2232014-06-02 12:01:23 -07003475void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003476{
François Gaffie98cc1912015-03-18 17:52:40 +01003477 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003478 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003479 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003480}
Eric Laurente552edb2014-03-10 17:42:56 -07003481
Eric Laurentc75307b2015-03-17 15:29:32 -07003482void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003483 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003484 const String8 address /*in*/,
3485 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003486 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003487 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003488 if (devDesc != 0) {
3489 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3490 desc->mIoHandle, address.string());
3491 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003492 }
3493}
3494
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003495status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003496 audio_policy_dev_state_t state,
3497 SortedVector<audio_io_handle_t>& outputs,
3498 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003499{
François Gaffie53615e22015-03-19 09:24:12 +01003500 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003501 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003502
3503 if (audio_device_is_digital(device)) {
3504 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003505 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003506 }
Eric Laurente552edb2014-03-10 17:42:56 -07003507
Eric Laurent3b73df72014-03-11 09:06:29 -07003508 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003509 // first list already open outputs that can be routed to this device
3510 for (size_t i = 0; i < mOutputs.size(); i++) {
3511 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003512 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003513 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003514 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3515 outputs.add(mOutputs.keyAt(i));
3516 } else {
3517 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003518 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003519 }
Eric Laurente552edb2014-03-10 17:42:56 -07003520 }
3521 }
3522 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003523 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003524 for (size_t i = 0; i < mHwModules.size(); i++)
3525 {
3526 if (mHwModules[i]->mHandle == 0) {
3527 continue;
3528 }
3529 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3530 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003531 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003532 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003533 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003534 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003535 profiles.add(profile);
3536 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3537 }
Eric Laurente552edb2014-03-10 17:42:56 -07003538 }
3539 }
3540 }
3541
Eric Laurent7b279bb2015-12-14 10:18:23 -08003542 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003543
Eric Laurente552edb2014-03-10 17:42:56 -07003544 if (profiles.isEmpty() && outputs.isEmpty()) {
3545 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3546 return BAD_VALUE;
3547 }
3548
3549 // open outputs for matching profiles if needed. Direct outputs are also opened to
3550 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3551 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003552 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003553
3554 // nothing to do if one output is already opened for this profile
3555 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003556 for (j = 0; j < outputs.size(); j++) {
3557 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003558 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003559 // matching profile: save the sample rates, format and channel masks supported
3560 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003561 if (audio_device_is_digital(device)) {
3562 devDesc->importAudioPort(profile);
3563 }
Eric Laurente552edb2014-03-10 17:42:56 -07003564 break;
3565 }
3566 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003567 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003568 continue;
3569 }
3570
Eric Laurent83b88082014-06-20 18:31:16 -07003571 ALOGV("opening output for device %08x with params %s profile %p",
3572 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003573 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003574 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003575 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3576 config.sample_rate = desc->mSamplingRate;
3577 config.channel_mask = desc->mChannelMask;
3578 config.format = desc->mFormat;
3579 config.offload_info.sample_rate = desc->mSamplingRate;
3580 config.offload_info.channel_mask = desc->mChannelMask;
3581 config.offload_info.format = desc->mFormat;
3582 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003583 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003584 &output,
3585 &config,
3586 &desc->mDevice,
3587 address,
3588 &desc->mLatency,
3589 desc->mFlags);
3590 if (status == NO_ERROR) {
3591 desc->mSamplingRate = config.sample_rate;
3592 desc->mChannelMask = config.channel_mask;
3593 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003594
Eric Laurentd4692962014-05-05 18:13:44 -07003595 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003596 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003597 char *param = audio_device_address_to_parameter(device, address);
3598 mpClientInterface->setParameters(output, String8(param));
3599 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003600 }
François Gaffie112b0af2015-11-19 16:13:25 +01003601 updateAudioProfiles(output, profile->getAudioProfiles());
3602 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003603 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003604 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003605 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003606 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003607 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003608 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003609 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003610 config.offload_info.sample_rate = config.sample_rate;
3611 config.offload_info.channel_mask = config.channel_mask;
3612 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003613 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003614 &output,
3615 &config,
3616 &desc->mDevice,
3617 address,
3618 &desc->mLatency,
3619 desc->mFlags);
3620 if (status == NO_ERROR) {
3621 desc->mSamplingRate = config.sample_rate;
3622 desc->mChannelMask = config.channel_mask;
3623 desc->mFormat = config.format;
3624 } else {
3625 output = AUDIO_IO_HANDLE_NONE;
3626 }
Eric Laurentd4692962014-05-05 18:13:44 -07003627 }
3628
Eric Laurentcf2c0212014-07-25 16:20:43 -07003629 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003630 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003631 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003632 sp<AudioPolicyMix> policyMix;
3633 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003634 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3635 address.string());
3636 }
François Gaffie036e1e92015-03-19 10:16:24 +01003637 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003638 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003639
Eric Laurent87ffa392015-05-22 10:32:38 -07003640 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3641 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003642 // no duplicated output for direct outputs and
3643 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003644 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003645
Eric Laurentd4692962014-05-05 18:13:44 -07003646 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003647 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003648
Eric Laurentd4692962014-05-05 18:13:44 -07003649 //TODO: configure audio effect output stage here
3650
3651 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003652 duplicatedOutput =
3653 mpClientInterface->openDuplicateOutput(output,
3654 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003655 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003656 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003657 sp<SwAudioOutputDescriptor> dupOutputDesc =
3658 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3659 dupOutputDesc->mOutput1 = mPrimaryOutput;
3660 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003661 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3662 dupOutputDesc->mFormat = desc->mFormat;
3663 dupOutputDesc->mChannelMask = desc->mChannelMask;
3664 dupOutputDesc->mLatency = desc->mLatency;
3665 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003666 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003667 } else {
3668 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003669 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003670 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003671 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003672 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003673 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003674 }
Eric Laurente552edb2014-03-10 17:42:56 -07003675 }
3676 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003677 } else {
3678 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003679 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003680 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003681 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003682 profiles.removeAt(profile_index);
3683 profile_index--;
3684 } else {
3685 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003686 // Load digital format info only for digital devices
3687 if (audio_device_is_digital(device)) {
3688 devDesc->importAudioPort(profile);
3689 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003690
François Gaffie53615e22015-03-19 09:24:12 +01003691 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003692 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3693 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003694 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003695 NULL/*patch handle*/, address.string());
3696 }
Eric Laurente552edb2014-03-10 17:42:56 -07003697 ALOGV("checkOutputsForDevice(): adding output %d", output);
3698 }
3699 }
3700
3701 if (profiles.isEmpty()) {
3702 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3703 return BAD_VALUE;
3704 }
Eric Laurentd4692962014-05-05 18:13:44 -07003705 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003706 // check if one opened output is not needed any more after disconnecting one device
3707 for (size_t i = 0; i < mOutputs.size(); i++) {
3708 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003709 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003710 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003711 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003712 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003713 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003714 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003715 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3716 mOutputs.keyAt(i));
3717 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003718 }
Eric Laurente552edb2014-03-10 17:42:56 -07003719 }
3720 }
Eric Laurentd4692962014-05-05 18:13:44 -07003721 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003722 for (size_t i = 0; i < mHwModules.size(); i++)
3723 {
3724 if (mHwModules[i]->mHandle == 0) {
3725 continue;
3726 }
3727 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3728 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003729 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003730 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003731 ALOGV("checkOutputsForDevice(): "
3732 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003733 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003734 }
3735 }
3736 }
3737 }
3738 return NO_ERROR;
3739}
3740
Paul McLean9080a4c2015-06-18 08:24:02 -07003741status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003742 audio_policy_dev_state_t state,
3743 SortedVector<audio_io_handle_t>& inputs,
3744 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003745{
Paul McLean9080a4c2015-06-18 08:24:02 -07003746 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003747 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003748
3749 if (audio_device_is_digital(device)) {
3750 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003751 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003752 }
3753
Eric Laurentd4692962014-05-05 18:13:44 -07003754 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3755 // first list already open inputs that can be routed to this device
3756 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3757 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003758 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003759 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3760 inputs.add(mInputs.keyAt(input_index));
3761 }
3762 }
3763
3764 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003765 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003766 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3767 {
3768 if (mHwModules[module_idx]->mHandle == 0) {
3769 continue;
3770 }
3771 for (size_t profile_index = 0;
3772 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3773 profile_index++)
3774 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003775 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3776
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003777 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003778 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003779 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003780 profiles.add(profile);
3781 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3782 profile_index, module_idx);
3783 }
Eric Laurentd4692962014-05-05 18:13:44 -07003784 }
3785 }
3786 }
3787
3788 if (profiles.isEmpty() && inputs.isEmpty()) {
3789 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3790 return BAD_VALUE;
3791 }
3792
3793 // open inputs for matching profiles if needed. Direct inputs are also opened to
3794 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3795 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3796
Eric Laurent1c333e22014-05-20 10:48:17 -07003797 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003798 // nothing to do if one input is already opened for this profile
3799 size_t input_index;
3800 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3801 desc = mInputs.valueAt(input_index);
3802 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003803 if (audio_device_is_digital(device)) {
3804 devDesc->importAudioPort(profile);
3805 }
Eric Laurentd4692962014-05-05 18:13:44 -07003806 break;
3807 }
3808 }
3809 if (input_index != mInputs.size()) {
3810 continue;
3811 }
3812
3813 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3814 desc = new AudioInputDescriptor(profile);
3815 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003816 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3817 config.sample_rate = desc->mSamplingRate;
3818 config.channel_mask = desc->mChannelMask;
3819 config.format = desc->mFormat;
3820 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003821 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003822 &input,
3823 &config,
3824 &desc->mDevice,
3825 address,
3826 AUDIO_SOURCE_MIC,
3827 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003828
Eric Laurentcf2c0212014-07-25 16:20:43 -07003829 if (status == NO_ERROR) {
3830 desc->mSamplingRate = config.sample_rate;
3831 desc->mChannelMask = config.channel_mask;
3832 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003833
Eric Laurentd4692962014-05-05 18:13:44 -07003834 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003835 char *param = audio_device_address_to_parameter(device, address);
3836 mpClientInterface->setParameters(input, String8(param));
3837 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003838 }
François Gaffie112b0af2015-11-19 16:13:25 +01003839 updateAudioProfiles(input, profile->getAudioProfiles());
3840 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003841 ALOGW("checkInputsForDevice() direct input missing param");
3842 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003843 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003844 }
3845
3846 if (input != 0) {
3847 addInput(input, desc);
3848 }
3849 } // endif input != 0
3850
Eric Laurentcf2c0212014-07-25 16:20:43 -07003851 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003852 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003853 profiles.removeAt(profile_index);
3854 profile_index--;
3855 } else {
3856 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003857 if (audio_device_is_digital(device)) {
3858 devDesc->importAudioPort(profile);
3859 }
Eric Laurentd4692962014-05-05 18:13:44 -07003860 ALOGV("checkInputsForDevice(): adding input %d", input);
3861 }
3862 } // end scan profiles
3863
3864 if (profiles.isEmpty()) {
3865 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3866 return BAD_VALUE;
3867 }
3868 } else {
3869 // Disconnect
3870 // check if one opened input is not needed any more after disconnecting one device
3871 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3872 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003873 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07003874 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3875 mInputs.keyAt(input_index));
3876 inputs.add(mInputs.keyAt(input_index));
3877 }
3878 }
3879 // Clear any profiles associated with the disconnected device.
3880 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3881 if (mHwModules[module_index]->mHandle == 0) {
3882 continue;
3883 }
3884 for (size_t profile_index = 0;
3885 profile_index < mHwModules[module_index]->mInputProfiles.size();
3886 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003887 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003888 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003889 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003890 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01003891 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07003892 }
3893 }
3894 }
3895 } // end disconnect
3896
3897 return NO_ERROR;
3898}
3899
3900
Eric Laurente0720872014-03-11 09:30:41 -07003901void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003902{
3903 ALOGV("closeOutput(%d)", output);
3904
Eric Laurentc75307b2015-03-17 15:29:32 -07003905 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003906 if (outputDesc == NULL) {
3907 ALOGW("closeOutput() unknown output %d", output);
3908 return;
3909 }
François Gaffie036e1e92015-03-19 10:16:24 +01003910 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003911
Eric Laurente552edb2014-03-10 17:42:56 -07003912 // look for duplicated outputs connected to the output being removed.
3913 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003914 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003915 if (dupOutputDesc->isDuplicated() &&
3916 (dupOutputDesc->mOutput1 == outputDesc ||
3917 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003918 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003919 if (dupOutputDesc->mOutput1 == outputDesc) {
3920 outputDesc2 = dupOutputDesc->mOutput2;
3921 } else {
3922 outputDesc2 = dupOutputDesc->mOutput1;
3923 }
3924 // As all active tracks on duplicated output will be deleted,
3925 // and as they were also referenced on the other output, the reference
3926 // count for their stream type must be adjusted accordingly on
3927 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003928 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003929 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003930 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003931 }
3932 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3933 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3934
3935 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003936 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003937 }
3938 }
3939
Eric Laurent05b90f82014-08-27 15:32:29 -07003940 nextAudioPortGeneration();
3941
Jean-Michel Triviff155c62016-02-26 12:07:16 -08003942 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07003943 if (index >= 0) {
3944 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3945 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3946 mAudioPatches.removeItemsAt(index);
3947 mpClientInterface->onAudioPatchListUpdate();
3948 }
3949
Eric Laurente552edb2014-03-10 17:42:56 -07003950 AudioParameter param;
3951 param.add(String8("closing"), String8("true"));
3952 mpClientInterface->setParameters(output, param.toString());
3953
3954 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003955 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003956 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003957}
3958
3959void AudioPolicyManager::closeInput(audio_io_handle_t input)
3960{
3961 ALOGV("closeInput(%d)", input);
3962
3963 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3964 if (inputDesc == NULL) {
3965 ALOGW("closeInput() unknown input %d", input);
3966 return;
3967 }
3968
Eric Laurent6a94d692014-05-20 11:18:06 -07003969 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003970
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08003971 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07003972 if (index >= 0) {
3973 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3974 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3975 mAudioPatches.removeItemsAt(index);
3976 mpClientInterface->onAudioPatchListUpdate();
3977 }
3978
3979 mpClientInterface->closeInput(input);
3980 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003981}
3982
Eric Laurentc75307b2015-03-17 15:29:32 -07003983SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3984 audio_devices_t device,
3985 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003986{
3987 SortedVector<audio_io_handle_t> outputs;
3988
3989 ALOGVV("getOutputsForDevice() device %04x", device);
3990 for (size_t i = 0; i < openOutputs.size(); i++) {
3991 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003992 i, openOutputs.valueAt(i)->isDuplicated(),
3993 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003994 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3995 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3996 outputs.add(openOutputs.keyAt(i));
3997 }
3998 }
3999 return outputs;
4000}
4001
Eric Laurente0720872014-03-11 09:30:41 -07004002bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004003 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004004{
4005 if (outputs1.size() != outputs2.size()) {
4006 return false;
4007 }
4008 for (size_t i = 0; i < outputs1.size(); i++) {
4009 if (outputs1[i] != outputs2[i]) {
4010 return false;
4011 }
4012 }
4013 return true;
4014}
4015
Eric Laurente0720872014-03-11 09:30:41 -07004016void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004017{
4018 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4019 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4020 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4021 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4022
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004023 // also take into account external policy-related changes: add all outputs which are
4024 // associated with policies in the "before" and "after" output vectors
4025 ALOGVV("checkOutputForStrategy(): policy related outputs");
4026 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004027 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004028 if (desc != 0 && desc->mPolicyMix != NULL) {
4029 srcOutputs.add(desc->mIoHandle);
4030 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4031 }
4032 }
4033 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004034 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004035 if (desc != 0 && desc->mPolicyMix != NULL) {
4036 dstOutputs.add(desc->mIoHandle);
4037 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4038 }
4039 }
4040
Eric Laurente552edb2014-03-10 17:42:56 -07004041 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4042 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4043 strategy, srcOutputs[0], dstOutputs[0]);
4044 // mute strategy while moving tracks from one output to another
4045 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004046 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004047 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004048 setStrategyMute(strategy, true, desc);
4049 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004050 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004051 sp<AudioSourceDescriptor> source =
4052 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4053 if (source != 0){
4054 connectAudioSource(source);
4055 }
Eric Laurente552edb2014-03-10 17:42:56 -07004056 }
4057
4058 // Move effects associated to this strategy from previous output to new output
4059 if (strategy == STRATEGY_MEDIA) {
4060 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4061 SortedVector<audio_io_handle_t> moved;
4062 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004063 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4064 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4065 effectDesc->mIo != fxOutput) {
4066 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004067 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4068 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004069 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004070 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004071 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004072 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004073 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004074 }
4075 }
4076 }
4077 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07004078 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004079 if (i == AUDIO_STREAM_PATCH) {
4080 continue;
4081 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004082 if (getStrategy((audio_stream_type_t)i) == strategy) {
4083 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004084 }
4085 }
4086 }
4087}
4088
Eric Laurente0720872014-03-11 09:30:41 -07004089void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004090{
François Gaffie2110e042015-03-24 08:41:51 +01004091 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004092 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004093 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004094 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004095 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004096 checkOutputForStrategy(STRATEGY_SONIFICATION);
4097 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004098 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004099 checkOutputForStrategy(STRATEGY_MEDIA);
4100 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004101 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004102}
4103
Eric Laurente0720872014-03-11 09:30:41 -07004104void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004105{
François Gaffie53615e22015-03-19 09:24:12 +01004106 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004107 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004108 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004109 return;
4110 }
4111
Eric Laurent3a4311c2014-03-17 12:00:47 -07004112 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004113 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4114 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4115 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004116 // suspend A2DP output if:
4117 // (NOT already suspended) &&
4118 // ((SCO device is connected &&
4119 // (forced usage for communication || for record is SCO))) ||
4120 // (phone state is ringing || in call)
4121 //
4122 // restore A2DP output if:
4123 // (Already suspended) &&
4124 // ((SCO device is NOT connected ||
4125 // (forced usage NOT for communication && NOT for record is SCO))) &&
4126 // (phone state is NOT ringing && NOT in call)
4127 //
4128 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004129 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004130 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4131 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4132 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4133 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004134
4135 mpClientInterface->restoreOutput(a2dpOutput);
4136 mA2dpSuspended = false;
4137 }
4138 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004139 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004140 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4141 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4142 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4143 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004144
4145 mpClientInterface->suspendOutput(a2dpOutput);
4146 mA2dpSuspended = true;
4147 }
4148 }
4149}
4150
Eric Laurentc75307b2015-03-17 15:29:32 -07004151audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4152 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004153{
4154 audio_devices_t device = AUDIO_DEVICE_NONE;
4155
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004156 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004157 if (index >= 0) {
4158 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4159 if (patchDesc->mUid != mUidCached) {
4160 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004161 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004162 return outputDesc->device();
4163 }
4164 }
4165
Eric Laurente552edb2014-03-10 17:42:56 -07004166 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004167 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004168 // use device for strategy enforced audible
4169 // 2: we are in call or the strategy phone is active on the output:
4170 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004171 // 3: the strategy for enforced audible is active but not enforced on the output:
4172 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004173 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004174 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004175 // 5: the strategy sonification is active on the output:
4176 // use device for strategy sonification
4177 // 6: the strategy "respectful" sonification is active on the output:
4178 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004179 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004180 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004181 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004182 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004183 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004184 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004185 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004186 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004187 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4188 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004189 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004190 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004191 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004192 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004193 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4194 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004195 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004196 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004197 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004198 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004199 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004200 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004201 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004202 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004203 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004204 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004205 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004206 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004207 }
4208
Eric Laurent1c333e22014-05-20 10:48:17 -07004209 ALOGV("getNewOutputDevice() selected device %x", device);
4210 return device;
4211}
4212
Eric Laurent232f26f2016-02-17 18:06:27 -08004213audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004214{
Eric Laurent232f26f2016-02-17 18:06:27 -08004215 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004216
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004217 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004218 if (index >= 0) {
4219 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4220 if (patchDesc->mUid != mUidCached) {
4221 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004222 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004223 return inputDesc->mDevice;
4224 }
4225 }
4226
Eric Laurent232f26f2016-02-17 18:06:27 -08004227 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004228
Eric Laurente552edb2014-03-10 17:42:56 -07004229 return device;
4230}
4231
Eric Laurente0720872014-03-11 09:30:41 -07004232uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004233 return (uint32_t)getStrategy(stream);
4234}
4235
Eric Laurente0720872014-03-11 09:30:41 -07004236audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004237 // By checking the range of stream before calling getStrategy, we avoid
4238 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4239 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004240 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004241 return AUDIO_DEVICE_NONE;
4242 }
4243 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004244 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004245 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4246 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4247 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004248 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004249 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004250 devices = outputDesc->device();
4251 break;
4252 }
Eric Laurente552edb2014-03-10 17:42:56 -07004253 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004254
4255 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4256 and doesn't really need to.*/
4257 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4258 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4259 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4260 }
Eric Laurente552edb2014-03-10 17:42:56 -07004261 return devices;
4262}
4263
François Gaffie2110e042015-03-24 08:41:51 +01004264routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4265{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004266 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004267 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004268}
4269
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004270uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4271 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004272 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4273 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4274 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004275 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4276 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4277 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004278 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004279 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004280}
4281
Eric Laurente0720872014-03-11 09:30:41 -07004282void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004283 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004284 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004285 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4286 updateDevicesAndOutputs();
4287 break;
4288 default:
4289 break;
4290 }
4291}
4292
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004293uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004294
4295 // skip beacon mute management if a dedicated TTS output is available
4296 if (mTtsOutputAvailable) {
4297 return 0;
4298 }
4299
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004300 switch(event) {
4301 case STARTING_OUTPUT:
4302 mBeaconMuteRefCount++;
4303 break;
4304 case STOPPING_OUTPUT:
4305 if (mBeaconMuteRefCount > 0) {
4306 mBeaconMuteRefCount--;
4307 }
4308 break;
4309 case STARTING_BEACON:
4310 mBeaconPlayingRefCount++;
4311 break;
4312 case STOPPING_BEACON:
4313 if (mBeaconPlayingRefCount > 0) {
4314 mBeaconPlayingRefCount--;
4315 }
4316 break;
4317 }
4318
4319 if (mBeaconMuteRefCount > 0) {
4320 // any playback causes beacon to be muted
4321 return setBeaconMute(true);
4322 } else {
4323 // no other playback: unmute when beacon starts playing, mute when it stops
4324 return setBeaconMute(mBeaconPlayingRefCount == 0);
4325 }
4326}
4327
4328uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4329 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4330 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4331 // keep track of muted state to avoid repeating mute/unmute operations
4332 if (mBeaconMuted != mute) {
4333 // mute/unmute AUDIO_STREAM_TTS on all outputs
4334 ALOGV("\t muting %d", mute);
4335 uint32_t maxLatency = 0;
4336 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004337 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004338 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004339 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004340 0 /*delay*/, AUDIO_DEVICE_NONE);
4341 const uint32_t latency = desc->latency() * 2;
4342 if (latency > maxLatency) {
4343 maxLatency = latency;
4344 }
4345 }
4346 mBeaconMuted = mute;
4347 return maxLatency;
4348 }
4349 return 0;
4350}
4351
Eric Laurente0720872014-03-11 09:30:41 -07004352audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004353 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004354{
Paul McLeanaa981192015-03-21 09:55:15 -07004355 // Routing
4356 // see if we have an explicit route
4357 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4358 // (getStrategy(stream)).
4359 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4360 // and activity count is non-zero
4361 // the device = the device from the descriptor in the RouteMap, and exit.
4362 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4363 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4364 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004365 // Special case for accessibility strategy which must follow any strategy it is
4366 // currently remapped to
4367 bool strategyMatch = (strat == strategy) ||
4368 ((strategy == STRATEGY_ACCESSIBILITY) &&
4369 ((mEngine->getStrategyForUsage(
4370 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4371 (strat == STRATEGY_MEDIA)));
4372 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004373 return route->mDeviceDescriptor->type();
4374 }
4375 }
4376
Eric Laurente552edb2014-03-10 17:42:56 -07004377 if (fromCache) {
4378 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4379 strategy, mDeviceForStrategy[strategy]);
4380 return mDeviceForStrategy[strategy];
4381 }
François Gaffie2110e042015-03-24 08:41:51 +01004382 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004383}
4384
Eric Laurente0720872014-03-11 09:30:41 -07004385void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004386{
4387 for (int i = 0; i < NUM_STRATEGIES; i++) {
4388 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4389 }
4390 mPreviousOutputs = mOutputs;
4391}
4392
Eric Laurent1f2f2232014-06-02 12:01:23 -07004393uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004394 audio_devices_t prevDevice,
4395 uint32_t delayMs)
4396{
4397 // mute/unmute strategies using an incompatible device combination
4398 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4399 // if unmuting, unmute only after the specified delay
4400 if (outputDesc->isDuplicated()) {
4401 return 0;
4402 }
4403
4404 uint32_t muteWaitMs = 0;
4405 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004406 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004407
4408 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4409 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004410 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004411 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4412 bool doMute = false;
4413
4414 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4415 doMute = true;
4416 outputDesc->mStrategyMutedByDevice[i] = true;
4417 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4418 doMute = true;
4419 outputDesc->mStrategyMutedByDevice[i] = false;
4420 }
Eric Laurent99401132014-05-07 19:48:15 -07004421 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004422 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004423 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004424 // skip output if it does not share any device with current output
4425 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4426 == AUDIO_DEVICE_NONE) {
4427 continue;
4428 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004429 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4430 mute ? "muting" : "unmuting", i, curDevice);
4431 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004432 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004433 if (mute) {
4434 // FIXME: should not need to double latency if volume could be applied
4435 // immediately by the audioflinger mixer. We must account for the delay
4436 // between now and the next time the audioflinger thread for this output
4437 // will process a buffer (which corresponds to one buffer size,
4438 // usually 1/2 or 1/4 of the latency).
4439 if (muteWaitMs < desc->latency() * 2) {
4440 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004441 }
4442 }
4443 }
4444 }
4445 }
4446 }
4447
Eric Laurent99401132014-05-07 19:48:15 -07004448 // temporary mute output if device selection changes to avoid volume bursts due to
4449 // different per device volumes
4450 if (outputDesc->isActive() && (device != prevDevice)) {
4451 if (muteWaitMs < outputDesc->latency() * 2) {
4452 muteWaitMs = outputDesc->latency() * 2;
4453 }
4454 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004455 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004456 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004457 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004458 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004459 muteWaitMs *2, device);
4460 }
4461 }
4462 }
4463
Eric Laurente552edb2014-03-10 17:42:56 -07004464 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4465 if (muteWaitMs > delayMs) {
4466 muteWaitMs -= delayMs;
4467 usleep(muteWaitMs * 1000);
4468 return muteWaitMs;
4469 }
4470 return 0;
4471}
4472
Eric Laurentc75307b2015-03-17 15:29:32 -07004473uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004474 audio_devices_t device,
4475 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004476 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004477 audio_patch_handle_t *patchHandle,
4478 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004479{
Eric Laurentc75307b2015-03-17 15:29:32 -07004480 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004481 AudioParameter param;
4482 uint32_t muteWaitMs;
4483
4484 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004485 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4486 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004487 return muteWaitMs;
4488 }
4489 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4490 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004491 if ((device != AUDIO_DEVICE_NONE) &&
4492 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004493 return 0;
4494 }
4495
4496 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004497 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004498
4499 audio_devices_t prevDevice = outputDesc->mDevice;
4500
Paul McLeanaa981192015-03-21 09:55:15 -07004501 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004502
4503 if (device != AUDIO_DEVICE_NONE) {
4504 outputDesc->mDevice = device;
4505 }
4506 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4507
4508 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004509 // the requested device is AUDIO_DEVICE_NONE
4510 // OR the requested device is the same as current device
4511 // AND force is not specified
4512 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004513 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004514 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4515 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004516 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004517 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004518 return muteWaitMs;
4519 }
4520
4521 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004522
Eric Laurente552edb2014-03-10 17:42:56 -07004523 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004524 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004525 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004526 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004527 DeviceVector deviceList = (address == NULL) ?
4528 mAvailableOutputDevices.getDevicesFromType(device)
4529 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004530 if (!deviceList.isEmpty()) {
4531 struct audio_patch patch;
4532 outputDesc->toAudioPortConfig(&patch.sources[0]);
4533 patch.num_sources = 1;
4534 patch.num_sinks = 0;
4535 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4536 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004537 patch.num_sinks++;
4538 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004539 ssize_t index;
4540 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4541 index = mAudioPatches.indexOfKey(*patchHandle);
4542 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004543 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004544 }
4545 sp< AudioPatch> patchDesc;
4546 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4547 if (index >= 0) {
4548 patchDesc = mAudioPatches.valueAt(index);
4549 afPatchHandle = patchDesc->mAfPatchHandle;
4550 }
4551
Eric Laurent1c333e22014-05-20 10:48:17 -07004552 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004553 &afPatchHandle,
4554 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004555 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4556 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004557 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004558 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004559 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004560 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004561 addAudioPatch(patchDesc->mHandle, patchDesc);
4562 } else {
4563 patchDesc->mPatch = patch;
4564 }
4565 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004566 if (patchHandle) {
4567 *patchHandle = patchDesc->mHandle;
4568 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004569 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004570 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004571 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004572 }
4573 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004574
4575 // inform all input as well
4576 for (size_t i = 0; i < mInputs.size(); i++) {
4577 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004578 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004579 AudioParameter inputCmd = AudioParameter();
4580 ALOGV("%s: inform input %d of device:%d", __func__,
4581 inputDescriptor->mIoHandle, device);
4582 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4583 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4584 inputCmd.toString(),
4585 delayMs);
4586 }
4587 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004588 }
Eric Laurente552edb2014-03-10 17:42:56 -07004589
4590 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004591 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004592
4593 return muteWaitMs;
4594}
4595
Eric Laurentc75307b2015-03-17 15:29:32 -07004596status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004597 int delayMs,
4598 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004599{
Eric Laurent6a94d692014-05-20 11:18:06 -07004600 ssize_t index;
4601 if (patchHandle) {
4602 index = mAudioPatches.indexOfKey(*patchHandle);
4603 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004604 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004605 }
4606 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004607 return INVALID_OPERATION;
4608 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004609 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4610 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004611 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004612 outputDesc->setPatchHandle(0);
Eric Laurent6a94d692014-05-20 11:18:06 -07004613 removeAudioPatch(patchDesc->mHandle);
4614 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004615 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004616 return status;
4617}
4618
4619status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4620 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004621 bool force,
4622 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004623{
4624 status_t status = NO_ERROR;
4625
Eric Laurent1f2f2232014-06-02 12:01:23 -07004626 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004627 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4628 inputDesc->mDevice = device;
4629
4630 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4631 if (!deviceList.isEmpty()) {
4632 struct audio_patch patch;
4633 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004634 // AUDIO_SOURCE_HOTWORD is for internal use only:
4635 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004636 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004637 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004638 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4639 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004640 patch.num_sinks = 1;
4641 //only one input device for now
4642 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004643 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004644 ssize_t index;
4645 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4646 index = mAudioPatches.indexOfKey(*patchHandle);
4647 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004648 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004649 }
4650 sp< AudioPatch> patchDesc;
4651 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4652 if (index >= 0) {
4653 patchDesc = mAudioPatches.valueAt(index);
4654 afPatchHandle = patchDesc->mAfPatchHandle;
4655 }
4656
Eric Laurent1c333e22014-05-20 10:48:17 -07004657 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004658 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004659 0);
4660 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004661 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004662 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004663 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004664 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004665 addAudioPatch(patchDesc->mHandle, patchDesc);
4666 } else {
4667 patchDesc->mPatch = patch;
4668 }
4669 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004670 if (patchHandle) {
4671 *patchHandle = patchDesc->mHandle;
4672 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004673 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004674 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004675 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004676 }
4677 }
4678 }
4679 return status;
4680}
4681
Eric Laurent6a94d692014-05-20 11:18:06 -07004682status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4683 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004684{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004685 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004686 ssize_t index;
4687 if (patchHandle) {
4688 index = mAudioPatches.indexOfKey(*patchHandle);
4689 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004690 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004691 }
4692 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004693 return INVALID_OPERATION;
4694 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004695 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4696 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004697 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004698 inputDesc->setPatchHandle(0);
Eric Laurent6a94d692014-05-20 11:18:06 -07004699 removeAudioPatch(patchDesc->mHandle);
4700 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004701 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004702 return status;
4703}
4704
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004705sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004706 String8 address,
4707 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004708 audio_format_t& format,
4709 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004710 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004711{
4712 // Choose an input profile based on the requested capture parameters: select the first available
4713 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004714 //
4715 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4716 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004717
4718 for (size_t i = 0; i < mHwModules.size(); i++)
4719 {
4720 if (mHwModules[i]->mHandle == 0) {
4721 continue;
4722 }
4723 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4724 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004725 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004726 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004727 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004728 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004729 format,
4730 &format /*updatedFormat*/,
4731 channelMask,
4732 &channelMask /*updatedChannelMask*/,
4733 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004734
Eric Laurente552edb2014-03-10 17:42:56 -07004735 return profile;
4736 }
4737 }
4738 }
4739 return NULL;
4740}
4741
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004742
4743audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004744 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004745{
François Gaffie036e1e92015-03-19 10:16:24 +01004746 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4747 audio_devices_t selectedDeviceFromMix =
4748 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004749
François Gaffie036e1e92015-03-19 10:16:24 +01004750 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4751 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004752 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004753 return getDeviceForInputSource(inputSource);
4754}
4755
4756audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4757{
Paul McLean466dc8e2015-04-17 13:15:36 -06004758 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4759 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004760 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004761 return route->mDeviceDescriptor->type();
4762 }
4763 }
4764
4765 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004766}
4767
Eric Laurente0720872014-03-11 09:30:41 -07004768float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004769 int index,
4770 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004771{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004772 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004773 // if a headset is connected, apply the following rules to ring tones and notifications
4774 // to avoid sound level bursts in user's ears:
4775 // - always attenuate ring tones and notifications volume by 6dB
4776 // - if music is playing, always limit the volume to current music volume,
4777 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004778 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004779 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4780 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4781 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4782 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4783 ((stream_strategy == STRATEGY_SONIFICATION)
4784 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004785 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004786 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004787 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004788 mVolumeCurves->canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004789 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004790 // when the phone is ringing we must consider that music could have been paused just before
4791 // by the music application and behave as if music was active if the last music track was
4792 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004793 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004794 mLimitRingtoneVolume) {
4795 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004796 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004797 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4798 musicDevice),
4799 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004800 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4801 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4802 if (volumeDb > minVolDB) {
4803 volumeDb = minVolDB;
4804 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004805 }
4806 }
4807 }
4808
Eric Laurentffbc80f2015-03-18 18:30:19 -07004809 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004810}
4811
Eric Laurente0720872014-03-11 09:30:41 -07004812status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004813 int index,
4814 const sp<AudioOutputDescriptor>& outputDesc,
4815 audio_devices_t device,
4816 int delayMs,
4817 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004818{
Eric Laurente552edb2014-03-10 17:42:56 -07004819 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004820 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004821 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004822 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004823 return NO_ERROR;
4824 }
François Gaffie2110e042015-03-24 08:41:51 +01004825 audio_policy_forced_cfg_t forceUseForComm =
4826 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004827 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004828 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4829 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004830 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004831 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004832 return INVALID_OPERATION;
4833 }
4834
Eric Laurentc75307b2015-03-17 15:29:32 -07004835 if (device == AUDIO_DEVICE_NONE) {
4836 device = outputDesc->device();
4837 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004838
Eric Laurentffbc80f2015-03-18 18:30:19 -07004839 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004840 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004841 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004842 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004843
Eric Laurentffbc80f2015-03-18 18:30:19 -07004844 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004845
Eric Laurent3b73df72014-03-11 09:06:29 -07004846 if (stream == AUDIO_STREAM_VOICE_CALL ||
4847 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004848 float voiceVolume;
4849 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004850 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004851 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004852 } else {
4853 voiceVolume = 1.0;
4854 }
4855
Eric Laurentc75307b2015-03-17 15:29:32 -07004856 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004857 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4858 mLastVoiceVolume = voiceVolume;
4859 }
4860 }
4861
4862 return NO_ERROR;
4863}
4864
Eric Laurentc75307b2015-03-17 15:29:32 -07004865void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4866 audio_devices_t device,
4867 int delayMs,
4868 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004869{
Eric Laurentc75307b2015-03-17 15:29:32 -07004870 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004871
Eric Laurent3b73df72014-03-11 09:06:29 -07004872 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004873 if (stream == AUDIO_STREAM_PATCH) {
4874 continue;
4875 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004876 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004877 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004878 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004879 device,
4880 delayMs,
4881 force);
4882 }
4883}
4884
Eric Laurente0720872014-03-11 09:30:41 -07004885void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004886 bool on,
4887 const sp<AudioOutputDescriptor>& outputDesc,
4888 int delayMs,
4889 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004890{
Eric Laurent72249d52015-06-08 11:12:15 -07004891 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4892 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004893 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004894 if (stream == AUDIO_STREAM_PATCH) {
4895 continue;
4896 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004897 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004898 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004899 }
4900 }
4901}
4902
Eric Laurente0720872014-03-11 09:30:41 -07004903void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004904 bool on,
4905 const sp<AudioOutputDescriptor>& outputDesc,
4906 int delayMs,
4907 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004908{
Eric Laurente552edb2014-03-10 17:42:56 -07004909 if (device == AUDIO_DEVICE_NONE) {
4910 device = outputDesc->device();
4911 }
4912
Eric Laurentc75307b2015-03-17 15:29:32 -07004913 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4914 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004915
4916 if (on) {
4917 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004918 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004919 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004920 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004921 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004922 }
4923 }
4924 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4925 outputDesc->mMuteCount[stream]++;
4926 } else {
4927 if (outputDesc->mMuteCount[stream] == 0) {
4928 ALOGV("setStreamMute() unmuting non muted stream!");
4929 return;
4930 }
4931 if (--outputDesc->mMuteCount[stream] == 0) {
4932 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004933 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004934 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004935 device,
4936 delayMs);
4937 }
4938 }
4939}
4940
Eric Laurente0720872014-03-11 09:30:41 -07004941void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004942 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004943{
Eric Laurent87ffa392015-05-22 10:32:38 -07004944 if(!hasPrimaryOutput()) {
4945 return;
4946 }
4947
Eric Laurente552edb2014-03-10 17:42:56 -07004948 // if the stream pertains to sonification strategy and we are in call we must
4949 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4950 // in the device used for phone strategy and play the tone if the selected device does not
4951 // interfere with the device used for phone strategy
4952 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4953 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004954 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004955 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4956 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004957 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004958 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4959 stream, starting, outputDesc->mDevice, stateChange);
4960 if (outputDesc->mRefCount[stream]) {
4961 int muteCount = 1;
4962 if (stateChange) {
4963 muteCount = outputDesc->mRefCount[stream];
4964 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004965 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004966 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4967 for (int i = 0; i < muteCount; i++) {
4968 setStreamMute(stream, starting, mPrimaryOutput);
4969 }
4970 } else {
4971 ALOGV("handleIncallSonification() high visibility");
4972 if (outputDesc->device() &
4973 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4974 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4975 for (int i = 0; i < muteCount; i++) {
4976 setStreamMute(stream, starting, mPrimaryOutput);
4977 }
4978 }
4979 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004980 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4981 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004982 } else {
4983 mpClientInterface->stopTone();
4984 }
4985 }
4986 }
4987 }
4988}
4989
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004990audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4991{
4992 // flags to stream type mapping
4993 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4994 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4995 }
4996 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4997 return AUDIO_STREAM_BLUETOOTH_SCO;
4998 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004999 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5000 return AUDIO_STREAM_TTS;
5001 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005002
5003 // usage to stream type mapping
5004 switch (attr->usage) {
5005 case AUDIO_USAGE_MEDIA:
5006 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005007 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5008 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005009 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005010 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5011 return AUDIO_STREAM_ALARM;
5012 }
5013 if (isStreamActive(AUDIO_STREAM_RING)) {
5014 return AUDIO_STREAM_RING;
5015 }
5016 if (isInCall()) {
5017 return AUDIO_STREAM_VOICE_CALL;
5018 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005019 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005020 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5021 return AUDIO_STREAM_SYSTEM;
5022 case AUDIO_USAGE_VOICE_COMMUNICATION:
5023 return AUDIO_STREAM_VOICE_CALL;
5024
5025 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5026 return AUDIO_STREAM_DTMF;
5027
5028 case AUDIO_USAGE_ALARM:
5029 return AUDIO_STREAM_ALARM;
5030 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5031 return AUDIO_STREAM_RING;
5032
5033 case AUDIO_USAGE_NOTIFICATION:
5034 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5035 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5036 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5037 case AUDIO_USAGE_NOTIFICATION_EVENT:
5038 return AUDIO_STREAM_NOTIFICATION;
5039
5040 case AUDIO_USAGE_UNKNOWN:
5041 default:
5042 return AUDIO_STREAM_MUSIC;
5043 }
5044}
Eric Laurente83b55d2014-11-14 10:06:21 -08005045
François Gaffie53615e22015-03-19 09:24:12 +01005046bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5047{
Eric Laurente83b55d2014-11-14 10:06:21 -08005048 // has flags that map to a strategy?
5049 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5050 return true;
5051 }
5052
5053 // has known usage?
5054 switch (paa->usage) {
5055 case AUDIO_USAGE_UNKNOWN:
5056 case AUDIO_USAGE_MEDIA:
5057 case AUDIO_USAGE_VOICE_COMMUNICATION:
5058 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5059 case AUDIO_USAGE_ALARM:
5060 case AUDIO_USAGE_NOTIFICATION:
5061 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5062 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5063 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5064 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5065 case AUDIO_USAGE_NOTIFICATION_EVENT:
5066 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5067 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5068 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5069 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005070 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005071 break;
5072 default:
5073 return false;
5074 }
5075 return true;
5076}
5077
François Gaffiead3183e2015-03-18 16:55:35 +01005078bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5079 routing_strategy strategy, uint32_t inPastMs,
5080 nsecs_t sysTime) const
5081{
5082 if ((sysTime == 0) && (inPastMs != 0)) {
5083 sysTime = systemTime();
5084 }
5085 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5086 if (i == AUDIO_STREAM_PATCH) {
5087 continue;
5088 }
5089 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5090 (NUM_STRATEGIES == strategy)) &&
5091 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5092 return true;
5093 }
5094 }
5095 return false;
5096}
5097
François Gaffie2110e042015-03-24 08:41:51 +01005098audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5099{
5100 return mEngine->getForceUse(usage);
5101}
5102
5103bool AudioPolicyManager::isInCall()
5104{
5105 return isStateInCall(mEngine->getPhoneState());
5106}
5107
5108bool AudioPolicyManager::isStateInCall(int state)
5109{
5110 return is_state_in_call(state);
5111}
5112
Eric Laurentd60560a2015-04-10 11:31:20 -07005113void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5114{
5115 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5116 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5117 if (sourceDesc->mDevice->equals(deviceDesc)) {
5118 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5119 stopAudioSource(sourceDesc->getHandle());
5120 }
5121 }
5122
5123 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5124 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5125 bool release = false;
5126 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5127 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5128 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5129 source->ext.device.type == deviceDesc->type()) {
5130 release = true;
5131 }
5132 }
5133 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5134 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5135 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5136 sink->ext.device.type == deviceDesc->type()) {
5137 release = true;
5138 }
5139 }
5140 if (release) {
5141 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5142 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5143 }
5144 }
5145}
5146
Phil Burk09bc4612016-02-24 15:58:15 -08005147// Modify the list of surround sound formats supported.
5148void AudioPolicyManager::filterSurroundFormats(FormatVector &formats) {
5149
5150 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5151 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5152 ALOGI("%s: forced use = %d", __FUNCTION__, forceUse);
5153
5154 // Analyze original support for various formats.
5155 bool supportsRawSurround = false;
5156 bool supportsIEC61937 = false;
5157 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5158 audio_format_t format = formats[formatIndex];
5159 ALOGI("%s: original formats: #%x", __FUNCTION__, format);
5160 switch (format) {
5161 case AUDIO_FORMAT_AC3:
5162 case AUDIO_FORMAT_E_AC3:
5163 case AUDIO_FORMAT_DTS:
5164 case AUDIO_FORMAT_DTS_HD:
5165 supportsRawSurround = true;
5166 break;
5167 case AUDIO_FORMAT_IEC61937:
5168 supportsIEC61937 = true;
5169 break;
5170 default:
5171 break;
5172 }
5173 }
5174 ALOGI("%s: supportsRawSurround = %d, supportsIEC61937 = %d",
5175 __FUNCTION__, supportsRawSurround, supportsIEC61937);
5176
5177 // Modify formats based on surround preferences.
5178 // If NEVER, remove support for surround formats.
5179 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER)
5180 && (supportsRawSurround || supportsIEC61937)) {
5181 // Remove surround sound related formats.
5182 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5183 audio_format_t format = formats[formatIndex];
5184 switch(format) {
5185 case AUDIO_FORMAT_AC3:
5186 case AUDIO_FORMAT_E_AC3:
5187 case AUDIO_FORMAT_DTS:
5188 case AUDIO_FORMAT_DTS_HD:
5189 case AUDIO_FORMAT_IEC61937:
5190 ALOGI("%s: remove #%x", __FUNCTION__, format);
5191 formats.removeAt(formatIndex);
5192 break;
5193 default:
5194 formatIndex++; // keep it
5195 break;
5196 }
5197 }
5198 supportsRawSurround = false;
5199 supportsIEC61937 = false;
5200 }
5201 // If ALWAYS, add support for raw surround formats if all are missing.
5202 // This assumes that if any of these formats are reported by the HAL
5203 // then the report is valid and should not be modified.
5204 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5205 && !supportsRawSurround) {
5206 formats.add(AUDIO_FORMAT_AC3);
5207 formats.add(AUDIO_FORMAT_E_AC3);
5208 formats.add(AUDIO_FORMAT_DTS);
5209 formats.add(AUDIO_FORMAT_DTS_HD);
5210 supportsRawSurround = true;
5211 }
5212 // Add support for IEC61937 if raw surround supported.
5213 // The HAL could do this but add it here, just in case.
5214 if (supportsRawSurround && !supportsIEC61937) {
5215 formats.add(AUDIO_FORMAT_IEC61937);
5216 // supportsIEC61937 = true;
5217 }
5218 // Just for debugging.
5219 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5220 audio_format_t format = formats[formatIndex];
5221 ALOGI("%s: final formats: #%x", __FUNCTION__, format);
5222 }
5223}
5224
François Gaffie112b0af2015-11-19 16:13:25 +01005225void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle,
5226 AudioProfileVector &profiles)
5227{
5228 String8 reply;
5229 char *value;
5230 // Format MUST be checked first to update the list of AudioProfile
5231 if (profiles.hasDynamicFormat()) {
5232 reply = mpClientInterface->getParameters(ioHandle,
5233 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk09bc4612016-02-24 15:58:15 -08005234 ALOGI("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005235 AudioParameter repliedParameters(reply);
5236 if (repliedParameters.get(
5237 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005238 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5239 return;
5240 }
Phil Burk09bc4612016-02-24 15:58:15 -08005241 FormatVector formats = formatsFromString(reply.string());
5242 filterSurroundFormats(formats);
5243 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005244 }
5245 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5246
Eric Laurent20eb3a42016-01-26 18:39:17 -08005247 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005248 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005249 ChannelsVector channelMasks;
5250 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005251 AudioParameter requestedParameters;
5252 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5253
5254 if (profiles.hasDynamicRateFor(format)) {
5255 reply = mpClientInterface->getParameters(ioHandle,
5256 requestedParameters.toString() + ";" +
5257 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5258 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005259 AudioParameter repliedParameters(reply);
5260 if (repliedParameters.get(
5261 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5262 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005263 }
5264 }
5265 if (profiles.hasDynamicChannelsFor(format)) {
5266 reply = mpClientInterface->getParameters(ioHandle,
5267 requestedParameters.toString() + ";" +
5268 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5269 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005270 AudioParameter repliedParameters(reply);
5271 if (repliedParameters.get(
5272 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5273 channelMasks = channelMasksFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005274 }
5275 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005276 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005277 }
5278}
Eric Laurentd60560a2015-04-10 11:31:20 -07005279
Eric Laurente552edb2014-03-10 17:42:56 -07005280}; // namespace android