blob: 534155902ea79f9e03e394ba730226b15d49d709 [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
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
Eric Laurente552edb2014-03-10 17:42:56 -070030#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070031#include <utils/Log.h>
32#include <hardware/audio.h>
33#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070034#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080035#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070036#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070037#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070038#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010039#include <ConfigParsingUtils.h>
40#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070041
Eric Laurent3b73df72014-03-11 09:06:29 -070042namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070043
44// ----------------------------------------------------------------------------
45// AudioPolicyInterface implementation
46// ----------------------------------------------------------------------------
47
Eric Laurente0720872014-03-11 09:30:41 -070048status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080049 audio_policy_dev_state_t state,
50 const char *device_address,
51 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070052{
Paul McLeane743a472015-01-28 11:07:31 -080053 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080054}
55
56status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080057 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080058 const char *device_address,
59 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080060{
Paul McLeane743a472015-01-28 11:07:31 -080061 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
62- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070063
64 // connect/disconnect only 1 device at a time
65 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
66
François Gaffie53615e22015-03-19 09:24:12 +010067 sp<DeviceDescriptor> devDesc =
68 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080069
Eric Laurente552edb2014-03-10 17:42:56 -070070 // handle output devices
71 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070072 SortedVector <audio_io_handle_t> outputs;
73
Eric Laurent3a4311c2014-03-17 12:00:47 -070074 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
75
Eric Laurente552edb2014-03-10 17:42:56 -070076 // save a copy of the opened output descriptors before any output is opened or closed
77 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
78 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070079 switch (state)
80 {
81 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080082 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070083 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070084 ALOGW("setDeviceConnectionState() device already connected: %x", device);
85 return INVALID_OPERATION;
86 }
87 ALOGV("setDeviceConnectionState() connecting device %x", device);
88
Eric Laurente552edb2014-03-10 17:42:56 -070089 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070090 index = mAvailableOutputDevices.add(devDesc);
91 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010092 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070093 if (module == 0) {
94 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
95 device);
96 mAvailableOutputDevices.remove(devDesc);
97 return INVALID_OPERATION;
98 }
Paul McLeane743a472015-01-28 11:07:31 -080099 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700100 } else {
101 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700102 }
103
Eric Laurenta1d525f2015-01-29 13:36:45 -0800104 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700105 mAvailableOutputDevices.remove(devDesc);
106 return INVALID_OPERATION;
107 }
108 // outputs should never be empty here
109 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
110 "checkOutputsForDevice() returned no outputs but status OK");
111 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
112 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800113
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800114 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800115 AudioParameter param = AudioParameter(devDesc->mAddress);
116 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
117 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
118
119 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700120 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700121 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700122 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700123 ALOGW("setDeviceConnectionState() device not connected: %x", device);
124 return INVALID_OPERATION;
125 }
126
Paul McLean5c477aa2014-08-20 16:47:57 -0700127 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
128
Paul McLeane743a472015-01-28 11:07:31 -0800129 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800130 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700131 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
132 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
133
Eric Laurente552edb2014-03-10 17:42:56 -0700134 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700135 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700136
Eric Laurenta1d525f2015-01-29 13:36:45 -0800137 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
Eric Laurente552edb2014-03-10 17:42:56 -0700138 } break;
139
140 default:
141 ALOGE("setDeviceConnectionState() invalid state: %x", state);
142 return BAD_VALUE;
143 }
144
Eric Laurent3a4311c2014-03-17 12:00:47 -0700145 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
146 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700147 checkA2dpSuspend();
148 checkOutputForAllStrategies();
149 // outputs must be closed after checkOutputForAllStrategies() is executed
150 if (!outputs.isEmpty()) {
151 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700152 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700153 // close unused outputs after device disconnection or direct outputs that have been
154 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700155 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700156 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
157 (desc->mDirectOpenCount == 0))) {
158 closeOutput(outputs[i]);
159 }
160 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700161 // check again after closing A2DP output to reset mA2dpSuspended if needed
162 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700163 }
164
165 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700166 if (mPhoneState == AUDIO_MODE_IN_CALL) {
167 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
168 updateCallRouting(newDevice);
169 }
Eric Laurente552edb2014-03-10 17:42:56 -0700170 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700171 audio_io_handle_t output = mOutputs.keyAt(i);
172 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
173 audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i),
174 true /*fromCache*/);
175 // do not force device change on duplicated output because if device is 0, it will
176 // also force a device 0 for the two outputs it is duplicated to which may override
177 // a valid device selection on those outputs.
178 bool force = !mOutputs.valueAt(i)->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100179 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700180 // always force when disconnecting (a non-duplicated device)
181 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
182 setOutputDevice(output, newDevice, force, 0);
183 }
Eric Laurente552edb2014-03-10 17:42:56 -0700184 }
185
Eric Laurent72aa32f2014-05-30 18:51:48 -0700186 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700187 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700188 } // end if is output device
189
Eric Laurente552edb2014-03-10 17:42:56 -0700190 // handle input devices
191 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700192 SortedVector <audio_io_handle_t> inputs;
193
Eric Laurent3a4311c2014-03-17 12:00:47 -0700194 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700195 switch (state)
196 {
197 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700198 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700199 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700200 ALOGW("setDeviceConnectionState() device already connected: %d", device);
201 return INVALID_OPERATION;
202 }
François Gaffie53615e22015-03-19 09:24:12 +0100203 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700204 if (module == NULL) {
205 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
206 device);
207 return INVALID_OPERATION;
208 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800209 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700210 return INVALID_OPERATION;
211 }
212
Eric Laurent3a4311c2014-03-17 12:00:47 -0700213 index = mAvailableInputDevices.add(devDesc);
214 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800215 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700216 } else {
217 return NO_MEMORY;
218 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800219
220 // Set connect to HALs
221 AudioParameter param = AudioParameter(devDesc->mAddress);
222 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
223 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
224
Eric Laurentd4692962014-05-05 18:13:44 -0700225 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700226
227 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700228 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700229 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700230 ALOGW("setDeviceConnectionState() device not connected: %d", device);
231 return INVALID_OPERATION;
232 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700233
234 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
235
236 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800237 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700238 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
239 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
240
Eric Laurenta1d525f2015-01-29 13:36:45 -0800241 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700242 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700243
Eric Laurentd4692962014-05-05 18:13:44 -0700244 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700245
246 default:
247 ALOGE("setDeviceConnectionState() invalid state: %x", state);
248 return BAD_VALUE;
249 }
250
Eric Laurentd4692962014-05-05 18:13:44 -0700251 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700252
Eric Laurentc2730ba2014-07-20 15:47:07 -0700253 if (mPhoneState == AUDIO_MODE_IN_CALL) {
254 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
255 updateCallRouting(newDevice);
256 }
257
Eric Laurentb52c1522014-05-20 11:27:36 -0700258 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700259 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700260 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700261
262 ALOGW("setDeviceConnectionState() invalid device: %x", device);
263 return BAD_VALUE;
264}
265
Eric Laurente0720872014-03-11 09:30:41 -0700266audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100267 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700268{
François Gaffie53615e22015-03-19 09:24:12 +0100269 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
270
Eric Laurent3a4311c2014-03-17 12:00:47 -0700271 DeviceVector *deviceVector;
272
Eric Laurente552edb2014-03-10 17:42:56 -0700273 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700274 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700275 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700276 deviceVector = &mAvailableInputDevices;
277 } else {
278 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
279 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700280 }
François Gaffie53615e22015-03-19 09:24:12 +0100281 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800282}
283
Eric Laurentc2730ba2014-07-20 15:47:07 -0700284void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
285{
286 bool createTxPatch = false;
287 struct audio_patch patch;
288 patch.num_sources = 1;
289 patch.num_sinks = 1;
290 status_t status;
291 audio_patch_handle_t afPatchHandle;
292 DeviceVector deviceList;
293
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800294 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
296
297 // release existing RX patch if any
298 if (mCallRxPatch != 0) {
299 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
300 mCallRxPatch.clear();
301 }
302 // release TX patch if any
303 if (mCallTxPatch != 0) {
304 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
305 mCallTxPatch.clear();
306 }
307
308 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
309 // via setOutputDevice() on primary output.
310 // Otherwise, create two audio patches for TX and RX path.
311 if (availablePrimaryOutputDevices() & rxDevice) {
312 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
313 // If the TX device is also on the primary HW module, setOutputDevice() will take care
314 // of it due to legacy implementation. If not, create a patch.
315 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
316 == AUDIO_DEVICE_NONE) {
317 createTxPatch = true;
318 }
319 } else {
320 // create RX path audio patch
321 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
322 ALOG_ASSERT(!deviceList.isEmpty(),
323 "updateCallRouting() selected device not in output device list");
324 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
325 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
326 ALOG_ASSERT(!deviceList.isEmpty(),
327 "updateCallRouting() no telephony RX device");
328 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
329
330 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
331 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
332
333 // request to reuse existing output stream if one is already opened to reach the RX device
334 SortedVector<audio_io_handle_t> outputs =
335 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700336 audio_io_handle_t output = selectOutput(outputs,
337 AUDIO_OUTPUT_FLAG_NONE,
338 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700339 if (output != AUDIO_IO_HANDLE_NONE) {
340 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
341 ALOG_ASSERT(!outputDesc->isDuplicated(),
342 "updateCallRouting() RX device output is duplicated");
343 outputDesc->toAudioPortConfig(&patch.sources[1]);
344 patch.num_sources = 2;
345 }
346
347 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
348 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
349 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
350 status);
351 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100352 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 mCallRxPatch->mAfPatchHandle = afPatchHandle;
354 mCallRxPatch->mUid = mUidCached;
355 }
356 createTxPatch = true;
357 }
358 if (createTxPatch) {
359
360 struct audio_patch patch;
361 patch.num_sources = 1;
362 patch.num_sinks = 1;
363 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
364 ALOG_ASSERT(!deviceList.isEmpty(),
365 "updateCallRouting() selected device not in input device list");
366 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
367 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
368 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
369 ALOG_ASSERT(!deviceList.isEmpty(),
370 "updateCallRouting() no telephony TX device");
371 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
372 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
373
374 SortedVector<audio_io_handle_t> outputs =
375 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700376 audio_io_handle_t output = selectOutput(outputs,
377 AUDIO_OUTPUT_FLAG_NONE,
378 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700379 // request to reuse existing output stream if one is already opened to reach the TX
380 // path output device
381 if (output != AUDIO_IO_HANDLE_NONE) {
382 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
383 ALOG_ASSERT(!outputDesc->isDuplicated(),
384 "updateCallRouting() RX device output is duplicated");
385 outputDesc->toAudioPortConfig(&patch.sources[1]);
386 patch.num_sources = 2;
387 }
388
389 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
390 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
391 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
392 status);
393 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100394 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700395 mCallTxPatch->mAfPatchHandle = afPatchHandle;
396 mCallTxPatch->mUid = mUidCached;
397 }
398 }
399}
400
Eric Laurente0720872014-03-11 09:30:41 -0700401void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700402{
403 ALOGV("setPhoneState() state %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700404 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700405 ALOGW("setPhoneState() invalid state %d", state);
406 return;
407 }
408
409 if (state == mPhoneState ) {
410 ALOGW("setPhoneState() setting same state %d", state);
411 return;
412 }
413
414 // if leaving call state, handle special case of active streams
415 // pertaining to sonification strategy see handleIncallSonification()
416 if (isInCall()) {
417 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700418 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800419 if (stream == AUDIO_STREAM_PATCH) {
420 continue;
421 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700422 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700423 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800424
425 // force reevaluating accessibility routing when call starts
426 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700427 }
428
429 // store previous phone state for management of sonification strategy below
430 int oldState = mPhoneState;
431 mPhoneState = state;
432 bool force = false;
433
434 // are we entering or starting a call
435 if (!isStateInCall(oldState) && isStateInCall(state)) {
436 ALOGV(" Entering call in setPhoneState()");
437 // force routing command to audio hardware when starting a call
438 // even if no device change is needed
439 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800440 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700441 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800442 ApmGains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700443 }
444 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
445 ALOGV(" Exiting call in setPhoneState()");
446 // force routing command to audio hardware when exiting a call
447 // even if no device change is needed
448 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800449 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700450 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800451 ApmGains::sVolumeProfiles[AUDIO_STREAM_DTMF][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700452 }
453 } else if (isStateInCall(state) && (state != oldState)) {
454 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
455 // force routing command to audio hardware when switching between telephony and VoIP
456 // even if no device change is needed
457 force = true;
458 }
459
460 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700461 checkA2dpSuspend();
462 checkOutputForAllStrategies();
463 updateDevicesAndOutputs();
464
Eric Laurent1f2f2232014-06-02 12:01:23 -0700465 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700466
Eric Laurente552edb2014-03-10 17:42:56 -0700467 int delayMs = 0;
468 if (isStateInCall(state)) {
469 nsecs_t sysTime = systemTime();
470 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700471 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700472 // mute media and sonification strategies and delay device switch by the largest
473 // latency of any output where either strategy is active.
474 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100475 if ((isStrategyActive(desc, STRATEGY_MEDIA,
476 SONIFICATION_HEADSET_MUSIC_DELAY,
477 sysTime) ||
478 isStrategyActive(desc, STRATEGY_SONIFICATION,
479 SONIFICATION_HEADSET_MUSIC_DELAY,
480 sysTime)) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700481 (delayMs < (int)desc->mLatency*2)) {
482 delayMs = desc->mLatency*2;
483 }
484 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
485 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
486 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
487 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
488 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
489 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
490 }
491 }
492
Eric Laurentc2730ba2014-07-20 15:47:07 -0700493 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
494 // the device returned is not necessarily reachable via this output
495 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
496 // force routing command to audio hardware when ending call
497 // even if no device change is needed
498 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
499 rxDevice = hwOutputDesc->device();
500 }
Eric Laurente552edb2014-03-10 17:42:56 -0700501
Eric Laurentc2730ba2014-07-20 15:47:07 -0700502 if (state == AUDIO_MODE_IN_CALL) {
503 updateCallRouting(rxDevice, delayMs);
504 } else if (oldState == AUDIO_MODE_IN_CALL) {
505 if (mCallRxPatch != 0) {
506 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
507 mCallRxPatch.clear();
508 }
509 if (mCallTxPatch != 0) {
510 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
511 mCallTxPatch.clear();
512 }
513 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
514 } else {
515 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
516 }
Eric Laurente552edb2014-03-10 17:42:56 -0700517 // if entering in call state, handle special case of active streams
518 // pertaining to sonification strategy see handleIncallSonification()
519 if (isStateInCall(state)) {
520 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700521 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800522 if (stream == AUDIO_STREAM_PATCH) {
523 continue;
524 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700525 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700526 }
527 }
528
529 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 if (state == AUDIO_MODE_RINGTONE &&
531 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700532 mLimitRingtoneVolume = true;
533 } else {
534 mLimitRingtoneVolume = false;
535 }
536}
537
Eric Laurente0720872014-03-11 09:30:41 -0700538void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700539 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700540{
541 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
542
543 bool forceVolumeReeval = false;
544 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700545 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
546 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
547 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700548 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
549 return;
550 }
551 forceVolumeReeval = true;
552 mForceUse[usage] = config;
553 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700554 case AUDIO_POLICY_FORCE_FOR_MEDIA:
555 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
556 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
557 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
558 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
Hochi Huang327cb702014-09-21 09:47:31 +0800559 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Eric Laurente552edb2014-03-10 17:42:56 -0700560 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
561 return;
562 }
563 mForceUse[usage] = config;
564 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700565 case AUDIO_POLICY_FORCE_FOR_RECORD:
566 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
567 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700568 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
569 return;
570 }
571 mForceUse[usage] = config;
572 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700573 case AUDIO_POLICY_FORCE_FOR_DOCK:
574 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
575 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
576 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
577 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
578 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700579 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
580 }
581 forceVolumeReeval = true;
582 mForceUse[usage] = config;
583 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700584 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
585 if (config != AUDIO_POLICY_FORCE_NONE &&
586 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700587 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
588 }
589 forceVolumeReeval = true;
590 mForceUse[usage] = config;
591 break;
Jungshik Jang7b24ee32014-07-15 19:38:42 +0900592 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
593 if (config != AUDIO_POLICY_FORCE_NONE &&
594 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
595 ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config);
596 }
597 mForceUse[usage] = config;
598 break;
Eric Laurente552edb2014-03-10 17:42:56 -0700599 default:
600 ALOGW("setForceUse() invalid usage %d", usage);
601 break;
602 }
603
604 // check for device and output changes triggered by new force usage
605 checkA2dpSuspend();
606 checkOutputForAllStrategies();
607 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700608 if (mPhoneState == AUDIO_MODE_IN_CALL) {
609 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
610 updateCallRouting(newDevice);
611 }
Eric Laurente552edb2014-03-10 17:42:56 -0700612 for (size_t i = 0; i < mOutputs.size(); i++) {
613 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700614 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700615 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
616 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
617 }
Eric Laurente552edb2014-03-10 17:42:56 -0700618 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
619 applyStreamVolumes(output, newDevice, 0, true);
620 }
621 }
622
François Gaffie53615e22015-03-19 09:24:12 +0100623 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700624 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700625 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700626 }
627
628}
629
Eric Laurente0720872014-03-11 09:30:41 -0700630audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700631{
632 return mForceUse[usage];
633}
634
Eric Laurente0720872014-03-11 09:30:41 -0700635void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700636{
637 ALOGV("setSystemProperty() property %s, value %s", property, value);
638}
639
640// Find a direct output profile compatible with the parameters passed, even if the input flags do
641// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800642sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700643 audio_devices_t device,
644 uint32_t samplingRate,
645 audio_format_t format,
646 audio_channel_mask_t channelMask,
647 audio_output_flags_t flags)
648{
649 for (size_t i = 0; i < mHwModules.size(); i++) {
650 if (mHwModules[i]->mHandle == 0) {
651 continue;
652 }
653 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700654 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent275e8e92014-11-30 15:14:47 -0800655 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700656 NULL /*updatedSamplingRate*/, format, channelMask,
657 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
658 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700659 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
660 return profile;
661 }
Eric Laurente552edb2014-03-10 17:42:56 -0700662 }
663 }
664 return 0;
665}
666
Eric Laurente0720872014-03-11 09:30:41 -0700667audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100668 uint32_t samplingRate,
669 audio_format_t format,
670 audio_channel_mask_t channelMask,
671 audio_output_flags_t flags,
672 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700673{
Eric Laurent3b73df72014-03-11 09:06:29 -0700674 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700675 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
676 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
677 device, stream, samplingRate, format, channelMask, flags);
678
Eric Laurente83b55d2014-11-14 10:06:21 -0800679 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
680 stream, samplingRate,format, channelMask,
681 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700682}
683
Eric Laurente83b55d2014-11-14 10:06:21 -0800684status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
685 audio_io_handle_t *output,
686 audio_session_t session,
687 audio_stream_type_t *stream,
688 uint32_t samplingRate,
689 audio_format_t format,
690 audio_channel_mask_t channelMask,
691 audio_output_flags_t flags,
692 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700693{
Eric Laurente83b55d2014-11-14 10:06:21 -0800694 audio_attributes_t attributes;
695 if (attr != NULL) {
696 if (!isValidAttributes(attr)) {
697 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
698 attr->usage, attr->content_type, attr->flags,
699 attr->tags);
700 return BAD_VALUE;
701 }
702 attributes = *attr;
703 } else {
704 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
705 ALOGE("getOutputForAttr(): invalid stream type");
706 return BAD_VALUE;
707 }
708 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700709 }
François Gaffie036e1e92015-03-19 10:16:24 +0100710 sp<AudioOutputDescriptor> desc;
711 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
712 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
713 if (!audio_is_linear_pcm(format)) {
714 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800715 }
François Gaffie036e1e92015-03-19 10:16:24 +0100716 *stream = streamTypefromAttributesInt(&attributes);
717 *output = desc->mIoHandle;
718 ALOGV("getOutputForAttr() returns output %d", *output);
719 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800720 }
721 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
722 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
723 return BAD_VALUE;
724 }
725
Eric Laurent93c3d412014-08-01 14:48:35 -0700726 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
François Gaffie53615e22015-03-19 09:24:12 +0100727 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700728
Eric Laurente83b55d2014-11-14 10:06:21 -0800729 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700730 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700731
Eric Laurente83b55d2014-11-14 10:06:21 -0800732 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700733 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
734 }
735
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700736 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700737 device, samplingRate, format, channelMask, flags);
738
Eric Laurente83b55d2014-11-14 10:06:21 -0800739 *stream = streamTypefromAttributesInt(&attributes);
740 *output = getOutputForDevice(device, session, *stream,
741 samplingRate, format, channelMask,
742 flags, offloadInfo);
743 if (*output == AUDIO_IO_HANDLE_NONE) {
744 return INVALID_OPERATION;
745 }
746 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700747}
748
749audio_io_handle_t AudioPolicyManager::getOutputForDevice(
750 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800751 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700752 audio_stream_type_t stream,
753 uint32_t samplingRate,
754 audio_format_t format,
755 audio_channel_mask_t channelMask,
756 audio_output_flags_t flags,
757 const audio_offload_info_t *offloadInfo)
758{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700759 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700760 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700761 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700762
Eric Laurente552edb2014-03-10 17:42:56 -0700763#ifdef AUDIO_POLICY_TEST
764 if (mCurOutput != 0) {
765 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
766 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
767
768 if (mTestOutputs[mCurOutput] == 0) {
769 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700770 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700771 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700772 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700773 outputDesc->mFlags =
774 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700775 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700776 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
777 config.sample_rate = mTestSamplingRate;
778 config.channel_mask = mTestChannels;
779 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700780 if (offloadInfo != NULL) {
781 config.offload_info = *offloadInfo;
782 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700783 status = mpClientInterface->openOutput(0,
784 &mTestOutputs[mCurOutput],
785 &config,
786 &outputDesc->mDevice,
787 String8(""),
788 &outputDesc->mLatency,
789 outputDesc->mFlags);
790 if (status == NO_ERROR) {
791 outputDesc->mSamplingRate = config.sample_rate;
792 outputDesc->mFormat = config.format;
793 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700794 AudioParameter outputCmd = AudioParameter();
795 outputCmd.addInt(String8("set_id"),mCurOutput);
796 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
797 addOutput(mTestOutputs[mCurOutput], outputDesc);
798 }
799 }
800 return mTestOutputs[mCurOutput];
801 }
802#endif //AUDIO_POLICY_TEST
803
804 // open a direct output if required by specified parameters
805 //force direct flag if offload flag is set: offloading implies a direct output stream
806 // and all common behaviors are driven by checking only the direct flag
807 // this should normally be set appropriately in the policy configuration file
808 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700809 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700810 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700811 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
812 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
813 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800814 // only allow deep buffering for music stream type
815 if (stream != AUDIO_STREAM_MUSIC) {
816 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
817 }
Eric Laurente552edb2014-03-10 17:42:56 -0700818
Eric Laurentb732cf52014-09-24 19:08:21 -0700819 sp<IOProfile> profile;
820
821 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700822 // and not explicitly requested
823 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
824 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700825 audio_channel_count_from_out_mask(channelMask) <= 2) {
826 goto non_direct_output;
827 }
828
Eric Laurente552edb2014-03-10 17:42:56 -0700829 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
830 // creating an offloaded track and tearing it down immediately after start when audioflinger
831 // detects there is an active non offloadable effect.
832 // FIXME: We should check the audio session here but we do not have it in this context.
833 // This may prevent offloading in rare situations where effects are left active by apps
834 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700835
Eric Laurente552edb2014-03-10 17:42:56 -0700836 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100837 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700838 profile = getProfileForDirectOutput(device,
839 samplingRate,
840 format,
841 channelMask,
842 (audio_output_flags_t)flags);
843 }
844
Eric Laurent1c333e22014-05-20 10:48:17 -0700845 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700846 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700847
848 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700849 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700850 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
851 outputDesc = desc;
852 // reuse direct output if currently open and configured with same parameters
853 if ((samplingRate == outputDesc->mSamplingRate) &&
854 (format == outputDesc->mFormat) &&
855 (channelMask == outputDesc->mChannelMask)) {
856 outputDesc->mDirectOpenCount++;
857 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
858 return mOutputs.keyAt(i);
859 }
860 }
861 }
862 // close direct output if currently open and configured with different parameters
863 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700864 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700865 }
866 outputDesc = new AudioOutputDescriptor(profile);
867 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700868 outputDesc->mLatency = 0;
869 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700870 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
871 config.sample_rate = samplingRate;
872 config.channel_mask = channelMask;
873 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700874 if (offloadInfo != NULL) {
875 config.offload_info = *offloadInfo;
876 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700877 status = mpClientInterface->openOutput(profile->mModule->mHandle,
878 &output,
879 &config,
880 &outputDesc->mDevice,
881 String8(""),
882 &outputDesc->mLatency,
883 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700884
885 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700886 if (status != NO_ERROR ||
887 (samplingRate != 0 && samplingRate != config.sample_rate) ||
888 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
889 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700890 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
891 "format %d %d, channelMask %04x %04x", output, samplingRate,
892 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
893 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700894 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700895 mpClientInterface->closeOutput(output);
896 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800897 // fall back to mixer output if possible when the direct output could not be open
898 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
899 goto non_direct_output;
900 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800901 // fall back to mixer output if possible when the direct output could not be open
902 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
903 goto non_direct_output;
904 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700905 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700906 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700907 outputDesc->mSamplingRate = config.sample_rate;
908 outputDesc->mChannelMask = config.channel_mask;
909 outputDesc->mFormat = config.format;
910 outputDesc->mRefCount[stream] = 0;
911 outputDesc->mStopTime[stream] = 0;
912 outputDesc->mDirectOpenCount = 1;
913
Eric Laurente552edb2014-03-10 17:42:56 -0700914 audio_io_handle_t srcOutput = getOutputForEffect();
915 addOutput(output, outputDesc);
916 audio_io_handle_t dstOutput = getOutputForEffect();
917 if (dstOutput == output) {
918 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
919 }
920 mPreviousOutputs = mOutputs;
921 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700922 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700923 return output;
924 }
925
Eric Laurentb732cf52014-09-24 19:08:21 -0700926non_direct_output:
927
Eric Laurente552edb2014-03-10 17:42:56 -0700928 // ignoring channel mask due to downmix capability in mixer
929
930 // open a non direct output
931
932 // for non direct outputs, only PCM is supported
933 if (audio_is_linear_pcm(format)) {
934 // get which output is suitable for the specified stream. The actual
935 // routing change will happen when startOutput() will be called
936 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
937
Eric Laurent8838a382014-09-08 16:44:28 -0700938 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
939 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
940 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700941 }
942 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
943 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
944
945 ALOGV("getOutput() returns output %d", output);
946
947 return output;
948}
949
Eric Laurente0720872014-03-11 09:30:41 -0700950audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700951 audio_output_flags_t flags,
952 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700953{
954 // select one output among several that provide a path to a particular device or set of
955 // devices (the list was previously build by getOutputsForDevice()).
956 // The priority is as follows:
957 // 1: the output with the highest number of requested policy flags
958 // 2: the primary output
959 // 3: the first output in the list
960
961 if (outputs.size() == 0) {
962 return 0;
963 }
964 if (outputs.size() == 1) {
965 return outputs[0];
966 }
967
968 int maxCommonFlags = 0;
969 audio_io_handle_t outputFlags = 0;
970 audio_io_handle_t outputPrimary = 0;
971
972 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700973 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700974 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700975 // if a valid format is specified, skip output if not compatible
976 if (format != AUDIO_FORMAT_INVALID) {
977 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
978 if (format != outputDesc->mFormat) {
979 continue;
980 }
981 } else if (!audio_is_linear_pcm(format)) {
982 continue;
983 }
984 }
985
Eric Laurent3b73df72014-03-11 09:06:29 -0700986 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700987 if (commonFlags > maxCommonFlags) {
988 outputFlags = outputs[i];
989 maxCommonFlags = commonFlags;
990 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
991 }
992 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
993 outputPrimary = outputs[i];
994 }
995 }
996 }
997
998 if (outputFlags != 0) {
999 return outputFlags;
1000 }
1001 if (outputPrimary != 0) {
1002 return outputPrimary;
1003 }
1004
1005 return outputs[0];
1006}
1007
Eric Laurente0720872014-03-11 09:30:41 -07001008status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001009 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001010 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001011{
1012 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
1013 ssize_t index = mOutputs.indexOfKey(output);
1014 if (index < 0) {
1015 ALOGW("startOutput() unknown output %d", output);
1016 return BAD_VALUE;
1017 }
1018
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001019 // cannot start playback of STREAM_TTS if any other output is being used
1020 uint32_t beaconMuteLatency = 0;
1021 if (stream == AUDIO_STREAM_TTS) {
1022 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001023 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001024 return INVALID_OPERATION;
1025 } else {
1026 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1027 }
1028 } else {
1029 // some playback other than beacon starts
1030 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1031 }
1032
Eric Laurent1f2f2232014-06-02 12:01:23 -07001033 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001034
1035 // increment usage count for this stream on the requested output:
1036 // NOTE that the usage count is the same for duplicated output and hardware output which is
1037 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1038 outputDesc->changeRefCount(stream, 1);
1039
1040 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001041 // starting an output being rerouted?
1042 audio_devices_t newDevice;
Eric Laurentc722f302014-12-10 11:21:49 -08001043 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001044 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1045 } else {
1046 newDevice = getNewOutputDevice(output, false /*fromCache*/);
1047 }
Eric Laurente552edb2014-03-10 17:42:56 -07001048 routing_strategy strategy = getStrategy(stream);
1049 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001050 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1051 (beaconMuteLatency > 0);
1052 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001053 bool force = false;
1054 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001055 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001056 if (desc != outputDesc) {
1057 // force a device change if any other output is managed by the same hw
1058 // module and has a current device selection that differs from selected device.
1059 // In this case, the audio HAL must receive the new device selection so that it can
1060 // change the device currently selected by the other active output.
1061 if (outputDesc->sharesHwModuleWith(desc) &&
1062 desc->device() != newDevice) {
1063 force = true;
1064 }
1065 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001066 // a notification so that audio focus effect can propagate, or that a mute/unmute
1067 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001068 uint32_t latency = desc->latency();
1069 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1070 waitMs = latency;
1071 }
1072 }
1073 }
1074 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
1075
1076 // handle special case for sonification while in call
1077 if (isInCall()) {
1078 handleIncallSonification(stream, true, false);
1079 }
1080
1081 // apply volume rules for current stream and device if necessary
1082 checkAndSetVolume(stream,
1083 mStreams[stream].getVolumeIndex(newDevice),
1084 output,
1085 newDevice);
1086
1087 // update the outputs if starting an output with a stream that can affect notification
1088 // routing
1089 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001090
1091 // Automatically enable the remote submix input when output is started on a re routing mix
1092 // of type MIX_TYPE_RECORDERS
1093 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1094 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001095 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001096 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001097 outputDesc->mPolicyMix->mRegistrationId,
1098 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001099 }
1100
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001101 // force reevaluating accessibility routing when ringtone or alarm starts
1102 if (strategy == STRATEGY_SONIFICATION) {
1103 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1104 }
1105
Eric Laurente552edb2014-03-10 17:42:56 -07001106 if (waitMs > muteWaitMs) {
1107 usleep((waitMs - muteWaitMs) * 2 * 1000);
1108 }
1109 }
1110 return NO_ERROR;
1111}
1112
1113
Eric Laurente0720872014-03-11 09:30:41 -07001114status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001115 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001116 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001117{
1118 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1119 ssize_t index = mOutputs.indexOfKey(output);
1120 if (index < 0) {
1121 ALOGW("stopOutput() unknown output %d", output);
1122 return BAD_VALUE;
1123 }
1124
Eric Laurent1f2f2232014-06-02 12:01:23 -07001125 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001126
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001127 // always handle stream stop, check which stream type is stopping
1128 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1129
Eric Laurente552edb2014-03-10 17:42:56 -07001130 // handle special case for sonification while in call
1131 if (isInCall()) {
1132 handleIncallSonification(stream, false, false);
1133 }
1134
1135 if (outputDesc->mRefCount[stream] > 0) {
1136 // decrement usage count of this stream on the output
1137 outputDesc->changeRefCount(stream, -1);
1138 // store time at which the stream was stopped - see isStreamActive()
1139 if (outputDesc->mRefCount[stream] == 0) {
Eric Laurentc722f302014-12-10 11:21:49 -08001140 // Automatically disable the remote submix input when output is stopped on a
1141 // re routing mix of type MIX_TYPE_RECORDERS
1142 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1143 outputDesc->mPolicyMix != NULL &&
1144 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001145 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001146 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001147 outputDesc->mPolicyMix->mRegistrationId,
1148 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001149 }
1150
Eric Laurente552edb2014-03-10 17:42:56 -07001151 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -07001152 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001153 // delay the device switch by twice the latency because stopOutput() is executed when
1154 // the track stop() command is received and at that time the audio track buffer can
1155 // still contain data that needs to be drained. The latency only covers the audio HAL
1156 // and kernel buffers. Also the latency does not always include additional delay in the
1157 // audio path (audio DSP, CODEC ...)
1158 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1159
1160 // force restoring the device selection on other active outputs if it differs from the
1161 // one being selected for this output
1162 for (size_t i = 0; i < mOutputs.size(); i++) {
1163 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001164 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001165 if (curOutput != output &&
1166 desc->isActive() &&
1167 outputDesc->sharesHwModuleWith(desc) &&
1168 (newDevice != desc->device())) {
1169 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -07001170 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001171 true,
1172 outputDesc->mLatency*2);
1173 }
1174 }
1175 // update the outputs if stopping one with a stream that can affect notification routing
1176 handleNotificationRoutingForStream(stream);
1177 }
1178 return NO_ERROR;
1179 } else {
1180 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1181 return INVALID_OPERATION;
1182 }
1183}
1184
Eric Laurente83b55d2014-11-14 10:06:21 -08001185void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001186 audio_stream_type_t stream __unused,
1187 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001188{
1189 ALOGV("releaseOutput() %d", output);
1190 ssize_t index = mOutputs.indexOfKey(output);
1191 if (index < 0) {
1192 ALOGW("releaseOutput() releasing unknown output %d", output);
1193 return;
1194 }
1195
1196#ifdef AUDIO_POLICY_TEST
1197 int testIndex = testOutputIndex(output);
1198 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001199 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001200 if (outputDesc->isActive()) {
1201 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001202 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001203 mTestOutputs[testIndex] = 0;
1204 }
1205 return;
1206 }
1207#endif //AUDIO_POLICY_TEST
1208
Eric Laurent1f2f2232014-06-02 12:01:23 -07001209 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001210 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001211 if (desc->mDirectOpenCount <= 0) {
1212 ALOGW("releaseOutput() invalid open count %d for output %d",
1213 desc->mDirectOpenCount, output);
1214 return;
1215 }
1216 if (--desc->mDirectOpenCount == 0) {
1217 closeOutput(output);
1218 // If effects where present on the output, audioflinger moved them to the primary
1219 // output by default: move them back to the appropriate output.
1220 audio_io_handle_t dstOutput = getOutputForEffect();
1221 if (dstOutput != mPrimaryOutput) {
1222 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1223 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001224 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001225 }
1226 }
1227}
1228
1229
Eric Laurentcaf7f482014-11-25 17:50:47 -08001230status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1231 audio_io_handle_t *input,
1232 audio_session_t session,
1233 uint32_t samplingRate,
1234 audio_format_t format,
1235 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001236 audio_input_flags_t flags,
1237 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001238{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001239 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1240 "session %d, flags %#x",
1241 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001242
Eric Laurentcaf7f482014-11-25 17:50:47 -08001243 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001244 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001245 audio_devices_t device;
1246 // handle legacy remote submix case where the address was not always specified
1247 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001248 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001249 audio_source_t inputSource = attr->source;
1250 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001251 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001252
Eric Laurentc447ded2015-01-06 08:47:05 -08001253 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1254 inputSource = AUDIO_SOURCE_MIC;
1255 }
1256 halInputSource = inputSource;
1257
1258 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
François Gaffie036e1e92015-03-19 10:16:24 +01001260 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, policyMix);
1261 if (ret != NO_ERROR) {
1262 return ret;
1263 }
1264 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001265 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1266 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001267 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001268 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001269 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001270 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001271 return BAD_VALUE;
1272 }
Eric Laurentc722f302014-12-10 11:21:49 -08001273 if (policyMix != NULL) {
1274 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001275 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1276 // there is an external policy, but this input is attached to a mix of recorders,
1277 // meaning it receives audio injected into the framework, so the recorder doesn't
1278 // know about it and is therefore considered "legacy"
1279 *inputType = API_INPUT_LEGACY;
1280 } else {
1281 // recording a mix of players defined by an external policy, we're rerouting for
1282 // an external policy
1283 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1284 }
Eric Laurentc722f302014-12-10 11:21:49 -08001285 } else if (audio_is_remote_submix_device(device)) {
1286 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001287 *inputType = API_INPUT_MIX_CAPTURE;
1288 } else {
1289 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001290 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001291 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001292 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001293 case AUDIO_SOURCE_VOICE_UPLINK:
1294 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1295 break;
1296 case AUDIO_SOURCE_VOICE_DOWNLINK:
1297 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1298 break;
1299 case AUDIO_SOURCE_VOICE_CALL:
1300 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1301 break;
1302 default:
1303 break;
1304 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001305 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001306 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1307 if (index >= 0) {
1308 *input = mSoundTriggerSessions.valueFor(session);
1309 isSoundTrigger = true;
1310 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1311 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1312 } else {
1313 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1314 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001315 }
1316 }
1317
Eric Laurent275e8e92014-11-30 15:14:47 -08001318 sp<IOProfile> profile = getInputProfile(device, address,
1319 samplingRate, format, channelMask,
1320 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001321 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001322 //retry without flags
1323 audio_input_flags_t log_flags = flags;
1324 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001325 profile = getInputProfile(device, address,
1326 samplingRate, format, channelMask,
1327 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001328 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001329 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1330 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001331 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001332 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001333 }
Eric Laurente552edb2014-03-10 17:42:56 -07001334 }
1335
1336 if (profile->mModule->mHandle == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001337 ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName);
1338 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001339 }
1340
1341 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1342 config.sample_rate = samplingRate;
1343 config.channel_mask = channelMask;
1344 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001345
Eric Laurentcf2c0212014-07-25 16:20:43 -07001346 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001347 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001348 &config,
1349 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001350 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001351 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001352 flags);
1353
1354 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001356 (samplingRate != config.sample_rate) ||
1357 (format != config.format) ||
1358 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001359 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001360 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001361 if (*input != AUDIO_IO_HANDLE_NONE) {
1362 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001363 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001364 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001365 }
1366
Eric Laurent1f2f2232014-06-02 12:01:23 -07001367 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001368 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001369 inputDesc->mRefCount = 0;
1370 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001371 inputDesc->mSamplingRate = samplingRate;
1372 inputDesc->mFormat = format;
1373 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001374 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001375 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001376 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001377 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001378
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001379 ALOGV("getInputForAttr() returns input type = %d", inputType);
1380
Eric Laurentcaf7f482014-11-25 17:50:47 -08001381 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001382 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001383 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001384}
1385
Eric Laurent4dc68062014-07-28 17:26:49 -07001386status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1387 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001388{
1389 ALOGV("startInput() input %d", input);
1390 ssize_t index = mInputs.indexOfKey(input);
1391 if (index < 0) {
1392 ALOGW("startInput() unknown input %d", input);
1393 return BAD_VALUE;
1394 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001395 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001396
Eric Laurentc722f302014-12-10 11:21:49 -08001397 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001398 if (index < 0) {
1399 ALOGW("startInput() unknown session %d on input %d", session, input);
1400 return BAD_VALUE;
1401 }
1402
Glenn Kasten74a8e252014-07-24 14:09:55 -07001403 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001404 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001405
1406 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001407 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001408 if (activeInput != 0 && activeInput != input) {
1409
1410 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1411 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001412 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001413 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001414 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001415 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1416 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001417 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001418 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001419 return INVALID_OPERATION;
1420 }
1421 }
1422 }
1423
Glenn Kasten74a8e252014-07-24 14:09:55 -07001424 if (inputDesc->mRefCount == 0) {
François Gaffie53615e22015-03-19 09:24:12 +01001425 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001426 SoundTrigger::setCaptureState(true);
1427 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001428 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001429
Eric Laurentc722f302014-12-10 11:21:49 -08001430 // automatically enable the remote submix output when input is started if not
1431 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001432 // For remote submix (a virtual device), we open only one input per capture request.
1433 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001434 String8 address = String8("");
1435 if (inputDesc->mPolicyMix == NULL) {
1436 address = String8("0");
1437 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1438 address = inputDesc->mPolicyMix->mRegistrationId;
1439 }
1440 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001441 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001442 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001443 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001444 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001445 }
Eric Laurente552edb2014-03-10 17:42:56 -07001446 }
1447
Eric Laurente552edb2014-03-10 17:42:56 -07001448 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1449
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001450 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001451 return NO_ERROR;
1452}
1453
Eric Laurent4dc68062014-07-28 17:26:49 -07001454status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1455 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001456{
1457 ALOGV("stopInput() input %d", input);
1458 ssize_t index = mInputs.indexOfKey(input);
1459 if (index < 0) {
1460 ALOGW("stopInput() unknown input %d", input);
1461 return BAD_VALUE;
1462 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001463 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001464
Eric Laurentc722f302014-12-10 11:21:49 -08001465 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001466 if (index < 0) {
1467 ALOGW("stopInput() unknown session %d on input %d", session, input);
1468 return BAD_VALUE;
1469 }
1470
Eric Laurente552edb2014-03-10 17:42:56 -07001471 if (inputDesc->mRefCount == 0) {
1472 ALOGW("stopInput() input %d already stopped", input);
1473 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001474 }
1475
1476 inputDesc->mRefCount--;
1477 if (inputDesc->mRefCount == 0) {
1478
Eric Laurentc722f302014-12-10 11:21:49 -08001479 // automatically disable the remote submix output when input is stopped if not
1480 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001481 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001482 String8 address = String8("");
1483 if (inputDesc->mPolicyMix == NULL) {
1484 address = String8("0");
1485 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1486 address = inputDesc->mPolicyMix->mRegistrationId;
1487 }
1488 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001489 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001490 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001491 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001492 }
Eric Laurente552edb2014-03-10 17:42:56 -07001493 }
1494
Eric Laurent1c333e22014-05-20 10:48:17 -07001495 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001496
François Gaffie53615e22015-03-19 09:24:12 +01001497 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001498 SoundTrigger::setCaptureState(false);
1499 }
Eric Laurente552edb2014-03-10 17:42:56 -07001500 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001501 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001502}
1503
Eric Laurent4dc68062014-07-28 17:26:49 -07001504void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1505 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001506{
1507 ALOGV("releaseInput() %d", input);
1508 ssize_t index = mInputs.indexOfKey(input);
1509 if (index < 0) {
1510 ALOGW("releaseInput() releasing unknown input %d", input);
1511 return;
1512 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001513 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1514 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001515
Eric Laurentc722f302014-12-10 11:21:49 -08001516 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001517 if (index < 0) {
1518 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1519 return;
1520 }
Eric Laurentc722f302014-12-10 11:21:49 -08001521 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001522 if (inputDesc->mOpenRefCount == 0) {
1523 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1524 return;
1525 }
1526 inputDesc->mOpenRefCount--;
1527 if (inputDesc->mOpenRefCount > 0) {
1528 ALOGV("releaseInput() exit > 0");
1529 return;
1530 }
1531
Eric Laurent05b90f82014-08-27 15:32:29 -07001532 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001533 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001534 ALOGV("releaseInput() exit");
1535}
1536
Eric Laurentd4692962014-05-05 18:13:44 -07001537void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001538 bool patchRemoved = false;
1539
Eric Laurentd4692962014-05-05 18:13:44 -07001540 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001541 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1542 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1543 if (patch_index >= 0) {
1544 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1545 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1546 mAudioPatches.removeItemsAt(patch_index);
1547 patchRemoved = true;
1548 }
Eric Laurentd4692962014-05-05 18:13:44 -07001549 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1550 }
1551 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001552 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001553
1554 if (patchRemoved) {
1555 mpClientInterface->onAudioPatchListUpdate();
1556 }
Eric Laurentd4692962014-05-05 18:13:44 -07001557}
1558
Eric Laurente0720872014-03-11 09:30:41 -07001559void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001560 int indexMin,
1561 int indexMax)
1562{
1563 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1564 if (indexMin < 0 || indexMin >= indexMax) {
1565 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1566 return;
1567 }
1568 mStreams[stream].mIndexMin = indexMin;
1569 mStreams[stream].mIndexMax = indexMax;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001570 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1571 if (stream == AUDIO_STREAM_MUSIC) {
1572 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin;
1573 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax;
1574 }
Eric Laurente552edb2014-03-10 17:42:56 -07001575}
1576
Eric Laurente0720872014-03-11 09:30:41 -07001577status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001578 int index,
1579 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001580{
1581
1582 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1583 return BAD_VALUE;
1584 }
1585 if (!audio_is_output_device(device)) {
1586 return BAD_VALUE;
1587 }
1588
1589 // Force max volume if stream cannot be muted
1590 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1591
1592 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1593 stream, device, index);
1594
1595 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1596 // clear all device specific values
1597 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1598 mStreams[stream].mIndexCur.clear();
1599 }
1600 mStreams[stream].mIndexCur.add(device, index);
1601
Eric Laurent31551f82014-10-10 18:21:56 -07001602 // update volume on all outputs whose current device is also selected by the same
1603 // strategy as the device specified by the caller
1604 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001605
1606
1607 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1608 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1609 if (stream == AUDIO_STREAM_MUSIC) {
1610 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index);
1611 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1612 }
1613 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1614 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001615 return NO_ERROR;
1616 }
Eric Laurente552edb2014-03-10 17:42:56 -07001617 status_t status = NO_ERROR;
1618 for (size_t i = 0; i < mOutputs.size(); i++) {
1619 audio_devices_t curDevice =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001620 ApmGains::getDeviceForVolume(mOutputs.valueAt(i)->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001621 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001622 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1623 if (volStatus != NO_ERROR) {
1624 status = volStatus;
1625 }
1626 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001627 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1628 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
1629 index, mOutputs.keyAt(i), curDevice);
1630 }
Eric Laurente552edb2014-03-10 17:42:56 -07001631 }
1632 return status;
1633}
1634
Eric Laurente0720872014-03-11 09:30:41 -07001635status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001636 int *index,
1637 audio_devices_t device)
1638{
1639 if (index == NULL) {
1640 return BAD_VALUE;
1641 }
1642 if (!audio_is_output_device(device)) {
1643 return BAD_VALUE;
1644 }
1645 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1646 // the strategy the stream belongs to.
1647 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1648 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1649 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001650 device = ApmGains::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001651
1652 *index = mStreams[stream].getVolumeIndex(device);
1653 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1654 return NO_ERROR;
1655}
1656
Eric Laurente0720872014-03-11 09:30:41 -07001657audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001658 const SortedVector<audio_io_handle_t>& outputs)
1659{
1660 // select one output among several suitable for global effects.
1661 // The priority is as follows:
1662 // 1: An offloaded output. If the effect ends up not being offloadable,
1663 // AudioFlinger will invalidate the track and the offloaded output
1664 // will be closed causing the effect to be moved to a PCM output.
1665 // 2: A deep buffer output
1666 // 3: the first output in the list
1667
1668 if (outputs.size() == 0) {
1669 return 0;
1670 }
1671
1672 audio_io_handle_t outputOffloaded = 0;
1673 audio_io_handle_t outputDeepBuffer = 0;
1674
1675 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001676 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001677 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001678 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1679 outputOffloaded = outputs[i];
1680 }
1681 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1682 outputDeepBuffer = outputs[i];
1683 }
1684 }
1685
1686 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1687 outputOffloaded, outputDeepBuffer);
1688 if (outputOffloaded != 0) {
1689 return outputOffloaded;
1690 }
1691 if (outputDeepBuffer != 0) {
1692 return outputDeepBuffer;
1693 }
1694
1695 return outputs[0];
1696}
1697
Eric Laurente0720872014-03-11 09:30:41 -07001698audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001699{
1700 // apply simple rule where global effects are attached to the same output as MUSIC streams
1701
Eric Laurent3b73df72014-03-11 09:06:29 -07001702 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001703 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1704 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1705
1706 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1707 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1708 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1709
1710 return output;
1711}
1712
Eric Laurente0720872014-03-11 09:30:41 -07001713status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001714 audio_io_handle_t io,
1715 uint32_t strategy,
1716 int session,
1717 int id)
1718{
1719 ssize_t index = mOutputs.indexOfKey(io);
1720 if (index < 0) {
1721 index = mInputs.indexOfKey(io);
1722 if (index < 0) {
1723 ALOGW("registerEffect() unknown io %d", io);
1724 return INVALID_OPERATION;
1725 }
1726 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001727 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001728}
1729
Eric Laurente0720872014-03-11 09:30:41 -07001730bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001731{
1732 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001733 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001734 if (inputDescriptor->mRefCount == 0) {
1735 continue;
1736 }
1737 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001738 return true;
1739 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001740 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1741 // corresponds to an active capture triggered by a hardware hotword recognition
1742 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1743 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1744 // FIXME: we should not assume that the first session is the active one and keep
1745 // activity count per session. Same in startInput().
1746 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1747 if (index >= 0) {
1748 return true;
1749 }
1750 }
Eric Laurente552edb2014-03-10 17:42:56 -07001751 }
1752 return false;
1753}
1754
Eric Laurent275e8e92014-11-30 15:14:47 -08001755// Register a list of custom mixes with their attributes and format.
1756// When a mix is registered, corresponding input and output profiles are
1757// added to the remote submix hw module. The profile contains only the
1758// parameters (sampling rate, format...) specified by the mix.
1759// The corresponding input remote submix device is also connected.
1760//
1761// When a remote submix device is connected, the address is checked to select the
1762// appropriate profile and the corresponding input or output stream is opened.
1763//
1764// When capture starts, getInputForAttr() will:
1765// - 1 look for a mix matching the address passed in attribtutes tags if any
1766// - 2 if none found, getDeviceForInputSource() will:
1767// - 2.1 look for a mix matching the attributes source
1768// - 2.2 if none found, default to device selection by policy rules
1769// At this time, the corresponding output remote submix device is also connected
1770// and active playback use cases can be transferred to this mix if needed when reconnecting
1771// after AudioTracks are invalidated
1772//
1773// When playback starts, getOutputForAttr() will:
1774// - 1 look for a mix matching the address passed in attribtutes tags if any
1775// - 2 if none found, look for a mix matching the attributes usage
1776// - 3 if none found, default to device and output selection by policy rules.
1777
1778status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1779{
1780 sp<HwModule> module;
1781 for (size_t i = 0; i < mHwModules.size(); i++) {
1782 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1783 mHwModules[i]->mHandle != 0) {
1784 module = mHwModules[i];
1785 break;
1786 }
1787 }
1788
1789 if (module == 0) {
1790 return INVALID_OPERATION;
1791 }
1792
1793 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1794
1795 for (size_t i = 0; i < mixes.size(); i++) {
1796 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001797
1798 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001799 continue;
1800 }
1801 audio_config_t outputConfig = mixes[i].mFormat;
1802 audio_config_t inputConfig = mixes[i].mFormat;
1803 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1804 // stereo and let audio flinger do the channel conversion if needed.
1805 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1806 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1807 module->addOutputProfile(address, &outputConfig,
1808 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1809 module->addInputProfile(address, &inputConfig,
1810 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001811
Eric Laurentc722f302014-12-10 11:21:49 -08001812 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001813 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001814 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001815 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001816 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001817 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001818 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001819 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001820 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001821 }
1822 return NO_ERROR;
1823}
1824
1825status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1826{
1827 sp<HwModule> module;
1828 for (size_t i = 0; i < mHwModules.size(); i++) {
1829 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1830 mHwModules[i]->mHandle != 0) {
1831 module = mHwModules[i];
1832 break;
1833 }
1834 }
1835
1836 if (module == 0) {
1837 return INVALID_OPERATION;
1838 }
1839
1840 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1841
1842 for (size_t i = 0; i < mixes.size(); i++) {
1843 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001844
1845 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001846 continue;
1847 }
1848
Eric Laurentc722f302014-12-10 11:21:49 -08001849 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1850 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1851 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001852 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001853 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001854 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001855 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001856
1857 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1858 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1859 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001860 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001861 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001862 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001863 }
1864 module->removeOutputProfile(address);
1865 module->removeInputProfile(address);
1866 }
1867 return NO_ERROR;
1868}
1869
Eric Laurente552edb2014-03-10 17:42:56 -07001870
Eric Laurente0720872014-03-11 09:30:41 -07001871status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001872{
1873 const size_t SIZE = 256;
1874 char buffer[SIZE];
1875 String8 result;
1876
1877 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1878 result.append(buffer);
1879
1880 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1881 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001882 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1883 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001884 snprintf(buffer, SIZE, " Force use for communications %d\n",
1885 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07001886 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001887 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07001888 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001889 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07001890 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001891 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07001892 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001893 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07001894 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001895 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
1896 mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]);
1897 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001898
François Gaffie53615e22015-03-19 09:24:12 +01001899 mAvailableOutputDevices.dump(fd, String8("output"));
1900 mAvailableInputDevices.dump(fd, String8("input"));
1901 mHwModules.dump(fd);
1902 mOutputs.dump(fd);
1903 mInputs.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001904
1905 snprintf(buffer, SIZE, "\nStreams dump:\n");
1906 write(fd, buffer, strlen(buffer));
1907 snprintf(buffer, SIZE,
1908 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1909 write(fd, buffer, strlen(buffer));
Mark Salyzynbeb9e302014-06-18 16:33:15 -07001910 for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001911 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07001912 write(fd, buffer, strlen(buffer));
1913 mStreams[i].dump(fd);
1914 }
1915
François Gaffie45ed3b02015-03-19 10:35:14 +01001916 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001917 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001918
1919 return NO_ERROR;
1920}
1921
1922// This function checks for the parameters which can be offloaded.
1923// This can be enhanced depending on the capability of the DSP and policy
1924// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001925bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001926{
1927 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001928 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001929 offloadInfo.sample_rate, offloadInfo.channel_mask,
1930 offloadInfo.format,
1931 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1932 offloadInfo.has_video);
1933
1934 // Check if offload has been disabled
1935 char propValue[PROPERTY_VALUE_MAX];
1936 if (property_get("audio.offload.disable", propValue, "0")) {
1937 if (atoi(propValue) != 0) {
1938 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1939 return false;
1940 }
1941 }
1942
1943 // Check if stream type is music, then only allow offload as of now.
1944 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1945 {
1946 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1947 return false;
1948 }
1949
1950 //TODO: enable audio offloading with video when ready
1951 if (offloadInfo.has_video)
1952 {
1953 ALOGV("isOffloadSupported: has_video == true, returning false");
1954 return false;
1955 }
1956
1957 //If duration is less than minimum value defined in property, return false
1958 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1959 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1960 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1961 return false;
1962 }
1963 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1964 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1965 return false;
1966 }
1967
1968 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1969 // creating an offloaded track and tearing it down immediately after start when audioflinger
1970 // detects there is an active non offloadable effect.
1971 // FIXME: We should check the audio session here but we do not have it in this context.
1972 // This may prevent offloading in rare situations where effects are left active by apps
1973 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01001974 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001975 return false;
1976 }
1977
1978 // See if there is a profile to support this.
1979 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001980 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001981 offloadInfo.sample_rate,
1982 offloadInfo.format,
1983 offloadInfo.channel_mask,
1984 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001985 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1986 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001987}
1988
Eric Laurent6a94d692014-05-20 11:18:06 -07001989status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1990 audio_port_type_t type,
1991 unsigned int *num_ports,
1992 struct audio_port *ports,
1993 unsigned int *generation)
1994{
1995 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1996 generation == NULL) {
1997 return BAD_VALUE;
1998 }
1999 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2000 if (ports == NULL) {
2001 *num_ports = 0;
2002 }
2003
2004 size_t portsWritten = 0;
2005 size_t portsMax = *num_ports;
2006 *num_ports = 0;
2007 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2008 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2009 for (size_t i = 0;
2010 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2011 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2012 }
2013 *num_ports += mAvailableOutputDevices.size();
2014 }
2015 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2016 for (size_t i = 0;
2017 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2018 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2019 }
2020 *num_ports += mAvailableInputDevices.size();
2021 }
2022 }
2023 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2024 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2025 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2026 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2027 }
2028 *num_ports += mInputs.size();
2029 }
2030 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002031 size_t numOutputs = 0;
2032 for (size_t i = 0; i < mOutputs.size(); i++) {
2033 if (!mOutputs[i]->isDuplicated()) {
2034 numOutputs++;
2035 if (portsWritten < portsMax) {
2036 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2037 }
2038 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002039 }
Eric Laurent84c70242014-06-23 08:46:27 -07002040 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002041 }
2042 }
2043 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002044 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002045 return NO_ERROR;
2046}
2047
2048status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2049{
2050 return NO_ERROR;
2051}
2052
Eric Laurent6a94d692014-05-20 11:18:06 -07002053status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2054 audio_patch_handle_t *handle,
2055 uid_t uid)
2056{
2057 ALOGV("createAudioPatch()");
2058
2059 if (handle == NULL || patch == NULL) {
2060 return BAD_VALUE;
2061 }
2062 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2063
Eric Laurent874c42872014-08-08 15:13:39 -07002064 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2065 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2066 return BAD_VALUE;
2067 }
2068 // only one source per audio patch supported for now
2069 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002070 return INVALID_OPERATION;
2071 }
Eric Laurent874c42872014-08-08 15:13:39 -07002072
2073 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002074 return INVALID_OPERATION;
2075 }
Eric Laurent874c42872014-08-08 15:13:39 -07002076 for (size_t i = 0; i < patch->num_sinks; i++) {
2077 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2078 return INVALID_OPERATION;
2079 }
2080 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002081
2082 sp<AudioPatch> patchDesc;
2083 ssize_t index = mAudioPatches.indexOfKey(*handle);
2084
Eric Laurent6a94d692014-05-20 11:18:06 -07002085 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2086 patch->sources[0].role,
2087 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002088#if LOG_NDEBUG == 0
2089 for (size_t i = 0; i < patch->num_sinks; i++) {
2090 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2091 patch->sinks[i].role,
2092 patch->sinks[i].type);
2093 }
2094#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002095
2096 if (index >= 0) {
2097 patchDesc = mAudioPatches.valueAt(index);
2098 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2099 mUidCached, patchDesc->mUid, uid);
2100 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2101 return INVALID_OPERATION;
2102 }
2103 } else {
2104 *handle = 0;
2105 }
2106
2107 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002108 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002109 if (outputDesc == NULL) {
2110 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2111 return BAD_VALUE;
2112 }
Eric Laurent84c70242014-06-23 08:46:27 -07002113 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2114 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002115 if (patchDesc != 0) {
2116 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2117 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2118 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2119 return BAD_VALUE;
2120 }
2121 }
Eric Laurent874c42872014-08-08 15:13:39 -07002122 DeviceVector devices;
2123 for (size_t i = 0; i < patch->num_sinks; i++) {
2124 // Only support mix to devices connection
2125 // TODO add support for mix to mix connection
2126 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2127 ALOGV("createAudioPatch() source mix but sink is not a device");
2128 return INVALID_OPERATION;
2129 }
2130 sp<DeviceDescriptor> devDesc =
2131 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2132 if (devDesc == 0) {
2133 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2134 return BAD_VALUE;
2135 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002136
François Gaffie53615e22015-03-19 09:24:12 +01002137 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002138 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002139 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002140 NULL, // updatedSamplingRate
2141 patch->sources[0].format,
2142 patch->sources[0].channel_mask,
2143 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2144 ALOGV("createAudioPatch() profile not supported for device %08x", devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002145 return INVALID_OPERATION;
2146 }
2147 devices.add(devDesc);
2148 }
2149 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002150 return INVALID_OPERATION;
2151 }
Eric Laurent874c42872014-08-08 15:13:39 -07002152
Eric Laurent6a94d692014-05-20 11:18:06 -07002153 // TODO: reconfigure output format and channels here
2154 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002155 devices.types(), outputDesc->mIoHandle);
2156 setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002157 index = mAudioPatches.indexOfKey(*handle);
2158 if (index >= 0) {
2159 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2160 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2161 }
2162 patchDesc = mAudioPatches.valueAt(index);
2163 patchDesc->mUid = uid;
2164 ALOGV("createAudioPatch() success");
2165 } else {
2166 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2167 return INVALID_OPERATION;
2168 }
2169 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2170 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2171 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002172 // only one sink supported when connecting an input device to a mix
2173 if (patch->num_sinks > 1) {
2174 return INVALID_OPERATION;
2175 }
François Gaffie53615e22015-03-19 09:24:12 +01002176 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002177 if (inputDesc == NULL) {
2178 return BAD_VALUE;
2179 }
2180 if (patchDesc != 0) {
2181 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2182 return BAD_VALUE;
2183 }
2184 }
2185 sp<DeviceDescriptor> devDesc =
2186 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2187 if (devDesc == 0) {
2188 return BAD_VALUE;
2189 }
2190
François Gaffie53615e22015-03-19 09:24:12 +01002191 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002192 devDesc->mAddress,
2193 patch->sinks[0].sample_rate,
2194 NULL, /*updatedSampleRate*/
2195 patch->sinks[0].format,
2196 patch->sinks[0].channel_mask,
2197 // FIXME for the parameter type,
2198 // and the NONE
2199 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002200 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002201 return INVALID_OPERATION;
2202 }
2203 // TODO: reconfigure output format and channels here
2204 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002205 devDesc->type(), inputDesc->mIoHandle);
2206 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002207 index = mAudioPatches.indexOfKey(*handle);
2208 if (index >= 0) {
2209 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2210 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2211 }
2212 patchDesc = mAudioPatches.valueAt(index);
2213 patchDesc->mUid = uid;
2214 ALOGV("createAudioPatch() success");
2215 } else {
2216 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2217 return INVALID_OPERATION;
2218 }
2219 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2220 // device to device connection
2221 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002222 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002223 return BAD_VALUE;
2224 }
2225 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002226 sp<DeviceDescriptor> srcDeviceDesc =
2227 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002228 if (srcDeviceDesc == 0) {
2229 return BAD_VALUE;
2230 }
Eric Laurent874c42872014-08-08 15:13:39 -07002231
Eric Laurent6a94d692014-05-20 11:18:06 -07002232 //update source and sink with our own data as the data passed in the patch may
2233 // be incomplete.
2234 struct audio_patch newPatch = *patch;
2235 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002236
Eric Laurent874c42872014-08-08 15:13:39 -07002237 for (size_t i = 0; i < patch->num_sinks; i++) {
2238 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2239 ALOGV("createAudioPatch() source device but one sink is not a device");
2240 return INVALID_OPERATION;
2241 }
2242
2243 sp<DeviceDescriptor> sinkDeviceDesc =
2244 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2245 if (sinkDeviceDesc == 0) {
2246 return BAD_VALUE;
2247 }
2248 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2249
2250 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
2251 // only one sink supported when connected devices across HW modules
2252 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002253 return INVALID_OPERATION;
2254 }
Eric Laurent874c42872014-08-08 15:13:39 -07002255 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002256 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002257 // if the sink device is reachable via an opened output stream, request to go via
2258 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002259 audio_io_handle_t output = selectOutput(outputs,
2260 AUDIO_OUTPUT_FLAG_NONE,
2261 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002262 if (output != AUDIO_IO_HANDLE_NONE) {
2263 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2264 if (outputDesc->isDuplicated()) {
2265 return INVALID_OPERATION;
2266 }
2267 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
2268 newPatch.num_sources = 2;
2269 }
Eric Laurent83b88082014-06-20 18:31:16 -07002270 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002271 }
2272 // TODO: check from routing capabilities in config file and other conflicting patches
2273
2274 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2275 if (index >= 0) {
2276 afPatchHandle = patchDesc->mAfPatchHandle;
2277 }
2278
2279 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2280 &afPatchHandle,
2281 0);
2282 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2283 status, afPatchHandle);
2284 if (status == NO_ERROR) {
2285 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002286 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002287 addAudioPatch(patchDesc->mHandle, patchDesc);
2288 } else {
2289 patchDesc->mPatch = newPatch;
2290 }
2291 patchDesc->mAfPatchHandle = afPatchHandle;
2292 *handle = patchDesc->mHandle;
2293 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002294 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002295 } else {
2296 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2297 status);
2298 return INVALID_OPERATION;
2299 }
2300 } else {
2301 return BAD_VALUE;
2302 }
2303 } else {
2304 return BAD_VALUE;
2305 }
2306 return NO_ERROR;
2307}
2308
2309status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2310 uid_t uid)
2311{
2312 ALOGV("releaseAudioPatch() patch %d", handle);
2313
2314 ssize_t index = mAudioPatches.indexOfKey(handle);
2315
2316 if (index < 0) {
2317 return BAD_VALUE;
2318 }
2319 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2320 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2321 mUidCached, patchDesc->mUid, uid);
2322 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2323 return INVALID_OPERATION;
2324 }
2325
2326 struct audio_patch *patch = &patchDesc->mPatch;
2327 patchDesc->mUid = mUidCached;
2328 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002329 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002330 if (outputDesc == NULL) {
2331 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2332 return BAD_VALUE;
2333 }
2334
2335 setOutputDevice(outputDesc->mIoHandle,
2336 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2337 true,
2338 0,
2339 NULL);
2340 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2341 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002342 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002343 if (inputDesc == NULL) {
2344 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2345 return BAD_VALUE;
2346 }
2347 setInputDevice(inputDesc->mIoHandle,
2348 getNewInputDevice(inputDesc->mIoHandle),
2349 true,
2350 NULL);
2351 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2352 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2353 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2354 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2355 status, patchDesc->mAfPatchHandle);
2356 removeAudioPatch(patchDesc->mHandle);
2357 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002358 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002359 } else {
2360 return BAD_VALUE;
2361 }
2362 } else {
2363 return BAD_VALUE;
2364 }
2365 return NO_ERROR;
2366}
2367
2368status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2369 struct audio_patch *patches,
2370 unsigned int *generation)
2371{
François Gaffie53615e22015-03-19 09:24:12 +01002372 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002373 return BAD_VALUE;
2374 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002375 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002376 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002377}
2378
Eric Laurente1715a42014-05-20 11:30:42 -07002379status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002380{
Eric Laurente1715a42014-05-20 11:30:42 -07002381 ALOGV("setAudioPortConfig()");
2382
2383 if (config == NULL) {
2384 return BAD_VALUE;
2385 }
2386 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2387 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002388 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2389 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002390 }
2391
Eric Laurenta121f902014-06-03 13:32:54 -07002392 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002393 if (config->type == AUDIO_PORT_TYPE_MIX) {
2394 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
François Gaffie53615e22015-03-19 09:24:12 +01002395 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002396 if (outputDesc == NULL) {
2397 return BAD_VALUE;
2398 }
Eric Laurent84c70242014-06-23 08:46:27 -07002399 ALOG_ASSERT(!outputDesc->isDuplicated(),
2400 "setAudioPortConfig() called on duplicated output %d",
2401 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002402 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002403 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002404 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002405 if (inputDesc == NULL) {
2406 return BAD_VALUE;
2407 }
Eric Laurenta121f902014-06-03 13:32:54 -07002408 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002409 } else {
2410 return BAD_VALUE;
2411 }
2412 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2413 sp<DeviceDescriptor> deviceDesc;
2414 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2415 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2416 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2417 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2418 } else {
2419 return BAD_VALUE;
2420 }
2421 if (deviceDesc == NULL) {
2422 return BAD_VALUE;
2423 }
Eric Laurenta121f902014-06-03 13:32:54 -07002424 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002425 } else {
2426 return BAD_VALUE;
2427 }
2428
Eric Laurenta121f902014-06-03 13:32:54 -07002429 struct audio_port_config backupConfig;
2430 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2431 if (status == NO_ERROR) {
2432 struct audio_port_config newConfig;
2433 audioPortConfig->toAudioPortConfig(&newConfig, config);
2434 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002435 }
Eric Laurenta121f902014-06-03 13:32:54 -07002436 if (status != NO_ERROR) {
2437 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002438 }
Eric Laurente1715a42014-05-20 11:30:42 -07002439
2440 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002441}
2442
2443void AudioPolicyManager::clearAudioPatches(uid_t uid)
2444{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002445 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002446 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2447 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002448 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002449 }
2450 }
2451}
2452
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002453status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2454 audio_io_handle_t *ioHandle,
2455 audio_devices_t *device)
2456{
2457 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2458 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002459 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002460
François Gaffiedf372692015-03-19 10:43:27 +01002461 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002462}
2463
Eric Laurente552edb2014-03-10 17:42:56 -07002464// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002465// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002466// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002467uint32_t AudioPolicyManager::nextAudioPortGeneration()
2468{
2469 return android_atomic_inc(&mAudioPortGeneration);
2470}
2471
Eric Laurente0720872014-03-11 09:30:41 -07002472AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002473 :
2474#ifdef AUDIO_POLICY_TEST
2475 Thread(false),
2476#endif //AUDIO_POLICY_TEST
2477 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002478 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002479 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002480 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002481 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002482 mAudioPortGeneration(1),
2483 mBeaconMuteRefCount(0),
2484 mBeaconPlayingRefCount(0),
2485 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002486{
Eric Laurent6a94d692014-05-20 11:18:06 -07002487 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002488 mpClientInterface = clientInterface;
2489
Eric Laurent3b73df72014-03-11 09:06:29 -07002490 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2491 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002492 }
2493
Paul McLeane743a472015-01-28 11:07:31 -08002494 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002495 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2496 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2497 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2498 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2499 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2500 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002501 ALOGE("could not load audio policy configuration file, setting defaults");
2502 defaultAudioPolicyConfig();
2503 }
2504 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002505 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002506
2507 // must be done after reading the policy
2508 initializeVolumeCurves();
2509
2510 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002511 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2512 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002513 for (size_t i = 0; i < mHwModules.size(); i++) {
2514 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2515 if (mHwModules[i]->mHandle == 0) {
2516 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2517 continue;
2518 }
2519 // open all output streams needed to access attached devices
2520 // except for direct output streams that are only opened when they are actually
2521 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002522 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002523 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2524 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002525 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002526
Eric Laurent3a4311c2014-03-17 12:00:47 -07002527 if (outProfile->mSupportedDevices.isEmpty()) {
2528 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2529 continue;
2530 }
2531
Eric Laurentd78f1532014-09-16 16:38:20 -07002532 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2533 continue;
2534 }
Eric Laurent83b88082014-06-20 18:31:16 -07002535 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002536 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2537 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002538 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002539 // chose first device present in mSupportedDevices also part of
2540 // outputDeviceTypes
2541 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002542 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002543 if ((profileType & outputDeviceTypes) != 0) {
2544 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002545 }
Eric Laurente552edb2014-03-10 17:42:56 -07002546 }
2547 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002548 if ((profileType & outputDeviceTypes) == 0) {
2549 continue;
2550 }
2551 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
2552
2553 outputDesc->mDevice = profileType;
2554 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2555 config.sample_rate = outputDesc->mSamplingRate;
2556 config.channel_mask = outputDesc->mChannelMask;
2557 config.format = outputDesc->mFormat;
2558 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2559 status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle,
2560 &output,
2561 &config,
2562 &outputDesc->mDevice,
2563 String8(""),
2564 &outputDesc->mLatency,
2565 outputDesc->mFlags);
2566
2567 if (status != NO_ERROR) {
2568 ALOGW("Cannot open output stream for device %08x on hw module %s",
2569 outputDesc->mDevice,
2570 mHwModules[i]->mName);
2571 } else {
2572 outputDesc->mSamplingRate = config.sample_rate;
2573 outputDesc->mChannelMask = config.channel_mask;
2574 outputDesc->mFormat = config.format;
2575
2576 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002577 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002578 ssize_t index =
2579 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2580 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002581 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2582 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002583 }
2584 }
2585 if (mPrimaryOutput == 0 &&
2586 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2587 mPrimaryOutput = output;
2588 }
2589 addOutput(output, outputDesc);
2590 setOutputDevice(output,
2591 outputDesc->mDevice,
2592 true);
2593 }
Eric Laurente552edb2014-03-10 17:42:56 -07002594 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002595 // open input streams needed to access attached devices to validate
2596 // mAvailableInputDevices list
2597 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2598 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002599 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002600
Eric Laurent3a4311c2014-03-17 12:00:47 -07002601 if (inProfile->mSupportedDevices.isEmpty()) {
2602 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2603 continue;
2604 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002605 // chose first device present in mSupportedDevices also part of
2606 // inputDeviceTypes
2607 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2608 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002609 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002610 if (profileType & inputDeviceTypes) {
2611 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002612 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002613 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002614 if ((profileType & inputDeviceTypes) == 0) {
2615 continue;
2616 }
2617 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2618
2619 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2620 inputDesc->mDevice = profileType;
2621
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002622 // find the address
2623 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2624 // the inputs vector must be of size 1, but we don't want to crash here
2625 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2626 : String8("");
2627 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2628 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2629
Eric Laurentd78f1532014-09-16 16:38:20 -07002630 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2631 config.sample_rate = inputDesc->mSamplingRate;
2632 config.channel_mask = inputDesc->mChannelMask;
2633 config.format = inputDesc->mFormat;
2634 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
2635 status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle,
2636 &input,
2637 &config,
2638 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002639 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002640 AUDIO_SOURCE_MIC,
2641 AUDIO_INPUT_FLAG_NONE);
2642
2643 if (status == NO_ERROR) {
2644 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002645 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002646 ssize_t index =
2647 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2648 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002649 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2650 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002651 }
2652 }
2653 mpClientInterface->closeInput(input);
2654 } else {
2655 ALOGW("Cannot open input stream for device %08x on hw module %s",
2656 inputDesc->mDevice,
2657 mHwModules[i]->mName);
2658 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002659 }
2660 }
2661 // make sure all attached devices have been allocated a unique ID
2662 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002663 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002664 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002665 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2666 continue;
2667 }
2668 i++;
2669 }
2670 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002671 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002672 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002673 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2674 continue;
2675 }
2676 i++;
2677 }
2678 // make sure default device is reachable
2679 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002680 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002681 }
Eric Laurente552edb2014-03-10 17:42:56 -07002682
2683 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2684
2685 updateDevicesAndOutputs();
2686
2687#ifdef AUDIO_POLICY_TEST
2688 if (mPrimaryOutput != 0) {
2689 AudioParameter outputCmd = AudioParameter();
2690 outputCmd.addInt(String8("set_id"), 0);
2691 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2692
2693 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2694 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002695 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2696 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002697 mTestLatencyMs = 0;
2698 mCurOutput = 0;
2699 mDirectOutput = false;
2700 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2701 mTestOutputs[i] = 0;
2702 }
2703
2704 const size_t SIZE = 256;
2705 char buffer[SIZE];
2706 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2707 run(buffer, ANDROID_PRIORITY_AUDIO);
2708 }
2709#endif //AUDIO_POLICY_TEST
2710}
2711
Eric Laurente0720872014-03-11 09:30:41 -07002712AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002713{
2714#ifdef AUDIO_POLICY_TEST
2715 exit();
2716#endif //AUDIO_POLICY_TEST
2717 for (size_t i = 0; i < mOutputs.size(); i++) {
2718 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002719 }
2720 for (size_t i = 0; i < mInputs.size(); i++) {
2721 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002722 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002723 mAvailableOutputDevices.clear();
2724 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002725 mOutputs.clear();
2726 mInputs.clear();
2727 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002728}
2729
Eric Laurente0720872014-03-11 09:30:41 -07002730status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002731{
2732 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2733}
2734
2735#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002736bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002737{
2738 ALOGV("entering threadLoop()");
2739 while (!exitPending())
2740 {
2741 String8 command;
2742 int valueInt;
2743 String8 value;
2744
2745 Mutex::Autolock _l(mLock);
2746 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2747
2748 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2749 AudioParameter param = AudioParameter(command);
2750
2751 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2752 valueInt != 0) {
2753 ALOGV("Test command %s received", command.string());
2754 String8 target;
2755 if (param.get(String8("target"), target) != NO_ERROR) {
2756 target = "Manager";
2757 }
2758 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2759 param.remove(String8("test_cmd_policy_output"));
2760 mCurOutput = valueInt;
2761 }
2762 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2763 param.remove(String8("test_cmd_policy_direct"));
2764 if (value == "false") {
2765 mDirectOutput = false;
2766 } else if (value == "true") {
2767 mDirectOutput = true;
2768 }
2769 }
2770 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2771 param.remove(String8("test_cmd_policy_input"));
2772 mTestInput = valueInt;
2773 }
2774
2775 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2776 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002777 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002778 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002779 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002780 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002781 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002782 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002783 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002784 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002785 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002786 if (target == "Manager") {
2787 mTestFormat = format;
2788 } else if (mTestOutputs[mCurOutput] != 0) {
2789 AudioParameter outputParam = AudioParameter();
2790 outputParam.addInt(String8("format"), format);
2791 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2792 }
2793 }
2794 }
2795 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2796 param.remove(String8("test_cmd_policy_channels"));
2797 int channels = 0;
2798
2799 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002800 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002801 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002802 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002803 }
2804 if (channels != 0) {
2805 if (target == "Manager") {
2806 mTestChannels = channels;
2807 } else if (mTestOutputs[mCurOutput] != 0) {
2808 AudioParameter outputParam = AudioParameter();
2809 outputParam.addInt(String8("channels"), channels);
2810 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2811 }
2812 }
2813 }
2814 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2815 param.remove(String8("test_cmd_policy_sampleRate"));
2816 if (valueInt >= 0 && valueInt <= 96000) {
2817 int samplingRate = valueInt;
2818 if (target == "Manager") {
2819 mTestSamplingRate = samplingRate;
2820 } else if (mTestOutputs[mCurOutput] != 0) {
2821 AudioParameter outputParam = AudioParameter();
2822 outputParam.addInt(String8("sampling_rate"), samplingRate);
2823 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2824 }
2825 }
2826 }
2827
2828 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2829 param.remove(String8("test_cmd_policy_reopen"));
2830
Eric Laurent1f2f2232014-06-02 12:01:23 -07002831 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07002832 mpClientInterface->closeOutput(mPrimaryOutput);
2833
2834 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
2835
François Gaffie53615e22015-03-19 09:24:12 +01002836 removeOutput(mPrimaryOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07002837 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07002838 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002839 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2840 config.sample_rate = outputDesc->mSamplingRate;
2841 config.channel_mask = outputDesc->mChannelMask;
2842 config.format = outputDesc->mFormat;
2843 status_t status = mpClientInterface->openOutput(moduleHandle,
2844 &mPrimaryOutput,
2845 &config,
2846 &outputDesc->mDevice,
2847 String8(""),
2848 &outputDesc->mLatency,
2849 outputDesc->mFlags);
2850 if (status != NO_ERROR) {
2851 ALOGE("Failed to reopen hardware output stream, "
2852 "samplingRate: %d, format %d, channels %d",
2853 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07002854 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002855 outputDesc->mSamplingRate = config.sample_rate;
2856 outputDesc->mChannelMask = config.channel_mask;
2857 outputDesc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07002858 AudioParameter outputCmd = AudioParameter();
2859 outputCmd.addInt(String8("set_id"), 0);
2860 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2861 addOutput(mPrimaryOutput, outputDesc);
2862 }
2863 }
2864
2865
2866 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2867 }
2868 }
2869 return false;
2870}
2871
Eric Laurente0720872014-03-11 09:30:41 -07002872void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002873{
2874 {
2875 AutoMutex _l(mLock);
2876 requestExit();
2877 mWaitWorkCV.signal();
2878 }
2879 requestExitAndWait();
2880}
2881
Eric Laurente0720872014-03-11 09:30:41 -07002882int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002883{
2884 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2885 if (output == mTestOutputs[i]) return i;
2886 }
2887 return 0;
2888}
2889#endif //AUDIO_POLICY_TEST
2890
2891// ---
2892
Eric Laurent1f2f2232014-06-02 12:01:23 -07002893void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002894{
François Gaffie98cc1912015-03-18 17:52:40 +01002895 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07002896 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002897 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002898}
2899
François Gaffie53615e22015-03-19 09:24:12 +01002900void AudioPolicyManager::removeOutput(audio_io_handle_t output)
2901{
2902 mOutputs.removeItem(output);
2903}
2904
Eric Laurent1f2f2232014-06-02 12:01:23 -07002905void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002906{
François Gaffie98cc1912015-03-18 17:52:40 +01002907 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07002908 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002909 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002910}
Eric Laurente552edb2014-03-10 17:42:56 -07002911
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002912void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08002913 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002914 const String8 address /*in*/,
2915 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08002916 sp<DeviceDescriptor> devDesc =
2917 desc->mProfile->mSupportedDevices.getDevice(device, address);
2918 if (devDesc != 0) {
2919 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
2920 desc->mIoHandle, address.string());
2921 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002922 }
2923}
2924
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002925status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01002926 audio_policy_dev_state_t state,
2927 SortedVector<audio_io_handle_t>& outputs,
2928 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002929{
François Gaffie53615e22015-03-19 09:24:12 +01002930 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002931 sp<AudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002932 // erase all current sample rates, formats and channel masks
2933 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07002934
Eric Laurent3b73df72014-03-11 09:06:29 -07002935 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002936 // first list already open outputs that can be routed to this device
2937 for (size_t i = 0; i < mOutputs.size(); i++) {
2938 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002939 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01002940 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002941 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2942 outputs.add(mOutputs.keyAt(i));
2943 } else {
2944 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08002945 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002946 }
Eric Laurente552edb2014-03-10 17:42:56 -07002947 }
2948 }
2949 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002950 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002951 for (size_t i = 0; i < mHwModules.size(); i++)
2952 {
2953 if (mHwModules[i]->mHandle == 0) {
2954 continue;
2955 }
2956 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2957 {
Eric Laurent275e8e92014-11-30 15:14:47 -08002958 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
2959 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01002960 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08002961 address == profile->mSupportedDevices[0]->mAddress) {
2962 profiles.add(profile);
2963 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
2964 }
Eric Laurente552edb2014-03-10 17:42:56 -07002965 }
2966 }
2967 }
2968
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002969 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
2970
Eric Laurente552edb2014-03-10 17:42:56 -07002971 if (profiles.isEmpty() && outputs.isEmpty()) {
2972 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2973 return BAD_VALUE;
2974 }
2975
2976 // open outputs for matching profiles if needed. Direct outputs are also opened to
2977 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2978 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002979 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002980
2981 // nothing to do if one output is already opened for this profile
2982 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002983 for (j = 0; j < outputs.size(); j++) {
2984 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07002985 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002986 // matching profile: save the sample rates, format and channel masks supported
2987 // by the profile in our device descriptor
2988 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07002989 break;
2990 }
2991 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002992 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002993 continue;
2994 }
2995
Eric Laurent83b88082014-06-20 18:31:16 -07002996 ALOGV("opening output for device %08x with params %s profile %p",
2997 device, address.string(), profile.get());
Eric Laurente552edb2014-03-10 17:42:56 -07002998 desc = new AudioOutputDescriptor(profile);
2999 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003000 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3001 config.sample_rate = desc->mSamplingRate;
3002 config.channel_mask = desc->mChannelMask;
3003 config.format = desc->mFormat;
3004 config.offload_info.sample_rate = desc->mSamplingRate;
3005 config.offload_info.channel_mask = desc->mChannelMask;
3006 config.offload_info.format = desc->mFormat;
3007 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
3008 status_t status = mpClientInterface->openOutput(profile->mModule->mHandle,
3009 &output,
3010 &config,
3011 &desc->mDevice,
3012 address,
3013 &desc->mLatency,
3014 desc->mFlags);
3015 if (status == NO_ERROR) {
3016 desc->mSamplingRate = config.sample_rate;
3017 desc->mChannelMask = config.channel_mask;
3018 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003019
Eric Laurentd4692962014-05-05 18:13:44 -07003020 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003021 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003022 char *param = audio_device_address_to_parameter(device, address);
3023 mpClientInterface->setParameters(output, String8(param));
3024 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003025 }
3026
Eric Laurentd4692962014-05-05 18:13:44 -07003027 // Here is where we step through and resolve any "dynamic" fields
3028 String8 reply;
3029 char *value;
3030 if (profile->mSamplingRates[0] == 0) {
3031 reply = mpClientInterface->getParameters(output,
3032 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003033 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003034 reply.string());
3035 value = strpbrk((char *)reply.string(), "=");
3036 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003037 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003038 }
Eric Laurentd4692962014-05-05 18:13:44 -07003039 }
3040 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3041 reply = mpClientInterface->getParameters(output,
3042 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003043 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003044 reply.string());
3045 value = strpbrk((char *)reply.string(), "=");
3046 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003047 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003048 }
Eric Laurentd4692962014-05-05 18:13:44 -07003049 }
3050 if (profile->mChannelMasks[0] == 0) {
3051 reply = mpClientInterface->getParameters(output,
3052 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003053 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003054 reply.string());
3055 value = strpbrk((char *)reply.string(), "=");
3056 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003057 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003058 }
Eric Laurentd4692962014-05-05 18:13:44 -07003059 }
3060 if (((profile->mSamplingRates[0] == 0) &&
3061 (profile->mSamplingRates.size() < 2)) ||
3062 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3063 (profile->mFormats.size() < 2)) ||
3064 ((profile->mChannelMasks[0] == 0) &&
3065 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003066 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003067 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003068 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003069 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3070 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003071 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003072 config.sample_rate = profile->pickSamplingRate();
3073 config.channel_mask = profile->pickChannelMask();
3074 config.format = profile->pickFormat();
3075 config.offload_info.sample_rate = config.sample_rate;
3076 config.offload_info.channel_mask = config.channel_mask;
3077 config.offload_info.format = config.format;
3078 status = mpClientInterface->openOutput(profile->mModule->mHandle,
3079 &output,
3080 &config,
3081 &desc->mDevice,
3082 address,
3083 &desc->mLatency,
3084 desc->mFlags);
3085 if (status == NO_ERROR) {
3086 desc->mSamplingRate = config.sample_rate;
3087 desc->mChannelMask = config.channel_mask;
3088 desc->mFormat = config.format;
3089 } else {
3090 output = AUDIO_IO_HANDLE_NONE;
3091 }
Eric Laurentd4692962014-05-05 18:13:44 -07003092 }
3093
Eric Laurentcf2c0212014-07-25 16:20:43 -07003094 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003095 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003096 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003097 sp<AudioPolicyMix> policyMix;
3098 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003099 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3100 address.string());
3101 }
François Gaffie036e1e92015-03-19 10:16:24 +01003102 policyMix->setOutput(desc);
3103 desc->mPolicyMix = &(policyMix->getMix());
3104
Eric Laurentc722f302014-12-10 11:21:49 -08003105 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3106 // no duplicated output for direct outputs and
3107 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003108 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003109
Eric Laurentd4692962014-05-05 18:13:44 -07003110 // set initial stream volume for device
3111 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003112
Eric Laurentd4692962014-05-05 18:13:44 -07003113 //TODO: configure audio effect output stage here
3114
3115 // open a duplicating output thread for the new output and the primary output
3116 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
3117 mPrimaryOutput);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003118 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003119 // add duplicated output descriptor
Eric Laurentcf2c0212014-07-25 16:20:43 -07003120 sp<AudioOutputDescriptor> dupOutputDesc =
3121 new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07003122 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
3123 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
3124 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3125 dupOutputDesc->mFormat = desc->mFormat;
3126 dupOutputDesc->mChannelMask = desc->mChannelMask;
3127 dupOutputDesc->mLatency = desc->mLatency;
3128 addOutput(duplicatedOutput, dupOutputDesc);
3129 applyStreamVolumes(duplicatedOutput, device, 0, true);
3130 } else {
3131 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
3132 mPrimaryOutput, output);
3133 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003134 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003135 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003136 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003137 }
Eric Laurente552edb2014-03-10 17:42:56 -07003138 }
3139 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003140 } else {
3141 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003142 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003143 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003144 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003145 profiles.removeAt(profile_index);
3146 profile_index--;
3147 } else {
3148 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003149 devDesc->importAudioPort(profile);
3150
François Gaffie53615e22015-03-19 09:24:12 +01003151 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003152 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3153 device, address.string());
3154 setOutputDevice(output, device, true/*force*/, 0/*delay*/,
3155 NULL/*patch handle*/, address.string());
3156 }
Eric Laurente552edb2014-03-10 17:42:56 -07003157 ALOGV("checkOutputsForDevice(): adding output %d", output);
3158 }
3159 }
3160
3161 if (profiles.isEmpty()) {
3162 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3163 return BAD_VALUE;
3164 }
Eric Laurentd4692962014-05-05 18:13:44 -07003165 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003166 // check if one opened output is not needed any more after disconnecting one device
3167 for (size_t i = 0; i < mOutputs.size(); i++) {
3168 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003169 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003170 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003171 if (device_distinguishes_on_address(device) &&
Eric Laurent275e8e92014-11-30 15:14:47 -08003172 (desc->mProfile->mSupportedDevices.types() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003173 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurent275e8e92014-11-30 15:14:47 -08003174 } else if (!(desc->mProfile->mSupportedDevices.types()
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003175 & mAvailableOutputDevices.types())) {
3176 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3177 mOutputs.keyAt(i));
3178 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003179 }
Eric Laurente552edb2014-03-10 17:42:56 -07003180 }
3181 }
Eric Laurentd4692962014-05-05 18:13:44 -07003182 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003183 for (size_t i = 0; i < mHwModules.size(); i++)
3184 {
3185 if (mHwModules[i]->mHandle == 0) {
3186 continue;
3187 }
3188 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3189 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003190 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003191 if (profile->mSupportedDevices.types() & device) {
3192 ALOGV("checkOutputsForDevice(): "
3193 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003194 if (profile->mSamplingRates[0] == 0) {
3195 profile->mSamplingRates.clear();
3196 profile->mSamplingRates.add(0);
3197 }
3198 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3199 profile->mFormats.clear();
3200 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3201 }
3202 if (profile->mChannelMasks[0] == 0) {
3203 profile->mChannelMasks.clear();
3204 profile->mChannelMasks.add(0);
3205 }
3206 }
3207 }
3208 }
3209 }
3210 return NO_ERROR;
3211}
3212
Eric Laurentd4692962014-05-05 18:13:44 -07003213status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003214 audio_policy_dev_state_t state,
3215 SortedVector<audio_io_handle_t>& inputs,
3216 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003217{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003218 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003219 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3220 // first list already open inputs that can be routed to this device
3221 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3222 desc = mInputs.valueAt(input_index);
3223 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3224 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3225 inputs.add(mInputs.keyAt(input_index));
3226 }
3227 }
3228
3229 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003230 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003231 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3232 {
3233 if (mHwModules[module_idx]->mHandle == 0) {
3234 continue;
3235 }
3236 for (size_t profile_index = 0;
3237 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3238 profile_index++)
3239 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003240 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3241
3242 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003243 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003244 address == profile->mSupportedDevices[0]->mAddress) {
3245 profiles.add(profile);
3246 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3247 profile_index, module_idx);
3248 }
Eric Laurentd4692962014-05-05 18:13:44 -07003249 }
3250 }
3251 }
3252
3253 if (profiles.isEmpty() && inputs.isEmpty()) {
3254 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3255 return BAD_VALUE;
3256 }
3257
3258 // open inputs for matching profiles if needed. Direct inputs are also opened to
3259 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3260 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3261
Eric Laurent1c333e22014-05-20 10:48:17 -07003262 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003263 // nothing to do if one input is already opened for this profile
3264 size_t input_index;
3265 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3266 desc = mInputs.valueAt(input_index);
3267 if (desc->mProfile == profile) {
3268 break;
3269 }
3270 }
3271 if (input_index != mInputs.size()) {
3272 continue;
3273 }
3274
3275 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3276 desc = new AudioInputDescriptor(profile);
3277 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003278 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3279 config.sample_rate = desc->mSamplingRate;
3280 config.channel_mask = desc->mChannelMask;
3281 config.format = desc->mFormat;
3282 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3283 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
3284 &input,
3285 &config,
3286 &desc->mDevice,
3287 address,
3288 AUDIO_SOURCE_MIC,
3289 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003290
Eric Laurentcf2c0212014-07-25 16:20:43 -07003291 if (status == NO_ERROR) {
3292 desc->mSamplingRate = config.sample_rate;
3293 desc->mChannelMask = config.channel_mask;
3294 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003295
Eric Laurentd4692962014-05-05 18:13:44 -07003296 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003297 char *param = audio_device_address_to_parameter(device, address);
3298 mpClientInterface->setParameters(input, String8(param));
3299 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003300 }
3301
3302 // Here is where we step through and resolve any "dynamic" fields
3303 String8 reply;
3304 char *value;
3305 if (profile->mSamplingRates[0] == 0) {
3306 reply = mpClientInterface->getParameters(input,
3307 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3308 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3309 reply.string());
3310 value = strpbrk((char *)reply.string(), "=");
3311 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003312 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003313 }
3314 }
3315 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3316 reply = mpClientInterface->getParameters(input,
3317 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3318 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3319 value = strpbrk((char *)reply.string(), "=");
3320 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003321 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003322 }
3323 }
3324 if (profile->mChannelMasks[0] == 0) {
3325 reply = mpClientInterface->getParameters(input,
3326 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3327 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3328 reply.string());
3329 value = strpbrk((char *)reply.string(), "=");
3330 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003331 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003332 }
3333 }
3334 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3335 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3336 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3337 ALOGW("checkInputsForDevice() direct input missing param");
3338 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003339 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003340 }
3341
3342 if (input != 0) {
3343 addInput(input, desc);
3344 }
3345 } // endif input != 0
3346
Eric Laurentcf2c0212014-07-25 16:20:43 -07003347 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003348 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003349 profiles.removeAt(profile_index);
3350 profile_index--;
3351 } else {
3352 inputs.add(input);
3353 ALOGV("checkInputsForDevice(): adding input %d", input);
3354 }
3355 } // end scan profiles
3356
3357 if (profiles.isEmpty()) {
3358 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3359 return BAD_VALUE;
3360 }
3361 } else {
3362 // Disconnect
3363 // check if one opened input is not needed any more after disconnecting one device
3364 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3365 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003366 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3367 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003368 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3369 mInputs.keyAt(input_index));
3370 inputs.add(mInputs.keyAt(input_index));
3371 }
3372 }
3373 // Clear any profiles associated with the disconnected device.
3374 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3375 if (mHwModules[module_index]->mHandle == 0) {
3376 continue;
3377 }
3378 for (size_t profile_index = 0;
3379 profile_index < mHwModules[module_index]->mInputProfiles.size();
3380 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003381 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003382 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003383 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003384 profile_index, module_index);
3385 if (profile->mSamplingRates[0] == 0) {
3386 profile->mSamplingRates.clear();
3387 profile->mSamplingRates.add(0);
3388 }
3389 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3390 profile->mFormats.clear();
3391 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3392 }
3393 if (profile->mChannelMasks[0] == 0) {
3394 profile->mChannelMasks.clear();
3395 profile->mChannelMasks.add(0);
3396 }
3397 }
3398 }
3399 }
3400 } // end disconnect
3401
3402 return NO_ERROR;
3403}
3404
3405
Eric Laurente0720872014-03-11 09:30:41 -07003406void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003407{
3408 ALOGV("closeOutput(%d)", output);
3409
Eric Laurent1f2f2232014-06-02 12:01:23 -07003410 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003411 if (outputDesc == NULL) {
3412 ALOGW("closeOutput() unknown output %d", output);
3413 return;
3414 }
François Gaffie036e1e92015-03-19 10:16:24 +01003415 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003416
Eric Laurente552edb2014-03-10 17:42:56 -07003417 // look for duplicated outputs connected to the output being removed.
3418 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003419 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003420 if (dupOutputDesc->isDuplicated() &&
3421 (dupOutputDesc->mOutput1 == outputDesc ||
3422 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003423 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003424 if (dupOutputDesc->mOutput1 == outputDesc) {
3425 outputDesc2 = dupOutputDesc->mOutput2;
3426 } else {
3427 outputDesc2 = dupOutputDesc->mOutput1;
3428 }
3429 // As all active tracks on duplicated output will be deleted,
3430 // and as they were also referenced on the other output, the reference
3431 // count for their stream type must be adjusted accordingly on
3432 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003433 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003434 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003435 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003436 }
3437 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3438 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3439
3440 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003441 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003442 }
3443 }
3444
Eric Laurent05b90f82014-08-27 15:32:29 -07003445 nextAudioPortGeneration();
3446
3447 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3448 if (index >= 0) {
3449 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3450 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3451 mAudioPatches.removeItemsAt(index);
3452 mpClientInterface->onAudioPatchListUpdate();
3453 }
3454
Eric Laurente552edb2014-03-10 17:42:56 -07003455 AudioParameter param;
3456 param.add(String8("closing"), String8("true"));
3457 mpClientInterface->setParameters(output, param.toString());
3458
3459 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003460 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003461 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003462}
3463
3464void AudioPolicyManager::closeInput(audio_io_handle_t input)
3465{
3466 ALOGV("closeInput(%d)", input);
3467
3468 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3469 if (inputDesc == NULL) {
3470 ALOGW("closeInput() unknown input %d", input);
3471 return;
3472 }
3473
Eric Laurent6a94d692014-05-20 11:18:06 -07003474 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003475
3476 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3477 if (index >= 0) {
3478 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3479 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3480 mAudioPatches.removeItemsAt(index);
3481 mpClientInterface->onAudioPatchListUpdate();
3482 }
3483
3484 mpClientInterface->closeInput(input);
3485 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003486}
3487
Eric Laurente0720872014-03-11 09:30:41 -07003488SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003489 AudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003490{
3491 SortedVector<audio_io_handle_t> outputs;
3492
3493 ALOGVV("getOutputsForDevice() device %04x", device);
3494 for (size_t i = 0; i < openOutputs.size(); i++) {
3495 ALOGVV("output %d isDuplicated=%d device=%04x",
3496 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3497 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3498 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3499 outputs.add(openOutputs.keyAt(i));
3500 }
3501 }
3502 return outputs;
3503}
3504
Eric Laurente0720872014-03-11 09:30:41 -07003505bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003506 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003507{
3508 if (outputs1.size() != outputs2.size()) {
3509 return false;
3510 }
3511 for (size_t i = 0; i < outputs1.size(); i++) {
3512 if (outputs1[i] != outputs2[i]) {
3513 return false;
3514 }
3515 }
3516 return true;
3517}
3518
Eric Laurente0720872014-03-11 09:30:41 -07003519void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003520{
3521 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3522 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3523 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3524 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3525
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003526 // also take into account external policy-related changes: add all outputs which are
3527 // associated with policies in the "before" and "after" output vectors
3528 ALOGVV("checkOutputForStrategy(): policy related outputs");
3529 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
3530 const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
3531 if (desc != 0 && desc->mPolicyMix != NULL) {
3532 srcOutputs.add(desc->mIoHandle);
3533 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3534 }
3535 }
3536 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
3537 const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3538 if (desc != 0 && desc->mPolicyMix != NULL) {
3539 dstOutputs.add(desc->mIoHandle);
3540 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3541 }
3542 }
3543
Eric Laurente552edb2014-03-10 17:42:56 -07003544 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3545 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3546 strategy, srcOutputs[0], dstOutputs[0]);
3547 // mute strategy while moving tracks from one output to another
3548 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003549 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003550 if (isStrategyActive(desc, strategy)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003551 setStrategyMute(strategy, true, srcOutputs[i]);
3552 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3553 }
3554 }
3555
3556 // Move effects associated to this strategy from previous output to new output
3557 if (strategy == STRATEGY_MEDIA) {
3558 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3559 SortedVector<audio_io_handle_t> moved;
3560 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003561 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3562 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3563 effectDesc->mIo != fxOutput) {
3564 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003565 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3566 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003567 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003568 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003569 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003570 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003571 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003572 }
3573 }
3574 }
3575 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003576 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003577 if (i == AUDIO_STREAM_PATCH) {
3578 continue;
3579 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003580 if (getStrategy((audio_stream_type_t)i) == strategy) {
3581 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003582 }
3583 }
3584 }
3585}
3586
Eric Laurente0720872014-03-11 09:30:41 -07003587void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003588{
Jon Eklund966095e2014-09-09 15:39:49 -05003589 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
3590 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003591 checkOutputForStrategy(STRATEGY_PHONE);
Jon Eklund966095e2014-09-09 15:39:49 -05003592 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
3593 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003594 checkOutputForStrategy(STRATEGY_SONIFICATION);
3595 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003596 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003597 checkOutputForStrategy(STRATEGY_MEDIA);
3598 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003599 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003600}
3601
Eric Laurente0720872014-03-11 09:30:41 -07003602void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003603{
François Gaffie53615e22015-03-19 09:24:12 +01003604 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003605 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003606 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003607 return;
3608 }
3609
Eric Laurent3a4311c2014-03-17 12:00:47 -07003610 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003611 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3612 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3613 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003614 // suspend A2DP output if:
3615 // (NOT already suspended) &&
3616 // ((SCO device is connected &&
3617 // (forced usage for communication || for record is SCO))) ||
3618 // (phone state is ringing || in call)
3619 //
3620 // restore A2DP output if:
3621 // (Already suspended) &&
3622 // ((SCO device is NOT connected ||
3623 // (forced usage NOT for communication && NOT for record is SCO))) &&
3624 // (phone state is NOT ringing && NOT in call)
3625 //
3626 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003627 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003628 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
3629 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
3630 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
3631 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003632
3633 mpClientInterface->restoreOutput(a2dpOutput);
3634 mA2dpSuspended = false;
3635 }
3636 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003637 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003638 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
3639 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
3640 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
3641 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003642
3643 mpClientInterface->suspendOutput(a2dpOutput);
3644 mA2dpSuspended = true;
3645 }
3646 }
3647}
3648
Eric Laurent1c333e22014-05-20 10:48:17 -07003649audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003650{
3651 audio_devices_t device = AUDIO_DEVICE_NONE;
3652
Eric Laurent1f2f2232014-06-02 12:01:23 -07003653 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003654
3655 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3656 if (index >= 0) {
3657 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3658 if (patchDesc->mUid != mUidCached) {
3659 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3660 outputDesc->device(), outputDesc->mPatchHandle);
3661 return outputDesc->device();
3662 }
3663 }
3664
Eric Laurente552edb2014-03-10 17:42:56 -07003665 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003666 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003667 // use device for strategy enforced audible
3668 // 2: we are in call or the strategy phone is active on the output:
3669 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003670 // 3: the strategy for enforced audible is active but not enforced on the output:
3671 // use the device for strategy enforced audible
3672 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003673 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003674 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003675 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003676 // 6: the strategy accessibility is active on the output:
3677 // use device for strategy accessibility
3678 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003679 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003680 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003681 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003682 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003683 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003684 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
Jon Eklund966095e2014-09-09 15:39:49 -05003685 mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003686 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3687 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003688 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003689 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003690 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003691 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003692 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003693 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003694 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003695 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003696 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003697 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003698 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003699 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003700 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003701 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003702 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003703 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003704 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003705 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003706 }
3707
Eric Laurent1c333e22014-05-20 10:48:17 -07003708 ALOGV("getNewOutputDevice() selected device %x", device);
3709 return device;
3710}
3711
3712audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3713{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003714 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003715
3716 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3717 if (index >= 0) {
3718 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3719 if (patchDesc->mUid != mUidCached) {
3720 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3721 inputDesc->mDevice, inputDesc->mPatchHandle);
3722 return inputDesc->mDevice;
3723 }
3724 }
3725
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003726 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003727
3728 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003729 return device;
3730}
3731
Eric Laurente0720872014-03-11 09:30:41 -07003732uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003733 return (uint32_t)getStrategy(stream);
3734}
3735
Eric Laurente0720872014-03-11 09:30:41 -07003736audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003737 // By checking the range of stream before calling getStrategy, we avoid
3738 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3739 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003740 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003741 return AUDIO_DEVICE_NONE;
3742 }
3743 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003744 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003745 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3746 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3747 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003748 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003749 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003750 devices = outputDesc->device();
3751 break;
3752 }
Eric Laurente552edb2014-03-10 17:42:56 -07003753 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003754
3755 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3756 and doesn't really need to.*/
3757 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3758 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3759 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3760 }
3761
Eric Laurente552edb2014-03-10 17:42:56 -07003762 return devices;
3763}
3764
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003765routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07003766 audio_stream_type_t stream) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003767
3768 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
3769
Eric Laurente552edb2014-03-10 17:42:56 -07003770 // stream to strategy mapping
3771 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003772 case AUDIO_STREAM_VOICE_CALL:
3773 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003774 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07003775 case AUDIO_STREAM_RING:
3776 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07003777 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07003778 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07003779 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07003780 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07003781 return STRATEGY_DTMF;
3782 default:
Eric Laurent223fd5c2014-11-11 13:43:36 -08003783 ALOGE("unknown stream type %d", stream);
Eric Laurent3b73df72014-03-11 09:06:29 -07003784 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07003785 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
3786 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07003787 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003788 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07003789 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07003790 return STRATEGY_ENFORCED_AUDIBLE;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003791 case AUDIO_STREAM_TTS:
3792 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
Eric Laurent223fd5c2014-11-11 13:43:36 -08003793 case AUDIO_STREAM_ACCESSIBILITY:
3794 return STRATEGY_ACCESSIBILITY;
3795 case AUDIO_STREAM_REROUTING:
3796 return STRATEGY_REROUTING;
Eric Laurente552edb2014-03-10 17:42:56 -07003797 }
3798}
3799
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003800uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3801 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003802 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3803 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3804 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003805 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3806 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3807 }
3808
3809 // usage to strategy mapping
3810 switch (attr->usage) {
Eric Laurent29e6cec2014-11-13 18:17:55 -08003811 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
3812 if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) {
3813 return (uint32_t) STRATEGY_SONIFICATION;
3814 }
3815 if (isInCall()) {
3816 return (uint32_t) STRATEGY_PHONE;
3817 }
Eric Laurent0f78eab2014-11-25 11:01:34 -08003818 return (uint32_t) STRATEGY_ACCESSIBILITY;
Eric Laurent29e6cec2014-11-13 18:17:55 -08003819
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003820 case AUDIO_USAGE_MEDIA:
3821 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003822 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
3823 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
3824 return (uint32_t) STRATEGY_MEDIA;
3825
3826 case AUDIO_USAGE_VOICE_COMMUNICATION:
3827 return (uint32_t) STRATEGY_PHONE;
3828
3829 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
3830 return (uint32_t) STRATEGY_DTMF;
3831
3832 case AUDIO_USAGE_ALARM:
3833 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
3834 return (uint32_t) STRATEGY_SONIFICATION;
3835
3836 case AUDIO_USAGE_NOTIFICATION:
3837 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
3838 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
3839 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
3840 case AUDIO_USAGE_NOTIFICATION_EVENT:
3841 return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL;
3842
3843 case AUDIO_USAGE_UNKNOWN:
3844 default:
3845 return (uint32_t) STRATEGY_MEDIA;
3846 }
3847}
3848
Eric Laurente0720872014-03-11 09:30:41 -07003849void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003850 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003851 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003852 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3853 updateDevicesAndOutputs();
3854 break;
3855 default:
3856 break;
3857 }
3858}
3859
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003860uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3861 switch(event) {
3862 case STARTING_OUTPUT:
3863 mBeaconMuteRefCount++;
3864 break;
3865 case STOPPING_OUTPUT:
3866 if (mBeaconMuteRefCount > 0) {
3867 mBeaconMuteRefCount--;
3868 }
3869 break;
3870 case STARTING_BEACON:
3871 mBeaconPlayingRefCount++;
3872 break;
3873 case STOPPING_BEACON:
3874 if (mBeaconPlayingRefCount > 0) {
3875 mBeaconPlayingRefCount--;
3876 }
3877 break;
3878 }
3879
3880 if (mBeaconMuteRefCount > 0) {
3881 // any playback causes beacon to be muted
3882 return setBeaconMute(true);
3883 } else {
3884 // no other playback: unmute when beacon starts playing, mute when it stops
3885 return setBeaconMute(mBeaconPlayingRefCount == 0);
3886 }
3887}
3888
3889uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3890 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3891 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3892 // keep track of muted state to avoid repeating mute/unmute operations
3893 if (mBeaconMuted != mute) {
3894 // mute/unmute AUDIO_STREAM_TTS on all outputs
3895 ALOGV("\t muting %d", mute);
3896 uint32_t maxLatency = 0;
3897 for (size_t i = 0; i < mOutputs.size(); i++) {
3898 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3899 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
3900 desc->mIoHandle,
3901 0 /*delay*/, AUDIO_DEVICE_NONE);
3902 const uint32_t latency = desc->latency() * 2;
3903 if (latency > maxLatency) {
3904 maxLatency = latency;
3905 }
3906 }
3907 mBeaconMuted = mute;
3908 return maxLatency;
3909 }
3910 return 0;
3911}
3912
Eric Laurente0720872014-03-11 09:30:41 -07003913audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01003914 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003915{
3916 uint32_t device = AUDIO_DEVICE_NONE;
3917
3918 if (fromCache) {
3919 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3920 strategy, mDeviceForStrategy[strategy]);
3921 return mDeviceForStrategy[strategy];
3922 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003923 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07003924 switch (strategy) {
3925
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003926 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
3927 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
3928 if (!device) {
3929 ALOGE("getDeviceForStrategy() no device found for "\
3930 "STRATEGY_TRANSMITTED_THROUGH_SPEAKER");
3931 }
3932 break;
3933
Eric Laurente552edb2014-03-10 17:42:56 -07003934 case STRATEGY_SONIFICATION_RESPECTFUL:
3935 if (isInCall()) {
3936 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003937 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07003938 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
3939 // while media is playing on a remote device, use the the sonification behavior.
3940 // Note that we test this usecase before testing if media is playing because
3941 // the isStreamActive() method only informs about the activity of a stream, not
3942 // if it's for local playback. Note also that we use the same delay between both tests
3943 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05003944 //user "safe" speaker if available instead of normal speaker to avoid triggering
3945 //other acoustic safety mechanisms for notification
3946 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3947 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurent3b73df72014-03-11 09:06:29 -07003948 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003949 // while media is playing (or has recently played), use the same device
3950 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3951 } else {
3952 // when media is not playing anymore, fall back on the sonification behavior
3953 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05003954 //user "safe" speaker if available instead of normal speaker to avoid triggering
3955 //other acoustic safety mechanisms for notification
3956 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3957 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurente552edb2014-03-10 17:42:56 -07003958 }
3959
3960 break;
3961
3962 case STRATEGY_DTMF:
3963 if (!isInCall()) {
3964 // when off call, DTMF strategy follows the same rules as MEDIA strategy
3965 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3966 break;
3967 }
3968 // when in call, DTMF and PHONE strategies follow the same rules
3969 // FALL THROUGH
3970
3971 case STRATEGY_PHONE:
Eric Laurentc2730ba2014-07-20 15:47:07 -07003972 // Force use of only devices on primary output if:
3973 // - in call AND
3974 // - cannot route from voice call RX OR
3975 // - audio HAL version is < 3.0 and TX device is on the primary HW module
3976 if (mPhoneState == AUDIO_MODE_IN_CALL) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003977 audio_devices_t txDevice =
3978 getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -07003979 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
3980 if (((mAvailableInputDevices.types() &
3981 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
3982 (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Marco Nelissen961ec212014-08-25 15:58:39 -07003983 (hwOutputDesc->getAudioPort()->mModule->mHalVersion <
Eric Laurentc2730ba2014-07-20 15:47:07 -07003984 AUDIO_DEVICE_API_VERSION_3_0))) {
3985 availableOutputDeviceTypes = availablePrimaryOutputDevices();
3986 }
3987 }
Eric Laurente552edb2014-03-10 17:42:56 -07003988 // for phone strategy, we first consider the forced use and then the available devices by order
3989 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07003990 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
3991 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003992 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003993 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003994 if (device) break;
3995 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003996 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003997 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003998 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07003999 if (device) break;
4000 // if SCO device is requested but no SCO device is available, fall back to default case
4001 // FALL THROUGH
4002
4003 default: // FORCE_NONE
4004 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07004005 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004006 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
François Gaffie53615e22015-03-19 09:24:12 +01004007 (mOutputs.getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004008 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004009 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004010 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004011 if (device) break;
4012 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004013 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004014 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004015 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004016 if (device) break;
Eric Laurentc2730ba2014-07-20 15:47:07 -07004017 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
4018 if (device) break;
Eric Laurentacc930d2015-03-04 10:08:29 -08004019 if (!isInCall()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004020 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004021 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004022 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004023 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004024 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004025 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004026 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004027 if (device) break;
4028 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004029 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07004030 if (device) break;
François Gaffie53615e22015-03-19 09:24:12 +01004031 device = mDefaultOutputDevice->type();
Eric Laurente552edb2014-03-10 17:42:56 -07004032 if (device == AUDIO_DEVICE_NONE) {
4033 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
4034 }
4035 break;
4036
Eric Laurent3b73df72014-03-11 09:06:29 -07004037 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07004038 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
4039 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07004040 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004041 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
François Gaffie53615e22015-03-19 09:24:12 +01004042 (mOutputs.getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004043 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004044 if (device) break;
4045 }
Eric Laurentcd71a692014-12-16 12:01:12 -08004046 if (!isInCall()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004047 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004048 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004049 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004050 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004051 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004052 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004053 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004054 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004055 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004056 if (device) break;
4057 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004058 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4059 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004060 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004061 if (device) break;
François Gaffie53615e22015-03-19 09:24:12 +01004062 device = mDefaultOutputDevice->type();
Eric Laurente552edb2014-03-10 17:42:56 -07004063 if (device == AUDIO_DEVICE_NONE) {
4064 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
4065 }
4066 break;
4067 }
4068 break;
4069
4070 case STRATEGY_SONIFICATION:
4071
4072 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
4073 // handleIncallSonification().
4074 if (isInCall()) {
4075 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
4076 break;
4077 }
4078 // FALL THROUGH
4079
4080 case STRATEGY_ENFORCED_AUDIBLE:
4081 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
4082 // except:
4083 // - when in call where it doesn't default to STRATEGY_PHONE behavior
4084 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
4085
4086 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004087 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004088 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004089 if (device == AUDIO_DEVICE_NONE) {
4090 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
4091 }
4092 }
4093 // The second device used for sonification is the same as the device used by media strategy
4094 // FALL THROUGH
4095
Eric Laurent223fd5c2014-11-11 13:43:36 -08004096 // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now
4097 case STRATEGY_ACCESSIBILITY:
Eric Laurent066ceec2014-11-25 12:35:01 -08004098 if (strategy == STRATEGY_ACCESSIBILITY) {
4099 // do not route accessibility prompts to a digital output currently configured with a
4100 // compressed format as they would likely not be mixed and dropped.
4101 for (size_t i = 0; i < mOutputs.size(); i++) {
4102 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4103 audio_devices_t devices = desc->device() &
4104 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
4105 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
4106 devices != AUDIO_DEVICE_NONE) {
4107 availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices;
4108 }
4109 }
4110 }
4111 // FALL THROUGH
4112
Eric Laurent223fd5c2014-11-11 13:43:36 -08004113 case STRATEGY_REROUTING:
Eric Laurente552edb2014-03-10 17:42:56 -07004114 case STRATEGY_MEDIA: {
4115 uint32_t device2 = AUDIO_DEVICE_NONE;
4116 if (strategy != STRATEGY_SONIFICATION) {
4117 // no sonification on remote submix (e.g. WFD)
Eric Laurent275e8e92014-11-30 15:14:47 -08004118 if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
4119 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
4120 }
Eric Laurente552edb2014-03-10 17:42:56 -07004121 }
4122 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004123 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
François Gaffie53615e22015-03-19 09:24:12 +01004124 (mOutputs.getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004125 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004126 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004127 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004128 }
4129 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004130 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004131 }
4132 }
Hochi Huang327cb702014-09-21 09:47:31 +08004133 if ((device2 == AUDIO_DEVICE_NONE) &&
4134 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
4135 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4136 }
Eric Laurente552edb2014-03-10 17:42:56 -07004137 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004138 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004139 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004140 if ((device2 == AUDIO_DEVICE_NONE)) {
4141 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4142 }
Eric Laurente552edb2014-03-10 17:42:56 -07004143 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004144 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004145 }
4146 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004147 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004148 }
4149 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004150 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004151 }
4152 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004153 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004154 }
4155 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
4156 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07004157 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004158 }
4159 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004160 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004161 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004162 }
4163 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004164 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004165 }
Jungshik Jang839e4f32014-06-26 17:23:40 +09004166 int device3 = AUDIO_DEVICE_NONE;
4167 if (strategy == STRATEGY_MEDIA) {
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004168 // ARC, SPDIF and AUX_LINE can co-exist with others.
Jungshik Jang0c943092014-07-08 22:11:24 +09004169 device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC;
4170 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004171 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE);
Jungshik Jang839e4f32014-06-26 17:23:40 +09004172 }
Eric Laurente552edb2014-03-10 17:42:56 -07004173
Jungshik Jang839e4f32014-06-26 17:23:40 +09004174 device2 |= device3;
Eric Laurente552edb2014-03-10 17:42:56 -07004175 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
4176 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
4177 device |= device2;
Jungshik Jang839e4f32014-06-26 17:23:40 +09004178
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004179 // If hdmi system audio mode is on, remove speaker out of output list.
4180 if ((strategy == STRATEGY_MEDIA) &&
4181 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
4182 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
4183 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
4184 }
4185
Eric Laurente552edb2014-03-10 17:42:56 -07004186 if (device) break;
François Gaffie53615e22015-03-19 09:24:12 +01004187 device = mDefaultOutputDevice->type();
Eric Laurente552edb2014-03-10 17:42:56 -07004188 if (device == AUDIO_DEVICE_NONE) {
4189 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
4190 }
4191 } break;
4192
4193 default:
4194 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
4195 break;
4196 }
4197
4198 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
4199 return device;
4200}
4201
Eric Laurente0720872014-03-11 09:30:41 -07004202void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004203{
4204 for (int i = 0; i < NUM_STRATEGIES; i++) {
4205 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4206 }
4207 mPreviousOutputs = mOutputs;
4208}
4209
Eric Laurent1f2f2232014-06-02 12:01:23 -07004210uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004211 audio_devices_t prevDevice,
4212 uint32_t delayMs)
4213{
4214 // mute/unmute strategies using an incompatible device combination
4215 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4216 // if unmuting, unmute only after the specified delay
4217 if (outputDesc->isDuplicated()) {
4218 return 0;
4219 }
4220
4221 uint32_t muteWaitMs = 0;
4222 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004223 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004224
4225 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4226 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurent31551f82014-10-10 18:21:56 -07004227 curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004228 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4229 bool doMute = false;
4230
4231 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4232 doMute = true;
4233 outputDesc->mStrategyMutedByDevice[i] = true;
4234 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4235 doMute = true;
4236 outputDesc->mStrategyMutedByDevice[i] = false;
4237 }
Eric Laurent99401132014-05-07 19:48:15 -07004238 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004239 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004240 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004241 // skip output if it does not share any device with current output
4242 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4243 == AUDIO_DEVICE_NONE) {
4244 continue;
4245 }
4246 audio_io_handle_t curOutput = mOutputs.keyAt(j);
4247 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
4248 mute ? "muting" : "unmuting", i, curDevice, curOutput);
4249 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004250 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004251 if (mute) {
4252 // FIXME: should not need to double latency if volume could be applied
4253 // immediately by the audioflinger mixer. We must account for the delay
4254 // between now and the next time the audioflinger thread for this output
4255 // will process a buffer (which corresponds to one buffer size,
4256 // usually 1/2 or 1/4 of the latency).
4257 if (muteWaitMs < desc->latency() * 2) {
4258 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004259 }
4260 }
4261 }
4262 }
4263 }
4264 }
4265
Eric Laurent99401132014-05-07 19:48:15 -07004266 // temporary mute output if device selection changes to avoid volume bursts due to
4267 // different per device volumes
4268 if (outputDesc->isActive() && (device != prevDevice)) {
4269 if (muteWaitMs < outputDesc->latency() * 2) {
4270 muteWaitMs = outputDesc->latency() * 2;
4271 }
4272 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004273 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004274 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07004275 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07004276 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07004277 muteWaitMs *2, device);
4278 }
4279 }
4280 }
4281
Eric Laurente552edb2014-03-10 17:42:56 -07004282 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4283 if (muteWaitMs > delayMs) {
4284 muteWaitMs -= delayMs;
4285 usleep(muteWaitMs * 1000);
4286 return muteWaitMs;
4287 }
4288 return 0;
4289}
4290
Eric Laurente0720872014-03-11 09:30:41 -07004291uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004292 audio_devices_t device,
4293 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004294 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004295 audio_patch_handle_t *patchHandle,
4296 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004297{
4298 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004299 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004300 AudioParameter param;
4301 uint32_t muteWaitMs;
4302
4303 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004304 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
4305 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004306 return muteWaitMs;
4307 }
4308 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4309 // output profile
4310 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004311 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004312 return 0;
4313 }
4314
4315 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07004316 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004317
4318 audio_devices_t prevDevice = outputDesc->mDevice;
4319
4320 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
4321
4322 if (device != AUDIO_DEVICE_NONE) {
4323 outputDesc->mDevice = device;
4324 }
4325 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4326
4327 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004328 // the requested device is AUDIO_DEVICE_NONE
4329 // OR the requested device is the same as current device
4330 // AND force is not specified
4331 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004332 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentb80a2a82014-10-27 16:07:59 -07004333 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force &&
4334 outputDesc->mPatchHandle != 0) {
4335 ALOGV("setOutputDevice() setting same device %04x or null device for output %d",
4336 device, output);
Eric Laurente552edb2014-03-10 17:42:56 -07004337 return muteWaitMs;
4338 }
4339
4340 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004341
Eric Laurente552edb2014-03-10 17:42:56 -07004342 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004343 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004344 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004345 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004346 DeviceVector deviceList = (address == NULL) ?
4347 mAvailableOutputDevices.getDevicesFromType(device)
4348 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004349 if (!deviceList.isEmpty()) {
4350 struct audio_patch patch;
4351 outputDesc->toAudioPortConfig(&patch.sources[0]);
4352 patch.num_sources = 1;
4353 patch.num_sinks = 0;
4354 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4355 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004356 patch.num_sinks++;
4357 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004358 ssize_t index;
4359 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4360 index = mAudioPatches.indexOfKey(*patchHandle);
4361 } else {
4362 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4363 }
4364 sp< AudioPatch> patchDesc;
4365 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4366 if (index >= 0) {
4367 patchDesc = mAudioPatches.valueAt(index);
4368 afPatchHandle = patchDesc->mAfPatchHandle;
4369 }
4370
Eric Laurent1c333e22014-05-20 10:48:17 -07004371 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004372 &afPatchHandle,
4373 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004374 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4375 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004376 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004377 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004378 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004379 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004380 addAudioPatch(patchDesc->mHandle, patchDesc);
4381 } else {
4382 patchDesc->mPatch = patch;
4383 }
4384 patchDesc->mAfPatchHandle = afPatchHandle;
4385 patchDesc->mUid = mUidCached;
4386 if (patchHandle) {
4387 *patchHandle = patchDesc->mHandle;
4388 }
4389 outputDesc->mPatchHandle = patchDesc->mHandle;
4390 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004391 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004392 }
4393 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004394
4395 // inform all input as well
4396 for (size_t i = 0; i < mInputs.size(); i++) {
4397 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004398 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004399 AudioParameter inputCmd = AudioParameter();
4400 ALOGV("%s: inform input %d of device:%d", __func__,
4401 inputDescriptor->mIoHandle, device);
4402 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4403 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4404 inputCmd.toString(),
4405 delayMs);
4406 }
4407 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004408 }
Eric Laurente552edb2014-03-10 17:42:56 -07004409
4410 // update stream volumes according to new device
4411 applyStreamVolumes(output, device, delayMs);
4412
4413 return muteWaitMs;
4414}
4415
Eric Laurent1c333e22014-05-20 10:48:17 -07004416status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07004417 int delayMs,
4418 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004419{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004420 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004421 ssize_t index;
4422 if (patchHandle) {
4423 index = mAudioPatches.indexOfKey(*patchHandle);
4424 } else {
4425 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4426 }
4427 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004428 return INVALID_OPERATION;
4429 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004430 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4431 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004432 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4433 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004434 removeAudioPatch(patchDesc->mHandle);
4435 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004436 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004437 return status;
4438}
4439
4440status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4441 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004442 bool force,
4443 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004444{
4445 status_t status = NO_ERROR;
4446
Eric Laurent1f2f2232014-06-02 12:01:23 -07004447 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004448 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4449 inputDesc->mDevice = device;
4450
4451 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4452 if (!deviceList.isEmpty()) {
4453 struct audio_patch patch;
4454 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004455 // AUDIO_SOURCE_HOTWORD is for internal use only:
4456 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004457 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4458 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004459 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4460 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004461 patch.num_sinks = 1;
4462 //only one input device for now
4463 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004464 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004465 ssize_t index;
4466 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4467 index = mAudioPatches.indexOfKey(*patchHandle);
4468 } else {
4469 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4470 }
4471 sp< AudioPatch> patchDesc;
4472 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4473 if (index >= 0) {
4474 patchDesc = mAudioPatches.valueAt(index);
4475 afPatchHandle = patchDesc->mAfPatchHandle;
4476 }
4477
Eric Laurent1c333e22014-05-20 10:48:17 -07004478 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004479 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004480 0);
4481 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004482 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004483 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004484 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004485 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004486 addAudioPatch(patchDesc->mHandle, patchDesc);
4487 } else {
4488 patchDesc->mPatch = patch;
4489 }
4490 patchDesc->mAfPatchHandle = afPatchHandle;
4491 patchDesc->mUid = mUidCached;
4492 if (patchHandle) {
4493 *patchHandle = patchDesc->mHandle;
4494 }
4495 inputDesc->mPatchHandle = patchDesc->mHandle;
4496 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004497 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004498 }
4499 }
4500 }
4501 return status;
4502}
4503
Eric Laurent6a94d692014-05-20 11:18:06 -07004504status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4505 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004506{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004507 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004508 ssize_t index;
4509 if (patchHandle) {
4510 index = mAudioPatches.indexOfKey(*patchHandle);
4511 } else {
4512 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4513 }
4514 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004515 return INVALID_OPERATION;
4516 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004517 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4518 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004519 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4520 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004521 removeAudioPatch(patchDesc->mHandle);
4522 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004523 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004524 return status;
4525}
4526
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004527sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004528 String8 address,
4529 uint32_t& samplingRate,
4530 audio_format_t format,
4531 audio_channel_mask_t channelMask,
4532 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004533{
4534 // Choose an input profile based on the requested capture parameters: select the first available
4535 // profile supporting all requested parameters.
4536
4537 for (size_t i = 0; i < mHwModules.size(); i++)
4538 {
4539 if (mHwModules[i]->mHandle == 0) {
4540 continue;
4541 }
4542 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4543 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004544 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004545 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004546 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004547 &samplingRate /*updatedSamplingRate*/,
4548 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004549
Eric Laurente552edb2014-03-10 17:42:56 -07004550 return profile;
4551 }
4552 }
4553 }
4554 return NULL;
4555}
4556
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004557
4558audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004559 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004560{
François Gaffie036e1e92015-03-19 10:16:24 +01004561 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4562 audio_devices_t selectedDeviceFromMix =
4563 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004564
François Gaffie036e1e92015-03-19 10:16:24 +01004565 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4566 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004567 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004568 return getDeviceForInputSource(inputSource);
4569}
4570
4571audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4572{
4573 uint32_t device = AUDIO_DEVICE_NONE;
4574 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
4575 ~AUDIO_DEVICE_BIT_IN;
4576
Eric Laurente552edb2014-03-10 17:42:56 -07004577 switch (inputSource) {
4578 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07004579 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004580 device = AUDIO_DEVICE_IN_VOICE_CALL;
4581 break;
4582 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07004583 break;
Eric Laurente552edb2014-03-10 17:42:56 -07004584
4585 case AUDIO_SOURCE_DEFAULT:
4586 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07004587 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
4588 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
Eric Laurentdc136ff2014-12-16 12:24:18 -08004589 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
4590 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
4591 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurentc2730ba2014-07-20 15:47:07 -07004592 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
4593 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
4594 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
4595 device = AUDIO_DEVICE_IN_USB_DEVICE;
4596 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
4597 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Mike Lockwood41b0e242014-05-13 15:23:35 -07004598 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07004599 break;
4600
4601 case AUDIO_SOURCE_VOICE_COMMUNICATION:
4602 // Allow only use of devices on primary input if in call and HAL does not support routing
4603 // to voice call path.
4604 if ((mPhoneState == AUDIO_MODE_IN_CALL) &&
4605 (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
4606 availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN;
4607 }
4608
4609 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
4610 case AUDIO_POLICY_FORCE_BT_SCO:
4611 // if SCO device is requested but no SCO device is available, fall back to default case
4612 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
4613 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
4614 break;
4615 }
4616 // FALL THROUGH
4617
4618 default: // FORCE_NONE
4619 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
4620 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
4621 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
4622 device = AUDIO_DEVICE_IN_USB_DEVICE;
4623 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
4624 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
4625 }
4626 break;
4627
4628 case AUDIO_POLICY_FORCE_SPEAKER:
4629 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
4630 device = AUDIO_DEVICE_IN_BACK_MIC;
4631 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
4632 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
4633 }
4634 break;
4635 }
4636 break;
Mike Lockwood41b0e242014-05-13 15:23:35 -07004637
Eric Laurente552edb2014-03-10 17:42:56 -07004638 case AUDIO_SOURCE_VOICE_RECOGNITION:
4639 case AUDIO_SOURCE_HOTWORD:
Eric Laurent3b73df72014-03-11 09:06:29 -07004640 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004641 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07004642 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004643 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07004644 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07004645 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
4646 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004647 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07004648 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
4649 }
4650 break;
4651 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07004652 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07004653 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004654 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07004655 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
4656 }
4657 break;
4658 case AUDIO_SOURCE_VOICE_DOWNLINK:
4659 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07004660 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004661 device = AUDIO_DEVICE_IN_VOICE_CALL;
4662 }
4663 break;
4664 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07004665 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07004666 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
4667 }
4668 break;
Hochi Huang327cb702014-09-21 09:47:31 +08004669 case AUDIO_SOURCE_FM_TUNER:
4670 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
4671 device = AUDIO_DEVICE_IN_FM_TUNER;
4672 }
4673 break;
Eric Laurente552edb2014-03-10 17:42:56 -07004674 default:
4675 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
4676 break;
4677 }
4678 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
4679 return device;
4680}
4681
Eric Laurente0720872014-03-11 09:30:41 -07004682void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07004683{
4684 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004685 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004686 mStreams[i].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004687 ApmGains::sVolumeProfiles[i][j];
Eric Laurente552edb2014-03-10 17:42:56 -07004688 }
4689 }
4690
4691 // Check availability of DRC on speaker path: if available, override some of the speaker curves
4692 if (mSpeakerDrcEnabled) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004693 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4694 ApmGains::sDefaultSystemVolumeCurveDrc;
4695 mStreams[AUDIO_STREAM_RING].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4696 ApmGains::sSpeakerSonificationVolumeCurveDrc;
4697 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4698 ApmGains::sSpeakerSonificationVolumeCurveDrc;
4699 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4700 ApmGains::sSpeakerSonificationVolumeCurveDrc;
4701 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4702 ApmGains::sSpeakerMediaVolumeCurveDrc;
4703 mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
4704 ApmGains::sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07004705 }
4706}
4707
Eric Laurente0720872014-03-11 09:30:41 -07004708float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004709 int index,
4710 audio_io_handle_t output,
4711 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004712{
4713 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004714 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004715 StreamDescriptor &streamDesc = mStreams[stream];
4716
4717 if (device == AUDIO_DEVICE_NONE) {
4718 device = outputDesc->device();
4719 }
4720
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004721 volume = ApmGains::volIndexToAmpl(device, streamDesc, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004722
4723 // if a headset is connected, apply the following rules to ring tones and notifications
4724 // to avoid sound level bursts in user's ears:
4725 // - always attenuate ring tones and notifications volume by 6dB
4726 // - if music is playing, always limit the volume to current music volume,
4727 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004728 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004729 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4730 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4731 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4732 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4733 ((stream_strategy == STRATEGY_SONIFICATION)
4734 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004735 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004736 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004737 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07004738 streamDesc.mCanBeMuted) {
4739 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4740 // when the phone is ringing we must consider that music could have been paused just before
4741 // by the music application and behave as if music was active if the last music track was
4742 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004743 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004744 mLimitRingtoneVolume) {
4745 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004746 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
4747 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004748 output,
4749 musicDevice);
4750 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4751 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4752 if (volume > minVol) {
4753 volume = minVol;
4754 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4755 }
4756 }
4757 }
4758
4759 return volume;
4760}
4761
Eric Laurente0720872014-03-11 09:30:41 -07004762status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004763 int index,
4764 audio_io_handle_t output,
4765 audio_devices_t device,
4766 int delayMs,
4767 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004768{
4769
4770 // do not change actual stream volume if the stream is muted
4771 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
4772 ALOGVV("checkAndSetVolume() stream %d muted count %d",
4773 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
4774 return NO_ERROR;
4775 }
4776
4777 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07004778 if ((stream == AUDIO_STREAM_VOICE_CALL &&
4779 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4780 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
4781 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004782 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07004783 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07004784 return INVALID_OPERATION;
4785 }
4786
4787 float volume = computeVolume(stream, index, output, device);
Eric Laurent275e8e92014-11-30 15:14:47 -08004788 // unit gain if rerouting to external policy
4789 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
4790 ssize_t index = mOutputs.indexOfKey(output);
4791 if (index >= 0) {
4792 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurentc722f302014-12-10 11:21:49 -08004793 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004794 ALOGV("max gain when rerouting for output=%d", output);
4795 volume = 1.0f;
4796 }
4797 }
4798
4799 }
Eric Laurente552edb2014-03-10 17:42:56 -07004800 // We actually change the volume if:
4801 // - the float value returned by computeVolume() changed
4802 // - the force flag is set
4803 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
4804 force) {
4805 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
4806 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
4807 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
4808 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07004809 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
4810 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004811 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004812 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004813 }
4814
Eric Laurent3b73df72014-03-11 09:06:29 -07004815 if (stream == AUDIO_STREAM_VOICE_CALL ||
4816 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004817 float voiceVolume;
4818 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004819 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004820 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
4821 } else {
4822 voiceVolume = 1.0;
4823 }
4824
4825 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
4826 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4827 mLastVoiceVolume = voiceVolume;
4828 }
4829 }
4830
4831 return NO_ERROR;
4832}
4833
Eric Laurente0720872014-03-11 09:30:41 -07004834void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
François Gaffie53615e22015-03-19 09:24:12 +01004835 audio_devices_t device,
4836 int delayMs,
4837 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004838{
4839 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
4840
Eric Laurent3b73df72014-03-11 09:06:29 -07004841 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004842 if (stream == AUDIO_STREAM_PATCH) {
4843 continue;
4844 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004845 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004846 mStreams[stream].getVolumeIndex(device),
4847 output,
4848 device,
4849 delayMs,
4850 force);
4851 }
4852}
4853
Eric Laurente0720872014-03-11 09:30:41 -07004854void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004855 bool on,
4856 audio_io_handle_t output,
4857 int delayMs,
4858 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004859{
4860 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004861 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004862 if (stream == AUDIO_STREAM_PATCH) {
4863 continue;
4864 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004865 if (getStrategy((audio_stream_type_t)stream) == strategy) {
4866 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004867 }
4868 }
4869}
4870
Eric Laurente0720872014-03-11 09:30:41 -07004871void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004872 bool on,
4873 audio_io_handle_t output,
4874 int delayMs,
4875 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004876{
4877 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07004878 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004879 if (device == AUDIO_DEVICE_NONE) {
4880 device = outputDesc->device();
4881 }
4882
4883 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
4884 stream, on, output, outputDesc->mMuteCount[stream], device);
4885
4886 if (on) {
4887 if (outputDesc->mMuteCount[stream] == 0) {
4888 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004889 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
4890 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004891 checkAndSetVolume(stream, 0, output, device, delayMs);
4892 }
4893 }
4894 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4895 outputDesc->mMuteCount[stream]++;
4896 } else {
4897 if (outputDesc->mMuteCount[stream] == 0) {
4898 ALOGV("setStreamMute() unmuting non muted stream!");
4899 return;
4900 }
4901 if (--outputDesc->mMuteCount[stream] == 0) {
4902 checkAndSetVolume(stream,
4903 streamDesc.getVolumeIndex(device),
4904 output,
4905 device,
4906 delayMs);
4907 }
4908 }
4909}
4910
Eric Laurente0720872014-03-11 09:30:41 -07004911void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004912 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004913{
4914 // if the stream pertains to sonification strategy and we are in call we must
4915 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4916 // in the device used for phone strategy and play the tone if the selected device does not
4917 // interfere with the device used for phone strategy
4918 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4919 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004920 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004921 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4922 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004923 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004924 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4925 stream, starting, outputDesc->mDevice, stateChange);
4926 if (outputDesc->mRefCount[stream]) {
4927 int muteCount = 1;
4928 if (stateChange) {
4929 muteCount = outputDesc->mRefCount[stream];
4930 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004931 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004932 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4933 for (int i = 0; i < muteCount; i++) {
4934 setStreamMute(stream, starting, mPrimaryOutput);
4935 }
4936 } else {
4937 ALOGV("handleIncallSonification() high visibility");
4938 if (outputDesc->device() &
4939 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4940 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4941 for (int i = 0; i < muteCount; i++) {
4942 setStreamMute(stream, starting, mPrimaryOutput);
4943 }
4944 }
4945 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004946 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4947 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004948 } else {
4949 mpClientInterface->stopTone();
4950 }
4951 }
4952 }
4953 }
4954}
4955
Eric Laurente0720872014-03-11 09:30:41 -07004956bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07004957{
4958 return isStateInCall(mPhoneState);
4959}
4960
Eric Laurente0720872014-03-11 09:30:41 -07004961bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004962 return ((state == AUDIO_MODE_IN_CALL) ||
4963 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07004964}
4965
Eric Laurente0720872014-03-11 09:30:41 -07004966void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004967{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004968 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004969 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004970 sp<DeviceDescriptor> defaultInputDevice =
4971 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004972 mAvailableOutputDevices.add(mDefaultOutputDevice);
4973 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004974
4975 module = new HwModule("primary");
4976
Eric Laurent1afeecb2014-05-14 08:52:28 -07004977 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004978 profile->mSamplingRates.add(44100);
4979 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4980 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004981 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004982 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4983 module->mOutputProfiles.add(profile);
4984
Eric Laurent1afeecb2014-05-14 08:52:28 -07004985 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004986 profile->mSamplingRates.add(8000);
4987 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4988 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004989 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004990 module->mInputProfiles.add(profile);
4991
4992 mHwModules.add(module);
4993}
4994
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004995audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4996{
4997 // flags to stream type mapping
4998 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4999 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5000 }
5001 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5002 return AUDIO_STREAM_BLUETOOTH_SCO;
5003 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005004 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5005 return AUDIO_STREAM_TTS;
5006 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005007
5008 // usage to stream type mapping
5009 switch (attr->usage) {
5010 case AUDIO_USAGE_MEDIA:
5011 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005012 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5013 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005014 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005015 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5016 return AUDIO_STREAM_ALARM;
5017 }
5018 if (isStreamActive(AUDIO_STREAM_RING)) {
5019 return AUDIO_STREAM_RING;
5020 }
5021 if (isInCall()) {
5022 return AUDIO_STREAM_VOICE_CALL;
5023 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005024 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005025 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5026 return AUDIO_STREAM_SYSTEM;
5027 case AUDIO_USAGE_VOICE_COMMUNICATION:
5028 return AUDIO_STREAM_VOICE_CALL;
5029
5030 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5031 return AUDIO_STREAM_DTMF;
5032
5033 case AUDIO_USAGE_ALARM:
5034 return AUDIO_STREAM_ALARM;
5035 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5036 return AUDIO_STREAM_RING;
5037
5038 case AUDIO_USAGE_NOTIFICATION:
5039 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5040 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5041 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5042 case AUDIO_USAGE_NOTIFICATION_EVENT:
5043 return AUDIO_STREAM_NOTIFICATION;
5044
5045 case AUDIO_USAGE_UNKNOWN:
5046 default:
5047 return AUDIO_STREAM_MUSIC;
5048 }
5049}
Eric Laurente83b55d2014-11-14 10:06:21 -08005050
François Gaffie53615e22015-03-19 09:24:12 +01005051bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
5052{
Eric Laurente83b55d2014-11-14 10:06:21 -08005053 // has flags that map to a strategy?
5054 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5055 return true;
5056 }
5057
5058 // has known usage?
5059 switch (paa->usage) {
5060 case AUDIO_USAGE_UNKNOWN:
5061 case AUDIO_USAGE_MEDIA:
5062 case AUDIO_USAGE_VOICE_COMMUNICATION:
5063 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5064 case AUDIO_USAGE_ALARM:
5065 case AUDIO_USAGE_NOTIFICATION:
5066 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5067 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5068 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5069 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5070 case AUDIO_USAGE_NOTIFICATION_EVENT:
5071 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5072 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5073 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5074 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005075 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005076 break;
5077 default:
5078 return false;
5079 }
5080 return true;
5081}
5082
François Gaffiead3183e2015-03-18 16:55:35 +01005083
5084bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5085 routing_strategy strategy, uint32_t inPastMs,
5086 nsecs_t sysTime) const
5087{
5088 if ((sysTime == 0) && (inPastMs != 0)) {
5089 sysTime = systemTime();
5090 }
5091 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5092 if (i == AUDIO_STREAM_PATCH) {
5093 continue;
5094 }
5095 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5096 (NUM_STRATEGIES == strategy)) &&
5097 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5098 return true;
5099 }
5100 }
5101 return false;
5102}
5103
Eric Laurente552edb2014-03-10 17:42:56 -07005104}; // namespace android