blob: bf68d463919a746e9eaeb3a3947784bb7045f7b9 [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) &&
Eric Laurente6930022016-02-11 10:20:40 -0800879 audio_formats_match(format, outputDesc->mFormat) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700880 (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) ||
Eric Laurente6930022016-02-11 10:20:40 -0800930 (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) ||
Eric Laurentcf2c0212014-07-25 16:20:43 -0700931 (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
Eric Laurente6930022016-02-11 10:20:40 -0800995 // 2: the output with the bit depth the closest to the requested one
996 // 3: the primary output
997 // 4: the first output in the list
Eric Laurente552edb2014-03-10 17:42:56 -0700998
999 if (outputs.size() == 0) {
1000 return 0;
1001 }
1002 if (outputs.size() == 1) {
1003 return outputs[0];
1004 }
1005
1006 int maxCommonFlags = 0;
Eric Laurente6930022016-02-11 10:20:40 -08001007 audio_io_handle_t outputForFlags = 0;
1008 audio_io_handle_t outputForPrimary = 0;
1009 audio_io_handle_t outputForFormat = 0;
1010 audio_format_t bestFormat = AUDIO_FORMAT_INVALID;
1011 audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001012
1013 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001014 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001015 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001016 // if a valid format is specified, skip output if not compatible
1017 if (format != AUDIO_FORMAT_INVALID) {
1018 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente6930022016-02-11 10:20:40 -08001019 if (!audio_formats_match(format, outputDesc->mFormat)) {
Eric Laurent8838a382014-09-08 16:44:28 -07001020 continue;
1021 }
1022 } else if (!audio_is_linear_pcm(format)) {
1023 continue;
1024 }
Eric Laurente6930022016-02-11 10:20:40 -08001025 if (AudioPort::isBetterFormatMatch(
1026 outputDesc->mFormat, bestFormat, format)) {
1027 outputForFormat = outputs[i];
1028 bestFormat = outputDesc->mFormat;
1029 }
Eric Laurent8838a382014-09-08 16:44:28 -07001030 }
1031
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001032 int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags);
Eric Laurente6930022016-02-11 10:20:40 -08001033 if (commonFlags >= maxCommonFlags) {
1034 if (commonFlags == maxCommonFlags) {
1035 if (AudioPort::isBetterFormatMatch(
1036 outputDesc->mFormat, bestFormatForFlags, format)) {
1037 outputForFlags = outputs[i];
1038 bestFormatForFlags = outputDesc->mFormat;
1039 }
1040 } else {
1041 outputForFlags = outputs[i];
1042 maxCommonFlags = commonFlags;
1043 bestFormatForFlags = outputDesc->mFormat;
1044 }
Eric Laurente552edb2014-03-10 17:42:56 -07001045 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1046 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01001047 if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurente6930022016-02-11 10:20:40 -08001048 outputForPrimary = outputs[i];
Eric Laurente552edb2014-03-10 17:42:56 -07001049 }
1050 }
1051 }
1052
Eric Laurente6930022016-02-11 10:20:40 -08001053 if (outputForFlags != 0) {
1054 return outputForFlags;
Eric Laurente552edb2014-03-10 17:42:56 -07001055 }
Eric Laurente6930022016-02-11 10:20:40 -08001056 if (outputForFormat != 0) {
1057 return outputForFormat;
1058 }
1059 if (outputForPrimary != 0) {
1060 return outputForPrimary;
Eric Laurente552edb2014-03-10 17:42:56 -07001061 }
1062
1063 return outputs[0];
1064}
1065
Eric Laurente0720872014-03-11 09:30:41 -07001066status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001067 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001068 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001069{
Paul McLeanaa981192015-03-21 09:55:15 -07001070 ALOGV("startOutput() output %d, stream %d, session %d",
1071 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001072 ssize_t index = mOutputs.indexOfKey(output);
1073 if (index < 0) {
1074 ALOGW("startOutput() unknown output %d", output);
1075 return BAD_VALUE;
1076 }
1077
Eric Laurentc75307b2015-03-17 15:29:32 -07001078 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1079
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001080 // Routing?
1081 mOutputRoutes.incRouteActivity(session);
1082
Eric Laurentc75307b2015-03-17 15:29:32 -07001083 audio_devices_t newDevice;
1084 if (outputDesc->mPolicyMix != NULL) {
1085 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001086 } else if (mOutputRoutes.hasRouteChanged(session)) {
1087 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001088 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001089 } else {
1090 newDevice = AUDIO_DEVICE_NONE;
1091 }
1092
1093 uint32_t delayMs = 0;
1094
Eric Laurentc75307b2015-03-17 15:29:32 -07001095 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1096
1097 if (status != NO_ERROR) {
1098 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001099 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001100 }
1101 // Automatically enable the remote submix input when output is started on a re routing mix
1102 // of type MIX_TYPE_RECORDERS
1103 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1104 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1105 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1106 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1107 outputDesc->mPolicyMix->mRegistrationId,
1108 "remote-submix");
1109 }
1110
1111 if (delayMs != 0) {
1112 usleep(delayMs * 1000);
1113 }
1114
1115 return status;
1116}
1117
1118status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1119 audio_stream_type_t stream,
1120 audio_devices_t device,
1121 uint32_t *delayMs)
1122{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001123 // cannot start playback of STREAM_TTS if any other output is being used
1124 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001125
1126 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001127 if (stream == AUDIO_STREAM_TTS) {
1128 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001129 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001130 return INVALID_OPERATION;
1131 } else {
1132 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1133 }
1134 } else {
1135 // some playback other than beacon starts
1136 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1137 }
1138
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001139 // check active before incrementing usage count
1140 bool force = !outputDesc->isActive();
1141
Eric Laurente552edb2014-03-10 17:42:56 -07001142 // increment usage count for this stream on the requested output:
1143 // NOTE that the usage count is the same for duplicated output and hardware output which is
1144 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1145 outputDesc->changeRefCount(stream, 1);
1146
Eric Laurent493404d2015-04-21 15:07:36 -07001147 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001148 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001149 if (device == AUDIO_DEVICE_NONE) {
1150 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001151 }
Eric Laurente552edb2014-03-10 17:42:56 -07001152 routing_strategy strategy = getStrategy(stream);
1153 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001154 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1155 (beaconMuteLatency > 0);
1156 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001157 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001158 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001159 if (desc != outputDesc) {
1160 // force a device change if any other output is managed by the same hw
1161 // module and has a current device selection that differs from selected device.
1162 // In this case, the audio HAL must receive the new device selection so that it can
1163 // change the device currently selected by the other active output.
1164 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001165 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001166 force = true;
1167 }
1168 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001169 // a notification so that audio focus effect can propagate, or that a mute/unmute
1170 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001171 uint32_t latency = desc->latency();
1172 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1173 waitMs = latency;
1174 }
1175 }
1176 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001177 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001178
1179 // handle special case for sonification while in call
1180 if (isInCall()) {
1181 handleIncallSonification(stream, true, false);
1182 }
1183
1184 // apply volume rules for current stream and device if necessary
1185 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01001186 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07001187 outputDesc,
1188 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001189
1190 // update the outputs if starting an output with a stream that can affect notification
1191 // routing
1192 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001193
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001194 // force reevaluating accessibility routing when ringtone or alarm starts
1195 if (strategy == STRATEGY_SONIFICATION) {
1196 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1197 }
Eric Laurente552edb2014-03-10 17:42:56 -07001198 }
1199 return NO_ERROR;
1200}
1201
1202
Eric Laurente0720872014-03-11 09:30:41 -07001203status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001204 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001205 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001206{
1207 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1208 ssize_t index = mOutputs.indexOfKey(output);
1209 if (index < 0) {
1210 ALOGW("stopOutput() unknown output %d", output);
1211 return BAD_VALUE;
1212 }
1213
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001215
Eric Laurentc75307b2015-03-17 15:29:32 -07001216 if (outputDesc->mRefCount[stream] == 1) {
1217 // Automatically disable the remote submix input when output is stopped on a
1218 // re routing mix of type MIX_TYPE_RECORDERS
1219 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1220 outputDesc->mPolicyMix != NULL &&
1221 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1222 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1223 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1224 outputDesc->mPolicyMix->mRegistrationId,
1225 "remote-submix");
1226 }
1227 }
1228
1229 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001230 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001231 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001232 int activityCount = mOutputRoutes.decRouteActivity(session);
1233 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1234
1235 if (forceDeviceUpdate) {
1236 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1237 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001238 }
1239
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001240 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001241}
1242
1243status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001244 audio_stream_type_t stream,
1245 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001246{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001247 // always handle stream stop, check which stream type is stopping
1248 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1249
Eric Laurente552edb2014-03-10 17:42:56 -07001250 // handle special case for sonification while in call
1251 if (isInCall()) {
1252 handleIncallSonification(stream, false, false);
1253 }
1254
1255 if (outputDesc->mRefCount[stream] > 0) {
1256 // decrement usage count of this stream on the output
1257 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001258
Eric Laurente552edb2014-03-10 17:42:56 -07001259 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001260 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001261 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001262 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001263 // delay the device switch by twice the latency because stopOutput() is executed when
1264 // the track stop() command is received and at that time the audio track buffer can
1265 // still contain data that needs to be drained. The latency only covers the audio HAL
1266 // and kernel buffers. Also the latency does not always include additional delay in the
1267 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001268 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001269
1270 // force restoring the device selection on other active outputs if it differs from the
1271 // one being selected for this output
1272 for (size_t i = 0; i < mOutputs.size(); i++) {
1273 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001274 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001275 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001276 desc->isActive() &&
1277 outputDesc->sharesHwModuleWith(desc) &&
1278 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001279 setOutputDevice(desc,
1280 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001281 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001282 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001283 }
1284 }
1285 // update the outputs if stopping one with a stream that can affect notification routing
1286 handleNotificationRoutingForStream(stream);
1287 }
1288 return NO_ERROR;
1289 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001290 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001291 return INVALID_OPERATION;
1292 }
1293}
1294
Eric Laurente83b55d2014-11-14 10:06:21 -08001295void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001296 audio_stream_type_t stream __unused,
1297 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001298{
1299 ALOGV("releaseOutput() %d", output);
1300 ssize_t index = mOutputs.indexOfKey(output);
1301 if (index < 0) {
1302 ALOGW("releaseOutput() releasing unknown output %d", output);
1303 return;
1304 }
1305
1306#ifdef AUDIO_POLICY_TEST
1307 int testIndex = testOutputIndex(output);
1308 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001309 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001310 if (outputDesc->isActive()) {
1311 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001312 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001313 mTestOutputs[testIndex] = 0;
1314 }
1315 return;
1316 }
1317#endif //AUDIO_POLICY_TEST
1318
Paul McLeanaa981192015-03-21 09:55:15 -07001319 // Routing
1320 mOutputRoutes.removeRoute(session);
1321
Eric Laurentc75307b2015-03-17 15:29:32 -07001322 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001323 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001324 if (desc->mDirectOpenCount <= 0) {
1325 ALOGW("releaseOutput() invalid open count %d for output %d",
1326 desc->mDirectOpenCount, output);
1327 return;
1328 }
1329 if (--desc->mDirectOpenCount == 0) {
1330 closeOutput(output);
1331 // If effects where present on the output, audioflinger moved them to the primary
1332 // output by default: move them back to the appropriate output.
1333 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001334 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001335 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1336 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001337 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001338 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001339 }
1340 }
1341}
1342
1343
Eric Laurentcaf7f482014-11-25 17:50:47 -08001344status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1345 audio_io_handle_t *input,
1346 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001347 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001348 uint32_t samplingRate,
1349 audio_format_t format,
1350 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001351 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001352 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001353 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001354{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1356 "session %d, flags %#x",
1357 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001358
Eric Laurentcaf7f482014-11-25 17:50:47 -08001359 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001360 *inputType = API_INPUT_INVALID;
Eric Laurentfb66dd92016-01-28 18:32:03 -08001361
Eric Laurent275e8e92014-11-30 15:14:47 -08001362 audio_devices_t device;
1363 // handle legacy remote submix case where the address was not always specified
1364 String8 address = String8("");
Eric Laurentc447ded2015-01-06 08:47:05 -08001365 audio_source_t inputSource = attr->source;
1366 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001367 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001368
Eric Laurentc447ded2015-01-06 08:47:05 -08001369 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1370 inputSource = AUDIO_SOURCE_MIC;
1371 }
1372 halInputSource = inputSource;
1373
Paul McLean466dc8e2015-04-17 13:15:36 -06001374 // Explicit routing?
1375 sp<DeviceDescriptor> deviceDesc;
1376 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1377 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1378 deviceDesc = mAvailableInputDevices[i];
1379 break;
1380 }
1381 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001382 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001383
Eric Laurentc447ded2015-01-06 08:47:05 -08001384 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001385 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001386 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001387 if (ret != NO_ERROR) {
1388 return ret;
1389 }
1390 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001391 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1392 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001393 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001394 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001395 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001396 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001397 return BAD_VALUE;
1398 }
Eric Laurentc722f302014-12-10 11:21:49 -08001399 if (policyMix != NULL) {
1400 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001401 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1402 // there is an external policy, but this input is attached to a mix of recorders,
1403 // meaning it receives audio injected into the framework, so the recorder doesn't
1404 // know about it and is therefore considered "legacy"
1405 *inputType = API_INPUT_LEGACY;
1406 } else {
1407 // recording a mix of players defined by an external policy, we're rerouting for
1408 // an external policy
1409 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1410 }
Eric Laurentc722f302014-12-10 11:21:49 -08001411 } else if (audio_is_remote_submix_device(device)) {
1412 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001413 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001414 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1415 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001416 } else {
1417 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001418 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001419
Eric Laurent599c7582015-12-07 18:05:55 -08001420 }
1421
1422 *input = getInputForDevice(device, address, session, uid, inputSource,
1423 samplingRate, format, channelMask, flags,
1424 policyMix);
1425 if (*input == AUDIO_IO_HANDLE_NONE) {
1426 mInputRoutes.removeRoute(session);
1427 return INVALID_OPERATION;
1428 }
1429 ALOGV("getInputForAttr() returns input type = %d", *inputType);
1430 return NO_ERROR;
1431}
1432
1433
1434audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device,
1435 String8 address,
1436 audio_session_t session,
1437 uid_t uid,
1438 audio_source_t inputSource,
1439 uint32_t samplingRate,
1440 audio_format_t format,
1441 audio_channel_mask_t channelMask,
1442 audio_input_flags_t flags,
1443 AudioMix *policyMix)
1444{
1445 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1446 audio_source_t halInputSource = inputSource;
1447 bool isSoundTrigger = false;
1448
1449 if (inputSource == AUDIO_SOURCE_HOTWORD) {
1450 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1451 if (index >= 0) {
1452 input = mSoundTriggerSessions.valueFor(session);
1453 isSoundTrigger = true;
1454 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1455 ALOGV("SoundTrigger capture on session %d input %d", session, input);
1456 } else {
1457 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001458 }
1459 }
1460
Andy Hungf129b032015-04-07 13:45:50 -07001461 // find a compatible input profile (not necessarily identical in parameters)
1462 sp<IOProfile> profile;
1463 // samplingRate and flags may be updated by getInputProfile
1464 uint32_t profileSamplingRate = samplingRate;
1465 audio_format_t profileFormat = format;
1466 audio_channel_mask_t profileChannelMask = channelMask;
1467 audio_input_flags_t profileFlags = flags;
1468 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001469 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001470 profileSamplingRate, profileFormat, profileChannelMask,
1471 profileFlags);
1472 if (profile != 0) {
1473 break; // success
1474 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1475 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1476 } else { // fail
Eric Laurent599c7582015-12-07 18:05:55 -08001477 ALOGW("getInputForDevice() could not find profile for device 0x%X,"
1478 "samplingRate %u, format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001479 device, samplingRate, format, channelMask, flags);
Eric Laurent599c7582015-12-07 18:05:55 -08001480 return input;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001481 }
Eric Laurente552edb2014-03-10 17:42:56 -07001482 }
1483
Eric Laurent322b4d22015-04-03 15:57:54 -07001484 if (profile->getModuleHandle() == 0) {
1485 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurent599c7582015-12-07 18:05:55 -08001486 return input;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001487 }
1488
Eric Laurent599c7582015-12-07 18:05:55 -08001489 sp<AudioSession> audioSession = new AudioSession(session,
1490 inputSource,
1491 format,
1492 samplingRate,
1493 channelMask,
1494 flags,
1495 uid,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001496 isSoundTrigger,
1497 policyMix, mpClientInterface);
Eric Laurent599c7582015-12-07 18:05:55 -08001498
Eric Laurentfb66dd92016-01-28 18:32:03 -08001499
Eric Laurent599c7582015-12-07 18:05:55 -08001500 // reuse an open input if possible
1501 for (size_t i = 0; i < mInputs.size(); i++) {
1502 sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001503 // reuse input if:
1504 // - it shares the same profile
1505 // AND
1506 // - it is not a reroute submix input
1507 // AND
1508 // - it is: not used for sound trigger
1509 // OR
1510 // used for sound trigger and all clients use the same session ID
1511 //
1512 if ((profile == desc->mProfile) &&
1513 (isSoundTrigger == desc->isSoundTrigger()) &&
1514 !is_virtual_input_device(device)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001515
1516 sp<AudioSession> as = desc->getAudioSession(session);
1517 if (as != 0) {
1518 // do not allow unmatching properties on same session
1519 if (as->matches(audioSession)) {
1520 as->changeOpenCount(1);
1521 } else {
1522 ALOGW("getInputForDevice() record with different attributes"
1523 " exists for session %d", session);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001524 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001525 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001526 } else if (isSoundTrigger) {
1527 break;
1528 }
1529 // force close input if current source is now the highest priority request on this input
1530 // and current input properties are not exactly as requested.
1531 if ((desc->mSamplingRate != samplingRate ||
1532 desc->mChannelMask != channelMask ||
Eric Laurente6930022016-02-11 10:20:40 -08001533 !audio_formats_match(desc->mFormat, format)) &&
Eric Laurentfb66dd92016-01-28 18:32:03 -08001534 (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) <
1535 source_priority(inputSource))) {
1536 ALOGV("%s: ", __FUNCTION__);
1537 AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/);
1538 for (size_t j = 0; j < sessions.size(); j++) {
1539 audio_session_t currentSession = sessions.keyAt(j);
1540 stopInput(desc->mIoHandle, currentSession);
1541 releaseInput(desc->mIoHandle, currentSession);
1542 }
1543 break;
Eric Laurent599c7582015-12-07 18:05:55 -08001544 } else {
1545 desc->addAudioSession(session, audioSession);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001546 ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i));
1547 return mInputs.keyAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08001548 }
Eric Laurent599c7582015-12-07 18:05:55 -08001549 }
1550 }
Eric Laurent599c7582015-12-07 18:05:55 -08001551
Eric Laurentcf2c0212014-07-25 16:20:43 -07001552 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001553 config.sample_rate = profileSamplingRate;
1554 config.channel_mask = profileChannelMask;
1555 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001556
Eric Laurent322b4d22015-04-03 15:57:54 -07001557 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurent599c7582015-12-07 18:05:55 -08001558 &input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001559 &config,
1560 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001561 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001562 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001563 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001564
1565 // only accept input with the exact requested set of parameters
Eric Laurent599c7582015-12-07 18:05:55 -08001566 if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001567 (profileSamplingRate != config.sample_rate) ||
Eric Laurente6930022016-02-11 10:20:40 -08001568 !audio_formats_match(profileFormat, config.format) ||
Andy Hungf129b032015-04-07 13:45:50 -07001569 (profileChannelMask != config.channel_mask)) {
Eric Laurent599c7582015-12-07 18:05:55 -08001570 ALOGW("getInputForAttr() failed opening input: samplingRate %d"
1571 ", format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001572 samplingRate, format, channelMask);
Eric Laurent599c7582015-12-07 18:05:55 -08001573 if (input != AUDIO_IO_HANDLE_NONE) {
1574 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001575 }
Eric Laurent599c7582015-12-07 18:05:55 -08001576 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001577 }
1578
Eric Laurent1f2f2232014-06-02 12:01:23 -07001579 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Andy Hungf129b032015-04-07 13:45:50 -07001580 inputDesc->mSamplingRate = profileSamplingRate;
1581 inputDesc->mFormat = profileFormat;
1582 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001583 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001584 inputDesc->mPolicyMix = policyMix;
Eric Laurent599c7582015-12-07 18:05:55 -08001585 inputDesc->addAudioSession(session, audioSession);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001586
Eric Laurent599c7582015-12-07 18:05:55 -08001587 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001588 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001589
Eric Laurent599c7582015-12-07 18:05:55 -08001590 return input;
Eric Laurente552edb2014-03-10 17:42:56 -07001591}
1592
Eric Laurentfb66dd92016-01-28 18:32:03 -08001593bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc,
1594 const sp<AudioSession>& audioSession)
1595{
1596 // Do not allow capture if an active voice call is using a software patch and
1597 // the call TX source device is on the same HW module.
1598 // FIXME: would be better to refine to only inputs whose profile connects to the
1599 // call TX device but this information is not in the audio patch
1600 if (mCallTxPatch != 0 &&
1601 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1602 return false;
1603 }
1604
1605 // starting concurrent capture is enabled if:
1606 // 1) capturing for re-routing
1607 // 2) capturing for HOTWORD source
1608 // 3) capturing for FM TUNER source
1609 // 3) All other active captures are either for re-routing or HOTWORD
1610
1611 if (is_virtual_input_device(inputDesc->mDevice) ||
1612 audioSession->inputSource() == AUDIO_SOURCE_HOTWORD ||
1613 audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) {
1614 return true;
1615 }
1616
1617 Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs();
1618 for (size_t i = 0; i < activeInputs.size(); i++) {
1619 sp<AudioInputDescriptor> activeInput = activeInputs[i];
1620 if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) &&
1621 (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) &&
1622 !is_virtual_input_device(activeInput->mDevice)) {
1623 return false;
1624 }
1625 }
1626
1627 return true;
1628}
1629
1630
Eric Laurent4dc68062014-07-28 17:26:49 -07001631status_t AudioPolicyManager::startInput(audio_io_handle_t input,
Eric Laurentfb66dd92016-01-28 18:32:03 -08001632 audio_session_t session,
1633 concurrency_type__mask_t *concurrency)
Eric Laurente552edb2014-03-10 17:42:56 -07001634{
1635 ALOGV("startInput() input %d", input);
Eric Laurentfb66dd92016-01-28 18:32:03 -08001636 *concurrency = API_INPUT_CONCURRENCY_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001637 ssize_t index = mInputs.indexOfKey(input);
1638 if (index < 0) {
1639 ALOGW("startInput() unknown input %d", input);
1640 return BAD_VALUE;
1641 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001642 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001643
Eric Laurent599c7582015-12-07 18:05:55 -08001644 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
1645 if (audioSession == 0) {
Eric Laurent4dc68062014-07-28 17:26:49 -07001646 ALOGW("startInput() unknown session %d on input %d", session, input);
1647 return BAD_VALUE;
1648 }
1649
Eric Laurentfb66dd92016-01-28 18:32:03 -08001650 if (!isConcurentCaptureAllowed(inputDesc, audioSession)) {
1651 ALOGW("startInput(%d) failed: other input already started", input);
1652 return INVALID_OPERATION;
1653 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001654
Eric Laurentfb66dd92016-01-28 18:32:03 -08001655 if (isInCall()) {
1656 *concurrency |= API_INPUT_CONCURRENCY_CALL;
1657 }
1658 if (mInputs.activeInputsCount() != 0) {
1659 *concurrency |= API_INPUT_CONCURRENCY_CAPTURE;
Eric Laurente552edb2014-03-10 17:42:56 -07001660 }
1661
Eric Laurent313d1e72016-01-29 09:56:57 -08001662 // increment activity count before calling getNewInputDevice() below as only active sessions
1663 // are considered for device selection
1664 audioSession->changeActiveCount(1);
1665
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001666 // Routing?
1667 mInputRoutes.incRouteActivity(session);
1668
Eric Laurent313d1e72016-01-29 09:56:57 -08001669 if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001670
Eric Laurentfb66dd92016-01-28 18:32:03 -08001671 setInputDevice(input, getNewInputDevice(inputDesc), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001672
Jean-Michel Trivi56afc7a2016-02-03 11:35:52 -08001673 if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) {
Eric Laurentfb66dd92016-01-28 18:32:03 -08001674 // if input maps to a dynamic policy with an activity listener, notify of state change
1675 if ((inputDesc->mPolicyMix != NULL)
1676 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1677 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1678 MIX_STATE_MIXING);
Eric Laurentc722f302014-12-10 11:21:49 -08001679 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001680
1681 if (mInputs.activeInputsCount() == 0) {
1682 SoundTrigger::setCaptureState(true);
1683 }
1684
1685 // automatically enable the remote submix output when input is started if not
1686 // used by a policy mix of type MIX_TYPE_RECORDERS
1687 // For remote submix (a virtual device), we open only one input per capture request.
1688 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1689 String8 address = String8("");
1690 if (inputDesc->mPolicyMix == NULL) {
1691 address = String8("0");
1692 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1693 address = inputDesc->mPolicyMix->mRegistrationId;
1694 }
1695 if (address != "") {
1696 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1697 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1698 address, "remote-submix");
1699 }
Eric Laurentc722f302014-12-10 11:21:49 -08001700 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001701 }
Eric Laurente552edb2014-03-10 17:42:56 -07001702 }
1703
Eric Laurent599c7582015-12-07 18:05:55 -08001704 ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource());
Eric Laurente552edb2014-03-10 17:42:56 -07001705
Eric Laurente552edb2014-03-10 17:42:56 -07001706 return NO_ERROR;
1707}
1708
Eric Laurent4dc68062014-07-28 17:26:49 -07001709status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1710 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001711{
1712 ALOGV("stopInput() input %d", input);
1713 ssize_t index = mInputs.indexOfKey(input);
1714 if (index < 0) {
1715 ALOGW("stopInput() unknown input %d", input);
1716 return BAD_VALUE;
1717 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001718 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001719
Eric Laurent599c7582015-12-07 18:05:55 -08001720 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001721 if (index < 0) {
1722 ALOGW("stopInput() unknown session %d on input %d", session, input);
1723 return BAD_VALUE;
1724 }
1725
Eric Laurent599c7582015-12-07 18:05:55 -08001726 if (audioSession->activeCount() == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001727 ALOGW("stopInput() input %d already stopped", input);
1728 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001729 }
1730
Eric Laurent599c7582015-12-07 18:05:55 -08001731 audioSession->changeActiveCount(-1);
Paul McLean466dc8e2015-04-17 13:15:36 -06001732
1733 // Routing?
1734 mInputRoutes.decRouteActivity(session);
1735
Eric Laurentfb66dd92016-01-28 18:32:03 -08001736 if (audioSession->activeCount() == 0) {
Eric Laurent84332aa2016-01-28 22:19:18 +00001737
Eric Laurentfb66dd92016-01-28 18:32:03 -08001738 if (inputDesc->isActive()) {
1739 setInputDevice(input, getNewInputDevice(inputDesc), false /* force */);
1740 } else {
1741 // if input maps to a dynamic policy with an activity listener, notify of state change
1742 if ((inputDesc->mPolicyMix != NULL)
1743 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
1744 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1745 MIX_STATE_IDLE);
Eric Laurent84332aa2016-01-28 22:19:18 +00001746 }
Eric Laurentfb66dd92016-01-28 18:32:03 -08001747
1748 // automatically disable the remote submix output when input is stopped if not
1749 // used by a policy mix of type MIX_TYPE_RECORDERS
1750 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1751 String8 address = String8("");
1752 if (inputDesc->mPolicyMix == NULL) {
1753 address = String8("0");
1754 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1755 address = inputDesc->mPolicyMix->mRegistrationId;
1756 }
1757 if (address != "") {
1758 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1759 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1760 address, "remote-submix");
1761 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001762 }
Eric Laurent84332aa2016-01-28 22:19:18 +00001763
Eric Laurentfb66dd92016-01-28 18:32:03 -08001764 resetInputDevice(input);
Eric Laurent84332aa2016-01-28 22:19:18 +00001765
Eric Laurentfb66dd92016-01-28 18:32:03 -08001766 if (mInputs.activeInputsCount() == 0) {
1767 SoundTrigger::setCaptureState(false);
1768 }
1769 inputDesc->clearPreemptedSessions();
Eric Laurent84332aa2016-01-28 22:19:18 +00001770 }
Eric Laurente552edb2014-03-10 17:42:56 -07001771 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001772 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001773}
1774
Eric Laurent4dc68062014-07-28 17:26:49 -07001775void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1776 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001777{
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001778
Eric Laurente552edb2014-03-10 17:42:56 -07001779 ALOGV("releaseInput() %d", input);
1780 ssize_t index = mInputs.indexOfKey(input);
1781 if (index < 0) {
1782 ALOGW("releaseInput() releasing unknown input %d", input);
1783 return;
1784 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001785
1786 // Routing
1787 mInputRoutes.removeRoute(session);
1788
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001789 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1790 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001791
Eric Laurent599c7582015-12-07 18:05:55 -08001792 sp<AudioSession> audioSession = inputDesc->getAudioSession(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001793 if (index < 0) {
1794 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1795 return;
1796 }
Eric Laurent599c7582015-12-07 18:05:55 -08001797
1798 if (audioSession->openCount() == 0) {
1799 ALOGW("releaseInput() invalid open count %d on session %d",
1800 audioSession->openCount(), session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001801 return;
1802 }
Eric Laurent599c7582015-12-07 18:05:55 -08001803
1804 if (audioSession->changeOpenCount(-1) == 0) {
1805 inputDesc->removeAudioSession(session);
1806 }
1807
Jean-Michel Trivi65bfe912015-12-04 15:56:47 -08001808 if (inputDesc->getOpenRefCount() > 0) {
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001809 ALOGV("releaseInput() exit > 0");
1810 return;
1811 }
1812
Eric Laurent05b90f82014-08-27 15:32:29 -07001813 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001814 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001815 ALOGV("releaseInput() exit");
1816}
1817
Eric Laurentd4692962014-05-05 18:13:44 -07001818void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001819 bool patchRemoved = false;
1820
Eric Laurentd4692962014-05-05 18:13:44 -07001821 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001822 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1823 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1824 if (patch_index >= 0) {
1825 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1826 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1827 mAudioPatches.removeItemsAt(patch_index);
1828 patchRemoved = true;
1829 }
Eric Laurentd4692962014-05-05 18:13:44 -07001830 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1831 }
1832 mInputs.clear();
Chris Thornton52785f32016-02-09 17:28:49 -08001833 SoundTrigger::setCaptureState(false);
Eric Laurent6a94d692014-05-20 11:18:06 -07001834 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001835
1836 if (patchRemoved) {
1837 mpClientInterface->onAudioPatchListUpdate();
1838 }
Eric Laurentd4692962014-05-05 18:13:44 -07001839}
1840
Eric Laurente0720872014-03-11 09:30:41 -07001841void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001842 int indexMin,
1843 int indexMax)
1844{
1845 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffied1ab2bd2015-12-02 18:20:06 +01001846 mVolumeCurves->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001847 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001848 mVolumeCurves->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001849 }
Eric Laurente552edb2014-03-10 17:42:56 -07001850}
1851
Eric Laurente0720872014-03-11 09:30:41 -07001852status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001853 int index,
1854 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001855{
1856
François Gaffied1ab2bd2015-12-02 18:20:06 +01001857 if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
1858 (index > mVolumeCurves->getVolumeIndexMax(stream))) {
Eric Laurente552edb2014-03-10 17:42:56 -07001859 return BAD_VALUE;
1860 }
1861 if (!audio_is_output_device(device)) {
1862 return BAD_VALUE;
1863 }
1864
1865 // Force max volume if stream cannot be muted
François Gaffied1ab2bd2015-12-02 18:20:06 +01001866 if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001867
1868 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1869 stream, device, index);
1870
1871 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1872 // clear all device specific values
1873 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001874 mVolumeCurves->clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001875 }
François Gaffied1ab2bd2015-12-02 18:20:06 +01001876 mVolumeCurves->addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001877
Eric Laurent31551f82014-10-10 18:21:56 -07001878 // update volume on all outputs whose current device is also selected by the same
1879 // strategy as the device specified by the caller
Eric Laurente04e62b2016-01-29 18:25:01 -08001880 audio_devices_t selectedDevices = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1881 // it is possible that the requested device is not selected by the strategy (e.g an explicit
1882 // audio patch is active causing getDevicesForStream() to return this device. We must make
1883 // sure that the device passed is part of the devices considered when applying volume below.
1884 selectedDevices |= device;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001885
1886 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1887 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1888 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01001889 mVolumeCurves->addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001890 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1891 }
Eric Laurente04e62b2016-01-29 18:25:01 -08001892
Eric Laurente552edb2014-03-10 17:42:56 -07001893 status_t status = NO_ERROR;
1894 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001895 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1896 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurente04e62b2016-01-29 18:25:01 -08001897 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & selectedDevices) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001898 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001899 if (volStatus != NO_ERROR) {
1900 status = volStatus;
1901 }
1902 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001903 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1904 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1905 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001906 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001907 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001908 }
Eric Laurente552edb2014-03-10 17:42:56 -07001909 }
1910 return status;
1911}
1912
Eric Laurente0720872014-03-11 09:30:41 -07001913status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001914 int *index,
1915 audio_devices_t device)
1916{
1917 if (index == NULL) {
1918 return BAD_VALUE;
1919 }
1920 if (!audio_is_output_device(device)) {
1921 return BAD_VALUE;
1922 }
1923 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1924 // the strategy the stream belongs to.
1925 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1926 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1927 }
François Gaffiedfd74092015-03-19 12:10:59 +01001928 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001929
François Gaffied1ab2bd2015-12-02 18:20:06 +01001930 *index = mVolumeCurves->getVolumeIndex(stream, device);
Eric Laurente552edb2014-03-10 17:42:56 -07001931 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1932 return NO_ERROR;
1933}
1934
Eric Laurente0720872014-03-11 09:30:41 -07001935audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001936 const SortedVector<audio_io_handle_t>& outputs)
1937{
1938 // select one output among several suitable for global effects.
1939 // The priority is as follows:
1940 // 1: An offloaded output. If the effect ends up not being offloadable,
1941 // AudioFlinger will invalidate the track and the offloaded output
1942 // will be closed causing the effect to be moved to a PCM output.
1943 // 2: A deep buffer output
1944 // 3: the first output in the list
1945
1946 if (outputs.size() == 0) {
1947 return 0;
1948 }
1949
1950 audio_io_handle_t outputOffloaded = 0;
1951 audio_io_handle_t outputDeepBuffer = 0;
1952
1953 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001954 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001955 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001956 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1957 outputOffloaded = outputs[i];
1958 }
1959 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1960 outputDeepBuffer = outputs[i];
1961 }
1962 }
1963
1964 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1965 outputOffloaded, outputDeepBuffer);
1966 if (outputOffloaded != 0) {
1967 return outputOffloaded;
1968 }
1969 if (outputDeepBuffer != 0) {
1970 return outputDeepBuffer;
1971 }
1972
1973 return outputs[0];
1974}
1975
Eric Laurente0720872014-03-11 09:30:41 -07001976audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001977{
1978 // apply simple rule where global effects are attached to the same output as MUSIC streams
1979
Eric Laurent3b73df72014-03-11 09:06:29 -07001980 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001981 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1982 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1983
1984 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1985 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1986 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1987
1988 return output;
1989}
1990
Eric Laurente0720872014-03-11 09:30:41 -07001991status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001992 audio_io_handle_t io,
1993 uint32_t strategy,
1994 int session,
1995 int id)
1996{
1997 ssize_t index = mOutputs.indexOfKey(io);
1998 if (index < 0) {
1999 index = mInputs.indexOfKey(io);
2000 if (index < 0) {
2001 ALOGW("registerEffect() unknown io %d", io);
2002 return INVALID_OPERATION;
2003 }
2004 }
François Gaffie45ed3b02015-03-19 10:35:14 +01002005 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07002006}
2007
Eric Laurentc75307b2015-03-17 15:29:32 -07002008bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
2009{
2010 return mOutputs.isStreamActive(stream, inPastMs);
2011}
2012
2013bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
2014{
2015 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
2016}
2017
Eric Laurente0720872014-03-11 09:30:41 -07002018bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07002019{
2020 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002021 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002022 if (inputDescriptor->isSourceActive(source)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002023 return true;
2024 }
2025 }
2026 return false;
2027}
2028
Eric Laurent275e8e92014-11-30 15:14:47 -08002029// Register a list of custom mixes with their attributes and format.
2030// When a mix is registered, corresponding input and output profiles are
2031// added to the remote submix hw module. The profile contains only the
2032// parameters (sampling rate, format...) specified by the mix.
2033// The corresponding input remote submix device is also connected.
2034//
2035// When a remote submix device is connected, the address is checked to select the
2036// appropriate profile and the corresponding input or output stream is opened.
2037//
2038// When capture starts, getInputForAttr() will:
2039// - 1 look for a mix matching the address passed in attribtutes tags if any
2040// - 2 if none found, getDeviceForInputSource() will:
2041// - 2.1 look for a mix matching the attributes source
2042// - 2.2 if none found, default to device selection by policy rules
2043// At this time, the corresponding output remote submix device is also connected
2044// and active playback use cases can be transferred to this mix if needed when reconnecting
2045// after AudioTracks are invalidated
2046//
2047// When playback starts, getOutputForAttr() will:
2048// - 1 look for a mix matching the address passed in attribtutes tags if any
2049// - 2 if none found, look for a mix matching the attributes usage
2050// - 3 if none found, default to device and output selection by policy rules.
2051
2052status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2053{
2054 sp<HwModule> module;
2055 for (size_t i = 0; i < mHwModules.size(); i++) {
2056 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2057 mHwModules[i]->mHandle != 0) {
2058 module = mHwModules[i];
2059 break;
2060 }
2061 }
2062
2063 if (module == 0) {
2064 return INVALID_OPERATION;
2065 }
2066
Eric Laurent7b279bb2015-12-14 10:18:23 -08002067 ALOGV("registerPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002068
2069 for (size_t i = 0; i < mixes.size(); i++) {
2070 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002071
2072 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002073 continue;
2074 }
2075 audio_config_t outputConfig = mixes[i].mFormat;
2076 audio_config_t inputConfig = mixes[i].mFormat;
2077 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2078 // stereo and let audio flinger do the channel conversion if needed.
2079 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2080 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2081 module->addOutputProfile(address, &outputConfig,
2082 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2083 module->addInputProfile(address, &inputConfig,
2084 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01002085
Eric Laurentc722f302014-12-10 11:21:49 -08002086 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002087 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002088 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002089 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002090 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002091 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002092 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002093 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002094 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002095 }
2096 return NO_ERROR;
2097}
2098
2099status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2100{
2101 sp<HwModule> module;
2102 for (size_t i = 0; i < mHwModules.size(); i++) {
2103 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2104 mHwModules[i]->mHandle != 0) {
2105 module = mHwModules[i];
2106 break;
2107 }
2108 }
2109
2110 if (module == 0) {
2111 return INVALID_OPERATION;
2112 }
2113
Eric Laurent7b279bb2015-12-14 10:18:23 -08002114 ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size());
Eric Laurent275e8e92014-11-30 15:14:47 -08002115
2116 for (size_t i = 0; i < mixes.size(); i++) {
2117 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01002118
2119 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08002120 continue;
2121 }
2122
Eric Laurentc722f302014-12-10 11:21:49 -08002123 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2124 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2125 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002126 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002127 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002128 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002129 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002130
2131 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2132 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2133 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002134 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002135 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002136 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002137 }
2138 module->removeOutputProfile(address);
2139 module->removeInputProfile(address);
2140 }
2141 return NO_ERROR;
2142}
2143
Eric Laurente552edb2014-03-10 17:42:56 -07002144
Eric Laurente0720872014-03-11 09:30:41 -07002145status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002146{
2147 const size_t SIZE = 256;
2148 char buffer[SIZE];
2149 String8 result;
2150
2151 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2152 result.append(buffer);
2153
Eric Laurent87ffa392015-05-22 10:32:38 -07002154 snprintf(buffer, SIZE, " Primary Output: %d\n",
2155 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002156 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002157 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002158 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002159 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002160 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002161 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002162 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002163 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002164 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002165 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002166 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002167 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002168 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002169 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002170 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002171 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002172 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002173 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2174 result.append(buffer);
Andy Hung2ddee192015-12-18 17:34:44 -08002175 snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off");
2176 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002177
François Gaffie2110e042015-03-24 08:41:51 +01002178 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002179
François Gaffie112b0af2015-11-19 16:13:25 +01002180 mAvailableOutputDevices.dump(fd, String8("Available output"));
2181 mAvailableInputDevices.dump(fd, String8("Available input"));
François Gaffie53615e22015-03-19 09:24:12 +01002182 mHwModules.dump(fd);
2183 mOutputs.dump(fd);
2184 mInputs.dump(fd);
François Gaffied1ab2bd2015-12-02 18:20:06 +01002185 mVolumeCurves->dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002186 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002187 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002188
2189 return NO_ERROR;
2190}
2191
2192// This function checks for the parameters which can be offloaded.
2193// This can be enhanced depending on the capability of the DSP and policy
2194// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002195bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002196{
2197 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002198 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002199 offloadInfo.sample_rate, offloadInfo.channel_mask,
2200 offloadInfo.format,
2201 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2202 offloadInfo.has_video);
2203
Andy Hung2ddee192015-12-18 17:34:44 -08002204 if (mMasterMono) {
2205 return false; // no offloading if mono is set.
2206 }
2207
Eric Laurente552edb2014-03-10 17:42:56 -07002208 // Check if offload has been disabled
2209 char propValue[PROPERTY_VALUE_MAX];
2210 if (property_get("audio.offload.disable", propValue, "0")) {
2211 if (atoi(propValue) != 0) {
2212 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2213 return false;
2214 }
2215 }
2216
2217 // Check if stream type is music, then only allow offload as of now.
2218 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2219 {
2220 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2221 return false;
2222 }
2223
Wei Jia7b8d4342016-02-16 17:56:37 -08002224 // Check if streaming is off, then only allow offload as of now.
Wei Jia4ca44d72016-02-17 16:56:38 -08002225 // This is a temporary work around until the root cause is fixed in offload
2226 // playback path.
Wei Jia7b8d4342016-02-16 17:56:37 -08002227 if (offloadInfo.is_streaming)
2228 {
2229 ALOGV("isOffloadSupported: is_streaming == true, returning false");
2230 return false;
2231 }
2232
Eric Laurente552edb2014-03-10 17:42:56 -07002233 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002234 const bool allowOffloadWithVideo =
2235 property_get_bool("audio.offload.video", false /* default_value */);
2236 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002237 ALOGV("isOffloadSupported: has_video == true, returning false");
2238 return false;
2239 }
2240
2241 //If duration is less than minimum value defined in property, return false
2242 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2243 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2244 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2245 return false;
2246 }
2247 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2248 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2249 return false;
2250 }
2251
2252 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2253 // creating an offloaded track and tearing it down immediately after start when audioflinger
2254 // detects there is an active non offloadable effect.
2255 // FIXME: We should check the audio session here but we do not have it in this context.
2256 // This may prevent offloading in rare situations where effects are left active by apps
2257 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002258 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002259 return false;
2260 }
2261
2262 // See if there is a profile to support this.
2263 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002264 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002265 offloadInfo.sample_rate,
2266 offloadInfo.format,
2267 offloadInfo.channel_mask,
2268 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002269 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2270 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002271}
2272
Eric Laurent6a94d692014-05-20 11:18:06 -07002273status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2274 audio_port_type_t type,
2275 unsigned int *num_ports,
2276 struct audio_port *ports,
2277 unsigned int *generation)
2278{
2279 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2280 generation == NULL) {
2281 return BAD_VALUE;
2282 }
2283 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2284 if (ports == NULL) {
2285 *num_ports = 0;
2286 }
2287
2288 size_t portsWritten = 0;
2289 size_t portsMax = *num_ports;
2290 *num_ports = 0;
2291 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2292 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2293 for (size_t i = 0;
2294 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2295 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2296 }
2297 *num_ports += mAvailableOutputDevices.size();
2298 }
2299 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2300 for (size_t i = 0;
2301 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2302 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2303 }
2304 *num_ports += mAvailableInputDevices.size();
2305 }
2306 }
2307 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2308 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2309 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2310 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2311 }
2312 *num_ports += mInputs.size();
2313 }
2314 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002315 size_t numOutputs = 0;
2316 for (size_t i = 0; i < mOutputs.size(); i++) {
2317 if (!mOutputs[i]->isDuplicated()) {
2318 numOutputs++;
2319 if (portsWritten < portsMax) {
2320 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2321 }
2322 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002323 }
Eric Laurent84c70242014-06-23 08:46:27 -07002324 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002325 }
2326 }
2327 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002328 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002329 return NO_ERROR;
2330}
2331
2332status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2333{
2334 return NO_ERROR;
2335}
2336
Eric Laurent6a94d692014-05-20 11:18:06 -07002337status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2338 audio_patch_handle_t *handle,
2339 uid_t uid)
2340{
2341 ALOGV("createAudioPatch()");
2342
2343 if (handle == NULL || patch == NULL) {
2344 return BAD_VALUE;
2345 }
2346 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2347
Eric Laurent874c42872014-08-08 15:13:39 -07002348 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2349 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2350 return BAD_VALUE;
2351 }
2352 // only one source per audio patch supported for now
2353 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002354 return INVALID_OPERATION;
2355 }
Eric Laurent874c42872014-08-08 15:13:39 -07002356
2357 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002358 return INVALID_OPERATION;
2359 }
Eric Laurent874c42872014-08-08 15:13:39 -07002360 for (size_t i = 0; i < patch->num_sinks; i++) {
2361 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2362 return INVALID_OPERATION;
2363 }
2364 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002365
2366 sp<AudioPatch> patchDesc;
2367 ssize_t index = mAudioPatches.indexOfKey(*handle);
2368
Eric Laurent6a94d692014-05-20 11:18:06 -07002369 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2370 patch->sources[0].role,
2371 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002372#if LOG_NDEBUG == 0
2373 for (size_t i = 0; i < patch->num_sinks; i++) {
Eric Laurent7b279bb2015-12-14 10:18:23 -08002374 ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id,
Eric Laurent874c42872014-08-08 15:13:39 -07002375 patch->sinks[i].role,
2376 patch->sinks[i].type);
2377 }
2378#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002379
2380 if (index >= 0) {
2381 patchDesc = mAudioPatches.valueAt(index);
2382 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2383 mUidCached, patchDesc->mUid, uid);
2384 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2385 return INVALID_OPERATION;
2386 }
2387 } else {
2388 *handle = 0;
2389 }
2390
2391 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002392 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002393 if (outputDesc == NULL) {
2394 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2395 return BAD_VALUE;
2396 }
Eric Laurent84c70242014-06-23 08:46:27 -07002397 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2398 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002399 if (patchDesc != 0) {
2400 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2401 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2402 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2403 return BAD_VALUE;
2404 }
2405 }
Eric Laurent874c42872014-08-08 15:13:39 -07002406 DeviceVector devices;
2407 for (size_t i = 0; i < patch->num_sinks; i++) {
2408 // Only support mix to devices connection
2409 // TODO add support for mix to mix connection
2410 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2411 ALOGV("createAudioPatch() source mix but sink is not a device");
2412 return INVALID_OPERATION;
2413 }
2414 sp<DeviceDescriptor> devDesc =
2415 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2416 if (devDesc == 0) {
2417 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2418 return BAD_VALUE;
2419 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002420
François Gaffie53615e22015-03-19 09:24:12 +01002421 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002422 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002423 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002424 NULL, // updatedSamplingRate
2425 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002426 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002427 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002428 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002429 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002430 ALOGV("createAudioPatch() profile not supported for device %08x",
2431 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002432 return INVALID_OPERATION;
2433 }
2434 devices.add(devDesc);
2435 }
2436 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002437 return INVALID_OPERATION;
2438 }
Eric Laurent874c42872014-08-08 15:13:39 -07002439
Eric Laurent6a94d692014-05-20 11:18:06 -07002440 // TODO: reconfigure output format and channels here
2441 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002442 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002443 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002444 index = mAudioPatches.indexOfKey(*handle);
2445 if (index >= 0) {
2446 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2447 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2448 }
2449 patchDesc = mAudioPatches.valueAt(index);
2450 patchDesc->mUid = uid;
2451 ALOGV("createAudioPatch() success");
2452 } else {
2453 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2454 return INVALID_OPERATION;
2455 }
2456 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2457 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2458 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002459 // only one sink supported when connecting an input device to a mix
2460 if (patch->num_sinks > 1) {
2461 return INVALID_OPERATION;
2462 }
François Gaffie53615e22015-03-19 09:24:12 +01002463 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002464 if (inputDesc == NULL) {
2465 return BAD_VALUE;
2466 }
2467 if (patchDesc != 0) {
2468 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2469 return BAD_VALUE;
2470 }
2471 }
2472 sp<DeviceDescriptor> devDesc =
2473 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2474 if (devDesc == 0) {
2475 return BAD_VALUE;
2476 }
2477
François Gaffie53615e22015-03-19 09:24:12 +01002478 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002479 devDesc->mAddress,
2480 patch->sinks[0].sample_rate,
2481 NULL, /*updatedSampleRate*/
2482 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002483 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002484 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002485 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002486 // FIXME for the parameter type,
2487 // and the NONE
2488 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002489 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002490 return INVALID_OPERATION;
2491 }
2492 // TODO: reconfigure output format and channels here
2493 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002494 devDesc->type(), inputDesc->mIoHandle);
2495 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002496 index = mAudioPatches.indexOfKey(*handle);
2497 if (index >= 0) {
2498 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2499 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2500 }
2501 patchDesc = mAudioPatches.valueAt(index);
2502 patchDesc->mUid = uid;
2503 ALOGV("createAudioPatch() success");
2504 } else {
2505 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2506 return INVALID_OPERATION;
2507 }
2508 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2509 // device to device connection
2510 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002511 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002512 return BAD_VALUE;
2513 }
2514 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002515 sp<DeviceDescriptor> srcDeviceDesc =
2516 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002517 if (srcDeviceDesc == 0) {
2518 return BAD_VALUE;
2519 }
Eric Laurent874c42872014-08-08 15:13:39 -07002520
Eric Laurent6a94d692014-05-20 11:18:06 -07002521 //update source and sink with our own data as the data passed in the patch may
2522 // be incomplete.
2523 struct audio_patch newPatch = *patch;
2524 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002525
Eric Laurent874c42872014-08-08 15:13:39 -07002526 for (size_t i = 0; i < patch->num_sinks; i++) {
2527 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2528 ALOGV("createAudioPatch() source device but one sink is not a device");
2529 return INVALID_OPERATION;
2530 }
2531
2532 sp<DeviceDescriptor> sinkDeviceDesc =
2533 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2534 if (sinkDeviceDesc == 0) {
2535 return BAD_VALUE;
2536 }
2537 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2538
Eric Laurent3bcf8592015-04-03 12:13:24 -07002539 // create a software bridge in PatchPanel if:
2540 // - source and sink devices are on differnt HW modules OR
2541 // - audio HAL version is < 3.0
Eric Laurentfb66dd92016-01-28 18:32:03 -08002542 if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002543 (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) {
Eric Laurent3bcf8592015-04-03 12:13:24 -07002544 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002545 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002546 return INVALID_OPERATION;
2547 }
Eric Laurent874c42872014-08-08 15:13:39 -07002548 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002549 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002550 // if the sink device is reachable via an opened output stream, request to go via
2551 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002552 audio_io_handle_t output = selectOutput(outputs,
2553 AUDIO_OUTPUT_FLAG_NONE,
2554 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002555 if (output != AUDIO_IO_HANDLE_NONE) {
2556 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2557 if (outputDesc->isDuplicated()) {
2558 return INVALID_OPERATION;
2559 }
2560 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002561 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002562 newPatch.num_sources = 2;
2563 }
Eric Laurent83b88082014-06-20 18:31:16 -07002564 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002565 }
2566 // TODO: check from routing capabilities in config file and other conflicting patches
2567
2568 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2569 if (index >= 0) {
2570 afPatchHandle = patchDesc->mAfPatchHandle;
2571 }
2572
2573 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2574 &afPatchHandle,
2575 0);
2576 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2577 status, afPatchHandle);
2578 if (status == NO_ERROR) {
2579 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002580 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002581 addAudioPatch(patchDesc->mHandle, patchDesc);
2582 } else {
2583 patchDesc->mPatch = newPatch;
2584 }
2585 patchDesc->mAfPatchHandle = afPatchHandle;
2586 *handle = patchDesc->mHandle;
2587 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002588 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002589 } else {
2590 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2591 status);
2592 return INVALID_OPERATION;
2593 }
2594 } else {
2595 return BAD_VALUE;
2596 }
2597 } else {
2598 return BAD_VALUE;
2599 }
2600 return NO_ERROR;
2601}
2602
2603status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2604 uid_t uid)
2605{
2606 ALOGV("releaseAudioPatch() patch %d", handle);
2607
2608 ssize_t index = mAudioPatches.indexOfKey(handle);
2609
2610 if (index < 0) {
2611 return BAD_VALUE;
2612 }
2613 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2614 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2615 mUidCached, patchDesc->mUid, uid);
2616 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2617 return INVALID_OPERATION;
2618 }
2619
2620 struct audio_patch *patch = &patchDesc->mPatch;
2621 patchDesc->mUid = mUidCached;
2622 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002623 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002624 if (outputDesc == NULL) {
2625 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2626 return BAD_VALUE;
2627 }
2628
Eric Laurentc75307b2015-03-17 15:29:32 -07002629 setOutputDevice(outputDesc,
2630 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002631 true,
2632 0,
2633 NULL);
2634 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2635 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002636 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002637 if (inputDesc == NULL) {
2638 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2639 return BAD_VALUE;
2640 }
2641 setInputDevice(inputDesc->mIoHandle,
Eric Laurentfb66dd92016-01-28 18:32:03 -08002642 getNewInputDevice(inputDesc),
Eric Laurent6a94d692014-05-20 11:18:06 -07002643 true,
2644 NULL);
2645 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2646 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2647 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2648 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2649 status, patchDesc->mAfPatchHandle);
2650 removeAudioPatch(patchDesc->mHandle);
2651 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002652 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002653 } else {
2654 return BAD_VALUE;
2655 }
2656 } else {
2657 return BAD_VALUE;
2658 }
2659 return NO_ERROR;
2660}
2661
2662status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2663 struct audio_patch *patches,
2664 unsigned int *generation)
2665{
François Gaffie53615e22015-03-19 09:24:12 +01002666 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002667 return BAD_VALUE;
2668 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002669 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002670 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002671}
2672
Eric Laurente1715a42014-05-20 11:30:42 -07002673status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002674{
Eric Laurente1715a42014-05-20 11:30:42 -07002675 ALOGV("setAudioPortConfig()");
2676
2677 if (config == NULL) {
2678 return BAD_VALUE;
2679 }
2680 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2681 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002682 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2683 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002684 }
2685
Eric Laurenta121f902014-06-03 13:32:54 -07002686 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002687 if (config->type == AUDIO_PORT_TYPE_MIX) {
2688 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002689 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002690 if (outputDesc == NULL) {
2691 return BAD_VALUE;
2692 }
Eric Laurent84c70242014-06-23 08:46:27 -07002693 ALOG_ASSERT(!outputDesc->isDuplicated(),
2694 "setAudioPortConfig() called on duplicated output %d",
2695 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002696 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002697 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002698 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002699 if (inputDesc == NULL) {
2700 return BAD_VALUE;
2701 }
Eric Laurenta121f902014-06-03 13:32:54 -07002702 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002703 } else {
2704 return BAD_VALUE;
2705 }
2706 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2707 sp<DeviceDescriptor> deviceDesc;
2708 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2709 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2710 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2711 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2712 } else {
2713 return BAD_VALUE;
2714 }
2715 if (deviceDesc == NULL) {
2716 return BAD_VALUE;
2717 }
Eric Laurenta121f902014-06-03 13:32:54 -07002718 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002719 } else {
2720 return BAD_VALUE;
2721 }
2722
Eric Laurenta121f902014-06-03 13:32:54 -07002723 struct audio_port_config backupConfig;
2724 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2725 if (status == NO_ERROR) {
2726 struct audio_port_config newConfig;
2727 audioPortConfig->toAudioPortConfig(&newConfig, config);
2728 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002729 }
Eric Laurenta121f902014-06-03 13:32:54 -07002730 if (status != NO_ERROR) {
2731 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002732 }
Eric Laurente1715a42014-05-20 11:30:42 -07002733
2734 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002735}
2736
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002737void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2738{
Eric Laurentd60560a2015-04-10 11:31:20 -07002739 clearAudioSources(uid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002740 clearAudioPatches(uid);
2741 clearSessionRoutes(uid);
2742}
2743
Eric Laurent6a94d692014-05-20 11:18:06 -07002744void AudioPolicyManager::clearAudioPatches(uid_t uid)
2745{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002746 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002747 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2748 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002749 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002750 }
2751 }
2752}
2753
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002754void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2755 audio_io_handle_t ouptutToSkip)
2756{
2757 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2758 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2759 for (size_t j = 0; j < mOutputs.size(); j++) {
2760 if (mOutputs.keyAt(j) == ouptutToSkip) {
2761 continue;
2762 }
2763 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2764 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2765 continue;
2766 }
2767 // If the default device for this strategy is on another output mix,
2768 // invalidate all tracks in this strategy to force re connection.
2769 // Otherwise select new device on the output mix.
2770 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2771 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2772 if (stream == AUDIO_STREAM_PATCH) {
2773 continue;
2774 }
2775 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2776 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2777 }
2778 }
2779 } else {
2780 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2781 setOutputDevice(outputDesc, newDevice, false);
2782 }
2783 }
2784}
2785
2786void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2787{
2788 // remove output routes associated with this uid
2789 SortedVector<routing_strategy> affectedStrategies;
2790 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2791 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2792 if (route->mUid == uid) {
2793 mOutputRoutes.removeItemsAt(i);
2794 if (route->mDeviceDescriptor != 0) {
2795 affectedStrategies.add(getStrategy(route->mStreamType));
2796 }
2797 }
2798 }
2799 // reroute outputs if necessary
2800 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2801 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2802 }
2803
2804 // remove input routes associated with this uid
2805 SortedVector<audio_source_t> affectedSources;
2806 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2807 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2808 if (route->mUid == uid) {
2809 mInputRoutes.removeItemsAt(i);
2810 if (route->mDeviceDescriptor != 0) {
2811 affectedSources.add(route->mSource);
2812 }
2813 }
2814 }
2815 // reroute inputs if necessary
2816 SortedVector<audio_io_handle_t> inputsToClose;
2817 for (size_t i = 0; i < mInputs.size(); i++) {
2818 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
Eric Laurent599c7582015-12-07 18:05:55 -08002819 if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002820 inputsToClose.add(inputDesc->mIoHandle);
2821 }
2822 }
2823 for (size_t i = 0; i < inputsToClose.size(); i++) {
2824 closeInput(inputsToClose[i]);
2825 }
2826}
2827
Eric Laurentd60560a2015-04-10 11:31:20 -07002828void AudioPolicyManager::clearAudioSources(uid_t uid)
2829{
2830 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
2831 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
2832 if (sourceDesc->mUid == uid) {
2833 stopAudioSource(mAudioSources.keyAt(i));
2834 }
2835 }
2836}
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002837
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002838status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2839 audio_io_handle_t *ioHandle,
2840 audio_devices_t *device)
2841{
2842 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2843 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002844 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002845
François Gaffiedf372692015-03-19 10:43:27 +01002846 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002847}
2848
Eric Laurentd60560a2015-04-10 11:31:20 -07002849status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2850 const audio_attributes_t *attributes,
2851 audio_io_handle_t *handle,
2852 uid_t uid)
Eric Laurent554a2772015-04-10 11:29:24 -07002853{
Eric Laurentd60560a2015-04-10 11:31:20 -07002854 ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle);
2855 if (source == NULL || attributes == NULL || handle == NULL) {
2856 return BAD_VALUE;
2857 }
2858
2859 *handle = AUDIO_IO_HANDLE_NONE;
2860
2861 if (source->role != AUDIO_PORT_ROLE_SOURCE ||
2862 source->type != AUDIO_PORT_TYPE_DEVICE) {
2863 ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type);
2864 return INVALID_OPERATION;
2865 }
2866
2867 sp<DeviceDescriptor> srcDeviceDesc =
2868 mAvailableInputDevices.getDevice(source->ext.device.type,
2869 String8(source->ext.device.address));
2870 if (srcDeviceDesc == 0) {
2871 ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type);
2872 return BAD_VALUE;
2873 }
2874 sp<AudioSourceDescriptor> sourceDesc =
2875 new AudioSourceDescriptor(srcDeviceDesc, attributes, uid);
2876
2877 struct audio_patch dummyPatch;
2878 sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid);
2879 sourceDesc->mPatchDesc = patchDesc;
2880
2881 status_t status = connectAudioSource(sourceDesc);
2882 if (status == NO_ERROR) {
2883 mAudioSources.add(sourceDesc->getHandle(), sourceDesc);
2884 *handle = sourceDesc->getHandle();
2885 }
2886 return status;
2887}
2888
2889status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
2890{
2891 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
2892
2893 // make sure we only have one patch per source.
2894 disconnectAudioSource(sourceDesc);
2895
2896 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
2897 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
2898 sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice;
2899
2900 audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true);
2901 sp<DeviceDescriptor> sinkDeviceDesc =
2902 mAvailableOutputDevices.getDevice(sinkDevice, String8(""));
2903
2904 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2905 struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch;
2906
2907 if (srcDeviceDesc->getAudioPort()->mModule->getHandle() ==
2908 sinkDeviceDesc->getAudioPort()->mModule->getHandle() &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01002909 srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 &&
Eric Laurentd60560a2015-04-10 11:31:20 -07002910 srcDeviceDesc->getAudioPort()->mGains.size() > 0) {
2911 ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__);
2912 // create patch between src device and output device
2913 // create Hwoutput and add to mHwOutputs
2914 } else {
2915 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs);
2916 audio_io_handle_t output =
2917 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID);
2918 if (output == AUDIO_IO_HANDLE_NONE) {
2919 ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice);
2920 return INVALID_OPERATION;
2921 }
2922 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2923 if (outputDesc->isDuplicated()) {
2924 ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice);
2925 return INVALID_OPERATION;
2926 }
2927 // create a special patch with no sink and two sources:
2928 // - the second source indicates to PatchPanel through which output mix this patch should
2929 // be connected as well as the stream type for volume control
2930 // - the sink is defined by whatever output device is currently selected for the output
2931 // though which this patch is routed.
2932 patch->num_sinks = 0;
2933 patch->num_sources = 2;
2934 srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL);
2935 outputDesc->toAudioPortConfig(&patch->sources[1], NULL);
2936 patch->sources[1].ext.mix.usecase.stream = stream;
2937 status_t status = mpClientInterface->createAudioPatch(patch,
2938 &afPatchHandle,
2939 0);
2940 ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__,
2941 status, afPatchHandle);
2942 if (status != NO_ERROR) {
2943 ALOGW("%s patch panel could not connect device patch, error %d",
2944 __FUNCTION__, status);
2945 return INVALID_OPERATION;
2946 }
2947 uint32_t delayMs = 0;
2948 status = startSource(outputDesc, stream, sinkDevice, &delayMs);
2949
2950 if (status != NO_ERROR) {
2951 mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0);
2952 return status;
2953 }
2954 sourceDesc->mSwOutput = outputDesc;
2955 if (delayMs != 0) {
2956 usleep(delayMs * 1000);
2957 }
2958 }
2959
2960 sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle;
2961 addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc);
2962
2963 return NO_ERROR;
Eric Laurent554a2772015-04-10 11:29:24 -07002964}
2965
Eric Laurent493404d2015-04-21 15:07:36 -07002966status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002967{
Eric Laurentd60560a2015-04-10 11:31:20 -07002968 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle);
2969 ALOGV("%s handle %d", __FUNCTION__, handle);
2970 if (sourceDesc == 0) {
2971 ALOGW("%s unknown source for handle %d", __FUNCTION__, handle);
2972 return BAD_VALUE;
2973 }
2974 status_t status = disconnectAudioSource(sourceDesc);
2975
2976 mAudioSources.removeItem(handle);
2977 return status;
2978}
2979
Andy Hung2ddee192015-12-18 17:34:44 -08002980status_t AudioPolicyManager::setMasterMono(bool mono)
2981{
2982 if (mMasterMono == mono) {
2983 return NO_ERROR;
2984 }
2985 mMasterMono = mono;
2986 // if enabling mono we close all offloaded devices, which will invalidate the
2987 // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible
2988 // for recreating the new AudioTrack as non-offloaded PCM.
2989 //
2990 // If disabling mono, we leave all tracks as is: we don't know which clients
2991 // and tracks are able to be recreated as offloaded. The next "song" should
2992 // play back offloaded.
2993 if (mMasterMono) {
2994 Vector<audio_io_handle_t> offloaded;
2995 for (size_t i = 0; i < mOutputs.size(); ++i) {
2996 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
2997 if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2998 offloaded.push(desc->mIoHandle);
2999 }
3000 }
3001 for (size_t i = 0; i < offloaded.size(); ++i) {
3002 closeOutput(offloaded[i]);
3003 }
3004 }
3005 // update master mono for all remaining outputs
3006 for (size_t i = 0; i < mOutputs.size(); ++i) {
3007 updateMono(mOutputs.keyAt(i));
3008 }
3009 return NO_ERROR;
3010}
3011
3012status_t AudioPolicyManager::getMasterMono(bool *mono)
3013{
3014 *mono = mMasterMono;
3015 return NO_ERROR;
3016}
3017
Eric Laurentd60560a2015-04-10 11:31:20 -07003018status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc)
3019{
3020 ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle());
3021
3022 sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle);
3023 if (patchDesc == 0) {
3024 ALOGW("%s source has no patch with handle %d", __FUNCTION__,
3025 sourceDesc->mPatchDesc->mHandle);
3026 return BAD_VALUE;
3027 }
3028 removeAudioPatch(sourceDesc->mPatchDesc->mHandle);
3029
3030 audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes);
3031 sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote();
3032 if (swOutputDesc != 0) {
3033 stopSource(swOutputDesc, stream, false);
3034 mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3035 } else {
3036 sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote();
3037 if (hwOutputDesc != 0) {
3038 // release patch between src device and output device
3039 // close Hwoutput and remove from mHwOutputs
3040 } else {
3041 ALOGW("%s source has neither SW nor HW output", __FUNCTION__);
3042 }
3043 }
3044 return NO_ERROR;
3045}
3046
3047sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput(
3048 audio_io_handle_t output, routing_strategy strategy)
3049{
3050 sp<AudioSourceDescriptor> source;
3051 for (size_t i = 0; i < mAudioSources.size(); i++) {
3052 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
3053 routing_strategy sourceStrategy =
3054 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes);
3055 sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote();
3056 if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) {
3057 source = sourceDesc;
3058 break;
3059 }
3060 }
3061 return source;
Eric Laurent554a2772015-04-10 11:29:24 -07003062}
3063
Eric Laurente552edb2014-03-10 17:42:56 -07003064// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07003065// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07003066// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07003067uint32_t AudioPolicyManager::nextAudioPortGeneration()
3068{
3069 return android_atomic_inc(&mAudioPortGeneration);
3070}
3071
Eric Laurente0720872014-03-11 09:30:41 -07003072AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07003073 :
3074#ifdef AUDIO_POLICY_TEST
3075 Thread(false),
3076#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07003077 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07003078 mA2dpSuspended(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003079 mAudioPortGeneration(1),
3080 mBeaconMuteRefCount(0),
3081 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07003082 mBeaconMuted(false),
Andy Hung2ddee192015-12-18 17:34:44 -08003083 mTtsOutputAvailable(false),
3084 mMasterMono(false)
Eric Laurente552edb2014-03-10 17:42:56 -07003085{
François Gaffied1ab2bd2015-12-02 18:20:06 +01003086 mUidCached = getuid();
3087 mpClientInterface = clientInterface;
3088
3089 // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
3090 // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
3091 // Note: remove also speaker_drc_enabled from global configuration of XML config file.
3092 bool speakerDrcEnabled = false;
3093
3094#ifdef USE_XML_AUDIO_POLICY_CONF
3095 mVolumeCurves = new VolumeCurvesCollection();
3096 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3097 mDefaultOutputDevice, speakerDrcEnabled,
3098 static_cast<VolumeCurvesCollection *>(mVolumeCurves));
3099 PolicySerializer serializer;
3100 if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) {
3101#else
3102 mVolumeCurves = new StreamDescriptorCollection();
3103 AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices,
3104 mDefaultOutputDevice, speakerDrcEnabled);
3105 if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) &&
3106 (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) {
3107#endif
3108 ALOGE("could not load audio policy configuration file, setting defaults");
3109 config.setDefault();
3110 }
3111 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
3112 mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled);
3113
3114 // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
François Gaffie2110e042015-03-24 08:41:51 +01003115 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
3116 if (!engineInstance) {
3117 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
3118 return;
3119 }
3120 // Retrieve the Policy Manager Interface
3121 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
3122 if (mEngine == NULL) {
3123 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
3124 return;
3125 }
3126 mEngine->setObserver(this);
3127 status_t status = mEngine->initCheck();
3128 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
3129
Eric Laurent3a4311c2014-03-17 12:00:47 -07003130 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07003131 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07003132 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
3133 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003134 for (size_t i = 0; i < mHwModules.size(); i++) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003135 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003136 if (mHwModules[i]->mHandle == 0) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003137 ALOGW("could not open HW module %s", mHwModules[i]->getName());
Eric Laurente552edb2014-03-10 17:42:56 -07003138 continue;
3139 }
3140 // open all output streams needed to access attached devices
3141 // except for direct output streams that are only opened when they are actually
3142 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07003143 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07003144 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3145 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003146 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003147
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003148 if (!outProfile->hasSupportedDevices()) {
3149 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003150 continue;
3151 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003152 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {
Eric Laurent9459fb02015-08-12 18:36:32 -07003153 mTtsOutputAvailable = true;
3154 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003155
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003156 if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
Eric Laurentd78f1532014-09-16 16:38:20 -07003157 continue;
3158 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003159 audio_devices_t profileType = outProfile->getSupportedDevicesType();
François Gaffie53615e22015-03-19 09:24:12 +01003160 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
3161 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07003162 } else {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003163 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003164 // outputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003165 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07003166 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003167 if ((profileType & outputDeviceTypes) == 0) {
3168 continue;
3169 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003170 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
3171 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07003172
3173 outputDesc->mDevice = profileType;
3174 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3175 config.sample_rate = outputDesc->mSamplingRate;
3176 config.channel_mask = outputDesc->mChannelMask;
3177 config.format = outputDesc->mFormat;
3178 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003179 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003180 &output,
3181 &config,
3182 &outputDesc->mDevice,
3183 String8(""),
3184 &outputDesc->mLatency,
3185 outputDesc->mFlags);
3186
3187 if (status != NO_ERROR) {
3188 ALOGW("Cannot open output stream for device %08x on hw module %s",
3189 outputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003190 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003191 } else {
3192 outputDesc->mSamplingRate = config.sample_rate;
3193 outputDesc->mChannelMask = config.channel_mask;
3194 outputDesc->mFormat = config.format;
3195
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003196 const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
3197 for (size_t k = 0; k < supportedDevices.size(); k++) {
3198 ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003199 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003200 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3201 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003202 }
3203 }
3204 if (mPrimaryOutput == 0 &&
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003205 outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003206 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07003207 }
3208 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003209 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07003210 outputDesc->mDevice,
3211 true);
3212 }
Eric Laurente552edb2014-03-10 17:42:56 -07003213 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003214 // open input streams needed to access attached devices to validate
3215 // mAvailableInputDevices list
3216 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3217 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003218 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003219
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003220 if (!inProfile->hasSupportedDevices()) {
3221 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003222 continue;
3223 }
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003224 // chose first device present in profile's SupportedDevices also part of
Eric Laurentd78f1532014-09-16 16:38:20 -07003225 // inputDeviceTypes
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003226 audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes);
3227
Eric Laurentd78f1532014-09-16 16:38:20 -07003228 if ((profileType & inputDeviceTypes) == 0) {
3229 continue;
3230 }
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08003231 sp<AudioInputDescriptor> inputDesc =
3232 new AudioInputDescriptor(inProfile);
Eric Laurentd78f1532014-09-16 16:38:20 -07003233
Eric Laurentd78f1532014-09-16 16:38:20 -07003234 inputDesc->mDevice = profileType;
3235
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003236 // find the address
3237 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3238 // the inputs vector must be of size 1, but we don't want to crash here
3239 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3240 : String8("");
3241 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3242 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3243
Eric Laurentd78f1532014-09-16 16:38:20 -07003244 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3245 config.sample_rate = inputDesc->mSamplingRate;
3246 config.channel_mask = inputDesc->mChannelMask;
3247 config.format = inputDesc->mFormat;
3248 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003249 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07003250 &input,
3251 &config,
3252 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003253 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003254 AUDIO_SOURCE_MIC,
3255 AUDIO_INPUT_FLAG_NONE);
3256
3257 if (status == NO_ERROR) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003258 const DeviceVector &supportedDevices = inProfile->getSupportedDevices();
3259 for (size_t k = 0; k < supportedDevices.size(); k++) {
3260 ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003261 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07003262 if (index >= 0) {
3263 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
3264 if (!devDesc->isAttached()) {
3265 devDesc->attach(mHwModules[i]);
3266 devDesc->importAudioPort(inProfile);
3267 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003268 }
3269 }
3270 mpClientInterface->closeInput(input);
3271 } else {
3272 ALOGW("Cannot open input stream for device %08x on hw module %s",
3273 inputDesc->mDevice,
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003274 mHwModules[i]->getName());
Eric Laurentd78f1532014-09-16 16:38:20 -07003275 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003276 }
3277 }
3278 // make sure all attached devices have been allocated a unique ID
3279 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003280 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003281 ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003282 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3283 continue;
3284 }
François Gaffie2110e042015-03-24 08:41:51 +01003285 // The device is now validated and can be appended to the available devices of the engine
3286 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
3287 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003288 i++;
3289 }
3290 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003291 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01003292 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003293 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3294 continue;
3295 }
François Gaffie2110e042015-03-24 08:41:51 +01003296 // The device is now validated and can be appended to the available devices of the engine
3297 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
3298 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003299 i++;
3300 }
3301 // make sure default device is reachable
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003302 if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01003303 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07003304 }
Eric Laurente552edb2014-03-10 17:42:56 -07003305
3306 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3307
3308 updateDevicesAndOutputs();
3309
3310#ifdef AUDIO_POLICY_TEST
3311 if (mPrimaryOutput != 0) {
3312 AudioParameter outputCmd = AudioParameter();
3313 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003314 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07003315
3316 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3317 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003318 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3319 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003320 mTestLatencyMs = 0;
3321 mCurOutput = 0;
3322 mDirectOutput = false;
3323 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3324 mTestOutputs[i] = 0;
3325 }
3326
3327 const size_t SIZE = 256;
3328 char buffer[SIZE];
3329 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3330 run(buffer, ANDROID_PRIORITY_AUDIO);
3331 }
3332#endif //AUDIO_POLICY_TEST
3333}
3334
Eric Laurente0720872014-03-11 09:30:41 -07003335AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003336{
3337#ifdef AUDIO_POLICY_TEST
3338 exit();
3339#endif //AUDIO_POLICY_TEST
3340 for (size_t i = 0; i < mOutputs.size(); i++) {
3341 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003342 }
3343 for (size_t i = 0; i < mInputs.size(); i++) {
3344 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003345 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003346 mAvailableOutputDevices.clear();
3347 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003348 mOutputs.clear();
3349 mInputs.clear();
3350 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003351}
3352
Eric Laurente0720872014-03-11 09:30:41 -07003353status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003354{
Eric Laurent87ffa392015-05-22 10:32:38 -07003355 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003356}
3357
3358#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003359bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003360{
3361 ALOGV("entering threadLoop()");
3362 while (!exitPending())
3363 {
3364 String8 command;
3365 int valueInt;
3366 String8 value;
3367
3368 Mutex::Autolock _l(mLock);
3369 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3370
3371 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3372 AudioParameter param = AudioParameter(command);
3373
3374 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3375 valueInt != 0) {
3376 ALOGV("Test command %s received", command.string());
3377 String8 target;
3378 if (param.get(String8("target"), target) != NO_ERROR) {
3379 target = "Manager";
3380 }
3381 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3382 param.remove(String8("test_cmd_policy_output"));
3383 mCurOutput = valueInt;
3384 }
3385 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3386 param.remove(String8("test_cmd_policy_direct"));
3387 if (value == "false") {
3388 mDirectOutput = false;
3389 } else if (value == "true") {
3390 mDirectOutput = true;
3391 }
3392 }
3393 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3394 param.remove(String8("test_cmd_policy_input"));
3395 mTestInput = valueInt;
3396 }
3397
3398 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3399 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003400 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003401 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003402 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003403 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003404 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003405 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003406 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003407 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003408 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003409 if (target == "Manager") {
3410 mTestFormat = format;
3411 } else if (mTestOutputs[mCurOutput] != 0) {
3412 AudioParameter outputParam = AudioParameter();
3413 outputParam.addInt(String8("format"), format);
3414 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3415 }
3416 }
3417 }
3418 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3419 param.remove(String8("test_cmd_policy_channels"));
3420 int channels = 0;
3421
3422 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003423 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003424 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003425 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003426 }
3427 if (channels != 0) {
3428 if (target == "Manager") {
3429 mTestChannels = channels;
3430 } else if (mTestOutputs[mCurOutput] != 0) {
3431 AudioParameter outputParam = AudioParameter();
3432 outputParam.addInt(String8("channels"), channels);
3433 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3434 }
3435 }
3436 }
3437 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3438 param.remove(String8("test_cmd_policy_sampleRate"));
3439 if (valueInt >= 0 && valueInt <= 96000) {
3440 int samplingRate = valueInt;
3441 if (target == "Manager") {
3442 mTestSamplingRate = samplingRate;
3443 } else if (mTestOutputs[mCurOutput] != 0) {
3444 AudioParameter outputParam = AudioParameter();
3445 outputParam.addInt(String8("sampling_rate"), samplingRate);
3446 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3447 }
3448 }
3449 }
3450
3451 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3452 param.remove(String8("test_cmd_policy_reopen"));
3453
Eric Laurentc75307b2015-03-17 15:29:32 -07003454 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003455
Eric Laurentc75307b2015-03-17 15:29:32 -07003456 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003457
Eric Laurentc75307b2015-03-17 15:29:32 -07003458 removeOutput(mPrimaryOutput->mIoHandle);
3459 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3460 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003461 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003462 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3463 config.sample_rate = outputDesc->mSamplingRate;
3464 config.channel_mask = outputDesc->mChannelMask;
3465 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003466 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003467 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003468 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003469 &config,
3470 &outputDesc->mDevice,
3471 String8(""),
3472 &outputDesc->mLatency,
3473 outputDesc->mFlags);
3474 if (status != NO_ERROR) {
3475 ALOGE("Failed to reopen hardware output stream, "
3476 "samplingRate: %d, format %d, channels %d",
3477 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003478 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003479 outputDesc->mSamplingRate = config.sample_rate;
3480 outputDesc->mChannelMask = config.channel_mask;
3481 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003482 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003483 AudioParameter outputCmd = AudioParameter();
3484 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003485 mpClientInterface->setParameters(handle, outputCmd.toString());
3486 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003487 }
3488 }
3489
3490
3491 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3492 }
3493 }
3494 return false;
3495}
3496
Eric Laurente0720872014-03-11 09:30:41 -07003497void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003498{
3499 {
3500 AutoMutex _l(mLock);
3501 requestExit();
3502 mWaitWorkCV.signal();
3503 }
3504 requestExitAndWait();
3505}
3506
Eric Laurente0720872014-03-11 09:30:41 -07003507int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003508{
3509 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3510 if (output == mTestOutputs[i]) return i;
3511 }
3512 return 0;
3513}
3514#endif //AUDIO_POLICY_TEST
3515
3516// ---
3517
Eric Laurentc75307b2015-03-17 15:29:32 -07003518void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003519{
François Gaffie98cc1912015-03-18 17:52:40 +01003520 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003521 mOutputs.add(output, outputDesc);
Andy Hung2ddee192015-12-18 17:34:44 -08003522 updateMono(output); // update mono status when adding to output list
Eric Laurent6a94d692014-05-20 11:18:06 -07003523 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003524}
3525
François Gaffie53615e22015-03-19 09:24:12 +01003526void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3527{
3528 mOutputs.removeItem(output);
3529}
3530
Eric Laurent1f2f2232014-06-02 12:01:23 -07003531void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003532{
François Gaffie98cc1912015-03-18 17:52:40 +01003533 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003534 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003535 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003536}
Eric Laurente552edb2014-03-10 17:42:56 -07003537
Eric Laurentc75307b2015-03-17 15:29:32 -07003538void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003539 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003540 const String8 address /*in*/,
3541 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003542 sp<DeviceDescriptor> devDesc =
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003543 desc->mProfile->getSupportedDeviceByAddress(device, address);
keunyoung3190e672014-12-30 13:00:52 -08003544 if (devDesc != 0) {
3545 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3546 desc->mIoHandle, address.string());
3547 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003548 }
3549}
3550
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003551status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003552 audio_policy_dev_state_t state,
3553 SortedVector<audio_io_handle_t>& outputs,
3554 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003555{
François Gaffie53615e22015-03-19 09:24:12 +01003556 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003557 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003558
3559 if (audio_device_is_digital(device)) {
3560 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003561 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003562 }
Eric Laurente552edb2014-03-10 17:42:56 -07003563
Eric Laurent3b73df72014-03-11 09:06:29 -07003564 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003565 // first list already open outputs that can be routed to this device
3566 for (size_t i = 0; i < mOutputs.size(); i++) {
3567 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003568 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003569 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003570 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3571 outputs.add(mOutputs.keyAt(i));
3572 } else {
3573 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003574 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003575 }
Eric Laurente552edb2014-03-10 17:42:56 -07003576 }
3577 }
3578 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003579 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003580 for (size_t i = 0; i < mHwModules.size(); i++)
3581 {
3582 if (mHwModules[i]->mHandle == 0) {
3583 continue;
3584 }
3585 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3586 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003587 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003588 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003589 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003590 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003591 profiles.add(profile);
3592 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3593 }
Eric Laurente552edb2014-03-10 17:42:56 -07003594 }
3595 }
3596 }
3597
Eric Laurent7b279bb2015-12-14 10:18:23 -08003598 ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size());
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003599
Eric Laurente552edb2014-03-10 17:42:56 -07003600 if (profiles.isEmpty() && outputs.isEmpty()) {
3601 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3602 return BAD_VALUE;
3603 }
3604
3605 // open outputs for matching profiles if needed. Direct outputs are also opened to
3606 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3607 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003608 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003609
3610 // nothing to do if one output is already opened for this profile
3611 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003612 for (j = 0; j < outputs.size(); j++) {
3613 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003614 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003615 // matching profile: save the sample rates, format and channel masks supported
3616 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003617 if (audio_device_is_digital(device)) {
3618 devDesc->importAudioPort(profile);
3619 }
Eric Laurente552edb2014-03-10 17:42:56 -07003620 break;
3621 }
3622 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003623 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003624 continue;
3625 }
3626
Eric Laurent83b88082014-06-20 18:31:16 -07003627 ALOGV("opening output for device %08x with params %s profile %p",
3628 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003629 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003630 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003631 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3632 config.sample_rate = desc->mSamplingRate;
3633 config.channel_mask = desc->mChannelMask;
3634 config.format = desc->mFormat;
3635 config.offload_info.sample_rate = desc->mSamplingRate;
3636 config.offload_info.channel_mask = desc->mChannelMask;
3637 config.offload_info.format = desc->mFormat;
3638 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003639 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003640 &output,
3641 &config,
3642 &desc->mDevice,
3643 address,
3644 &desc->mLatency,
3645 desc->mFlags);
3646 if (status == NO_ERROR) {
3647 desc->mSamplingRate = config.sample_rate;
3648 desc->mChannelMask = config.channel_mask;
3649 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003650
Eric Laurentd4692962014-05-05 18:13:44 -07003651 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003652 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003653 char *param = audio_device_address_to_parameter(device, address);
3654 mpClientInterface->setParameters(output, String8(param));
3655 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003656 }
François Gaffie112b0af2015-11-19 16:13:25 +01003657 updateAudioProfiles(output, profile->getAudioProfiles());
3658 if (!profile->hasValidAudioProfile()) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003659 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003660 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003661 output = AUDIO_IO_HANDLE_NONE;
François Gaffie112b0af2015-11-19 16:13:25 +01003662 } else if (profile->hasDynamicAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003663 mpClientInterface->closeOutput(output);
François Gaffie112b0af2015-11-19 16:13:25 +01003664 profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003665 config.offload_info.sample_rate = config.sample_rate;
3666 config.offload_info.channel_mask = config.channel_mask;
3667 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003668 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003669 &output,
3670 &config,
3671 &desc->mDevice,
3672 address,
3673 &desc->mLatency,
3674 desc->mFlags);
3675 if (status == NO_ERROR) {
3676 desc->mSamplingRate = config.sample_rate;
3677 desc->mChannelMask = config.channel_mask;
3678 desc->mFormat = config.format;
3679 } else {
3680 output = AUDIO_IO_HANDLE_NONE;
3681 }
Eric Laurentd4692962014-05-05 18:13:44 -07003682 }
3683
Eric Laurentcf2c0212014-07-25 16:20:43 -07003684 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003685 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003686 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003687 sp<AudioPolicyMix> policyMix;
3688 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003689 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3690 address.string());
3691 }
François Gaffie036e1e92015-03-19 10:16:24 +01003692 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003693 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003694
Eric Laurent87ffa392015-05-22 10:32:38 -07003695 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3696 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003697 // no duplicated output for direct outputs and
3698 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003699 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003700
Eric Laurentd4692962014-05-05 18:13:44 -07003701 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003702 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003703
Eric Laurentd4692962014-05-05 18:13:44 -07003704 //TODO: configure audio effect output stage here
3705
3706 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003707 duplicatedOutput =
3708 mpClientInterface->openDuplicateOutput(output,
3709 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003710 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003711 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003712 sp<SwAudioOutputDescriptor> dupOutputDesc =
3713 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3714 dupOutputDesc->mOutput1 = mPrimaryOutput;
3715 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003716 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3717 dupOutputDesc->mFormat = desc->mFormat;
3718 dupOutputDesc->mChannelMask = desc->mChannelMask;
3719 dupOutputDesc->mLatency = desc->mLatency;
3720 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003721 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003722 } else {
3723 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003724 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003725 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003726 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003727 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003728 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003729 }
Eric Laurente552edb2014-03-10 17:42:56 -07003730 }
3731 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003732 } else {
3733 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003734 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003735 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003736 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003737 profiles.removeAt(profile_index);
3738 profile_index--;
3739 } else {
3740 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003741 // Load digital format info only for digital devices
3742 if (audio_device_is_digital(device)) {
3743 devDesc->importAudioPort(profile);
3744 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003745
François Gaffie53615e22015-03-19 09:24:12 +01003746 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003747 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3748 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003749 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003750 NULL/*patch handle*/, address.string());
3751 }
Eric Laurente552edb2014-03-10 17:42:56 -07003752 ALOGV("checkOutputsForDevice(): adding output %d", output);
3753 }
3754 }
3755
3756 if (profiles.isEmpty()) {
3757 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3758 return BAD_VALUE;
3759 }
Eric Laurentd4692962014-05-05 18:13:44 -07003760 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003761 // check if one opened output is not needed any more after disconnecting one device
3762 for (size_t i = 0; i < mOutputs.size(); i++) {
3763 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003764 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003765 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003766 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003767 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003768 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003769 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003770 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3771 mOutputs.keyAt(i));
3772 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003773 }
Eric Laurente552edb2014-03-10 17:42:56 -07003774 }
3775 }
Eric Laurentd4692962014-05-05 18:13:44 -07003776 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003777 for (size_t i = 0; i < mHwModules.size(); i++)
3778 {
3779 if (mHwModules[i]->mHandle == 0) {
3780 continue;
3781 }
3782 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3783 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003784 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003785 if (profile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003786 ALOGV("checkOutputsForDevice(): "
3787 "clearing direct output profile %zu on module %zu", j, i);
François Gaffie112b0af2015-11-19 16:13:25 +01003788 profile->clearAudioProfiles();
Eric Laurente552edb2014-03-10 17:42:56 -07003789 }
3790 }
3791 }
3792 }
3793 return NO_ERROR;
3794}
3795
Paul McLean9080a4c2015-06-18 08:24:02 -07003796status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003797 audio_policy_dev_state_t state,
3798 SortedVector<audio_io_handle_t>& inputs,
3799 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003800{
Paul McLean9080a4c2015-06-18 08:24:02 -07003801 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003802 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003803
3804 if (audio_device_is_digital(device)) {
3805 // erase all current sample rates, formats and channel masks
Eric Laurent20eb3a42016-01-26 18:39:17 -08003806 devDesc->clearAudioProfiles();
Eric Laurentcc750d32015-06-25 11:48:20 -07003807 }
3808
Eric Laurentd4692962014-05-05 18:13:44 -07003809 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3810 // first list already open inputs that can be routed to this device
3811 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3812 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003813 if (desc->mProfile->supportDevice(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003814 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3815 inputs.add(mInputs.keyAt(input_index));
3816 }
3817 }
3818
3819 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003820 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003821 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3822 {
3823 if (mHwModules[module_idx]->mHandle == 0) {
3824 continue;
3825 }
3826 for (size_t profile_index = 0;
3827 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3828 profile_index++)
3829 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003830 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3831
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003832 if (profile->supportDevice(device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003833 if (!device_distinguishes_on_address(device) ||
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003834 profile->supportDeviceAddress(address)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003835 profiles.add(profile);
3836 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3837 profile_index, module_idx);
3838 }
Eric Laurentd4692962014-05-05 18:13:44 -07003839 }
3840 }
3841 }
3842
3843 if (profiles.isEmpty() && inputs.isEmpty()) {
3844 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3845 return BAD_VALUE;
3846 }
3847
3848 // open inputs for matching profiles if needed. Direct inputs are also opened to
3849 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3850 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3851
Eric Laurent1c333e22014-05-20 10:48:17 -07003852 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003853 // nothing to do if one input is already opened for this profile
3854 size_t input_index;
3855 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3856 desc = mInputs.valueAt(input_index);
3857 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003858 if (audio_device_is_digital(device)) {
3859 devDesc->importAudioPort(profile);
3860 }
Eric Laurentd4692962014-05-05 18:13:44 -07003861 break;
3862 }
3863 }
3864 if (input_index != mInputs.size()) {
3865 continue;
3866 }
3867
3868 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3869 desc = new AudioInputDescriptor(profile);
3870 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003871 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3872 config.sample_rate = desc->mSamplingRate;
3873 config.channel_mask = desc->mChannelMask;
3874 config.format = desc->mFormat;
3875 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003876 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003877 &input,
3878 &config,
3879 &desc->mDevice,
3880 address,
3881 AUDIO_SOURCE_MIC,
3882 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003883
Eric Laurentcf2c0212014-07-25 16:20:43 -07003884 if (status == NO_ERROR) {
3885 desc->mSamplingRate = config.sample_rate;
3886 desc->mChannelMask = config.channel_mask;
3887 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003888
Eric Laurentd4692962014-05-05 18:13:44 -07003889 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003890 char *param = audio_device_address_to_parameter(device, address);
3891 mpClientInterface->setParameters(input, String8(param));
3892 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003893 }
François Gaffie112b0af2015-11-19 16:13:25 +01003894 updateAudioProfiles(input, profile->getAudioProfiles());
3895 if (!profile->hasValidAudioProfile()) {
Eric Laurentd4692962014-05-05 18:13:44 -07003896 ALOGW("checkInputsForDevice() direct input missing param");
3897 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003898 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003899 }
3900
3901 if (input != 0) {
3902 addInput(input, desc);
3903 }
3904 } // endif input != 0
3905
Eric Laurentcf2c0212014-07-25 16:20:43 -07003906 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003907 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003908 profiles.removeAt(profile_index);
3909 profile_index--;
3910 } else {
3911 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003912 if (audio_device_is_digital(device)) {
3913 devDesc->importAudioPort(profile);
3914 }
Eric Laurentd4692962014-05-05 18:13:44 -07003915 ALOGV("checkInputsForDevice(): adding input %d", input);
3916 }
3917 } // end scan profiles
3918
3919 if (profiles.isEmpty()) {
3920 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3921 return BAD_VALUE;
3922 }
3923 } else {
3924 // Disconnect
3925 // check if one opened input is not needed any more after disconnecting one device
3926 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3927 desc = mInputs.valueAt(input_index);
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003928 if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) {
Eric Laurentd4692962014-05-05 18:13:44 -07003929 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3930 mInputs.keyAt(input_index));
3931 inputs.add(mInputs.keyAt(input_index));
3932 }
3933 }
3934 // Clear any profiles associated with the disconnected device.
3935 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3936 if (mHwModules[module_index]->mHandle == 0) {
3937 continue;
3938 }
3939 for (size_t profile_index = 0;
3940 profile_index < mHwModules[module_index]->mInputProfiles.size();
3941 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003942 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
François Gaffiea8ecc2c2015-11-09 16:10:40 +01003943 if (profile->supportDevice(device)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003944 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003945 profile_index, module_index);
François Gaffie112b0af2015-11-19 16:13:25 +01003946 profile->clearAudioProfiles();
Eric Laurentd4692962014-05-05 18:13:44 -07003947 }
3948 }
3949 }
3950 } // end disconnect
3951
3952 return NO_ERROR;
3953}
3954
3955
Eric Laurente0720872014-03-11 09:30:41 -07003956void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003957{
3958 ALOGV("closeOutput(%d)", output);
3959
Eric Laurentc75307b2015-03-17 15:29:32 -07003960 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003961 if (outputDesc == NULL) {
3962 ALOGW("closeOutput() unknown output %d", output);
3963 return;
3964 }
François Gaffie036e1e92015-03-19 10:16:24 +01003965 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003966
Eric Laurente552edb2014-03-10 17:42:56 -07003967 // look for duplicated outputs connected to the output being removed.
3968 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003969 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003970 if (dupOutputDesc->isDuplicated() &&
3971 (dupOutputDesc->mOutput1 == outputDesc ||
3972 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003973 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003974 if (dupOutputDesc->mOutput1 == outputDesc) {
3975 outputDesc2 = dupOutputDesc->mOutput2;
3976 } else {
3977 outputDesc2 = dupOutputDesc->mOutput1;
3978 }
3979 // As all active tracks on duplicated output will be deleted,
3980 // and as they were also referenced on the other output, the reference
3981 // count for their stream type must be adjusted accordingly on
3982 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003983 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003984 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003985 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003986 }
3987 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3988 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3989
3990 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003991 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003992 }
3993 }
3994
Eric Laurent05b90f82014-08-27 15:32:29 -07003995 nextAudioPortGeneration();
3996
3997 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3998 if (index >= 0) {
3999 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4000 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4001 mAudioPatches.removeItemsAt(index);
4002 mpClientInterface->onAudioPatchListUpdate();
4003 }
4004
Eric Laurente552edb2014-03-10 17:42:56 -07004005 AudioParameter param;
4006 param.add(String8("closing"), String8("true"));
4007 mpClientInterface->setParameters(output, param.toString());
4008
4009 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01004010 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004011 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07004012}
4013
4014void AudioPolicyManager::closeInput(audio_io_handle_t input)
4015{
4016 ALOGV("closeInput(%d)", input);
4017
4018 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
4019 if (inputDesc == NULL) {
4020 ALOGW("closeInput() unknown input %d", input);
4021 return;
4022 }
4023
Eric Laurent6a94d692014-05-20 11:18:06 -07004024 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07004025
4026 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4027 if (index >= 0) {
4028 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4029 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
4030 mAudioPatches.removeItemsAt(index);
4031 mpClientInterface->onAudioPatchListUpdate();
4032 }
4033
4034 mpClientInterface->closeInput(input);
4035 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07004036}
4037
Eric Laurentc75307b2015-03-17 15:29:32 -07004038SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
4039 audio_devices_t device,
4040 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07004041{
4042 SortedVector<audio_io_handle_t> outputs;
4043
4044 ALOGVV("getOutputsForDevice() device %04x", device);
4045 for (size_t i = 0; i < openOutputs.size(); i++) {
4046 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004047 i, openOutputs.valueAt(i)->isDuplicated(),
4048 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004049 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
4050 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
4051 outputs.add(openOutputs.keyAt(i));
4052 }
4053 }
4054 return outputs;
4055}
4056
Eric Laurente0720872014-03-11 09:30:41 -07004057bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01004058 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07004059{
4060 if (outputs1.size() != outputs2.size()) {
4061 return false;
4062 }
4063 for (size_t i = 0; i < outputs1.size(); i++) {
4064 if (outputs1[i] != outputs2[i]) {
4065 return false;
4066 }
4067 }
4068 return true;
4069}
4070
Eric Laurente0720872014-03-11 09:30:41 -07004071void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07004072{
4073 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
4074 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
4075 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
4076 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
4077
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004078 // also take into account external policy-related changes: add all outputs which are
4079 // associated with policies in the "before" and "after" output vectors
4080 ALOGVV("checkOutputForStrategy(): policy related outputs");
4081 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004082 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004083 if (desc != 0 && desc->mPolicyMix != NULL) {
4084 srcOutputs.add(desc->mIoHandle);
4085 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
4086 }
4087 }
4088 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004089 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08004090 if (desc != 0 && desc->mPolicyMix != NULL) {
4091 dstOutputs.add(desc->mIoHandle);
4092 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
4093 }
4094 }
4095
Eric Laurente552edb2014-03-10 17:42:56 -07004096 if (!vectorsEqual(srcOutputs,dstOutputs)) {
4097 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
4098 strategy, srcOutputs[0], dstOutputs[0]);
4099 // mute strategy while moving tracks from one output to another
4100 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004101 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004102 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004103 setStrategyMute(strategy, true, desc);
4104 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004105 }
Eric Laurentd60560a2015-04-10 11:31:20 -07004106 sp<AudioSourceDescriptor> source =
4107 getSourceForStrategyOnOutput(srcOutputs[i], strategy);
4108 if (source != 0){
4109 connectAudioSource(source);
4110 }
Eric Laurente552edb2014-03-10 17:42:56 -07004111 }
4112
4113 // Move effects associated to this strategy from previous output to new output
4114 if (strategy == STRATEGY_MEDIA) {
4115 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
4116 SortedVector<audio_io_handle_t> moved;
4117 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004118 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
4119 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
4120 effectDesc->mIo != fxOutput) {
4121 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004122 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
4123 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004124 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07004125 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004126 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004127 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004128 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004129 }
4130 }
4131 }
4132 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07004133 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004134 if (i == AUDIO_STREAM_PATCH) {
4135 continue;
4136 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004137 if (getStrategy((audio_stream_type_t)i) == strategy) {
4138 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004139 }
4140 }
4141 }
4142}
4143
Eric Laurente0720872014-03-11 09:30:41 -07004144void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004145{
François Gaffie2110e042015-03-24 08:41:51 +01004146 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004147 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004148 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01004149 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05004150 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004151 checkOutputForStrategy(STRATEGY_SONIFICATION);
4152 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004153 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004154 checkOutputForStrategy(STRATEGY_MEDIA);
4155 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004156 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004157}
4158
Eric Laurente0720872014-03-11 09:30:41 -07004159void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004160{
François Gaffie53615e22015-03-19 09:24:12 +01004161 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07004162 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004163 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004164 return;
4165 }
4166
Eric Laurent3a4311c2014-03-17 12:00:47 -07004167 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004168 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4169 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4170 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004171 // suspend A2DP output if:
4172 // (NOT already suspended) &&
4173 // ((SCO device is connected &&
4174 // (forced usage for communication || for record is SCO))) ||
4175 // (phone state is ringing || in call)
4176 //
4177 // restore A2DP output if:
4178 // (Already suspended) &&
4179 // ((SCO device is NOT connected ||
4180 // (forced usage NOT for communication && NOT for record is SCO))) &&
4181 // (phone state is NOT ringing && NOT in call)
4182 //
4183 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004184 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01004185 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
4186 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
4187 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
4188 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004189
4190 mpClientInterface->restoreOutput(a2dpOutput);
4191 mA2dpSuspended = false;
4192 }
4193 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004194 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01004195 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
4196 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
4197 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
4198 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004199
4200 mpClientInterface->suspendOutput(a2dpOutput);
4201 mA2dpSuspended = true;
4202 }
4203 }
4204}
4205
Eric Laurentc75307b2015-03-17 15:29:32 -07004206audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
4207 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004208{
4209 audio_devices_t device = AUDIO_DEVICE_NONE;
4210
Eric Laurent6a94d692014-05-20 11:18:06 -07004211 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4212 if (index >= 0) {
4213 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4214 if (patchDesc->mUid != mUidCached) {
4215 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
4216 outputDesc->device(), outputDesc->mPatchHandle);
4217 return outputDesc->device();
4218 }
4219 }
4220
Eric Laurente552edb2014-03-10 17:42:56 -07004221 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004222 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004223 // use device for strategy enforced audible
4224 // 2: we are in call or the strategy phone is active on the output:
4225 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004226 // 3: the strategy for enforced audible is active but not enforced on the output:
4227 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004228 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004229 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004230 // 5: the strategy sonification is active on the output:
4231 // use device for strategy sonification
4232 // 6: the strategy "respectful" sonification is active on the output:
4233 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004234 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004235 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004236 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004237 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004238 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004239 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004240 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004241 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004242 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4243 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004244 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004245 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004246 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004247 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07004248 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
4249 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004250 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004251 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004252 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004253 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004254 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004255 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004256 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004257 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004258 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004259 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004260 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004261 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 }
4263
Eric Laurent1c333e22014-05-20 10:48:17 -07004264 ALOGV("getNewOutputDevice() selected device %x", device);
4265 return device;
4266}
4267
Eric Laurentfb66dd92016-01-28 18:32:03 -08004268audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc)
Eric Laurent1c333e22014-05-20 10:48:17 -07004269{
Eric Laurentfb66dd92016-01-28 18:32:03 -08004270 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurent6a94d692014-05-20 11:18:06 -07004271
4272 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4273 if (index >= 0) {
4274 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4275 if (patchDesc->mUid != mUidCached) {
4276 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4277 inputDesc->mDevice, inputDesc->mPatchHandle);
4278 return inputDesc->mDevice;
4279 }
4280 }
4281
Eric Laurentfb66dd92016-01-28 18:32:03 -08004282 audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/);
4283 if (isInCall()) {
4284 device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4285 } else if (source != AUDIO_SOURCE_DEFAULT) {
4286 device = getDeviceAndMixForInputSource(source);
4287 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004288
Eric Laurente552edb2014-03-10 17:42:56 -07004289 return device;
4290}
4291
Eric Laurente0720872014-03-11 09:30:41 -07004292uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004293 return (uint32_t)getStrategy(stream);
4294}
4295
Eric Laurente0720872014-03-11 09:30:41 -07004296audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004297 // By checking the range of stream before calling getStrategy, we avoid
4298 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4299 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004300 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004301 return AUDIO_DEVICE_NONE;
4302 }
4303 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004304 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004305 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4306 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4307 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004308 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004309 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004310 devices = outputDesc->device();
4311 break;
4312 }
Eric Laurente552edb2014-03-10 17:42:56 -07004313 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004314
4315 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4316 and doesn't really need to.*/
4317 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4318 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4319 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4320 }
Eric Laurente552edb2014-03-10 17:42:56 -07004321 return devices;
4322}
4323
François Gaffie2110e042015-03-24 08:41:51 +01004324routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4325{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004326 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004327 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004328}
4329
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004330uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4331 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004332 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4333 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4334 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004335 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4336 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4337 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004338 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004339 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004340}
4341
Eric Laurente0720872014-03-11 09:30:41 -07004342void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004343 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004344 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004345 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4346 updateDevicesAndOutputs();
4347 break;
4348 default:
4349 break;
4350 }
4351}
4352
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004353uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004354
4355 // skip beacon mute management if a dedicated TTS output is available
4356 if (mTtsOutputAvailable) {
4357 return 0;
4358 }
4359
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004360 switch(event) {
4361 case STARTING_OUTPUT:
4362 mBeaconMuteRefCount++;
4363 break;
4364 case STOPPING_OUTPUT:
4365 if (mBeaconMuteRefCount > 0) {
4366 mBeaconMuteRefCount--;
4367 }
4368 break;
4369 case STARTING_BEACON:
4370 mBeaconPlayingRefCount++;
4371 break;
4372 case STOPPING_BEACON:
4373 if (mBeaconPlayingRefCount > 0) {
4374 mBeaconPlayingRefCount--;
4375 }
4376 break;
4377 }
4378
4379 if (mBeaconMuteRefCount > 0) {
4380 // any playback causes beacon to be muted
4381 return setBeaconMute(true);
4382 } else {
4383 // no other playback: unmute when beacon starts playing, mute when it stops
4384 return setBeaconMute(mBeaconPlayingRefCount == 0);
4385 }
4386}
4387
4388uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4389 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4390 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4391 // keep track of muted state to avoid repeating mute/unmute operations
4392 if (mBeaconMuted != mute) {
4393 // mute/unmute AUDIO_STREAM_TTS on all outputs
4394 ALOGV("\t muting %d", mute);
4395 uint32_t maxLatency = 0;
4396 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004397 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004398 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004399 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004400 0 /*delay*/, AUDIO_DEVICE_NONE);
4401 const uint32_t latency = desc->latency() * 2;
4402 if (latency > maxLatency) {
4403 maxLatency = latency;
4404 }
4405 }
4406 mBeaconMuted = mute;
4407 return maxLatency;
4408 }
4409 return 0;
4410}
4411
Eric Laurente0720872014-03-11 09:30:41 -07004412audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004413 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004414{
Paul McLeanaa981192015-03-21 09:55:15 -07004415 // Routing
4416 // see if we have an explicit route
4417 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4418 // (getStrategy(stream)).
4419 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4420 // and activity count is non-zero
4421 // the device = the device from the descriptor in the RouteMap, and exit.
4422 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4423 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4424 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004425 // Special case for accessibility strategy which must follow any strategy it is
4426 // currently remapped to
4427 bool strategyMatch = (strat == strategy) ||
4428 ((strategy == STRATEGY_ACCESSIBILITY) &&
4429 ((mEngine->getStrategyForUsage(
4430 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4431 (strat == STRATEGY_MEDIA)));
4432 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004433 return route->mDeviceDescriptor->type();
4434 }
4435 }
4436
Eric Laurente552edb2014-03-10 17:42:56 -07004437 if (fromCache) {
4438 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4439 strategy, mDeviceForStrategy[strategy]);
4440 return mDeviceForStrategy[strategy];
4441 }
François Gaffie2110e042015-03-24 08:41:51 +01004442 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004443}
4444
Eric Laurente0720872014-03-11 09:30:41 -07004445void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004446{
4447 for (int i = 0; i < NUM_STRATEGIES; i++) {
4448 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4449 }
4450 mPreviousOutputs = mOutputs;
4451}
4452
Eric Laurent1f2f2232014-06-02 12:01:23 -07004453uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004454 audio_devices_t prevDevice,
4455 uint32_t delayMs)
4456{
4457 // mute/unmute strategies using an incompatible device combination
4458 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4459 // if unmuting, unmute only after the specified delay
4460 if (outputDesc->isDuplicated()) {
4461 return 0;
4462 }
4463
4464 uint32_t muteWaitMs = 0;
4465 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004466 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004467
4468 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4469 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004470 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004471 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4472 bool doMute = false;
4473
4474 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4475 doMute = true;
4476 outputDesc->mStrategyMutedByDevice[i] = true;
4477 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4478 doMute = true;
4479 outputDesc->mStrategyMutedByDevice[i] = false;
4480 }
Eric Laurent99401132014-05-07 19:48:15 -07004481 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004482 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004483 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004484 // skip output if it does not share any device with current output
4485 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4486 == AUDIO_DEVICE_NONE) {
4487 continue;
4488 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004489 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4490 mute ? "muting" : "unmuting", i, curDevice);
4491 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004492 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004493 if (mute) {
4494 // FIXME: should not need to double latency if volume could be applied
4495 // immediately by the audioflinger mixer. We must account for the delay
4496 // between now and the next time the audioflinger thread for this output
4497 // will process a buffer (which corresponds to one buffer size,
4498 // usually 1/2 or 1/4 of the latency).
4499 if (muteWaitMs < desc->latency() * 2) {
4500 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004501 }
4502 }
4503 }
4504 }
4505 }
4506 }
4507
Eric Laurent99401132014-05-07 19:48:15 -07004508 // temporary mute output if device selection changes to avoid volume bursts due to
4509 // different per device volumes
4510 if (outputDesc->isActive() && (device != prevDevice)) {
4511 if (muteWaitMs < outputDesc->latency() * 2) {
4512 muteWaitMs = outputDesc->latency() * 2;
4513 }
4514 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004515 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004516 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004517 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004518 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004519 muteWaitMs *2, device);
4520 }
4521 }
4522 }
4523
Eric Laurente552edb2014-03-10 17:42:56 -07004524 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4525 if (muteWaitMs > delayMs) {
4526 muteWaitMs -= delayMs;
4527 usleep(muteWaitMs * 1000);
4528 return muteWaitMs;
4529 }
4530 return 0;
4531}
4532
Eric Laurentc75307b2015-03-17 15:29:32 -07004533uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004534 audio_devices_t device,
4535 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004536 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004537 audio_patch_handle_t *patchHandle,
4538 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004539{
Eric Laurentc75307b2015-03-17 15:29:32 -07004540 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004541 AudioParameter param;
4542 uint32_t muteWaitMs;
4543
4544 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004545 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4546 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004547 return muteWaitMs;
4548 }
4549 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4550 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004551 if ((device != AUDIO_DEVICE_NONE) &&
4552 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004553 return 0;
4554 }
4555
4556 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004557 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004558
4559 audio_devices_t prevDevice = outputDesc->mDevice;
4560
Paul McLeanaa981192015-03-21 09:55:15 -07004561 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004562
4563 if (device != AUDIO_DEVICE_NONE) {
4564 outputDesc->mDevice = device;
4565 }
4566 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4567
4568 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004569 // the requested device is AUDIO_DEVICE_NONE
4570 // OR the requested device is the same as current device
4571 // AND force is not specified
4572 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004573 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004574 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4575 !force &&
4576 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004577 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004578 return muteWaitMs;
4579 }
4580
4581 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004582
Eric Laurente552edb2014-03-10 17:42:56 -07004583 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004584 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004585 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004586 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004587 DeviceVector deviceList = (address == NULL) ?
4588 mAvailableOutputDevices.getDevicesFromType(device)
4589 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004590 if (!deviceList.isEmpty()) {
4591 struct audio_patch patch;
4592 outputDesc->toAudioPortConfig(&patch.sources[0]);
4593 patch.num_sources = 1;
4594 patch.num_sinks = 0;
4595 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4596 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004597 patch.num_sinks++;
4598 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004599 ssize_t index;
4600 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4601 index = mAudioPatches.indexOfKey(*patchHandle);
4602 } else {
4603 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4604 }
4605 sp< AudioPatch> patchDesc;
4606 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4607 if (index >= 0) {
4608 patchDesc = mAudioPatches.valueAt(index);
4609 afPatchHandle = patchDesc->mAfPatchHandle;
4610 }
4611
Eric Laurent1c333e22014-05-20 10:48:17 -07004612 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004613 &afPatchHandle,
4614 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004615 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4616 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004617 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004618 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004619 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004620 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004621 addAudioPatch(patchDesc->mHandle, patchDesc);
4622 } else {
4623 patchDesc->mPatch = patch;
4624 }
4625 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004626 if (patchHandle) {
4627 *patchHandle = patchDesc->mHandle;
4628 }
4629 outputDesc->mPatchHandle = patchDesc->mHandle;
4630 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004631 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004632 }
4633 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004634
4635 // inform all input as well
4636 for (size_t i = 0; i < mInputs.size(); i++) {
4637 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004638 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004639 AudioParameter inputCmd = AudioParameter();
4640 ALOGV("%s: inform input %d of device:%d", __func__,
4641 inputDescriptor->mIoHandle, device);
4642 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4643 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4644 inputCmd.toString(),
4645 delayMs);
4646 }
4647 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004648 }
Eric Laurente552edb2014-03-10 17:42:56 -07004649
4650 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004651 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004652
4653 return muteWaitMs;
4654}
4655
Eric Laurentc75307b2015-03-17 15:29:32 -07004656status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004657 int delayMs,
4658 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004659{
Eric Laurent6a94d692014-05-20 11:18:06 -07004660 ssize_t index;
4661 if (patchHandle) {
4662 index = mAudioPatches.indexOfKey(*patchHandle);
4663 } else {
4664 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4665 }
4666 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004667 return INVALID_OPERATION;
4668 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004669 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4670 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004671 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4672 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004673 removeAudioPatch(patchDesc->mHandle);
4674 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004675 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004676 return status;
4677}
4678
4679status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4680 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004681 bool force,
4682 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004683{
4684 status_t status = NO_ERROR;
4685
Eric Laurent1f2f2232014-06-02 12:01:23 -07004686 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004687 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4688 inputDesc->mDevice = device;
4689
4690 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4691 if (!deviceList.isEmpty()) {
4692 struct audio_patch patch;
4693 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004694 // AUDIO_SOURCE_HOTWORD is for internal use only:
4695 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004696 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
Eric Laurent599c7582015-12-07 18:05:55 -08004697 !inputDesc->isSoundTrigger()) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004698 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4699 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004700 patch.num_sinks = 1;
4701 //only one input device for now
4702 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004703 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004704 ssize_t index;
4705 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4706 index = mAudioPatches.indexOfKey(*patchHandle);
4707 } else {
4708 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4709 }
4710 sp< AudioPatch> patchDesc;
4711 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4712 if (index >= 0) {
4713 patchDesc = mAudioPatches.valueAt(index);
4714 afPatchHandle = patchDesc->mAfPatchHandle;
4715 }
4716
Eric Laurent1c333e22014-05-20 10:48:17 -07004717 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004718 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004719 0);
4720 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004721 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004722 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004723 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004724 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004725 addAudioPatch(patchDesc->mHandle, patchDesc);
4726 } else {
4727 patchDesc->mPatch = patch;
4728 }
4729 patchDesc->mAfPatchHandle = afPatchHandle;
Eric Laurent6a94d692014-05-20 11:18:06 -07004730 if (patchHandle) {
4731 *patchHandle = patchDesc->mHandle;
4732 }
4733 inputDesc->mPatchHandle = patchDesc->mHandle;
4734 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004735 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004736 }
4737 }
4738 }
4739 return status;
4740}
4741
Eric Laurent6a94d692014-05-20 11:18:06 -07004742status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4743 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004744{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004745 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004746 ssize_t index;
4747 if (patchHandle) {
4748 index = mAudioPatches.indexOfKey(*patchHandle);
4749 } else {
4750 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4751 }
4752 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004753 return INVALID_OPERATION;
4754 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004755 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4756 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004757 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4758 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004759 removeAudioPatch(patchDesc->mHandle);
4760 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004761 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004762 return status;
4763}
4764
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004765sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004766 String8 address,
4767 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004768 audio_format_t& format,
4769 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004770 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004771{
4772 // Choose an input profile based on the requested capture parameters: select the first available
4773 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004774 //
4775 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4776 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004777
4778 for (size_t i = 0; i < mHwModules.size(); i++)
4779 {
4780 if (mHwModules[i]->mHandle == 0) {
4781 continue;
4782 }
4783 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4784 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004785 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004786 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004787 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004788 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004789 format,
4790 &format /*updatedFormat*/,
4791 channelMask,
4792 &channelMask /*updatedChannelMask*/,
4793 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004794
Eric Laurente552edb2014-03-10 17:42:56 -07004795 return profile;
4796 }
4797 }
4798 }
4799 return NULL;
4800}
4801
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004802
4803audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004804 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004805{
François Gaffie036e1e92015-03-19 10:16:24 +01004806 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4807 audio_devices_t selectedDeviceFromMix =
4808 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004809
François Gaffie036e1e92015-03-19 10:16:24 +01004810 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4811 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004812 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004813 return getDeviceForInputSource(inputSource);
4814}
4815
4816audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4817{
Paul McLean466dc8e2015-04-17 13:15:36 -06004818 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4819 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004820 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004821 return route->mDeviceDescriptor->type();
4822 }
4823 }
4824
4825 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004826}
4827
Eric Laurente0720872014-03-11 09:30:41 -07004828float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004829 int index,
4830 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004831{
François Gaffied1ab2bd2015-12-02 18:20:06 +01004832 float volumeDb = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index);
Eric Laurente552edb2014-03-10 17:42:56 -07004833 // if a headset is connected, apply the following rules to ring tones and notifications
4834 // to avoid sound level bursts in user's ears:
4835 // - always attenuate ring tones and notifications volume by 6dB
4836 // - if music is playing, always limit the volume to current music volume,
4837 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004838 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004839 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4840 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4841 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4842 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4843 ((stream_strategy == STRATEGY_SONIFICATION)
4844 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004845 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004846 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004847 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
François Gaffied1ab2bd2015-12-02 18:20:06 +01004848 mVolumeCurves->canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004849 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004850 // when the phone is ringing we must consider that music could have been paused just before
4851 // by the music application and behave as if music was active if the last music track was
4852 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004853 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004854 mLimitRingtoneVolume) {
4855 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004856 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004857 mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC,
4858 musicDevice),
4859 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004860 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4861 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4862 if (volumeDb > minVolDB) {
4863 volumeDb = minVolDB;
4864 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004865 }
4866 }
4867 }
4868
Eric Laurentffbc80f2015-03-18 18:30:19 -07004869 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004870}
4871
Eric Laurente0720872014-03-11 09:30:41 -07004872status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004873 int index,
4874 const sp<AudioOutputDescriptor>& outputDesc,
4875 audio_devices_t device,
4876 int delayMs,
4877 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004878{
Eric Laurente552edb2014-03-10 17:42:56 -07004879 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004880 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004881 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004882 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004883 return NO_ERROR;
4884 }
François Gaffie2110e042015-03-24 08:41:51 +01004885 audio_policy_forced_cfg_t forceUseForComm =
4886 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004887 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004888 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4889 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004890 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004891 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004892 return INVALID_OPERATION;
4893 }
4894
Eric Laurentc75307b2015-03-17 15:29:32 -07004895 if (device == AUDIO_DEVICE_NONE) {
4896 device = outputDesc->device();
4897 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004898
Eric Laurentffbc80f2015-03-18 18:30:19 -07004899 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004900 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004901 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004902 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004903
Eric Laurentffbc80f2015-03-18 18:30:19 -07004904 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004905
Eric Laurent3b73df72014-03-11 09:06:29 -07004906 if (stream == AUDIO_STREAM_VOICE_CALL ||
4907 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004908 float voiceVolume;
4909 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004910 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004911 voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004912 } else {
4913 voiceVolume = 1.0;
4914 }
4915
Eric Laurentc75307b2015-03-17 15:29:32 -07004916 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004917 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4918 mLastVoiceVolume = voiceVolume;
4919 }
4920 }
4921
4922 return NO_ERROR;
4923}
4924
Eric Laurentc75307b2015-03-17 15:29:32 -07004925void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4926 audio_devices_t device,
4927 int delayMs,
4928 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004929{
Eric Laurentc75307b2015-03-17 15:29:32 -07004930 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004931
Eric Laurent3b73df72014-03-11 09:06:29 -07004932 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004933 if (stream == AUDIO_STREAM_PATCH) {
4934 continue;
4935 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004936 checkAndSetVolume((audio_stream_type_t)stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004937 mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004938 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004939 device,
4940 delayMs,
4941 force);
4942 }
4943}
4944
Eric Laurente0720872014-03-11 09:30:41 -07004945void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004946 bool on,
4947 const sp<AudioOutputDescriptor>& outputDesc,
4948 int delayMs,
4949 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004950{
Eric Laurent72249d52015-06-08 11:12:15 -07004951 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4952 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004953 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004954 if (stream == AUDIO_STREAM_PATCH) {
4955 continue;
4956 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004957 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004958 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004959 }
4960 }
4961}
4962
Eric Laurente0720872014-03-11 09:30:41 -07004963void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004964 bool on,
4965 const sp<AudioOutputDescriptor>& outputDesc,
4966 int delayMs,
4967 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004968{
Eric Laurente552edb2014-03-10 17:42:56 -07004969 if (device == AUDIO_DEVICE_NONE) {
4970 device = outputDesc->device();
4971 }
4972
Eric Laurentc75307b2015-03-17 15:29:32 -07004973 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4974 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004975
4976 if (on) {
4977 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffied1ab2bd2015-12-02 18:20:06 +01004978 if (mVolumeCurves->canBeMuted(stream) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004979 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004980 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004981 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004982 }
4983 }
4984 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4985 outputDesc->mMuteCount[stream]++;
4986 } else {
4987 if (outputDesc->mMuteCount[stream] == 0) {
4988 ALOGV("setStreamMute() unmuting non muted stream!");
4989 return;
4990 }
4991 if (--outputDesc->mMuteCount[stream] == 0) {
4992 checkAndSetVolume(stream,
François Gaffied1ab2bd2015-12-02 18:20:06 +01004993 mVolumeCurves->getVolumeIndex(stream, device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004994 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004995 device,
4996 delayMs);
4997 }
4998 }
4999}
5000
Eric Laurente0720872014-03-11 09:30:41 -07005001void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005002 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005003{
Eric Laurent87ffa392015-05-22 10:32:38 -07005004 if(!hasPrimaryOutput()) {
5005 return;
5006 }
5007
Eric Laurente552edb2014-03-10 17:42:56 -07005008 // if the stream pertains to sonification strategy and we are in call we must
5009 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5010 // in the device used for phone strategy and play the tone if the selected device does not
5011 // interfere with the device used for phone strategy
5012 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5013 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005014 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005015 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5016 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07005017 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07005018 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5019 stream, starting, outputDesc->mDevice, stateChange);
5020 if (outputDesc->mRefCount[stream]) {
5021 int muteCount = 1;
5022 if (stateChange) {
5023 muteCount = outputDesc->mRefCount[stream];
5024 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005025 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005026 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5027 for (int i = 0; i < muteCount; i++) {
5028 setStreamMute(stream, starting, mPrimaryOutput);
5029 }
5030 } else {
5031 ALOGV("handleIncallSonification() high visibility");
5032 if (outputDesc->device() &
5033 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5034 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5035 for (int i = 0; i < muteCount; i++) {
5036 setStreamMute(stream, starting, mPrimaryOutput);
5037 }
5038 }
5039 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005040 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5041 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005042 } else {
5043 mpClientInterface->stopTone();
5044 }
5045 }
5046 }
5047 }
5048}
5049
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005050audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5051{
5052 // flags to stream type mapping
5053 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5054 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5055 }
5056 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5057 return AUDIO_STREAM_BLUETOOTH_SCO;
5058 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005059 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5060 return AUDIO_STREAM_TTS;
5061 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005062
5063 // usage to stream type mapping
5064 switch (attr->usage) {
5065 case AUDIO_USAGE_MEDIA:
5066 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005067 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5068 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005069 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005070 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5071 return AUDIO_STREAM_ALARM;
5072 }
5073 if (isStreamActive(AUDIO_STREAM_RING)) {
5074 return AUDIO_STREAM_RING;
5075 }
5076 if (isInCall()) {
5077 return AUDIO_STREAM_VOICE_CALL;
5078 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005079 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005080 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5081 return AUDIO_STREAM_SYSTEM;
5082 case AUDIO_USAGE_VOICE_COMMUNICATION:
5083 return AUDIO_STREAM_VOICE_CALL;
5084
5085 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5086 return AUDIO_STREAM_DTMF;
5087
5088 case AUDIO_USAGE_ALARM:
5089 return AUDIO_STREAM_ALARM;
5090 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5091 return AUDIO_STREAM_RING;
5092
5093 case AUDIO_USAGE_NOTIFICATION:
5094 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5095 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5096 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5097 case AUDIO_USAGE_NOTIFICATION_EVENT:
5098 return AUDIO_STREAM_NOTIFICATION;
5099
5100 case AUDIO_USAGE_UNKNOWN:
5101 default:
5102 return AUDIO_STREAM_MUSIC;
5103 }
5104}
Eric Laurente83b55d2014-11-14 10:06:21 -08005105
François Gaffie53615e22015-03-19 09:24:12 +01005106bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5107{
Eric Laurente83b55d2014-11-14 10:06:21 -08005108 // has flags that map to a strategy?
5109 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5110 return true;
5111 }
5112
5113 // has known usage?
5114 switch (paa->usage) {
5115 case AUDIO_USAGE_UNKNOWN:
5116 case AUDIO_USAGE_MEDIA:
5117 case AUDIO_USAGE_VOICE_COMMUNICATION:
5118 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5119 case AUDIO_USAGE_ALARM:
5120 case AUDIO_USAGE_NOTIFICATION:
5121 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5122 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5123 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5124 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5125 case AUDIO_USAGE_NOTIFICATION_EVENT:
5126 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5127 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5128 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5129 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005130 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005131 break;
5132 default:
5133 return false;
5134 }
5135 return true;
5136}
5137
François Gaffiead3183e2015-03-18 16:55:35 +01005138bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5139 routing_strategy strategy, uint32_t inPastMs,
5140 nsecs_t sysTime) const
5141{
5142 if ((sysTime == 0) && (inPastMs != 0)) {
5143 sysTime = systemTime();
5144 }
5145 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5146 if (i == AUDIO_STREAM_PATCH) {
5147 continue;
5148 }
5149 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5150 (NUM_STRATEGIES == strategy)) &&
5151 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5152 return true;
5153 }
5154 }
5155 return false;
5156}
5157
François Gaffie2110e042015-03-24 08:41:51 +01005158audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
5159{
5160 return mEngine->getForceUse(usage);
5161}
5162
5163bool AudioPolicyManager::isInCall()
5164{
5165 return isStateInCall(mEngine->getPhoneState());
5166}
5167
5168bool AudioPolicyManager::isStateInCall(int state)
5169{
5170 return is_state_in_call(state);
5171}
5172
Eric Laurentd60560a2015-04-10 11:31:20 -07005173void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc)
5174{
5175 for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) {
5176 sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i);
5177 if (sourceDesc->mDevice->equals(deviceDesc)) {
5178 ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle());
5179 stopAudioSource(sourceDesc->getHandle());
5180 }
5181 }
5182
5183 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
5184 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
5185 bool release = false;
5186 for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) {
5187 const struct audio_port_config *source = &patchDesc->mPatch.sources[j];
5188 if (source->type == AUDIO_PORT_TYPE_DEVICE &&
5189 source->ext.device.type == deviceDesc->type()) {
5190 release = true;
5191 }
5192 }
5193 for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) {
5194 const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j];
5195 if (sink->type == AUDIO_PORT_TYPE_DEVICE &&
5196 sink->ext.device.type == deviceDesc->type()) {
5197 release = true;
5198 }
5199 }
5200 if (release) {
5201 ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle);
5202 releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid);
5203 }
5204 }
5205}
5206
François Gaffie112b0af2015-11-19 16:13:25 +01005207void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle,
5208 AudioProfileVector &profiles)
5209{
5210 String8 reply;
5211 char *value;
5212 // Format MUST be checked first to update the list of AudioProfile
5213 if (profiles.hasDynamicFormat()) {
5214 reply = mpClientInterface->getParameters(ioHandle,
5215 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
5216 ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005217 AudioParameter repliedParameters(reply);
5218 if (repliedParameters.get(
5219 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
François Gaffie112b0af2015-11-19 16:13:25 +01005220 ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__);
5221 return;
5222 }
Eric Laurent62e4bc52016-02-02 18:37:28 -08005223 profiles.setFormats(formatsFromString(reply.string()));
François Gaffie112b0af2015-11-19 16:13:25 +01005224 }
5225 const FormatVector &supportedFormats = profiles.getSupportedFormats();
5226
Eric Laurent20eb3a42016-01-26 18:39:17 -08005227 for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) {
François Gaffie112b0af2015-11-19 16:13:25 +01005228 audio_format_t format = supportedFormats[formatIndex];
Eric Laurent20eb3a42016-01-26 18:39:17 -08005229 ChannelsVector channelMasks;
5230 SampleRateVector samplingRates;
François Gaffie112b0af2015-11-19 16:13:25 +01005231 AudioParameter requestedParameters;
5232 requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format);
5233
5234 if (profiles.hasDynamicRateFor(format)) {
5235 reply = mpClientInterface->getParameters(ioHandle,
5236 requestedParameters.toString() + ";" +
5237 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES);
5238 ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005239 AudioParameter repliedParameters(reply);
5240 if (repliedParameters.get(
5241 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES), reply) == NO_ERROR) {
5242 samplingRates = samplingRatesFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005243 }
5244 }
5245 if (profiles.hasDynamicChannelsFor(format)) {
5246 reply = mpClientInterface->getParameters(ioHandle,
5247 requestedParameters.toString() + ";" +
5248 AUDIO_PARAMETER_STREAM_SUP_CHANNELS);
5249 ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string());
Eric Laurent62e4bc52016-02-02 18:37:28 -08005250 AudioParameter repliedParameters(reply);
5251 if (repliedParameters.get(
5252 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
5253 channelMasks = channelMasksFromString(reply.string());
François Gaffie112b0af2015-11-19 16:13:25 +01005254 }
5255 }
Eric Laurent20eb3a42016-01-26 18:39:17 -08005256 profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));
François Gaffie112b0af2015-11-19 16:13:25 +01005257 }
5258}
Eric Laurentd60560a2015-04-10 11:31:20 -07005259
Eric Laurente552edb2014-03-10 17:42:56 -07005260}; // namespace android