blob: 7d996cc581be835a58ea748c2ebdfe0c048bb381 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
François Gaffief4ad6e52015-11-19 16:59:57 +010027#define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml"
28
Eric Laurentd4692962014-05-05 18:13:44 -070029#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070030#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070031
François Gaffie2110e042015-03-24 08:41:51 +010032#include <AudioPolicyManagerInterface.h>
33#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070034#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070035#include <utils/Log.h>
36#include <hardware/audio.h>
37#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070038#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080039#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070040#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070041#include "AudioPolicyManager.h"
François Gaffied1ab2bd2015-12-02 18:20:06 +010042#ifndef USE_XML_AUDIO_POLICY_CONF
François Gaffie53615e22015-03-19 09:24:12 +010043#include <ConfigParsingUtils.h>
François Gaffied1ab2bd2015-12-02 18:20:06 +010044#include <StreamDescriptor.h>
François Gaffief4ad6e52015-11-19 16:59:57 +010045#endif
François Gaffied1ab2bd2015-12-02 18:20:06 +010046#include <Serializer.h>
François Gaffiea8ecc2c2015-11-09 16:10:40 +010047#include "TypeConverter.h"
François Gaffie53615e22015-03-19 09:24:12 +010048#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070049
Eric Laurent3b73df72014-03-11 09:06:29 -070050namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070051
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 Laurentfb66dd92016-01-28 18:32:03 -0800423 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
424 for (size_t i = 0; i < activeInputs.size(); i++) {
425 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
426 if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) {
427 AudioSessionCollection activeSessions =
428 activeDesc->getAudioSessions(true /*activeOnly*/);
429 for (size_t j = 0; j < activeSessions.size(); j++) {
430 audio_session_t activeSession = activeSessions.keyAt(j);
431 stopInput(activeDesc->mIoHandle, activeSession);
432 releaseInput(activeDesc->mIoHandle, activeSession);
433 }
Eric Laurentc0a889f2015-10-14 14:36:34 -0700434 }
435 }
436
Eric Laurentc2730ba2014-07-20 15:47:07 -0700437 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
438 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
439 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
440 status);
441 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100442 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700443 mCallTxPatch->mAfPatchHandle = afPatchHandle;
444 mCallTxPatch->mUid = mUidCached;
445 }
446 }
447}
448
Eric Laurente0720872014-03-11 09:30:41 -0700449void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700450{
451 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100452 // store previous phone state for management of sonification strategy below
453 int oldState = mEngine->getPhoneState();
454
455 if (mEngine->setPhoneState(state) != NO_ERROR) {
456 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700457 return;
458 }
François Gaffie2110e042015-03-24 08:41:51 +0100459 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // if leaving call state, handle special case of active streams
461 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700462 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700463 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700464 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800465 if (stream == AUDIO_STREAM_PATCH) {
466 continue;
467 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700468 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700469 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800470
Eric Laurent63dea1d2015-07-02 17:10:28 -0700471 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800472 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700473 }
474
François Gaffie2110e042015-03-24 08:41:51 +0100475 /**
476 * Switching to or from incall state or switching between telephony and VoIP lead to force
477 * routing command.
478 */
479 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
480 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700481
482 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700483 checkA2dpSuspend();
484 checkOutputForAllStrategies();
485 updateDevicesAndOutputs();
486
Eric Laurente552edb2014-03-10 17:42:56 -0700487 int delayMs = 0;
488 if (isStateInCall(state)) {
489 nsecs_t sysTime = systemTime();
490 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700491 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700492 // mute media and sonification strategies and delay device switch by the largest
493 // latency of any output where either strategy is active.
494 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100495 if ((isStrategyActive(desc, STRATEGY_MEDIA,
496 SONIFICATION_HEADSET_MUSIC_DELAY,
497 sysTime) ||
498 isStrategyActive(desc, STRATEGY_SONIFICATION,
499 SONIFICATION_HEADSET_MUSIC_DELAY,
500 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700501 (delayMs < (int)desc->latency()*2)) {
502 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700503 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700504 setStrategyMute(STRATEGY_MEDIA, true, desc);
505 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700506 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700507 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
508 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700509 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
510 }
511 }
512
Eric Laurent87ffa392015-05-22 10:32:38 -0700513 if (hasPrimaryOutput()) {
514 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
515 // the device returned is not necessarily reachable via this output
516 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
517 // force routing command to audio hardware when ending call
518 // even if no device change is needed
519 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
520 rxDevice = mPrimaryOutput->device();
521 }
Eric Laurente552edb2014-03-10 17:42:56 -0700522
Eric Laurent87ffa392015-05-22 10:32:38 -0700523 if (state == AUDIO_MODE_IN_CALL) {
524 updateCallRouting(rxDevice, delayMs);
525 } else if (oldState == AUDIO_MODE_IN_CALL) {
526 if (mCallRxPatch != 0) {
527 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
528 mCallRxPatch.clear();
529 }
530 if (mCallTxPatch != 0) {
531 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
532 mCallTxPatch.clear();
533 }
534 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
535 } else {
536 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700537 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700538 }
Eric Laurente552edb2014-03-10 17:42:56 -0700539 // if entering in call state, handle special case of active streams
540 // pertaining to sonification strategy see handleIncallSonification()
541 if (isStateInCall(state)) {
542 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700543 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800544 if (stream == AUDIO_STREAM_PATCH) {
545 continue;
546 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700547 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700548 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700549
550 // force reevaluating accessibility routing when call starts
551 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700552 }
553
554 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700555 if (state == AUDIO_MODE_RINGTONE &&
556 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700557 mLimitRingtoneVolume = true;
558 } else {
559 mLimitRingtoneVolume = false;
560 }
561}
562
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700563audio_mode_t AudioPolicyManager::getPhoneState() {
564 return mEngine->getPhoneState();
565}
566
Eric Laurente0720872014-03-11 09:30:41 -0700567void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700568 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
François Gaffie2110e042015-03-24 08:41:51 +0100570 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700571
François Gaffie2110e042015-03-24 08:41:51 +0100572 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
573 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
574 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700575 }
François Gaffie2110e042015-03-24 08:41:51 +0100576 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
577 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
578 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700579
580 // check for device and output changes triggered by new force usage
581 checkA2dpSuspend();
582 checkOutputForAllStrategies();
583 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700584 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700585 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
586 updateCallRouting(newDevice);
587 }
Eric Laurente552edb2014-03-10 17:42:56 -0700588 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700589 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
590 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
591 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
592 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700593 }
Eric Laurente552edb2014-03-10 17:42:56 -0700594 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700595 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700596 }
597 }
598
Eric Laurentfb66dd92016-01-28 18:32:03 -0800599 Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
600 for (size_t i = 0; i < activeInputs.size(); i++) {
601 sp<AudioInputDescriptor> activeDesc = activeInputs[i];
602 audio_devices_t newDevice = getNewInputDevice(activeDesc);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700603 // Force new input selection if the new device can not be reached via current input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800604 if (activeDesc->mProfile->getSupportedDevices().types() &
605 (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
606 setInputDevice(activeDesc->mIoHandle, newDevice);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700607 } else {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800608 closeInput(activeDesc->mIoHandle);
Eric Laurentc171c7c2015-09-25 12:21:06 -0700609 }
Eric Laurente552edb2014-03-10 17:42:56 -0700610 }
Eric Laurente552edb2014-03-10 17:42:56 -0700611}
612
Eric Laurente0720872014-03-11 09:30:41 -0700613void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700614{
615 ALOGV("setSystemProperty() property %s, value %s", property, value);
616}
617
618// Find a direct output profile compatible with the parameters passed, even if the input flags do
619// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800620sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700621 audio_devices_t device,
622 uint32_t samplingRate,
623 audio_format_t format,
624 audio_channel_mask_t channelMask,
625 audio_output_flags_t flags)
626{
Eric Laurent861a6282015-05-18 15:40:16 -0700627 // only retain flags that will drive the direct output profile selection
628 // if explicitly requested
629 static const uint32_t kRelevantFlags =
630 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
631 flags =
632 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
633
634 sp<IOProfile> profile;
635
Eric Laurente552edb2014-03-10 17:42:56 -0700636 for (size_t i = 0; i < mHwModules.size(); i++) {
637 if (mHwModules[i]->mHandle == 0) {
638 continue;
639 }
640 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700641 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
642 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700643 samplingRate, NULL /*updatedSamplingRate*/,
644 format, NULL /*updatedFormat*/,
645 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700646 flags)) {
647 continue;
648 }
649 // reject profiles not corresponding to a device currently available
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100650 if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700651 continue;
652 }
653 // if several profiles are compatible, give priority to one with offload capability
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100654 if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
Eric Laurent861a6282015-05-18 15:40:16 -0700655 continue;
656 }
657 profile = curProfile;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100658 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent861a6282015-05-18 15:40:16 -0700659 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700660 }
Eric Laurente552edb2014-03-10 17:42:56 -0700661 }
662 }
Eric Laurent861a6282015-05-18 15:40:16 -0700663 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700664}
665
Eric Laurente0720872014-03-11 09:30:41 -0700666audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100667 uint32_t samplingRate,
668 audio_format_t format,
669 audio_channel_mask_t channelMask,
670 audio_output_flags_t flags,
671 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700672{
Eric Laurent3b73df72014-03-11 09:06:29 -0700673 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700674 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
675 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
676 device, stream, samplingRate, format, channelMask, flags);
677
Eric Laurente83b55d2014-11-14 10:06:21 -0800678 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
679 stream, samplingRate,format, channelMask,
680 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700681}
682
Eric Laurente83b55d2014-11-14 10:06:21 -0800683status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
684 audio_io_handle_t *output,
685 audio_session_t session,
686 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700687 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800688 uint32_t samplingRate,
689 audio_format_t format,
690 audio_channel_mask_t channelMask,
691 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700692 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800693 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700694{
Eric Laurente83b55d2014-11-14 10:06:21 -0800695 audio_attributes_t attributes;
696 if (attr != NULL) {
697 if (!isValidAttributes(attr)) {
698 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
699 attr->usage, attr->content_type, attr->flags,
700 attr->tags);
701 return BAD_VALUE;
702 }
703 attributes = *attr;
704 } else {
705 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
706 ALOGE("getOutputForAttr(): invalid stream type");
707 return BAD_VALUE;
708 }
709 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700711 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivie8deced2016-02-11 12:50:39 -0800712 if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) {
François Gaffie036e1e92015-03-19 10:16:24 +0100713 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
Phil Burkfdb3c072016-02-09 10:47:02 -0800714 if (!audio_has_proportional_frames(format)) {
François Gaffie036e1e92015-03-19 10:16:24 +0100715 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800716 }
François Gaffie036e1e92015-03-19 10:16:24 +0100717 *stream = streamTypefromAttributesInt(&attributes);
718 *output = desc->mIoHandle;
719 ALOGV("getOutputForAttr() returns output %d", *output);
720 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800721 }
722 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
723 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
724 return BAD_VALUE;
725 }
726
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700727 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
728 " session %d selectedDeviceId %d",
729 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
730 session, selectedDeviceId);
731
732 *stream = streamTypefromAttributesInt(&attributes);
733
734 // Explicit routing?
735 sp<DeviceDescriptor> deviceDesc;
736 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
737 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
738 deviceDesc = mAvailableOutputDevices[i];
739 break;
740 }
741 }
742 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700743
Eric Laurente83b55d2014-11-14 10:06:21 -0800744 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700745 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700746
Eric Laurente83b55d2014-11-14 10:06:21 -0800747 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700748 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
749 }
750
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700751 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700752 device, samplingRate, format, channelMask, flags);
753
Eric Laurente83b55d2014-11-14 10:06:21 -0800754 *output = getOutputForDevice(device, session, *stream,
755 samplingRate, format, channelMask,
756 flags, offloadInfo);
757 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700758 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800759 return INVALID_OPERATION;
760 }
Paul McLeanaa981192015-03-21 09:55:15 -0700761
Eric Laurente83b55d2014-11-14 10:06:21 -0800762 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700763}
764
765audio_io_handle_t AudioPolicyManager::getOutputForDevice(
766 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800767 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700768 audio_stream_type_t stream,
769 uint32_t samplingRate,
770 audio_format_t format,
771 audio_channel_mask_t channelMask,
772 audio_output_flags_t flags,
773 const audio_offload_info_t *offloadInfo)
774{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700775 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700776 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700777 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700778
Eric Laurente552edb2014-03-10 17:42:56 -0700779#ifdef AUDIO_POLICY_TEST
780 if (mCurOutput != 0) {
781 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
782 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
783
784 if (mTestOutputs[mCurOutput] == 0) {
785 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700786 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
787 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700788 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700789 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700790 outputDesc->mFlags =
791 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700792 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700793 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
794 config.sample_rate = mTestSamplingRate;
795 config.channel_mask = mTestChannels;
796 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700797 if (offloadInfo != NULL) {
798 config.offload_info = *offloadInfo;
799 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700800 status = mpClientInterface->openOutput(0,
801 &mTestOutputs[mCurOutput],
802 &config,
803 &outputDesc->mDevice,
804 String8(""),
805 &outputDesc->mLatency,
806 outputDesc->mFlags);
807 if (status == NO_ERROR) {
808 outputDesc->mSamplingRate = config.sample_rate;
809 outputDesc->mFormat = config.format;
810 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700811 AudioParameter outputCmd = AudioParameter();
812 outputCmd.addInt(String8("set_id"),mCurOutput);
813 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
814 addOutput(mTestOutputs[mCurOutput], outputDesc);
815 }
816 }
817 return mTestOutputs[mCurOutput];
818 }
819#endif //AUDIO_POLICY_TEST
820
821 // open a direct output if required by specified parameters
822 //force direct flag if offload flag is set: offloading implies a direct output stream
823 // and all common behaviors are driven by checking only the direct flag
824 // this should normally be set appropriately in the policy configuration file
825 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700826 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700827 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700828 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
829 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
830 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800831 // only allow deep buffering for music stream type
832 if (stream != AUDIO_STREAM_MUSIC) {
833 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700834 } else if (/* stream == AUDIO_STREAM_MUSIC && */
835 flags == AUDIO_OUTPUT_FLAG_NONE &&
836 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
837 // use DEEP_BUFFER as default output for music stream type
838 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800839 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700840 if (stream == AUDIO_STREAM_TTS) {
841 flags = AUDIO_OUTPUT_FLAG_TTS;
842 }
Eric Laurente552edb2014-03-10 17:42:56 -0700843
Eric Laurentb732cf52014-09-24 19:08:21 -0700844 sp<IOProfile> profile;
845
846 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700847 // and not explicitly requested
848 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
849 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700850 audio_channel_count_from_out_mask(channelMask) <= 2) {
851 goto non_direct_output;
852 }
853
Andy Hung2ddee192015-12-18 17:34:44 -0800854 // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled.
855 // This prevents creating an offloaded track and tearing it down immediately after start
856 // when audioflinger detects there is an active non offloadable effect.
Eric Laurente552edb2014-03-10 17:42:56 -0700857 // FIXME: We should check the audio session here but we do not have it in this context.
858 // This may prevent offloading in rare situations where effects are left active by apps
859 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700860
Eric Laurente552edb2014-03-10 17:42:56 -0700861 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
Andy Hung2ddee192015-12-18 17:34:44 -0800862 !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700863 profile = getProfileForDirectOutput(device,
864 samplingRate,
865 format,
866 channelMask,
867 (audio_output_flags_t)flags);
868 }
869
Eric Laurent1c333e22014-05-20 10:48:17 -0700870 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700871 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700872
873 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700874 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700875 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
876 outputDesc = desc;
877 // reuse direct output if currently open and configured with same parameters
878 if ((samplingRate == outputDesc->mSamplingRate) &&
879 (format == outputDesc->mFormat) &&
880 (channelMask == outputDesc->mChannelMask)) {
881 outputDesc->mDirectOpenCount++;
882 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
883 return mOutputs.keyAt(i);
884 }
885 }
886 }
887 // close direct output if currently open and configured with different parameters
888 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700889 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700890 }
Eric Laurent861a6282015-05-18 15:40:16 -0700891
892 // if the selected profile is offloaded and no offload info was specified,
893 // create a default one
894 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
François Gaffiea8ecc2c2015-11-09 16:10:40 +0100895 if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
Eric Laurent861a6282015-05-18 15:40:16 -0700896 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
897 defaultOffloadInfo.sample_rate = samplingRate;
898 defaultOffloadInfo.channel_mask = channelMask;
899 defaultOffloadInfo.format = format;
900 defaultOffloadInfo.stream_type = stream;
901 defaultOffloadInfo.bit_rate = 0;
902 defaultOffloadInfo.duration_us = -1;
903 defaultOffloadInfo.has_video = true; // conservative
904 defaultOffloadInfo.is_streaming = true; // likely
905 offloadInfo = &defaultOffloadInfo;
906 }
907
Eric Laurentc75307b2015-03-17 15:29:32 -0700908 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700909 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700910 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700911 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700912 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
913 config.sample_rate = samplingRate;
914 config.channel_mask = channelMask;
915 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700916 if (offloadInfo != NULL) {
917 config.offload_info = *offloadInfo;
918 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700919 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700920 &output,
921 &config,
922 &outputDesc->mDevice,
923 String8(""),
924 &outputDesc->mLatency,
925 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700926
927 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700928 if (status != NO_ERROR ||
929 (samplingRate != 0 && samplingRate != config.sample_rate) ||
930 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
931 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700932 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
933 "format %d %d, channelMask %04x %04x", output, samplingRate,
934 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
935 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700936 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700937 mpClientInterface->closeOutput(output);
938 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800939 // fall back to mixer output if possible when the direct output could not be open
940 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
941 goto non_direct_output;
942 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700944 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700945 outputDesc->mSamplingRate = config.sample_rate;
946 outputDesc->mChannelMask = config.channel_mask;
947 outputDesc->mFormat = config.format;
948 outputDesc->mRefCount[stream] = 0;
949 outputDesc->mStopTime[stream] = 0;
950 outputDesc->mDirectOpenCount = 1;
951
Eric Laurente552edb2014-03-10 17:42:56 -0700952 audio_io_handle_t srcOutput = getOutputForEffect();
953 addOutput(output, outputDesc);
954 audio_io_handle_t dstOutput = getOutputForEffect();
955 if (dstOutput == output) {
956 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
957 }
958 mPreviousOutputs = mOutputs;
959 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700960 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700961 return output;
962 }
963
Eric Laurentb732cf52014-09-24 19:08:21 -0700964non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700965 // ignoring channel mask due to downmix capability in mixer
966
967 // open a non direct output
968
969 // for non direct outputs, only PCM is supported
970 if (audio_is_linear_pcm(format)) {
971 // get which output is suitable for the specified stream. The actual
972 // routing change will happen when startOutput() will be called
973 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
974
Eric Laurent8838a382014-09-08 16:44:28 -0700975 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
976 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
977 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700978 }
979 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
980 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
981
Paul McLeanaa981192015-03-21 09:55:15 -0700982 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700983
984 return output;
985}
986
Eric Laurente0720872014-03-11 09:30:41 -0700987audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700988 audio_output_flags_t flags,
989 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700990{
991 // select one output among several that provide a path to a particular device or set of
992 // devices (the list was previously build by getOutputsForDevice()).
993 // The priority is as follows:
994 // 1: the output with the highest number of requested policy flags
995 // 2: the primary output
996 // 3: the first output in the list
997
998 if (outputs.size() == 0) {
999 return 0;
1000 }
1001 if (outputs.size() == 1) {
1002 return outputs[0];
1003 }
1004
1005 int maxCommonFlags = 0;
1006 audio_io_handle_t outputFlags = 0;
1007 audio_io_handle_t outputPrimary = 0;
1008
1009 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001010 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001011 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001012 // if a valid format is specified, skip output if not compatible
1013 if (format != AUDIO_FORMAT_INVALID) {
1014 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1015 if (format != outputDesc->mFormat) {
1016 continue;
1017 }
1018 } else if (!audio_is_linear_pcm(format)) {
1019 continue;
1020 }
1021 }
1022
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001023 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001024 if (commonFlags > maxCommonFlags) {
1025 outputFlags = outputs[i];
1026 maxCommonFlags = commonFlags;
1027 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1028 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001029 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente552edb2014-03-10 17:42:56 -07001030 outputPrimary = outputs[i];
1031 }
1032 }
1033 }
1034
1035 if (outputFlags != 0) {
1036 return outputFlags;
1037 }
1038 if (outputPrimary != 0) {
1039 return outputPrimary;
1040 }
1041
1042 return outputs[0];
1043}
1044
Eric Laurente0720872014-03-11 09:30:41 -07001045status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001046 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001047 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001048{
Paul McLeanaa981192015-03-21 09:55:15 -07001049 ALOGV("startOutput() output %d, stream %d, session %d",
1050 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001051 ssize_t index = mOutputs.indexOfKey(output);
1052 if (index < 0) {
1053 ALOGW("startOutput() unknown output %d", output);
1054 return BAD_VALUE;
1055 }
1056
Eric Laurentc75307b2015-03-17 15:29:32 -07001057 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1058
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001059 // Routing?
1060 mOutputRoutes.incRouteActivity(session);
1061
Eric Laurentc75307b2015-03-17 15:29:32 -07001062 audio_devices_t newDevice;
1063 if (outputDesc->mPolicyMix != NULL) {
1064 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001065 } else if (mOutputRoutes.hasRouteChanged(session)) {
1066 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001067 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001068 } else {
1069 newDevice = AUDIO_DEVICE_NONE;
1070 }
1071
1072 uint32_t delayMs = 0;
1073
Eric Laurentc75307b2015-03-17 15:29:32 -07001074 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1075
1076 if (status != NO_ERROR) {
1077 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001078 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001079 }
1080 // Automatically enable the remote submix input when output is started on a re routing mix
1081 // of type MIX_TYPE_RECORDERS
1082 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1083 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1084 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1085 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1086 outputDesc->mPolicyMix->mRegistrationId,
1087 "remote-submix");
1088 }
1089
1090 if (delayMs != 0) {
1091 usleep(delayMs * 1000);
1092 }
1093
1094 return status;
1095}
1096
1097status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1098 audio_stream_type_t stream,
1099 audio_devices_t device,
1100 uint32_t *delayMs)
1101{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001102 // cannot start playback of STREAM_TTS if any other output is being used
1103 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001104
1105 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001106 if (stream == AUDIO_STREAM_TTS) {
1107 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001108 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001109 return INVALID_OPERATION;
1110 } else {
1111 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1112 }
1113 } else {
1114 // some playback other than beacon starts
1115 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1116 }
1117
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001118 // check active before incrementing usage count
1119 bool force = !outputDesc->isActive();
1120
Eric Laurente552edb2014-03-10 17:42:56 -07001121 // increment usage count for this stream on the requested output:
1122 // NOTE that the usage count is the same for duplicated output and hardware output which is
1123 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1124 outputDesc->changeRefCount(stream, 1);
1125
Eric Laurent493404d2015-04-21 15:07:36 -07001126 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001127 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001128 if (device == AUDIO_DEVICE_NONE) {
1129 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001130 }
Eric Laurente552edb2014-03-10 17:42:56 -07001131 routing_strategy strategy = getStrategy(stream);
1132 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001133 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1134 (beaconMuteLatency > 0);
1135 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001136 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001137 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001138 if (desc != outputDesc) {
1139 // force a device change if any other output is managed by the same hw
1140 // module and has a current device selection that differs from selected device.
1141 // In this case, the audio HAL must receive the new device selection so that it can
1142 // change the device currently selected by the other active output.
1143 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001144 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001145 force = true;
1146 }
1147 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001148 // a notification so that audio focus effect can propagate, or that a mute/unmute
1149 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001150 uint32_t latency = desc->latency();
1151 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1152 waitMs = latency;
1153 }
1154 }
1155 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001157
1158 // handle special case for sonification while in call
1159 if (isInCall()) {
1160 handleIncallSonification(stream, true, false);
1161 }
1162
1163 // apply volume rules for current stream and device if necessary
1164 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001165 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001166 outputDesc,
1167 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001168
1169 // update the outputs if starting an output with a stream that can affect notification
1170 // routing
1171 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001172
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001173 // force reevaluating accessibility routing when ringtone or alarm starts
1174 if (strategy == STRATEGY_SONIFICATION) {
1175 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1176 }
Eric Laurente552edb2014-03-10 17:42:56 -07001177 }
1178 return NO_ERROR;
1179}
1180
1181
Eric Laurente0720872014-03-11 09:30:41 -07001182status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001183 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001184 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001185{
1186 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1187 ssize_t index = mOutputs.indexOfKey(output);
1188 if (index < 0) {
1189 ALOGW("stopOutput() unknown output %d", output);
1190 return BAD_VALUE;
1191 }
1192
Eric Laurentc75307b2015-03-17 15:29:32 -07001193 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001194
Eric Laurentc75307b2015-03-17 15:29:32 -07001195 if (outputDesc->mRefCount[stream] == 1) {
1196 // Automatically disable the remote submix input when output is stopped on a
1197 // re routing mix of type MIX_TYPE_RECORDERS
1198 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1199 outputDesc->mPolicyMix != NULL &&
1200 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1201 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1202 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1203 outputDesc->mPolicyMix->mRegistrationId,
1204 "remote-submix");
1205 }
1206 }
1207
1208 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001209 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001210 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001211 int activityCount = mOutputRoutes.decRouteActivity(session);
1212 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1213
1214 if (forceDeviceUpdate) {
1215 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1216 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001217 }
1218
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001219 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001220}
1221
1222status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001223 audio_stream_type_t stream,
1224 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001225{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001226 // always handle stream stop, check which stream type is stopping
1227 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1228
Eric Laurente552edb2014-03-10 17:42:56 -07001229 // handle special case for sonification while in call
1230 if (isInCall()) {
1231 handleIncallSonification(stream, false, false);
1232 }
1233
1234 if (outputDesc->mRefCount[stream] > 0) {
1235 // decrement usage count of this stream on the output
1236 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001237
Eric Laurente552edb2014-03-10 17:42:56 -07001238 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001239 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001240 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001241 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001242 // delay the device switch by twice the latency because stopOutput() is executed when
1243 // the track stop() command is received and at that time the audio track buffer can
1244 // still contain data that needs to be drained. The latency only covers the audio HAL
1245 // and kernel buffers. Also the latency does not always include additional delay in the
1246 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001247 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001248
1249 // force restoring the device selection on other active outputs if it differs from the
1250 // one being selected for this output
1251 for (size_t i = 0; i < mOutputs.size(); i++) {
1252 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001253 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001254 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001255 desc->isActive() &&
1256 outputDesc->sharesHwModuleWith(desc) &&
1257 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001258 setOutputDevice(desc,
1259 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001260 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001261 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001262 }
1263 }
1264 // update the outputs if stopping one with a stream that can affect notification routing
1265 handleNotificationRoutingForStream(stream);
1266 }
1267 return NO_ERROR;
1268 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001269 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001270 return INVALID_OPERATION;
1271 }
1272}
1273
Eric Laurente83b55d2014-11-14 10:06:21 -08001274void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001275 audio_stream_type_t stream __unused,
1276 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001277{
1278 ALOGV("releaseOutput() %d", output);
1279 ssize_t index = mOutputs.indexOfKey(output);
1280 if (index < 0) {
1281 ALOGW("releaseOutput() releasing unknown output %d", output);
1282 return;
1283 }
1284
1285#ifdef AUDIO_POLICY_TEST
1286 int testIndex = testOutputIndex(output);
1287 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001288 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001289 if (outputDesc->isActive()) {
1290 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001291 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001292 mTestOutputs[testIndex] = 0;
1293 }
1294 return;
1295 }
1296#endif //AUDIO_POLICY_TEST
1297
Paul McLeanaa981192015-03-21 09:55:15 -07001298 // Routing
1299 mOutputRoutes.removeRoute(session);
1300
Eric Laurentc75307b2015-03-17 15:29:32 -07001301 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001302 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001303 if (desc->mDirectOpenCount <= 0) {
1304 ALOGW("releaseOutput() invalid open count %d for output %d",
1305 desc->mDirectOpenCount, output);
1306 return;
1307 }
1308 if (--desc->mDirectOpenCount == 0) {
1309 closeOutput(output);
1310 // If effects where present on the output, audioflinger moved them to the primary
1311 // output by default: move them back to the appropriate output.
1312 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001313 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001314 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1315 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001316 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001317 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001318 }
1319 }
1320}
1321
1322
Eric Laurentcaf7f482014-11-25 17:50:47 -08001323status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1324 audio_io_handle_t *input,
1325 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001326 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001327 uint32_t samplingRate,
1328 audio_format_t format,
1329 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001330 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001331 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001332 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001333{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001334 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1335 "session %d, flags %#x",
1336 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001337
Eric Laurentcaf7f482014-11-25 17:50:47 -08001338 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001339 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001340
Eric Laurent275e8e92014-11-30 15:14:47 -08001341 audio_devices_t device;
1342 // handle legacy remote submix case where the address was not always specified
1343 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001344 audio_source_t inputSource = attr->source;
1345 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001346 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001347
Eric Laurentc447ded2015-01-06 08:47:05 -08001348 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1349 inputSource = AUDIO_SOURCE_MIC;
1350 }
1351 halInputSource = inputSource;
1352
Paul McLean466dc8e2015-04-17 13:15:36 -06001353 // Explicit routing?
1354 sp<DeviceDescriptor> deviceDesc;
1355 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1356 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1357 deviceDesc = mAvailableInputDevices[i];
1358 break;
1359 }
1360 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001361 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001362
Eric Laurentc447ded2015-01-06 08:47:05 -08001363 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001364 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001365 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001366 if (ret != NO_ERROR) {
1367 return ret;
1368 }
1369 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001370 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1371 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001372 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001373 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001374 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001375 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001376 return BAD_VALUE;
1377 }
Eric Laurentc722f302014-12-10 11:21:49 -08001378 if (policyMix != NULL) {
1379 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001380 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1381 // there is an external policy, but this input is attached to a mix of recorders,
1382 // meaning it receives audio injected into the framework, so the recorder doesn't
1383 // know about it and is therefore considered "legacy"
1384 *inputType = API_INPUT_LEGACY;
1385 } else {
1386 // recording a mix of players defined by an external policy, we're rerouting for
1387 // an external policy
1388 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1389 }
Eric Laurentc722f302014-12-10 11:21:49 -08001390 } else if (audio_is_remote_submix_device(device)) {
1391 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001392 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001393 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1394 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001395 } else {
1396 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001397 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001398
Eric Laurent599c7582015-12-07 18:05:55 -08001399 }
1400
1401 *input = getInputForDevice(device, address, session, uid, inputSource,
1402 samplingRate, format, channelMask, flags,
1403 policyMix);
1404 if (*input == AUDIO_IO_HANDLE_NONE) {
1405 mInputRoutes.removeRoute(session);
1406 return INVALID_OPERATION;
1407 }
1408 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1409 return NO_ERROR;
1410}
1411
1412
1413audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1414 String8 address,
1415 audio_session_t session,
1416 uid_t uid,
1417 audio_source_t inputSource,
1418 uint32_t samplingRate,
1419 audio_format_t format,
1420 audio_channel_mask_t channelMask,
1421 audio_input_flags_t flags,
1422 AudioMix *policyMix)
1423{
1424 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1425 audio_source_t halInputSource = inputSource;
1426 bool isSoundTrigger = false;
1427
1428 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1429 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1430 if (index >= 0) {
1431 input = mSoundTriggerSessions.valueFor(session);
1432 isSoundTrigger = true;
1433 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1434 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1435 } else {
1436 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001437 }
1438 }
1439
Andy Hungf129b032015-04-07 13:45:50 -07001440 // find a compatible input profile (not necessarily identical in parameters)
1441 sp<IOProfile> profile;
1442 // samplingRate and flags may be updated by getInputProfile
1443 uint32_t profileSamplingRate = samplingRate;
1444 audio_format_t profileFormat = format;
1445 audio_channel_mask_t profileChannelMask = channelMask;
1446 audio_input_flags_t profileFlags = flags;
1447 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001448 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001449 profileSamplingRate, profileFormat, profileChannelMask,
1450 profileFlags);
1451 if (profile != 0) {
1452 break; // success
1453 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1454 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1455 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001456 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1457 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001458 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001459 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001460 }
Eric Laurente552edb2014-03-10 17:42:56 -07001461 }
1462
Eric Laurent322b4d22015-04-03 15:57:54 -07001463 if (profile->getModuleHandle() == 0) {
1464 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001465 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001466 }
1467
Eric Laurent599c7582015-12-07 18:05:55 -08001468 sp<AudioSession> audioSession = new AudioSession(session,
1469 inputSource,
1470 format,
1471 samplingRate,
1472 channelMask,
1473 flags,
1474 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001475 isSoundTrigger,
1476 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001477
Eric Laurentfb66dd92016-01-28 18:32:03 -08001478
Eric Laurent599c7582015-12-07 18:05:55 -08001479 // reuse an open input if possible
1480 for (size_t i = 0; i < mInputs.size(); i++) {
1481 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001482 // reuse input if:
1483 // - it shares the same profile
1484 // AND
1485 // - it is not a reroute submix input
1486 // AND
1487 // - it is: not used for sound trigger
1488 // OR
1489 // used for sound trigger and all clients use the same session ID
1490 //
1491 if ((profile == desc->mProfile) &&
1492 (isSoundTrigger == desc->isSoundTrigger()) &&
1493 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001494
1495 sp<AudioSession> as = desc->getAudioSession(session);
1496 if (as != 0) {
1497 // do not allow unmatching properties on same session
1498 if (as->matches(audioSession)) {
1499 as->changeOpenCount(1);
1500 } else {
1501 ALOGW("getInputForDevice() record with different attributes"
1502 " exists for session %d", session);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001503 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001504 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001505 } else if (isSoundTrigger) {
1506 break;
1507 }
1508 // force close input if current source is now the highest priority request on this input
1509 // and current input properties are not exactly as requested.
1510 if ((desc->mSamplingRate != samplingRate ||
1511 desc->mChannelMask != channelMask ||
1512 desc->mFormat != format) &&
1513 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
1514 source_priority(inputSource))) {
1515 ALOGV("%s: ", __FUNCTION__);
1516 AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/);
1517 for (size_t j = 0; j < sessions.size(); j++) {
1518 audio_session_t currentSession = sessions.keyAt(j);
1519 stopInput(desc->mIoHandle, currentSession);
1520 releaseInput(desc->mIoHandle, currentSession);
1521 }
1522 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001523 } else {
1524 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001525 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1526 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001527 }
Eric Laurent599c7582015-12-07 18:05:55 -08001528 }
1529 }
Eric Laurent599c7582015-12-07 18:05:55 -08001530
Eric Laurentcf2c0212014-07-25 16:20:43 -07001531 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001532 config.sample_rate = profileSamplingRate;
1533 config.channel_mask = profileChannelMask;
1534 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001535
Eric Laurent322b4d22015-04-03 15:57:54 -07001536 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001537 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001538 &config,
1539 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001540 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001541 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001542 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001543
1544 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001545 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001546 (profileSamplingRate != config.sample_rate) ||
1547 (profileFormat != config.format) ||
1548 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001549 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1550 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001551 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001552 if (input != AUDIO_IO_HANDLE_NONE) {
1553 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001554 }
Eric Laurent599c7582015-12-07 18:05:55 -08001555 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001556 }
1557
Eric Laurent1f2f2232014-06-02 12:01:23 -07001558 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001559 inputDesc->mSamplingRate = profileSamplingRate;
1560 inputDesc->mFormat = profileFormat;
1561 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001562 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001563 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001564 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001565
Eric Laurent599c7582015-12-07 18:05:55 -08001566 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001567 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001568
Eric Laurent599c7582015-12-07 18:05:55 -08001569 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001570}
1571
Eric Laurentfb66dd92016-01-28 18:32:03 -08001572bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1573 const sp<AudioSession>& audioSession)
1574{
1575 // Do not allow capture if an active voice call is using a software patch and
1576 // the call TX source device is on the same HW module.
1577 // FIXME: would be better to refine to only inputs whose profile connects to the
1578 // call TX device but this information is not in the audio patch
1579 if (mCallTxPatch != 0 &&
1580 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1581 return false;
1582 }
1583
1584 // starting concurrent capture is enabled if:
1585 // 1) capturing for re-routing
1586 // 2) capturing for HOTWORD source
1587 // 3) capturing for FM TUNER source
1588 // 3) All other active captures are either for re-routing or HOTWORD
1589
1590 if (is_virtual_input_device(inputDesc->mDevice) ||
1591 audioSession->inputSource() == AUDIO_SOURCE_HOTWORD ||
1592 audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) {
1593 return true;
1594 }
1595
1596 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1597 for (size_t i = 0; i < activeInputs.size(); i++) {
1598 sp<AudioInputDescriptor> activeInput = activeInputs[i];
1599 if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) &&
1600 (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) &&
1601 !is_virtual_input_device(activeInput->mDevice)) {
1602 return false;
1603 }
1604 }
1605
1606 return true;
1607}
1608
1609
Eric Laurent4dc68062014-07-28 17:26:49 -07001610status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001611 audio_session_t session,
1612 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001613{
1614 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001615 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001616 ssize_t index = mInputs.indexOfKey(input);
1617 if (index < 0) {
1618 ALOGW("startInput() unknown input %d", input);
1619 return BAD_VALUE;
1620 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001621 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001622
Eric Laurent599c7582015-12-07 18:05:55 -08001623 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1624 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001625 ALOGW("startInput() unknown session %d on input %d", session, input);
1626 return BAD_VALUE;
1627 }
1628
Eric Laurentfb66dd92016-01-28 18:32:03 -08001629 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1630 ALOGW("startInput(%d) failed: other input already started", input);
1631 return INVALID_OPERATION;
1632 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001633
Eric Laurentfb66dd92016-01-28 18:32:03 -08001634 if (isInCall()) {
1635 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1636 }
1637 if (mInputs.activeInputsCount() != 0) {
1638 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001639 }
1640
Eric Laurent313d1e72016-01-29 09:56:57 -08001641 // increment activity count before calling getNewInputDevice() below as only active sessions
1642 // are considered for device selection
1643 audioSession->changeActiveCount(1);
1644
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001645 // Routing?
1646 mInputRoutes.incRouteActivity(session);
1647
Eric Laurent313d1e72016-01-29 09:56:57 -08001648 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001649
Eric Laurentfb66dd92016-01-28 18:32:03 -08001650 setInputDevice(input, getNewInputDevice(inputDesc), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001651
Jean-Michel Trivi56afc7a2016-02-03 11:35:52 -08001652 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001653 // if input maps to a dynamic policy with an activity listener, notify of state change
1654 if ((inputDesc->mPolicyMix != NULL)
1655 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1656 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1657 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001658 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001659
1660 if (mInputs.activeInputsCount() == 0) {
1661 SoundTrigger::setCaptureState(true);
1662 }
1663
1664 // automatically enable the remote submix output when input is started if not
1665 // used by a policy mix of type MIX_TYPE_RECORDERS
1666 // For remote submix (a virtual device), we open only one input per capture request.
1667 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1668 String8 address = String8("");
1669 if (inputDesc->mPolicyMix == NULL) {
1670 address = String8("0");
1671 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1672 address = inputDesc->mPolicyMix->mRegistrationId;
1673 }
1674 if (address != "") {
1675 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1676 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1677 address, "remote-submix");
1678 }
Eric Laurentc722f302014-12-10 11:21:49 -08001679 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001680 }
Eric Laurente552edb2014-03-10 17:42:56 -07001681 }
1682
Eric Laurent599c7582015-12-07 18:05:55 -08001683 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001684
Eric Laurente552edb2014-03-10 17:42:56 -07001685 return NO_ERROR;
1686}
1687
Eric Laurent4dc68062014-07-28 17:26:49 -07001688status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1689 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001690{
1691 ALOGV("stopInput() input %d", input);
1692 ssize_t index = mInputs.indexOfKey(input);
1693 if (index < 0) {
1694 ALOGW("stopInput() unknown input %d", input);
1695 return BAD_VALUE;
1696 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001697 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001698
Eric Laurent599c7582015-12-07 18:05:55 -08001699 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001700 if (index < 0) {
1701 ALOGW("stopInput() unknown session %d on input %d", session, input);
1702 return BAD_VALUE;
1703 }
1704
Eric Laurent599c7582015-12-07 18:05:55 -08001705 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001706 ALOGW("stopInput() input %d already stopped", input);
1707 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001708 }
1709
Eric Laurent599c7582015-12-07 18:05:55 -08001710 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001711
1712 // Routing?
1713 mInputRoutes.decRouteActivity(session);
1714
Eric Laurentfb66dd92016-01-28 18:32:03 -08001715 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001716
Eric Laurentfb66dd92016-01-28 18:32:03 -08001717 if (inputDesc->isActive()) {
1718 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1719 } else {
1720 // if input maps to a dynamic policy with an activity listener, notify of state change
1721 if ((inputDesc->mPolicyMix != NULL)
1722 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1723 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1724 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001725 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001726
1727 // automatically disable the remote submix output when input is stopped if not
1728 // used by a policy mix of type MIX_TYPE_RECORDERS
1729 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1730 String8 address = String8("");
1731 if (inputDesc->mPolicyMix == NULL) {
1732 address = String8("0");
1733 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1734 address = inputDesc->mPolicyMix->mRegistrationId;
1735 }
1736 if (address != "") {
1737 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1738 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1739 address, "remote-submix");
1740 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001741 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001742
Eric Laurentfb66dd92016-01-28 18:32:03 -08001743 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001744
Eric Laurentfb66dd92016-01-28 18:32:03 -08001745 if (mInputs.activeInputsCount() == 0) {
1746 SoundTrigger::setCaptureState(false);
1747 }
1748 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00001749 }
Eric Laurente552edb2014-03-10 17:42:56 -07001750 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001751 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001752}
1753
Eric Laurent4dc68062014-07-28 17:26:49 -07001754void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1755 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001756{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001757
Eric Laurente552edb2014-03-10 17:42:56 -07001758 ALOGV("releaseInput() %d", input);
1759 ssize_t index = mInputs.indexOfKey(input);
1760 if (index < 0) {
1761 ALOGW("releaseInput() releasing unknown input %d", input);
1762 return;
1763 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001764
1765 // Routing
1766 mInputRoutes.removeRoute(session);
1767
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001768 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1769 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001770
Eric Laurent599c7582015-12-07 18:05:55 -08001771 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001772 if (index < 0) {
1773 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1774 return;
1775 }
Eric Laurent599c7582015-12-07 18:05:55 -08001776
1777 if (audioSession->openCount() == 0) {
1778 ALOGW("releaseInput() invalid open count %d on session %d",
1779 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001780 return;
1781 }
Eric Laurent599c7582015-12-07 18:05:55 -08001782
1783 if (audioSession->changeOpenCount(-1) == 0) {
1784 inputDesc->removeAudioSession(session);
1785 }
1786
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001787 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001788 ALOGV("releaseInput() exit > 0");
1789 return;
1790 }
1791
Eric Laurent05b90f82014-08-27 15:32:29 -07001792 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001793 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001794 ALOGV("releaseInput() exit");
1795}
1796
Eric Laurentd4692962014-05-05 18:13:44 -07001797void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001798 bool patchRemoved = false;
1799
Eric Laurentd4692962014-05-05 18:13:44 -07001800 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001801 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1802 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1803 if (patch_index >= 0) {
1804 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1805 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1806 mAudioPatches.removeItemsAt(patch_index);
1807 patchRemoved = true;
1808 }
Eric Laurentd4692962014-05-05 18:13:44 -07001809 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1810 }
1811 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001812 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001813 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001814
1815 if (patchRemoved) {
1816 mpClientInterface->onAudioPatchListUpdate();
1817 }
Eric Laurentd4692962014-05-05 18:13:44 -07001818}
1819
Eric Laurente0720872014-03-11 09:30:41 -07001820void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001821 int indexMin,
1822 int indexMax)
1823{
1824 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001825 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001826 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001827 mVolumeCurves->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001828 }
Eric Laurente552edb2014-03-10 17:42:56 -07001829}
1830
Eric Laurente0720872014-03-11 09:30:41 -07001831status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001832 int index,
1833 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001834{
1835
François Gaffied1ab2bd2015-12-02 18:20:06 +01001836 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1837 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001838 return BAD_VALUE;
1839 }
1840 if (!audio_is_output_device(device)) {
1841 return BAD_VALUE;
1842 }
1843
1844 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001845 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001846
1847 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1848 stream, device, index);
1849
1850 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1851 // clear all device specific values
1852 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001853 mVolumeCurves->clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001854 }
François Gaffied1ab2bd2015-12-02 18:20:06 +01001855 mVolumeCurves->addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001856
Eric Laurent31551f82014-10-10 18:21:56 -07001857 // update volume on all outputs whose current device is also selected by the same
1858 // strategy as the device specified by the caller
Eric Laurente04e62b2016-01-29 18:25:01 -08001859 audio_devices_t selectedDevices = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1860 // it is possible that the requested device is not selected by the strategy (e.g an explicit
1861 // audio patch is active causing getDevicesForStream() to return this device. We must make
1862 // sure that the device passed is part of the devices considered when applying volume below.
1863 selectedDevices |= device;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001864
1865 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1866 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1867 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001868 mVolumeCurves->addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001869 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1870 }
Eric Laurente04e62b2016-01-29 18:25:01 -08001871
Eric Laurente552edb2014-03-10 17:42:56 -07001872 status_t status = NO_ERROR;
1873 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001874 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1875 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurente04e62b2016-01-29 18:25:01 -08001876 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & selectedDevices) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001877 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001878 if (volStatus != NO_ERROR) {
1879 status = volStatus;
1880 }
1881 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001882 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1883 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1884 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001885 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001886 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001887 }
Eric Laurente552edb2014-03-10 17:42:56 -07001888 }
1889 return status;
1890}
1891
Eric Laurente0720872014-03-11 09:30:41 -07001892status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001893 int *index,
1894 audio_devices_t device)
1895{
1896 if (index == NULL) {
1897 return BAD_VALUE;
1898 }
1899 if (!audio_is_output_device(device)) {
1900 return BAD_VALUE;
1901 }
1902 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1903 // the strategy the stream belongs to.
1904 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1905 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1906 }
François Gaffiedfd74092015-03-19 12:10:59 +01001907 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001908
François Gaffied1ab2bd2015-12-02 18:20:06 +01001909 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001910 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1911 return NO_ERROR;
1912}
1913
Eric Laurente0720872014-03-11 09:30:41 -07001914audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001915 const SortedVector<audio_io_handle_t>& outputs)
1916{
1917 // select one output among several suitable for global effects.
1918 // The priority is as follows:
1919 // 1: An offloaded output. If the effect ends up not being offloadable,
1920 // AudioFlinger will invalidate the track and the offloaded output
1921 // will be closed causing the effect to be moved to a PCM output.
1922 // 2: A deep buffer output
1923 // 3: the first output in the list
1924
1925 if (outputs.size() == 0) {
1926 return 0;
1927 }
1928
1929 audio_io_handle_t outputOffloaded = 0;
1930 audio_io_handle_t outputDeepBuffer = 0;
1931
1932 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001933 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001934 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001935 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1936 outputOffloaded = outputs[i];
1937 }
1938 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1939 outputDeepBuffer = outputs[i];
1940 }
1941 }
1942
1943 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1944 outputOffloaded, outputDeepBuffer);
1945 if (outputOffloaded != 0) {
1946 return outputOffloaded;
1947 }
1948 if (outputDeepBuffer != 0) {
1949 return outputDeepBuffer;
1950 }
1951
1952 return outputs[0];
1953}
1954
Eric Laurente0720872014-03-11 09:30:41 -07001955audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001956{
1957 // apply simple rule where global effects are attached to the same output as MUSIC streams
1958
Eric Laurent3b73df72014-03-11 09:06:29 -07001959 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001960 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1961 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1962
1963 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1964 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1965 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1966
1967 return output;
1968}
1969
Eric Laurente0720872014-03-11 09:30:41 -07001970status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001971 audio_io_handle_t io,
1972 uint32_t strategy,
1973 int session,
1974 int id)
1975{
1976 ssize_t index = mOutputs.indexOfKey(io);
1977 if (index < 0) {
1978 index = mInputs.indexOfKey(io);
1979 if (index < 0) {
1980 ALOGW("registerEffect() unknown io %d", io);
1981 return INVALID_OPERATION;
1982 }
1983 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001984 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001985}
1986
Eric Laurentc75307b2015-03-17 15:29:32 -07001987bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1988{
1989 return mOutputs.isStreamActive(stream, inPastMs);
1990}
1991
1992bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1993{
1994 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1995}
1996
Eric Laurente0720872014-03-11 09:30:41 -07001997bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001998{
1999 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002000 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002001 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002002 return true;
2003 }
2004 }
2005 return false;
2006}
2007
Eric Laurent275e8e92014-11-30 15:14:47 -08002008// Register a list of custom mixes with their attributes and format.
2009// When a mix is registered, corresponding input and output profiles are
2010// added to the remote submix hw module. The profile contains only the
2011// parameters (sampling rate, format...) specified by the mix.
2012// The corresponding input remote submix device is also connected.
2013//
2014// When a remote submix device is connected, the address is checked to select the
2015// appropriate profile and the corresponding input or output stream is opened.
2016//
2017// When capture starts, getInputForAttr() will:
2018// - 1 look for a mix matching the address passed in attribtutes tags if any
2019// - 2 if none found, getDeviceForInputSource() will:
2020// - 2.1 look for a mix matching the attributes source
2021// - 2.2 if none found, default to device selection by policy rules
2022// At this time, the corresponding output remote submix device is also connected
2023// and active playback use cases can be transferred to this mix if needed when reconnecting
2024// after AudioTracks are invalidated
2025//
2026// When playback starts, getOutputForAttr() will:
2027// - 1 look for a mix matching the address passed in attribtutes tags if any
2028// - 2 if none found, look for a mix matching the attributes usage
2029// - 3 if none found, default to device and output selection by policy rules.
2030
2031status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2032{
2033 sp<HwModule> module;
2034 for (size_t i = 0; i < mHwModules.size(); i++) {
2035 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2036 mHwModules[i]->mHandle != 0) {
2037 module = mHwModules[i];
2038 break;
2039 }
2040 }
2041
2042 if (module == 0) {
2043 return INVALID_OPERATION;
2044 }
2045
Eric Laurent7b279bb2015-12-14 10:18:23 -08002046 ALOGV("registerPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002047
2048 for (size_t i = 0; i < mixes.size(); i++) {
2049 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002050
2051 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002052 continue;
2053 }
2054 audio_config_t outputConfig = mixes[i].mFormat;
2055 audio_config_t inputConfig = mixes[i].mFormat;
2056 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2057 // stereo and let audio flinger do the channel conversion if needed.
2058 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2059 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2060 module->addOutputProfile(address, &outputConfig,
2061 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2062 module->addInputProfile(address, &inputConfig,
2063 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002064
Eric Laurentc722f302014-12-10 11:21:49 -08002065 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002066 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002067 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002068 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002069 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002070 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002071 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002072 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002073 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002074 }
2075 return NO_ERROR;
2076}
2077
2078status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2079{
2080 sp<HwModule> module;
2081 for (size_t i = 0; i < mHwModules.size(); i++) {
2082 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2083 mHwModules[i]->mHandle != 0) {
2084 module = mHwModules[i];
2085 break;
2086 }
2087 }
2088
2089 if (module == 0) {
2090 return INVALID_OPERATION;
2091 }
2092
Eric Laurent7b279bb2015-12-14 10:18:23 -08002093 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002094
2095 for (size_t i = 0; i < mixes.size(); i++) {
2096 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002097
2098 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002099 continue;
2100 }
2101
Eric Laurentc722f302014-12-10 11:21:49 -08002102 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2103 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2104 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002105 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002106 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002107 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002108 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002109
2110 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2111 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2112 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002113 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002114 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002115 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002116 }
2117 module->removeOutputProfile(address);
2118 module->removeInputProfile(address);
2119 }
2120 return NO_ERROR;
2121}
2122
Eric Laurente552edb2014-03-10 17:42:56 -07002123
Eric Laurente0720872014-03-11 09:30:41 -07002124status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002125{
2126 const size_t SIZE = 256;
2127 char buffer[SIZE];
2128 String8 result;
2129
2130 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2131 result.append(buffer);
2132
Eric Laurent87ffa392015-05-22 10:32:38 -07002133 snprintf(buffer, SIZE, " Primary Output: %d\n",
2134 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002135 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002136 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002137 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002138 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002139 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002140 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002141 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002142 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002143 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002144 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002145 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002146 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002147 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002148 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002149 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002150 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002151 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002152 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2153 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002154 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2155 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002156
François Gaffie2110e042015-03-24 08:41:51 +01002157 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002158
François Gaffie112b0af2015-11-19 16:13:25 +01002159 mAvailableOutputDevices.dump(fd, String8("Available output"));
2160 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002161 mHwModules.dump(fd);
2162 mOutputs.dump(fd);
2163 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002164 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002165 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002166 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002167
2168 return NO_ERROR;
2169}
2170
2171// This function checks for the parameters which can be offloaded.
2172// This can be enhanced depending on the capability of the DSP and policy
2173// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002174bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002175{
2176 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002177 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002178 offloadInfo.sample_rate, offloadInfo.channel_mask,
2179 offloadInfo.format,
2180 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2181 offloadInfo.has_video);
2182
Andy Hung2ddee192015-12-18 17:34:44 -08002183 if (mMasterMono) {
2184 return false; // no offloading if mono is set.
2185 }
2186
Eric Laurente552edb2014-03-10 17:42:56 -07002187 // Check if offload has been disabled
2188 char propValue[PROPERTY_VALUE_MAX];
2189 if (property_get("audio.offload.disable", propValue, "0")) {
2190 if (atoi(propValue) != 0) {
2191 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2192 return false;
2193 }
2194 }
2195
2196 // Check if stream type is music, then only allow offload as of now.
2197 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2198 {
2199 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2200 return false;
2201 }
2202
Wei Jia7b8d4342016-02-16 17:56:37 -08002203 // Check if streaming is off, then only allow offload as of now.
2204 if (offloadInfo.is_streaming)
2205 {
2206 ALOGV("isOffloadSupported: is_streaming == true, returning false");
2207 return false;
2208 }
2209
Eric Laurente552edb2014-03-10 17:42:56 -07002210 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002211 const bool allowOffloadWithVideo =
2212 property_get_bool("audio.offload.video", false /* default_value */);
2213 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002214 ALOGV("isOffloadSupported: has_video == true, returning false");
2215 return false;
2216 }
2217
2218 //If duration is less than minimum value defined in property, return false
2219 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2220 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2221 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2222 return false;
2223 }
2224 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2225 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2226 return false;
2227 }
2228
2229 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2230 // creating an offloaded track and tearing it down immediately after start when audioflinger
2231 // detects there is an active non offloadable effect.
2232 // FIXME: We should check the audio session here but we do not have it in this context.
2233 // This may prevent offloading in rare situations where effects are left active by apps
2234 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002235 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002236 return false;
2237 }
2238
2239 // See if there is a profile to support this.
2240 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002241 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002242 offloadInfo.sample_rate,
2243 offloadInfo.format,
2244 offloadInfo.channel_mask,
2245 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002246 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2247 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002248}
2249
Eric Laurent6a94d692014-05-20 11:18:06 -07002250status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2251 audio_port_type_t type,
2252 unsigned int *num_ports,
2253 struct audio_port *ports,
2254 unsigned int *generation)
2255{
2256 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2257 generation == NULL) {
2258 return BAD_VALUE;
2259 }
2260 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2261 if (ports == NULL) {
2262 *num_ports = 0;
2263 }
2264
2265 size_t portsWritten = 0;
2266 size_t portsMax = *num_ports;
2267 *num_ports = 0;
2268 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2269 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2270 for (size_t i = 0;
2271 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2272 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2273 }
2274 *num_ports += mAvailableOutputDevices.size();
2275 }
2276 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2277 for (size_t i = 0;
2278 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2279 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2280 }
2281 *num_ports += mAvailableInputDevices.size();
2282 }
2283 }
2284 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2285 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2286 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2287 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2288 }
2289 *num_ports += mInputs.size();
2290 }
2291 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002292 size_t numOutputs = 0;
2293 for (size_t i = 0; i < mOutputs.size(); i++) {
2294 if (!mOutputs[i]->isDuplicated()) {
2295 numOutputs++;
2296 if (portsWritten < portsMax) {
2297 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2298 }
2299 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002300 }
Eric Laurent84c70242014-06-23 08:46:27 -07002301 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002302 }
2303 }
2304 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002305 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002306 return NO_ERROR;
2307}
2308
2309status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2310{
2311 return NO_ERROR;
2312}
2313
Eric Laurent6a94d692014-05-20 11:18:06 -07002314status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2315 audio_patch_handle_t *handle,
2316 uid_t uid)
2317{
2318 ALOGV("createAudioPatch()");
2319
2320 if (handle == NULL || patch == NULL) {
2321 return BAD_VALUE;
2322 }
2323 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2324
Eric Laurent874c42872014-08-08 15:13:39 -07002325 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2326 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2327 return BAD_VALUE;
2328 }
2329 // only one source per audio patch supported for now
2330 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002331 return INVALID_OPERATION;
2332 }
Eric Laurent874c42872014-08-08 15:13:39 -07002333
2334 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002335 return INVALID_OPERATION;
2336 }
Eric Laurent874c42872014-08-08 15:13:39 -07002337 for (size_t i = 0; i < patch->num_sinks; i++) {
2338 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2339 return INVALID_OPERATION;
2340 }
2341 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002342
2343 sp<AudioPatch> patchDesc;
2344 ssize_t index = mAudioPatches.indexOfKey(*handle);
2345
Eric Laurent6a94d692014-05-20 11:18:06 -07002346 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2347 patch->sources[0].role,
2348 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002349#if LOG_NDEBUG == 0
2350 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002351 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002352 patch->sinks[i].role,
2353 patch->sinks[i].type);
2354 }
2355#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002356
2357 if (index >= 0) {
2358 patchDesc = mAudioPatches.valueAt(index);
2359 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2360 mUidCached, patchDesc->mUid, uid);
2361 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2362 return INVALID_OPERATION;
2363 }
2364 } else {
2365 *handle = 0;
2366 }
2367
2368 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002369 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002370 if (outputDesc == NULL) {
2371 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2372 return BAD_VALUE;
2373 }
Eric Laurent84c70242014-06-23 08:46:27 -07002374 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2375 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002376 if (patchDesc != 0) {
2377 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2378 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2379 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2380 return BAD_VALUE;
2381 }
2382 }
Eric Laurent874c42872014-08-08 15:13:39 -07002383 DeviceVector devices;
2384 for (size_t i = 0; i < patch->num_sinks; i++) {
2385 // Only support mix to devices connection
2386 // TODO add support for mix to mix connection
2387 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2388 ALOGV("createAudioPatch() source mix but sink is not a device");
2389 return INVALID_OPERATION;
2390 }
2391 sp<DeviceDescriptor> devDesc =
2392 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2393 if (devDesc == 0) {
2394 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2395 return BAD_VALUE;
2396 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002397
François Gaffie53615e22015-03-19 09:24:12 +01002398 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002399 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002400 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002401 NULL, // updatedSamplingRate
2402 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002403 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002404 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002405 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002406 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002407 ALOGV("createAudioPatch() profile not supported for device %08x",
2408 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002409 return INVALID_OPERATION;
2410 }
2411 devices.add(devDesc);
2412 }
2413 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002414 return INVALID_OPERATION;
2415 }
Eric Laurent874c42872014-08-08 15:13:39 -07002416
Eric Laurent6a94d692014-05-20 11:18:06 -07002417 // TODO: reconfigure output format and channels here
2418 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002419 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002420 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002421 index = mAudioPatches.indexOfKey(*handle);
2422 if (index >= 0) {
2423 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2424 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2425 }
2426 patchDesc = mAudioPatches.valueAt(index);
2427 patchDesc->mUid = uid;
2428 ALOGV("createAudioPatch() success");
2429 } else {
2430 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2431 return INVALID_OPERATION;
2432 }
2433 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2434 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2435 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002436 // only one sink supported when connecting an input device to a mix
2437 if (patch->num_sinks > 1) {
2438 return INVALID_OPERATION;
2439 }
François Gaffie53615e22015-03-19 09:24:12 +01002440 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002441 if (inputDesc == NULL) {
2442 return BAD_VALUE;
2443 }
2444 if (patchDesc != 0) {
2445 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2446 return BAD_VALUE;
2447 }
2448 }
2449 sp<DeviceDescriptor> devDesc =
2450 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2451 if (devDesc == 0) {
2452 return BAD_VALUE;
2453 }
2454
François Gaffie53615e22015-03-19 09:24:12 +01002455 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002456 devDesc->mAddress,
2457 patch->sinks[0].sample_rate,
2458 NULL, /*updatedSampleRate*/
2459 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002460 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002461 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002462 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002463 // FIXME for the parameter type,
2464 // and the NONE
2465 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002466 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002467 return INVALID_OPERATION;
2468 }
2469 // TODO: reconfigure output format and channels here
2470 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002471 devDesc->type(), inputDesc->mIoHandle);
2472 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002473 index = mAudioPatches.indexOfKey(*handle);
2474 if (index >= 0) {
2475 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2476 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2477 }
2478 patchDesc = mAudioPatches.valueAt(index);
2479 patchDesc->mUid = uid;
2480 ALOGV("createAudioPatch() success");
2481 } else {
2482 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2483 return INVALID_OPERATION;
2484 }
2485 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2486 // device to device connection
2487 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002488 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002489 return BAD_VALUE;
2490 }
2491 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002492 sp<DeviceDescriptor> srcDeviceDesc =
2493 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002494 if (srcDeviceDesc == 0) {
2495 return BAD_VALUE;
2496 }
Eric Laurent874c42872014-08-08 15:13:39 -07002497
Eric Laurent6a94d692014-05-20 11:18:06 -07002498 //update source and sink with our own data as the data passed in the patch may
2499 // be incomplete.
2500 struct audio_patch newPatch = *patch;
2501 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002502
Eric Laurent874c42872014-08-08 15:13:39 -07002503 for (size_t i = 0; i < patch->num_sinks; i++) {
2504 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2505 ALOGV("createAudioPatch() source device but one sink is not a device");
2506 return INVALID_OPERATION;
2507 }
2508
2509 sp<DeviceDescriptor> sinkDeviceDesc =
2510 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2511 if (sinkDeviceDesc == 0) {
2512 return BAD_VALUE;
2513 }
2514 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2515
Eric Laurent3bcf8592015-04-03 12:13:24 -07002516 // create a software bridge in PatchPanel if:
2517 // - source and sink devices are on differnt HW modules OR
2518 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002519 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002520 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002521 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002522 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002523 return INVALID_OPERATION;
2524 }
Eric Laurent874c42872014-08-08 15:13:39 -07002525 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002526 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002527 // if the sink device is reachable via an opened output stream, request to go via
2528 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002529 audio_io_handle_t output = selectOutput(outputs,
2530 AUDIO_OUTPUT_FLAG_NONE,
2531 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002532 if (output != AUDIO_IO_HANDLE_NONE) {
2533 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2534 if (outputDesc->isDuplicated()) {
2535 return INVALID_OPERATION;
2536 }
2537 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002538 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002539 newPatch.num_sources = 2;
2540 }
Eric Laurent83b88082014-06-20 18:31:16 -07002541 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002542 }
2543 // TODO: check from routing capabilities in config file and other conflicting patches
2544
2545 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2546 if (index >= 0) {
2547 afPatchHandle = patchDesc->mAfPatchHandle;
2548 }
2549
2550 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2551 &afPatchHandle,
2552 0);
2553 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2554 status, afPatchHandle);
2555 if (status == NO_ERROR) {
2556 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002557 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002558 addAudioPatch(patchDesc->mHandle, patchDesc);
2559 } else {
2560 patchDesc->mPatch = newPatch;
2561 }
2562 patchDesc->mAfPatchHandle = afPatchHandle;
2563 *handle = patchDesc->mHandle;
2564 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002565 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002566 } else {
2567 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2568 status);
2569 return INVALID_OPERATION;
2570 }
2571 } else {
2572 return BAD_VALUE;
2573 }
2574 } else {
2575 return BAD_VALUE;
2576 }
2577 return NO_ERROR;
2578}
2579
2580status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2581 uid_t uid)
2582{
2583 ALOGV("releaseAudioPatch() patch %d", handle);
2584
2585 ssize_t index = mAudioPatches.indexOfKey(handle);
2586
2587 if (index < 0) {
2588 return BAD_VALUE;
2589 }
2590 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2591 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2592 mUidCached, patchDesc->mUid, uid);
2593 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2594 return INVALID_OPERATION;
2595 }
2596
2597 struct audio_patch *patch = &patchDesc->mPatch;
2598 patchDesc->mUid = mUidCached;
2599 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002600 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002601 if (outputDesc == NULL) {
2602 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2603 return BAD_VALUE;
2604 }
2605
Eric Laurentc75307b2015-03-17 15:29:32 -07002606 setOutputDevice(outputDesc,
2607 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002608 true,
2609 0,
2610 NULL);
2611 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2612 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002613 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002614 if (inputDesc == NULL) {
2615 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2616 return BAD_VALUE;
2617 }
2618 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002619 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002620 true,
2621 NULL);
2622 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2623 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2624 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2625 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2626 status, patchDesc->mAfPatchHandle);
2627 removeAudioPatch(patchDesc->mHandle);
2628 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002629 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002630 } else {
2631 return BAD_VALUE;
2632 }
2633 } else {
2634 return BAD_VALUE;
2635 }
2636 return NO_ERROR;
2637}
2638
2639status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2640 struct audio_patch *patches,
2641 unsigned int *generation)
2642{
François Gaffie53615e22015-03-19 09:24:12 +01002643 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002644 return BAD_VALUE;
2645 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002646 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002647 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002648}
2649
Eric Laurente1715a42014-05-20 11:30:42 -07002650status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002651{
Eric Laurente1715a42014-05-20 11:30:42 -07002652 ALOGV("setAudioPortConfig()");
2653
2654 if (config == NULL) {
2655 return BAD_VALUE;
2656 }
2657 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2658 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002659 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2660 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002661 }
2662
Eric Laurenta121f902014-06-03 13:32:54 -07002663 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002664 if (config->type == AUDIO_PORT_TYPE_MIX) {
2665 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002666 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002667 if (outputDesc == NULL) {
2668 return BAD_VALUE;
2669 }
Eric Laurent84c70242014-06-23 08:46:27 -07002670 ALOG_ASSERT(!outputDesc->isDuplicated(),
2671 "setAudioPortConfig() called on duplicated output %d",
2672 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002673 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002674 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002675 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002676 if (inputDesc == NULL) {
2677 return BAD_VALUE;
2678 }
Eric Laurenta121f902014-06-03 13:32:54 -07002679 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002680 } else {
2681 return BAD_VALUE;
2682 }
2683 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2684 sp<DeviceDescriptor> deviceDesc;
2685 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2686 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2687 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2688 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2689 } else {
2690 return BAD_VALUE;
2691 }
2692 if (deviceDesc == NULL) {
2693 return BAD_VALUE;
2694 }
Eric Laurenta121f902014-06-03 13:32:54 -07002695 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002696 } else {
2697 return BAD_VALUE;
2698 }
2699
Eric Laurenta121f902014-06-03 13:32:54 -07002700 struct audio_port_config backupConfig;
2701 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2702 if (status == NO_ERROR) {
2703 struct audio_port_config newConfig;
2704 audioPortConfig->toAudioPortConfig(&newConfig, config);
2705 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002706 }
Eric Laurenta121f902014-06-03 13:32:54 -07002707 if (status != NO_ERROR) {
2708 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002709 }
Eric Laurente1715a42014-05-20 11:30:42 -07002710
2711 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002712}
2713
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002714void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2715{
Eric Laurentd60560a2015-04-10 11:31:20 -07002716 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002717 clearAudioPatches(uid);
2718 clearSessionRoutes(uid);
2719}
2720
Eric Laurent6a94d692014-05-20 11:18:06 -07002721void AudioPolicyManager::clearAudioPatches(uid_t uid)
2722{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002723 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002724 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2725 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002726 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002727 }
2728 }
2729}
2730
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002731void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2732 audio_io_handle_t ouptutToSkip)
2733{
2734 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2735 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2736 for (size_t j = 0; j < mOutputs.size(); j++) {
2737 if (mOutputs.keyAt(j) == ouptutToSkip) {
2738 continue;
2739 }
2740 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2741 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2742 continue;
2743 }
2744 // If the default device for this strategy is on another output mix,
2745 // invalidate all tracks in this strategy to force re connection.
2746 // Otherwise select new device on the output mix.
2747 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2748 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2749 if (stream == AUDIO_STREAM_PATCH) {
2750 continue;
2751 }
2752 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2753 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2754 }
2755 }
2756 } else {
2757 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2758 setOutputDevice(outputDesc, newDevice, false);
2759 }
2760 }
2761}
2762
2763void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2764{
2765 // remove output routes associated with this uid
2766 SortedVector<routing_strategy> affectedStrategies;
2767 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2768 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2769 if (route->mUid == uid) {
2770 mOutputRoutes.removeItemsAt(i);
2771 if (route->mDeviceDescriptor != 0) {
2772 affectedStrategies.add(getStrategy(route->mStreamType));
2773 }
2774 }
2775 }
2776 // reroute outputs if necessary
2777 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2778 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2779 }
2780
2781 // remove input routes associated with this uid
2782 SortedVector<audio_source_t> affectedSources;
2783 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2784 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2785 if (route->mUid == uid) {
2786 mInputRoutes.removeItemsAt(i);
2787 if (route->mDeviceDescriptor != 0) {
2788 affectedSources.add(route->mSource);
2789 }
2790 }
2791 }
2792 // reroute inputs if necessary
2793 SortedVector<audio_io_handle_t> inputsToClose;
2794 for (size_t i = 0; i < mInputs.size(); i++) {
2795 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002796 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002797 inputsToClose.add(inputDesc->mIoHandle);
2798 }
2799 }
2800 for (size_t i = 0; i < inputsToClose.size(); i++) {
2801 closeInput(inputsToClose[i]);
2802 }
2803}
2804
Eric Laurentd60560a2015-04-10 11:31:20 -07002805void AudioPolicyManager::clearAudioSources(uid_t uid)
2806{
2807 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2808 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2809 if (sourceDesc->mUid == uid) {
2810 stopAudioSource(mAudioSources.keyAt(i));
2811 }
2812 }
2813}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002814
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002815status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2816 audio_io_handle_t *ioHandle,
2817 audio_devices_t *device)
2818{
2819 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2820 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002821 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002822
François Gaffiedf372692015-03-19 10:43:27 +01002823 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002824}
2825
Eric Laurentd60560a2015-04-10 11:31:20 -07002826status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2827 const audio_attributes_t *attributes,
2828 audio_io_handle_t *handle,
2829 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002830{
Eric Laurentd60560a2015-04-10 11:31:20 -07002831 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2832 if (source == NULL || attributes == NULL || handle == NULL) {
2833 return BAD_VALUE;
2834 }
2835
2836 *handle = AUDIO_IO_HANDLE_NONE;
2837
2838 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2839 source->type != AUDIO_PORT_TYPE_DEVICE) {
2840 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2841 return INVALID_OPERATION;
2842 }
2843
2844 sp<DeviceDescriptor> srcDeviceDesc =
2845 mAvailableInputDevices.getDevice(source->ext.device.type,
2846 String8(source->ext.device.address));
2847 if (srcDeviceDesc == 0) {
2848 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2849 return BAD_VALUE;
2850 }
2851 sp<AudioSourceDescriptor> sourceDesc =
2852 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2853
2854 struct audio_patch dummyPatch;
2855 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2856 sourceDesc->mPatchDesc = patchDesc;
2857
2858 status_t status = connectAudioSource(sourceDesc);
2859 if (status == NO_ERROR) {
2860 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2861 *handle = sourceDesc->getHandle();
2862 }
2863 return status;
2864}
2865
2866status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2867{
2868 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2869
2870 // make sure we only have one patch per source.
2871 disconnectAudioSource(sourceDesc);
2872
2873 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
2874 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
2875 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2876
2877 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2878 sp<DeviceDescriptor> sinkDeviceDesc =
2879 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2880
2881 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2882 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2883
2884 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2885 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002886 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002887 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2888 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2889 // create patch between src device and output device
2890 // create Hwoutput and add to mHwOutputs
2891 } else {
2892 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2893 audio_io_handle_t output =
2894 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2895 if (output == AUDIO_IO_HANDLE_NONE) {
2896 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2897 return INVALID_OPERATION;
2898 }
2899 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2900 if (outputDesc->isDuplicated()) {
2901 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2902 return INVALID_OPERATION;
2903 }
2904 // create a special patch with no sink and two sources:
2905 // - the second source indicates to PatchPanel through which output mix this patch should
2906 // be connected as well as the stream type for volume control
2907 // - the sink is defined by whatever output device is currently selected for the output
2908 // though which this patch is routed.
2909 patch->num_sinks = 0;
2910 patch->num_sources = 2;
2911 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
2912 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
2913 patch->sources[1].ext.mix.usecase.stream = stream;
2914 status_t status = mpClientInterface->createAudioPatch(patch,
2915 &afPatchHandle,
2916 0);
2917 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
2918 status, afPatchHandle);
2919 if (status != NO_ERROR) {
2920 ALOGW("%s patch panel could not connect device patch, error %d",
2921 __FUNCTION__, status);
2922 return INVALID_OPERATION;
2923 }
2924 uint32_t delayMs = 0;
2925 status = startSource(outputDesc, stream, sinkDevice, &delayMs);
2926
2927 if (status != NO_ERROR) {
2928 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
2929 return status;
2930 }
2931 sourceDesc->mSwOutput = outputDesc;
2932 if (delayMs != 0) {
2933 usleep(delayMs * 1000);
2934 }
2935 }
2936
2937 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
2938 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
2939
2940 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07002941}
2942
Eric Laurent493404d2015-04-21 15:07:36 -07002943status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002944{
Eric Laurentd60560a2015-04-10 11:31:20 -07002945 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
2946 ALOGV("%s handle %d", __FUNCTION__, handle);
2947 if (sourceDesc == 0) {
2948 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
2949 return BAD_VALUE;
2950 }
2951 status_t status = disconnectAudioSource(sourceDesc);
2952
2953 mAudioSources.removeItem(handle);
2954 return status;
2955}
2956
Andy Hung2ddee192015-12-18 17:34:44 -08002957status_t AudioPolicyManager::setMasterMono(bool mono)
2958{
2959 if (mMasterMono == mono) {
2960 return NO_ERROR;
2961 }
2962 mMasterMono = mono;
2963 // if enabling mono we close all offloaded devices, which will invalidate the
2964 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
2965 // for recreating the new AudioTrack as non-offloaded PCM.
2966 //
2967 // If disabling mono, we leave all tracks as is: we don't know which clients
2968 // and tracks are able to be recreated as offloaded. The next "song" should
2969 // play back offloaded.
2970 if (mMasterMono) {
2971 Vector<audio_io_handle_t> offloaded;
2972 for (size_t i = 0; i < mOutputs.size(); ++i) {
2973 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2974 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2975 offloaded.push(desc->mIoHandle);
2976 }
2977 }
2978 for (size_t i = 0; i < offloaded.size(); ++i) {
2979 closeOutput(offloaded[i]);
2980 }
2981 }
2982 // update master mono for all remaining outputs
2983 for (size_t i = 0; i < mOutputs.size(); ++i) {
2984 updateMono(mOutputs.keyAt(i));
2985 }
2986 return NO_ERROR;
2987}
2988
2989status_t AudioPolicyManager::getMasterMono(bool *mono)
2990{
2991 *mono = mMasterMono;
2992 return NO_ERROR;
2993}
2994
Eric Laurentd60560a2015-04-10 11:31:20 -07002995status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2996{
2997 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2998
2999 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3000 if (patchDesc == 0) {
3001 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3002 sourceDesc->mPatchDesc->mHandle);
3003 return BAD_VALUE;
3004 }
3005 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3006
3007 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
3008 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3009 if (swOutputDesc != 0) {
3010 stopSource(swOutputDesc, stream, false);
3011 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3012 } else {
3013 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3014 if (hwOutputDesc != 0) {
3015 // release patch between src device and output device
3016 // close Hwoutput and remove from mHwOutputs
3017 } else {
3018 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3019 }
3020 }
3021 return NO_ERROR;
3022}
3023
3024sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3025 audio_io_handle_t output, routing_strategy strategy)
3026{
3027 sp<AudioSourceDescriptor> source;
3028 for (size_t i = 0; i < mAudioSources.size(); i++) {
3029 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3030 routing_strategy sourceStrategy =
3031 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3032 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3033 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3034 source = sourceDesc;
3035 break;
3036 }
3037 }
3038 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003039}
3040
Eric Laurente552edb2014-03-10 17:42:56 -07003041// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003042// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003043// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003044uint32_t AudioPolicyManager::nextAudioPortGeneration()
3045{
3046 return android_atomic_inc(&mAudioPortGeneration);
3047}
3048
Eric Laurente0720872014-03-11 09:30:41 -07003049AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003050 :
3051#ifdef AUDIO_POLICY_TEST
3052 Thread(false),
3053#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003054 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003055 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003056 mAudioPortGeneration(1),
3057 mBeaconMuteRefCount(0),
3058 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003059 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003060 mTtsOutputAvailable(false),
3061 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003062{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003063 mUidCached = getuid();
3064 mpClientInterface = clientInterface;
3065
3066 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3067 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3068 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3069 bool speakerDrcEnabled = false;
3070
3071#ifdef USE_XML_AUDIO_POLICY_CONF
3072 mVolumeCurves = new VolumeCurvesCollection();
3073 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3074 mDefaultOutputDevice, speakerDrcEnabled,
3075 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3076 PolicySerializer serializer;
3077 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3078#else
3079 mVolumeCurves = new StreamDescriptorCollection();
3080 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3081 mDefaultOutputDevice, speakerDrcEnabled);
3082 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3083 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3084#endif
3085 ALOGE("could not load audio policy configuration file, setting defaults");
3086 config.setDefault();
3087 }
3088 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3089 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3090
3091 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003092 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3093 if (!engineInstance) {
3094 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3095 return;
3096 }
3097 // Retrieve the Policy Manager Interface
3098 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3099 if (mEngine == NULL) {
3100 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3101 return;
3102 }
3103 mEngine->setObserver(this);
3104 status_t status = mEngine->initCheck();
3105 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3106
Eric Laurent3a4311c2014-03-17 12:00:47 -07003107 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003108 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003109 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3110 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003111 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003112 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003113 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003114 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003115 continue;
3116 }
3117 // open all output streams needed to access attached devices
3118 // except for direct output streams that are only opened when they are actually
3119 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003120 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003121 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3122 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003123 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003124
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003125 if (!outProfile->hasSupportedDevices()) {
3126 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003127 continue;
3128 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003129 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003130 mTtsOutputAvailable = true;
3131 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003132
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003133 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003134 continue;
3135 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003136 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003137 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3138 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003139 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003140 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003141 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003142 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003143 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003144 if ((profileType & outputDeviceTypes) == 0) {
3145 continue;
3146 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003147 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3148 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07003149
3150 outputDesc->mDevice = profileType;
3151 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3152 config.sample_rate = outputDesc->mSamplingRate;
3153 config.channel_mask = outputDesc->mChannelMask;
3154 config.format = outputDesc->mFormat;
3155 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003156 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003157 &output,
3158 &config,
3159 &outputDesc->mDevice,
3160 String8(""),
3161 &outputDesc->mLatency,
3162 outputDesc->mFlags);
3163
3164 if (status != NO_ERROR) {
3165 ALOGW("Cannot open output stream for device %08x on hw module %s",
3166 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003167 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003168 } else {
3169 outputDesc->mSamplingRate = config.sample_rate;
3170 outputDesc->mChannelMask = config.channel_mask;
3171 outputDesc->mFormat = config.format;
3172
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003173 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3174 for (size_t k = 0; k < supportedDevices.size(); k++) {
3175 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003176 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003177 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3178 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003179 }
3180 }
3181 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003182 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003183 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003184 }
3185 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003186 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003187 outputDesc->mDevice,
3188 true);
3189 }
Eric Laurente552edb2014-03-10 17:42:56 -07003190 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003191 // open input streams needed to access attached devices to validate
3192 // mAvailableInputDevices list
3193 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3194 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003195 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003196
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003197 if (!inProfile->hasSupportedDevices()) {
3198 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003199 continue;
3200 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003201 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003202 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003203 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3204
Eric Laurentd78f1532014-09-16 16:38:20 -07003205 if ((profileType & inputDeviceTypes) == 0) {
3206 continue;
3207 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003208 sp<AudioInputDescriptor> inputDesc =
3209 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003210
Eric Laurentd78f1532014-09-16 16:38:20 -07003211 inputDesc->mDevice = profileType;
3212
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003213 // find the address
3214 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3215 // the inputs vector must be of size 1, but we don't want to crash here
3216 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3217 : String8("");
3218 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3219 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3220
Eric Laurentd78f1532014-09-16 16:38:20 -07003221 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3222 config.sample_rate = inputDesc->mSamplingRate;
3223 config.channel_mask = inputDesc->mChannelMask;
3224 config.format = inputDesc->mFormat;
3225 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003226 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003227 &input,
3228 &config,
3229 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003230 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003231 AUDIO_SOURCE_MIC,
3232 AUDIO_INPUT_FLAG_NONE);
3233
3234 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003235 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3236 for (size_t k = 0; k < supportedDevices.size(); k++) {
3237 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003238 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003239 if (index >= 0) {
3240 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3241 if (!devDesc->isAttached()) {
3242 devDesc->attach(mHwModules[i]);
3243 devDesc->importAudioPort(inProfile);
3244 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003245 }
3246 }
3247 mpClientInterface->closeInput(input);
3248 } else {
3249 ALOGW("Cannot open input stream for device %08x on hw module %s",
3250 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003251 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003252 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003253 }
3254 }
3255 // make sure all attached devices have been allocated a unique ID
3256 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003257 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003259 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3260 continue;
3261 }
François Gaffie2110e042015-03-24 08:41:51 +01003262 // The device is now validated and can be appended to the available devices of the engine
3263 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3264 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003265 i++;
3266 }
3267 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003268 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003269 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003270 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3271 continue;
3272 }
François Gaffie2110e042015-03-24 08:41:51 +01003273 // The device is now validated and can be appended to the available devices of the engine
3274 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3275 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003276 i++;
3277 }
3278 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003279 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003280 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003281 }
Eric Laurente552edb2014-03-10 17:42:56 -07003282
3283 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3284
3285 updateDevicesAndOutputs();
3286
3287#ifdef AUDIO_POLICY_TEST
3288 if (mPrimaryOutput != 0) {
3289 AudioParameter outputCmd = AudioParameter();
3290 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003291 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003292
3293 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3294 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003295 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3296 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003297 mTestLatencyMs = 0;
3298 mCurOutput = 0;
3299 mDirectOutput = false;
3300 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3301 mTestOutputs[i] = 0;
3302 }
3303
3304 const size_t SIZE = 256;
3305 char buffer[SIZE];
3306 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3307 run(buffer, ANDROID_PRIORITY_AUDIO);
3308 }
3309#endif //AUDIO_POLICY_TEST
3310}
3311
Eric Laurente0720872014-03-11 09:30:41 -07003312AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003313{
3314#ifdef AUDIO_POLICY_TEST
3315 exit();
3316#endif //AUDIO_POLICY_TEST
3317 for (size_t i = 0; i < mOutputs.size(); i++) {
3318 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003319 }
3320 for (size_t i = 0; i < mInputs.size(); i++) {
3321 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003322 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003323 mAvailableOutputDevices.clear();
3324 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003325 mOutputs.clear();
3326 mInputs.clear();
3327 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003328}
3329
Eric Laurente0720872014-03-11 09:30:41 -07003330status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003331{
Eric Laurent87ffa392015-05-22 10:32:38 -07003332 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003333}
3334
3335#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003336bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003337{
3338 ALOGV("entering threadLoop()");
3339 while (!exitPending())
3340 {
3341 String8 command;
3342 int valueInt;
3343 String8 value;
3344
3345 Mutex::Autolock _l(mLock);
3346 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3347
3348 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3349 AudioParameter param = AudioParameter(command);
3350
3351 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3352 valueInt != 0) {
3353 ALOGV("Test command %s received", command.string());
3354 String8 target;
3355 if (param.get(String8("target"), target) != NO_ERROR) {
3356 target = "Manager";
3357 }
3358 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3359 param.remove(String8("test_cmd_policy_output"));
3360 mCurOutput = valueInt;
3361 }
3362 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3363 param.remove(String8("test_cmd_policy_direct"));
3364 if (value == "false") {
3365 mDirectOutput = false;
3366 } else if (value == "true") {
3367 mDirectOutput = true;
3368 }
3369 }
3370 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3371 param.remove(String8("test_cmd_policy_input"));
3372 mTestInput = valueInt;
3373 }
3374
3375 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3376 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003377 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003378 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003379 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003380 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003381 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003382 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003383 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003384 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003385 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003386 if (target == "Manager") {
3387 mTestFormat = format;
3388 } else if (mTestOutputs[mCurOutput] != 0) {
3389 AudioParameter outputParam = AudioParameter();
3390 outputParam.addInt(String8("format"), format);
3391 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3392 }
3393 }
3394 }
3395 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3396 param.remove(String8("test_cmd_policy_channels"));
3397 int channels = 0;
3398
3399 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003400 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003401 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003402 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003403 }
3404 if (channels != 0) {
3405 if (target == "Manager") {
3406 mTestChannels = channels;
3407 } else if (mTestOutputs[mCurOutput] != 0) {
3408 AudioParameter outputParam = AudioParameter();
3409 outputParam.addInt(String8("channels"), channels);
3410 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3411 }
3412 }
3413 }
3414 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3415 param.remove(String8("test_cmd_policy_sampleRate"));
3416 if (valueInt >= 0 && valueInt <= 96000) {
3417 int samplingRate = valueInt;
3418 if (target == "Manager") {
3419 mTestSamplingRate = samplingRate;
3420 } else if (mTestOutputs[mCurOutput] != 0) {
3421 AudioParameter outputParam = AudioParameter();
3422 outputParam.addInt(String8("sampling_rate"), samplingRate);
3423 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3424 }
3425 }
3426 }
3427
3428 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3429 param.remove(String8("test_cmd_policy_reopen"));
3430
Eric Laurentc75307b2015-03-17 15:29:32 -07003431 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003432
Eric Laurentc75307b2015-03-17 15:29:32 -07003433 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003434
Eric Laurentc75307b2015-03-17 15:29:32 -07003435 removeOutput(mPrimaryOutput->mIoHandle);
3436 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3437 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003438 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003439 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3440 config.sample_rate = outputDesc->mSamplingRate;
3441 config.channel_mask = outputDesc->mChannelMask;
3442 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003443 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003444 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003445 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003446 &config,
3447 &outputDesc->mDevice,
3448 String8(""),
3449 &outputDesc->mLatency,
3450 outputDesc->mFlags);
3451 if (status != NO_ERROR) {
3452 ALOGE("Failed to reopen hardware output stream, "
3453 "samplingRate: %d, format %d, channels %d",
3454 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003455 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003456 outputDesc->mSamplingRate = config.sample_rate;
3457 outputDesc->mChannelMask = config.channel_mask;
3458 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003459 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003460 AudioParameter outputCmd = AudioParameter();
3461 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003462 mpClientInterface->setParameters(handle, outputCmd.toString());
3463 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003464 }
3465 }
3466
3467
3468 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3469 }
3470 }
3471 return false;
3472}
3473
Eric Laurente0720872014-03-11 09:30:41 -07003474void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003475{
3476 {
3477 AutoMutex _l(mLock);
3478 requestExit();
3479 mWaitWorkCV.signal();
3480 }
3481 requestExitAndWait();
3482}
3483
Eric Laurente0720872014-03-11 09:30:41 -07003484int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003485{
3486 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3487 if (output == mTestOutputs[i]) return i;
3488 }
3489 return 0;
3490}
3491#endif //AUDIO_POLICY_TEST
3492
3493// ---
3494
Eric Laurentc75307b2015-03-17 15:29:32 -07003495void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003496{
François Gaffie98cc1912015-03-18 17:52:40 +01003497 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003498 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003499 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003500 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003501}
3502
François Gaffie53615e22015-03-19 09:24:12 +01003503void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3504{
3505 mOutputs.removeItem(output);
3506}
3507
Eric Laurent1f2f2232014-06-02 12:01:23 -07003508void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003509{
François Gaffie98cc1912015-03-18 17:52:40 +01003510 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003511 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003512 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003513}
Eric Laurente552edb2014-03-10 17:42:56 -07003514
Eric Laurentc75307b2015-03-17 15:29:32 -07003515void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003516 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003517 const String8 address /*in*/,
3518 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003519 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003520 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003521 if (devDesc != 0) {
3522 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3523 desc->mIoHandle, address.string());
3524 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003525 }
3526}
3527
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003528status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003529 audio_policy_dev_state_t state,
3530 SortedVector<audio_io_handle_t>& outputs,
3531 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003532{
François Gaffie53615e22015-03-19 09:24:12 +01003533 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003534 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003535
3536 if (audio_device_is_digital(device)) {
3537 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003538 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003539 }
Eric Laurente552edb2014-03-10 17:42:56 -07003540
Eric Laurent3b73df72014-03-11 09:06:29 -07003541 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003542 // first list already open outputs that can be routed to this device
3543 for (size_t i = 0; i < mOutputs.size(); i++) {
3544 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003545 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003546 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003547 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3548 outputs.add(mOutputs.keyAt(i));
3549 } else {
3550 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003551 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003552 }
Eric Laurente552edb2014-03-10 17:42:56 -07003553 }
3554 }
3555 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003556 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003557 for (size_t i = 0; i < mHwModules.size(); i++)
3558 {
3559 if (mHwModules[i]->mHandle == 0) {
3560 continue;
3561 }
3562 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3563 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003564 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003565 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003566 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003567 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003568 profiles.add(profile);
3569 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3570 }
Eric Laurente552edb2014-03-10 17:42:56 -07003571 }
3572 }
3573 }
3574
Eric Laurent7b279bb2015-12-14 10:18:23 -08003575 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003576
Eric Laurente552edb2014-03-10 17:42:56 -07003577 if (profiles.isEmpty() && outputs.isEmpty()) {
3578 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3579 return BAD_VALUE;
3580 }
3581
3582 // open outputs for matching profiles if needed. Direct outputs are also opened to
3583 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3584 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003585 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003586
3587 // nothing to do if one output is already opened for this profile
3588 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003589 for (j = 0; j < outputs.size(); j++) {
3590 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003591 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003592 // matching profile: save the sample rates, format and channel masks supported
3593 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003594 if (audio_device_is_digital(device)) {
3595 devDesc->importAudioPort(profile);
3596 }
Eric Laurente552edb2014-03-10 17:42:56 -07003597 break;
3598 }
3599 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003600 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003601 continue;
3602 }
3603
Eric Laurent83b88082014-06-20 18:31:16 -07003604 ALOGV("opening output for device %08x with params %s profile %p",
3605 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003606 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003607 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003608 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3609 config.sample_rate = desc->mSamplingRate;
3610 config.channel_mask = desc->mChannelMask;
3611 config.format = desc->mFormat;
3612 config.offload_info.sample_rate = desc->mSamplingRate;
3613 config.offload_info.channel_mask = desc->mChannelMask;
3614 config.offload_info.format = desc->mFormat;
3615 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003616 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003617 &output,
3618 &config,
3619 &desc->mDevice,
3620 address,
3621 &desc->mLatency,
3622 desc->mFlags);
3623 if (status == NO_ERROR) {
3624 desc->mSamplingRate = config.sample_rate;
3625 desc->mChannelMask = config.channel_mask;
3626 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003627
Eric Laurentd4692962014-05-05 18:13:44 -07003628 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003629 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003630 char *param = audio_device_address_to_parameter(device, address);
3631 mpClientInterface->setParameters(output, String8(param));
3632 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003633 }
François Gaffie112b0af2015-11-19 16:13:25 +01003634 updateAudioProfiles(output, profile->getAudioProfiles());
3635 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003636 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003637 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003638 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003639 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003640 mpClientInterface->closeOutput(output);
François Gaffie112b0af2015-11-19 16:13:25 +01003641 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003642 config.offload_info.sample_rate = config.sample_rate;
3643 config.offload_info.channel_mask = config.channel_mask;
3644 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003645 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003646 &output,
3647 &config,
3648 &desc->mDevice,
3649 address,
3650 &desc->mLatency,
3651 desc->mFlags);
3652 if (status == NO_ERROR) {
3653 desc->mSamplingRate = config.sample_rate;
3654 desc->mChannelMask = config.channel_mask;
3655 desc->mFormat = config.format;
3656 } else {
3657 output = AUDIO_IO_HANDLE_NONE;
3658 }
Eric Laurentd4692962014-05-05 18:13:44 -07003659 }
3660
Eric Laurentcf2c0212014-07-25 16:20:43 -07003661 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003662 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003663 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003664 sp<AudioPolicyMix> policyMix;
3665 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003666 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3667 address.string());
3668 }
François Gaffie036e1e92015-03-19 10:16:24 +01003669 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003670 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003671
Eric Laurent87ffa392015-05-22 10:32:38 -07003672 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3673 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003674 // no duplicated output for direct outputs and
3675 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003676 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003677
Eric Laurentd4692962014-05-05 18:13:44 -07003678 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003679 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003680
Eric Laurentd4692962014-05-05 18:13:44 -07003681 //TODO: configure audio effect output stage here
3682
3683 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003684 duplicatedOutput =
3685 mpClientInterface->openDuplicateOutput(output,
3686 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003687 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003688 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003689 sp<SwAudioOutputDescriptor> dupOutputDesc =
3690 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3691 dupOutputDesc->mOutput1 = mPrimaryOutput;
3692 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003693 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3694 dupOutputDesc->mFormat = desc->mFormat;
3695 dupOutputDesc->mChannelMask = desc->mChannelMask;
3696 dupOutputDesc->mLatency = desc->mLatency;
3697 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003698 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003699 } else {
3700 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003701 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003702 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003703 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003704 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003705 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003706 }
Eric Laurente552edb2014-03-10 17:42:56 -07003707 }
3708 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003709 } else {
3710 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003711 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003712 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003713 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003714 profiles.removeAt(profile_index);
3715 profile_index--;
3716 } else {
3717 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003718 // Load digital format info only for digital devices
3719 if (audio_device_is_digital(device)) {
3720 devDesc->importAudioPort(profile);
3721 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003722
François Gaffie53615e22015-03-19 09:24:12 +01003723 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003724 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3725 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003726 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003727 NULL/*patch handle*/, address.string());
3728 }
Eric Laurente552edb2014-03-10 17:42:56 -07003729 ALOGV("checkOutputsForDevice(): adding output %d", output);
3730 }
3731 }
3732
3733 if (profiles.isEmpty()) {
3734 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3735 return BAD_VALUE;
3736 }
Eric Laurentd4692962014-05-05 18:13:44 -07003737 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003738 // check if one opened output is not needed any more after disconnecting one device
3739 for (size_t i = 0; i < mOutputs.size(); i++) {
3740 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003741 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003742 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003743 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003744 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003745 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003746 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003747 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3748 mOutputs.keyAt(i));
3749 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003750 }
Eric Laurente552edb2014-03-10 17:42:56 -07003751 }
3752 }
Eric Laurentd4692962014-05-05 18:13:44 -07003753 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003754 for (size_t i = 0; i < mHwModules.size(); i++)
3755 {
3756 if (mHwModules[i]->mHandle == 0) {
3757 continue;
3758 }
3759 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3760 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003761 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003762 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003763 ALOGV("checkOutputsForDevice(): "
3764 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003765 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003766 }
3767 }
3768 }
3769 }
3770 return NO_ERROR;
3771}
3772
Paul McLean9080a4c2015-06-18 08:24:02 -07003773status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003774 audio_policy_dev_state_t state,
3775 SortedVector<audio_io_handle_t>& inputs,
3776 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003777{
Paul McLean9080a4c2015-06-18 08:24:02 -07003778 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003779 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003780
3781 if (audio_device_is_digital(device)) {
3782 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003783 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003784 }
3785
Eric Laurentd4692962014-05-05 18:13:44 -07003786 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3787 // first list already open inputs that can be routed to this device
3788 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3789 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003790 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003791 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3792 inputs.add(mInputs.keyAt(input_index));
3793 }
3794 }
3795
3796 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003797 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003798 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3799 {
3800 if (mHwModules[module_idx]->mHandle == 0) {
3801 continue;
3802 }
3803 for (size_t profile_index = 0;
3804 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3805 profile_index++)
3806 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003807 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3808
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003809 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003810 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003811 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003812 profiles.add(profile);
3813 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3814 profile_index, module_idx);
3815 }
Eric Laurentd4692962014-05-05 18:13:44 -07003816 }
3817 }
3818 }
3819
3820 if (profiles.isEmpty() && inputs.isEmpty()) {
3821 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3822 return BAD_VALUE;
3823 }
3824
3825 // open inputs for matching profiles if needed. Direct inputs are also opened to
3826 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3827 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3828
Eric Laurent1c333e22014-05-20 10:48:17 -07003829 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003830 // nothing to do if one input is already opened for this profile
3831 size_t input_index;
3832 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3833 desc = mInputs.valueAt(input_index);
3834 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003835 if (audio_device_is_digital(device)) {
3836 devDesc->importAudioPort(profile);
3837 }
Eric Laurentd4692962014-05-05 18:13:44 -07003838 break;
3839 }
3840 }
3841 if (input_index != mInputs.size()) {
3842 continue;
3843 }
3844
3845 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3846 desc = new AudioInputDescriptor(profile);
3847 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003848 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3849 config.sample_rate = desc->mSamplingRate;
3850 config.channel_mask = desc->mChannelMask;
3851 config.format = desc->mFormat;
3852 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003853 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003854 &input,
3855 &config,
3856 &desc->mDevice,
3857 address,
3858 AUDIO_SOURCE_MIC,
3859 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003860
Eric Laurentcf2c0212014-07-25 16:20:43 -07003861 if (status == NO_ERROR) {
3862 desc->mSamplingRate = config.sample_rate;
3863 desc->mChannelMask = config.channel_mask;
3864 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003865
Eric Laurentd4692962014-05-05 18:13:44 -07003866 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003867 char *param = audio_device_address_to_parameter(device, address);
3868 mpClientInterface->setParameters(input, String8(param));
3869 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003870 }
François Gaffie112b0af2015-11-19 16:13:25 +01003871 updateAudioProfiles(input, profile->getAudioProfiles());
3872 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003873 ALOGW("checkInputsForDevice() direct input missing param");
3874 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003875 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003876 }
3877
3878 if (input != 0) {
3879 addInput(input, desc);
3880 }
3881 } // endif input != 0
3882
Eric Laurentcf2c0212014-07-25 16:20:43 -07003883 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003884 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003885 profiles.removeAt(profile_index);
3886 profile_index--;
3887 } else {
3888 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003889 if (audio_device_is_digital(device)) {
3890 devDesc->importAudioPort(profile);
3891 }
Eric Laurentd4692962014-05-05 18:13:44 -07003892 ALOGV("checkInputsForDevice(): adding input %d", input);
3893 }
3894 } // end scan profiles
3895
3896 if (profiles.isEmpty()) {
3897 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3898 return BAD_VALUE;
3899 }
3900 } else {
3901 // Disconnect
3902 // check if one opened input is not needed any more after disconnecting one device
3903 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3904 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003905 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07003906 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3907 mInputs.keyAt(input_index));
3908 inputs.add(mInputs.keyAt(input_index));
3909 }
3910 }
3911 // Clear any profiles associated with the disconnected device.
3912 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3913 if (mHwModules[module_index]->mHandle == 0) {
3914 continue;
3915 }
3916 for (size_t profile_index = 0;
3917 profile_index < mHwModules[module_index]->mInputProfiles.size();
3918 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003919 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003920 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003921 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003922 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01003923 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07003924 }
3925 }
3926 }
3927 } // end disconnect
3928
3929 return NO_ERROR;
3930}
3931
3932
Eric Laurente0720872014-03-11 09:30:41 -07003933void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003934{
3935 ALOGV("closeOutput(%d)", output);
3936
Eric Laurentc75307b2015-03-17 15:29:32 -07003937 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003938 if (outputDesc == NULL) {
3939 ALOGW("closeOutput() unknown output %d", output);
3940 return;
3941 }
François Gaffie036e1e92015-03-19 10:16:24 +01003942 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003943
Eric Laurente552edb2014-03-10 17:42:56 -07003944 // look for duplicated outputs connected to the output being removed.
3945 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003946 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003947 if (dupOutputDesc->isDuplicated() &&
3948 (dupOutputDesc->mOutput1 == outputDesc ||
3949 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003950 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003951 if (dupOutputDesc->mOutput1 == outputDesc) {
3952 outputDesc2 = dupOutputDesc->mOutput2;
3953 } else {
3954 outputDesc2 = dupOutputDesc->mOutput1;
3955 }
3956 // As all active tracks on duplicated output will be deleted,
3957 // and as they were also referenced on the other output, the reference
3958 // count for their stream type must be adjusted accordingly on
3959 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003960 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003961 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003962 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003963 }
3964 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3965 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3966
3967 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003968 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003969 }
3970 }
3971
Eric Laurent05b90f82014-08-27 15:32:29 -07003972 nextAudioPortGeneration();
3973
3974 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3975 if (index >= 0) {
3976 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3977 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3978 mAudioPatches.removeItemsAt(index);
3979 mpClientInterface->onAudioPatchListUpdate();
3980 }
3981
Eric Laurente552edb2014-03-10 17:42:56 -07003982 AudioParameter param;
3983 param.add(String8("closing"), String8("true"));
3984 mpClientInterface->setParameters(output, param.toString());
3985
3986 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003987 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003988 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003989}
3990
3991void AudioPolicyManager::closeInput(audio_io_handle_t input)
3992{
3993 ALOGV("closeInput(%d)", input);
3994
3995 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3996 if (inputDesc == NULL) {
3997 ALOGW("closeInput() unknown input %d", input);
3998 return;
3999 }
4000
Eric Laurent6a94d692014-05-20 11:18:06 -07004001 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004002
4003 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4004 if (index >= 0) {
4005 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4006 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4007 mAudioPatches.removeItemsAt(index);
4008 mpClientInterface->onAudioPatchListUpdate();
4009 }
4010
4011 mpClientInterface->closeInput(input);
4012 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004013}
4014
Eric Laurentc75307b2015-03-17 15:29:32 -07004015SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4016 audio_devices_t device,
4017 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004018{
4019 SortedVector<audio_io_handle_t> outputs;
4020
4021 ALOGVV("getOutputsForDevice() device %04x", device);
4022 for (size_t i = 0; i < openOutputs.size(); i++) {
4023 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004024 i, openOutputs.valueAt(i)->isDuplicated(),
4025 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004026 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4027 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4028 outputs.add(openOutputs.keyAt(i));
4029 }
4030 }
4031 return outputs;
4032}
4033
Eric Laurente0720872014-03-11 09:30:41 -07004034bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004035 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004036{
4037 if (outputs1.size() != outputs2.size()) {
4038 return false;
4039 }
4040 for (size_t i = 0; i < outputs1.size(); i++) {
4041 if (outputs1[i] != outputs2[i]) {
4042 return false;
4043 }
4044 }
4045 return true;
4046}
4047
Eric Laurente0720872014-03-11 09:30:41 -07004048void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004049{
4050 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4051 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4052 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4053 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4054
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004055 // also take into account external policy-related changes: add all outputs which are
4056 // associated with policies in the "before" and "after" output vectors
4057 ALOGVV("checkOutputForStrategy(): policy related outputs");
4058 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004059 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004060 if (desc != 0 && desc->mPolicyMix != NULL) {
4061 srcOutputs.add(desc->mIoHandle);
4062 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4063 }
4064 }
4065 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004066 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004067 if (desc != 0 && desc->mPolicyMix != NULL) {
4068 dstOutputs.add(desc->mIoHandle);
4069 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4070 }
4071 }
4072
Eric Laurente552edb2014-03-10 17:42:56 -07004073 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4074 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4075 strategy, srcOutputs[0], dstOutputs[0]);
4076 // mute strategy while moving tracks from one output to another
4077 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004078 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004079 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004080 setStrategyMute(strategy, true, desc);
4081 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004082 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004083 sp<AudioSourceDescriptor> source =
4084 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4085 if (source != 0){
4086 connectAudioSource(source);
4087 }
Eric Laurente552edb2014-03-10 17:42:56 -07004088 }
4089
4090 // Move effects associated to this strategy from previous output to new output
4091 if (strategy == STRATEGY_MEDIA) {
4092 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4093 SortedVector<audio_io_handle_t> moved;
4094 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004095 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4096 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4097 effectDesc->mIo != fxOutput) {
4098 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004099 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4100 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004101 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004102 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004103 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004104 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004105 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004106 }
4107 }
4108 }
4109 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07004110 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004111 if (i == AUDIO_STREAM_PATCH) {
4112 continue;
4113 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004114 if (getStrategy((audio_stream_type_t)i) == strategy) {
4115 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004116 }
4117 }
4118 }
4119}
4120
Eric Laurente0720872014-03-11 09:30:41 -07004121void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004122{
François Gaffie2110e042015-03-24 08:41:51 +01004123 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004124 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004125 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004126 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004127 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004128 checkOutputForStrategy(STRATEGY_SONIFICATION);
4129 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004130 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004131 checkOutputForStrategy(STRATEGY_MEDIA);
4132 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004133 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004134}
4135
Eric Laurente0720872014-03-11 09:30:41 -07004136void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004137{
François Gaffie53615e22015-03-19 09:24:12 +01004138 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004139 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004140 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004141 return;
4142 }
4143
Eric Laurent3a4311c2014-03-17 12:00:47 -07004144 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004145 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4146 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4147 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004148 // suspend A2DP output if:
4149 // (NOT already suspended) &&
4150 // ((SCO device is connected &&
4151 // (forced usage for communication || for record is SCO))) ||
4152 // (phone state is ringing || in call)
4153 //
4154 // restore A2DP output if:
4155 // (Already suspended) &&
4156 // ((SCO device is NOT connected ||
4157 // (forced usage NOT for communication && NOT for record is SCO))) &&
4158 // (phone state is NOT ringing && NOT in call)
4159 //
4160 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004161 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004162 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4163 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4164 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4165 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004166
4167 mpClientInterface->restoreOutput(a2dpOutput);
4168 mA2dpSuspended = false;
4169 }
4170 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004171 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004172 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4173 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4174 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4175 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004176
4177 mpClientInterface->suspendOutput(a2dpOutput);
4178 mA2dpSuspended = true;
4179 }
4180 }
4181}
4182
Eric Laurentc75307b2015-03-17 15:29:32 -07004183audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4184 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004185{
4186 audio_devices_t device = AUDIO_DEVICE_NONE;
4187
Eric Laurent6a94d692014-05-20 11:18:06 -07004188 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4189 if (index >= 0) {
4190 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4191 if (patchDesc->mUid != mUidCached) {
4192 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
4193 outputDesc->device(), outputDesc->mPatchHandle);
4194 return outputDesc->device();
4195 }
4196 }
4197
Eric Laurente552edb2014-03-10 17:42:56 -07004198 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004199 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004200 // use device for strategy enforced audible
4201 // 2: we are in call or the strategy phone is active on the output:
4202 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004203 // 3: the strategy for enforced audible is active but not enforced on the output:
4204 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004205 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004206 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004207 // 5: the strategy sonification is active on the output:
4208 // use device for strategy sonification
4209 // 6: the strategy "respectful" sonification is active on the output:
4210 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004211 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004212 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004213 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004214 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004215 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004216 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004217 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004218 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004219 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4220 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004221 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004222 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004223 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004224 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004225 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4226 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004227 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004228 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004229 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004230 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004231 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004232 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004233 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004234 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004235 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004236 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004237 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004238 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004239 }
4240
Eric Laurent1c333e22014-05-20 10:48:17 -07004241 ALOGV("getNewOutputDevice() selected device %x", device);
4242 return device;
4243}
4244
Eric Laurentfb66dd92016-01-28 18:32:03 -08004245audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004246{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004247 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004248
4249 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4250 if (index >= 0) {
4251 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4252 if (patchDesc->mUid != mUidCached) {
4253 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4254 inputDesc->mDevice, inputDesc->mPatchHandle);
4255 return inputDesc->mDevice;
4256 }
4257 }
4258
Eric Laurentfb66dd92016-01-28 18:32:03 -08004259 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4260 if (isInCall()) {
4261 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4262 } else if (source != AUDIO_SOURCE_DEFAULT) {
4263 device = getDeviceAndMixForInputSource(source);
4264 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004265
Eric Laurente552edb2014-03-10 17:42:56 -07004266 return device;
4267}
4268
Eric Laurente0720872014-03-11 09:30:41 -07004269uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004270 return (uint32_t)getStrategy(stream);
4271}
4272
Eric Laurente0720872014-03-11 09:30:41 -07004273audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004274 // By checking the range of stream before calling getStrategy, we avoid
4275 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4276 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004277 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004278 return AUDIO_DEVICE_NONE;
4279 }
4280 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004281 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004282 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4283 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4284 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004285 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004286 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004287 devices = outputDesc->device();
4288 break;
4289 }
Eric Laurente552edb2014-03-10 17:42:56 -07004290 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004291
4292 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4293 and doesn't really need to.*/
4294 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4295 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4296 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4297 }
Eric Laurente552edb2014-03-10 17:42:56 -07004298 return devices;
4299}
4300
François Gaffie2110e042015-03-24 08:41:51 +01004301routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4302{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004303 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004304 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004305}
4306
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004307uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4308 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004309 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4310 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4311 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004312 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4313 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4314 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004315 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004316 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004317}
4318
Eric Laurente0720872014-03-11 09:30:41 -07004319void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004320 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004321 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004322 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4323 updateDevicesAndOutputs();
4324 break;
4325 default:
4326 break;
4327 }
4328}
4329
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004330uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004331
4332 // skip beacon mute management if a dedicated TTS output is available
4333 if (mTtsOutputAvailable) {
4334 return 0;
4335 }
4336
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004337 switch(event) {
4338 case STARTING_OUTPUT:
4339 mBeaconMuteRefCount++;
4340 break;
4341 case STOPPING_OUTPUT:
4342 if (mBeaconMuteRefCount > 0) {
4343 mBeaconMuteRefCount--;
4344 }
4345 break;
4346 case STARTING_BEACON:
4347 mBeaconPlayingRefCount++;
4348 break;
4349 case STOPPING_BEACON:
4350 if (mBeaconPlayingRefCount > 0) {
4351 mBeaconPlayingRefCount--;
4352 }
4353 break;
4354 }
4355
4356 if (mBeaconMuteRefCount > 0) {
4357 // any playback causes beacon to be muted
4358 return setBeaconMute(true);
4359 } else {
4360 // no other playback: unmute when beacon starts playing, mute when it stops
4361 return setBeaconMute(mBeaconPlayingRefCount == 0);
4362 }
4363}
4364
4365uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4366 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4367 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4368 // keep track of muted state to avoid repeating mute/unmute operations
4369 if (mBeaconMuted != mute) {
4370 // mute/unmute AUDIO_STREAM_TTS on all outputs
4371 ALOGV("\t muting %d", mute);
4372 uint32_t maxLatency = 0;
4373 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004374 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004375 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004376 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004377 0 /*delay*/, AUDIO_DEVICE_NONE);
4378 const uint32_t latency = desc->latency() * 2;
4379 if (latency > maxLatency) {
4380 maxLatency = latency;
4381 }
4382 }
4383 mBeaconMuted = mute;
4384 return maxLatency;
4385 }
4386 return 0;
4387}
4388
Eric Laurente0720872014-03-11 09:30:41 -07004389audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004390 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004391{
Paul McLeanaa981192015-03-21 09:55:15 -07004392 // Routing
4393 // see if we have an explicit route
4394 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4395 // (getStrategy(stream)).
4396 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4397 // and activity count is non-zero
4398 // the device = the device from the descriptor in the RouteMap, and exit.
4399 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4400 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4401 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004402 // Special case for accessibility strategy which must follow any strategy it is
4403 // currently remapped to
4404 bool strategyMatch = (strat == strategy) ||
4405 ((strategy == STRATEGY_ACCESSIBILITY) &&
4406 ((mEngine->getStrategyForUsage(
4407 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4408 (strat == STRATEGY_MEDIA)));
4409 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004410 return route->mDeviceDescriptor->type();
4411 }
4412 }
4413
Eric Laurente552edb2014-03-10 17:42:56 -07004414 if (fromCache) {
4415 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4416 strategy, mDeviceForStrategy[strategy]);
4417 return mDeviceForStrategy[strategy];
4418 }
François Gaffie2110e042015-03-24 08:41:51 +01004419 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004420}
4421
Eric Laurente0720872014-03-11 09:30:41 -07004422void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004423{
4424 for (int i = 0; i < NUM_STRATEGIES; i++) {
4425 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4426 }
4427 mPreviousOutputs = mOutputs;
4428}
4429
Eric Laurent1f2f2232014-06-02 12:01:23 -07004430uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004431 audio_devices_t prevDevice,
4432 uint32_t delayMs)
4433{
4434 // mute/unmute strategies using an incompatible device combination
4435 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4436 // if unmuting, unmute only after the specified delay
4437 if (outputDesc->isDuplicated()) {
4438 return 0;
4439 }
4440
4441 uint32_t muteWaitMs = 0;
4442 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004443 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004444
4445 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4446 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004447 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004448 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4449 bool doMute = false;
4450
4451 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4452 doMute = true;
4453 outputDesc->mStrategyMutedByDevice[i] = true;
4454 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4455 doMute = true;
4456 outputDesc->mStrategyMutedByDevice[i] = false;
4457 }
Eric Laurent99401132014-05-07 19:48:15 -07004458 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004459 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004460 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004461 // skip output if it does not share any device with current output
4462 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4463 == AUDIO_DEVICE_NONE) {
4464 continue;
4465 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004466 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4467 mute ? "muting" : "unmuting", i, curDevice);
4468 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004469 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004470 if (mute) {
4471 // FIXME: should not need to double latency if volume could be applied
4472 // immediately by the audioflinger mixer. We must account for the delay
4473 // between now and the next time the audioflinger thread for this output
4474 // will process a buffer (which corresponds to one buffer size,
4475 // usually 1/2 or 1/4 of the latency).
4476 if (muteWaitMs < desc->latency() * 2) {
4477 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004478 }
4479 }
4480 }
4481 }
4482 }
4483 }
4484
Eric Laurent99401132014-05-07 19:48:15 -07004485 // temporary mute output if device selection changes to avoid volume bursts due to
4486 // different per device volumes
4487 if (outputDesc->isActive() && (device != prevDevice)) {
4488 if (muteWaitMs < outputDesc->latency() * 2) {
4489 muteWaitMs = outputDesc->latency() * 2;
4490 }
4491 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004492 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004493 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004494 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004495 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004496 muteWaitMs *2, device);
4497 }
4498 }
4499 }
4500
Eric Laurente552edb2014-03-10 17:42:56 -07004501 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4502 if (muteWaitMs > delayMs) {
4503 muteWaitMs -= delayMs;
4504 usleep(muteWaitMs * 1000);
4505 return muteWaitMs;
4506 }
4507 return 0;
4508}
4509
Eric Laurentc75307b2015-03-17 15:29:32 -07004510uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004511 audio_devices_t device,
4512 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004513 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004514 audio_patch_handle_t *patchHandle,
4515 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004516{
Eric Laurentc75307b2015-03-17 15:29:32 -07004517 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004518 AudioParameter param;
4519 uint32_t muteWaitMs;
4520
4521 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004522 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4523 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004524 return muteWaitMs;
4525 }
4526 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4527 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004528 if ((device != AUDIO_DEVICE_NONE) &&
4529 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004530 return 0;
4531 }
4532
4533 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004534 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004535
4536 audio_devices_t prevDevice = outputDesc->mDevice;
4537
Paul McLeanaa981192015-03-21 09:55:15 -07004538 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004539
4540 if (device != AUDIO_DEVICE_NONE) {
4541 outputDesc->mDevice = device;
4542 }
4543 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4544
4545 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004546 // the requested device is AUDIO_DEVICE_NONE
4547 // OR the requested device is the same as current device
4548 // AND force is not specified
4549 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004550 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004551 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4552 !force &&
4553 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004554 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004555 return muteWaitMs;
4556 }
4557
4558 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004559
Eric Laurente552edb2014-03-10 17:42:56 -07004560 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004561 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004562 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004563 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004564 DeviceVector deviceList = (address == NULL) ?
4565 mAvailableOutputDevices.getDevicesFromType(device)
4566 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004567 if (!deviceList.isEmpty()) {
4568 struct audio_patch patch;
4569 outputDesc->toAudioPortConfig(&patch.sources[0]);
4570 patch.num_sources = 1;
4571 patch.num_sinks = 0;
4572 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4573 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004574 patch.num_sinks++;
4575 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004576 ssize_t index;
4577 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4578 index = mAudioPatches.indexOfKey(*patchHandle);
4579 } else {
4580 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4581 }
4582 sp< AudioPatch> patchDesc;
4583 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4584 if (index >= 0) {
4585 patchDesc = mAudioPatches.valueAt(index);
4586 afPatchHandle = patchDesc->mAfPatchHandle;
4587 }
4588
Eric Laurent1c333e22014-05-20 10:48:17 -07004589 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004590 &afPatchHandle,
4591 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004592 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4593 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004594 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004595 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004596 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004597 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004598 addAudioPatch(patchDesc->mHandle, patchDesc);
4599 } else {
4600 patchDesc->mPatch = patch;
4601 }
4602 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004603 if (patchHandle) {
4604 *patchHandle = patchDesc->mHandle;
4605 }
4606 outputDesc->mPatchHandle = patchDesc->mHandle;
4607 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004608 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004609 }
4610 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004611
4612 // inform all input as well
4613 for (size_t i = 0; i < mInputs.size(); i++) {
4614 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004615 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004616 AudioParameter inputCmd = AudioParameter();
4617 ALOGV("%s: inform input %d of device:%d", __func__,
4618 inputDescriptor->mIoHandle, device);
4619 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4620 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4621 inputCmd.toString(),
4622 delayMs);
4623 }
4624 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004625 }
Eric Laurente552edb2014-03-10 17:42:56 -07004626
4627 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004628 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004629
4630 return muteWaitMs;
4631}
4632
Eric Laurentc75307b2015-03-17 15:29:32 -07004633status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004634 int delayMs,
4635 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004636{
Eric Laurent6a94d692014-05-20 11:18:06 -07004637 ssize_t index;
4638 if (patchHandle) {
4639 index = mAudioPatches.indexOfKey(*patchHandle);
4640 } else {
4641 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4642 }
4643 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004644 return INVALID_OPERATION;
4645 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004646 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4647 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004648 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4649 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004650 removeAudioPatch(patchDesc->mHandle);
4651 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004652 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004653 return status;
4654}
4655
4656status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4657 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004658 bool force,
4659 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004660{
4661 status_t status = NO_ERROR;
4662
Eric Laurent1f2f2232014-06-02 12:01:23 -07004663 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004664 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4665 inputDesc->mDevice = device;
4666
4667 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4668 if (!deviceList.isEmpty()) {
4669 struct audio_patch patch;
4670 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004671 // AUDIO_SOURCE_HOTWORD is for internal use only:
4672 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004673 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004674 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004675 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4676 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004677 patch.num_sinks = 1;
4678 //only one input device for now
4679 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004680 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004681 ssize_t index;
4682 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4683 index = mAudioPatches.indexOfKey(*patchHandle);
4684 } else {
4685 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4686 }
4687 sp< AudioPatch> patchDesc;
4688 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4689 if (index >= 0) {
4690 patchDesc = mAudioPatches.valueAt(index);
4691 afPatchHandle = patchDesc->mAfPatchHandle;
4692 }
4693
Eric Laurent1c333e22014-05-20 10:48:17 -07004694 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004695 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004696 0);
4697 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004698 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004699 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004700 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004701 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004702 addAudioPatch(patchDesc->mHandle, patchDesc);
4703 } else {
4704 patchDesc->mPatch = patch;
4705 }
4706 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004707 if (patchHandle) {
4708 *patchHandle = patchDesc->mHandle;
4709 }
4710 inputDesc->mPatchHandle = patchDesc->mHandle;
4711 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004712 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004713 }
4714 }
4715 }
4716 return status;
4717}
4718
Eric Laurent6a94d692014-05-20 11:18:06 -07004719status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4720 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004721{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004722 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004723 ssize_t index;
4724 if (patchHandle) {
4725 index = mAudioPatches.indexOfKey(*patchHandle);
4726 } else {
4727 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4728 }
4729 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004730 return INVALID_OPERATION;
4731 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004732 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4733 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004734 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4735 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004736 removeAudioPatch(patchDesc->mHandle);
4737 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004738 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004739 return status;
4740}
4741
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004742sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004743 String8 address,
4744 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004745 audio_format_t& format,
4746 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004747 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004748{
4749 // Choose an input profile based on the requested capture parameters: select the first available
4750 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004751 //
4752 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4753 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004754
4755 for (size_t i = 0; i < mHwModules.size(); i++)
4756 {
4757 if (mHwModules[i]->mHandle == 0) {
4758 continue;
4759 }
4760 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4761 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004762 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004763 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004764 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004765 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004766 format,
4767 &format /*updatedFormat*/,
4768 channelMask,
4769 &channelMask /*updatedChannelMask*/,
4770 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004771
Eric Laurente552edb2014-03-10 17:42:56 -07004772 return profile;
4773 }
4774 }
4775 }
4776 return NULL;
4777}
4778
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004779
4780audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004781 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004782{
François Gaffie036e1e92015-03-19 10:16:24 +01004783 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4784 audio_devices_t selectedDeviceFromMix =
4785 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004786
François Gaffie036e1e92015-03-19 10:16:24 +01004787 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4788 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004789 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004790 return getDeviceForInputSource(inputSource);
4791}
4792
4793audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4794{
Paul McLean466dc8e2015-04-17 13:15:36 -06004795 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4796 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004797 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004798 return route->mDeviceDescriptor->type();
4799 }
4800 }
4801
4802 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004803}
4804
Eric Laurente0720872014-03-11 09:30:41 -07004805float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004806 int index,
4807 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004808{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004809 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004810 // if a headset is connected, apply the following rules to ring tones and notifications
4811 // to avoid sound level bursts in user's ears:
4812 // - always attenuate ring tones and notifications volume by 6dB
4813 // - if music is playing, always limit the volume to current music volume,
4814 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004815 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004816 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4817 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4818 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4819 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4820 ((stream_strategy == STRATEGY_SONIFICATION)
4821 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004822 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004823 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004824 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004825 mVolumeCurves->canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004826 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004827 // when the phone is ringing we must consider that music could have been paused just before
4828 // by the music application and behave as if music was active if the last music track was
4829 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004830 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004831 mLimitRingtoneVolume) {
4832 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004833 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004834 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4835 musicDevice),
4836 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004837 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4838 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4839 if (volumeDb > minVolDB) {
4840 volumeDb = minVolDB;
4841 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004842 }
4843 }
4844 }
4845
Eric Laurentffbc80f2015-03-18 18:30:19 -07004846 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004847}
4848
Eric Laurente0720872014-03-11 09:30:41 -07004849status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004850 int index,
4851 const sp<AudioOutputDescriptor>& outputDesc,
4852 audio_devices_t device,
4853 int delayMs,
4854 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004855{
Eric Laurente552edb2014-03-10 17:42:56 -07004856 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004857 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004858 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004859 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004860 return NO_ERROR;
4861 }
François Gaffie2110e042015-03-24 08:41:51 +01004862 audio_policy_forced_cfg_t forceUseForComm =
4863 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004864 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004865 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4866 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004867 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004868 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004869 return INVALID_OPERATION;
4870 }
4871
Eric Laurentc75307b2015-03-17 15:29:32 -07004872 if (device == AUDIO_DEVICE_NONE) {
4873 device = outputDesc->device();
4874 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004875
Eric Laurentffbc80f2015-03-18 18:30:19 -07004876 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004877 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004878 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004879 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004880
Eric Laurentffbc80f2015-03-18 18:30:19 -07004881 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004882
Eric Laurent3b73df72014-03-11 09:06:29 -07004883 if (stream == AUDIO_STREAM_VOICE_CALL ||
4884 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004885 float voiceVolume;
4886 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004887 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004888 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004889 } else {
4890 voiceVolume = 1.0;
4891 }
4892
Eric Laurentc75307b2015-03-17 15:29:32 -07004893 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004894 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4895 mLastVoiceVolume = voiceVolume;
4896 }
4897 }
4898
4899 return NO_ERROR;
4900}
4901
Eric Laurentc75307b2015-03-17 15:29:32 -07004902void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4903 audio_devices_t device,
4904 int delayMs,
4905 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004906{
Eric Laurentc75307b2015-03-17 15:29:32 -07004907 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004908
Eric Laurent3b73df72014-03-11 09:06:29 -07004909 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004910 if (stream == AUDIO_STREAM_PATCH) {
4911 continue;
4912 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004913 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004914 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004915 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004916 device,
4917 delayMs,
4918 force);
4919 }
4920}
4921
Eric Laurente0720872014-03-11 09:30:41 -07004922void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004923 bool on,
4924 const sp<AudioOutputDescriptor>& outputDesc,
4925 int delayMs,
4926 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004927{
Eric Laurent72249d52015-06-08 11:12:15 -07004928 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4929 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004930 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004931 if (stream == AUDIO_STREAM_PATCH) {
4932 continue;
4933 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004934 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004935 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004936 }
4937 }
4938}
4939
Eric Laurente0720872014-03-11 09:30:41 -07004940void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004941 bool on,
4942 const sp<AudioOutputDescriptor>& outputDesc,
4943 int delayMs,
4944 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004945{
Eric Laurente552edb2014-03-10 17:42:56 -07004946 if (device == AUDIO_DEVICE_NONE) {
4947 device = outputDesc->device();
4948 }
4949
Eric Laurentc75307b2015-03-17 15:29:32 -07004950 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4951 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004952
4953 if (on) {
4954 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004955 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004956 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004957 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004958 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004959 }
4960 }
4961 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4962 outputDesc->mMuteCount[stream]++;
4963 } else {
4964 if (outputDesc->mMuteCount[stream] == 0) {
4965 ALOGV("setStreamMute() unmuting non muted stream!");
4966 return;
4967 }
4968 if (--outputDesc->mMuteCount[stream] == 0) {
4969 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004970 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004971 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004972 device,
4973 delayMs);
4974 }
4975 }
4976}
4977
Eric Laurente0720872014-03-11 09:30:41 -07004978void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004979 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004980{
Eric Laurent87ffa392015-05-22 10:32:38 -07004981 if(!hasPrimaryOutput()) {
4982 return;
4983 }
4984
Eric Laurente552edb2014-03-10 17:42:56 -07004985 // if the stream pertains to sonification strategy and we are in call we must
4986 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4987 // in the device used for phone strategy and play the tone if the selected device does not
4988 // interfere with the device used for phone strategy
4989 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4990 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004991 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004992 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4993 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004994 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004995 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4996 stream, starting, outputDesc->mDevice, stateChange);
4997 if (outputDesc->mRefCount[stream]) {
4998 int muteCount = 1;
4999 if (stateChange) {
5000 muteCount = outputDesc->mRefCount[stream];
5001 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005002 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005003 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5004 for (int i = 0; i < muteCount; i++) {
5005 setStreamMute(stream, starting, mPrimaryOutput);
5006 }
5007 } else {
5008 ALOGV("handleIncallSonification() high visibility");
5009 if (outputDesc->device() &
5010 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5011 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5012 for (int i = 0; i < muteCount; i++) {
5013 setStreamMute(stream, starting, mPrimaryOutput);
5014 }
5015 }
5016 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005017 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5018 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005019 } else {
5020 mpClientInterface->stopTone();
5021 }
5022 }
5023 }
5024 }
5025}
5026
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005027audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5028{
5029 // flags to stream type mapping
5030 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5031 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5032 }
5033 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5034 return AUDIO_STREAM_BLUETOOTH_SCO;
5035 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005036 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5037 return AUDIO_STREAM_TTS;
5038 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005039
5040 // usage to stream type mapping
5041 switch (attr->usage) {
5042 case AUDIO_USAGE_MEDIA:
5043 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005044 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5045 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005046 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005047 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5048 return AUDIO_STREAM_ALARM;
5049 }
5050 if (isStreamActive(AUDIO_STREAM_RING)) {
5051 return AUDIO_STREAM_RING;
5052 }
5053 if (isInCall()) {
5054 return AUDIO_STREAM_VOICE_CALL;
5055 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005056 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005057 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5058 return AUDIO_STREAM_SYSTEM;
5059 case AUDIO_USAGE_VOICE_COMMUNICATION:
5060 return AUDIO_STREAM_VOICE_CALL;
5061
5062 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5063 return AUDIO_STREAM_DTMF;
5064
5065 case AUDIO_USAGE_ALARM:
5066 return AUDIO_STREAM_ALARM;
5067 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5068 return AUDIO_STREAM_RING;
5069
5070 case AUDIO_USAGE_NOTIFICATION:
5071 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5072 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5073 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5074 case AUDIO_USAGE_NOTIFICATION_EVENT:
5075 return AUDIO_STREAM_NOTIFICATION;
5076
5077 case AUDIO_USAGE_UNKNOWN:
5078 default:
5079 return AUDIO_STREAM_MUSIC;
5080 }
5081}
Eric Laurente83b55d2014-11-14 10:06:21 -08005082
François Gaffie53615e22015-03-19 09:24:12 +01005083bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5084{
Eric Laurente83b55d2014-11-14 10:06:21 -08005085 // has flags that map to a strategy?
5086 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5087 return true;
5088 }
5089
5090 // has known usage?
5091 switch (paa->usage) {
5092 case AUDIO_USAGE_UNKNOWN:
5093 case AUDIO_USAGE_MEDIA:
5094 case AUDIO_USAGE_VOICE_COMMUNICATION:
5095 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5096 case AUDIO_USAGE_ALARM:
5097 case AUDIO_USAGE_NOTIFICATION:
5098 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5099 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5100 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5101 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5102 case AUDIO_USAGE_NOTIFICATION_EVENT:
5103 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5104 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5105 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5106 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005107 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005108 break;
5109 default:
5110 return false;
5111 }
5112 return true;
5113}
5114
François Gaffiead3183e2015-03-18 16:55:35 +01005115bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5116 routing_strategy strategy, uint32_t inPastMs,
5117 nsecs_t sysTime) const
5118{
5119 if ((sysTime == 0) && (inPastMs != 0)) {
5120 sysTime = systemTime();
5121 }
5122 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5123 if (i == AUDIO_STREAM_PATCH) {
5124 continue;
5125 }
5126 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5127 (NUM_STRATEGIES == strategy)) &&
5128 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5129 return true;
5130 }
5131 }
5132 return false;
5133}
5134
François Gaffie2110e042015-03-24 08:41:51 +01005135audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5136{
5137 return mEngine->getForceUse(usage);
5138}
5139
5140bool AudioPolicyManager::isInCall()
5141{
5142 return isStateInCall(mEngine->getPhoneState());
5143}
5144
5145bool AudioPolicyManager::isStateInCall(int state)
5146{
5147 return is_state_in_call(state);
5148}
5149
Eric Laurentd60560a2015-04-10 11:31:20 -07005150void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5151{
5152 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5153 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5154 if (sourceDesc->mDevice->equals(deviceDesc)) {
5155 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5156 stopAudioSource(sourceDesc->getHandle());
5157 }
5158 }
5159
5160 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5161 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5162 bool release = false;
5163 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5164 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5165 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5166 source->ext.device.type == deviceDesc->type()) {
5167 release = true;
5168 }
5169 }
5170 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5171 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5172 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5173 sink->ext.device.type == deviceDesc->type()) {
5174 release = true;
5175 }
5176 }
5177 if (release) {
5178 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5179 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5180 }
5181 }
5182}
5183
François Gaffie112b0af2015-11-19 16:13:25 +01005184void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle,
5185 AudioProfileVector &profiles)
5186{
5187 String8 reply;
5188 char *value;
5189 // Format MUST be checked first to update the list of AudioProfile
5190 if (profiles.hasDynamicFormat()) {
5191 reply = mpClientInterface->getParameters(ioHandle,
5192 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
5193 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005194 AudioParameter repliedParameters(reply);
5195 if (repliedParameters.get(
5196 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005197 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5198 return;
5199 }
Eric Laurent62e4bc52016-02-02 18:37:28 -08005200 profiles.setFormats(formatsFromString(reply.string()));
François Gaffie112b0af2015-11-19 16:13:25 +01005201 }
5202 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5203
Eric Laurent20eb3a42016-01-26 18:39:17 -08005204 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005205 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005206 ChannelsVector channelMasks;
5207 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005208 AudioParameter requestedParameters;
5209 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5210
5211 if (profiles.hasDynamicRateFor(format)) {
5212 reply = mpClientInterface->getParameters(ioHandle,
5213 requestedParameters.toString() + ";" +
5214 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5215 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005216 AudioParameter repliedParameters(reply);
5217 if (repliedParameters.get(
5218 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5219 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005220 }
5221 }
5222 if (profiles.hasDynamicChannelsFor(format)) {
5223 reply = mpClientInterface->getParameters(ioHandle,
5224 requestedParameters.toString() + ";" +
5225 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5226 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005227 AudioParameter repliedParameters(reply);
5228 if (repliedParameters.get(
5229 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5230 channelMasks = channelMasksFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005231 }
5232 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005233 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005234 }
5235}
Eric Laurentd60560a2015-04-10 11:31:20 -07005236
Eric Laurente552edb2014-03-10 17:42:56 -07005237}; // namespace android