blob: c3b15299bac1668ee6a1ce4df70177b4adf4df3c [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 Laurent794fde22016-03-11 09:50:45 -0800462 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700463 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700464 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800465
Eric Laurent63dea1d2015-07-02 17:10:28 -0700466 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800467 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700468 }
469
François Gaffie2110e042015-03-24 08:41:51 +0100470 /**
471 * Switching to or from incall state or switching between telephony and VoIP lead to force
472 * routing command.
473 */
474 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
475 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700476
477 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700478 checkA2dpSuspend();
479 checkOutputForAllStrategies();
480 updateDevicesAndOutputs();
481
Eric Laurente552edb2014-03-10 17:42:56 -0700482 int delayMs = 0;
483 if (isStateInCall(state)) {
484 nsecs_t sysTime = systemTime();
485 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700486 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700487 // mute media and sonification strategies and delay device switch by the largest
488 // latency of any output where either strategy is active.
489 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100490 if ((isStrategyActive(desc, STRATEGY_MEDIA,
491 SONIFICATION_HEADSET_MUSIC_DELAY,
492 sysTime) ||
493 isStrategyActive(desc, STRATEGY_SONIFICATION,
494 SONIFICATION_HEADSET_MUSIC_DELAY,
495 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700496 (delayMs < (int)desc->latency()*2)) {
497 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700498 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700499 setStrategyMute(STRATEGY_MEDIA, true, desc);
500 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700501 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700502 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
503 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700504 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
505 }
506 }
507
Eric Laurent87ffa392015-05-22 10:32:38 -0700508 if (hasPrimaryOutput()) {
509 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
510 // the device returned is not necessarily reachable via this output
511 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
512 // force routing command to audio hardware when ending call
513 // even if no device change is needed
514 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
515 rxDevice = mPrimaryOutput->device();
516 }
Eric Laurente552edb2014-03-10 17:42:56 -0700517
Eric Laurent87ffa392015-05-22 10:32:38 -0700518 if (state == AUDIO_MODE_IN_CALL) {
519 updateCallRouting(rxDevice, delayMs);
520 } else if (oldState == AUDIO_MODE_IN_CALL) {
521 if (mCallRxPatch != 0) {
522 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
523 mCallRxPatch.clear();
524 }
525 if (mCallTxPatch != 0) {
526 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
527 mCallTxPatch.clear();
528 }
529 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
530 } else {
531 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700532 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700533 }
Eric Laurente552edb2014-03-10 17:42:56 -0700534 // if entering in call state, handle special case of active streams
535 // pertaining to sonification strategy see handleIncallSonification()
536 if (isStateInCall(state)) {
537 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent794fde22016-03-11 09:50:45 -0800538 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700539 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700540 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700541
542 // force reevaluating accessibility routing when call starts
543 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700544 }
545
546 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700547 if (state == AUDIO_MODE_RINGTONE &&
548 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700549 mLimitRingtoneVolume = true;
550 } else {
551 mLimitRingtoneVolume = false;
552 }
553}
554
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700555audio_mode_t AudioPolicyManager::getPhoneState() {
556 return mEngine->getPhoneState();
557}
558
Eric Laurente0720872014-03-11 09:30:41 -0700559void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700560 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700561{
François Gaffie2110e042015-03-24 08:41:51 +0100562 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700563
François Gaffie2110e042015-03-24 08:41:51 +0100564 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
565 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
566 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700567 }
François Gaffie2110e042015-03-24 08:41:51 +0100568 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
569 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
570 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700571
572 // check for device and output changes triggered by new force usage
573 checkA2dpSuspend();
574 checkOutputForAllStrategies();
575 updateDevicesAndOutputs();
Phil Burk09bc4612016-02-24 15:58:15 -0800576
Eric Laurent87ffa392015-05-22 10:32:38 -0700577 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700578 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
579 updateCallRouting(newDevice);
580 }
Eric Laurente552edb2014-03-10 17:42:56 -0700581 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700582 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
583 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
584 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
585 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700586 }
Eric Laurente552edb2014-03-10 17:42:56 -0700587 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700588 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700589 }
590 }
591
Eric Laurent232f26f2016-02-17 18:06:27 -0800592 audio_io_handle_t activeInput = mInputs.getActiveInput();
593 if (activeInput != 0) {
594 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
595 audio_devices_t newDevice = getNewInputDevice(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700596 // Force new input selection if the new device can not be reached via current input
Eric Laurent232f26f2016-02-17 18:06:27 -0800597 if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
598 setInputDevice(activeInput, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700599 } else {
Eric Laurent232f26f2016-02-17 18:06:27 -0800600 closeInput(activeInput);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700601 }
Eric Laurente552edb2014-03-10 17:42:56 -0700602 }
Eric Laurente552edb2014-03-10 17:42:56 -0700603}
604
Eric Laurente0720872014-03-11 09:30:41 -0700605void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700606{
607 ALOGV("setSystemProperty() property %s, value %s", property, value);
608}
609
610// Find a direct output profile compatible with the parameters passed, even if the input flags do
611// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800612sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700613 audio_devices_t device,
614 uint32_t samplingRate,
615 audio_format_t format,
616 audio_channel_mask_t channelMask,
617 audio_output_flags_t flags)
618{
Eric Laurent861a6282015-05-18 15:40:16 -0700619 // only retain flags that will drive the direct output profile selection
620 // if explicitly requested
621 static const uint32_t kRelevantFlags =
622 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
623 flags =
624 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
625
626 sp<IOProfile> profile;
627
Eric Laurente552edb2014-03-10 17:42:56 -0700628 for (size_t i = 0; i < mHwModules.size(); i++) {
629 if (mHwModules[i]->mHandle == 0) {
630 continue;
631 }
632 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700633 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
634 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700635 samplingRate, NULL /*updatedSamplingRate*/,
636 format, NULL /*updatedFormat*/,
637 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700638 flags)) {
639 continue;
640 }
641 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100642 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700643 continue;
644 }
645 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100646 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700647 continue;
648 }
649 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100650 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700651 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700652 }
Eric Laurente552edb2014-03-10 17:42:56 -0700653 }
654 }
Eric Laurent861a6282015-05-18 15:40:16 -0700655 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700656}
657
Eric Laurente0720872014-03-11 09:30:41 -0700658audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100659 uint32_t samplingRate,
660 audio_format_t format,
661 audio_channel_mask_t channelMask,
662 audio_output_flags_t flags,
663 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700664{
Eric Laurent3b73df72014-03-11 09:06:29 -0700665 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700666 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
667 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
668 device, stream, samplingRate, format, channelMask, flags);
669
Eric Laurente83b55d2014-11-14 10:06:21 -0800670 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
671 stream, samplingRate,format, channelMask,
672 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700673}
674
Eric Laurente83b55d2014-11-14 10:06:21 -0800675status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
676 audio_io_handle_t *output,
677 audio_session_t session,
678 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700679 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800680 uint32_t samplingRate,
681 audio_format_t format,
682 audio_channel_mask_t channelMask,
683 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700684 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800685 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700686{
Eric Laurente83b55d2014-11-14 10:06:21 -0800687 audio_attributes_t attributes;
688 if (attr != NULL) {
689 if (!isValidAttributes(attr)) {
690 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
691 attr->usage, attr->content_type, attr->flags,
692 attr->tags);
693 return BAD_VALUE;
694 }
695 attributes = *attr;
696 } else {
697 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
698 ALOGE("getOutputForAttr(): invalid stream type");
699 return BAD_VALUE;
700 }
701 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700702 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700703 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800704 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100705 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800706 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100707 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800708 }
François Gaffie036e1e92015-03-19 10:16:24 +0100709 *stream = streamTypefromAttributesInt(&attributes);
710 *output = desc->mIoHandle;
711 ALOGV("getOutputForAttr() returns output %d", *output);
712 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800713 }
714 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
715 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
716 return BAD_VALUE;
717 }
718
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700719 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
720 " session %d selectedDeviceId %d",
721 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
722 session, selectedDeviceId);
723
724 *stream = streamTypefromAttributesInt(&attributes);
725
726 // Explicit routing?
727 sp<DeviceDescriptor> deviceDesc;
728 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
729 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
730 deviceDesc = mAvailableOutputDevices[i];
731 break;
732 }
733 }
734 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700735
Eric Laurente83b55d2014-11-14 10:06:21 -0800736 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700737 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700738
Eric Laurente83b55d2014-11-14 10:06:21 -0800739 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700740 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
741 }
742
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700743 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700744 device, samplingRate, format, channelMask, flags);
745
Eric Laurente83b55d2014-11-14 10:06:21 -0800746 *output = getOutputForDevice(device, session, *stream,
747 samplingRate, format, channelMask,
748 flags, offloadInfo);
749 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700750 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800751 return INVALID_OPERATION;
752 }
Paul McLeanaa981192015-03-21 09:55:15 -0700753
Eric Laurente83b55d2014-11-14 10:06:21 -0800754 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700755}
756
757audio_io_handle_t AudioPolicyManager::getOutputForDevice(
758 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800759 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700760 audio_stream_type_t stream,
761 uint32_t samplingRate,
762 audio_format_t format,
763 audio_channel_mask_t channelMask,
764 audio_output_flags_t flags,
765 const audio_offload_info_t *offloadInfo)
766{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700767 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700768 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700769 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700770
Eric Laurente552edb2014-03-10 17:42:56 -0700771#ifdef AUDIO_POLICY_TEST
772 if (mCurOutput != 0) {
773 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
774 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
775
776 if (mTestOutputs[mCurOutput] == 0) {
777 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700778 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
779 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700780 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700781 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700782 outputDesc->mFlags =
783 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700784 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700785 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
786 config.sample_rate = mTestSamplingRate;
787 config.channel_mask = mTestChannels;
788 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700789 if (offloadInfo != NULL) {
790 config.offload_info = *offloadInfo;
791 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700792 status = mpClientInterface->openOutput(0,
793 &mTestOutputs[mCurOutput],
794 &config,
795 &outputDesc->mDevice,
796 String8(""),
797 &outputDesc->mLatency,
798 outputDesc->mFlags);
799 if (status == NO_ERROR) {
800 outputDesc->mSamplingRate = config.sample_rate;
801 outputDesc->mFormat = config.format;
802 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700803 AudioParameter outputCmd = AudioParameter();
804 outputCmd.addInt(String8("set_id"),mCurOutput);
805 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
806 addOutput(mTestOutputs[mCurOutput], outputDesc);
807 }
808 }
809 return mTestOutputs[mCurOutput];
810 }
811#endif //AUDIO_POLICY_TEST
812
813 // open a direct output if required by specified parameters
814 //force direct flag if offload flag is set: offloading implies a direct output stream
815 // and all common behaviors are driven by checking only the direct flag
816 // this should normally be set appropriately in the policy configuration file
817 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700818 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700819 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700820 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
821 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
822 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800823 // only allow deep buffering for music stream type
824 if (stream != AUDIO_STREAM_MUSIC) {
825 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700826 } else if (/* stream == AUDIO_STREAM_MUSIC && */
827 flags == AUDIO_OUTPUT_FLAG_NONE &&
828 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
829 // use DEEP_BUFFER as default output for music stream type
830 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800831 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700832 if (stream == AUDIO_STREAM_TTS) {
833 flags = AUDIO_OUTPUT_FLAG_TTS;
834 }
Eric Laurente552edb2014-03-10 17:42:56 -0700835
Eric Laurentb732cf52014-09-24 19:08:21 -0700836 sp<IOProfile> profile;
837
838 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700839 // and not explicitly requested
840 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
Glenn Kasten05ddca52016-02-11 08:17:12 -0800841 audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700842 audio_channel_count_from_out_mask(channelMask) <= 2) {
843 goto non_direct_output;
844 }
845
Andy Hung2ddee192015-12-18 17:34:44 -0800846 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
847 // This prevents creating an offloaded track and tearing it down immediately after start
848 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700849 // FIXME: We should check the audio session here but we do not have it in this context.
850 // This may prevent offloading in rare situations where effects are left active by apps
851 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700852
Eric Laurente552edb2014-03-10 17:42:56 -0700853 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800854 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700855 profile = getProfileForDirectOutput(device,
856 samplingRate,
857 format,
858 channelMask,
859 (audio_output_flags_t)flags);
860 }
861
Eric Laurent1c333e22014-05-20 10:48:17 -0700862 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700863 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700864
865 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700866 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700867 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
868 outputDesc = desc;
869 // reuse direct output if currently open and configured with same parameters
870 if ((samplingRate == outputDesc->mSamplingRate) &&
Eric Laurente6930022016-02-11 10:20:40 -0800871 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700872 (channelMask == outputDesc->mChannelMask)) {
873 outputDesc->mDirectOpenCount++;
874 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
875 return mOutputs.keyAt(i);
876 }
877 }
878 }
879 // close direct output if currently open and configured with different parameters
880 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700881 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700882 }
Eric Laurent861a6282015-05-18 15:40:16 -0700883
884 // if the selected profile is offloaded and no offload info was specified,
885 // create a default one
886 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100887 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700888 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
889 defaultOffloadInfo.sample_rate = samplingRate;
890 defaultOffloadInfo.channel_mask = channelMask;
891 defaultOffloadInfo.format = format;
892 defaultOffloadInfo.stream_type = stream;
893 defaultOffloadInfo.bit_rate = 0;
894 defaultOffloadInfo.duration_us = -1;
895 defaultOffloadInfo.has_video = true; // conservative
896 defaultOffloadInfo.is_streaming = true; // likely
897 offloadInfo = &defaultOffloadInfo;
898 }
899
Eric Laurentc75307b2015-03-17 15:29:32 -0700900 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700901 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700902 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700903 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700904 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
905 config.sample_rate = samplingRate;
906 config.channel_mask = channelMask;
907 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700908 if (offloadInfo != NULL) {
909 config.offload_info = *offloadInfo;
910 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700911 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700912 &output,
913 &config,
914 &outputDesc->mDevice,
915 String8(""),
916 &outputDesc->mLatency,
917 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700918
919 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700920 if (status != NO_ERROR ||
921 (samplingRate != 0 && samplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -0800922 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700923 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700924 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
925 "format %d %d, channelMask %04x %04x", output, samplingRate,
926 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
927 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700928 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700929 mpClientInterface->closeOutput(output);
930 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800931 // fall back to mixer output if possible when the direct output could not be open
Glenn Kasten05ddca52016-02-11 08:17:12 -0800932 if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) {
Eric Laurenta82797f2015-01-30 11:49:43 -0800933 goto non_direct_output;
934 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700935 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700936 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700937 outputDesc->mSamplingRate = config.sample_rate;
938 outputDesc->mChannelMask = config.channel_mask;
939 outputDesc->mFormat = config.format;
940 outputDesc->mRefCount[stream] = 0;
941 outputDesc->mStopTime[stream] = 0;
942 outputDesc->mDirectOpenCount = 1;
943
Eric Laurente552edb2014-03-10 17:42:56 -0700944 audio_io_handle_t srcOutput = getOutputForEffect();
945 addOutput(output, outputDesc);
946 audio_io_handle_t dstOutput = getOutputForEffect();
947 if (dstOutput == output) {
948 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
949 }
950 mPreviousOutputs = mOutputs;
951 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700952 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700953 return output;
954 }
955
Eric Laurentb732cf52014-09-24 19:08:21 -0700956non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700957 // ignoring channel mask due to downmix capability in mixer
958
959 // open a non direct output
960
961 // for non direct outputs, only PCM is supported
962 if (audio_is_linear_pcm(format)) {
963 // get which output is suitable for the specified stream. The actual
964 // routing change will happen when startOutput() will be called
965 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
966
Eric Laurent8838a382014-09-08 16:44:28 -0700967 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
968 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
969 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700970 }
971 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
972 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
973
Paul McLeanaa981192015-03-21 09:55:15 -0700974 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700975
976 return output;
977}
978
Eric Laurente0720872014-03-11 09:30:41 -0700979audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700980 audio_output_flags_t flags,
981 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700982{
983 // select one output among several that provide a path to a particular device or set of
984 // devices (the list was previously build by getOutputsForDevice()).
985 // The priority is as follows:
986 // 1: the output with the highest number of requested policy flags
Eric Laurente6930022016-02-11 10:20:40 -0800987 // 2: the output with the bit depth the closest to the requested one
988 // 3: the primary output
989 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -0700990
991 if (outputs.size() == 0) {
992 return 0;
993 }
994 if (outputs.size() == 1) {
995 return outputs[0];
996 }
997
998 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -0800999 audio_io_handle_t outputForFlags = 0;
1000 audio_io_handle_t outputForPrimary = 0;
1001 audio_io_handle_t outputForFormat = 0;
1002 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1003 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001004
1005 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001006 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001007 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001008 // if a valid format is specified, skip output if not compatible
1009 if (format != AUDIO_FORMAT_INVALID) {
1010 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001011 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001012 continue;
1013 }
1014 } else if (!audio_is_linear_pcm(format)) {
1015 continue;
1016 }
Eric Laurente6930022016-02-11 10:20:40 -08001017 if (AudioPort::isBetterFormatMatch(
1018 outputDesc->mFormat, bestFormat, format)) {
1019 outputForFormat = outputs[i];
1020 bestFormat = outputDesc->mFormat;
1021 }
Eric Laurent8838a382014-09-08 16:44:28 -07001022 }
1023
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001024 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001025 if (commonFlags >= maxCommonFlags) {
1026 if (commonFlags == maxCommonFlags) {
1027 if (AudioPort::isBetterFormatMatch(
1028 outputDesc->mFormat, bestFormatForFlags, format)) {
1029 outputForFlags = outputs[i];
1030 bestFormatForFlags = outputDesc->mFormat;
1031 }
1032 } else {
1033 outputForFlags = outputs[i];
1034 maxCommonFlags = commonFlags;
1035 bestFormatForFlags = outputDesc->mFormat;
1036 }
Eric Laurente552edb2014-03-10 17:42:56 -07001037 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1038 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001039 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001040 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001041 }
1042 }
1043 }
1044
Eric Laurente6930022016-02-11 10:20:40 -08001045 if (outputForFlags != 0) {
1046 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001047 }
Eric Laurente6930022016-02-11 10:20:40 -08001048 if (outputForFormat != 0) {
1049 return outputForFormat;
1050 }
1051 if (outputForPrimary != 0) {
1052 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001053 }
1054
1055 return outputs[0];
1056}
1057
Eric Laurente0720872014-03-11 09:30:41 -07001058status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001059 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001060 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001061{
Paul McLeanaa981192015-03-21 09:55:15 -07001062 ALOGV("startOutput() output %d, stream %d, session %d",
1063 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001064 ssize_t index = mOutputs.indexOfKey(output);
1065 if (index < 0) {
1066 ALOGW("startOutput() unknown output %d", output);
1067 return BAD_VALUE;
1068 }
1069
Eric Laurentc75307b2015-03-17 15:29:32 -07001070 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1071
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001072 // Routing?
1073 mOutputRoutes.incRouteActivity(session);
1074
Eric Laurentc75307b2015-03-17 15:29:32 -07001075 audio_devices_t newDevice;
1076 if (outputDesc->mPolicyMix != NULL) {
1077 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001078 } else if (mOutputRoutes.hasRouteChanged(session)) {
1079 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001080 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001081 } else {
1082 newDevice = AUDIO_DEVICE_NONE;
1083 }
1084
1085 uint32_t delayMs = 0;
1086
Eric Laurentc75307b2015-03-17 15:29:32 -07001087 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1088
1089 if (status != NO_ERROR) {
1090 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001091 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001092 }
1093 // Automatically enable the remote submix input when output is started on a re routing mix
1094 // of type MIX_TYPE_RECORDERS
1095 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1096 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1097 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1098 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1099 outputDesc->mPolicyMix->mRegistrationId,
1100 "remote-submix");
1101 }
1102
1103 if (delayMs != 0) {
1104 usleep(delayMs * 1000);
1105 }
1106
1107 return status;
1108}
1109
1110status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1111 audio_stream_type_t stream,
1112 audio_devices_t device,
1113 uint32_t *delayMs)
1114{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001115 // cannot start playback of STREAM_TTS if any other output is being used
1116 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001117
1118 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001119 if (stream == AUDIO_STREAM_TTS) {
1120 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001121 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001122 return INVALID_OPERATION;
1123 } else {
1124 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1125 }
1126 } else {
1127 // some playback other than beacon starts
1128 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1129 }
1130
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001131 // check active before incrementing usage count
1132 bool force = !outputDesc->isActive();
1133
Eric Laurente552edb2014-03-10 17:42:56 -07001134 // increment usage count for this stream on the requested output:
1135 // NOTE that the usage count is the same for duplicated output and hardware output which is
1136 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1137 outputDesc->changeRefCount(stream, 1);
1138
Eric Laurent493404d2015-04-21 15:07:36 -07001139 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001140 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001141 if (device == AUDIO_DEVICE_NONE) {
1142 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001143 }
Eric Laurente552edb2014-03-10 17:42:56 -07001144 routing_strategy strategy = getStrategy(stream);
1145 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001146 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1147 (beaconMuteLatency > 0);
1148 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001149 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001150 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001151 if (desc != outputDesc) {
1152 // force a device change if any other output is managed by the same hw
1153 // module and has a current device selection that differs from selected device.
1154 // In this case, the audio HAL must receive the new device selection so that it can
1155 // change the device currently selected by the other active output.
1156 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001157 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001158 force = true;
1159 }
1160 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001161 // a notification so that audio focus effect can propagate, or that a mute/unmute
1162 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001163 uint32_t latency = desc->latency();
1164 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1165 waitMs = latency;
1166 }
1167 }
1168 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001169 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001170
1171 // handle special case for sonification while in call
1172 if (isInCall()) {
1173 handleIncallSonification(stream, true, false);
1174 }
1175
1176 // apply volume rules for current stream and device if necessary
1177 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001178 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001179 outputDesc,
1180 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001181
1182 // update the outputs if starting an output with a stream that can affect notification
1183 // routing
1184 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001185
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001186 // force reevaluating accessibility routing when ringtone or alarm starts
1187 if (strategy == STRATEGY_SONIFICATION) {
1188 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1189 }
Eric Laurente552edb2014-03-10 17:42:56 -07001190 }
1191 return NO_ERROR;
1192}
1193
1194
Eric Laurente0720872014-03-11 09:30:41 -07001195status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001196 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001197 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001198{
1199 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1200 ssize_t index = mOutputs.indexOfKey(output);
1201 if (index < 0) {
1202 ALOGW("stopOutput() unknown output %d", output);
1203 return BAD_VALUE;
1204 }
1205
Eric Laurentc75307b2015-03-17 15:29:32 -07001206 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001207
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 if (outputDesc->mRefCount[stream] == 1) {
1209 // Automatically disable the remote submix input when output is stopped on a
1210 // re routing mix of type MIX_TYPE_RECORDERS
1211 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1212 outputDesc->mPolicyMix != NULL &&
1213 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1214 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1215 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1216 outputDesc->mPolicyMix->mRegistrationId,
1217 "remote-submix");
1218 }
1219 }
1220
1221 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001222 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001223 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001224 int activityCount = mOutputRoutes.decRouteActivity(session);
1225 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1226
1227 if (forceDeviceUpdate) {
1228 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1229 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001230 }
1231
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001232 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001233}
1234
1235status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001236 audio_stream_type_t stream,
1237 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001238{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001239 // always handle stream stop, check which stream type is stopping
1240 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1241
Eric Laurente552edb2014-03-10 17:42:56 -07001242 // handle special case for sonification while in call
1243 if (isInCall()) {
1244 handleIncallSonification(stream, false, false);
1245 }
1246
1247 if (outputDesc->mRefCount[stream] > 0) {
1248 // decrement usage count of this stream on the output
1249 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001250
Eric Laurente552edb2014-03-10 17:42:56 -07001251 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001252 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001253 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001254 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001255 // delay the device switch by twice the latency because stopOutput() is executed when
1256 // the track stop() command is received and at that time the audio track buffer can
1257 // still contain data that needs to be drained. The latency only covers the audio HAL
1258 // and kernel buffers. Also the latency does not always include additional delay in the
1259 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001260 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001261
1262 // force restoring the device selection on other active outputs if it differs from the
1263 // one being selected for this output
1264 for (size_t i = 0; i < mOutputs.size(); i++) {
1265 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001266 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001267 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001268 desc->isActive() &&
1269 outputDesc->sharesHwModuleWith(desc) &&
1270 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001271 setOutputDevice(desc,
1272 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001273 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001274 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001275 }
1276 }
1277 // update the outputs if stopping one with a stream that can affect notification routing
1278 handleNotificationRoutingForStream(stream);
1279 }
1280 return NO_ERROR;
1281 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001282 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001283 return INVALID_OPERATION;
1284 }
1285}
1286
Eric Laurente83b55d2014-11-14 10:06:21 -08001287void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001288 audio_stream_type_t stream __unused,
1289 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001290{
1291 ALOGV("releaseOutput() %d", output);
1292 ssize_t index = mOutputs.indexOfKey(output);
1293 if (index < 0) {
1294 ALOGW("releaseOutput() releasing unknown output %d", output);
1295 return;
1296 }
1297
1298#ifdef AUDIO_POLICY_TEST
1299 int testIndex = testOutputIndex(output);
1300 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001301 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001302 if (outputDesc->isActive()) {
1303 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001304 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001305 mTestOutputs[testIndex] = 0;
1306 }
1307 return;
1308 }
1309#endif //AUDIO_POLICY_TEST
1310
Paul McLeanaa981192015-03-21 09:55:15 -07001311 // Routing
1312 mOutputRoutes.removeRoute(session);
1313
Eric Laurentc75307b2015-03-17 15:29:32 -07001314 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001315 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001316 if (desc->mDirectOpenCount <= 0) {
1317 ALOGW("releaseOutput() invalid open count %d for output %d",
1318 desc->mDirectOpenCount, output);
1319 return;
1320 }
1321 if (--desc->mDirectOpenCount == 0) {
1322 closeOutput(output);
1323 // If effects where present on the output, audioflinger moved them to the primary
1324 // output by default: move them back to the appropriate output.
1325 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001326 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001327 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1328 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001329 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001330 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001331 }
1332 }
1333}
1334
1335
Eric Laurentcaf7f482014-11-25 17:50:47 -08001336status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1337 audio_io_handle_t *input,
1338 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001339 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001340 uint32_t samplingRate,
1341 audio_format_t format,
1342 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001343 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001344 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001345 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001346{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001347 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1348 "session %d, flags %#x",
1349 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001350
Eric Laurentcaf7f482014-11-25 17:50:47 -08001351 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001352 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001353 audio_devices_t device;
1354 // handle legacy remote submix case where the address was not always specified
1355 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001356 audio_source_t inputSource = attr->source;
1357 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001358 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001359
Eric Laurentc447ded2015-01-06 08:47:05 -08001360 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1361 inputSource = AUDIO_SOURCE_MIC;
1362 }
1363 halInputSource = inputSource;
1364
Paul McLean466dc8e2015-04-17 13:15:36 -06001365 // Explicit routing?
1366 sp<DeviceDescriptor> deviceDesc;
1367 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1368 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1369 deviceDesc = mAvailableInputDevices[i];
1370 break;
1371 }
1372 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001373 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001374
Eric Laurentc447ded2015-01-06 08:47:05 -08001375 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001376 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001377 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001378 if (ret != NO_ERROR) {
1379 return ret;
1380 }
1381 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001382 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1383 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001384 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001385 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001386 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001387 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001388 return BAD_VALUE;
1389 }
Eric Laurentc722f302014-12-10 11:21:49 -08001390 if (policyMix != NULL) {
1391 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001392 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1393 // there is an external policy, but this input is attached to a mix of recorders,
1394 // meaning it receives audio injected into the framework, so the recorder doesn't
1395 // know about it and is therefore considered "legacy"
1396 *inputType = API_INPUT_LEGACY;
1397 } else {
1398 // recording a mix of players defined by an external policy, we're rerouting for
1399 // an external policy
1400 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1401 }
Eric Laurentc722f302014-12-10 11:21:49 -08001402 } else if (audio_is_remote_submix_device(device)) {
1403 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001404 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001405 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1406 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001407 } else {
1408 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001409 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001410
Eric Laurent599c7582015-12-07 18:05:55 -08001411 }
1412
1413 *input = getInputForDevice(device, address, session, uid, inputSource,
1414 samplingRate, format, channelMask, flags,
1415 policyMix);
1416 if (*input == AUDIO_IO_HANDLE_NONE) {
1417 mInputRoutes.removeRoute(session);
1418 return INVALID_OPERATION;
1419 }
1420 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1421 return NO_ERROR;
1422}
1423
1424
1425audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1426 String8 address,
1427 audio_session_t session,
1428 uid_t uid,
1429 audio_source_t inputSource,
1430 uint32_t samplingRate,
1431 audio_format_t format,
1432 audio_channel_mask_t channelMask,
1433 audio_input_flags_t flags,
1434 AudioMix *policyMix)
1435{
1436 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1437 audio_source_t halInputSource = inputSource;
1438 bool isSoundTrigger = false;
1439
1440 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1441 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1442 if (index >= 0) {
1443 input = mSoundTriggerSessions.valueFor(session);
1444 isSoundTrigger = true;
1445 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1446 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1447 } else {
1448 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001449 }
1450 }
1451
Andy Hungf129b032015-04-07 13:45:50 -07001452 // find a compatible input profile (not necessarily identical in parameters)
1453 sp<IOProfile> profile;
1454 // samplingRate and flags may be updated by getInputProfile
Glenn Kasten05ddca52016-02-11 08:17:12 -08001455 uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate;
Andy Hungf129b032015-04-07 13:45:50 -07001456 audio_format_t profileFormat = format;
1457 audio_channel_mask_t profileChannelMask = channelMask;
1458 audio_input_flags_t profileFlags = flags;
1459 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001460 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001461 profileSamplingRate, profileFormat, profileChannelMask,
1462 profileFlags);
1463 if (profile != 0) {
1464 break; // success
1465 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1466 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1467 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001468 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1469 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001470 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001471 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001472 }
Eric Laurente552edb2014-03-10 17:42:56 -07001473 }
Glenn Kasten05ddca52016-02-11 08:17:12 -08001474 // Pick input sampling rate if not specified by client
1475 if (samplingRate == 0) {
1476 samplingRate = profileSamplingRate;
1477 }
Eric Laurente552edb2014-03-10 17:42:56 -07001478
Eric Laurent322b4d22015-04-03 15:57:54 -07001479 if (profile->getModuleHandle() == 0) {
1480 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001481 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001482 }
1483
Eric Laurent599c7582015-12-07 18:05:55 -08001484 sp<AudioSession> audioSession = new AudioSession(session,
1485 inputSource,
1486 format,
1487 samplingRate,
1488 channelMask,
1489 flags,
1490 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001491 isSoundTrigger,
1492 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001493
Eric Laurent232f26f2016-02-17 18:06:27 -08001494// TODO enable input reuse
1495#if 0
Eric Laurent599c7582015-12-07 18:05:55 -08001496 // reuse an open input if possible
1497 for (size_t i = 0; i < mInputs.size(); i++) {
1498 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurent232f26f2016-02-17 18:06:27 -08001499 // reuse input if it shares the same profile and same sound trigger attribute
1500 if (profile == desc->mProfile &&
1501 isSoundTrigger == desc->isSoundTrigger()) {
Eric Laurent599c7582015-12-07 18:05:55 -08001502
1503 sp<AudioSession> as = desc->getAudioSession(session);
1504 if (as != 0) {
1505 // do not allow unmatching properties on same session
1506 if (as->matches(audioSession)) {
1507 as->changeOpenCount(1);
1508 } else {
1509 ALOGW("getInputForDevice() record with different attributes"
1510 " exists for session %d", session);
Eric Laurent232f26f2016-02-17 18:06:27 -08001511 return input;
Eric Laurent599c7582015-12-07 18:05:55 -08001512 }
1513 } else {
1514 desc->addAudioSession(session, audioSession);
1515 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001516 ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i));
1517 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001518 }
1519 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001520#endif
Eric Laurent599c7582015-12-07 18:05:55 -08001521
Eric Laurentcf2c0212014-07-25 16:20:43 -07001522 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001523 config.sample_rate = profileSamplingRate;
1524 config.channel_mask = profileChannelMask;
1525 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001526
Eric Laurent322b4d22015-04-03 15:57:54 -07001527 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001528 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001529 &config,
1530 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001531 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001532 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001533 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001534
1535 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001536 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001537 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001538 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001539 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001540 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1541 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001542 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001543 if (input != AUDIO_IO_HANDLE_NONE) {
1544 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001545 }
Eric Laurent599c7582015-12-07 18:05:55 -08001546 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001547 }
1548
Eric Laurent1f2f2232014-06-02 12:01:23 -07001549 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001550 inputDesc->mSamplingRate = profileSamplingRate;
1551 inputDesc->mFormat = profileFormat;
1552 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001553 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001554 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001555 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001556
Eric Laurent599c7582015-12-07 18:05:55 -08001557 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001558 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001559
Eric Laurent599c7582015-12-07 18:05:55 -08001560 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001561}
1562
Eric Laurent4dc68062014-07-28 17:26:49 -07001563status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurent232f26f2016-02-17 18:06:27 -08001564 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001565{
1566 ALOGV("startInput() input %d", input);
1567 ssize_t index = mInputs.indexOfKey(input);
1568 if (index < 0) {
1569 ALOGW("startInput() unknown input %d", input);
1570 return BAD_VALUE;
1571 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001572 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001573
Eric Laurent599c7582015-12-07 18:05:55 -08001574 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1575 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001576 ALOGW("startInput() unknown session %d on input %d", session, input);
1577 return BAD_VALUE;
1578 }
1579
Eric Laurent232f26f2016-02-17 18:06:27 -08001580 // virtual input devices are compatible with other input devices
1581 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001582
Eric Laurent232f26f2016-02-17 18:06:27 -08001583 // for a non-virtual input device, check if there is another (non-virtual) active input
1584 audio_io_handle_t activeInput = mInputs.getActiveInput();
1585 if (activeInput != 0 && activeInput != input) {
Eric Laurente552edb2014-03-10 17:42:56 -07001586
Eric Laurent232f26f2016-02-17 18:06:27 -08001587 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1588 // otherwise the active input continues and the new input cannot be started.
1589 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
1590 if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) &&
1591 !activeDesc->hasPreemptedSession(session)) {
1592 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
1593 //FIXME: consider all active sessions
1594 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions();
1595 audio_session_t activeSession = activeSessions.keyAt(0);
1596 SortedVector<audio_session_t> sessions =
1597 activeDesc->getPreemptedSessions();
1598 sessions.add(activeSession);
1599 inputDesc->setPreemptedSessions(sessions);
1600 stopInput(activeInput, activeSession);
1601 releaseInput(activeInput, activeSession);
1602 } else {
1603 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
1604 return INVALID_OPERATION;
1605 }
1606 }
1607
1608 // Do not allow capture if an active voice call is using a software patch and
1609 // the call TX source device is on the same HW module.
1610 // FIXME: would be better to refine to only inputs whose profile connects to the
1611 // call TX device but this information is not in the audio patch
1612 if (mCallTxPatch != 0 &&
1613 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1614 return INVALID_OPERATION;
1615 }
1616 }
Eric Laurent313d1e72016-01-29 09:56:57 -08001617
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001618 // Routing?
1619 mInputRoutes.incRouteActivity(session);
1620
Eric Laurent232f26f2016-02-17 18:06:27 -08001621 if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) {
1622 // if input maps to a dynamic policy with an activity listener, notify of state change
1623 if ((inputDesc->mPolicyMix != NULL)
1624 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1625 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1626 MIX_STATE_MIXING);
1627 }
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001628
Eric Laurent232f26f2016-02-17 18:06:27 -08001629 if (mInputs.activeInputsCount() == 0) {
1630 SoundTrigger::setCaptureState(true);
1631 }
1632 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001633
Eric Laurent232f26f2016-02-17 18:06:27 -08001634 // automatically enable the remote submix output when input is started if not
1635 // used by a policy mix of type MIX_TYPE_RECORDERS
1636 // For remote submix (a virtual device), we open only one input per capture request.
1637 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1638 String8 address = String8("");
1639 if (inputDesc->mPolicyMix == NULL) {
1640 address = String8("0");
1641 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1642 address = inputDesc->mPolicyMix->mRegistrationId;
Eric Laurentc722f302014-12-10 11:21:49 -08001643 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001644 if (address != "") {
1645 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1646 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1647 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001648 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001649 }
Eric Laurente552edb2014-03-10 17:42:56 -07001650 }
1651
Eric Laurent599c7582015-12-07 18:05:55 -08001652 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001653
Eric Laurent232f26f2016-02-17 18:06:27 -08001654 audioSession->changeActiveCount(1);
Eric Laurente552edb2014-03-10 17:42:56 -07001655 return NO_ERROR;
1656}
1657
Eric Laurent4dc68062014-07-28 17:26:49 -07001658status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1659 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001660{
1661 ALOGV("stopInput() input %d", input);
1662 ssize_t index = mInputs.indexOfKey(input);
1663 if (index < 0) {
1664 ALOGW("stopInput() unknown input %d", input);
1665 return BAD_VALUE;
1666 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001667 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001668
Eric Laurent599c7582015-12-07 18:05:55 -08001669 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001670 if (index < 0) {
1671 ALOGW("stopInput() unknown session %d on input %d", session, input);
1672 return BAD_VALUE;
1673 }
1674
Eric Laurent599c7582015-12-07 18:05:55 -08001675 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001676 ALOGW("stopInput() input %d already stopped", input);
1677 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001678 }
1679
Eric Laurent599c7582015-12-07 18:05:55 -08001680 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001681
1682 // Routing?
1683 mInputRoutes.decRouteActivity(session);
1684
Eric Laurent232f26f2016-02-17 18:06:27 -08001685 if (!inputDesc->isActive()) {
1686 // if input maps to a dynamic policy with an activity listener, notify of state change
1687 if ((inputDesc->mPolicyMix != NULL)
1688 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1689 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1690 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001691 }
Eric Laurent232f26f2016-02-17 18:06:27 -08001692
1693 // automatically disable the remote submix output when input is stopped if not
1694 // used by a policy mix of type MIX_TYPE_RECORDERS
1695 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1696 String8 address = String8("");
1697 if (inputDesc->mPolicyMix == NULL) {
1698 address = String8("0");
1699 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1700 address = inputDesc->mPolicyMix->mRegistrationId;
1701 }
1702 if (address != "") {
1703 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1704 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1705 address, "remote-submix");
1706 }
1707 }
1708
1709 resetInputDevice(input);
1710
1711 if (mInputs.activeInputsCount() == 0) {
1712 SoundTrigger::setCaptureState(false);
1713 }
1714 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001715 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001716 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001717}
1718
Eric Laurent4dc68062014-07-28 17:26:49 -07001719void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1720 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001721{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001722
Eric Laurente552edb2014-03-10 17:42:56 -07001723 ALOGV("releaseInput() %d", input);
1724 ssize_t index = mInputs.indexOfKey(input);
1725 if (index < 0) {
1726 ALOGW("releaseInput() releasing unknown input %d", input);
1727 return;
1728 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001729
1730 // Routing
1731 mInputRoutes.removeRoute(session);
1732
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001733 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1734 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001735
Eric Laurent599c7582015-12-07 18:05:55 -08001736 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001737 if (index < 0) {
1738 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1739 return;
1740 }
Eric Laurent599c7582015-12-07 18:05:55 -08001741
1742 if (audioSession->openCount() == 0) {
1743 ALOGW("releaseInput() invalid open count %d on session %d",
1744 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001745 return;
1746 }
Eric Laurent599c7582015-12-07 18:05:55 -08001747
1748 if (audioSession->changeOpenCount(-1) == 0) {
1749 inputDesc->removeAudioSession(session);
1750 }
1751
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001752 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001753 ALOGV("releaseInput() exit > 0");
1754 return;
1755 }
1756
Eric Laurent05b90f82014-08-27 15:32:29 -07001757 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001758 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001759 ALOGV("releaseInput() exit");
1760}
1761
Eric Laurentd4692962014-05-05 18:13:44 -07001762void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001763 bool patchRemoved = false;
1764
Eric Laurentd4692962014-05-05 18:13:44 -07001765 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001766 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001767 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07001768 if (patch_index >= 0) {
1769 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1770 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1771 mAudioPatches.removeItemsAt(patch_index);
1772 patchRemoved = true;
1773 }
Eric Laurentd4692962014-05-05 18:13:44 -07001774 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1775 }
1776 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001777 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001778 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001779
1780 if (patchRemoved) {
1781 mpClientInterface->onAudioPatchListUpdate();
1782 }
Eric Laurentd4692962014-05-05 18:13:44 -07001783}
1784
Eric Laurente0720872014-03-11 09:30:41 -07001785void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001786 int indexMin,
1787 int indexMax)
1788{
1789 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001790 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent28d09f02016-03-08 10:43:05 -08001791
1792 // initialize other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001793 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1794 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001795 continue;
1796 }
1797 mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001798 }
Eric Laurente552edb2014-03-10 17:42:56 -07001799}
1800
Eric Laurente0720872014-03-11 09:30:41 -07001801status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001802 int index,
1803 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001804{
1805
François Gaffied1ab2bd2015-12-02 18:20:06 +01001806 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1807 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001808 return BAD_VALUE;
1809 }
1810 if (!audio_is_output_device(device)) {
1811 return BAD_VALUE;
1812 }
1813
1814 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001815 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001816
1817 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1818 stream, device, index);
1819
1820 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1821 // clear all device specific values
1822 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001823 mVolumeCurves->clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001824 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001825
1826 // update other private stream volumes which follow this one
Eric Laurent794fde22016-03-11 09:50:45 -08001827 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1828 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001829 continue;
1830 }
1831 mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index);
1832 }
Eric Laurente552edb2014-03-10 17:42:56 -07001833
Eric Laurent31551f82014-10-10 18:21:56 -07001834 // update volume on all outputs whose current device is also selected by the same
1835 // strategy as the device specified by the caller
Eric Laurente552edb2014-03-10 17:42:56 -07001836 status_t status = NO_ERROR;
1837 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001838 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1839 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent794fde22016-03-11 09:50:45 -08001840 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
1841 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001842 continue;
Eric Laurente552edb2014-03-10 17:42:56 -07001843 }
Eric Laurent794fde22016-03-11 09:50:45 -08001844 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08001845 audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, true /*fromCache*/);
1846 // it is possible that the requested device is not selected by the strategy
1847 // (e.g an explicit audio patch is active causing getDevicesForStream()
1848 // to return this device. We must make sure that the device passed is part of the
1849 // devices considered when applying volume below.
1850 curStreamDevice |= device;
1851
1852 if (((device == AUDIO_DEVICE_OUT_DEFAULT) ||
1853 ((curDevice & curStreamDevice) != 0))) {
1854 status_t volStatus =
1855 checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
1856 if (volStatus != NO_ERROR) {
1857 status = volStatus;
1858 }
1859 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001860 }
Eric Laurente552edb2014-03-10 17:42:56 -07001861 }
1862 return status;
1863}
1864
Eric Laurente0720872014-03-11 09:30:41 -07001865status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001866 int *index,
1867 audio_devices_t device)
1868{
1869 if (index == NULL) {
1870 return BAD_VALUE;
1871 }
1872 if (!audio_is_output_device(device)) {
1873 return BAD_VALUE;
1874 }
1875 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1876 // the strategy the stream belongs to.
1877 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1878 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1879 }
François Gaffiedfd74092015-03-19 12:10:59 +01001880 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001881
François Gaffied1ab2bd2015-12-02 18:20:06 +01001882 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001883 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1884 return NO_ERROR;
1885}
1886
Eric Laurente0720872014-03-11 09:30:41 -07001887audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001888 const SortedVector<audio_io_handle_t>& outputs)
1889{
1890 // select one output among several suitable for global effects.
1891 // The priority is as follows:
1892 // 1: An offloaded output. If the effect ends up not being offloadable,
1893 // AudioFlinger will invalidate the track and the offloaded output
1894 // will be closed causing the effect to be moved to a PCM output.
1895 // 2: A deep buffer output
1896 // 3: the first output in the list
1897
1898 if (outputs.size() == 0) {
1899 return 0;
1900 }
1901
1902 audio_io_handle_t outputOffloaded = 0;
1903 audio_io_handle_t outputDeepBuffer = 0;
1904
1905 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001906 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001907 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001908 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1909 outputOffloaded = outputs[i];
1910 }
1911 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1912 outputDeepBuffer = outputs[i];
1913 }
1914 }
1915
1916 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1917 outputOffloaded, outputDeepBuffer);
1918 if (outputOffloaded != 0) {
1919 return outputOffloaded;
1920 }
1921 if (outputDeepBuffer != 0) {
1922 return outputDeepBuffer;
1923 }
1924
1925 return outputs[0];
1926}
1927
Eric Laurente0720872014-03-11 09:30:41 -07001928audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001929{
1930 // apply simple rule where global effects are attached to the same output as MUSIC streams
1931
Eric Laurent3b73df72014-03-11 09:06:29 -07001932 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001933 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1934 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1935
1936 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1937 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1938 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1939
1940 return output;
1941}
1942
Eric Laurente0720872014-03-11 09:30:41 -07001943status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001944 audio_io_handle_t io,
1945 uint32_t strategy,
1946 int session,
1947 int id)
1948{
1949 ssize_t index = mOutputs.indexOfKey(io);
1950 if (index < 0) {
1951 index = mInputs.indexOfKey(io);
1952 if (index < 0) {
1953 ALOGW("registerEffect() unknown io %d", io);
1954 return INVALID_OPERATION;
1955 }
1956 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001957 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001958}
1959
Eric Laurentc75307b2015-03-17 15:29:32 -07001960bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1961{
Eric Laurent28d09f02016-03-08 10:43:05 -08001962 bool active = false;
Eric Laurent794fde22016-03-11 09:50:45 -08001963 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) {
1964 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08001965 continue;
1966 }
1967 active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs);
1968 }
Eric Laurent28d09f02016-03-08 10:43:05 -08001969 return active;
Eric Laurentc75307b2015-03-17 15:29:32 -07001970}
1971
1972bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1973{
1974 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1975}
1976
Eric Laurente0720872014-03-11 09:30:41 -07001977bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001978{
1979 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001980 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001981 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001982 return true;
1983 }
1984 }
1985 return false;
1986}
1987
Eric Laurent275e8e92014-11-30 15:14:47 -08001988// Register a list of custom mixes with their attributes and format.
1989// When a mix is registered, corresponding input and output profiles are
1990// added to the remote submix hw module. The profile contains only the
1991// parameters (sampling rate, format...) specified by the mix.
1992// The corresponding input remote submix device is also connected.
1993//
1994// When a remote submix device is connected, the address is checked to select the
1995// appropriate profile and the corresponding input or output stream is opened.
1996//
1997// When capture starts, getInputForAttr() will:
1998// - 1 look for a mix matching the address passed in attribtutes tags if any
1999// - 2 if none found, getDeviceForInputSource() will:
2000// - 2.1 look for a mix matching the attributes source
2001// - 2.2 if none found, default to device selection by policy rules
2002// At this time, the corresponding output remote submix device is also connected
2003// and active playback use cases can be transferred to this mix if needed when reconnecting
2004// after AudioTracks are invalidated
2005//
2006// When playback starts, getOutputForAttr() will:
2007// - 1 look for a mix matching the address passed in attribtutes tags if any
2008// - 2 if none found, look for a mix matching the attributes usage
2009// - 3 if none found, default to device and output selection by policy rules.
2010
2011status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2012{
2013 sp<HwModule> module;
2014 for (size_t i = 0; i < mHwModules.size(); i++) {
2015 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2016 mHwModules[i]->mHandle != 0) {
2017 module = mHwModules[i];
2018 break;
2019 }
2020 }
2021
2022 if (module == 0) {
2023 return INVALID_OPERATION;
2024 }
2025
Eric Laurent7b279bb2015-12-14 10:18:23 -08002026 ALOGV("registerPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002027
2028 for (size_t i = 0; i < mixes.size(); i++) {
2029 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002030
2031 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002032 continue;
2033 }
2034 audio_config_t outputConfig = mixes[i].mFormat;
2035 audio_config_t inputConfig = mixes[i].mFormat;
2036 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2037 // stereo and let audio flinger do the channel conversion if needed.
2038 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2039 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2040 module->addOutputProfile(address, &outputConfig,
2041 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2042 module->addInputProfile(address, &inputConfig,
2043 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002044
Eric Laurentc722f302014-12-10 11:21:49 -08002045 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002046 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002047 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002048 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002049 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002050 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002051 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002052 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002053 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002054 }
2055 return NO_ERROR;
2056}
2057
2058status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2059{
2060 sp<HwModule> module;
2061 for (size_t i = 0; i < mHwModules.size(); i++) {
2062 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2063 mHwModules[i]->mHandle != 0) {
2064 module = mHwModules[i];
2065 break;
2066 }
2067 }
2068
2069 if (module == 0) {
2070 return INVALID_OPERATION;
2071 }
2072
Eric Laurent7b279bb2015-12-14 10:18:23 -08002073 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002074
2075 for (size_t i = 0; i < mixes.size(); i++) {
2076 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002077
2078 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002079 continue;
2080 }
2081
Eric Laurentc722f302014-12-10 11:21:49 -08002082 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2083 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2084 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002085 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002086 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002087 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002088 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002089
2090 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2091 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2092 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002093 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002094 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002095 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002096 }
2097 module->removeOutputProfile(address);
2098 module->removeInputProfile(address);
2099 }
2100 return NO_ERROR;
2101}
2102
Eric Laurente552edb2014-03-10 17:42:56 -07002103
Eric Laurente0720872014-03-11 09:30:41 -07002104status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002105{
2106 const size_t SIZE = 256;
2107 char buffer[SIZE];
2108 String8 result;
2109
2110 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2111 result.append(buffer);
2112
Eric Laurent87ffa392015-05-22 10:32:38 -07002113 snprintf(buffer, SIZE, " Primary Output: %d\n",
2114 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002115 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002116 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002117 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002118 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002119 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002120 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002121 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002122 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002123 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002124 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002125 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002126 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002127 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002128 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002129 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002130 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002131 result.append(buffer);
Phil Burk09bc4612016-02-24 15:58:15 -08002132 snprintf(buffer, SIZE, " Force use for encoded surround output %d\n",
2133 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND));
2134 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002135 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2136 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002137 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2138 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002139
François Gaffie2110e042015-03-24 08:41:51 +01002140 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002141
François Gaffie112b0af2015-11-19 16:13:25 +01002142 mAvailableOutputDevices.dump(fd, String8("Available output"));
2143 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002144 mHwModules.dump(fd);
2145 mOutputs.dump(fd);
2146 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002147 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002148 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002149 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002150
2151 return NO_ERROR;
2152}
2153
2154// This function checks for the parameters which can be offloaded.
2155// This can be enhanced depending on the capability of the DSP and policy
2156// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002157bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002158{
2159 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002160 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002161 offloadInfo.sample_rate, offloadInfo.channel_mask,
2162 offloadInfo.format,
2163 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2164 offloadInfo.has_video);
2165
Andy Hung2ddee192015-12-18 17:34:44 -08002166 if (mMasterMono) {
2167 return false; // no offloading if mono is set.
2168 }
2169
Eric Laurente552edb2014-03-10 17:42:56 -07002170 // Check if offload has been disabled
2171 char propValue[PROPERTY_VALUE_MAX];
2172 if (property_get("audio.offload.disable", propValue, "0")) {
2173 if (atoi(propValue) != 0) {
2174 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2175 return false;
2176 }
2177 }
2178
2179 // Check if stream type is music, then only allow offload as of now.
2180 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2181 {
2182 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2183 return false;
2184 }
2185
2186 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002187 const bool allowOffloadWithVideo =
2188 property_get_bool("audio.offload.video", false /* default_value */);
2189 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002190 ALOGV("isOffloadSupported: has_video == true, returning false");
2191 return false;
2192 }
2193
2194 //If duration is less than minimum value defined in property, return false
2195 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2196 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2197 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2198 return false;
2199 }
2200 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2201 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2202 return false;
2203 }
2204
2205 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2206 // creating an offloaded track and tearing it down immediately after start when audioflinger
2207 // detects there is an active non offloadable effect.
2208 // FIXME: We should check the audio session here but we do not have it in this context.
2209 // This may prevent offloading in rare situations where effects are left active by apps
2210 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002211 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002212 return false;
2213 }
2214
2215 // See if there is a profile to support this.
2216 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002217 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002218 offloadInfo.sample_rate,
2219 offloadInfo.format,
2220 offloadInfo.channel_mask,
2221 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002222 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2223 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002224}
2225
Eric Laurent6a94d692014-05-20 11:18:06 -07002226status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2227 audio_port_type_t type,
2228 unsigned int *num_ports,
2229 struct audio_port *ports,
2230 unsigned int *generation)
2231{
2232 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2233 generation == NULL) {
2234 return BAD_VALUE;
2235 }
2236 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2237 if (ports == NULL) {
2238 *num_ports = 0;
2239 }
2240
2241 size_t portsWritten = 0;
2242 size_t portsMax = *num_ports;
2243 *num_ports = 0;
2244 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2245 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2246 for (size_t i = 0;
2247 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2248 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2249 }
2250 *num_ports += mAvailableOutputDevices.size();
2251 }
2252 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2253 for (size_t i = 0;
2254 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2255 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2256 }
2257 *num_ports += mAvailableInputDevices.size();
2258 }
2259 }
2260 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2261 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2262 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2263 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2264 }
2265 *num_ports += mInputs.size();
2266 }
2267 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002268 size_t numOutputs = 0;
2269 for (size_t i = 0; i < mOutputs.size(); i++) {
2270 if (!mOutputs[i]->isDuplicated()) {
2271 numOutputs++;
2272 if (portsWritten < portsMax) {
2273 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2274 }
2275 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002276 }
Eric Laurent84c70242014-06-23 08:46:27 -07002277 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002278 }
2279 }
2280 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002281 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002282 return NO_ERROR;
2283}
2284
2285status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2286{
2287 return NO_ERROR;
2288}
2289
Eric Laurent6a94d692014-05-20 11:18:06 -07002290status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2291 audio_patch_handle_t *handle,
2292 uid_t uid)
2293{
2294 ALOGV("createAudioPatch()");
2295
2296 if (handle == NULL || patch == NULL) {
2297 return BAD_VALUE;
2298 }
2299 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2300
Eric Laurent874c42872014-08-08 15:13:39 -07002301 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2302 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2303 return BAD_VALUE;
2304 }
2305 // only one source per audio patch supported for now
2306 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002307 return INVALID_OPERATION;
2308 }
Eric Laurent874c42872014-08-08 15:13:39 -07002309
2310 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002311 return INVALID_OPERATION;
2312 }
Eric Laurent874c42872014-08-08 15:13:39 -07002313 for (size_t i = 0; i < patch->num_sinks; i++) {
2314 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2315 return INVALID_OPERATION;
2316 }
2317 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002318
2319 sp<AudioPatch> patchDesc;
2320 ssize_t index = mAudioPatches.indexOfKey(*handle);
2321
Eric Laurent6a94d692014-05-20 11:18:06 -07002322 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2323 patch->sources[0].role,
2324 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002325#if LOG_NDEBUG == 0
2326 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002327 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002328 patch->sinks[i].role,
2329 patch->sinks[i].type);
2330 }
2331#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002332
2333 if (index >= 0) {
2334 patchDesc = mAudioPatches.valueAt(index);
2335 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2336 mUidCached, patchDesc->mUid, uid);
2337 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2338 return INVALID_OPERATION;
2339 }
2340 } else {
2341 *handle = 0;
2342 }
2343
2344 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002345 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002346 if (outputDesc == NULL) {
2347 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2348 return BAD_VALUE;
2349 }
Eric Laurent84c70242014-06-23 08:46:27 -07002350 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2351 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002352 if (patchDesc != 0) {
2353 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2354 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2355 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2356 return BAD_VALUE;
2357 }
2358 }
Eric Laurent874c42872014-08-08 15:13:39 -07002359 DeviceVector devices;
2360 for (size_t i = 0; i < patch->num_sinks; i++) {
2361 // Only support mix to devices connection
2362 // TODO add support for mix to mix connection
2363 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2364 ALOGV("createAudioPatch() source mix but sink is not a device");
2365 return INVALID_OPERATION;
2366 }
2367 sp<DeviceDescriptor> devDesc =
2368 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2369 if (devDesc == 0) {
2370 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2371 return BAD_VALUE;
2372 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002373
François Gaffie53615e22015-03-19 09:24:12 +01002374 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002375 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002376 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002377 NULL, // updatedSamplingRate
2378 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002379 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002380 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002381 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002382 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002383 ALOGV("createAudioPatch() profile not supported for device %08x",
2384 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002385 return INVALID_OPERATION;
2386 }
2387 devices.add(devDesc);
2388 }
2389 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002390 return INVALID_OPERATION;
2391 }
Eric Laurent874c42872014-08-08 15:13:39 -07002392
Eric Laurent6a94d692014-05-20 11:18:06 -07002393 // TODO: reconfigure output format and channels here
2394 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002395 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002396 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002397 index = mAudioPatches.indexOfKey(*handle);
2398 if (index >= 0) {
2399 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2400 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2401 }
2402 patchDesc = mAudioPatches.valueAt(index);
2403 patchDesc->mUid = uid;
2404 ALOGV("createAudioPatch() success");
2405 } else {
2406 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2407 return INVALID_OPERATION;
2408 }
2409 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2410 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2411 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002412 // only one sink supported when connecting an input device to a mix
2413 if (patch->num_sinks > 1) {
2414 return INVALID_OPERATION;
2415 }
François Gaffie53615e22015-03-19 09:24:12 +01002416 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002417 if (inputDesc == NULL) {
2418 return BAD_VALUE;
2419 }
2420 if (patchDesc != 0) {
2421 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2422 return BAD_VALUE;
2423 }
2424 }
2425 sp<DeviceDescriptor> devDesc =
2426 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2427 if (devDesc == 0) {
2428 return BAD_VALUE;
2429 }
2430
François Gaffie53615e22015-03-19 09:24:12 +01002431 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002432 devDesc->mAddress,
2433 patch->sinks[0].sample_rate,
2434 NULL, /*updatedSampleRate*/
2435 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002436 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002437 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002438 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002439 // FIXME for the parameter type,
2440 // and the NONE
2441 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002442 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002443 return INVALID_OPERATION;
2444 }
2445 // TODO: reconfigure output format and channels here
2446 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002447 devDesc->type(), inputDesc->mIoHandle);
2448 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002449 index = mAudioPatches.indexOfKey(*handle);
2450 if (index >= 0) {
2451 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2452 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2453 }
2454 patchDesc = mAudioPatches.valueAt(index);
2455 patchDesc->mUid = uid;
2456 ALOGV("createAudioPatch() success");
2457 } else {
2458 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2459 return INVALID_OPERATION;
2460 }
2461 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2462 // device to device connection
2463 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002464 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002465 return BAD_VALUE;
2466 }
2467 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002468 sp<DeviceDescriptor> srcDeviceDesc =
2469 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002470 if (srcDeviceDesc == 0) {
2471 return BAD_VALUE;
2472 }
Eric Laurent874c42872014-08-08 15:13:39 -07002473
Eric Laurent6a94d692014-05-20 11:18:06 -07002474 //update source and sink with our own data as the data passed in the patch may
2475 // be incomplete.
2476 struct audio_patch newPatch = *patch;
2477 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002478
Eric Laurent874c42872014-08-08 15:13:39 -07002479 for (size_t i = 0; i < patch->num_sinks; i++) {
2480 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2481 ALOGV("createAudioPatch() source device but one sink is not a device");
2482 return INVALID_OPERATION;
2483 }
2484
2485 sp<DeviceDescriptor> sinkDeviceDesc =
2486 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2487 if (sinkDeviceDesc == 0) {
2488 return BAD_VALUE;
2489 }
2490 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2491
Eric Laurent3bcf8592015-04-03 12:13:24 -07002492 // create a software bridge in PatchPanel if:
2493 // - source and sink devices are on differnt HW modules OR
2494 // - audio HAL version is < 3.0
Eric Laurent232f26f2016-02-17 18:06:27 -08002495 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002496 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002497 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002498 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002499 return INVALID_OPERATION;
2500 }
Eric Laurent874c42872014-08-08 15:13:39 -07002501 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002502 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002503 // if the sink device is reachable via an opened output stream, request to go via
2504 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002505 audio_io_handle_t output = selectOutput(outputs,
2506 AUDIO_OUTPUT_FLAG_NONE,
2507 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002508 if (output != AUDIO_IO_HANDLE_NONE) {
2509 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2510 if (outputDesc->isDuplicated()) {
2511 return INVALID_OPERATION;
2512 }
2513 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002514 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002515 newPatch.num_sources = 2;
2516 }
Eric Laurent83b88082014-06-20 18:31:16 -07002517 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002518 }
2519 // TODO: check from routing capabilities in config file and other conflicting patches
2520
2521 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2522 if (index >= 0) {
2523 afPatchHandle = patchDesc->mAfPatchHandle;
2524 }
2525
2526 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2527 &afPatchHandle,
2528 0);
2529 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2530 status, afPatchHandle);
2531 if (status == NO_ERROR) {
2532 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002533 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002534 addAudioPatch(patchDesc->mHandle, patchDesc);
2535 } else {
2536 patchDesc->mPatch = newPatch;
2537 }
2538 patchDesc->mAfPatchHandle = afPatchHandle;
2539 *handle = patchDesc->mHandle;
2540 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002541 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002542 } else {
2543 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2544 status);
2545 return INVALID_OPERATION;
2546 }
2547 } else {
2548 return BAD_VALUE;
2549 }
2550 } else {
2551 return BAD_VALUE;
2552 }
2553 return NO_ERROR;
2554}
2555
2556status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2557 uid_t uid)
2558{
2559 ALOGV("releaseAudioPatch() patch %d", handle);
2560
2561 ssize_t index = mAudioPatches.indexOfKey(handle);
2562
2563 if (index < 0) {
2564 return BAD_VALUE;
2565 }
2566 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2567 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2568 mUidCached, patchDesc->mUid, uid);
2569 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2570 return INVALID_OPERATION;
2571 }
2572
2573 struct audio_patch *patch = &patchDesc->mPatch;
2574 patchDesc->mUid = mUidCached;
2575 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002576 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002577 if (outputDesc == NULL) {
2578 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2579 return BAD_VALUE;
2580 }
2581
Eric Laurentc75307b2015-03-17 15:29:32 -07002582 setOutputDevice(outputDesc,
2583 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002584 true,
2585 0,
2586 NULL);
2587 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2588 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002589 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002590 if (inputDesc == NULL) {
2591 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2592 return BAD_VALUE;
2593 }
2594 setInputDevice(inputDesc->mIoHandle,
Eric Laurent232f26f2016-02-17 18:06:27 -08002595 getNewInputDevice(inputDesc->mIoHandle),
Eric Laurent6a94d692014-05-20 11:18:06 -07002596 true,
2597 NULL);
2598 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2599 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2600 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2601 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2602 status, patchDesc->mAfPatchHandle);
2603 removeAudioPatch(patchDesc->mHandle);
2604 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002605 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002606 } else {
2607 return BAD_VALUE;
2608 }
2609 } else {
2610 return BAD_VALUE;
2611 }
2612 return NO_ERROR;
2613}
2614
2615status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2616 struct audio_patch *patches,
2617 unsigned int *generation)
2618{
François Gaffie53615e22015-03-19 09:24:12 +01002619 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002620 return BAD_VALUE;
2621 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002622 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002623 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002624}
2625
Eric Laurente1715a42014-05-20 11:30:42 -07002626status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002627{
Eric Laurente1715a42014-05-20 11:30:42 -07002628 ALOGV("setAudioPortConfig()");
2629
2630 if (config == NULL) {
2631 return BAD_VALUE;
2632 }
2633 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2634 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002635 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2636 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002637 }
2638
Eric Laurenta121f902014-06-03 13:32:54 -07002639 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002640 if (config->type == AUDIO_PORT_TYPE_MIX) {
2641 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002642 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002643 if (outputDesc == NULL) {
2644 return BAD_VALUE;
2645 }
Eric Laurent84c70242014-06-23 08:46:27 -07002646 ALOG_ASSERT(!outputDesc->isDuplicated(),
2647 "setAudioPortConfig() called on duplicated output %d",
2648 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002649 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002650 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002651 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002652 if (inputDesc == NULL) {
2653 return BAD_VALUE;
2654 }
Eric Laurenta121f902014-06-03 13:32:54 -07002655 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002656 } else {
2657 return BAD_VALUE;
2658 }
2659 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2660 sp<DeviceDescriptor> deviceDesc;
2661 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2662 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2663 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2664 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2665 } else {
2666 return BAD_VALUE;
2667 }
2668 if (deviceDesc == NULL) {
2669 return BAD_VALUE;
2670 }
Eric Laurenta121f902014-06-03 13:32:54 -07002671 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002672 } else {
2673 return BAD_VALUE;
2674 }
2675
Eric Laurenta121f902014-06-03 13:32:54 -07002676 struct audio_port_config backupConfig;
2677 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2678 if (status == NO_ERROR) {
2679 struct audio_port_config newConfig;
2680 audioPortConfig->toAudioPortConfig(&newConfig, config);
2681 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002682 }
Eric Laurenta121f902014-06-03 13:32:54 -07002683 if (status != NO_ERROR) {
2684 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002685 }
Eric Laurente1715a42014-05-20 11:30:42 -07002686
2687 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002688}
2689
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002690void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2691{
Eric Laurentd60560a2015-04-10 11:31:20 -07002692 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002693 clearAudioPatches(uid);
2694 clearSessionRoutes(uid);
2695}
2696
Eric Laurent6a94d692014-05-20 11:18:06 -07002697void AudioPolicyManager::clearAudioPatches(uid_t uid)
2698{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002699 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002700 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2701 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002702 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002703 }
2704 }
2705}
2706
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002707void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2708 audio_io_handle_t ouptutToSkip)
2709{
2710 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2711 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2712 for (size_t j = 0; j < mOutputs.size(); j++) {
2713 if (mOutputs.keyAt(j) == ouptutToSkip) {
2714 continue;
2715 }
2716 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2717 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2718 continue;
2719 }
2720 // If the default device for this strategy is on another output mix,
2721 // invalidate all tracks in this strategy to force re connection.
2722 // Otherwise select new device on the output mix.
2723 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
Eric Laurent794fde22016-03-11 09:50:45 -08002724 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002725 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2726 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2727 }
2728 }
2729 } else {
2730 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2731 setOutputDevice(outputDesc, newDevice, false);
2732 }
2733 }
2734}
2735
2736void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2737{
2738 // remove output routes associated with this uid
2739 SortedVector<routing_strategy> affectedStrategies;
2740 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2741 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2742 if (route->mUid == uid) {
2743 mOutputRoutes.removeItemsAt(i);
2744 if (route->mDeviceDescriptor != 0) {
2745 affectedStrategies.add(getStrategy(route->mStreamType));
2746 }
2747 }
2748 }
2749 // reroute outputs if necessary
2750 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2751 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2752 }
2753
2754 // remove input routes associated with this uid
2755 SortedVector<audio_source_t> affectedSources;
2756 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2757 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2758 if (route->mUid == uid) {
2759 mInputRoutes.removeItemsAt(i);
2760 if (route->mDeviceDescriptor != 0) {
2761 affectedSources.add(route->mSource);
2762 }
2763 }
2764 }
2765 // reroute inputs if necessary
2766 SortedVector<audio_io_handle_t> inputsToClose;
2767 for (size_t i = 0; i < mInputs.size(); i++) {
2768 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002769 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002770 inputsToClose.add(inputDesc->mIoHandle);
2771 }
2772 }
2773 for (size_t i = 0; i < inputsToClose.size(); i++) {
2774 closeInput(inputsToClose[i]);
2775 }
2776}
2777
Eric Laurentd60560a2015-04-10 11:31:20 -07002778void AudioPolicyManager::clearAudioSources(uid_t uid)
2779{
2780 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2781 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2782 if (sourceDesc->mUid == uid) {
2783 stopAudioSource(mAudioSources.keyAt(i));
2784 }
2785 }
2786}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002787
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002788status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2789 audio_io_handle_t *ioHandle,
2790 audio_devices_t *device)
2791{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002792 *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
2793 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002794 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002795
François Gaffiedf372692015-03-19 10:43:27 +01002796 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002797}
2798
Eric Laurentd60560a2015-04-10 11:31:20 -07002799status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2800 const audio_attributes_t *attributes,
2801 audio_io_handle_t *handle,
2802 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002803{
Eric Laurentd60560a2015-04-10 11:31:20 -07002804 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2805 if (source == NULL || attributes == NULL || handle == NULL) {
2806 return BAD_VALUE;
2807 }
2808
2809 *handle = AUDIO_IO_HANDLE_NONE;
2810
2811 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2812 source->type != AUDIO_PORT_TYPE_DEVICE) {
2813 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2814 return INVALID_OPERATION;
2815 }
2816
2817 sp<DeviceDescriptor> srcDeviceDesc =
2818 mAvailableInputDevices.getDevice(source->ext.device.type,
2819 String8(source->ext.device.address));
2820 if (srcDeviceDesc == 0) {
2821 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2822 return BAD_VALUE;
2823 }
2824 sp<AudioSourceDescriptor> sourceDesc =
2825 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2826
2827 struct audio_patch dummyPatch;
2828 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2829 sourceDesc->mPatchDesc = patchDesc;
2830
2831 status_t status = connectAudioSource(sourceDesc);
2832 if (status == NO_ERROR) {
2833 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2834 *handle = sourceDesc->getHandle();
2835 }
2836 return status;
2837}
2838
2839status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2840{
2841 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2842
2843 // make sure we only have one patch per source.
2844 disconnectAudioSource(sourceDesc);
2845
2846 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
Eric Laurent28d09f02016-03-08 10:43:05 -08002847 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002848 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2849
2850 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2851 sp<DeviceDescriptor> sinkDeviceDesc =
2852 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2853
2854 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2855 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2856
2857 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2858 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002859 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002860 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2861 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2862 // create patch between src device and output device
2863 // create Hwoutput and add to mHwOutputs
2864 } else {
2865 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2866 audio_io_handle_t output =
2867 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2868 if (output == AUDIO_IO_HANDLE_NONE) {
2869 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2870 return INVALID_OPERATION;
2871 }
2872 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2873 if (outputDesc->isDuplicated()) {
2874 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2875 return INVALID_OPERATION;
2876 }
2877 // create a special patch with no sink and two sources:
2878 // - the second source indicates to PatchPanel through which output mix this patch should
2879 // be connected as well as the stream type for volume control
2880 // - the sink is defined by whatever output device is currently selected for the output
2881 // though which this patch is routed.
2882 patch->num_sinks = 0;
2883 patch->num_sources = 2;
2884 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
2885 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
2886 patch->sources[1].ext.mix.usecase.stream = stream;
2887 status_t status = mpClientInterface->createAudioPatch(patch,
2888 &afPatchHandle,
2889 0);
2890 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
2891 status, afPatchHandle);
2892 if (status != NO_ERROR) {
2893 ALOGW("%s patch panel could not connect device patch, error %d",
2894 __FUNCTION__, status);
2895 return INVALID_OPERATION;
2896 }
2897 uint32_t delayMs = 0;
2898 status = startSource(outputDesc, stream, sinkDevice, &delayMs);
2899
2900 if (status != NO_ERROR) {
2901 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
2902 return status;
2903 }
2904 sourceDesc->mSwOutput = outputDesc;
2905 if (delayMs != 0) {
2906 usleep(delayMs * 1000);
2907 }
2908 }
2909
2910 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
2911 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
2912
2913 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07002914}
2915
Eric Laurent493404d2015-04-21 15:07:36 -07002916status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002917{
Eric Laurentd60560a2015-04-10 11:31:20 -07002918 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
2919 ALOGV("%s handle %d", __FUNCTION__, handle);
2920 if (sourceDesc == 0) {
2921 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
2922 return BAD_VALUE;
2923 }
2924 status_t status = disconnectAudioSource(sourceDesc);
2925
2926 mAudioSources.removeItem(handle);
2927 return status;
2928}
2929
Andy Hung2ddee192015-12-18 17:34:44 -08002930status_t AudioPolicyManager::setMasterMono(bool mono)
2931{
2932 if (mMasterMono == mono) {
2933 return NO_ERROR;
2934 }
2935 mMasterMono = mono;
2936 // if enabling mono we close all offloaded devices, which will invalidate the
2937 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
2938 // for recreating the new AudioTrack as non-offloaded PCM.
2939 //
2940 // If disabling mono, we leave all tracks as is: we don't know which clients
2941 // and tracks are able to be recreated as offloaded. The next "song" should
2942 // play back offloaded.
2943 if (mMasterMono) {
2944 Vector<audio_io_handle_t> offloaded;
2945 for (size_t i = 0; i < mOutputs.size(); ++i) {
2946 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2947 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2948 offloaded.push(desc->mIoHandle);
2949 }
2950 }
2951 for (size_t i = 0; i < offloaded.size(); ++i) {
2952 closeOutput(offloaded[i]);
2953 }
2954 }
2955 // update master mono for all remaining outputs
2956 for (size_t i = 0; i < mOutputs.size(); ++i) {
2957 updateMono(mOutputs.keyAt(i));
2958 }
2959 return NO_ERROR;
2960}
2961
2962status_t AudioPolicyManager::getMasterMono(bool *mono)
2963{
2964 *mono = mMasterMono;
2965 return NO_ERROR;
2966}
2967
Eric Laurentd60560a2015-04-10 11:31:20 -07002968status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2969{
2970 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2971
2972 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
2973 if (patchDesc == 0) {
2974 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
2975 sourceDesc->mPatchDesc->mHandle);
2976 return BAD_VALUE;
2977 }
2978 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
2979
Eric Laurent28d09f02016-03-08 10:43:05 -08002980 audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes);
Eric Laurentd60560a2015-04-10 11:31:20 -07002981 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
2982 if (swOutputDesc != 0) {
2983 stopSource(swOutputDesc, stream, false);
2984 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2985 } else {
2986 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
2987 if (hwOutputDesc != 0) {
2988 // release patch between src device and output device
2989 // close Hwoutput and remove from mHwOutputs
2990 } else {
2991 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
2992 }
2993 }
2994 return NO_ERROR;
2995}
2996
2997sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
2998 audio_io_handle_t output, routing_strategy strategy)
2999{
3000 sp<AudioSourceDescriptor> source;
3001 for (size_t i = 0; i < mAudioSources.size(); i++) {
3002 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3003 routing_strategy sourceStrategy =
3004 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3005 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3006 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3007 source = sourceDesc;
3008 break;
3009 }
3010 }
3011 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003012}
3013
Eric Laurente552edb2014-03-10 17:42:56 -07003014// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003015// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003016// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003017uint32_t AudioPolicyManager::nextAudioPortGeneration()
3018{
3019 return android_atomic_inc(&mAudioPortGeneration);
3020}
3021
Eric Laurente0720872014-03-11 09:30:41 -07003022AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003023 :
3024#ifdef AUDIO_POLICY_TEST
3025 Thread(false),
3026#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003027 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003028 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003029 mAudioPortGeneration(1),
3030 mBeaconMuteRefCount(0),
3031 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003032 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003033 mTtsOutputAvailable(false),
3034 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003035{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003036 mUidCached = getuid();
3037 mpClientInterface = clientInterface;
3038
3039 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3040 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3041 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3042 bool speakerDrcEnabled = false;
3043
3044#ifdef USE_XML_AUDIO_POLICY_CONF
3045 mVolumeCurves = new VolumeCurvesCollection();
3046 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3047 mDefaultOutputDevice, speakerDrcEnabled,
3048 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3049 PolicySerializer serializer;
3050 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3051#else
3052 mVolumeCurves = new StreamDescriptorCollection();
3053 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3054 mDefaultOutputDevice, speakerDrcEnabled);
3055 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3056 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3057#endif
3058 ALOGE("could not load audio policy configuration file, setting defaults");
3059 config.setDefault();
3060 }
3061 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3062 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3063
3064 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003065 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3066 if (!engineInstance) {
3067 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3068 return;
3069 }
3070 // Retrieve the Policy Manager Interface
3071 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3072 if (mEngine == NULL) {
3073 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3074 return;
3075 }
3076 mEngine->setObserver(this);
3077 status_t status = mEngine->initCheck();
3078 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3079
Eric Laurent3a4311c2014-03-17 12:00:47 -07003080 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003081 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003082 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3083 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003084 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003085 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003086 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003087 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003088 continue;
3089 }
3090 // open all output streams needed to access attached devices
3091 // except for direct output streams that are only opened when they are actually
3092 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003093 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003094 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3095 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003096 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003097
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003098 if (!outProfile->hasSupportedDevices()) {
3099 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003100 continue;
3101 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003102 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003103 mTtsOutputAvailable = true;
3104 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003105
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003106 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003107 continue;
3108 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003109 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003110 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3111 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003112 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003113 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003114 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003115 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003116 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003117 if ((profileType & outputDeviceTypes) == 0) {
3118 continue;
3119 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003120 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3121 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07003122
3123 outputDesc->mDevice = profileType;
3124 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3125 config.sample_rate = outputDesc->mSamplingRate;
3126 config.channel_mask = outputDesc->mChannelMask;
3127 config.format = outputDesc->mFormat;
3128 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003129 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003130 &output,
3131 &config,
3132 &outputDesc->mDevice,
3133 String8(""),
3134 &outputDesc->mLatency,
3135 outputDesc->mFlags);
3136
3137 if (status != NO_ERROR) {
3138 ALOGW("Cannot open output stream for device %08x on hw module %s",
3139 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003140 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003141 } else {
3142 outputDesc->mSamplingRate = config.sample_rate;
3143 outputDesc->mChannelMask = config.channel_mask;
3144 outputDesc->mFormat = config.format;
3145
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003146 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3147 for (size_t k = 0; k < supportedDevices.size(); k++) {
3148 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003149 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003150 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3151 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003152 }
3153 }
3154 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003155 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003156 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003157 }
3158 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003159 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003160 outputDesc->mDevice,
3161 true);
3162 }
Eric Laurente552edb2014-03-10 17:42:56 -07003163 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003164 // open input streams needed to access attached devices to validate
3165 // mAvailableInputDevices list
3166 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3167 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003168 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003169
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003170 if (!inProfile->hasSupportedDevices()) {
3171 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003172 continue;
3173 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003174 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003175 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003176 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3177
Eric Laurentd78f1532014-09-16 16:38:20 -07003178 if ((profileType & inputDeviceTypes) == 0) {
3179 continue;
3180 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003181 sp<AudioInputDescriptor> inputDesc =
3182 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003183
Eric Laurentd78f1532014-09-16 16:38:20 -07003184 inputDesc->mDevice = profileType;
3185
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003186 // find the address
3187 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3188 // the inputs vector must be of size 1, but we don't want to crash here
3189 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3190 : String8("");
3191 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3192 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3193
Eric Laurentd78f1532014-09-16 16:38:20 -07003194 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3195 config.sample_rate = inputDesc->mSamplingRate;
3196 config.channel_mask = inputDesc->mChannelMask;
3197 config.format = inputDesc->mFormat;
3198 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003199 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003200 &input,
3201 &config,
3202 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003203 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003204 AUDIO_SOURCE_MIC,
3205 AUDIO_INPUT_FLAG_NONE);
3206
3207 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003208 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3209 for (size_t k = 0; k < supportedDevices.size(); k++) {
3210 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003211 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003212 if (index >= 0) {
3213 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3214 if (!devDesc->isAttached()) {
3215 devDesc->attach(mHwModules[i]);
3216 devDesc->importAudioPort(inProfile);
3217 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003218 }
3219 }
3220 mpClientInterface->closeInput(input);
3221 } else {
3222 ALOGW("Cannot open input stream for device %08x on hw module %s",
3223 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003224 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003225 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003226 }
3227 }
3228 // make sure all attached devices have been allocated a unique ID
3229 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003230 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003231 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003232 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3233 continue;
3234 }
François Gaffie2110e042015-03-24 08:41:51 +01003235 // The device is now validated and can be appended to the available devices of the engine
3236 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3237 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003238 i++;
3239 }
3240 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003241 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003242 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003243 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3244 continue;
3245 }
François Gaffie2110e042015-03-24 08:41:51 +01003246 // The device is now validated and can be appended to the available devices of the engine
3247 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3248 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003249 i++;
3250 }
3251 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003252 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003253 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003254 }
Eric Laurente552edb2014-03-10 17:42:56 -07003255
3256 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3257
3258 updateDevicesAndOutputs();
3259
3260#ifdef AUDIO_POLICY_TEST
3261 if (mPrimaryOutput != 0) {
3262 AudioParameter outputCmd = AudioParameter();
3263 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003264 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003265
3266 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3267 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003268 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3269 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003270 mTestLatencyMs = 0;
3271 mCurOutput = 0;
3272 mDirectOutput = false;
3273 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3274 mTestOutputs[i] = 0;
3275 }
3276
3277 const size_t SIZE = 256;
3278 char buffer[SIZE];
3279 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3280 run(buffer, ANDROID_PRIORITY_AUDIO);
3281 }
3282#endif //AUDIO_POLICY_TEST
3283}
3284
Eric Laurente0720872014-03-11 09:30:41 -07003285AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003286{
3287#ifdef AUDIO_POLICY_TEST
3288 exit();
3289#endif //AUDIO_POLICY_TEST
3290 for (size_t i = 0; i < mOutputs.size(); i++) {
3291 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003292 }
3293 for (size_t i = 0; i < mInputs.size(); i++) {
3294 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003295 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003296 mAvailableOutputDevices.clear();
3297 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003298 mOutputs.clear();
3299 mInputs.clear();
3300 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003301}
3302
Eric Laurente0720872014-03-11 09:30:41 -07003303status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003304{
Eric Laurent87ffa392015-05-22 10:32:38 -07003305 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003306}
3307
3308#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003309bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003310{
3311 ALOGV("entering threadLoop()");
3312 while (!exitPending())
3313 {
3314 String8 command;
3315 int valueInt;
3316 String8 value;
3317
3318 Mutex::Autolock _l(mLock);
3319 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3320
3321 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3322 AudioParameter param = AudioParameter(command);
3323
3324 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3325 valueInt != 0) {
3326 ALOGV("Test command %s received", command.string());
3327 String8 target;
3328 if (param.get(String8("target"), target) != NO_ERROR) {
3329 target = "Manager";
3330 }
3331 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3332 param.remove(String8("test_cmd_policy_output"));
3333 mCurOutput = valueInt;
3334 }
3335 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3336 param.remove(String8("test_cmd_policy_direct"));
3337 if (value == "false") {
3338 mDirectOutput = false;
3339 } else if (value == "true") {
3340 mDirectOutput = true;
3341 }
3342 }
3343 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3344 param.remove(String8("test_cmd_policy_input"));
3345 mTestInput = valueInt;
3346 }
3347
3348 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3349 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003350 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003351 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003352 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003353 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003354 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003355 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003356 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003357 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003358 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003359 if (target == "Manager") {
3360 mTestFormat = format;
3361 } else if (mTestOutputs[mCurOutput] != 0) {
3362 AudioParameter outputParam = AudioParameter();
3363 outputParam.addInt(String8("format"), format);
3364 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3365 }
3366 }
3367 }
3368 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3369 param.remove(String8("test_cmd_policy_channels"));
3370 int channels = 0;
3371
3372 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003373 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003374 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003375 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003376 }
3377 if (channels != 0) {
3378 if (target == "Manager") {
3379 mTestChannels = channels;
3380 } else if (mTestOutputs[mCurOutput] != 0) {
3381 AudioParameter outputParam = AudioParameter();
3382 outputParam.addInt(String8("channels"), channels);
3383 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3384 }
3385 }
3386 }
3387 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3388 param.remove(String8("test_cmd_policy_sampleRate"));
3389 if (valueInt >= 0 && valueInt <= 96000) {
3390 int samplingRate = valueInt;
3391 if (target == "Manager") {
3392 mTestSamplingRate = samplingRate;
3393 } else if (mTestOutputs[mCurOutput] != 0) {
3394 AudioParameter outputParam = AudioParameter();
3395 outputParam.addInt(String8("sampling_rate"), samplingRate);
3396 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3397 }
3398 }
3399 }
3400
3401 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3402 param.remove(String8("test_cmd_policy_reopen"));
3403
Eric Laurentc75307b2015-03-17 15:29:32 -07003404 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003405
Eric Laurentc75307b2015-03-17 15:29:32 -07003406 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003407
Eric Laurentc75307b2015-03-17 15:29:32 -07003408 removeOutput(mPrimaryOutput->mIoHandle);
3409 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3410 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003411 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003412 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3413 config.sample_rate = outputDesc->mSamplingRate;
3414 config.channel_mask = outputDesc->mChannelMask;
3415 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003416 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003417 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003418 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003419 &config,
3420 &outputDesc->mDevice,
3421 String8(""),
3422 &outputDesc->mLatency,
3423 outputDesc->mFlags);
3424 if (status != NO_ERROR) {
3425 ALOGE("Failed to reopen hardware output stream, "
3426 "samplingRate: %d, format %d, channels %d",
3427 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003428 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003429 outputDesc->mSamplingRate = config.sample_rate;
3430 outputDesc->mChannelMask = config.channel_mask;
3431 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003432 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003433 AudioParameter outputCmd = AudioParameter();
3434 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003435 mpClientInterface->setParameters(handle, outputCmd.toString());
3436 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003437 }
3438 }
3439
3440
3441 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3442 }
3443 }
3444 return false;
3445}
3446
Eric Laurente0720872014-03-11 09:30:41 -07003447void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003448{
3449 {
3450 AutoMutex _l(mLock);
3451 requestExit();
3452 mWaitWorkCV.signal();
3453 }
3454 requestExitAndWait();
3455}
3456
Eric Laurente0720872014-03-11 09:30:41 -07003457int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003458{
3459 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3460 if (output == mTestOutputs[i]) return i;
3461 }
3462 return 0;
3463}
3464#endif //AUDIO_POLICY_TEST
3465
3466// ---
3467
Eric Laurentc75307b2015-03-17 15:29:32 -07003468void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003469{
François Gaffie98cc1912015-03-18 17:52:40 +01003470 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003471 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003472 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003473 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003474}
3475
François Gaffie53615e22015-03-19 09:24:12 +01003476void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3477{
3478 mOutputs.removeItem(output);
3479}
3480
Eric Laurent1f2f2232014-06-02 12:01:23 -07003481void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003482{
François Gaffie98cc1912015-03-18 17:52:40 +01003483 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003484 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003485 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003486}
Eric Laurente552edb2014-03-10 17:42:56 -07003487
Eric Laurentc75307b2015-03-17 15:29:32 -07003488void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003489 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003490 const String8 address /*in*/,
3491 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003492 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003493 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003494 if (devDesc != 0) {
3495 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3496 desc->mIoHandle, address.string());
3497 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003498 }
3499}
3500
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003501status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003502 audio_policy_dev_state_t state,
3503 SortedVector<audio_io_handle_t>& outputs,
3504 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003505{
François Gaffie53615e22015-03-19 09:24:12 +01003506 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003507 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003508
3509 if (audio_device_is_digital(device)) {
3510 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003511 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003512 }
Eric Laurente552edb2014-03-10 17:42:56 -07003513
Eric Laurent3b73df72014-03-11 09:06:29 -07003514 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003515 // first list already open outputs that can be routed to this device
3516 for (size_t i = 0; i < mOutputs.size(); i++) {
3517 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003518 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003519 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003520 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3521 outputs.add(mOutputs.keyAt(i));
3522 } else {
3523 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003524 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003525 }
Eric Laurente552edb2014-03-10 17:42:56 -07003526 }
3527 }
3528 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003529 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003530 for (size_t i = 0; i < mHwModules.size(); i++)
3531 {
3532 if (mHwModules[i]->mHandle == 0) {
3533 continue;
3534 }
3535 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3536 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003537 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003538 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003539 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003540 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003541 profiles.add(profile);
3542 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3543 }
Eric Laurente552edb2014-03-10 17:42:56 -07003544 }
3545 }
3546 }
3547
Eric Laurent7b279bb2015-12-14 10:18:23 -08003548 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003549
Eric Laurente552edb2014-03-10 17:42:56 -07003550 if (profiles.isEmpty() && outputs.isEmpty()) {
3551 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3552 return BAD_VALUE;
3553 }
3554
3555 // open outputs for matching profiles if needed. Direct outputs are also opened to
3556 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3557 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003558 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003559
3560 // nothing to do if one output is already opened for this profile
3561 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003562 for (j = 0; j < outputs.size(); j++) {
3563 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003564 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003565 // matching profile: save the sample rates, format and channel masks supported
3566 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003567 if (audio_device_is_digital(device)) {
3568 devDesc->importAudioPort(profile);
3569 }
Eric Laurente552edb2014-03-10 17:42:56 -07003570 break;
3571 }
3572 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003573 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003574 continue;
3575 }
3576
Eric Laurent83b88082014-06-20 18:31:16 -07003577 ALOGV("opening output for device %08x with params %s profile %p",
3578 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003579 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003580 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003581 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3582 config.sample_rate = desc->mSamplingRate;
3583 config.channel_mask = desc->mChannelMask;
3584 config.format = desc->mFormat;
3585 config.offload_info.sample_rate = desc->mSamplingRate;
3586 config.offload_info.channel_mask = desc->mChannelMask;
3587 config.offload_info.format = desc->mFormat;
3588 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003589 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003590 &output,
3591 &config,
3592 &desc->mDevice,
3593 address,
3594 &desc->mLatency,
3595 desc->mFlags);
3596 if (status == NO_ERROR) {
3597 desc->mSamplingRate = config.sample_rate;
3598 desc->mChannelMask = config.channel_mask;
3599 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003600
Eric Laurentd4692962014-05-05 18:13:44 -07003601 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003602 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003603 char *param = audio_device_address_to_parameter(device, address);
3604 mpClientInterface->setParameters(output, String8(param));
3605 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003606 }
François Gaffie112b0af2015-11-19 16:13:25 +01003607 updateAudioProfiles(output, profile->getAudioProfiles());
3608 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003609 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003610 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003611 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003612 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003613 mpClientInterface->closeOutput(output);
Phil Burk702b1052016-03-02 16:38:26 -08003614 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003615 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003616 config.offload_info.sample_rate = config.sample_rate;
3617 config.offload_info.channel_mask = config.channel_mask;
3618 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003619 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003620 &output,
3621 &config,
3622 &desc->mDevice,
3623 address,
3624 &desc->mLatency,
3625 desc->mFlags);
3626 if (status == NO_ERROR) {
3627 desc->mSamplingRate = config.sample_rate;
3628 desc->mChannelMask = config.channel_mask;
3629 desc->mFormat = config.format;
3630 } else {
3631 output = AUDIO_IO_HANDLE_NONE;
3632 }
Eric Laurentd4692962014-05-05 18:13:44 -07003633 }
3634
Eric Laurentcf2c0212014-07-25 16:20:43 -07003635 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003636 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003637 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003638 sp<AudioPolicyMix> policyMix;
3639 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003640 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3641 address.string());
3642 }
François Gaffie036e1e92015-03-19 10:16:24 +01003643 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003644 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003645
Eric Laurent87ffa392015-05-22 10:32:38 -07003646 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3647 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003648 // no duplicated output for direct outputs and
3649 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003650 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003651
Eric Laurentd4692962014-05-05 18:13:44 -07003652 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003653 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003654
Eric Laurentd4692962014-05-05 18:13:44 -07003655 //TODO: configure audio effect output stage here
3656
3657 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003658 duplicatedOutput =
3659 mpClientInterface->openDuplicateOutput(output,
3660 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003661 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003662 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003663 sp<SwAudioOutputDescriptor> dupOutputDesc =
3664 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3665 dupOutputDesc->mOutput1 = mPrimaryOutput;
3666 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003667 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3668 dupOutputDesc->mFormat = desc->mFormat;
3669 dupOutputDesc->mChannelMask = desc->mChannelMask;
3670 dupOutputDesc->mLatency = desc->mLatency;
3671 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003672 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003673 } else {
3674 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003675 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003676 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003677 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003678 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003679 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003680 }
Eric Laurente552edb2014-03-10 17:42:56 -07003681 }
3682 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003683 } else {
3684 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003685 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003686 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003687 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003688 profiles.removeAt(profile_index);
3689 profile_index--;
3690 } else {
3691 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003692 // Load digital format info only for digital devices
3693 if (audio_device_is_digital(device)) {
3694 devDesc->importAudioPort(profile);
3695 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003696
François Gaffie53615e22015-03-19 09:24:12 +01003697 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003698 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3699 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003700 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003701 NULL/*patch handle*/, address.string());
3702 }
Eric Laurente552edb2014-03-10 17:42:56 -07003703 ALOGV("checkOutputsForDevice(): adding output %d", output);
3704 }
3705 }
3706
3707 if (profiles.isEmpty()) {
3708 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3709 return BAD_VALUE;
3710 }
Eric Laurentd4692962014-05-05 18:13:44 -07003711 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003712 // check if one opened output is not needed any more after disconnecting one device
3713 for (size_t i = 0; i < mOutputs.size(); i++) {
3714 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003715 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003716 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003717 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003718 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003719 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003720 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003721 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3722 mOutputs.keyAt(i));
3723 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003724 }
Eric Laurente552edb2014-03-10 17:42:56 -07003725 }
3726 }
Eric Laurentd4692962014-05-05 18:13:44 -07003727 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003728 for (size_t i = 0; i < mHwModules.size(); i++)
3729 {
3730 if (mHwModules[i]->mHandle == 0) {
3731 continue;
3732 }
3733 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3734 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003735 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003736 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003737 ALOGV("checkOutputsForDevice(): "
3738 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003739 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003740 }
3741 }
3742 }
3743 }
3744 return NO_ERROR;
3745}
3746
Paul McLean9080a4c2015-06-18 08:24:02 -07003747status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003748 audio_policy_dev_state_t state,
3749 SortedVector<audio_io_handle_t>& inputs,
3750 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003751{
Paul McLean9080a4c2015-06-18 08:24:02 -07003752 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003753 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003754
3755 if (audio_device_is_digital(device)) {
3756 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003757 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003758 }
3759
Eric Laurentd4692962014-05-05 18:13:44 -07003760 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3761 // first list already open inputs that can be routed to this device
3762 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3763 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003764 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003765 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3766 inputs.add(mInputs.keyAt(input_index));
3767 }
3768 }
3769
3770 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003771 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003772 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3773 {
3774 if (mHwModules[module_idx]->mHandle == 0) {
3775 continue;
3776 }
3777 for (size_t profile_index = 0;
3778 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3779 profile_index++)
3780 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003781 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3782
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003783 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003784 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003785 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003786 profiles.add(profile);
3787 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3788 profile_index, module_idx);
3789 }
Eric Laurentd4692962014-05-05 18:13:44 -07003790 }
3791 }
3792 }
3793
3794 if (profiles.isEmpty() && inputs.isEmpty()) {
3795 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3796 return BAD_VALUE;
3797 }
3798
3799 // open inputs for matching profiles if needed. Direct inputs are also opened to
3800 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3801 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3802
Eric Laurent1c333e22014-05-20 10:48:17 -07003803 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003804 // nothing to do if one input is already opened for this profile
3805 size_t input_index;
3806 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3807 desc = mInputs.valueAt(input_index);
3808 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003809 if (audio_device_is_digital(device)) {
3810 devDesc->importAudioPort(profile);
3811 }
Eric Laurentd4692962014-05-05 18:13:44 -07003812 break;
3813 }
3814 }
3815 if (input_index != mInputs.size()) {
3816 continue;
3817 }
3818
3819 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3820 desc = new AudioInputDescriptor(profile);
3821 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003822 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3823 config.sample_rate = desc->mSamplingRate;
3824 config.channel_mask = desc->mChannelMask;
3825 config.format = desc->mFormat;
3826 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003827 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003828 &input,
3829 &config,
3830 &desc->mDevice,
3831 address,
3832 AUDIO_SOURCE_MIC,
3833 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003834
Eric Laurentcf2c0212014-07-25 16:20:43 -07003835 if (status == NO_ERROR) {
3836 desc->mSamplingRate = config.sample_rate;
3837 desc->mChannelMask = config.channel_mask;
3838 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003839
Eric Laurentd4692962014-05-05 18:13:44 -07003840 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003841 char *param = audio_device_address_to_parameter(device, address);
3842 mpClientInterface->setParameters(input, String8(param));
3843 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003844 }
François Gaffie112b0af2015-11-19 16:13:25 +01003845 updateAudioProfiles(input, profile->getAudioProfiles());
3846 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003847 ALOGW("checkInputsForDevice() direct input missing param");
3848 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003849 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003850 }
3851
3852 if (input != 0) {
3853 addInput(input, desc);
3854 }
3855 } // endif input != 0
3856
Eric Laurentcf2c0212014-07-25 16:20:43 -07003857 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003858 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003859 profiles.removeAt(profile_index);
3860 profile_index--;
3861 } else {
3862 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003863 if (audio_device_is_digital(device)) {
3864 devDesc->importAudioPort(profile);
3865 }
Eric Laurentd4692962014-05-05 18:13:44 -07003866 ALOGV("checkInputsForDevice(): adding input %d", input);
3867 }
3868 } // end scan profiles
3869
3870 if (profiles.isEmpty()) {
3871 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3872 return BAD_VALUE;
3873 }
3874 } else {
3875 // Disconnect
3876 // check if one opened input is not needed any more after disconnecting one device
3877 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3878 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003879 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07003880 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3881 mInputs.keyAt(input_index));
3882 inputs.add(mInputs.keyAt(input_index));
3883 }
3884 }
3885 // Clear any profiles associated with the disconnected device.
3886 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3887 if (mHwModules[module_index]->mHandle == 0) {
3888 continue;
3889 }
3890 for (size_t profile_index = 0;
3891 profile_index < mHwModules[module_index]->mInputProfiles.size();
3892 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003893 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003894 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003895 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003896 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01003897 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07003898 }
3899 }
3900 }
3901 } // end disconnect
3902
3903 return NO_ERROR;
3904}
3905
3906
Eric Laurente0720872014-03-11 09:30:41 -07003907void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003908{
3909 ALOGV("closeOutput(%d)", output);
3910
Eric Laurentc75307b2015-03-17 15:29:32 -07003911 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003912 if (outputDesc == NULL) {
3913 ALOGW("closeOutput() unknown output %d", output);
3914 return;
3915 }
François Gaffie036e1e92015-03-19 10:16:24 +01003916 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003917
Eric Laurente552edb2014-03-10 17:42:56 -07003918 // look for duplicated outputs connected to the output being removed.
3919 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003920 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003921 if (dupOutputDesc->isDuplicated() &&
3922 (dupOutputDesc->mOutput1 == outputDesc ||
3923 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003924 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003925 if (dupOutputDesc->mOutput1 == outputDesc) {
3926 outputDesc2 = dupOutputDesc->mOutput2;
3927 } else {
3928 outputDesc2 = dupOutputDesc->mOutput1;
3929 }
3930 // As all active tracks on duplicated output will be deleted,
3931 // and as they were also referenced on the other output, the reference
3932 // count for their stream type must be adjusted accordingly on
3933 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003934 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003935 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003936 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003937 }
3938 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3939 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3940
3941 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003942 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003943 }
3944 }
3945
Eric Laurent05b90f82014-08-27 15:32:29 -07003946 nextAudioPortGeneration();
3947
Jean-Michel Triviff155c62016-02-26 12:07:16 -08003948 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07003949 if (index >= 0) {
3950 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3951 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3952 mAudioPatches.removeItemsAt(index);
3953 mpClientInterface->onAudioPatchListUpdate();
3954 }
3955
Eric Laurente552edb2014-03-10 17:42:56 -07003956 AudioParameter param;
3957 param.add(String8("closing"), String8("true"));
3958 mpClientInterface->setParameters(output, param.toString());
3959
3960 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003961 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003962 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003963}
3964
3965void AudioPolicyManager::closeInput(audio_io_handle_t input)
3966{
3967 ALOGV("closeInput(%d)", input);
3968
3969 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3970 if (inputDesc == NULL) {
3971 ALOGW("closeInput() unknown input %d", input);
3972 return;
3973 }
3974
Eric Laurent6a94d692014-05-20 11:18:06 -07003975 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003976
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08003977 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent05b90f82014-08-27 15:32:29 -07003978 if (index >= 0) {
3979 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3980 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3981 mAudioPatches.removeItemsAt(index);
3982 mpClientInterface->onAudioPatchListUpdate();
3983 }
3984
3985 mpClientInterface->closeInput(input);
3986 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003987}
3988
Eric Laurentc75307b2015-03-17 15:29:32 -07003989SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3990 audio_devices_t device,
3991 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003992{
3993 SortedVector<audio_io_handle_t> outputs;
3994
3995 ALOGVV("getOutputsForDevice() device %04x", device);
3996 for (size_t i = 0; i < openOutputs.size(); i++) {
3997 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003998 i, openOutputs.valueAt(i)->isDuplicated(),
3999 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004000 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4001 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4002 outputs.add(openOutputs.keyAt(i));
4003 }
4004 }
4005 return outputs;
4006}
4007
Eric Laurente0720872014-03-11 09:30:41 -07004008bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004009 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004010{
4011 if (outputs1.size() != outputs2.size()) {
4012 return false;
4013 }
4014 for (size_t i = 0; i < outputs1.size(); i++) {
4015 if (outputs1[i] != outputs2[i]) {
4016 return false;
4017 }
4018 }
4019 return true;
4020}
4021
Eric Laurente0720872014-03-11 09:30:41 -07004022void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004023{
4024 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4025 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4026 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4027 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4028
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004029 // also take into account external policy-related changes: add all outputs which are
4030 // associated with policies in the "before" and "after" output vectors
4031 ALOGVV("checkOutputForStrategy(): policy related outputs");
4032 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004033 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004034 if (desc != 0 && desc->mPolicyMix != NULL) {
4035 srcOutputs.add(desc->mIoHandle);
4036 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4037 }
4038 }
4039 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004040 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004041 if (desc != 0 && desc->mPolicyMix != NULL) {
4042 dstOutputs.add(desc->mIoHandle);
4043 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4044 }
4045 }
4046
Eric Laurente552edb2014-03-10 17:42:56 -07004047 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4048 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4049 strategy, srcOutputs[0], dstOutputs[0]);
4050 // mute strategy while moving tracks from one output to another
4051 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004052 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004053 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004054 setStrategyMute(strategy, true, desc);
4055 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004056 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004057 sp<AudioSourceDescriptor> source =
4058 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4059 if (source != 0){
4060 connectAudioSource(source);
4061 }
Eric Laurente552edb2014-03-10 17:42:56 -07004062 }
4063
4064 // Move effects associated to this strategy from previous output to new output
4065 if (strategy == STRATEGY_MEDIA) {
4066 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4067 SortedVector<audio_io_handle_t> moved;
4068 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004069 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4070 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4071 effectDesc->mIo != fxOutput) {
4072 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004073 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4074 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004075 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004076 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004077 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004078 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004079 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004080 }
4081 }
4082 }
4083 // Move tracks associated to this strategy from previous output to new output
Eric Laurent794fde22016-03-11 09:50:45 -08004084 for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004085 if (getStrategy((audio_stream_type_t)i) == strategy) {
4086 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004087 }
4088 }
4089 }
4090}
4091
Eric Laurente0720872014-03-11 09:30:41 -07004092void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004093{
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_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004097 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004098 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004099 checkOutputForStrategy(STRATEGY_SONIFICATION);
4100 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004101 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004102 checkOutputForStrategy(STRATEGY_MEDIA);
4103 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004104 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004105}
4106
Eric Laurente0720872014-03-11 09:30:41 -07004107void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004108{
François Gaffie53615e22015-03-19 09:24:12 +01004109 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004110 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004111 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004112 return;
4113 }
4114
Eric Laurent3a4311c2014-03-17 12:00:47 -07004115 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004116 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4117 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4118 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004119 // suspend A2DP output if:
4120 // (NOT already suspended) &&
4121 // ((SCO device is connected &&
4122 // (forced usage for communication || for record is SCO))) ||
4123 // (phone state is ringing || in call)
4124 //
4125 // restore A2DP output if:
4126 // (Already suspended) &&
4127 // ((SCO device is NOT connected ||
4128 // (forced usage NOT for communication && NOT for record is SCO))) &&
4129 // (phone state is NOT ringing && NOT in call)
4130 //
4131 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004132 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004133 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4134 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4135 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4136 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004137
4138 mpClientInterface->restoreOutput(a2dpOutput);
4139 mA2dpSuspended = false;
4140 }
4141 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004142 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004143 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4144 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4145 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4146 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004147
4148 mpClientInterface->suspendOutput(a2dpOutput);
4149 mA2dpSuspended = true;
4150 }
4151 }
4152}
4153
Eric Laurentc75307b2015-03-17 15:29:32 -07004154audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4155 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004156{
4157 audio_devices_t device = AUDIO_DEVICE_NONE;
4158
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004159 ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004160 if (index >= 0) {
4161 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4162 if (patchDesc->mUid != mUidCached) {
4163 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004164 outputDesc->device(), outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004165 return outputDesc->device();
4166 }
4167 }
4168
Eric Laurente552edb2014-03-10 17:42:56 -07004169 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004170 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004171 // use device for strategy enforced audible
4172 // 2: we are in call or the strategy phone is active on the output:
4173 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004174 // 3: the strategy for enforced audible is active but not enforced on the output:
4175 // use the device for strategy enforced audible
Eric Laurent28d09f02016-03-08 10:43:05 -08004176 // 4: the strategy sonification is active on the output:
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004177 // use device for strategy sonification
Eric Laurent28d09f02016-03-08 10:43:05 -08004178 // 5: the strategy accessibility is active on the output:
4179 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004180 // 6: the strategy "respectful" sonification is active on the output:
4181 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004182 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004183 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004184 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004185 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004186 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004187 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004188 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004189 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004190 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4191 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004192 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004193 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004194 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004195 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004196 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004197 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
Eric Laurent28d09f02016-03-08 10:43:05 -08004198 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4199 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004200 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004201 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004202 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004203 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004204 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004205 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004206 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004207 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004208 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004209 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004210 }
4211
Eric Laurent1c333e22014-05-20 10:48:17 -07004212 ALOGV("getNewOutputDevice() selected device %x", device);
4213 return device;
4214}
4215
Eric Laurent232f26f2016-02-17 18:06:27 -08004216audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
Eric Laurent1c333e22014-05-20 10:48:17 -07004217{
Eric Laurent232f26f2016-02-17 18:06:27 -08004218 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004219
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004220 ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004221 if (index >= 0) {
4222 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4223 if (patchDesc->mUid != mUidCached) {
4224 ALOGV("getNewInputDevice() device %08x forced by patch %d",
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004225 inputDesc->mDevice, inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004226 return inputDesc->mDevice;
4227 }
4228 }
4229
Eric Laurent232f26f2016-02-17 18:06:27 -08004230 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource());
Eric Laurent1c333e22014-05-20 10:48:17 -07004231
Eric Laurente552edb2014-03-10 17:42:56 -07004232 return device;
4233}
4234
Eric Laurent794fde22016-03-11 09:50:45 -08004235bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1,
4236 audio_stream_type_t stream2) {
4237 return ((stream1 == stream2) ||
4238 ((stream1 == AUDIO_STREAM_ACCESSIBILITY) && (stream2 == AUDIO_STREAM_MUSIC)) ||
4239 ((stream1 == AUDIO_STREAM_MUSIC) && (stream2 == AUDIO_STREAM_ACCESSIBILITY)));
Eric Laurent28d09f02016-03-08 10:43:05 -08004240}
4241
Eric Laurente0720872014-03-11 09:30:41 -07004242uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004243 return (uint32_t)getStrategy(stream);
4244}
4245
Eric Laurente0720872014-03-11 09:30:41 -07004246audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004247 // By checking the range of stream before calling getStrategy, we avoid
4248 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4249 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004250 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004251 return AUDIO_DEVICE_NONE;
4252 }
Eric Laurent28d09f02016-03-08 10:43:05 -08004253 audio_devices_t devices = AUDIO_DEVICE_NONE;
Eric Laurent794fde22016-03-11 09:50:45 -08004254 for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) {
4255 if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004256 continue;
Eric Laurent6a94d692014-05-20 11:18:06 -07004257 }
Eric Laurent794fde22016-03-11 09:50:45 -08004258 routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
Eric Laurent28d09f02016-03-08 10:43:05 -08004259 audio_devices_t curDevices =
4260 getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
4261 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
4262 for (size_t i = 0; i < outputs.size(); i++) {
4263 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent794fde22016-03-11 09:50:45 -08004264 if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) {
Eric Laurent28d09f02016-03-08 10:43:05 -08004265 curDevices |= outputDesc->device();
4266 }
4267 }
4268 devices |= curDevices;
Eric Laurente552edb2014-03-10 17:42:56 -07004269 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004270
4271 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4272 and doesn't really need to.*/
4273 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4274 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4275 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4276 }
Eric Laurente552edb2014-03-10 17:42:56 -07004277 return devices;
4278}
4279
François Gaffie2110e042015-03-24 08:41:51 +01004280routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4281{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004282 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004283 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004284}
4285
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004286uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4287 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004288 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4289 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4290 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004291 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4292 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4293 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004294 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004295 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004296}
4297
Eric Laurente0720872014-03-11 09:30:41 -07004298void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004299 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004300 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004301 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4302 updateDevicesAndOutputs();
4303 break;
4304 default:
4305 break;
4306 }
4307}
4308
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004309uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004310
4311 // skip beacon mute management if a dedicated TTS output is available
4312 if (mTtsOutputAvailable) {
4313 return 0;
4314 }
4315
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004316 switch(event) {
4317 case STARTING_OUTPUT:
4318 mBeaconMuteRefCount++;
4319 break;
4320 case STOPPING_OUTPUT:
4321 if (mBeaconMuteRefCount > 0) {
4322 mBeaconMuteRefCount--;
4323 }
4324 break;
4325 case STARTING_BEACON:
4326 mBeaconPlayingRefCount++;
4327 break;
4328 case STOPPING_BEACON:
4329 if (mBeaconPlayingRefCount > 0) {
4330 mBeaconPlayingRefCount--;
4331 }
4332 break;
4333 }
4334
4335 if (mBeaconMuteRefCount > 0) {
4336 // any playback causes beacon to be muted
4337 return setBeaconMute(true);
4338 } else {
4339 // no other playback: unmute when beacon starts playing, mute when it stops
4340 return setBeaconMute(mBeaconPlayingRefCount == 0);
4341 }
4342}
4343
4344uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4345 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4346 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4347 // keep track of muted state to avoid repeating mute/unmute operations
4348 if (mBeaconMuted != mute) {
4349 // mute/unmute AUDIO_STREAM_TTS on all outputs
4350 ALOGV("\t muting %d", mute);
4351 uint32_t maxLatency = 0;
4352 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004353 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004354 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004355 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004356 0 /*delay*/, AUDIO_DEVICE_NONE);
4357 const uint32_t latency = desc->latency() * 2;
4358 if (latency > maxLatency) {
4359 maxLatency = latency;
4360 }
4361 }
4362 mBeaconMuted = mute;
4363 return maxLatency;
4364 }
4365 return 0;
4366}
4367
Eric Laurente0720872014-03-11 09:30:41 -07004368audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004369 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004370{
Paul McLeanaa981192015-03-21 09:55:15 -07004371 // Routing
4372 // see if we have an explicit route
4373 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4374 // (getStrategy(stream)).
4375 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4376 // and activity count is non-zero
4377 // the device = the device from the descriptor in the RouteMap, and exit.
4378 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4379 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
Eric Laurent28d09f02016-03-08 10:43:05 -08004380 routing_strategy routeStrategy = getStrategy(route->mStreamType);
4381 if ((routeStrategy == strategy) && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004382 return route->mDeviceDescriptor->type();
4383 }
4384 }
4385
Eric Laurente552edb2014-03-10 17:42:56 -07004386 if (fromCache) {
4387 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4388 strategy, mDeviceForStrategy[strategy]);
4389 return mDeviceForStrategy[strategy];
4390 }
François Gaffie2110e042015-03-24 08:41:51 +01004391 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004392}
4393
Eric Laurente0720872014-03-11 09:30:41 -07004394void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004395{
4396 for (int i = 0; i < NUM_STRATEGIES; i++) {
4397 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4398 }
4399 mPreviousOutputs = mOutputs;
4400}
4401
Eric Laurent1f2f2232014-06-02 12:01:23 -07004402uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004403 audio_devices_t prevDevice,
4404 uint32_t delayMs)
4405{
4406 // mute/unmute strategies using an incompatible device combination
4407 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4408 // if unmuting, unmute only after the specified delay
4409 if (outputDesc->isDuplicated()) {
4410 return 0;
4411 }
4412
4413 uint32_t muteWaitMs = 0;
4414 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004415 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004416
4417 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4418 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004419 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004420 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4421 bool doMute = false;
4422
4423 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4424 doMute = true;
4425 outputDesc->mStrategyMutedByDevice[i] = true;
4426 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4427 doMute = true;
4428 outputDesc->mStrategyMutedByDevice[i] = false;
4429 }
Eric Laurent99401132014-05-07 19:48:15 -07004430 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004431 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004432 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004433 // skip output if it does not share any device with current output
4434 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4435 == AUDIO_DEVICE_NONE) {
4436 continue;
4437 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004438 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4439 mute ? "muting" : "unmuting", i, curDevice);
4440 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004441 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004442 if (mute) {
4443 // FIXME: should not need to double latency if volume could be applied
4444 // immediately by the audioflinger mixer. We must account for the delay
4445 // between now and the next time the audioflinger thread for this output
4446 // will process a buffer (which corresponds to one buffer size,
4447 // usually 1/2 or 1/4 of the latency).
4448 if (muteWaitMs < desc->latency() * 2) {
4449 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004450 }
4451 }
4452 }
4453 }
4454 }
4455 }
4456
Eric Laurent99401132014-05-07 19:48:15 -07004457 // temporary mute output if device selection changes to avoid volume bursts due to
4458 // different per device volumes
4459 if (outputDesc->isActive() && (device != prevDevice)) {
4460 if (muteWaitMs < outputDesc->latency() * 2) {
4461 muteWaitMs = outputDesc->latency() * 2;
4462 }
4463 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004464 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004465 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004466 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004467 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004468 muteWaitMs *2, device);
4469 }
4470 }
4471 }
4472
Eric Laurente552edb2014-03-10 17:42:56 -07004473 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4474 if (muteWaitMs > delayMs) {
4475 muteWaitMs -= delayMs;
4476 usleep(muteWaitMs * 1000);
4477 return muteWaitMs;
4478 }
4479 return 0;
4480}
4481
Eric Laurentc75307b2015-03-17 15:29:32 -07004482uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004483 audio_devices_t device,
4484 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004485 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004486 audio_patch_handle_t *patchHandle,
4487 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004488{
Eric Laurentc75307b2015-03-17 15:29:32 -07004489 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004490 AudioParameter param;
4491 uint32_t muteWaitMs;
4492
4493 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004494 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4495 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004496 return muteWaitMs;
4497 }
4498 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4499 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004500 if ((device != AUDIO_DEVICE_NONE) &&
4501 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004502 return 0;
4503 }
4504
4505 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004506 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004507
4508 audio_devices_t prevDevice = outputDesc->mDevice;
4509
Paul McLeanaa981192015-03-21 09:55:15 -07004510 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004511
4512 if (device != AUDIO_DEVICE_NONE) {
4513 outputDesc->mDevice = device;
4514 }
4515 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4516
4517 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004518 // the requested device is AUDIO_DEVICE_NONE
4519 // OR the requested device is the same as current device
4520 // AND force is not specified
4521 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004522 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004523 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4524 !force &&
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004525 outputDesc->getPatchHandle() != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004526 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004527 return muteWaitMs;
4528 }
4529
4530 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004531
Eric Laurente552edb2014-03-10 17:42:56 -07004532 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004533 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004534 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004535 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004536 DeviceVector deviceList = (address == NULL) ?
4537 mAvailableOutputDevices.getDevicesFromType(device)
4538 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004539 if (!deviceList.isEmpty()) {
4540 struct audio_patch patch;
4541 outputDesc->toAudioPortConfig(&patch.sources[0]);
4542 patch.num_sources = 1;
4543 patch.num_sinks = 0;
4544 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4545 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004546 patch.num_sinks++;
4547 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004548 ssize_t index;
4549 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4550 index = mAudioPatches.indexOfKey(*patchHandle);
4551 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004552 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004553 }
4554 sp< AudioPatch> patchDesc;
4555 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4556 if (index >= 0) {
4557 patchDesc = mAudioPatches.valueAt(index);
4558 afPatchHandle = patchDesc->mAfPatchHandle;
4559 }
4560
Eric Laurent1c333e22014-05-20 10:48:17 -07004561 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004562 &afPatchHandle,
4563 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004564 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4565 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004566 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004567 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004568 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004569 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004570 addAudioPatch(patchDesc->mHandle, patchDesc);
4571 } else {
4572 patchDesc->mPatch = patch;
4573 }
4574 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004575 if (patchHandle) {
4576 *patchHandle = patchDesc->mHandle;
4577 }
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004578 outputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004579 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004580 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004581 }
4582 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004583
4584 // inform all input as well
4585 for (size_t i = 0; i < mInputs.size(); i++) {
4586 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004587 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004588 AudioParameter inputCmd = AudioParameter();
4589 ALOGV("%s: inform input %d of device:%d", __func__,
4590 inputDescriptor->mIoHandle, device);
4591 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4592 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4593 inputCmd.toString(),
4594 delayMs);
4595 }
4596 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004597 }
Eric Laurente552edb2014-03-10 17:42:56 -07004598
4599 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004600 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004601
4602 return muteWaitMs;
4603}
4604
Eric Laurentc75307b2015-03-17 15:29:32 -07004605status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004606 int delayMs,
4607 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004608{
Eric Laurent6a94d692014-05-20 11:18:06 -07004609 ssize_t index;
4610 if (patchHandle) {
4611 index = mAudioPatches.indexOfKey(*patchHandle);
4612 } else {
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004613 index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004614 }
4615 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004616 return INVALID_OPERATION;
4617 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004618 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4619 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004620 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
Jean-Michel Triviff155c62016-02-26 12:07:16 -08004621 outputDesc->setPatchHandle(0);
Eric Laurent6a94d692014-05-20 11:18:06 -07004622 removeAudioPatch(patchDesc->mHandle);
4623 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004624 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004625 return status;
4626}
4627
4628status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4629 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004630 bool force,
4631 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004632{
4633 status_t status = NO_ERROR;
4634
Eric Laurent1f2f2232014-06-02 12:01:23 -07004635 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004636 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4637 inputDesc->mDevice = device;
4638
4639 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4640 if (!deviceList.isEmpty()) {
4641 struct audio_patch patch;
4642 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004643 // AUDIO_SOURCE_HOTWORD is for internal use only:
4644 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004645 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004646 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004647 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4648 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004649 patch.num_sinks = 1;
4650 //only one input device for now
4651 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004652 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004653 ssize_t index;
4654 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4655 index = mAudioPatches.indexOfKey(*patchHandle);
4656 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004657 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004658 }
4659 sp< AudioPatch> patchDesc;
4660 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4661 if (index >= 0) {
4662 patchDesc = mAudioPatches.valueAt(index);
4663 afPatchHandle = patchDesc->mAfPatchHandle;
4664 }
4665
Eric Laurent1c333e22014-05-20 10:48:17 -07004666 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004667 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004668 0);
4669 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004670 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004671 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004672 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004673 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004674 addAudioPatch(patchDesc->mHandle, patchDesc);
4675 } else {
4676 patchDesc->mPatch = patch;
4677 }
4678 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004679 if (patchHandle) {
4680 *patchHandle = patchDesc->mHandle;
4681 }
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004682 inputDesc->setPatchHandle(patchDesc->mHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07004683 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004684 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004685 }
4686 }
4687 }
4688 return status;
4689}
4690
Eric Laurent6a94d692014-05-20 11:18:06 -07004691status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4692 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004693{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004694 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004695 ssize_t index;
4696 if (patchHandle) {
4697 index = mAudioPatches.indexOfKey(*patchHandle);
4698 } else {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004699 index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
Eric Laurent6a94d692014-05-20 11:18:06 -07004700 }
4701 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004702 return INVALID_OPERATION;
4703 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004704 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4705 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004706 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08004707 inputDesc->setPatchHandle(0);
Eric Laurent6a94d692014-05-20 11:18:06 -07004708 removeAudioPatch(patchDesc->mHandle);
4709 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004710 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004711 return status;
4712}
4713
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004714sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004715 String8 address,
4716 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004717 audio_format_t& format,
4718 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004719 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004720{
4721 // Choose an input profile based on the requested capture parameters: select the first available
4722 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004723 //
4724 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4725 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004726
4727 for (size_t i = 0; i < mHwModules.size(); i++)
4728 {
4729 if (mHwModules[i]->mHandle == 0) {
4730 continue;
4731 }
4732 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4733 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004735 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004736 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004737 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004738 format,
4739 &format /*updatedFormat*/,
4740 channelMask,
4741 &channelMask /*updatedChannelMask*/,
4742 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004743
Eric Laurente552edb2014-03-10 17:42:56 -07004744 return profile;
4745 }
4746 }
4747 }
4748 return NULL;
4749}
4750
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004751
4752audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004753 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004754{
François Gaffie036e1e92015-03-19 10:16:24 +01004755 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4756 audio_devices_t selectedDeviceFromMix =
4757 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004758
François Gaffie036e1e92015-03-19 10:16:24 +01004759 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4760 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004761 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004762 return getDeviceForInputSource(inputSource);
4763}
4764
4765audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4766{
Paul McLean466dc8e2015-04-17 13:15:36 -06004767 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4768 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004769 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004770 return route->mDeviceDescriptor->type();
4771 }
4772 }
4773
4774 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004775}
4776
Eric Laurente0720872014-03-11 09:30:41 -07004777float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004778 int index,
4779 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004780{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004781 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004782 // if a headset is connected, apply the following rules to ring tones and notifications
4783 // to avoid sound level bursts in user's ears:
4784 // - always attenuate ring tones and notifications volume by 6dB
4785 // - if music is playing, always limit the volume to current music volume,
4786 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004787 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004788 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4789 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4790 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4791 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4792 ((stream_strategy == STRATEGY_SONIFICATION)
4793 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004794 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004795 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004796 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004797 mVolumeCurves->canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004798 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004799 // when the phone is ringing we must consider that music could have been paused just before
4800 // by the music application and behave as if music was active if the last music track was
4801 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004802 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004803 mLimitRingtoneVolume) {
4804 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004805 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004806 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4807 musicDevice),
4808 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004809 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4810 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4811 if (volumeDb > minVolDB) {
4812 volumeDb = minVolDB;
4813 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004814 }
4815 }
4816 }
4817
Eric Laurentffbc80f2015-03-18 18:30:19 -07004818 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004819}
4820
Eric Laurente0720872014-03-11 09:30:41 -07004821status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004822 int index,
4823 const sp<AudioOutputDescriptor>& outputDesc,
4824 audio_devices_t device,
4825 int delayMs,
4826 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004827{
Eric Laurente552edb2014-03-10 17:42:56 -07004828 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004829 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004830 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004831 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004832 return NO_ERROR;
4833 }
François Gaffie2110e042015-03-24 08:41:51 +01004834 audio_policy_forced_cfg_t forceUseForComm =
4835 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004836 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004837 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4838 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004839 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004840 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004841 return INVALID_OPERATION;
4842 }
4843
Eric Laurentc75307b2015-03-17 15:29:32 -07004844 if (device == AUDIO_DEVICE_NONE) {
4845 device = outputDesc->device();
4846 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004847
Eric Laurentffbc80f2015-03-18 18:30:19 -07004848 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004849 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004850 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004851 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004852
Eric Laurentffbc80f2015-03-18 18:30:19 -07004853 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004854
Eric Laurent3b73df72014-03-11 09:06:29 -07004855 if (stream == AUDIO_STREAM_VOICE_CALL ||
4856 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004857 float voiceVolume;
4858 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004859 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004860 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004861 } else {
4862 voiceVolume = 1.0;
4863 }
4864
Eric Laurentc75307b2015-03-17 15:29:32 -07004865 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004866 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4867 mLastVoiceVolume = voiceVolume;
4868 }
4869 }
4870
4871 return NO_ERROR;
4872}
4873
Eric Laurentc75307b2015-03-17 15:29:32 -07004874void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4875 audio_devices_t device,
4876 int delayMs,
4877 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004878{
Eric Laurentc75307b2015-03-17 15:29:32 -07004879 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004880
Eric Laurent794fde22016-03-11 09:50:45 -08004881 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004882 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004883 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004884 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004885 device,
4886 delayMs,
4887 force);
4888 }
4889}
4890
Eric Laurente0720872014-03-11 09:30:41 -07004891void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004892 bool on,
4893 const sp<AudioOutputDescriptor>& outputDesc,
4894 int delayMs,
4895 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004896{
Eric Laurent72249d52015-06-08 11:12:15 -07004897 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4898 strategy, on, outputDesc->getId());
Eric Laurent794fde22016-03-11 09:50:45 -08004899 for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004900 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004901 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004902 }
4903 }
4904}
4905
Eric Laurente0720872014-03-11 09:30:41 -07004906void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004907 bool on,
4908 const sp<AudioOutputDescriptor>& outputDesc,
4909 int delayMs,
4910 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004911{
Eric Laurente552edb2014-03-10 17:42:56 -07004912 if (device == AUDIO_DEVICE_NONE) {
4913 device = outputDesc->device();
4914 }
4915
Eric Laurentc75307b2015-03-17 15:29:32 -07004916 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4917 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004918
4919 if (on) {
4920 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004921 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004922 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004923 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004924 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004925 }
4926 }
4927 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4928 outputDesc->mMuteCount[stream]++;
4929 } else {
4930 if (outputDesc->mMuteCount[stream] == 0) {
4931 ALOGV("setStreamMute() unmuting non muted stream!");
4932 return;
4933 }
4934 if (--outputDesc->mMuteCount[stream] == 0) {
4935 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004936 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004937 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004938 device,
4939 delayMs);
4940 }
4941 }
4942}
4943
Eric Laurente0720872014-03-11 09:30:41 -07004944void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004945 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004946{
Eric Laurent87ffa392015-05-22 10:32:38 -07004947 if(!hasPrimaryOutput()) {
4948 return;
4949 }
4950
Eric Laurente552edb2014-03-10 17:42:56 -07004951 // if the stream pertains to sonification strategy and we are in call we must
4952 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4953 // in the device used for phone strategy and play the tone if the selected device does not
4954 // interfere with the device used for phone strategy
4955 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4956 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004957 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004958 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4959 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004960 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004961 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4962 stream, starting, outputDesc->mDevice, stateChange);
4963 if (outputDesc->mRefCount[stream]) {
4964 int muteCount = 1;
4965 if (stateChange) {
4966 muteCount = outputDesc->mRefCount[stream];
4967 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004968 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004969 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4970 for (int i = 0; i < muteCount; i++) {
4971 setStreamMute(stream, starting, mPrimaryOutput);
4972 }
4973 } else {
4974 ALOGV("handleIncallSonification() high visibility");
4975 if (outputDesc->device() &
4976 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4977 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4978 for (int i = 0; i < muteCount; i++) {
4979 setStreamMute(stream, starting, mPrimaryOutput);
4980 }
4981 }
4982 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004983 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4984 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004985 } else {
4986 mpClientInterface->stopTone();
4987 }
4988 }
4989 }
4990 }
4991}
4992
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004993audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4994{
4995 // flags to stream type mapping
4996 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4997 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4998 }
4999 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5000 return AUDIO_STREAM_BLUETOOTH_SCO;
5001 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005002 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5003 return AUDIO_STREAM_TTS;
5004 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005005
5006 // usage to stream type mapping
5007 switch (attr->usage) {
5008 case AUDIO_USAGE_MEDIA:
5009 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005010 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5011 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005012 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5013 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005014 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5015 return AUDIO_STREAM_SYSTEM;
5016 case AUDIO_USAGE_VOICE_COMMUNICATION:
5017 return AUDIO_STREAM_VOICE_CALL;
5018
5019 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5020 return AUDIO_STREAM_DTMF;
5021
5022 case AUDIO_USAGE_ALARM:
5023 return AUDIO_STREAM_ALARM;
5024 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5025 return AUDIO_STREAM_RING;
5026
5027 case AUDIO_USAGE_NOTIFICATION:
5028 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5029 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5030 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5031 case AUDIO_USAGE_NOTIFICATION_EVENT:
5032 return AUDIO_STREAM_NOTIFICATION;
5033
5034 case AUDIO_USAGE_UNKNOWN:
5035 default:
5036 return AUDIO_STREAM_MUSIC;
5037 }
5038}
Eric Laurente83b55d2014-11-14 10:06:21 -08005039
François Gaffie53615e22015-03-19 09:24:12 +01005040bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5041{
Eric Laurente83b55d2014-11-14 10:06:21 -08005042 // has flags that map to a strategy?
5043 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5044 return true;
5045 }
5046
5047 // has known usage?
5048 switch (paa->usage) {
5049 case AUDIO_USAGE_UNKNOWN:
5050 case AUDIO_USAGE_MEDIA:
5051 case AUDIO_USAGE_VOICE_COMMUNICATION:
5052 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5053 case AUDIO_USAGE_ALARM:
5054 case AUDIO_USAGE_NOTIFICATION:
5055 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5056 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5057 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5058 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5059 case AUDIO_USAGE_NOTIFICATION_EVENT:
5060 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5061 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5062 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5063 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005064 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005065 break;
5066 default:
5067 return false;
5068 }
5069 return true;
5070}
5071
François Gaffiead3183e2015-03-18 16:55:35 +01005072bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5073 routing_strategy strategy, uint32_t inPastMs,
5074 nsecs_t sysTime) const
5075{
5076 if ((sysTime == 0) && (inPastMs != 0)) {
5077 sysTime = systemTime();
5078 }
Eric Laurent794fde22016-03-11 09:50:45 -08005079 for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01005080 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5081 (NUM_STRATEGIES == strategy)) &&
5082 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5083 return true;
5084 }
5085 }
5086 return false;
5087}
5088
François Gaffie2110e042015-03-24 08:41:51 +01005089audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5090{
5091 return mEngine->getForceUse(usage);
5092}
5093
5094bool AudioPolicyManager::isInCall()
5095{
5096 return isStateInCall(mEngine->getPhoneState());
5097}
5098
5099bool AudioPolicyManager::isStateInCall(int state)
5100{
5101 return is_state_in_call(state);
5102}
5103
Eric Laurentd60560a2015-04-10 11:31:20 -07005104void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5105{
5106 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5107 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5108 if (sourceDesc->mDevice->equals(deviceDesc)) {
5109 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5110 stopAudioSource(sourceDesc->getHandle());
5111 }
5112 }
5113
5114 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5115 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5116 bool release = false;
5117 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5118 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5119 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5120 source->ext.device.type == deviceDesc->type()) {
5121 release = true;
5122 }
5123 }
5124 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5125 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5126 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5127 sink->ext.device.type == deviceDesc->type()) {
5128 release = true;
5129 }
5130 }
5131 if (release) {
5132 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5133 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5134 }
5135 }
5136}
5137
Phil Burk09bc4612016-02-24 15:58:15 -08005138// Modify the list of surround sound formats supported.
5139void AudioPolicyManager::filterSurroundFormats(FormatVector &formats) {
5140
5141 audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
5142 AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
5143 ALOGI("%s: forced use = %d", __FUNCTION__, forceUse);
5144
5145 // Analyze original support for various formats.
5146 bool supportsRawSurround = false;
5147 bool supportsIEC61937 = false;
5148 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5149 audio_format_t format = formats[formatIndex];
5150 ALOGI("%s: original formats: #%x", __FUNCTION__, format);
5151 switch (format) {
5152 case AUDIO_FORMAT_AC3:
5153 case AUDIO_FORMAT_E_AC3:
5154 case AUDIO_FORMAT_DTS:
5155 case AUDIO_FORMAT_DTS_HD:
5156 supportsRawSurround = true;
5157 break;
5158 case AUDIO_FORMAT_IEC61937:
5159 supportsIEC61937 = true;
5160 break;
5161 default:
5162 break;
5163 }
5164 }
5165 ALOGI("%s: supportsRawSurround = %d, supportsIEC61937 = %d",
5166 __FUNCTION__, supportsRawSurround, supportsIEC61937);
5167
5168 // Modify formats based on surround preferences.
5169 // If NEVER, remove support for surround formats.
5170 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER)
5171 && (supportsRawSurround || supportsIEC61937)) {
5172 // Remove surround sound related formats.
5173 for (size_t formatIndex = 0; formatIndex < formats.size(); ) {
5174 audio_format_t format = formats[formatIndex];
5175 switch(format) {
5176 case AUDIO_FORMAT_AC3:
5177 case AUDIO_FORMAT_E_AC3:
5178 case AUDIO_FORMAT_DTS:
5179 case AUDIO_FORMAT_DTS_HD:
5180 case AUDIO_FORMAT_IEC61937:
5181 ALOGI("%s: remove #%x", __FUNCTION__, format);
5182 formats.removeAt(formatIndex);
5183 break;
5184 default:
5185 formatIndex++; // keep it
5186 break;
5187 }
5188 }
5189 supportsRawSurround = false;
5190 supportsIEC61937 = false;
5191 }
5192 // If ALWAYS, add support for raw surround formats if all are missing.
5193 // This assumes that if any of these formats are reported by the HAL
5194 // then the report is valid and should not be modified.
5195 if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)
5196 && !supportsRawSurround) {
5197 formats.add(AUDIO_FORMAT_AC3);
5198 formats.add(AUDIO_FORMAT_E_AC3);
5199 formats.add(AUDIO_FORMAT_DTS);
5200 formats.add(AUDIO_FORMAT_DTS_HD);
5201 supportsRawSurround = true;
5202 }
5203 // Add support for IEC61937 if raw surround supported.
5204 // The HAL could do this but add it here, just in case.
5205 if (supportsRawSurround && !supportsIEC61937) {
5206 formats.add(AUDIO_FORMAT_IEC61937);
5207 // supportsIEC61937 = true;
5208 }
5209 // Just for debugging.
5210 for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
5211 audio_format_t format = formats[formatIndex];
5212 ALOGI("%s: final formats: #%x", __FUNCTION__, format);
5213 }
5214}
5215
François Gaffie112b0af2015-11-19 16:13:25 +01005216void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle,
5217 AudioProfileVector &profiles)
5218{
5219 String8 reply;
5220 char *value;
5221 // Format MUST be checked first to update the list of AudioProfile
5222 if (profiles.hasDynamicFormat()) {
5223 reply = mpClientInterface->getParameters(ioHandle,
5224 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Phil Burk09bc4612016-02-24 15:58:15 -08005225 ALOGI("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005226 AudioParameter repliedParameters(reply);
5227 if (repliedParameters.get(
5228 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005229 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5230 return;
5231 }
Phil Burk09bc4612016-02-24 15:58:15 -08005232 FormatVector formats = formatsFromString(reply.string());
5233 filterSurroundFormats(formats);
5234 profiles.setFormats(formats);
François Gaffie112b0af2015-11-19 16:13:25 +01005235 }
5236 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5237
Eric Laurent20eb3a42016-01-26 18:39:17 -08005238 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005239 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005240 ChannelsVector channelMasks;
5241 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005242 AudioParameter requestedParameters;
5243 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5244
5245 if (profiles.hasDynamicRateFor(format)) {
5246 reply = mpClientInterface->getParameters(ioHandle,
5247 requestedParameters.toString() + ";" +
5248 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5249 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005250 AudioParameter repliedParameters(reply);
5251 if (repliedParameters.get(
5252 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5253 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005254 }
5255 }
5256 if (profiles.hasDynamicChannelsFor(format)) {
5257 reply = mpClientInterface->getParameters(ioHandle,
5258 requestedParameters.toString() + ";" +
5259 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5260 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005261 AudioParameter repliedParameters(reply);
5262 if (repliedParameters.get(
5263 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5264 channelMasks = channelMasksFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005265 }
5266 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005267 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005268 }
5269}
Eric Laurentd60560a2015-04-10 11:31:20 -07005270
Eric Laurente552edb2014-03-10 17:42:56 -07005271}; // namespace android