blob: b48dc803f760ad012146345958545d3b8830dfd2 [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
27// A device mask for all audio input devices that are considered "virtual" when evaluating
28// active inputs in getActiveInput()
Hochi Huang327cb702014-09-21 09:47:31 +080029#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_FM_TUNER)
Eric Laurente552edb2014-03-10 17:42:56 -070030// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -070033// A device mask for all audio input and output devices where matching inputs/outputs on device
34// type alone is not enough: the address must match too
35#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
36 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Eric Laurente552edb2014-03-10 17:42:56 -070037
Eric Laurentd4692962014-05-05 18:13:44 -070038#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070039#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070040
Eric Laurente552edb2014-03-10 17:42:56 -070041#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070042#include <utils/Log.h>
43#include <hardware/audio.h>
44#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070045#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080046#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070047#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070048#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070049#include "audio_policy_conf.h"
Eric Laurente552edb2014-03-10 17:42:56 -070050
Eric Laurent3b73df72014-03-11 09:06:29 -070051namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070052
53// ----------------------------------------------------------------------------
54// AudioPolicyInterface implementation
55// ----------------------------------------------------------------------------
56
Eric Laurente0720872014-03-11 09:30:41 -070057status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080058 audio_policy_dev_state_t state,
59 const char *device_address,
60 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070061{
Paul McLeane743a472015-01-28 11:07:31 -080062 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080063}
64
65status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080066 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080067 const char *device_address,
68 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080069{
Paul McLeane743a472015-01-28 11:07:31 -080070 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
71- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070072
73 // connect/disconnect only 1 device at a time
74 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
75
Paul McLeane8610ba2015-02-06 13:53:36 -080076 sp<DeviceDescriptor> devDesc = getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080077
Eric Laurente552edb2014-03-10 17:42:56 -070078 // handle output devices
79 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070080 SortedVector <audio_io_handle_t> outputs;
81
Eric Laurent3a4311c2014-03-17 12:00:47 -070082 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
83
Eric Laurente552edb2014-03-10 17:42:56 -070084 // save a copy of the opened output descriptors before any output is opened or closed
85 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
86 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070087 switch (state)
88 {
89 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080090 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070091 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070092 ALOGW("setDeviceConnectionState() device already connected: %x", device);
93 return INVALID_OPERATION;
94 }
95 ALOGV("setDeviceConnectionState() connecting device %x", device);
96
Eric Laurente552edb2014-03-10 17:42:56 -070097 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070098 index = mAvailableOutputDevices.add(devDesc);
99 if (index >= 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700100 sp<HwModule> module = getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700101 if (module == 0) {
102 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
103 device);
104 mAvailableOutputDevices.remove(devDesc);
105 return INVALID_OPERATION;
106 }
Paul McLeane743a472015-01-28 11:07:31 -0800107 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700108 } else {
109 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700110 }
111
Eric Laurenta1d525f2015-01-29 13:36:45 -0800112 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 mAvailableOutputDevices.remove(devDesc);
114 return INVALID_OPERATION;
115 }
116 // outputs should never be empty here
117 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
118 "checkOutputsForDevice() returned no outputs but status OK");
119 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
120 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800121
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800122 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800123 AudioParameter param = AudioParameter(devDesc->mAddress);
124 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
125 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
126
127 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700128 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700129 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700130 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700131 ALOGW("setDeviceConnectionState() device not connected: %x", device);
132 return INVALID_OPERATION;
133 }
134
Paul McLean5c477aa2014-08-20 16:47:57 -0700135 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
136
Paul McLeane743a472015-01-28 11:07:31 -0800137 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800138 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700139 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
140 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
141
Eric Laurente552edb2014-03-10 17:42:56 -0700142 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700143 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700144
Eric Laurenta1d525f2015-01-29 13:36:45 -0800145 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700160 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700174 if (mPhoneState == AUDIO_MODE_IN_CALL) {
175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700179 audio_io_handle_t output = mOutputs.keyAt(i);
180 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i),
182 true /*fromCache*/);
183 // do not force device change on duplicated output because if device is 0, it will
184 // also force a device 0 for the two outputs it is duplicated to which may override
185 // a valid device selection on those outputs.
186 bool force = !mOutputs.valueAt(i)->isDuplicated()
187 && (!deviceDistinguishesOnAddress(device)
188 // always force when disconnecting (a non-duplicated device)
189 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
190 setOutputDevice(output, newDevice, force, 0);
191 }
Eric Laurente552edb2014-03-10 17:42:56 -0700192 }
193
Eric Laurent72aa32f2014-05-30 18:51:48 -0700194 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700195 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700196 } // end if is output device
197
Eric Laurente552edb2014-03-10 17:42:56 -0700198 // handle input devices
199 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700200 SortedVector <audio_io_handle_t> inputs;
201
Eric Laurent3a4311c2014-03-17 12:00:47 -0700202 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700203 switch (state)
204 {
205 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700206 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700207 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700208 ALOGW("setDeviceConnectionState() device already connected: %d", device);
209 return INVALID_OPERATION;
210 }
Eric Laurent1f2f2232014-06-02 12:01:23 -0700211 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700212 if (module == NULL) {
213 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
214 device);
215 return INVALID_OPERATION;
216 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800217 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700218 return INVALID_OPERATION;
219 }
220
Eric Laurent3a4311c2014-03-17 12:00:47 -0700221 index = mAvailableInputDevices.add(devDesc);
222 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800223 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700224 } else {
225 return NO_MEMORY;
226 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800227
228 // Set connect to HALs
229 AudioParameter param = AudioParameter(devDesc->mAddress);
230 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
231 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
232
Eric Laurentd4692962014-05-05 18:13:44 -0700233 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700234
235 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700236 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700237 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700238 ALOGW("setDeviceConnectionState() device not connected: %d", device);
239 return INVALID_OPERATION;
240 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700241
242 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
243
244 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800245 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700246 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
247 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
248
Eric Laurenta1d525f2015-01-29 13:36:45 -0800249 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700250 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700251
Eric Laurentd4692962014-05-05 18:13:44 -0700252 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700253
254 default:
255 ALOGE("setDeviceConnectionState() invalid state: %x", state);
256 return BAD_VALUE;
257 }
258
Eric Laurentd4692962014-05-05 18:13:44 -0700259 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700260
Eric Laurentc2730ba2014-07-20 15:47:07 -0700261 if (mPhoneState == AUDIO_MODE_IN_CALL) {
262 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
263 updateCallRouting(newDevice);
264 }
265
Eric Laurentb52c1522014-05-20 11:27:36 -0700266 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700267 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700268 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700269
270 ALOGW("setDeviceConnectionState() invalid device: %x", device);
271 return BAD_VALUE;
272}
273
Eric Laurente0720872014-03-11 09:30:41 -0700274audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700275 const char *device_address)
276{
Paul McLeane8610ba2015-02-06 13:53:36 -0800277 sp<DeviceDescriptor> devDesc = getDeviceDescriptor(device, device_address, "");
Eric Laurent3a4311c2014-03-17 12:00:47 -0700278 DeviceVector *deviceVector;
279
Eric Laurente552edb2014-03-10 17:42:56 -0700280 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700281 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700282 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700283 deviceVector = &mAvailableInputDevices;
284 } else {
285 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
286 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700287 }
288
Eric Laurenta1d525f2015-01-29 13:36:45 -0800289 ssize_t index = deviceVector->indexOf(devDesc);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700290 if (index >= 0) {
291 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
292 } else {
293 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
294 }
Eric Laurente552edb2014-03-10 17:42:56 -0700295}
296
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800297sp<DeviceDescriptor> AudioPolicyManager::getDeviceDescriptor(const audio_devices_t device,
298 const char *device_address,
299 const char *device_name)
Eric Laurenta1d525f2015-01-29 13:36:45 -0800300{
301 String8 address = (device_address == NULL) ? String8("") : String8(device_address);
302 // handle legacy remote submix case where the address was not always specified
303 if (deviceDistinguishesOnAddress(device) && (address.length() == 0)) {
304 address = String8("0");
305 }
306
307 for (size_t i = 0; i < mHwModules.size(); i++) {
308 if (mHwModules[i]->mHandle == 0) {
309 continue;
310 }
311 DeviceVector deviceList =
312 mHwModules[i]->mDeclaredDevices.getDevicesFromTypeAddr(device, address);
313 if (!deviceList.isEmpty()) {
314 return deviceList.itemAt(0);
315 }
316 deviceList = mHwModules[i]->mDeclaredDevices.getDevicesFromType(device);
317 if (!deviceList.isEmpty()) {
318 return deviceList.itemAt(0);
319 }
320 }
321
Paul McLeane8610ba2015-02-06 13:53:36 -0800322 sp<DeviceDescriptor> devDesc =
323 new DeviceDescriptor(String8(device_name != NULL ? device_name : ""), device);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800324 devDesc->mAddress = address;
325 return devDesc;
326}
327
Eric Laurentc2730ba2014-07-20 15:47:07 -0700328void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
329{
330 bool createTxPatch = false;
331 struct audio_patch patch;
332 patch.num_sources = 1;
333 patch.num_sinks = 1;
334 status_t status;
335 audio_patch_handle_t afPatchHandle;
336 DeviceVector deviceList;
337
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800338 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700339 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
340
341 // release existing RX patch if any
342 if (mCallRxPatch != 0) {
343 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
344 mCallRxPatch.clear();
345 }
346 // release TX patch if any
347 if (mCallTxPatch != 0) {
348 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
349 mCallTxPatch.clear();
350 }
351
352 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
353 // via setOutputDevice() on primary output.
354 // Otherwise, create two audio patches for TX and RX path.
355 if (availablePrimaryOutputDevices() & rxDevice) {
356 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
357 // If the TX device is also on the primary HW module, setOutputDevice() will take care
358 // of it due to legacy implementation. If not, create a patch.
359 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
360 == AUDIO_DEVICE_NONE) {
361 createTxPatch = true;
362 }
363 } else {
364 // create RX path audio patch
365 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
366 ALOG_ASSERT(!deviceList.isEmpty(),
367 "updateCallRouting() selected device not in output device list");
368 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
369 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
370 ALOG_ASSERT(!deviceList.isEmpty(),
371 "updateCallRouting() no telephony RX device");
372 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
373
374 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
375 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
376
377 // request to reuse existing output stream if one is already opened to reach the RX device
378 SortedVector<audio_io_handle_t> outputs =
379 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700380 audio_io_handle_t output = selectOutput(outputs,
381 AUDIO_OUTPUT_FLAG_NONE,
382 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700383 if (output != AUDIO_IO_HANDLE_NONE) {
384 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
385 ALOG_ASSERT(!outputDesc->isDuplicated(),
386 "updateCallRouting() RX device output is duplicated");
387 outputDesc->toAudioPortConfig(&patch.sources[1]);
388 patch.num_sources = 2;
389 }
390
391 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
392 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
393 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
394 status);
395 if (status == NO_ERROR) {
396 mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
397 &patch, mUidCached);
398 mCallRxPatch->mAfPatchHandle = afPatchHandle;
399 mCallRxPatch->mUid = mUidCached;
400 }
401 createTxPatch = true;
402 }
403 if (createTxPatch) {
404
405 struct audio_patch patch;
406 patch.num_sources = 1;
407 patch.num_sinks = 1;
408 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
409 ALOG_ASSERT(!deviceList.isEmpty(),
410 "updateCallRouting() selected device not in input device list");
411 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
412 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
413 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
414 ALOG_ASSERT(!deviceList.isEmpty(),
415 "updateCallRouting() no telephony TX device");
416 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
417 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
418
419 SortedVector<audio_io_handle_t> outputs =
420 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700421 audio_io_handle_t output = selectOutput(outputs,
422 AUDIO_OUTPUT_FLAG_NONE,
423 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700424 // request to reuse existing output stream if one is already opened to reach the TX
425 // path output device
426 if (output != AUDIO_IO_HANDLE_NONE) {
427 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
428 ALOG_ASSERT(!outputDesc->isDuplicated(),
429 "updateCallRouting() RX device output is duplicated");
430 outputDesc->toAudioPortConfig(&patch.sources[1]);
431 patch.num_sources = 2;
432 }
433
434 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
435 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
436 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
437 status);
438 if (status == NO_ERROR) {
439 mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
440 &patch, mUidCached);
441 mCallTxPatch->mAfPatchHandle = afPatchHandle;
442 mCallTxPatch->mUid = mUidCached;
443 }
444 }
445}
446
Eric Laurente0720872014-03-11 09:30:41 -0700447void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700448{
449 ALOGV("setPhoneState() state %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700450 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700451 ALOGW("setPhoneState() invalid state %d", state);
452 return;
453 }
454
455 if (state == mPhoneState ) {
456 ALOGW("setPhoneState() setting same state %d", state);
457 return;
458 }
459
460 // if leaving call state, handle special case of active streams
461 // pertaining to sonification strategy see handleIncallSonification()
462 if (isInCall()) {
463 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700464 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800465 if (stream == AUDIO_STREAM_PATCH) {
466 continue;
467 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700468 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700469 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800470
471 // force reevaluating accessibility routing when call starts
472 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700473 }
474
475 // store previous phone state for management of sonification strategy below
476 int oldState = mPhoneState;
477 mPhoneState = state;
478 bool force = false;
479
480 // are we entering or starting a call
481 if (!isStateInCall(oldState) && isStateInCall(state)) {
482 ALOGV(" Entering call in setPhoneState()");
483 // force routing command to audio hardware when starting a call
484 // even if no device change is needed
485 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800486 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700487 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800488 ApmGains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700489 }
490 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
491 ALOGV(" Exiting call in setPhoneState()");
492 // force routing command to audio hardware when exiting a call
493 // even if no device change is needed
494 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800495 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700496 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800497 ApmGains::sVolumeProfiles[AUDIO_STREAM_DTMF][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700498 }
499 } else if (isStateInCall(state) && (state != oldState)) {
500 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
501 // force routing command to audio hardware when switching between telephony and VoIP
502 // even if no device change is needed
503 force = true;
504 }
505
506 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700507 checkA2dpSuspend();
508 checkOutputForAllStrategies();
509 updateDevicesAndOutputs();
510
Eric Laurent1f2f2232014-06-02 12:01:23 -0700511 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700512
Eric Laurente552edb2014-03-10 17:42:56 -0700513 int delayMs = 0;
514 if (isStateInCall(state)) {
515 nsecs_t sysTime = systemTime();
516 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700517 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700518 // mute media and sonification strategies and delay device switch by the largest
519 // latency of any output where either strategy is active.
520 // This avoid sending the ring tone or music tail into the earpiece or headset.
521 if ((desc->isStrategyActive(STRATEGY_MEDIA,
522 SONIFICATION_HEADSET_MUSIC_DELAY,
523 sysTime) ||
524 desc->isStrategyActive(STRATEGY_SONIFICATION,
525 SONIFICATION_HEADSET_MUSIC_DELAY,
526 sysTime)) &&
527 (delayMs < (int)desc->mLatency*2)) {
528 delayMs = desc->mLatency*2;
529 }
530 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
531 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
532 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
533 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
534 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
535 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
536 }
537 }
538
Eric Laurentc2730ba2014-07-20 15:47:07 -0700539 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
540 // the device returned is not necessarily reachable via this output
541 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
542 // force routing command to audio hardware when ending call
543 // even if no device change is needed
544 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
545 rxDevice = hwOutputDesc->device();
546 }
Eric Laurente552edb2014-03-10 17:42:56 -0700547
Eric Laurentc2730ba2014-07-20 15:47:07 -0700548 if (state == AUDIO_MODE_IN_CALL) {
549 updateCallRouting(rxDevice, delayMs);
550 } else if (oldState == AUDIO_MODE_IN_CALL) {
551 if (mCallRxPatch != 0) {
552 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
553 mCallRxPatch.clear();
554 }
555 if (mCallTxPatch != 0) {
556 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
557 mCallTxPatch.clear();
558 }
559 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
560 } else {
561 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
562 }
Eric Laurente552edb2014-03-10 17:42:56 -0700563 // if entering in call state, handle special case of active streams
564 // pertaining to sonification strategy see handleIncallSonification()
565 if (isStateInCall(state)) {
566 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700567 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800568 if (stream == AUDIO_STREAM_PATCH) {
569 continue;
570 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700571 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700572 }
573 }
574
575 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700576 if (state == AUDIO_MODE_RINGTONE &&
577 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700578 mLimitRingtoneVolume = true;
579 } else {
580 mLimitRingtoneVolume = false;
581 }
582}
583
Eric Laurente0720872014-03-11 09:30:41 -0700584void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700585 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700586{
587 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
588
589 bool forceVolumeReeval = false;
590 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700591 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
592 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
593 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700594 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
595 return;
596 }
597 forceVolumeReeval = true;
598 mForceUse[usage] = config;
599 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700600 case AUDIO_POLICY_FORCE_FOR_MEDIA:
601 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
602 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
603 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
604 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
Hochi Huang327cb702014-09-21 09:47:31 +0800605 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Eric Laurente552edb2014-03-10 17:42:56 -0700606 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
607 return;
608 }
609 mForceUse[usage] = config;
610 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700611 case AUDIO_POLICY_FORCE_FOR_RECORD:
612 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
613 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700614 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
615 return;
616 }
617 mForceUse[usage] = config;
618 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700619 case AUDIO_POLICY_FORCE_FOR_DOCK:
620 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
621 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
622 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
623 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
624 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700625 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
626 }
627 forceVolumeReeval = true;
628 mForceUse[usage] = config;
629 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700630 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
631 if (config != AUDIO_POLICY_FORCE_NONE &&
632 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700633 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
634 }
635 forceVolumeReeval = true;
636 mForceUse[usage] = config;
637 break;
Jungshik Jang7b24ee32014-07-15 19:38:42 +0900638 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
639 if (config != AUDIO_POLICY_FORCE_NONE &&
640 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
641 ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config);
642 }
643 mForceUse[usage] = config;
644 break;
Eric Laurente552edb2014-03-10 17:42:56 -0700645 default:
646 ALOGW("setForceUse() invalid usage %d", usage);
647 break;
648 }
649
650 // check for device and output changes triggered by new force usage
651 checkA2dpSuspend();
652 checkOutputForAllStrategies();
653 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700654 if (mPhoneState == AUDIO_MODE_IN_CALL) {
655 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
656 updateCallRouting(newDevice);
657 }
Eric Laurente552edb2014-03-10 17:42:56 -0700658 for (size_t i = 0; i < mOutputs.size(); i++) {
659 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700660 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700661 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
662 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
663 }
Eric Laurente552edb2014-03-10 17:42:56 -0700664 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
665 applyStreamVolumes(output, newDevice, 0, true);
666 }
667 }
668
669 audio_io_handle_t activeInput = getActiveInput();
670 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700671 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700672 }
673
674}
675
Eric Laurente0720872014-03-11 09:30:41 -0700676audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700677{
678 return mForceUse[usage];
679}
680
Eric Laurente0720872014-03-11 09:30:41 -0700681void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700682{
683 ALOGV("setSystemProperty() property %s, value %s", property, value);
684}
685
686// Find a direct output profile compatible with the parameters passed, even if the input flags do
687// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800688sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700689 audio_devices_t device,
690 uint32_t samplingRate,
691 audio_format_t format,
692 audio_channel_mask_t channelMask,
693 audio_output_flags_t flags)
694{
695 for (size_t i = 0; i < mHwModules.size(); i++) {
696 if (mHwModules[i]->mHandle == 0) {
697 continue;
698 }
699 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700700 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent275e8e92014-11-30 15:14:47 -0800701 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700702 NULL /*updatedSamplingRate*/, format, channelMask,
703 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
704 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700705 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
706 return profile;
707 }
Eric Laurente552edb2014-03-10 17:42:56 -0700708 }
709 }
710 return 0;
711}
712
Eric Laurente0720872014-03-11 09:30:41 -0700713audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700714 uint32_t samplingRate,
715 audio_format_t format,
716 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700717 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700718 const audio_offload_info_t *offloadInfo)
719{
Eric Laurent3b73df72014-03-11 09:06:29 -0700720 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700721 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
722 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
723 device, stream, samplingRate, format, channelMask, flags);
724
Eric Laurente83b55d2014-11-14 10:06:21 -0800725 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
726 stream, samplingRate,format, channelMask,
727 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700728}
729
Eric Laurente83b55d2014-11-14 10:06:21 -0800730status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
731 audio_io_handle_t *output,
732 audio_session_t session,
733 audio_stream_type_t *stream,
734 uint32_t samplingRate,
735 audio_format_t format,
736 audio_channel_mask_t channelMask,
737 audio_output_flags_t flags,
738 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700739{
Eric Laurente83b55d2014-11-14 10:06:21 -0800740 audio_attributes_t attributes;
741 if (attr != NULL) {
742 if (!isValidAttributes(attr)) {
743 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
744 attr->usage, attr->content_type, attr->flags,
745 attr->tags);
746 return BAD_VALUE;
747 }
748 attributes = *attr;
749 } else {
750 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
751 ALOGE("getOutputForAttr(): invalid stream type");
752 return BAD_VALUE;
753 }
754 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700755 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800756
Eric Laurent275e8e92014-11-30 15:14:47 -0800757 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
758 sp<AudioOutputDescriptor> desc;
759 if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_PLAYERS) {
760 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
761 if ((RULE_MATCH_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
762 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage == attributes.usage) ||
763 (RULE_EXCLUDE_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
764 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage != attributes.usage)) {
765 desc = mPolicyMixes[i]->mOutput;
766 break;
767 }
768 if (strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
769 strncmp(attributes.tags + strlen("addr="),
770 mPolicyMixes[i]->mMix.mRegistrationId.string(),
771 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
772 desc = mPolicyMixes[i]->mOutput;
773 break;
774 }
775 }
776 } else if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_RECORDERS) {
777 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE &&
778 strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
779 strncmp(attributes.tags + strlen("addr="),
780 mPolicyMixes[i]->mMix.mRegistrationId.string(),
781 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
782 desc = mPolicyMixes[i]->mOutput;
Eric Laurent275e8e92014-11-30 15:14:47 -0800783 }
784 }
785 if (desc != 0) {
786 if (!audio_is_linear_pcm(format)) {
787 return BAD_VALUE;
788 }
Eric Laurentc722f302014-12-10 11:21:49 -0800789 desc->mPolicyMix = &mPolicyMixes[i]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -0800790 *stream = streamTypefromAttributesInt(&attributes);
791 *output = desc->mIoHandle;
792 ALOGV("getOutputForAttr() returns output %d", *output);
793 return NO_ERROR;
794 }
795 }
796 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
797 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
798 return BAD_VALUE;
799 }
800
Eric Laurent93c3d412014-08-01 14:48:35 -0700801 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
Eric Laurente83b55d2014-11-14 10:06:21 -0800802 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700803
Eric Laurente83b55d2014-11-14 10:06:21 -0800804 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700805 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700806
Eric Laurente83b55d2014-11-14 10:06:21 -0800807 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700808 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
809 }
810
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700811 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700812 device, samplingRate, format, channelMask, flags);
813
Eric Laurente83b55d2014-11-14 10:06:21 -0800814 *stream = streamTypefromAttributesInt(&attributes);
815 *output = getOutputForDevice(device, session, *stream,
816 samplingRate, format, channelMask,
817 flags, offloadInfo);
818 if (*output == AUDIO_IO_HANDLE_NONE) {
819 return INVALID_OPERATION;
820 }
821 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700822}
823
824audio_io_handle_t AudioPolicyManager::getOutputForDevice(
825 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800826 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700827 audio_stream_type_t stream,
828 uint32_t samplingRate,
829 audio_format_t format,
830 audio_channel_mask_t channelMask,
831 audio_output_flags_t flags,
832 const audio_offload_info_t *offloadInfo)
833{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700834 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700835 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700836 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700837
Eric Laurente552edb2014-03-10 17:42:56 -0700838#ifdef AUDIO_POLICY_TEST
839 if (mCurOutput != 0) {
840 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
841 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
842
843 if (mTestOutputs[mCurOutput] == 0) {
844 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700845 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700846 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700847 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700848 outputDesc->mFlags =
849 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700850 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700851 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
852 config.sample_rate = mTestSamplingRate;
853 config.channel_mask = mTestChannels;
854 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700855 if (offloadInfo != NULL) {
856 config.offload_info = *offloadInfo;
857 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700858 status = mpClientInterface->openOutput(0,
859 &mTestOutputs[mCurOutput],
860 &config,
861 &outputDesc->mDevice,
862 String8(""),
863 &outputDesc->mLatency,
864 outputDesc->mFlags);
865 if (status == NO_ERROR) {
866 outputDesc->mSamplingRate = config.sample_rate;
867 outputDesc->mFormat = config.format;
868 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700869 AudioParameter outputCmd = AudioParameter();
870 outputCmd.addInt(String8("set_id"),mCurOutput);
871 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
872 addOutput(mTestOutputs[mCurOutput], outputDesc);
873 }
874 }
875 return mTestOutputs[mCurOutput];
876 }
877#endif //AUDIO_POLICY_TEST
878
879 // open a direct output if required by specified parameters
880 //force direct flag if offload flag is set: offloading implies a direct output stream
881 // and all common behaviors are driven by checking only the direct flag
882 // this should normally be set appropriately in the policy configuration file
883 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700884 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700885 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700886 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
887 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
888 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800889 // only allow deep buffering for music stream type
890 if (stream != AUDIO_STREAM_MUSIC) {
891 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
892 }
Eric Laurente552edb2014-03-10 17:42:56 -0700893
Eric Laurentb732cf52014-09-24 19:08:21 -0700894 sp<IOProfile> profile;
895
896 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700897 // and not explicitly requested
898 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
899 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700900 audio_channel_count_from_out_mask(channelMask) <= 2) {
901 goto non_direct_output;
902 }
903
Eric Laurente552edb2014-03-10 17:42:56 -0700904 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
905 // creating an offloaded track and tearing it down immediately after start when audioflinger
906 // detects there is an active non offloadable effect.
907 // FIXME: We should check the audio session here but we do not have it in this context.
908 // This may prevent offloading in rare situations where effects are left active by apps
909 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700910
Eric Laurente552edb2014-03-10 17:42:56 -0700911 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
912 !isNonOffloadableEffectEnabled()) {
913 profile = getProfileForDirectOutput(device,
914 samplingRate,
915 format,
916 channelMask,
917 (audio_output_flags_t)flags);
918 }
919
Eric Laurent1c333e22014-05-20 10:48:17 -0700920 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700921 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700922
923 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700924 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700925 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
926 outputDesc = desc;
927 // reuse direct output if currently open and configured with same parameters
928 if ((samplingRate == outputDesc->mSamplingRate) &&
929 (format == outputDesc->mFormat) &&
930 (channelMask == outputDesc->mChannelMask)) {
931 outputDesc->mDirectOpenCount++;
932 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
933 return mOutputs.keyAt(i);
934 }
935 }
936 }
937 // close direct output if currently open and configured with different parameters
938 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700939 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700940 }
941 outputDesc = new AudioOutputDescriptor(profile);
942 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700943 outputDesc->mLatency = 0;
944 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700945 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
946 config.sample_rate = samplingRate;
947 config.channel_mask = channelMask;
948 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700949 if (offloadInfo != NULL) {
950 config.offload_info = *offloadInfo;
951 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700952 status = mpClientInterface->openOutput(profile->mModule->mHandle,
953 &output,
954 &config,
955 &outputDesc->mDevice,
956 String8(""),
957 &outputDesc->mLatency,
958 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700959
960 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700961 if (status != NO_ERROR ||
962 (samplingRate != 0 && samplingRate != config.sample_rate) ||
963 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
964 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700965 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
966 "format %d %d, channelMask %04x %04x", output, samplingRate,
967 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
968 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700969 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700970 mpClientInterface->closeOutput(output);
971 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800972 // fall back to mixer output if possible when the direct output could not be open
973 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
974 goto non_direct_output;
975 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800976 // fall back to mixer output if possible when the direct output could not be open
977 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
978 goto non_direct_output;
979 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700980 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700981 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700982 outputDesc->mSamplingRate = config.sample_rate;
983 outputDesc->mChannelMask = config.channel_mask;
984 outputDesc->mFormat = config.format;
985 outputDesc->mRefCount[stream] = 0;
986 outputDesc->mStopTime[stream] = 0;
987 outputDesc->mDirectOpenCount = 1;
988
Eric Laurente552edb2014-03-10 17:42:56 -0700989 audio_io_handle_t srcOutput = getOutputForEffect();
990 addOutput(output, outputDesc);
991 audio_io_handle_t dstOutput = getOutputForEffect();
992 if (dstOutput == output) {
993 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
994 }
995 mPreviousOutputs = mOutputs;
996 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700997 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700998 return output;
999 }
1000
Eric Laurentb732cf52014-09-24 19:08:21 -07001001non_direct_output:
1002
Eric Laurente552edb2014-03-10 17:42:56 -07001003 // ignoring channel mask due to downmix capability in mixer
1004
1005 // open a non direct output
1006
1007 // for non direct outputs, only PCM is supported
1008 if (audio_is_linear_pcm(format)) {
1009 // get which output is suitable for the specified stream. The actual
1010 // routing change will happen when startOutput() will be called
1011 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1012
Eric Laurent8838a382014-09-08 16:44:28 -07001013 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1014 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1015 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001016 }
1017 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1018 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1019
1020 ALOGV("getOutput() returns output %d", output);
1021
1022 return output;
1023}
1024
Eric Laurente0720872014-03-11 09:30:41 -07001025audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001026 audio_output_flags_t flags,
1027 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001028{
1029 // select one output among several that provide a path to a particular device or set of
1030 // devices (the list was previously build by getOutputsForDevice()).
1031 // The priority is as follows:
1032 // 1: the output with the highest number of requested policy flags
1033 // 2: the primary output
1034 // 3: the first output in the list
1035
1036 if (outputs.size() == 0) {
1037 return 0;
1038 }
1039 if (outputs.size() == 1) {
1040 return outputs[0];
1041 }
1042
1043 int maxCommonFlags = 0;
1044 audio_io_handle_t outputFlags = 0;
1045 audio_io_handle_t outputPrimary = 0;
1046
1047 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001048 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001049 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001050 // if a valid format is specified, skip output if not compatible
1051 if (format != AUDIO_FORMAT_INVALID) {
1052 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1053 if (format != outputDesc->mFormat) {
1054 continue;
1055 }
1056 } else if (!audio_is_linear_pcm(format)) {
1057 continue;
1058 }
1059 }
1060
Eric Laurent3b73df72014-03-11 09:06:29 -07001061 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001062 if (commonFlags > maxCommonFlags) {
1063 outputFlags = outputs[i];
1064 maxCommonFlags = commonFlags;
1065 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1066 }
1067 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1068 outputPrimary = outputs[i];
1069 }
1070 }
1071 }
1072
1073 if (outputFlags != 0) {
1074 return outputFlags;
1075 }
1076 if (outputPrimary != 0) {
1077 return outputPrimary;
1078 }
1079
1080 return outputs[0];
1081}
1082
Eric Laurente0720872014-03-11 09:30:41 -07001083status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001084 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001085 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001086{
1087 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
1088 ssize_t index = mOutputs.indexOfKey(output);
1089 if (index < 0) {
1090 ALOGW("startOutput() unknown output %d", output);
1091 return BAD_VALUE;
1092 }
1093
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001094 // cannot start playback of STREAM_TTS if any other output is being used
1095 uint32_t beaconMuteLatency = 0;
1096 if (stream == AUDIO_STREAM_TTS) {
1097 ALOGV("\t found BEACON stream");
1098 if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1099 return INVALID_OPERATION;
1100 } else {
1101 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1102 }
1103 } else {
1104 // some playback other than beacon starts
1105 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1106 }
1107
Eric Laurent1f2f2232014-06-02 12:01:23 -07001108 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001109
1110 // increment usage count for this stream on the requested output:
1111 // NOTE that the usage count is the same for duplicated output and hardware output which is
1112 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1113 outputDesc->changeRefCount(stream, 1);
1114
1115 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001116 // starting an output being rerouted?
1117 audio_devices_t newDevice;
Eric Laurentc722f302014-12-10 11:21:49 -08001118 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001119 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1120 } else {
1121 newDevice = getNewOutputDevice(output, false /*fromCache*/);
1122 }
Eric Laurente552edb2014-03-10 17:42:56 -07001123 routing_strategy strategy = getStrategy(stream);
1124 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001125 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1126 (beaconMuteLatency > 0);
1127 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001128 bool force = false;
1129 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001130 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001131 if (desc != outputDesc) {
1132 // force a device change if any other output is managed by the same hw
1133 // module and has a current device selection that differs from selected device.
1134 // In this case, the audio HAL must receive the new device selection so that it can
1135 // change the device currently selected by the other active output.
1136 if (outputDesc->sharesHwModuleWith(desc) &&
1137 desc->device() != newDevice) {
1138 force = true;
1139 }
1140 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001141 // a notification so that audio focus effect can propagate, or that a mute/unmute
1142 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001143 uint32_t latency = desc->latency();
1144 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1145 waitMs = latency;
1146 }
1147 }
1148 }
1149 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
1150
1151 // handle special case for sonification while in call
1152 if (isInCall()) {
1153 handleIncallSonification(stream, true, false);
1154 }
1155
1156 // apply volume rules for current stream and device if necessary
1157 checkAndSetVolume(stream,
1158 mStreams[stream].getVolumeIndex(newDevice),
1159 output,
1160 newDevice);
1161
1162 // update the outputs if starting an output with a stream that can affect notification
1163 // routing
1164 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001165
1166 // Automatically enable the remote submix input when output is started on a re routing mix
1167 // of type MIX_TYPE_RECORDERS
1168 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1169 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001170 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001171 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001172 outputDesc->mPolicyMix->mRegistrationId,
1173 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001174 }
1175
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001176 // force reevaluating accessibility routing when ringtone or alarm starts
1177 if (strategy == STRATEGY_SONIFICATION) {
1178 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1179 }
1180
Eric Laurente552edb2014-03-10 17:42:56 -07001181 if (waitMs > muteWaitMs) {
1182 usleep((waitMs - muteWaitMs) * 2 * 1000);
1183 }
1184 }
1185 return NO_ERROR;
1186}
1187
1188
Eric Laurente0720872014-03-11 09:30:41 -07001189status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001190 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001191 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001192{
1193 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1194 ssize_t index = mOutputs.indexOfKey(output);
1195 if (index < 0) {
1196 ALOGW("stopOutput() unknown output %d", output);
1197 return BAD_VALUE;
1198 }
1199
Eric Laurent1f2f2232014-06-02 12:01:23 -07001200 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001201
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001202 // always handle stream stop, check which stream type is stopping
1203 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1204
Eric Laurente552edb2014-03-10 17:42:56 -07001205 // handle special case for sonification while in call
1206 if (isInCall()) {
1207 handleIncallSonification(stream, false, false);
1208 }
1209
1210 if (outputDesc->mRefCount[stream] > 0) {
1211 // decrement usage count of this stream on the output
1212 outputDesc->changeRefCount(stream, -1);
1213 // store time at which the stream was stopped - see isStreamActive()
1214 if (outputDesc->mRefCount[stream] == 0) {
Eric Laurentc722f302014-12-10 11:21:49 -08001215 // Automatically disable the remote submix input when output is stopped on a
1216 // re routing mix of type MIX_TYPE_RECORDERS
1217 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1218 outputDesc->mPolicyMix != NULL &&
1219 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001220 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001221 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001222 outputDesc->mPolicyMix->mRegistrationId,
1223 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001224 }
1225
Eric Laurente552edb2014-03-10 17:42:56 -07001226 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -07001227 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001228 // delay the device switch by twice the latency because stopOutput() is executed when
1229 // the track stop() command is received and at that time the audio track buffer can
1230 // still contain data that needs to be drained. The latency only covers the audio HAL
1231 // and kernel buffers. Also the latency does not always include additional delay in the
1232 // audio path (audio DSP, CODEC ...)
1233 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1234
1235 // force restoring the device selection on other active outputs if it differs from the
1236 // one being selected for this output
1237 for (size_t i = 0; i < mOutputs.size(); i++) {
1238 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001239 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001240 if (curOutput != output &&
1241 desc->isActive() &&
1242 outputDesc->sharesHwModuleWith(desc) &&
1243 (newDevice != desc->device())) {
1244 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -07001245 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001246 true,
1247 outputDesc->mLatency*2);
1248 }
1249 }
1250 // update the outputs if stopping one with a stream that can affect notification routing
1251 handleNotificationRoutingForStream(stream);
1252 }
1253 return NO_ERROR;
1254 } else {
1255 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1256 return INVALID_OPERATION;
1257 }
1258}
1259
Eric Laurente83b55d2014-11-14 10:06:21 -08001260void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001261 audio_stream_type_t stream __unused,
1262 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001263{
1264 ALOGV("releaseOutput() %d", output);
1265 ssize_t index = mOutputs.indexOfKey(output);
1266 if (index < 0) {
1267 ALOGW("releaseOutput() releasing unknown output %d", output);
1268 return;
1269 }
1270
1271#ifdef AUDIO_POLICY_TEST
1272 int testIndex = testOutputIndex(output);
1273 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001274 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001275 if (outputDesc->isActive()) {
1276 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001277 mOutputs.removeItem(output);
1278 mTestOutputs[testIndex] = 0;
1279 }
1280 return;
1281 }
1282#endif //AUDIO_POLICY_TEST
1283
Eric Laurent1f2f2232014-06-02 12:01:23 -07001284 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001285 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001286 if (desc->mDirectOpenCount <= 0) {
1287 ALOGW("releaseOutput() invalid open count %d for output %d",
1288 desc->mDirectOpenCount, output);
1289 return;
1290 }
1291 if (--desc->mDirectOpenCount == 0) {
1292 closeOutput(output);
1293 // If effects where present on the output, audioflinger moved them to the primary
1294 // output by default: move them back to the appropriate output.
1295 audio_io_handle_t dstOutput = getOutputForEffect();
1296 if (dstOutput != mPrimaryOutput) {
1297 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1298 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001299 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001300 }
1301 }
1302}
1303
1304
Eric Laurentcaf7f482014-11-25 17:50:47 -08001305status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1306 audio_io_handle_t *input,
1307 audio_session_t session,
1308 uint32_t samplingRate,
1309 audio_format_t format,
1310 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001311 audio_input_flags_t flags,
1312 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001313{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001314 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1315 "session %d, flags %#x",
1316 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001317
Eric Laurentcaf7f482014-11-25 17:50:47 -08001318 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001319 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001320 audio_devices_t device;
1321 // handle legacy remote submix case where the address was not always specified
1322 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001323 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001324 audio_source_t inputSource = attr->source;
1325 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001326 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001327
Eric Laurentc447ded2015-01-06 08:47:05 -08001328 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1329 inputSource = AUDIO_SOURCE_MIC;
1330 }
1331 halInputSource = inputSource;
1332
1333 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001334 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
1335 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1336 address = String8(attr->tags + strlen("addr="));
1337 ssize_t index = mPolicyMixes.indexOfKey(address);
1338 if (index < 0) {
1339 ALOGW("getInputForAttr() no policy for address %s", address.string());
1340 return BAD_VALUE;
1341 }
Eric Laurentc722f302014-12-10 11:21:49 -08001342 if (mPolicyMixes[index]->mMix.mMixType != MIX_TYPE_PLAYERS) {
1343 ALOGW("getInputForAttr() bad policy mix type for address %s", address.string());
1344 return BAD_VALUE;
1345 }
1346 policyMix = &mPolicyMixes[index]->mMix;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001347 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001348 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001349 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001350 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001351 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001352 return BAD_VALUE;
1353 }
Eric Laurentc722f302014-12-10 11:21:49 -08001354 if (policyMix != NULL) {
1355 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001356 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1357 // there is an external policy, but this input is attached to a mix of recorders,
1358 // meaning it receives audio injected into the framework, so the recorder doesn't
1359 // know about it and is therefore considered "legacy"
1360 *inputType = API_INPUT_LEGACY;
1361 } else {
1362 // recording a mix of players defined by an external policy, we're rerouting for
1363 // an external policy
1364 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1365 }
Eric Laurentc722f302014-12-10 11:21:49 -08001366 } else if (audio_is_remote_submix_device(device)) {
1367 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001368 *inputType = API_INPUT_MIX_CAPTURE;
1369 } else {
1370 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001371 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001372 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001373 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001374 case AUDIO_SOURCE_VOICE_UPLINK:
1375 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1376 break;
1377 case AUDIO_SOURCE_VOICE_DOWNLINK:
1378 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1379 break;
1380 case AUDIO_SOURCE_VOICE_CALL:
1381 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1382 break;
1383 default:
1384 break;
1385 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001386 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001387 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1388 if (index >= 0) {
1389 *input = mSoundTriggerSessions.valueFor(session);
1390 isSoundTrigger = true;
1391 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1392 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1393 } else {
1394 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1395 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001396 }
1397 }
1398
Eric Laurent275e8e92014-11-30 15:14:47 -08001399 sp<IOProfile> profile = getInputProfile(device, address,
1400 samplingRate, format, channelMask,
1401 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001402 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001403 //retry without flags
1404 audio_input_flags_t log_flags = flags;
1405 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001406 profile = getInputProfile(device, address,
1407 samplingRate, format, channelMask,
1408 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001409 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001410 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1411 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001412 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001413 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001414 }
Eric Laurente552edb2014-03-10 17:42:56 -07001415 }
1416
1417 if (profile->mModule->mHandle == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001418 ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName);
1419 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001420 }
1421
1422 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1423 config.sample_rate = samplingRate;
1424 config.channel_mask = channelMask;
1425 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001426
Eric Laurentcf2c0212014-07-25 16:20:43 -07001427 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001428 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001429 &config,
1430 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001431 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001432 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001433 flags);
1434
1435 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001436 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001437 (samplingRate != config.sample_rate) ||
1438 (format != config.format) ||
1439 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001440 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001441 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001442 if (*input != AUDIO_IO_HANDLE_NONE) {
1443 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001444 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001445 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001446 }
1447
Eric Laurent1f2f2232014-06-02 12:01:23 -07001448 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001449 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001450 inputDesc->mRefCount = 0;
1451 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001452 inputDesc->mSamplingRate = samplingRate;
1453 inputDesc->mFormat = format;
1454 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001455 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001456 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001457 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001458 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001459
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001460 ALOGV("getInputForAttr() returns input type = %d", inputType);
1461
Eric Laurentcaf7f482014-11-25 17:50:47 -08001462 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001463 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001464 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001465}
1466
Eric Laurent4dc68062014-07-28 17:26:49 -07001467status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1468 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001469{
1470 ALOGV("startInput() input %d", input);
1471 ssize_t index = mInputs.indexOfKey(input);
1472 if (index < 0) {
1473 ALOGW("startInput() unknown input %d", input);
1474 return BAD_VALUE;
1475 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001476 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001477
Eric Laurentc722f302014-12-10 11:21:49 -08001478 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001479 if (index < 0) {
1480 ALOGW("startInput() unknown session %d on input %d", session, input);
1481 return BAD_VALUE;
1482 }
1483
Glenn Kasten74a8e252014-07-24 14:09:55 -07001484 // virtual input devices are compatible with other input devices
1485 if (!isVirtualInputDevice(inputDesc->mDevice)) {
1486
1487 // for a non-virtual input device, check if there is another (non-virtual) active input
Eric Laurente552edb2014-03-10 17:42:56 -07001488 audio_io_handle_t activeInput = getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001489 if (activeInput != 0 && activeInput != input) {
1490
1491 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1492 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001493 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001494 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001495 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001496 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1497 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001498 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001499 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001500 return INVALID_OPERATION;
1501 }
1502 }
1503 }
1504
Glenn Kasten74a8e252014-07-24 14:09:55 -07001505 if (inputDesc->mRefCount == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001506 if (activeInputsCount() == 0) {
1507 SoundTrigger::setCaptureState(true);
1508 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001509 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001510
Eric Laurentc722f302014-12-10 11:21:49 -08001511 // automatically enable the remote submix output when input is started if not
1512 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001513 // For remote submix (a virtual device), we open only one input per capture request.
1514 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001515 String8 address = String8("");
1516 if (inputDesc->mPolicyMix == NULL) {
1517 address = String8("0");
1518 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1519 address = inputDesc->mPolicyMix->mRegistrationId;
1520 }
1521 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001522 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001523 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001524 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001525 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001526 }
Eric Laurente552edb2014-03-10 17:42:56 -07001527 }
1528
Eric Laurente552edb2014-03-10 17:42:56 -07001529 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1530
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001531 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001532 return NO_ERROR;
1533}
1534
Eric Laurent4dc68062014-07-28 17:26:49 -07001535status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1536 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001537{
1538 ALOGV("stopInput() input %d", input);
1539 ssize_t index = mInputs.indexOfKey(input);
1540 if (index < 0) {
1541 ALOGW("stopInput() unknown input %d", input);
1542 return BAD_VALUE;
1543 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001544 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001545
Eric Laurentc722f302014-12-10 11:21:49 -08001546 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001547 if (index < 0) {
1548 ALOGW("stopInput() unknown session %d on input %d", session, input);
1549 return BAD_VALUE;
1550 }
1551
Eric Laurente552edb2014-03-10 17:42:56 -07001552 if (inputDesc->mRefCount == 0) {
1553 ALOGW("stopInput() input %d already stopped", input);
1554 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001555 }
1556
1557 inputDesc->mRefCount--;
1558 if (inputDesc->mRefCount == 0) {
1559
Eric Laurentc722f302014-12-10 11:21:49 -08001560 // automatically disable the remote submix output when input is stopped if not
1561 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001562 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001563 String8 address = String8("");
1564 if (inputDesc->mPolicyMix == NULL) {
1565 address = String8("0");
1566 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1567 address = inputDesc->mPolicyMix->mRegistrationId;
1568 }
1569 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001570 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001571 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001572 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001573 }
Eric Laurente552edb2014-03-10 17:42:56 -07001574 }
1575
Eric Laurent1c333e22014-05-20 10:48:17 -07001576 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001577
1578 if (activeInputsCount() == 0) {
1579 SoundTrigger::setCaptureState(false);
1580 }
Eric Laurente552edb2014-03-10 17:42:56 -07001581 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001582 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001583}
1584
Eric Laurent4dc68062014-07-28 17:26:49 -07001585void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1586 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001587{
1588 ALOGV("releaseInput() %d", input);
1589 ssize_t index = mInputs.indexOfKey(input);
1590 if (index < 0) {
1591 ALOGW("releaseInput() releasing unknown input %d", input);
1592 return;
1593 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001594 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1595 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001596
Eric Laurentc722f302014-12-10 11:21:49 -08001597 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001598 if (index < 0) {
1599 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1600 return;
1601 }
Eric Laurentc722f302014-12-10 11:21:49 -08001602 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001603 if (inputDesc->mOpenRefCount == 0) {
1604 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1605 return;
1606 }
1607 inputDesc->mOpenRefCount--;
1608 if (inputDesc->mOpenRefCount > 0) {
1609 ALOGV("releaseInput() exit > 0");
1610 return;
1611 }
1612
Eric Laurent05b90f82014-08-27 15:32:29 -07001613 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001614 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001615 ALOGV("releaseInput() exit");
1616}
1617
Eric Laurentd4692962014-05-05 18:13:44 -07001618void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001619 bool patchRemoved = false;
1620
Eric Laurentd4692962014-05-05 18:13:44 -07001621 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001622 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1623 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1624 if (patch_index >= 0) {
1625 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1626 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1627 mAudioPatches.removeItemsAt(patch_index);
1628 patchRemoved = true;
1629 }
Eric Laurentd4692962014-05-05 18:13:44 -07001630 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1631 }
1632 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001633 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001634
1635 if (patchRemoved) {
1636 mpClientInterface->onAudioPatchListUpdate();
1637 }
Eric Laurentd4692962014-05-05 18:13:44 -07001638}
1639
Eric Laurente0720872014-03-11 09:30:41 -07001640void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001641 int indexMin,
1642 int indexMax)
1643{
1644 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1645 if (indexMin < 0 || indexMin >= indexMax) {
1646 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1647 return;
1648 }
1649 mStreams[stream].mIndexMin = indexMin;
1650 mStreams[stream].mIndexMax = indexMax;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001651 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1652 if (stream == AUDIO_STREAM_MUSIC) {
1653 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin;
1654 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax;
1655 }
Eric Laurente552edb2014-03-10 17:42:56 -07001656}
1657
Eric Laurente0720872014-03-11 09:30:41 -07001658status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001659 int index,
1660 audio_devices_t device)
1661{
1662
1663 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1664 return BAD_VALUE;
1665 }
1666 if (!audio_is_output_device(device)) {
1667 return BAD_VALUE;
1668 }
1669
1670 // Force max volume if stream cannot be muted
1671 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1672
1673 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1674 stream, device, index);
1675
1676 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1677 // clear all device specific values
1678 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1679 mStreams[stream].mIndexCur.clear();
1680 }
1681 mStreams[stream].mIndexCur.add(device, index);
1682
Eric Laurent31551f82014-10-10 18:21:56 -07001683 // update volume on all outputs whose current device is also selected by the same
1684 // strategy as the device specified by the caller
1685 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001686
1687
1688 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1689 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1690 if (stream == AUDIO_STREAM_MUSIC) {
1691 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index);
1692 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1693 }
1694 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1695 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001696 return NO_ERROR;
1697 }
Eric Laurente552edb2014-03-10 17:42:56 -07001698 status_t status = NO_ERROR;
1699 for (size_t i = 0; i < mOutputs.size(); i++) {
1700 audio_devices_t curDevice =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001701 ApmGains::getDeviceForVolume(mOutputs.valueAt(i)->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001702 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001703 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1704 if (volStatus != NO_ERROR) {
1705 status = volStatus;
1706 }
1707 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001708 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1709 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
1710 index, mOutputs.keyAt(i), curDevice);
1711 }
Eric Laurente552edb2014-03-10 17:42:56 -07001712 }
1713 return status;
1714}
1715
Eric Laurente0720872014-03-11 09:30:41 -07001716status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001717 int *index,
1718 audio_devices_t device)
1719{
1720 if (index == NULL) {
1721 return BAD_VALUE;
1722 }
1723 if (!audio_is_output_device(device)) {
1724 return BAD_VALUE;
1725 }
1726 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1727 // the strategy the stream belongs to.
1728 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1729 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1730 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001731 device = ApmGains::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001732
1733 *index = mStreams[stream].getVolumeIndex(device);
1734 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1735 return NO_ERROR;
1736}
1737
Eric Laurente0720872014-03-11 09:30:41 -07001738audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001739 const SortedVector<audio_io_handle_t>& outputs)
1740{
1741 // select one output among several suitable for global effects.
1742 // The priority is as follows:
1743 // 1: An offloaded output. If the effect ends up not being offloadable,
1744 // AudioFlinger will invalidate the track and the offloaded output
1745 // will be closed causing the effect to be moved to a PCM output.
1746 // 2: A deep buffer output
1747 // 3: the first output in the list
1748
1749 if (outputs.size() == 0) {
1750 return 0;
1751 }
1752
1753 audio_io_handle_t outputOffloaded = 0;
1754 audio_io_handle_t outputDeepBuffer = 0;
1755
1756 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001757 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001758 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001759 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1760 outputOffloaded = outputs[i];
1761 }
1762 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1763 outputDeepBuffer = outputs[i];
1764 }
1765 }
1766
1767 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1768 outputOffloaded, outputDeepBuffer);
1769 if (outputOffloaded != 0) {
1770 return outputOffloaded;
1771 }
1772 if (outputDeepBuffer != 0) {
1773 return outputDeepBuffer;
1774 }
1775
1776 return outputs[0];
1777}
1778
Eric Laurente0720872014-03-11 09:30:41 -07001779audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001780{
1781 // apply simple rule where global effects are attached to the same output as MUSIC streams
1782
Eric Laurent3b73df72014-03-11 09:06:29 -07001783 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001784 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1785 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1786
1787 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1788 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1789 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1790
1791 return output;
1792}
1793
Eric Laurente0720872014-03-11 09:30:41 -07001794status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001795 audio_io_handle_t io,
1796 uint32_t strategy,
1797 int session,
1798 int id)
1799{
1800 ssize_t index = mOutputs.indexOfKey(io);
1801 if (index < 0) {
1802 index = mInputs.indexOfKey(io);
1803 if (index < 0) {
1804 ALOGW("registerEffect() unknown io %d", io);
1805 return INVALID_OPERATION;
1806 }
1807 }
1808
1809 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1810 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1811 desc->name, desc->memoryUsage);
1812 return INVALID_OPERATION;
1813 }
1814 mTotalEffectsMemory += desc->memoryUsage;
1815 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1816 desc->name, io, strategy, session, id);
1817 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1818
Eric Laurent1f2f2232014-06-02 12:01:23 -07001819 sp<EffectDescriptor> effectDesc = new EffectDescriptor();
1820 memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t));
1821 effectDesc->mIo = io;
1822 effectDesc->mStrategy = (routing_strategy)strategy;
1823 effectDesc->mSession = session;
1824 effectDesc->mEnabled = false;
Eric Laurente552edb2014-03-10 17:42:56 -07001825
Eric Laurent1f2f2232014-06-02 12:01:23 -07001826 mEffects.add(id, effectDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001827
1828 return NO_ERROR;
1829}
1830
Eric Laurente0720872014-03-11 09:30:41 -07001831status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001832{
1833 ssize_t index = mEffects.indexOfKey(id);
1834 if (index < 0) {
1835 ALOGW("unregisterEffect() unknown effect ID %d", id);
1836 return INVALID_OPERATION;
1837 }
1838
Eric Laurent1f2f2232014-06-02 12:01:23 -07001839 sp<EffectDescriptor> effectDesc = mEffects.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001840
Eric Laurent1f2f2232014-06-02 12:01:23 -07001841 setEffectEnabled(effectDesc, false);
Eric Laurente552edb2014-03-10 17:42:56 -07001842
Eric Laurent1f2f2232014-06-02 12:01:23 -07001843 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
Eric Laurente552edb2014-03-10 17:42:56 -07001844 ALOGW("unregisterEffect() memory %d too big for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001845 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1846 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
Eric Laurente552edb2014-03-10 17:42:56 -07001847 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001848 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
Eric Laurente552edb2014-03-10 17:42:56 -07001849 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001850 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
Eric Laurente552edb2014-03-10 17:42:56 -07001851
1852 mEffects.removeItem(id);
Eric Laurente552edb2014-03-10 17:42:56 -07001853
1854 return NO_ERROR;
1855}
1856
Eric Laurente0720872014-03-11 09:30:41 -07001857status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001858{
1859 ssize_t index = mEffects.indexOfKey(id);
1860 if (index < 0) {
1861 ALOGW("unregisterEffect() unknown effect ID %d", id);
1862 return INVALID_OPERATION;
1863 }
1864
1865 return setEffectEnabled(mEffects.valueAt(index), enabled);
1866}
1867
Eric Laurent1f2f2232014-06-02 12:01:23 -07001868status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001869{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001870 if (enabled == effectDesc->mEnabled) {
Eric Laurente552edb2014-03-10 17:42:56 -07001871 ALOGV("setEffectEnabled(%s) effect already %s",
1872 enabled?"true":"false", enabled?"enabled":"disabled");
1873 return INVALID_OPERATION;
1874 }
1875
1876 if (enabled) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001877 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001878 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001879 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
Eric Laurente552edb2014-03-10 17:42:56 -07001880 return INVALID_OPERATION;
1881 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001882 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001883 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1884 } else {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001885 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
Eric Laurente552edb2014-03-10 17:42:56 -07001886 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001887 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1888 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001889 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001890 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001891 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1892 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001893 effectDesc->mEnabled = enabled;
Eric Laurente552edb2014-03-10 17:42:56 -07001894 return NO_ERROR;
1895}
1896
Eric Laurente0720872014-03-11 09:30:41 -07001897bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001898{
1899 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001900 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
1901 if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) &&
1902 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001903 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001904 effectDesc->mDesc.name, effectDesc->mSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001905 return true;
1906 }
1907 }
1908 return false;
1909}
1910
Eric Laurente0720872014-03-11 09:30:41 -07001911bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001912{
1913 nsecs_t sysTime = systemTime();
1914 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001915 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001916 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001917 return true;
1918 }
1919 }
1920 return false;
1921}
1922
Eric Laurente0720872014-03-11 09:30:41 -07001923bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001924 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001925{
1926 nsecs_t sysTime = systemTime();
1927 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001928 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001929 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001930 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001931 // do not consider re routing (when the output is going to a dynamic policy)
1932 // as "remote playback"
1933 if (outputDesc->mPolicyMix == NULL) {
Jean-Michel Trivi1767df72014-12-09 18:11:49 -08001934 return true;
1935 }
Eric Laurente552edb2014-03-10 17:42:56 -07001936 }
1937 }
1938 return false;
1939}
1940
Eric Laurente0720872014-03-11 09:30:41 -07001941bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001942{
1943 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001944 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001945 if (inputDescriptor->mRefCount == 0) {
1946 continue;
1947 }
1948 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001949 return true;
1950 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001951 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1952 // corresponds to an active capture triggered by a hardware hotword recognition
1953 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1954 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1955 // FIXME: we should not assume that the first session is the active one and keep
1956 // activity count per session. Same in startInput().
1957 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1958 if (index >= 0) {
1959 return true;
1960 }
1961 }
Eric Laurente552edb2014-03-10 17:42:56 -07001962 }
1963 return false;
1964}
1965
Eric Laurent275e8e92014-11-30 15:14:47 -08001966// Register a list of custom mixes with their attributes and format.
1967// When a mix is registered, corresponding input and output profiles are
1968// added to the remote submix hw module. The profile contains only the
1969// parameters (sampling rate, format...) specified by the mix.
1970// The corresponding input remote submix device is also connected.
1971//
1972// When a remote submix device is connected, the address is checked to select the
1973// appropriate profile and the corresponding input or output stream is opened.
1974//
1975// When capture starts, getInputForAttr() will:
1976// - 1 look for a mix matching the address passed in attribtutes tags if any
1977// - 2 if none found, getDeviceForInputSource() will:
1978// - 2.1 look for a mix matching the attributes source
1979// - 2.2 if none found, default to device selection by policy rules
1980// At this time, the corresponding output remote submix device is also connected
1981// and active playback use cases can be transferred to this mix if needed when reconnecting
1982// after AudioTracks are invalidated
1983//
1984// When playback starts, getOutputForAttr() will:
1985// - 1 look for a mix matching the address passed in attribtutes tags if any
1986// - 2 if none found, look for a mix matching the attributes usage
1987// - 3 if none found, default to device and output selection by policy rules.
1988
1989status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1990{
1991 sp<HwModule> module;
1992 for (size_t i = 0; i < mHwModules.size(); i++) {
1993 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1994 mHwModules[i]->mHandle != 0) {
1995 module = mHwModules[i];
1996 break;
1997 }
1998 }
1999
2000 if (module == 0) {
2001 return INVALID_OPERATION;
2002 }
2003
2004 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
2005
2006 for (size_t i = 0; i < mixes.size(); i++) {
2007 String8 address = mixes[i].mRegistrationId;
2008 ssize_t index = mPolicyMixes.indexOfKey(address);
2009 if (index >= 0) {
2010 ALOGE("registerPolicyMixes(): mix for address %s already registered", address.string());
2011 continue;
2012 }
2013 audio_config_t outputConfig = mixes[i].mFormat;
2014 audio_config_t inputConfig = mixes[i].mFormat;
2015 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2016 // stereo and let audio flinger do the channel conversion if needed.
2017 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2018 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2019 module->addOutputProfile(address, &outputConfig,
2020 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2021 module->addInputProfile(address, &inputConfig,
2022 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
2023 sp<AudioPolicyMix> policyMix = new AudioPolicyMix();
2024 policyMix->mMix = mixes[i];
2025 mPolicyMixes.add(address, policyMix);
Eric Laurentc722f302014-12-10 11:21:49 -08002026 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002027 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002028 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002029 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002030 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002031 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002032 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002033 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002034 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002035 }
2036 return NO_ERROR;
2037}
2038
2039status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2040{
2041 sp<HwModule> module;
2042 for (size_t i = 0; i < mHwModules.size(); i++) {
2043 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2044 mHwModules[i]->mHandle != 0) {
2045 module = mHwModules[i];
2046 break;
2047 }
2048 }
2049
2050 if (module == 0) {
2051 return INVALID_OPERATION;
2052 }
2053
2054 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
2055
2056 for (size_t i = 0; i < mixes.size(); i++) {
2057 String8 address = mixes[i].mRegistrationId;
2058 ssize_t index = mPolicyMixes.indexOfKey(address);
2059 if (index < 0) {
2060 ALOGE("unregisterPolicyMixes(): mix for address %s not registered", address.string());
2061 continue;
2062 }
2063
2064 mPolicyMixes.removeItemsAt(index);
2065
Eric Laurentc722f302014-12-10 11:21:49 -08002066 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2067 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2068 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002069 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002070 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002071 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002072 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002073
2074 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2075 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2076 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002077 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002078 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002079 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002080 }
2081 module->removeOutputProfile(address);
2082 module->removeInputProfile(address);
2083 }
2084 return NO_ERROR;
2085}
2086
Eric Laurente552edb2014-03-10 17:42:56 -07002087
Eric Laurente0720872014-03-11 09:30:41 -07002088status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002089{
2090 const size_t SIZE = 256;
2091 char buffer[SIZE];
2092 String8 result;
2093
2094 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2095 result.append(buffer);
2096
2097 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
2098 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002099 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
2100 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002101 snprintf(buffer, SIZE, " Force use for communications %d\n",
2102 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07002103 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002104 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07002105 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002106 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07002107 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002108 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07002109 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002110 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07002111 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002112 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
2113 mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]);
2114 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002115
Eric Laurent3a4311c2014-03-17 12:00:47 -07002116 snprintf(buffer, SIZE, " Available output devices:\n");
2117 result.append(buffer);
2118 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002119 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002120 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002121 }
2122 snprintf(buffer, SIZE, "\n Available input devices:\n");
2123 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07002124 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002125 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002126 }
Eric Laurente552edb2014-03-10 17:42:56 -07002127
2128 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
2129 write(fd, buffer, strlen(buffer));
2130 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002131 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002132 write(fd, buffer, strlen(buffer));
2133 mHwModules[i]->dump(fd);
2134 }
2135
2136 snprintf(buffer, SIZE, "\nOutputs dump:\n");
2137 write(fd, buffer, strlen(buffer));
2138 for (size_t i = 0; i < mOutputs.size(); i++) {
2139 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
2140 write(fd, buffer, strlen(buffer));
2141 mOutputs.valueAt(i)->dump(fd);
2142 }
2143
2144 snprintf(buffer, SIZE, "\nInputs dump:\n");
2145 write(fd, buffer, strlen(buffer));
2146 for (size_t i = 0; i < mInputs.size(); i++) {
2147 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
2148 write(fd, buffer, strlen(buffer));
2149 mInputs.valueAt(i)->dump(fd);
2150 }
2151
2152 snprintf(buffer, SIZE, "\nStreams dump:\n");
2153 write(fd, buffer, strlen(buffer));
2154 snprintf(buffer, SIZE,
2155 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
2156 write(fd, buffer, strlen(buffer));
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002157 for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002158 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07002159 write(fd, buffer, strlen(buffer));
2160 mStreams[i].dump(fd);
2161 }
2162
2163 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
2164 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
2165 write(fd, buffer, strlen(buffer));
2166
2167 snprintf(buffer, SIZE, "Registered effects:\n");
2168 write(fd, buffer, strlen(buffer));
2169 for (size_t i = 0; i < mEffects.size(); i++) {
2170 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
2171 write(fd, buffer, strlen(buffer));
2172 mEffects.valueAt(i)->dump(fd);
2173 }
2174
Eric Laurent4d416952014-08-10 14:07:09 -07002175 snprintf(buffer, SIZE, "\nAudio Patches:\n");
2176 write(fd, buffer, strlen(buffer));
2177 for (size_t i = 0; i < mAudioPatches.size(); i++) {
2178 mAudioPatches[i]->dump(fd, 2, i);
2179 }
Eric Laurente552edb2014-03-10 17:42:56 -07002180
2181 return NO_ERROR;
2182}
2183
2184// This function checks for the parameters which can be offloaded.
2185// This can be enhanced depending on the capability of the DSP and policy
2186// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002187bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002188{
2189 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002190 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002191 offloadInfo.sample_rate, offloadInfo.channel_mask,
2192 offloadInfo.format,
2193 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2194 offloadInfo.has_video);
2195
2196 // Check if offload has been disabled
2197 char propValue[PROPERTY_VALUE_MAX];
2198 if (property_get("audio.offload.disable", propValue, "0")) {
2199 if (atoi(propValue) != 0) {
2200 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2201 return false;
2202 }
2203 }
2204
2205 // Check if stream type is music, then only allow offload as of now.
2206 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2207 {
2208 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2209 return false;
2210 }
2211
2212 //TODO: enable audio offloading with video when ready
2213 if (offloadInfo.has_video)
2214 {
2215 ALOGV("isOffloadSupported: has_video == true, returning false");
2216 return false;
2217 }
2218
2219 //If duration is less than minimum value defined in property, return false
2220 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2221 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2222 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2223 return false;
2224 }
2225 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2226 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2227 return false;
2228 }
2229
2230 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2231 // creating an offloaded track and tearing it down immediately after start when audioflinger
2232 // detects there is an active non offloadable effect.
2233 // FIXME: We should check the audio session here but we do not have it in this context.
2234 // This may prevent offloading in rare situations where effects are left active by apps
2235 // in the background.
2236 if (isNonOffloadableEffectEnabled()) {
2237 return false;
2238 }
2239
2240 // See if there is a profile to support this.
2241 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002242 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002243 offloadInfo.sample_rate,
2244 offloadInfo.format,
2245 offloadInfo.channel_mask,
2246 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002247 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2248 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002249}
2250
Eric Laurent6a94d692014-05-20 11:18:06 -07002251status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2252 audio_port_type_t type,
2253 unsigned int *num_ports,
2254 struct audio_port *ports,
2255 unsigned int *generation)
2256{
2257 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2258 generation == NULL) {
2259 return BAD_VALUE;
2260 }
2261 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2262 if (ports == NULL) {
2263 *num_ports = 0;
2264 }
2265
2266 size_t portsWritten = 0;
2267 size_t portsMax = *num_ports;
2268 *num_ports = 0;
2269 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2270 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2271 for (size_t i = 0;
2272 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2273 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2274 }
2275 *num_ports += mAvailableOutputDevices.size();
2276 }
2277 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2278 for (size_t i = 0;
2279 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2280 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2281 }
2282 *num_ports += mAvailableInputDevices.size();
2283 }
2284 }
2285 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2286 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2287 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2288 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2289 }
2290 *num_ports += mInputs.size();
2291 }
2292 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002293 size_t numOutputs = 0;
2294 for (size_t i = 0; i < mOutputs.size(); i++) {
2295 if (!mOutputs[i]->isDuplicated()) {
2296 numOutputs++;
2297 if (portsWritten < portsMax) {
2298 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2299 }
2300 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002301 }
Eric Laurent84c70242014-06-23 08:46:27 -07002302 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002303 }
2304 }
2305 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002306 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002307 return NO_ERROR;
2308}
2309
2310status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2311{
2312 return NO_ERROR;
2313}
2314
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002315sp<AudioOutputDescriptor> AudioPolicyManager::getOutputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002316 audio_port_handle_t id) const
2317{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002318 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002319 for (size_t i = 0; i < mOutputs.size(); i++) {
2320 outputDesc = mOutputs.valueAt(i);
2321 if (outputDesc->mId == id) {
2322 break;
2323 }
2324 }
2325 return outputDesc;
2326}
2327
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002328sp<AudioInputDescriptor> AudioPolicyManager::getInputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002329 audio_port_handle_t id) const
2330{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002331 sp<AudioInputDescriptor> inputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002332 for (size_t i = 0; i < mInputs.size(); i++) {
2333 inputDesc = mInputs.valueAt(i);
2334 if (inputDesc->mId == id) {
2335 break;
2336 }
2337 }
2338 return inputDesc;
2339}
2340
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002341sp <HwModule> AudioPolicyManager::getModuleForDevice(
Eric Laurent1f2f2232014-06-02 12:01:23 -07002342 audio_devices_t device) const
Eric Laurent6a94d692014-05-20 11:18:06 -07002343{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002344 sp <HwModule> module;
2345
Eric Laurent6a94d692014-05-20 11:18:06 -07002346 for (size_t i = 0; i < mHwModules.size(); i++) {
2347 if (mHwModules[i]->mHandle == 0) {
2348 continue;
2349 }
2350 if (audio_is_output_device(device)) {
2351 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2352 {
2353 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
2354 return mHwModules[i];
2355 }
2356 }
2357 } else {
2358 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2359 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
2360 device & ~AUDIO_DEVICE_BIT_IN) {
2361 return mHwModules[i];
2362 }
2363 }
2364 }
2365 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002366 return module;
Eric Laurent6a94d692014-05-20 11:18:06 -07002367}
2368
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002369sp <HwModule> AudioPolicyManager::getModuleFromName(const char *name) const
Eric Laurent1afeecb2014-05-14 08:52:28 -07002370{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002371 sp <HwModule> module;
2372
Eric Laurent1afeecb2014-05-14 08:52:28 -07002373 for (size_t i = 0; i < mHwModules.size(); i++)
2374 {
2375 if (strcmp(mHwModules[i]->mName, name) == 0) {
2376 return mHwModules[i];
2377 }
2378 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002379 return module;
Eric Laurent1afeecb2014-05-14 08:52:28 -07002380}
2381
Eric Laurentc2730ba2014-07-20 15:47:07 -07002382audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices()
2383{
2384 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
2385 audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types();
2386 return devices & mAvailableOutputDevices.types();
2387}
2388
2389audio_devices_t AudioPolicyManager::availablePrimaryInputDevices()
2390{
2391 audio_module_handle_t primaryHandle =
2392 mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle;
2393 audio_devices_t devices = AUDIO_DEVICE_NONE;
2394 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2395 if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) {
2396 devices |= mAvailableInputDevices[i]->mDeviceType;
2397 }
2398 }
2399 return devices;
2400}
Eric Laurent1afeecb2014-05-14 08:52:28 -07002401
Eric Laurent6a94d692014-05-20 11:18:06 -07002402status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2403 audio_patch_handle_t *handle,
2404 uid_t uid)
2405{
2406 ALOGV("createAudioPatch()");
2407
2408 if (handle == NULL || patch == NULL) {
2409 return BAD_VALUE;
2410 }
2411 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2412
Eric Laurent874c42872014-08-08 15:13:39 -07002413 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2414 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2415 return BAD_VALUE;
2416 }
2417 // only one source per audio patch supported for now
2418 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002419 return INVALID_OPERATION;
2420 }
Eric Laurent874c42872014-08-08 15:13:39 -07002421
2422 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002423 return INVALID_OPERATION;
2424 }
Eric Laurent874c42872014-08-08 15:13:39 -07002425 for (size_t i = 0; i < patch->num_sinks; i++) {
2426 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2427 return INVALID_OPERATION;
2428 }
2429 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002430
2431 sp<AudioPatch> patchDesc;
2432 ssize_t index = mAudioPatches.indexOfKey(*handle);
2433
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2435 patch->sources[0].role,
2436 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002437#if LOG_NDEBUG == 0
2438 for (size_t i = 0; i < patch->num_sinks; i++) {
2439 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2440 patch->sinks[i].role,
2441 patch->sinks[i].type);
2442 }
2443#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002444
2445 if (index >= 0) {
2446 patchDesc = mAudioPatches.valueAt(index);
2447 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2448 mUidCached, patchDesc->mUid, uid);
2449 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2450 return INVALID_OPERATION;
2451 }
2452 } else {
2453 *handle = 0;
2454 }
2455
2456 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002457 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002458 if (outputDesc == NULL) {
2459 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2460 return BAD_VALUE;
2461 }
Eric Laurent84c70242014-06-23 08:46:27 -07002462 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2463 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002464 if (patchDesc != 0) {
2465 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2466 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2467 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2468 return BAD_VALUE;
2469 }
2470 }
Eric Laurent874c42872014-08-08 15:13:39 -07002471 DeviceVector devices;
2472 for (size_t i = 0; i < patch->num_sinks; i++) {
2473 // Only support mix to devices connection
2474 // TODO add support for mix to mix connection
2475 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2476 ALOGV("createAudioPatch() source mix but sink is not a device");
2477 return INVALID_OPERATION;
2478 }
2479 sp<DeviceDescriptor> devDesc =
2480 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2481 if (devDesc == 0) {
2482 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2483 return BAD_VALUE;
2484 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002485
Eric Laurent874c42872014-08-08 15:13:39 -07002486 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002487 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002488 patch->sources[0].sample_rate,
2489 NULL, // updatedSamplingRate
2490 patch->sources[0].format,
2491 patch->sources[0].channel_mask,
2492 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2493 ALOGV("createAudioPatch() profile not supported for device %08x",
2494 devDesc->mDeviceType);
2495 return INVALID_OPERATION;
2496 }
2497 devices.add(devDesc);
2498 }
2499 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002500 return INVALID_OPERATION;
2501 }
Eric Laurent874c42872014-08-08 15:13:39 -07002502
Eric Laurent6a94d692014-05-20 11:18:06 -07002503 // TODO: reconfigure output format and channels here
2504 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002505 devices.types(), outputDesc->mIoHandle);
2506 setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002507 index = mAudioPatches.indexOfKey(*handle);
2508 if (index >= 0) {
2509 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2510 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2511 }
2512 patchDesc = mAudioPatches.valueAt(index);
2513 patchDesc->mUid = uid;
2514 ALOGV("createAudioPatch() success");
2515 } else {
2516 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2517 return INVALID_OPERATION;
2518 }
2519 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2520 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2521 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002522 // only one sink supported when connecting an input device to a mix
2523 if (patch->num_sinks > 1) {
2524 return INVALID_OPERATION;
2525 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002526 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002527 if (inputDesc == NULL) {
2528 return BAD_VALUE;
2529 }
2530 if (patchDesc != 0) {
2531 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2532 return BAD_VALUE;
2533 }
2534 }
2535 sp<DeviceDescriptor> devDesc =
2536 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2537 if (devDesc == 0) {
2538 return BAD_VALUE;
2539 }
2540
Eric Laurent84c70242014-06-23 08:46:27 -07002541 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002542 devDesc->mAddress,
2543 patch->sinks[0].sample_rate,
2544 NULL, /*updatedSampleRate*/
2545 patch->sinks[0].format,
2546 patch->sinks[0].channel_mask,
2547 // FIXME for the parameter type,
2548 // and the NONE
2549 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002550 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002551 return INVALID_OPERATION;
2552 }
2553 // TODO: reconfigure output format and channels here
2554 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent84c70242014-06-23 08:46:27 -07002555 devDesc->mDeviceType, inputDesc->mIoHandle);
Eric Laurent874c42872014-08-08 15:13:39 -07002556 setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002557 index = mAudioPatches.indexOfKey(*handle);
2558 if (index >= 0) {
2559 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2560 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2561 }
2562 patchDesc = mAudioPatches.valueAt(index);
2563 patchDesc->mUid = uid;
2564 ALOGV("createAudioPatch() success");
2565 } else {
2566 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2567 return INVALID_OPERATION;
2568 }
2569 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2570 // device to device connection
2571 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002572 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002573 return BAD_VALUE;
2574 }
2575 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002576 sp<DeviceDescriptor> srcDeviceDesc =
2577 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002578 if (srcDeviceDesc == 0) {
2579 return BAD_VALUE;
2580 }
Eric Laurent874c42872014-08-08 15:13:39 -07002581
Eric Laurent6a94d692014-05-20 11:18:06 -07002582 //update source and sink with our own data as the data passed in the patch may
2583 // be incomplete.
2584 struct audio_patch newPatch = *patch;
2585 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002586
Eric Laurent874c42872014-08-08 15:13:39 -07002587 for (size_t i = 0; i < patch->num_sinks; i++) {
2588 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2589 ALOGV("createAudioPatch() source device but one sink is not a device");
2590 return INVALID_OPERATION;
2591 }
2592
2593 sp<DeviceDescriptor> sinkDeviceDesc =
2594 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2595 if (sinkDeviceDesc == 0) {
2596 return BAD_VALUE;
2597 }
2598 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2599
2600 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
2601 // only one sink supported when connected devices across HW modules
2602 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002603 return INVALID_OPERATION;
2604 }
Eric Laurent874c42872014-08-08 15:13:39 -07002605 SortedVector<audio_io_handle_t> outputs =
2606 getOutputsForDevice(sinkDeviceDesc->mDeviceType,
2607 mOutputs);
2608 // if the sink device is reachable via an opened output stream, request to go via
2609 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002610 audio_io_handle_t output = selectOutput(outputs,
2611 AUDIO_OUTPUT_FLAG_NONE,
2612 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002613 if (output != AUDIO_IO_HANDLE_NONE) {
2614 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2615 if (outputDesc->isDuplicated()) {
2616 return INVALID_OPERATION;
2617 }
2618 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
2619 newPatch.num_sources = 2;
2620 }
Eric Laurent83b88082014-06-20 18:31:16 -07002621 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002622 }
2623 // TODO: check from routing capabilities in config file and other conflicting patches
2624
2625 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2626 if (index >= 0) {
2627 afPatchHandle = patchDesc->mAfPatchHandle;
2628 }
2629
2630 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2631 &afPatchHandle,
2632 0);
2633 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2634 status, afPatchHandle);
2635 if (status == NO_ERROR) {
2636 if (index < 0) {
2637 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
2638 &newPatch, uid);
2639 addAudioPatch(patchDesc->mHandle, patchDesc);
2640 } else {
2641 patchDesc->mPatch = newPatch;
2642 }
2643 patchDesc->mAfPatchHandle = afPatchHandle;
2644 *handle = patchDesc->mHandle;
2645 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002646 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002647 } else {
2648 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2649 status);
2650 return INVALID_OPERATION;
2651 }
2652 } else {
2653 return BAD_VALUE;
2654 }
2655 } else {
2656 return BAD_VALUE;
2657 }
2658 return NO_ERROR;
2659}
2660
2661status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2662 uid_t uid)
2663{
2664 ALOGV("releaseAudioPatch() patch %d", handle);
2665
2666 ssize_t index = mAudioPatches.indexOfKey(handle);
2667
2668 if (index < 0) {
2669 return BAD_VALUE;
2670 }
2671 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2672 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2673 mUidCached, patchDesc->mUid, uid);
2674 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2675 return INVALID_OPERATION;
2676 }
2677
2678 struct audio_patch *patch = &patchDesc->mPatch;
2679 patchDesc->mUid = mUidCached;
2680 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002681 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002682 if (outputDesc == NULL) {
2683 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2684 return BAD_VALUE;
2685 }
2686
2687 setOutputDevice(outputDesc->mIoHandle,
2688 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2689 true,
2690 0,
2691 NULL);
2692 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2693 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002694 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002695 if (inputDesc == NULL) {
2696 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2697 return BAD_VALUE;
2698 }
2699 setInputDevice(inputDesc->mIoHandle,
2700 getNewInputDevice(inputDesc->mIoHandle),
2701 true,
2702 NULL);
2703 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2704 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2705 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2706 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2707 status, patchDesc->mAfPatchHandle);
2708 removeAudioPatch(patchDesc->mHandle);
2709 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002710 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002711 } else {
2712 return BAD_VALUE;
2713 }
2714 } else {
2715 return BAD_VALUE;
2716 }
2717 return NO_ERROR;
2718}
2719
2720status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2721 struct audio_patch *patches,
2722 unsigned int *generation)
2723{
2724 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
2725 generation == NULL) {
2726 return BAD_VALUE;
2727 }
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002728 ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
Eric Laurent6a94d692014-05-20 11:18:06 -07002729 *num_patches, patches, mAudioPatches.size());
2730 if (patches == NULL) {
2731 *num_patches = 0;
2732 }
2733
2734 size_t patchesWritten = 0;
2735 size_t patchesMax = *num_patches;
2736 for (size_t i = 0;
2737 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2738 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2739 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002740 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07002741 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2742 }
2743 *num_patches = mAudioPatches.size();
2744
2745 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002746 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002747 return NO_ERROR;
2748}
2749
Eric Laurente1715a42014-05-20 11:30:42 -07002750status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002751{
Eric Laurente1715a42014-05-20 11:30:42 -07002752 ALOGV("setAudioPortConfig()");
2753
2754 if (config == NULL) {
2755 return BAD_VALUE;
2756 }
2757 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2758 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002759 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2760 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002761 }
2762
Eric Laurenta121f902014-06-03 13:32:54 -07002763 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002764 if (config->type == AUDIO_PORT_TYPE_MIX) {
2765 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002766 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002767 if (outputDesc == NULL) {
2768 return BAD_VALUE;
2769 }
Eric Laurent84c70242014-06-23 08:46:27 -07002770 ALOG_ASSERT(!outputDesc->isDuplicated(),
2771 "setAudioPortConfig() called on duplicated output %d",
2772 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002773 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002774 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002775 sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002776 if (inputDesc == NULL) {
2777 return BAD_VALUE;
2778 }
Eric Laurenta121f902014-06-03 13:32:54 -07002779 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002780 } else {
2781 return BAD_VALUE;
2782 }
2783 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2784 sp<DeviceDescriptor> deviceDesc;
2785 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2786 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2787 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2788 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2789 } else {
2790 return BAD_VALUE;
2791 }
2792 if (deviceDesc == NULL) {
2793 return BAD_VALUE;
2794 }
Eric Laurenta121f902014-06-03 13:32:54 -07002795 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002796 } else {
2797 return BAD_VALUE;
2798 }
2799
Eric Laurenta121f902014-06-03 13:32:54 -07002800 struct audio_port_config backupConfig;
2801 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2802 if (status == NO_ERROR) {
2803 struct audio_port_config newConfig;
2804 audioPortConfig->toAudioPortConfig(&newConfig, config);
2805 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002806 }
Eric Laurenta121f902014-06-03 13:32:54 -07002807 if (status != NO_ERROR) {
2808 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002809 }
Eric Laurente1715a42014-05-20 11:30:42 -07002810
2811 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002812}
2813
2814void AudioPolicyManager::clearAudioPatches(uid_t uid)
2815{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002816 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002817 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2818 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002819 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002820 }
2821 }
2822}
2823
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002824status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2825 audio_io_handle_t *ioHandle,
2826 audio_devices_t *device)
2827{
2828 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2829 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002830 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002831
2832 mSoundTriggerSessions.add(*session, *ioHandle);
2833
2834 return NO_ERROR;
2835}
2836
2837status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session)
2838{
2839 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
2840 if (index < 0) {
2841 ALOGW("acquireSoundTriggerSession() session %d not registered", session);
2842 return BAD_VALUE;
2843 }
2844
2845 mSoundTriggerSessions.removeItem(session);
2846 return NO_ERROR;
2847}
2848
Eric Laurent6a94d692014-05-20 11:18:06 -07002849status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2850 const sp<AudioPatch>& patch)
2851{
2852 ssize_t index = mAudioPatches.indexOfKey(handle);
2853
2854 if (index >= 0) {
2855 ALOGW("addAudioPatch() patch %d already in", handle);
2856 return ALREADY_EXISTS;
2857 }
2858 mAudioPatches.add(handle, patch);
2859 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2860 "sink handle %d",
2861 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2862 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2863 return NO_ERROR;
2864}
2865
2866status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2867{
2868 ssize_t index = mAudioPatches.indexOfKey(handle);
2869
2870 if (index < 0) {
2871 ALOGW("removeAudioPatch() patch %d not in", handle);
2872 return ALREADY_EXISTS;
2873 }
2874 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2875 mAudioPatches.valueAt(index)->mAfPatchHandle);
2876 mAudioPatches.removeItemsAt(index);
2877 return NO_ERROR;
2878}
2879
Eric Laurente552edb2014-03-10 17:42:56 -07002880// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002881// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002882// ----------------------------------------------------------------------------
2883
Eric Laurent3a4311c2014-03-17 12:00:47 -07002884uint32_t AudioPolicyManager::nextUniqueId()
2885{
2886 return android_atomic_inc(&mNextUniqueId);
2887}
2888
Eric Laurent6a94d692014-05-20 11:18:06 -07002889uint32_t AudioPolicyManager::nextAudioPortGeneration()
2890{
2891 return android_atomic_inc(&mAudioPortGeneration);
2892}
2893
Paul McLeane743a472015-01-28 11:07:31 -08002894int32_t volatile AudioPolicyManager::mNextUniqueId = 1;
2895
Eric Laurente0720872014-03-11 09:30:41 -07002896AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002897 :
2898#ifdef AUDIO_POLICY_TEST
2899 Thread(false),
2900#endif //AUDIO_POLICY_TEST
2901 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002902 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002903 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2904 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002905 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002906 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002907 mAudioPortGeneration(1),
2908 mBeaconMuteRefCount(0),
2909 mBeaconPlayingRefCount(0),
2910 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002911{
Eric Laurent6a94d692014-05-20 11:18:06 -07002912 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002913 mpClientInterface = clientInterface;
2914
Eric Laurent3b73df72014-03-11 09:06:29 -07002915 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2916 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002917 }
2918
Paul McLeane743a472015-01-28 11:07:31 -08002919 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002920 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2921 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2922 ALOGE("could not load audio policy configuration file, setting defaults");
2923 defaultAudioPolicyConfig();
2924 }
2925 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002926 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002927
2928 // must be done after reading the policy
2929 initializeVolumeCurves();
2930
2931 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002932 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2933 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002934 for (size_t i = 0; i < mHwModules.size(); i++) {
2935 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2936 if (mHwModules[i]->mHandle == 0) {
2937 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2938 continue;
2939 }
2940 // open all output streams needed to access attached devices
2941 // except for direct output streams that are only opened when they are actually
2942 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002943 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002944 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2945 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002946 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002947
Eric Laurent3a4311c2014-03-17 12:00:47 -07002948 if (outProfile->mSupportedDevices.isEmpty()) {
2949 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2950 continue;
2951 }
2952
Eric Laurentd78f1532014-09-16 16:38:20 -07002953 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2954 continue;
2955 }
Eric Laurent83b88082014-06-20 18:31:16 -07002956 audio_devices_t profileType = outProfile->mSupportedDevices.types();
2957 if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) {
2958 profileType = mDefaultOutputDevice->mDeviceType;
2959 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002960 // chose first device present in mSupportedDevices also part of
2961 // outputDeviceTypes
2962 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
2963 profileType = outProfile->mSupportedDevices[k]->mDeviceType;
2964 if ((profileType & outputDeviceTypes) != 0) {
2965 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002966 }
Eric Laurente552edb2014-03-10 17:42:56 -07002967 }
2968 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002969 if ((profileType & outputDeviceTypes) == 0) {
2970 continue;
2971 }
2972 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
2973
2974 outputDesc->mDevice = profileType;
2975 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2976 config.sample_rate = outputDesc->mSamplingRate;
2977 config.channel_mask = outputDesc->mChannelMask;
2978 config.format = outputDesc->mFormat;
2979 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2980 status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle,
2981 &output,
2982 &config,
2983 &outputDesc->mDevice,
2984 String8(""),
2985 &outputDesc->mLatency,
2986 outputDesc->mFlags);
2987
2988 if (status != NO_ERROR) {
2989 ALOGW("Cannot open output stream for device %08x on hw module %s",
2990 outputDesc->mDevice,
2991 mHwModules[i]->mName);
2992 } else {
2993 outputDesc->mSamplingRate = config.sample_rate;
2994 outputDesc->mChannelMask = config.channel_mask;
2995 outputDesc->mFormat = config.format;
2996
2997 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
2998 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
2999 ssize_t index =
3000 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
3001 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003002 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
3003 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003004 }
3005 }
3006 if (mPrimaryOutput == 0 &&
3007 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
3008 mPrimaryOutput = output;
3009 }
3010 addOutput(output, outputDesc);
3011 setOutputDevice(output,
3012 outputDesc->mDevice,
3013 true);
3014 }
Eric Laurente552edb2014-03-10 17:42:56 -07003015 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003016 // open input streams needed to access attached devices to validate
3017 // mAvailableInputDevices list
3018 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3019 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003020 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003021
Eric Laurent3a4311c2014-03-17 12:00:47 -07003022 if (inProfile->mSupportedDevices.isEmpty()) {
3023 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
3024 continue;
3025 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003026 // chose first device present in mSupportedDevices also part of
3027 // inputDeviceTypes
3028 audio_devices_t profileType = AUDIO_DEVICE_NONE;
3029 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3030 profileType = inProfile->mSupportedDevices[k]->mDeviceType;
3031 if (profileType & inputDeviceTypes) {
3032 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003033 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003034 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003035 if ((profileType & inputDeviceTypes) == 0) {
3036 continue;
3037 }
3038 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
3039
3040 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
3041 inputDesc->mDevice = profileType;
3042
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003043 // find the address
3044 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3045 // the inputs vector must be of size 1, but we don't want to crash here
3046 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3047 : String8("");
3048 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3049 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3050
Eric Laurentd78f1532014-09-16 16:38:20 -07003051 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3052 config.sample_rate = inputDesc->mSamplingRate;
3053 config.channel_mask = inputDesc->mChannelMask;
3054 config.format = inputDesc->mFormat;
3055 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3056 status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle,
3057 &input,
3058 &config,
3059 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003060 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003061 AUDIO_SOURCE_MIC,
3062 AUDIO_INPUT_FLAG_NONE);
3063
3064 if (status == NO_ERROR) {
3065 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3066 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
3067 ssize_t index =
3068 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
3069 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003070 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
3071 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003072 }
3073 }
3074 mpClientInterface->closeInput(input);
3075 } else {
3076 ALOGW("Cannot open input stream for device %08x on hw module %s",
3077 inputDesc->mDevice,
3078 mHwModules[i]->mName);
3079 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003080 }
3081 }
3082 // make sure all attached devices have been allocated a unique ID
3083 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003084 if (!mAvailableOutputDevices[i]->isAttached()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003085 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003086 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3087 continue;
3088 }
3089 i++;
3090 }
3091 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003092 if (!mAvailableInputDevices[i]->isAttached()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003093 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003094 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3095 continue;
3096 }
3097 i++;
3098 }
3099 // make sure default device is reachable
3100 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003101 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003102 }
Eric Laurente552edb2014-03-10 17:42:56 -07003103
3104 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3105
3106 updateDevicesAndOutputs();
3107
3108#ifdef AUDIO_POLICY_TEST
3109 if (mPrimaryOutput != 0) {
3110 AudioParameter outputCmd = AudioParameter();
3111 outputCmd.addInt(String8("set_id"), 0);
3112 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3113
3114 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3115 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003116 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3117 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003118 mTestLatencyMs = 0;
3119 mCurOutput = 0;
3120 mDirectOutput = false;
3121 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3122 mTestOutputs[i] = 0;
3123 }
3124
3125 const size_t SIZE = 256;
3126 char buffer[SIZE];
3127 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3128 run(buffer, ANDROID_PRIORITY_AUDIO);
3129 }
3130#endif //AUDIO_POLICY_TEST
3131}
3132
Eric Laurente0720872014-03-11 09:30:41 -07003133AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003134{
3135#ifdef AUDIO_POLICY_TEST
3136 exit();
3137#endif //AUDIO_POLICY_TEST
3138 for (size_t i = 0; i < mOutputs.size(); i++) {
3139 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003140 }
3141 for (size_t i = 0; i < mInputs.size(); i++) {
3142 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003143 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003144 mAvailableOutputDevices.clear();
3145 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003146 mOutputs.clear();
3147 mInputs.clear();
3148 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003149}
3150
Eric Laurente0720872014-03-11 09:30:41 -07003151status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003152{
3153 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
3154}
3155
3156#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003157bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003158{
3159 ALOGV("entering threadLoop()");
3160 while (!exitPending())
3161 {
3162 String8 command;
3163 int valueInt;
3164 String8 value;
3165
3166 Mutex::Autolock _l(mLock);
3167 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3168
3169 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3170 AudioParameter param = AudioParameter(command);
3171
3172 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3173 valueInt != 0) {
3174 ALOGV("Test command %s received", command.string());
3175 String8 target;
3176 if (param.get(String8("target"), target) != NO_ERROR) {
3177 target = "Manager";
3178 }
3179 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3180 param.remove(String8("test_cmd_policy_output"));
3181 mCurOutput = valueInt;
3182 }
3183 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3184 param.remove(String8("test_cmd_policy_direct"));
3185 if (value == "false") {
3186 mDirectOutput = false;
3187 } else if (value == "true") {
3188 mDirectOutput = true;
3189 }
3190 }
3191 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3192 param.remove(String8("test_cmd_policy_input"));
3193 mTestInput = valueInt;
3194 }
3195
3196 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3197 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003198 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003199 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003200 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003201 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003202 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003203 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003204 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003205 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003206 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003207 if (target == "Manager") {
3208 mTestFormat = format;
3209 } else if (mTestOutputs[mCurOutput] != 0) {
3210 AudioParameter outputParam = AudioParameter();
3211 outputParam.addInt(String8("format"), format);
3212 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3213 }
3214 }
3215 }
3216 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3217 param.remove(String8("test_cmd_policy_channels"));
3218 int channels = 0;
3219
3220 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003221 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003222 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003223 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003224 }
3225 if (channels != 0) {
3226 if (target == "Manager") {
3227 mTestChannels = channels;
3228 } else if (mTestOutputs[mCurOutput] != 0) {
3229 AudioParameter outputParam = AudioParameter();
3230 outputParam.addInt(String8("channels"), channels);
3231 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3232 }
3233 }
3234 }
3235 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3236 param.remove(String8("test_cmd_policy_sampleRate"));
3237 if (valueInt >= 0 && valueInt <= 96000) {
3238 int samplingRate = valueInt;
3239 if (target == "Manager") {
3240 mTestSamplingRate = samplingRate;
3241 } else if (mTestOutputs[mCurOutput] != 0) {
3242 AudioParameter outputParam = AudioParameter();
3243 outputParam.addInt(String8("sampling_rate"), samplingRate);
3244 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3245 }
3246 }
3247 }
3248
3249 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3250 param.remove(String8("test_cmd_policy_reopen"));
3251
Eric Laurent1f2f2232014-06-02 12:01:23 -07003252 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003253 mpClientInterface->closeOutput(mPrimaryOutput);
3254
3255 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
3256
Eric Laurente552edb2014-03-10 17:42:56 -07003257 mOutputs.removeItem(mPrimaryOutput);
3258
Eric Laurent1f2f2232014-06-02 12:01:23 -07003259 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07003260 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003261 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3262 config.sample_rate = outputDesc->mSamplingRate;
3263 config.channel_mask = outputDesc->mChannelMask;
3264 config.format = outputDesc->mFormat;
3265 status_t status = mpClientInterface->openOutput(moduleHandle,
3266 &mPrimaryOutput,
3267 &config,
3268 &outputDesc->mDevice,
3269 String8(""),
3270 &outputDesc->mLatency,
3271 outputDesc->mFlags);
3272 if (status != NO_ERROR) {
3273 ALOGE("Failed to reopen hardware output stream, "
3274 "samplingRate: %d, format %d, channels %d",
3275 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003276 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003277 outputDesc->mSamplingRate = config.sample_rate;
3278 outputDesc->mChannelMask = config.channel_mask;
3279 outputDesc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003280 AudioParameter outputCmd = AudioParameter();
3281 outputCmd.addInt(String8("set_id"), 0);
3282 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3283 addOutput(mPrimaryOutput, outputDesc);
3284 }
3285 }
3286
3287
3288 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3289 }
3290 }
3291 return false;
3292}
3293
Eric Laurente0720872014-03-11 09:30:41 -07003294void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003295{
3296 {
3297 AutoMutex _l(mLock);
3298 requestExit();
3299 mWaitWorkCV.signal();
3300 }
3301 requestExitAndWait();
3302}
3303
Eric Laurente0720872014-03-11 09:30:41 -07003304int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003305{
3306 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3307 if (output == mTestOutputs[i]) return i;
3308 }
3309 return 0;
3310}
3311#endif //AUDIO_POLICY_TEST
3312
3313// ---
3314
Eric Laurent1f2f2232014-06-02 12:01:23 -07003315void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003316{
Eric Laurent1c333e22014-05-20 10:48:17 -07003317 outputDesc->mIoHandle = output;
3318 outputDesc->mId = nextUniqueId();
3319 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003320 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003321}
3322
Eric Laurent1f2f2232014-06-02 12:01:23 -07003323void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003324{
Eric Laurent1c333e22014-05-20 10:48:17 -07003325 inputDesc->mIoHandle = input;
3326 inputDesc->mId = nextUniqueId();
3327 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003328 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003329}
Eric Laurente552edb2014-03-10 17:42:56 -07003330
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003331void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003332 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003333 const String8 address /*in*/,
3334 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003335 sp<DeviceDescriptor> devDesc =
3336 desc->mProfile->mSupportedDevices.getDevice(device, address);
3337 if (devDesc != 0) {
3338 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3339 desc->mIoHandle, address.string());
3340 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003341 }
3342}
3343
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003344status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
Eric Laurent3b73df72014-03-11 09:06:29 -07003345 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07003346 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07003347 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003348{
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003349 audio_devices_t device = devDesc->mDeviceType;
Eric Laurent1f2f2232014-06-02 12:01:23 -07003350 sp<AudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003351 // erase all current sample rates, formats and channel masks
3352 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003353
Eric Laurent3b73df72014-03-11 09:06:29 -07003354 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003355 // first list already open outputs that can be routed to this device
3356 for (size_t i = 0; i < mOutputs.size(); i++) {
3357 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003358 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003359 if (!deviceDistinguishesOnAddress(device)) {
3360 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3361 outputs.add(mOutputs.keyAt(i));
3362 } else {
3363 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003364 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003365 }
Eric Laurente552edb2014-03-10 17:42:56 -07003366 }
3367 }
3368 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003369 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003370 for (size_t i = 0; i < mHwModules.size(); i++)
3371 {
3372 if (mHwModules[i]->mHandle == 0) {
3373 continue;
3374 }
3375 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3376 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003377 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3378 if (profile->mSupportedDevices.types() & device) {
3379 if (!deviceDistinguishesOnAddress(device) ||
3380 address == profile->mSupportedDevices[0]->mAddress) {
3381 profiles.add(profile);
3382 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3383 }
Eric Laurente552edb2014-03-10 17:42:56 -07003384 }
3385 }
3386 }
3387
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003388 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3389
Eric Laurente552edb2014-03-10 17:42:56 -07003390 if (profiles.isEmpty() && outputs.isEmpty()) {
3391 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3392 return BAD_VALUE;
3393 }
3394
3395 // open outputs for matching profiles if needed. Direct outputs are also opened to
3396 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3397 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003398 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003399
3400 // nothing to do if one output is already opened for this profile
3401 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003402 for (j = 0; j < outputs.size(); j++) {
3403 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003404 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003405 // matching profile: save the sample rates, format and channel masks supported
3406 // by the profile in our device descriptor
3407 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003408 break;
3409 }
3410 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003411 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003412 continue;
3413 }
3414
Eric Laurent83b88082014-06-20 18:31:16 -07003415 ALOGV("opening output for device %08x with params %s profile %p",
3416 device, address.string(), profile.get());
Eric Laurente552edb2014-03-10 17:42:56 -07003417 desc = new AudioOutputDescriptor(profile);
3418 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003419 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3420 config.sample_rate = desc->mSamplingRate;
3421 config.channel_mask = desc->mChannelMask;
3422 config.format = desc->mFormat;
3423 config.offload_info.sample_rate = desc->mSamplingRate;
3424 config.offload_info.channel_mask = desc->mChannelMask;
3425 config.offload_info.format = desc->mFormat;
3426 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
3427 status_t status = mpClientInterface->openOutput(profile->mModule->mHandle,
3428 &output,
3429 &config,
3430 &desc->mDevice,
3431 address,
3432 &desc->mLatency,
3433 desc->mFlags);
3434 if (status == NO_ERROR) {
3435 desc->mSamplingRate = config.sample_rate;
3436 desc->mChannelMask = config.channel_mask;
3437 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003438
Eric Laurentd4692962014-05-05 18:13:44 -07003439 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003440 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003441 char *param = audio_device_address_to_parameter(device, address);
3442 mpClientInterface->setParameters(output, String8(param));
3443 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003444 }
3445
Eric Laurentd4692962014-05-05 18:13:44 -07003446 // Here is where we step through and resolve any "dynamic" fields
3447 String8 reply;
3448 char *value;
3449 if (profile->mSamplingRates[0] == 0) {
3450 reply = mpClientInterface->getParameters(output,
3451 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003452 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003453 reply.string());
3454 value = strpbrk((char *)reply.string(), "=");
3455 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003456 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003457 }
Eric Laurentd4692962014-05-05 18:13:44 -07003458 }
3459 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3460 reply = mpClientInterface->getParameters(output,
3461 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003462 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003463 reply.string());
3464 value = strpbrk((char *)reply.string(), "=");
3465 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003466 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003467 }
Eric Laurentd4692962014-05-05 18:13:44 -07003468 }
3469 if (profile->mChannelMasks[0] == 0) {
3470 reply = mpClientInterface->getParameters(output,
3471 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003472 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003473 reply.string());
3474 value = strpbrk((char *)reply.string(), "=");
3475 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003476 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003477 }
Eric Laurentd4692962014-05-05 18:13:44 -07003478 }
3479 if (((profile->mSamplingRates[0] == 0) &&
3480 (profile->mSamplingRates.size() < 2)) ||
3481 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3482 (profile->mFormats.size() < 2)) ||
3483 ((profile->mChannelMasks[0] == 0) &&
3484 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003485 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003486 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003487 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003488 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3489 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003490 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003491 config.sample_rate = profile->pickSamplingRate();
3492 config.channel_mask = profile->pickChannelMask();
3493 config.format = profile->pickFormat();
3494 config.offload_info.sample_rate = config.sample_rate;
3495 config.offload_info.channel_mask = config.channel_mask;
3496 config.offload_info.format = config.format;
3497 status = mpClientInterface->openOutput(profile->mModule->mHandle,
3498 &output,
3499 &config,
3500 &desc->mDevice,
3501 address,
3502 &desc->mLatency,
3503 desc->mFlags);
3504 if (status == NO_ERROR) {
3505 desc->mSamplingRate = config.sample_rate;
3506 desc->mChannelMask = config.channel_mask;
3507 desc->mFormat = config.format;
3508 } else {
3509 output = AUDIO_IO_HANDLE_NONE;
3510 }
Eric Laurentd4692962014-05-05 18:13:44 -07003511 }
3512
Eric Laurentcf2c0212014-07-25 16:20:43 -07003513 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003514 addOutput(output, desc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003515 if (deviceDistinguishesOnAddress(device) && address != "0") {
3516 ssize_t index = mPolicyMixes.indexOfKey(address);
3517 if (index >= 0) {
3518 mPolicyMixes[index]->mOutput = desc;
Eric Laurentc722f302014-12-10 11:21:49 -08003519 desc->mPolicyMix = &mPolicyMixes[index]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08003520 } else {
3521 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3522 address.string());
3523 }
Eric Laurentc722f302014-12-10 11:21:49 -08003524 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3525 // no duplicated output for direct outputs and
3526 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003527 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003528
Eric Laurentd4692962014-05-05 18:13:44 -07003529 // set initial stream volume for device
3530 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003531
Eric Laurentd4692962014-05-05 18:13:44 -07003532 //TODO: configure audio effect output stage here
3533
3534 // open a duplicating output thread for the new output and the primary output
3535 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
3536 mPrimaryOutput);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003537 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003538 // add duplicated output descriptor
Eric Laurentcf2c0212014-07-25 16:20:43 -07003539 sp<AudioOutputDescriptor> dupOutputDesc =
3540 new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07003541 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
3542 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
3543 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3544 dupOutputDesc->mFormat = desc->mFormat;
3545 dupOutputDesc->mChannelMask = desc->mChannelMask;
3546 dupOutputDesc->mLatency = desc->mLatency;
3547 addOutput(duplicatedOutput, dupOutputDesc);
3548 applyStreamVolumes(duplicatedOutput, device, 0, true);
3549 } else {
3550 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
3551 mPrimaryOutput, output);
3552 mpClientInterface->closeOutput(output);
3553 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003554 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003555 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003556 }
Eric Laurente552edb2014-03-10 17:42:56 -07003557 }
3558 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003559 } else {
3560 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003561 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003562 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003563 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003564 profiles.removeAt(profile_index);
3565 profile_index--;
3566 } else {
3567 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003568 devDesc->importAudioPort(profile);
3569
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003570 if (deviceDistinguishesOnAddress(device)) {
3571 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3572 device, address.string());
3573 setOutputDevice(output, device, true/*force*/, 0/*delay*/,
3574 NULL/*patch handle*/, address.string());
3575 }
Eric Laurente552edb2014-03-10 17:42:56 -07003576 ALOGV("checkOutputsForDevice(): adding output %d", output);
3577 }
3578 }
3579
3580 if (profiles.isEmpty()) {
3581 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3582 return BAD_VALUE;
3583 }
Eric Laurentd4692962014-05-05 18:13:44 -07003584 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003585 // check if one opened output is not needed any more after disconnecting one device
3586 for (size_t i = 0; i < mOutputs.size(); i++) {
3587 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003588 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003589 // exact match on device
3590 if (deviceDistinguishesOnAddress(device) &&
3591 (desc->mProfile->mSupportedDevices.types() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003592 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurent275e8e92014-11-30 15:14:47 -08003593 } else if (!(desc->mProfile->mSupportedDevices.types()
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003594 & mAvailableOutputDevices.types())) {
3595 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3596 mOutputs.keyAt(i));
3597 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003598 }
Eric Laurente552edb2014-03-10 17:42:56 -07003599 }
3600 }
Eric Laurentd4692962014-05-05 18:13:44 -07003601 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003602 for (size_t i = 0; i < mHwModules.size(); i++)
3603 {
3604 if (mHwModules[i]->mHandle == 0) {
3605 continue;
3606 }
3607 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3608 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003609 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003610 if (profile->mSupportedDevices.types() & device) {
3611 ALOGV("checkOutputsForDevice(): "
3612 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003613 if (profile->mSamplingRates[0] == 0) {
3614 profile->mSamplingRates.clear();
3615 profile->mSamplingRates.add(0);
3616 }
3617 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3618 profile->mFormats.clear();
3619 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3620 }
3621 if (profile->mChannelMasks[0] == 0) {
3622 profile->mChannelMasks.clear();
3623 profile->mChannelMasks.add(0);
3624 }
3625 }
3626 }
3627 }
3628 }
3629 return NO_ERROR;
3630}
3631
Eric Laurentd4692962014-05-05 18:13:44 -07003632status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
3633 audio_policy_dev_state_t state,
3634 SortedVector<audio_io_handle_t>& inputs,
3635 const String8 address)
3636{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003637 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003638 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3639 // first list already open inputs that can be routed to this device
3640 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3641 desc = mInputs.valueAt(input_index);
3642 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3643 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3644 inputs.add(mInputs.keyAt(input_index));
3645 }
3646 }
3647
3648 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003649 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003650 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3651 {
3652 if (mHwModules[module_idx]->mHandle == 0) {
3653 continue;
3654 }
3655 for (size_t profile_index = 0;
3656 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3657 profile_index++)
3658 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003659 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3660
3661 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3662 if (!deviceDistinguishesOnAddress(device) ||
3663 address == profile->mSupportedDevices[0]->mAddress) {
3664 profiles.add(profile);
3665 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3666 profile_index, module_idx);
3667 }
Eric Laurentd4692962014-05-05 18:13:44 -07003668 }
3669 }
3670 }
3671
3672 if (profiles.isEmpty() && inputs.isEmpty()) {
3673 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3674 return BAD_VALUE;
3675 }
3676
3677 // open inputs for matching profiles if needed. Direct inputs are also opened to
3678 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3679 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3680
Eric Laurent1c333e22014-05-20 10:48:17 -07003681 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003682 // nothing to do if one input is already opened for this profile
3683 size_t input_index;
3684 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3685 desc = mInputs.valueAt(input_index);
3686 if (desc->mProfile == profile) {
3687 break;
3688 }
3689 }
3690 if (input_index != mInputs.size()) {
3691 continue;
3692 }
3693
3694 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3695 desc = new AudioInputDescriptor(profile);
3696 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003697 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3698 config.sample_rate = desc->mSamplingRate;
3699 config.channel_mask = desc->mChannelMask;
3700 config.format = desc->mFormat;
3701 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3702 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
3703 &input,
3704 &config,
3705 &desc->mDevice,
3706 address,
3707 AUDIO_SOURCE_MIC,
3708 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003709
Eric Laurentcf2c0212014-07-25 16:20:43 -07003710 if (status == NO_ERROR) {
3711 desc->mSamplingRate = config.sample_rate;
3712 desc->mChannelMask = config.channel_mask;
3713 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003714
Eric Laurentd4692962014-05-05 18:13:44 -07003715 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003716 char *param = audio_device_address_to_parameter(device, address);
3717 mpClientInterface->setParameters(input, String8(param));
3718 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003719 }
3720
3721 // Here is where we step through and resolve any "dynamic" fields
3722 String8 reply;
3723 char *value;
3724 if (profile->mSamplingRates[0] == 0) {
3725 reply = mpClientInterface->getParameters(input,
3726 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3727 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3728 reply.string());
3729 value = strpbrk((char *)reply.string(), "=");
3730 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003731 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003732 }
3733 }
3734 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3735 reply = mpClientInterface->getParameters(input,
3736 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3737 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3738 value = strpbrk((char *)reply.string(), "=");
3739 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003740 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003741 }
3742 }
3743 if (profile->mChannelMasks[0] == 0) {
3744 reply = mpClientInterface->getParameters(input,
3745 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3746 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3747 reply.string());
3748 value = strpbrk((char *)reply.string(), "=");
3749 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003750 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003751 }
3752 }
3753 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3754 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3755 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3756 ALOGW("checkInputsForDevice() direct input missing param");
3757 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003758 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003759 }
3760
3761 if (input != 0) {
3762 addInput(input, desc);
3763 }
3764 } // endif input != 0
3765
Eric Laurentcf2c0212014-07-25 16:20:43 -07003766 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003767 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003768 profiles.removeAt(profile_index);
3769 profile_index--;
3770 } else {
3771 inputs.add(input);
3772 ALOGV("checkInputsForDevice(): adding input %d", input);
3773 }
3774 } // end scan profiles
3775
3776 if (profiles.isEmpty()) {
3777 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3778 return BAD_VALUE;
3779 }
3780 } else {
3781 // Disconnect
3782 // check if one opened input is not needed any more after disconnecting one device
3783 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3784 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003785 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3786 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003787 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3788 mInputs.keyAt(input_index));
3789 inputs.add(mInputs.keyAt(input_index));
3790 }
3791 }
3792 // Clear any profiles associated with the disconnected device.
3793 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3794 if (mHwModules[module_index]->mHandle == 0) {
3795 continue;
3796 }
3797 for (size_t profile_index = 0;
3798 profile_index < mHwModules[module_index]->mInputProfiles.size();
3799 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003800 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003801 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003802 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003803 profile_index, module_index);
3804 if (profile->mSamplingRates[0] == 0) {
3805 profile->mSamplingRates.clear();
3806 profile->mSamplingRates.add(0);
3807 }
3808 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3809 profile->mFormats.clear();
3810 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3811 }
3812 if (profile->mChannelMasks[0] == 0) {
3813 profile->mChannelMasks.clear();
3814 profile->mChannelMasks.add(0);
3815 }
3816 }
3817 }
3818 }
3819 } // end disconnect
3820
3821 return NO_ERROR;
3822}
3823
3824
Eric Laurente0720872014-03-11 09:30:41 -07003825void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003826{
3827 ALOGV("closeOutput(%d)", output);
3828
Eric Laurent1f2f2232014-06-02 12:01:23 -07003829 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003830 if (outputDesc == NULL) {
3831 ALOGW("closeOutput() unknown output %d", output);
3832 return;
3833 }
3834
Eric Laurent275e8e92014-11-30 15:14:47 -08003835 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
3836 if (mPolicyMixes[i]->mOutput == outputDesc) {
3837 mPolicyMixes[i]->mOutput.clear();
3838 }
3839 }
3840
Eric Laurente552edb2014-03-10 17:42:56 -07003841 // look for duplicated outputs connected to the output being removed.
3842 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003843 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003844 if (dupOutputDesc->isDuplicated() &&
3845 (dupOutputDesc->mOutput1 == outputDesc ||
3846 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003847 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003848 if (dupOutputDesc->mOutput1 == outputDesc) {
3849 outputDesc2 = dupOutputDesc->mOutput2;
3850 } else {
3851 outputDesc2 = dupOutputDesc->mOutput1;
3852 }
3853 // As all active tracks on duplicated output will be deleted,
3854 // and as they were also referenced on the other output, the reference
3855 // count for their stream type must be adjusted accordingly on
3856 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003857 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003858 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003859 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003860 }
3861 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3862 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3863
3864 mpClientInterface->closeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003865 mOutputs.removeItem(duplicatedOutput);
3866 }
3867 }
3868
Eric Laurent05b90f82014-08-27 15:32:29 -07003869 nextAudioPortGeneration();
3870
3871 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3872 if (index >= 0) {
3873 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3874 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3875 mAudioPatches.removeItemsAt(index);
3876 mpClientInterface->onAudioPatchListUpdate();
3877 }
3878
Eric Laurente552edb2014-03-10 17:42:56 -07003879 AudioParameter param;
3880 param.add(String8("closing"), String8("true"));
3881 mpClientInterface->setParameters(output, param.toString());
3882
3883 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003884 mOutputs.removeItem(output);
3885 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003886}
3887
3888void AudioPolicyManager::closeInput(audio_io_handle_t input)
3889{
3890 ALOGV("closeInput(%d)", input);
3891
3892 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3893 if (inputDesc == NULL) {
3894 ALOGW("closeInput() unknown input %d", input);
3895 return;
3896 }
3897
Eric Laurent6a94d692014-05-20 11:18:06 -07003898 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003899
3900 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3901 if (index >= 0) {
3902 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3903 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3904 mAudioPatches.removeItemsAt(index);
3905 mpClientInterface->onAudioPatchListUpdate();
3906 }
3907
3908 mpClientInterface->closeInput(input);
3909 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003910}
3911
Eric Laurente0720872014-03-11 09:30:41 -07003912SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -07003913 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003914{
3915 SortedVector<audio_io_handle_t> outputs;
3916
3917 ALOGVV("getOutputsForDevice() device %04x", device);
3918 for (size_t i = 0; i < openOutputs.size(); i++) {
3919 ALOGVV("output %d isDuplicated=%d device=%04x",
3920 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3921 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3922 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3923 outputs.add(openOutputs.keyAt(i));
3924 }
3925 }
3926 return outputs;
3927}
3928
Eric Laurente0720872014-03-11 09:30:41 -07003929bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07003930 SortedVector<audio_io_handle_t>& outputs2)
3931{
3932 if (outputs1.size() != outputs2.size()) {
3933 return false;
3934 }
3935 for (size_t i = 0; i < outputs1.size(); i++) {
3936 if (outputs1[i] != outputs2[i]) {
3937 return false;
3938 }
3939 }
3940 return true;
3941}
3942
Eric Laurente0720872014-03-11 09:30:41 -07003943void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003944{
3945 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3946 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3947 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3948 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3949
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003950 // also take into account external policy-related changes: add all outputs which are
3951 // associated with policies in the "before" and "after" output vectors
3952 ALOGVV("checkOutputForStrategy(): policy related outputs");
3953 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
3954 const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
3955 if (desc != 0 && desc->mPolicyMix != NULL) {
3956 srcOutputs.add(desc->mIoHandle);
3957 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3958 }
3959 }
3960 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
3961 const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3962 if (desc != 0 && desc->mPolicyMix != NULL) {
3963 dstOutputs.add(desc->mIoHandle);
3964 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3965 }
3966 }
3967
Eric Laurente552edb2014-03-10 17:42:56 -07003968 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3969 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3970 strategy, srcOutputs[0], dstOutputs[0]);
3971 // mute strategy while moving tracks from one output to another
3972 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003973 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07003974 if (desc->isStrategyActive(strategy)) {
3975 setStrategyMute(strategy, true, srcOutputs[i]);
3976 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3977 }
3978 }
3979
3980 // Move effects associated to this strategy from previous output to new output
3981 if (strategy == STRATEGY_MEDIA) {
3982 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3983 SortedVector<audio_io_handle_t> moved;
3984 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003985 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3986 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3987 effectDesc->mIo != fxOutput) {
3988 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003989 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3990 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003991 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003992 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003993 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003994 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003995 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003996 }
3997 }
3998 }
3999 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07004000 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004001 if (i == AUDIO_STREAM_PATCH) {
4002 continue;
4003 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004004 if (getStrategy((audio_stream_type_t)i) == strategy) {
4005 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004006 }
4007 }
4008 }
4009}
4010
Eric Laurente0720872014-03-11 09:30:41 -07004011void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004012{
Jon Eklund966095e2014-09-09 15:39:49 -05004013 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4014 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004015 checkOutputForStrategy(STRATEGY_PHONE);
Jon Eklund966095e2014-09-09 15:39:49 -05004016 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4017 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004018 checkOutputForStrategy(STRATEGY_SONIFICATION);
4019 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004020 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004021 checkOutputForStrategy(STRATEGY_MEDIA);
4022 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004023 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004024}
4025
Eric Laurente0720872014-03-11 09:30:41 -07004026audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07004027{
Eric Laurente552edb2014-03-10 17:42:56 -07004028 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004029 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004030 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
4031 return mOutputs.keyAt(i);
4032 }
4033 }
4034
4035 return 0;
4036}
4037
Eric Laurente0720872014-03-11 09:30:41 -07004038void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004039{
Eric Laurente552edb2014-03-10 17:42:56 -07004040 audio_io_handle_t a2dpOutput = getA2dpOutput();
4041 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004042 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004043 return;
4044 }
4045
Eric Laurent3a4311c2014-03-17 12:00:47 -07004046 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004047 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4048 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4049 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004050 // suspend A2DP output if:
4051 // (NOT already suspended) &&
4052 // ((SCO device is connected &&
4053 // (forced usage for communication || for record is SCO))) ||
4054 // (phone state is ringing || in call)
4055 //
4056 // restore A2DP output if:
4057 // (Already suspended) &&
4058 // ((SCO device is NOT connected ||
4059 // (forced usage NOT for communication && NOT for record is SCO))) &&
4060 // (phone state is NOT ringing && NOT in call)
4061 //
4062 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004063 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004064 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
4065 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
4066 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
4067 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004068
4069 mpClientInterface->restoreOutput(a2dpOutput);
4070 mA2dpSuspended = false;
4071 }
4072 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004073 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004074 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4075 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
4076 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
4077 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004078
4079 mpClientInterface->suspendOutput(a2dpOutput);
4080 mA2dpSuspended = true;
4081 }
4082 }
4083}
4084
Eric Laurent1c333e22014-05-20 10:48:17 -07004085audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004086{
4087 audio_devices_t device = AUDIO_DEVICE_NONE;
4088
Eric Laurent1f2f2232014-06-02 12:01:23 -07004089 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004090
4091 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4092 if (index >= 0) {
4093 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4094 if (patchDesc->mUid != mUidCached) {
4095 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
4096 outputDesc->device(), outputDesc->mPatchHandle);
4097 return outputDesc->device();
4098 }
4099 }
4100
Eric Laurente552edb2014-03-10 17:42:56 -07004101 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004102 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004103 // use device for strategy enforced audible
4104 // 2: we are in call or the strategy phone is active on the output:
4105 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004106 // 3: the strategy for enforced audible is active but not enforced on the output:
4107 // use the device for strategy enforced audible
4108 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004109 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05004110 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004111 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004112 // 6: the strategy accessibility is active on the output:
4113 // use device for strategy accessibility
4114 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004115 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004116 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004117 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004118 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004119 // use device for strategy t-t-s
Jon Eklund966095e2014-09-09 15:39:49 -05004120 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) &&
4121 mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004122 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4123 } else if (isInCall() ||
4124 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
4125 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
Jon Eklund966095e2014-09-09 15:39:49 -05004126 } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
4127 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004128 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
4129 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
4130 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
4131 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004132 } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) {
4133 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004134 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
4135 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
4136 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
4137 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004138 } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
4139 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004140 } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) {
4141 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004142 }
4143
Eric Laurent1c333e22014-05-20 10:48:17 -07004144 ALOGV("getNewOutputDevice() selected device %x", device);
4145 return device;
4146}
4147
4148audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
4149{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004150 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004151
4152 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4153 if (index >= 0) {
4154 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4155 if (patchDesc->mUid != mUidCached) {
4156 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4157 inputDesc->mDevice, inputDesc->mPatchHandle);
4158 return inputDesc->mDevice;
4159 }
4160 }
4161
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004162 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07004163
4164 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004165 return device;
4166}
4167
Eric Laurente0720872014-03-11 09:30:41 -07004168uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004169 return (uint32_t)getStrategy(stream);
4170}
4171
Eric Laurente0720872014-03-11 09:30:41 -07004172audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004173 // By checking the range of stream before calling getStrategy, we avoid
4174 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4175 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004176 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004177 return AUDIO_DEVICE_NONE;
4178 }
4179 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004180 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004181 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4182 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4183 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004184 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent6a94d692014-05-20 11:18:06 -07004185 if (outputDesc->isStrategyActive(strategy)) {
4186 devices = outputDesc->device();
4187 break;
4188 }
Eric Laurente552edb2014-03-10 17:42:56 -07004189 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004190
4191 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4192 and doesn't really need to.*/
4193 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4194 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4195 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4196 }
4197
Eric Laurente552edb2014-03-10 17:42:56 -07004198 return devices;
4199}
4200
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004201routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07004202 audio_stream_type_t stream) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004203
4204 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
4205
Eric Laurente552edb2014-03-10 17:42:56 -07004206 // stream to strategy mapping
4207 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004208 case AUDIO_STREAM_VOICE_CALL:
4209 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004210 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004211 case AUDIO_STREAM_RING:
4212 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07004213 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07004214 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07004215 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07004216 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07004217 return STRATEGY_DTMF;
4218 default:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004219 ALOGE("unknown stream type %d", stream);
Eric Laurent3b73df72014-03-11 09:06:29 -07004220 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07004221 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
4222 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07004223 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004224 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07004225 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07004226 return STRATEGY_ENFORCED_AUDIBLE;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004227 case AUDIO_STREAM_TTS:
4228 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004229 case AUDIO_STREAM_ACCESSIBILITY:
4230 return STRATEGY_ACCESSIBILITY;
4231 case AUDIO_STREAM_REROUTING:
4232 return STRATEGY_REROUTING;
Eric Laurente552edb2014-03-10 17:42:56 -07004233 }
4234}
4235
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004236uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4237 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004238 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4239 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4240 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004241 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4242 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4243 }
4244
4245 // usage to strategy mapping
4246 switch (attr->usage) {
Eric Laurent29e6cec2014-11-13 18:17:55 -08004247 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4248 if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) {
4249 return (uint32_t) STRATEGY_SONIFICATION;
4250 }
4251 if (isInCall()) {
4252 return (uint32_t) STRATEGY_PHONE;
4253 }
Eric Laurent0f78eab2014-11-25 11:01:34 -08004254 return (uint32_t) STRATEGY_ACCESSIBILITY;
Eric Laurent29e6cec2014-11-13 18:17:55 -08004255
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004256 case AUDIO_USAGE_MEDIA:
4257 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004258 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4259 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4260 return (uint32_t) STRATEGY_MEDIA;
4261
4262 case AUDIO_USAGE_VOICE_COMMUNICATION:
4263 return (uint32_t) STRATEGY_PHONE;
4264
4265 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4266 return (uint32_t) STRATEGY_DTMF;
4267
4268 case AUDIO_USAGE_ALARM:
4269 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4270 return (uint32_t) STRATEGY_SONIFICATION;
4271
4272 case AUDIO_USAGE_NOTIFICATION:
4273 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4274 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4275 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4276 case AUDIO_USAGE_NOTIFICATION_EVENT:
4277 return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL;
4278
4279 case AUDIO_USAGE_UNKNOWN:
4280 default:
4281 return (uint32_t) STRATEGY_MEDIA;
4282 }
4283}
4284
Eric Laurente0720872014-03-11 09:30:41 -07004285void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004286 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004287 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004288 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4289 updateDevicesAndOutputs();
4290 break;
4291 default:
4292 break;
4293 }
4294}
4295
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004296bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) {
4297 for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) {
4298 if (s == (size_t) streamToIgnore) {
4299 continue;
4300 }
4301 for (size_t i = 0; i < mOutputs.size(); i++) {
4302 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
4303 if (outputDesc->mRefCount[s] != 0) {
4304 return true;
4305 }
4306 }
4307 }
4308 return false;
4309}
4310
4311uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4312 switch(event) {
4313 case STARTING_OUTPUT:
4314 mBeaconMuteRefCount++;
4315 break;
4316 case STOPPING_OUTPUT:
4317 if (mBeaconMuteRefCount > 0) {
4318 mBeaconMuteRefCount--;
4319 }
4320 break;
4321 case STARTING_BEACON:
4322 mBeaconPlayingRefCount++;
4323 break;
4324 case STOPPING_BEACON:
4325 if (mBeaconPlayingRefCount > 0) {
4326 mBeaconPlayingRefCount--;
4327 }
4328 break;
4329 }
4330
4331 if (mBeaconMuteRefCount > 0) {
4332 // any playback causes beacon to be muted
4333 return setBeaconMute(true);
4334 } else {
4335 // no other playback: unmute when beacon starts playing, mute when it stops
4336 return setBeaconMute(mBeaconPlayingRefCount == 0);
4337 }
4338}
4339
4340uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4341 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4342 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4343 // keep track of muted state to avoid repeating mute/unmute operations
4344 if (mBeaconMuted != mute) {
4345 // mute/unmute AUDIO_STREAM_TTS on all outputs
4346 ALOGV("\t muting %d", mute);
4347 uint32_t maxLatency = 0;
4348 for (size_t i = 0; i < mOutputs.size(); i++) {
4349 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4350 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
4351 desc->mIoHandle,
4352 0 /*delay*/, AUDIO_DEVICE_NONE);
4353 const uint32_t latency = desc->latency() * 2;
4354 if (latency > maxLatency) {
4355 maxLatency = latency;
4356 }
4357 }
4358 mBeaconMuted = mute;
4359 return maxLatency;
4360 }
4361 return 0;
4362}
4363
Eric Laurente0720872014-03-11 09:30:41 -07004364audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004365 bool fromCache)
4366{
4367 uint32_t device = AUDIO_DEVICE_NONE;
4368
4369 if (fromCache) {
4370 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4371 strategy, mDeviceForStrategy[strategy]);
4372 return mDeviceForStrategy[strategy];
4373 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004374 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004375 switch (strategy) {
4376
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004377 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
4378 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4379 if (!device) {
4380 ALOGE("getDeviceForStrategy() no device found for "\
4381 "STRATEGY_TRANSMITTED_THROUGH_SPEAKER");
4382 }
4383 break;
4384
Eric Laurente552edb2014-03-10 17:42:56 -07004385 case STRATEGY_SONIFICATION_RESPECTFUL:
4386 if (isInCall()) {
4387 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004388 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07004389 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
4390 // while media is playing on a remote device, use the the sonification behavior.
4391 // Note that we test this usecase before testing if media is playing because
4392 // the isStreamActive() method only informs about the activity of a stream, not
4393 // if it's for local playback. Note also that we use the same delay between both tests
4394 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004395 //user "safe" speaker if available instead of normal speaker to avoid triggering
4396 //other acoustic safety mechanisms for notification
4397 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4398 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004399 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004400 // while media is playing (or has recently played), use the same device
4401 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4402 } else {
4403 // when media is not playing anymore, fall back on the sonification behavior
4404 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004405 //user "safe" speaker if available instead of normal speaker to avoid triggering
4406 //other acoustic safety mechanisms for notification
4407 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4408 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurente552edb2014-03-10 17:42:56 -07004409 }
4410
4411 break;
4412
4413 case STRATEGY_DTMF:
4414 if (!isInCall()) {
4415 // when off call, DTMF strategy follows the same rules as MEDIA strategy
4416 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4417 break;
4418 }
4419 // when in call, DTMF and PHONE strategies follow the same rules
4420 // FALL THROUGH
4421
4422 case STRATEGY_PHONE:
Eric Laurentc2730ba2014-07-20 15:47:07 -07004423 // Force use of only devices on primary output if:
4424 // - in call AND
4425 // - cannot route from voice call RX OR
4426 // - audio HAL version is < 3.0 and TX device is on the primary HW module
4427 if (mPhoneState == AUDIO_MODE_IN_CALL) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004428 audio_devices_t txDevice =
4429 getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -07004430 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
4431 if (((mAvailableInputDevices.types() &
4432 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
4433 (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Marco Nelissen961ec212014-08-25 15:58:39 -07004434 (hwOutputDesc->getAudioPort()->mModule->mHalVersion <
Eric Laurentc2730ba2014-07-20 15:47:07 -07004435 AUDIO_DEVICE_API_VERSION_3_0))) {
4436 availableOutputDeviceTypes = availablePrimaryOutputDevices();
4437 }
4438 }
Eric Laurente552edb2014-03-10 17:42:56 -07004439 // for phone strategy, we first consider the forced use and then the available devices by order
4440 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07004441 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
4442 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004443 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004444 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07004445 if (device) break;
4446 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004447 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004448 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004449 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07004450 if (device) break;
4451 // if SCO device is requested but no SCO device is available, fall back to default case
4452 // FALL THROUGH
4453
4454 default: // FORCE_NONE
4455 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07004456 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004457 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004458 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004459 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004460 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004461 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004462 if (device) break;
4463 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004464 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004465 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004466 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004467 if (device) break;
Eric Laurentc2730ba2014-07-20 15:47:07 -07004468 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
4469 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07004470 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004471 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004472 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004473 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004474 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004475 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004476 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004477 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004478 if (device) break;
4479 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004480 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07004481 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004482 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004483 if (device == AUDIO_DEVICE_NONE) {
4484 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
4485 }
4486 break;
4487
Eric Laurent3b73df72014-03-11 09:06:29 -07004488 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07004489 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
4490 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07004491 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004492 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004493 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004494 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004495 if (device) break;
4496 }
Eric Laurentcd71a692014-12-16 12:01:12 -08004497 if (!isInCall()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004498 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004499 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004500 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004501 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004502 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004503 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004504 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004505 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004506 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004507 if (device) break;
4508 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004509 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4510 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004511 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004512 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004513 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004514 if (device == AUDIO_DEVICE_NONE) {
4515 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
4516 }
4517 break;
4518 }
4519 break;
4520
4521 case STRATEGY_SONIFICATION:
4522
4523 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
4524 // handleIncallSonification().
4525 if (isInCall()) {
4526 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
4527 break;
4528 }
4529 // FALL THROUGH
4530
4531 case STRATEGY_ENFORCED_AUDIBLE:
4532 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
4533 // except:
4534 // - when in call where it doesn't default to STRATEGY_PHONE behavior
4535 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
4536
4537 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004538 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004539 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004540 if (device == AUDIO_DEVICE_NONE) {
4541 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
4542 }
4543 }
4544 // The second device used for sonification is the same as the device used by media strategy
4545 // FALL THROUGH
4546
Eric Laurent223fd5c2014-11-11 13:43:36 -08004547 // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now
4548 case STRATEGY_ACCESSIBILITY:
Eric Laurent066ceec2014-11-25 12:35:01 -08004549 if (strategy == STRATEGY_ACCESSIBILITY) {
4550 // do not route accessibility prompts to a digital output currently configured with a
4551 // compressed format as they would likely not be mixed and dropped.
4552 for (size_t i = 0; i < mOutputs.size(); i++) {
4553 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4554 audio_devices_t devices = desc->device() &
4555 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
4556 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
4557 devices != AUDIO_DEVICE_NONE) {
4558 availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices;
4559 }
4560 }
4561 }
4562 // FALL THROUGH
4563
Eric Laurent223fd5c2014-11-11 13:43:36 -08004564 case STRATEGY_REROUTING:
Eric Laurente552edb2014-03-10 17:42:56 -07004565 case STRATEGY_MEDIA: {
4566 uint32_t device2 = AUDIO_DEVICE_NONE;
4567 if (strategy != STRATEGY_SONIFICATION) {
4568 // no sonification on remote submix (e.g. WFD)
Eric Laurent275e8e92014-11-30 15:14:47 -08004569 if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
4570 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
4571 }
Eric Laurente552edb2014-03-10 17:42:56 -07004572 }
4573 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004574 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004575 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004576 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004577 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004578 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004579 }
4580 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004581 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004582 }
4583 }
Hochi Huang327cb702014-09-21 09:47:31 +08004584 if ((device2 == AUDIO_DEVICE_NONE) &&
4585 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
4586 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4587 }
Eric Laurente552edb2014-03-10 17:42:56 -07004588 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004589 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004590 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004591 if ((device2 == AUDIO_DEVICE_NONE)) {
4592 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4593 }
Eric Laurente552edb2014-03-10 17:42:56 -07004594 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004595 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004596 }
4597 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004598 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004599 }
4600 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004601 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004602 }
4603 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004604 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004605 }
4606 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
4607 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07004608 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004609 }
4610 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004611 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004612 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004613 }
4614 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004615 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004616 }
Jungshik Jang839e4f32014-06-26 17:23:40 +09004617 int device3 = AUDIO_DEVICE_NONE;
4618 if (strategy == STRATEGY_MEDIA) {
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004619 // ARC, SPDIF and AUX_LINE can co-exist with others.
Jungshik Jang0c943092014-07-08 22:11:24 +09004620 device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC;
4621 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004622 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE);
Jungshik Jang839e4f32014-06-26 17:23:40 +09004623 }
Eric Laurente552edb2014-03-10 17:42:56 -07004624
Jungshik Jang839e4f32014-06-26 17:23:40 +09004625 device2 |= device3;
Eric Laurente552edb2014-03-10 17:42:56 -07004626 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
4627 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
4628 device |= device2;
Jungshik Jang839e4f32014-06-26 17:23:40 +09004629
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004630 // If hdmi system audio mode is on, remove speaker out of output list.
4631 if ((strategy == STRATEGY_MEDIA) &&
4632 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
4633 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
4634 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
4635 }
4636
Eric Laurente552edb2014-03-10 17:42:56 -07004637 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004638 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004639 if (device == AUDIO_DEVICE_NONE) {
4640 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
4641 }
4642 } break;
4643
4644 default:
4645 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
4646 break;
4647 }
4648
4649 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
4650 return device;
4651}
4652
Eric Laurente0720872014-03-11 09:30:41 -07004653void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004654{
4655 for (int i = 0; i < NUM_STRATEGIES; i++) {
4656 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4657 }
4658 mPreviousOutputs = mOutputs;
4659}
4660
Eric Laurent1f2f2232014-06-02 12:01:23 -07004661uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004662 audio_devices_t prevDevice,
4663 uint32_t delayMs)
4664{
4665 // mute/unmute strategies using an incompatible device combination
4666 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4667 // if unmuting, unmute only after the specified delay
4668 if (outputDesc->isDuplicated()) {
4669 return 0;
4670 }
4671
4672 uint32_t muteWaitMs = 0;
4673 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004674 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004675
4676 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4677 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurent31551f82014-10-10 18:21:56 -07004678 curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004679 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4680 bool doMute = false;
4681
4682 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4683 doMute = true;
4684 outputDesc->mStrategyMutedByDevice[i] = true;
4685 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4686 doMute = true;
4687 outputDesc->mStrategyMutedByDevice[i] = false;
4688 }
Eric Laurent99401132014-05-07 19:48:15 -07004689 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004690 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004691 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004692 // skip output if it does not share any device with current output
4693 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4694 == AUDIO_DEVICE_NONE) {
4695 continue;
4696 }
4697 audio_io_handle_t curOutput = mOutputs.keyAt(j);
4698 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
4699 mute ? "muting" : "unmuting", i, curDevice, curOutput);
4700 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
4701 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004702 if (mute) {
4703 // FIXME: should not need to double latency if volume could be applied
4704 // immediately by the audioflinger mixer. We must account for the delay
4705 // between now and the next time the audioflinger thread for this output
4706 // will process a buffer (which corresponds to one buffer size,
4707 // usually 1/2 or 1/4 of the latency).
4708 if (muteWaitMs < desc->latency() * 2) {
4709 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004710 }
4711 }
4712 }
4713 }
4714 }
4715 }
4716
Eric Laurent99401132014-05-07 19:48:15 -07004717 // temporary mute output if device selection changes to avoid volume bursts due to
4718 // different per device volumes
4719 if (outputDesc->isActive() && (device != prevDevice)) {
4720 if (muteWaitMs < outputDesc->latency() * 2) {
4721 muteWaitMs = outputDesc->latency() * 2;
4722 }
4723 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4724 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004725 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07004726 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07004727 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07004728 muteWaitMs *2, device);
4729 }
4730 }
4731 }
4732
Eric Laurente552edb2014-03-10 17:42:56 -07004733 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4734 if (muteWaitMs > delayMs) {
4735 muteWaitMs -= delayMs;
4736 usleep(muteWaitMs * 1000);
4737 return muteWaitMs;
4738 }
4739 return 0;
4740}
4741
Eric Laurente0720872014-03-11 09:30:41 -07004742uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004743 audio_devices_t device,
4744 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004745 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004746 audio_patch_handle_t *patchHandle,
4747 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004748{
4749 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004750 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004751 AudioParameter param;
4752 uint32_t muteWaitMs;
4753
4754 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004755 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
4756 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004757 return muteWaitMs;
4758 }
4759 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4760 // output profile
4761 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004762 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004763 return 0;
4764 }
4765
4766 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07004767 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004768
4769 audio_devices_t prevDevice = outputDesc->mDevice;
4770
4771 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
4772
4773 if (device != AUDIO_DEVICE_NONE) {
4774 outputDesc->mDevice = device;
4775 }
4776 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4777
4778 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004779 // the requested device is AUDIO_DEVICE_NONE
4780 // OR the requested device is the same as current device
4781 // AND force is not specified
4782 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004783 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentb80a2a82014-10-27 16:07:59 -07004784 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force &&
4785 outputDesc->mPatchHandle != 0) {
4786 ALOGV("setOutputDevice() setting same device %04x or null device for output %d",
4787 device, output);
Eric Laurente552edb2014-03-10 17:42:56 -07004788 return muteWaitMs;
4789 }
4790
4791 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004792
Eric Laurente552edb2014-03-10 17:42:56 -07004793 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004794 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004795 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004796 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004797 DeviceVector deviceList = (address == NULL) ?
4798 mAvailableOutputDevices.getDevicesFromType(device)
4799 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004800 if (!deviceList.isEmpty()) {
4801 struct audio_patch patch;
4802 outputDesc->toAudioPortConfig(&patch.sources[0]);
4803 patch.num_sources = 1;
4804 patch.num_sinks = 0;
4805 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4806 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004807 patch.num_sinks++;
4808 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004809 ssize_t index;
4810 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4811 index = mAudioPatches.indexOfKey(*patchHandle);
4812 } else {
4813 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4814 }
4815 sp< AudioPatch> patchDesc;
4816 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4817 if (index >= 0) {
4818 patchDesc = mAudioPatches.valueAt(index);
4819 afPatchHandle = patchDesc->mAfPatchHandle;
4820 }
4821
Eric Laurent1c333e22014-05-20 10:48:17 -07004822 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004823 &afPatchHandle,
4824 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004825 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4826 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004827 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004828 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004829 if (index < 0) {
4830 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
4831 &patch, mUidCached);
4832 addAudioPatch(patchDesc->mHandle, patchDesc);
4833 } else {
4834 patchDesc->mPatch = patch;
4835 }
4836 patchDesc->mAfPatchHandle = afPatchHandle;
4837 patchDesc->mUid = mUidCached;
4838 if (patchHandle) {
4839 *patchHandle = patchDesc->mHandle;
4840 }
4841 outputDesc->mPatchHandle = patchDesc->mHandle;
4842 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004843 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004844 }
4845 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004846
4847 // inform all input as well
4848 for (size_t i = 0; i < mInputs.size(); i++) {
4849 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
4850 if (!isVirtualInputDevice(inputDescriptor->mDevice)) {
4851 AudioParameter inputCmd = AudioParameter();
4852 ALOGV("%s: inform input %d of device:%d", __func__,
4853 inputDescriptor->mIoHandle, device);
4854 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4855 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4856 inputCmd.toString(),
4857 delayMs);
4858 }
4859 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004860 }
Eric Laurente552edb2014-03-10 17:42:56 -07004861
4862 // update stream volumes according to new device
4863 applyStreamVolumes(output, device, delayMs);
4864
4865 return muteWaitMs;
4866}
4867
Eric Laurent1c333e22014-05-20 10:48:17 -07004868status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07004869 int delayMs,
4870 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004871{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004872 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004873 ssize_t index;
4874 if (patchHandle) {
4875 index = mAudioPatches.indexOfKey(*patchHandle);
4876 } else {
4877 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4878 }
4879 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004880 return INVALID_OPERATION;
4881 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004882 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4883 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004884 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4885 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004886 removeAudioPatch(patchDesc->mHandle);
4887 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004888 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004889 return status;
4890}
4891
4892status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4893 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004894 bool force,
4895 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004896{
4897 status_t status = NO_ERROR;
4898
Eric Laurent1f2f2232014-06-02 12:01:23 -07004899 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004900 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4901 inputDesc->mDevice = device;
4902
4903 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4904 if (!deviceList.isEmpty()) {
4905 struct audio_patch patch;
4906 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004907 // AUDIO_SOURCE_HOTWORD is for internal use only:
4908 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004909 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4910 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004911 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4912 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004913 patch.num_sinks = 1;
4914 //only one input device for now
4915 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004916 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004917 ssize_t index;
4918 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4919 index = mAudioPatches.indexOfKey(*patchHandle);
4920 } else {
4921 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4922 }
4923 sp< AudioPatch> patchDesc;
4924 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4925 if (index >= 0) {
4926 patchDesc = mAudioPatches.valueAt(index);
4927 afPatchHandle = patchDesc->mAfPatchHandle;
4928 }
4929
Eric Laurent1c333e22014-05-20 10:48:17 -07004930 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004931 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004932 0);
4933 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004934 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004935 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004936 if (index < 0) {
4937 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
4938 &patch, mUidCached);
4939 addAudioPatch(patchDesc->mHandle, patchDesc);
4940 } else {
4941 patchDesc->mPatch = patch;
4942 }
4943 patchDesc->mAfPatchHandle = afPatchHandle;
4944 patchDesc->mUid = mUidCached;
4945 if (patchHandle) {
4946 *patchHandle = patchDesc->mHandle;
4947 }
4948 inputDesc->mPatchHandle = patchDesc->mHandle;
4949 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004950 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004951 }
4952 }
4953 }
4954 return status;
4955}
4956
Eric Laurent6a94d692014-05-20 11:18:06 -07004957status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4958 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004959{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004960 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004961 ssize_t index;
4962 if (patchHandle) {
4963 index = mAudioPatches.indexOfKey(*patchHandle);
4964 } else {
4965 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4966 }
4967 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004968 return INVALID_OPERATION;
4969 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004970 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4971 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004972 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4973 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004974 removeAudioPatch(patchDesc->mHandle);
4975 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004976 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004977 return status;
4978}
4979
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004980sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -08004981 String8 address,
Glenn Kastencbd48022014-07-24 13:46:44 -07004982 uint32_t& samplingRate,
Eric Laurente552edb2014-03-10 17:42:56 -07004983 audio_format_t format,
Glenn Kasten6a8ab052014-07-24 14:08:35 -07004984 audio_channel_mask_t channelMask,
Glenn Kastencbd48022014-07-24 13:46:44 -07004985 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004986{
4987 // Choose an input profile based on the requested capture parameters: select the first available
4988 // profile supporting all requested parameters.
4989
4990 for (size_t i = 0; i < mHwModules.size(); i++)
4991 {
4992 if (mHwModules[i]->mHandle == 0) {
4993 continue;
4994 }
4995 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4996 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004997 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004998 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004999 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005000 &samplingRate /*updatedSamplingRate*/,
5001 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005002
Eric Laurente552edb2014-03-10 17:42:56 -07005003 return profile;
5004 }
5005 }
5006 }
5007 return NULL;
5008}
5009
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005010
5011audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
Eric Laurentc722f302014-12-10 11:21:49 -08005012 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07005013{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005014 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
5015 ~AUDIO_DEVICE_BIT_IN;
Eric Laurent275e8e92014-11-30 15:14:47 -08005016
5017 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
5018 if (mPolicyMixes[i]->mMix.mMixType != MIX_TYPE_RECORDERS) {
5019 continue;
5020 }
5021 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
5022 if ((RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5023 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource == inputSource) ||
5024 (RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5025 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource != inputSource)) {
5026 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurentc722f302014-12-10 11:21:49 -08005027 if (policyMix != NULL) {
5028 *policyMix = &mPolicyMixes[i]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005029 }
5030 return AUDIO_DEVICE_IN_REMOTE_SUBMIX;
5031 }
5032 break;
5033 }
5034 }
5035 }
5036
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005037 return getDeviceForInputSource(inputSource);
5038}
5039
5040audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5041{
5042 uint32_t device = AUDIO_DEVICE_NONE;
5043 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
5044 ~AUDIO_DEVICE_BIT_IN;
5045
Eric Laurente552edb2014-03-10 17:42:56 -07005046 switch (inputSource) {
5047 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005048 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005049 device = AUDIO_DEVICE_IN_VOICE_CALL;
5050 break;
5051 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005052 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005053
5054 case AUDIO_SOURCE_DEFAULT:
5055 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07005056 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
5057 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
Eric Laurentdc136ff2014-12-16 12:24:18 -08005058 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
5059 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
5060 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurentc2730ba2014-07-20 15:47:07 -07005061 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5062 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5063 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5064 device = AUDIO_DEVICE_IN_USB_DEVICE;
5065 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5066 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005067 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005068 break;
5069
5070 case AUDIO_SOURCE_VOICE_COMMUNICATION:
5071 // Allow only use of devices on primary input if in call and HAL does not support routing
5072 // to voice call path.
5073 if ((mPhoneState == AUDIO_MODE_IN_CALL) &&
5074 (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
5075 availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN;
5076 }
5077
5078 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
5079 case AUDIO_POLICY_FORCE_BT_SCO:
5080 // if SCO device is requested but no SCO device is available, fall back to default case
5081 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
5082 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
5083 break;
5084 }
5085 // FALL THROUGH
5086
5087 default: // FORCE_NONE
5088 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5089 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5090 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5091 device = AUDIO_DEVICE_IN_USB_DEVICE;
5092 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5093 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5094 }
5095 break;
5096
5097 case AUDIO_POLICY_FORCE_SPEAKER:
5098 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
5099 device = AUDIO_DEVICE_IN_BACK_MIC;
5100 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5101 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5102 }
5103 break;
5104 }
5105 break;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005106
Eric Laurente552edb2014-03-10 17:42:56 -07005107 case AUDIO_SOURCE_VOICE_RECOGNITION:
5108 case AUDIO_SOURCE_HOTWORD:
Eric Laurent3b73df72014-03-11 09:06:29 -07005109 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005110 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005111 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005112 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005113 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07005114 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5115 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005116 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005117 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5118 }
5119 break;
5120 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005121 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005122 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005123 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005124 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5125 }
5126 break;
5127 case AUDIO_SOURCE_VOICE_DOWNLINK:
5128 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005129 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005130 device = AUDIO_DEVICE_IN_VOICE_CALL;
5131 }
5132 break;
5133 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005134 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07005135 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
5136 }
5137 break;
Hochi Huang327cb702014-09-21 09:47:31 +08005138 case AUDIO_SOURCE_FM_TUNER:
5139 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
5140 device = AUDIO_DEVICE_IN_FM_TUNER;
5141 }
5142 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005143 default:
5144 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
5145 break;
5146 }
5147 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
5148 return device;
5149}
5150
Eric Laurente0720872014-03-11 09:30:41 -07005151bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005152{
5153 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
5154 device &= ~AUDIO_DEVICE_BIT_IN;
5155 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
5156 return true;
5157 }
5158 return false;
5159}
5160
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005161bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005162 return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL & ~AUDIO_DEVICE_BIT_IN) != 0);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005163}
5164
Eric Laurente0720872014-03-11 09:30:41 -07005165audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07005166{
5167 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005168 const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07005169 if ((input_descriptor->mRefCount > 0)
5170 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
5171 return mInputs.keyAt(i);
5172 }
5173 }
5174 return 0;
5175}
5176
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005177uint32_t AudioPolicyManager::activeInputsCount() const
5178{
5179 uint32_t count = 0;
5180 for (size_t i = 0; i < mInputs.size(); i++) {
5181 const sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
5182 if (desc->mRefCount > 0) {
Eric Laurenta34c9ce2014-12-19 11:16:32 -08005183 count++;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005184 }
5185 }
5186 return count;
5187}
5188
Eric Laurente552edb2014-03-10 17:42:56 -07005189
Eric Laurente0720872014-03-11 09:30:41 -07005190void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07005191{
5192 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005193 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07005194 mStreams[i].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005195 ApmGains::sVolumeProfiles[i][j];
Eric Laurente552edb2014-03-10 17:42:56 -07005196 }
5197 }
5198
5199 // Check availability of DRC on speaker path: if available, override some of the speaker curves
5200 if (mSpeakerDrcEnabled) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005201 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5202 ApmGains::sDefaultSystemVolumeCurveDrc;
5203 mStreams[AUDIO_STREAM_RING].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5204 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5205 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5206 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5207 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5208 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5209 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5210 ApmGains::sSpeakerMediaVolumeCurveDrc;
5211 mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5212 ApmGains::sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07005213 }
5214}
5215
Eric Laurente0720872014-03-11 09:30:41 -07005216float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005217 int index,
5218 audio_io_handle_t output,
5219 audio_devices_t device)
5220{
5221 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005222 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005223 StreamDescriptor &streamDesc = mStreams[stream];
5224
5225 if (device == AUDIO_DEVICE_NONE) {
5226 device = outputDesc->device();
5227 }
5228
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005229 volume = ApmGains::volIndexToAmpl(device, streamDesc, index);
Eric Laurente552edb2014-03-10 17:42:56 -07005230
5231 // if a headset is connected, apply the following rules to ring tones and notifications
5232 // to avoid sound level bursts in user's ears:
5233 // - always attenuate ring tones and notifications volume by 6dB
5234 // - if music is playing, always limit the volume to current music volume,
5235 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005236 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005237 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5238 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5239 AUDIO_DEVICE_OUT_WIRED_HEADSET |
5240 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
5241 ((stream_strategy == STRATEGY_SONIFICATION)
5242 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005243 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005244 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005245 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005246 streamDesc.mCanBeMuted) {
5247 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
5248 // when the phone is ringing we must consider that music could have been paused just before
5249 // by the music application and behave as if music was active if the last music track was
5250 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005251 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005252 mLimitRingtoneVolume) {
5253 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07005254 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
5255 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07005256 output,
5257 musicDevice);
5258 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
5259 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
5260 if (volume > minVol) {
5261 volume = minVol;
5262 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
5263 }
5264 }
5265 }
5266
5267 return volume;
5268}
5269
Eric Laurente0720872014-03-11 09:30:41 -07005270status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005271 int index,
5272 audio_io_handle_t output,
5273 audio_devices_t device,
5274 int delayMs,
5275 bool force)
5276{
5277
5278 // do not change actual stream volume if the stream is muted
5279 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
5280 ALOGVV("checkAndSetVolume() stream %d muted count %d",
5281 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
5282 return NO_ERROR;
5283 }
5284
5285 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07005286 if ((stream == AUDIO_STREAM_VOICE_CALL &&
5287 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
5288 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
5289 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005290 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07005291 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07005292 return INVALID_OPERATION;
5293 }
5294
5295 float volume = computeVolume(stream, index, output, device);
Eric Laurent275e8e92014-11-30 15:14:47 -08005296 // unit gain if rerouting to external policy
5297 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
5298 ssize_t index = mOutputs.indexOfKey(output);
5299 if (index >= 0) {
5300 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurentc722f302014-12-10 11:21:49 -08005301 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005302 ALOGV("max gain when rerouting for output=%d", output);
5303 volume = 1.0f;
5304 }
5305 }
5306
5307 }
Eric Laurente552edb2014-03-10 17:42:56 -07005308 // We actually change the volume if:
5309 // - the float value returned by computeVolume() changed
5310 // - the force flag is set
5311 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
5312 force) {
5313 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
5314 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
5315 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
5316 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07005317 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
5318 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005319 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005320 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005321 }
5322
Eric Laurent3b73df72014-03-11 09:06:29 -07005323 if (stream == AUDIO_STREAM_VOICE_CALL ||
5324 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005325 float voiceVolume;
5326 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005327 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005328 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
5329 } else {
5330 voiceVolume = 1.0;
5331 }
5332
5333 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
5334 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5335 mLastVoiceVolume = voiceVolume;
5336 }
5337 }
5338
5339 return NO_ERROR;
5340}
5341
Eric Laurente0720872014-03-11 09:30:41 -07005342void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07005343 audio_devices_t device,
5344 int delayMs,
5345 bool force)
5346{
5347 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
5348
Eric Laurent3b73df72014-03-11 09:06:29 -07005349 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005350 if (stream == AUDIO_STREAM_PATCH) {
5351 continue;
5352 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005353 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005354 mStreams[stream].getVolumeIndex(device),
5355 output,
5356 device,
5357 delayMs,
5358 force);
5359 }
5360}
5361
Eric Laurente0720872014-03-11 09:30:41 -07005362void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07005363 bool on,
5364 audio_io_handle_t output,
5365 int delayMs,
5366 audio_devices_t device)
5367{
5368 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07005369 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005370 if (stream == AUDIO_STREAM_PATCH) {
5371 continue;
5372 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005373 if (getStrategy((audio_stream_type_t)stream) == strategy) {
5374 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005375 }
5376 }
5377}
5378
Eric Laurente0720872014-03-11 09:30:41 -07005379void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005380 bool on,
5381 audio_io_handle_t output,
5382 int delayMs,
5383 audio_devices_t device)
5384{
5385 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07005386 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005387 if (device == AUDIO_DEVICE_NONE) {
5388 device = outputDesc->device();
5389 }
5390
5391 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
5392 stream, on, output, outputDesc->mMuteCount[stream], device);
5393
5394 if (on) {
5395 if (outputDesc->mMuteCount[stream] == 0) {
5396 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005397 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
5398 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07005399 checkAndSetVolume(stream, 0, output, device, delayMs);
5400 }
5401 }
5402 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5403 outputDesc->mMuteCount[stream]++;
5404 } else {
5405 if (outputDesc->mMuteCount[stream] == 0) {
5406 ALOGV("setStreamMute() unmuting non muted stream!");
5407 return;
5408 }
5409 if (--outputDesc->mMuteCount[stream] == 0) {
5410 checkAndSetVolume(stream,
5411 streamDesc.getVolumeIndex(device),
5412 output,
5413 device,
5414 delayMs);
5415 }
5416 }
5417}
5418
Eric Laurente0720872014-03-11 09:30:41 -07005419void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005420 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005421{
5422 // if the stream pertains to sonification strategy and we are in call we must
5423 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5424 // in the device used for phone strategy and play the tone if the selected device does not
5425 // interfere with the device used for phone strategy
5426 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5427 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005428 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005429 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5430 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005431 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07005432 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5433 stream, starting, outputDesc->mDevice, stateChange);
5434 if (outputDesc->mRefCount[stream]) {
5435 int muteCount = 1;
5436 if (stateChange) {
5437 muteCount = outputDesc->mRefCount[stream];
5438 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005439 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005440 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5441 for (int i = 0; i < muteCount; i++) {
5442 setStreamMute(stream, starting, mPrimaryOutput);
5443 }
5444 } else {
5445 ALOGV("handleIncallSonification() high visibility");
5446 if (outputDesc->device() &
5447 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5448 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5449 for (int i = 0; i < muteCount; i++) {
5450 setStreamMute(stream, starting, mPrimaryOutput);
5451 }
5452 }
5453 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005454 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5455 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005456 } else {
5457 mpClientInterface->stopTone();
5458 }
5459 }
5460 }
5461 }
5462}
5463
Eric Laurente0720872014-03-11 09:30:41 -07005464bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07005465{
5466 return isStateInCall(mPhoneState);
5467}
5468
Eric Laurente0720872014-03-11 09:30:41 -07005469bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005470 return ((state == AUDIO_MODE_IN_CALL) ||
5471 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07005472}
5473
Eric Laurente0720872014-03-11 09:30:41 -07005474uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07005475{
5476 return MAX_EFFECTS_CPU_LOAD;
5477}
5478
Eric Laurente0720872014-03-11 09:30:41 -07005479uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07005480{
5481 return MAX_EFFECTS_MEMORY;
5482}
5483
Eric Laurent6a94d692014-05-20 11:18:06 -07005484
Eric Laurente552edb2014-03-10 17:42:56 -07005485// --- EffectDescriptor class implementation
5486
Eric Laurente0720872014-03-11 09:30:41 -07005487status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005488{
5489 const size_t SIZE = 256;
5490 char buffer[SIZE];
5491 String8 result;
5492
5493 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
5494 result.append(buffer);
5495 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
5496 result.append(buffer);
5497 snprintf(buffer, SIZE, " Session: %d\n", mSession);
5498 result.append(buffer);
5499 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
5500 result.append(buffer);
5501 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
5502 result.append(buffer);
5503 write(fd, result.string(), result.size());
5504
5505 return NO_ERROR;
5506}
5507
Eric Laurent3a4311c2014-03-17 12:00:47 -07005508
5509// --- audio_policy.conf file parsing
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005510// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005511void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005512{
Eric Laurente552edb2014-03-10 17:42:56 -07005513 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005514 cnode *node;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005515 sp<HwModule> module = new HwModule(root->name);
Eric Laurente552edb2014-03-10 17:42:56 -07005516
Eric Laurent1afeecb2014-05-14 08:52:28 -07005517 node = config_find(root, DEVICES_TAG);
5518 if (node != NULL) {
5519 node = node->first_child;
5520 while (node) {
5521 ALOGV("loadHwModule() loading device %s", node->name);
5522 status_t tmpStatus = module->loadDevice(node);
5523 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5524 status = tmpStatus;
5525 }
5526 node = node->next;
5527 }
5528 }
5529 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07005530 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005531 node = node->first_child;
5532 while (node) {
5533 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005534 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005535 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5536 status = tmpStatus;
5537 }
5538 node = node->next;
5539 }
5540 }
5541 node = config_find(root, INPUTS_TAG);
5542 if (node != NULL) {
5543 node = node->first_child;
5544 while (node) {
5545 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005546 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005547 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5548 status = tmpStatus;
5549 }
5550 node = node->next;
5551 }
5552 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005553 loadGlobalConfig(root, module);
5554
Eric Laurente552edb2014-03-10 17:42:56 -07005555 if (status == NO_ERROR) {
5556 mHwModules.add(module);
Eric Laurente552edb2014-03-10 17:42:56 -07005557 }
5558}
5559
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005560// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005561void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005562{
5563 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
5564 if (node == NULL) {
5565 return;
5566 }
5567
5568 node = node->first_child;
5569 while (node) {
5570 ALOGV("loadHwModules() loading module %s", node->name);
5571 loadHwModule(node);
5572 node = node->next;
5573 }
5574}
5575
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005576// TODO candidate to be moved to ConfigParsingUtils
Eric Laurent1f2f2232014-06-02 12:01:23 -07005577void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module)
Eric Laurente552edb2014-03-10 17:42:56 -07005578{
5579 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
Eric Laurenteb108a42014-06-06 14:56:52 -07005580
Eric Laurente552edb2014-03-10 17:42:56 -07005581 if (node == NULL) {
5582 return;
5583 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005584 DeviceVector declaredDevices;
5585 if (module != NULL) {
5586 declaredDevices = module->mDeclaredDevices;
5587 }
5588
Eric Laurente552edb2014-03-10 17:42:56 -07005589 node = node->first_child;
5590 while (node) {
5591 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005592 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
5593 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005594 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
5595 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005596 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005597 audio_devices_t device = (audio_devices_t)ConfigParsingUtils::stringToEnum(
5598 sDeviceNameToEnumTable,
5599 ARRAY_SIZE(sDeviceNameToEnumTable),
5600 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005601 if (device != AUDIO_DEVICE_NONE) {
Paul McLeane743a472015-01-28 11:07:31 -08005602 mDefaultOutputDevice = new DeviceDescriptor(String8("default-output"), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005603 } else {
5604 ALOGW("loadGlobalConfig() default device not specified");
5605 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005606 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07005607 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005608 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
5609 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005610 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005611 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005612 mSpeakerDrcEnabled = ConfigParsingUtils::stringToBool((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07005613 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
Eric Laurenteb108a42014-06-06 14:56:52 -07005614 } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) {
5615 uint32_t major, minor;
5616 sscanf((char *)node->value, "%u.%u", &major, &minor);
5617 module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor);
5618 ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u",
5619 module->mHalVersion, major, minor);
Eric Laurente552edb2014-03-10 17:42:56 -07005620 }
5621 node = node->next;
5622 }
5623}
5624
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005625// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005626status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07005627{
5628 cnode *root;
5629 char *data;
5630
5631 data = (char *)load_file(path, NULL);
5632 if (data == NULL) {
5633 return -ENODEV;
5634 }
5635 root = config_node("", "");
5636 config_load(root, data);
5637
Eric Laurente552edb2014-03-10 17:42:56 -07005638 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005639 // legacy audio_policy.conf files have one global_configuration section
5640 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07005641 config_free(root);
5642 free(root);
5643 free(data);
5644
5645 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
5646
5647 return NO_ERROR;
5648}
5649
Eric Laurente0720872014-03-11 09:30:41 -07005650void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07005651{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005652 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07005653 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08005654 sp<DeviceDescriptor> defaultInputDevice =
5655 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005656 mAvailableOutputDevices.add(mDefaultOutputDevice);
5657 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005658
5659 module = new HwModule("primary");
5660
Eric Laurent1afeecb2014-05-14 08:52:28 -07005661 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005662 profile->mSamplingRates.add(44100);
5663 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5664 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005665 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005666 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
5667 module->mOutputProfiles.add(profile);
5668
Eric Laurent1afeecb2014-05-14 08:52:28 -07005669 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005670 profile->mSamplingRates.add(8000);
5671 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5672 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005673 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005674 module->mInputProfiles.add(profile);
5675
5676 mHwModules.add(module);
5677}
5678
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005679audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5680{
5681 // flags to stream type mapping
5682 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5683 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5684 }
5685 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5686 return AUDIO_STREAM_BLUETOOTH_SCO;
5687 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005688 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5689 return AUDIO_STREAM_TTS;
5690 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005691
5692 // usage to stream type mapping
5693 switch (attr->usage) {
5694 case AUDIO_USAGE_MEDIA:
5695 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005696 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5697 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005698 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005699 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5700 return AUDIO_STREAM_ALARM;
5701 }
5702 if (isStreamActive(AUDIO_STREAM_RING)) {
5703 return AUDIO_STREAM_RING;
5704 }
5705 if (isInCall()) {
5706 return AUDIO_STREAM_VOICE_CALL;
5707 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005708 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005709 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5710 return AUDIO_STREAM_SYSTEM;
5711 case AUDIO_USAGE_VOICE_COMMUNICATION:
5712 return AUDIO_STREAM_VOICE_CALL;
5713
5714 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5715 return AUDIO_STREAM_DTMF;
5716
5717 case AUDIO_USAGE_ALARM:
5718 return AUDIO_STREAM_ALARM;
5719 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5720 return AUDIO_STREAM_RING;
5721
5722 case AUDIO_USAGE_NOTIFICATION:
5723 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5724 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5725 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5726 case AUDIO_USAGE_NOTIFICATION_EVENT:
5727 return AUDIO_STREAM_NOTIFICATION;
5728
5729 case AUDIO_USAGE_UNKNOWN:
5730 default:
5731 return AUDIO_STREAM_MUSIC;
5732 }
5733}
Eric Laurente83b55d2014-11-14 10:06:21 -08005734
5735bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) {
5736 // has flags that map to a strategy?
5737 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5738 return true;
5739 }
5740
5741 // has known usage?
5742 switch (paa->usage) {
5743 case AUDIO_USAGE_UNKNOWN:
5744 case AUDIO_USAGE_MEDIA:
5745 case AUDIO_USAGE_VOICE_COMMUNICATION:
5746 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5747 case AUDIO_USAGE_ALARM:
5748 case AUDIO_USAGE_NOTIFICATION:
5749 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5750 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5751 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5752 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5753 case AUDIO_USAGE_NOTIFICATION_EVENT:
5754 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5755 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5756 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5757 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005758 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005759 break;
5760 default:
5761 return false;
5762 }
5763 return true;
5764}
5765
Eric Laurente552edb2014-03-10 17:42:56 -07005766}; // namespace android