blob: a110ada7026e15aa31b81a63963d0243e0ce192e [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) {
François Gaffie98cc1912015-03-18 17:52:40 +0100396 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700397 mCallRxPatch->mAfPatchHandle = afPatchHandle;
398 mCallRxPatch->mUid = mUidCached;
399 }
400 createTxPatch = true;
401 }
402 if (createTxPatch) {
403
404 struct audio_patch patch;
405 patch.num_sources = 1;
406 patch.num_sinks = 1;
407 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
408 ALOG_ASSERT(!deviceList.isEmpty(),
409 "updateCallRouting() selected device not in input device list");
410 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
411 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
412 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
413 ALOG_ASSERT(!deviceList.isEmpty(),
414 "updateCallRouting() no telephony TX device");
415 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
416 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
417
418 SortedVector<audio_io_handle_t> outputs =
419 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700420 audio_io_handle_t output = selectOutput(outputs,
421 AUDIO_OUTPUT_FLAG_NONE,
422 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700423 // request to reuse existing output stream if one is already opened to reach the TX
424 // path output device
425 if (output != AUDIO_IO_HANDLE_NONE) {
426 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
427 ALOG_ASSERT(!outputDesc->isDuplicated(),
428 "updateCallRouting() RX device output is duplicated");
429 outputDesc->toAudioPortConfig(&patch.sources[1]);
430 patch.num_sources = 2;
431 }
432
433 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
434 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
435 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
436 status);
437 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100438 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700439 mCallTxPatch->mAfPatchHandle = afPatchHandle;
440 mCallTxPatch->mUid = mUidCached;
441 }
442 }
443}
444
Eric Laurente0720872014-03-11 09:30:41 -0700445void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700446{
447 ALOGV("setPhoneState() state %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700448 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700449 ALOGW("setPhoneState() invalid state %d", state);
450 return;
451 }
452
453 if (state == mPhoneState ) {
454 ALOGW("setPhoneState() setting same state %d", state);
455 return;
456 }
457
458 // if leaving call state, handle special case of active streams
459 // pertaining to sonification strategy see handleIncallSonification()
460 if (isInCall()) {
461 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700462 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800463 if (stream == AUDIO_STREAM_PATCH) {
464 continue;
465 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700466 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700467 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800468
469 // force reevaluating accessibility routing when call starts
470 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
472
473 // store previous phone state for management of sonification strategy below
474 int oldState = mPhoneState;
475 mPhoneState = state;
476 bool force = false;
477
478 // are we entering or starting a call
479 if (!isStateInCall(oldState) && isStateInCall(state)) {
480 ALOGV(" Entering call in setPhoneState()");
481 // force routing command to audio hardware when starting a call
482 // even if no device change is needed
483 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800484 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700485 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800486 ApmGains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700487 }
488 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
489 ALOGV(" Exiting call in setPhoneState()");
490 // force routing command to audio hardware when exiting a call
491 // even if no device change is needed
492 force = true;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800493 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -0700494 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800495 ApmGains::sVolumeProfiles[AUDIO_STREAM_DTMF][j];
Eric Laurente552edb2014-03-10 17:42:56 -0700496 }
497 } else if (isStateInCall(state) && (state != oldState)) {
498 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
499 // force routing command to audio hardware when switching between telephony and VoIP
500 // even if no device change is needed
501 force = true;
502 }
503
504 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700505 checkA2dpSuspend();
506 checkOutputForAllStrategies();
507 updateDevicesAndOutputs();
508
Eric Laurent1f2f2232014-06-02 12:01:23 -0700509 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700510
Eric Laurente552edb2014-03-10 17:42:56 -0700511 int delayMs = 0;
512 if (isStateInCall(state)) {
513 nsecs_t sysTime = systemTime();
514 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700515 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 // mute media and sonification strategies and delay device switch by the largest
517 // latency of any output where either strategy is active.
518 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100519 if ((isStrategyActive(desc, STRATEGY_MEDIA,
520 SONIFICATION_HEADSET_MUSIC_DELAY,
521 sysTime) ||
522 isStrategyActive(desc, STRATEGY_SONIFICATION,
523 SONIFICATION_HEADSET_MUSIC_DELAY,
524 sysTime)) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700525 (delayMs < (int)desc->mLatency*2)) {
526 delayMs = desc->mLatency*2;
527 }
528 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
529 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
530 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
531 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
532 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
533 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
534 }
535 }
536
Eric Laurentc2730ba2014-07-20 15:47:07 -0700537 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
538 // the device returned is not necessarily reachable via this output
539 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
540 // force routing command to audio hardware when ending call
541 // even if no device change is needed
542 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
543 rxDevice = hwOutputDesc->device();
544 }
Eric Laurente552edb2014-03-10 17:42:56 -0700545
Eric Laurentc2730ba2014-07-20 15:47:07 -0700546 if (state == AUDIO_MODE_IN_CALL) {
547 updateCallRouting(rxDevice, delayMs);
548 } else if (oldState == AUDIO_MODE_IN_CALL) {
549 if (mCallRxPatch != 0) {
550 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
551 mCallRxPatch.clear();
552 }
553 if (mCallTxPatch != 0) {
554 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
555 mCallTxPatch.clear();
556 }
557 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
558 } else {
559 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
560 }
Eric Laurente552edb2014-03-10 17:42:56 -0700561 // if entering in call state, handle special case of active streams
562 // pertaining to sonification strategy see handleIncallSonification()
563 if (isStateInCall(state)) {
564 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700565 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800566 if (stream == AUDIO_STREAM_PATCH) {
567 continue;
568 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700569 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700570 }
571 }
572
573 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700574 if (state == AUDIO_MODE_RINGTONE &&
575 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700576 mLimitRingtoneVolume = true;
577 } else {
578 mLimitRingtoneVolume = false;
579 }
580}
581
Eric Laurente0720872014-03-11 09:30:41 -0700582void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700583 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700584{
585 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
586
587 bool forceVolumeReeval = false;
588 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700589 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
590 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
591 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700592 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
593 return;
594 }
595 forceVolumeReeval = true;
596 mForceUse[usage] = config;
597 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700598 case AUDIO_POLICY_FORCE_FOR_MEDIA:
599 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
600 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
601 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
602 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
Hochi Huang327cb702014-09-21 09:47:31 +0800603 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Eric Laurente552edb2014-03-10 17:42:56 -0700604 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
605 return;
606 }
607 mForceUse[usage] = config;
608 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 case AUDIO_POLICY_FORCE_FOR_RECORD:
610 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
611 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700612 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
613 return;
614 }
615 mForceUse[usage] = config;
616 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700617 case AUDIO_POLICY_FORCE_FOR_DOCK:
618 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
619 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
620 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
621 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
622 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700623 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
624 }
625 forceVolumeReeval = true;
626 mForceUse[usage] = config;
627 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700628 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
629 if (config != AUDIO_POLICY_FORCE_NONE &&
630 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700631 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
632 }
633 forceVolumeReeval = true;
634 mForceUse[usage] = config;
635 break;
Jungshik Jang7b24ee32014-07-15 19:38:42 +0900636 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
637 if (config != AUDIO_POLICY_FORCE_NONE &&
638 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
639 ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config);
640 }
641 mForceUse[usage] = config;
642 break;
Eric Laurente552edb2014-03-10 17:42:56 -0700643 default:
644 ALOGW("setForceUse() invalid usage %d", usage);
645 break;
646 }
647
648 // check for device and output changes triggered by new force usage
649 checkA2dpSuspend();
650 checkOutputForAllStrategies();
651 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700652 if (mPhoneState == AUDIO_MODE_IN_CALL) {
653 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
654 updateCallRouting(newDevice);
655 }
Eric Laurente552edb2014-03-10 17:42:56 -0700656 for (size_t i = 0; i < mOutputs.size(); i++) {
657 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700658 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700659 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
660 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
661 }
Eric Laurente552edb2014-03-10 17:42:56 -0700662 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
663 applyStreamVolumes(output, newDevice, 0, true);
664 }
665 }
666
667 audio_io_handle_t activeInput = getActiveInput();
668 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700669 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700670 }
671
672}
673
Eric Laurente0720872014-03-11 09:30:41 -0700674audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700675{
676 return mForceUse[usage];
677}
678
Eric Laurente0720872014-03-11 09:30:41 -0700679void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700680{
681 ALOGV("setSystemProperty() property %s, value %s", property, value);
682}
683
684// Find a direct output profile compatible with the parameters passed, even if the input flags do
685// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800686sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700687 audio_devices_t device,
688 uint32_t samplingRate,
689 audio_format_t format,
690 audio_channel_mask_t channelMask,
691 audio_output_flags_t flags)
692{
693 for (size_t i = 0; i < mHwModules.size(); i++) {
694 if (mHwModules[i]->mHandle == 0) {
695 continue;
696 }
697 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700698 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent275e8e92014-11-30 15:14:47 -0800699 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700700 NULL /*updatedSamplingRate*/, format, channelMask,
701 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
702 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700703 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
704 return profile;
705 }
Eric Laurente552edb2014-03-10 17:42:56 -0700706 }
707 }
708 return 0;
709}
710
Eric Laurente0720872014-03-11 09:30:41 -0700711audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700712 uint32_t samplingRate,
713 audio_format_t format,
714 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700715 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700716 const audio_offload_info_t *offloadInfo)
717{
Eric Laurent3b73df72014-03-11 09:06:29 -0700718 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700719 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
720 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
721 device, stream, samplingRate, format, channelMask, flags);
722
Eric Laurente83b55d2014-11-14 10:06:21 -0800723 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
724 stream, samplingRate,format, channelMask,
725 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726}
727
Eric Laurente83b55d2014-11-14 10:06:21 -0800728status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
729 audio_io_handle_t *output,
730 audio_session_t session,
731 audio_stream_type_t *stream,
732 uint32_t samplingRate,
733 audio_format_t format,
734 audio_channel_mask_t channelMask,
735 audio_output_flags_t flags,
736 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700737{
Eric Laurente83b55d2014-11-14 10:06:21 -0800738 audio_attributes_t attributes;
739 if (attr != NULL) {
740 if (!isValidAttributes(attr)) {
741 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
742 attr->usage, attr->content_type, attr->flags,
743 attr->tags);
744 return BAD_VALUE;
745 }
746 attributes = *attr;
747 } else {
748 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
749 ALOGE("getOutputForAttr(): invalid stream type");
750 return BAD_VALUE;
751 }
752 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700753 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800754
Eric Laurent275e8e92014-11-30 15:14:47 -0800755 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
756 sp<AudioOutputDescriptor> desc;
757 if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_PLAYERS) {
758 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
759 if ((RULE_MATCH_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
760 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage == attributes.usage) ||
761 (RULE_EXCLUDE_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
762 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage != attributes.usage)) {
763 desc = mPolicyMixes[i]->mOutput;
764 break;
765 }
766 if (strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
767 strncmp(attributes.tags + strlen("addr="),
768 mPolicyMixes[i]->mMix.mRegistrationId.string(),
769 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
770 desc = mPolicyMixes[i]->mOutput;
771 break;
772 }
773 }
774 } else if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_RECORDERS) {
775 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE &&
776 strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
777 strncmp(attributes.tags + strlen("addr="),
778 mPolicyMixes[i]->mMix.mRegistrationId.string(),
779 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
780 desc = mPolicyMixes[i]->mOutput;
Eric Laurent275e8e92014-11-30 15:14:47 -0800781 }
782 }
783 if (desc != 0) {
784 if (!audio_is_linear_pcm(format)) {
785 return BAD_VALUE;
786 }
Eric Laurentc722f302014-12-10 11:21:49 -0800787 desc->mPolicyMix = &mPolicyMixes[i]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -0800788 *stream = streamTypefromAttributesInt(&attributes);
789 *output = desc->mIoHandle;
790 ALOGV("getOutputForAttr() returns output %d", *output);
791 return NO_ERROR;
792 }
793 }
794 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
795 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
796 return BAD_VALUE;
797 }
798
Eric Laurent93c3d412014-08-01 14:48:35 -0700799 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
Eric Laurente83b55d2014-11-14 10:06:21 -0800800 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700801
Eric Laurente83b55d2014-11-14 10:06:21 -0800802 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700803 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700804
Eric Laurente83b55d2014-11-14 10:06:21 -0800805 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700806 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
807 }
808
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700809 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700810 device, samplingRate, format, channelMask, flags);
811
Eric Laurente83b55d2014-11-14 10:06:21 -0800812 *stream = streamTypefromAttributesInt(&attributes);
813 *output = getOutputForDevice(device, session, *stream,
814 samplingRate, format, channelMask,
815 flags, offloadInfo);
816 if (*output == AUDIO_IO_HANDLE_NONE) {
817 return INVALID_OPERATION;
818 }
819 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700820}
821
822audio_io_handle_t AudioPolicyManager::getOutputForDevice(
823 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800824 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700825 audio_stream_type_t stream,
826 uint32_t samplingRate,
827 audio_format_t format,
828 audio_channel_mask_t channelMask,
829 audio_output_flags_t flags,
830 const audio_offload_info_t *offloadInfo)
831{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700832 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700833 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700834 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700835
Eric Laurente552edb2014-03-10 17:42:56 -0700836#ifdef AUDIO_POLICY_TEST
837 if (mCurOutput != 0) {
838 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
839 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
840
841 if (mTestOutputs[mCurOutput] == 0) {
842 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700843 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700844 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700845 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700846 outputDesc->mFlags =
847 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700848 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700849 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
850 config.sample_rate = mTestSamplingRate;
851 config.channel_mask = mTestChannels;
852 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700853 if (offloadInfo != NULL) {
854 config.offload_info = *offloadInfo;
855 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700856 status = mpClientInterface->openOutput(0,
857 &mTestOutputs[mCurOutput],
858 &config,
859 &outputDesc->mDevice,
860 String8(""),
861 &outputDesc->mLatency,
862 outputDesc->mFlags);
863 if (status == NO_ERROR) {
864 outputDesc->mSamplingRate = config.sample_rate;
865 outputDesc->mFormat = config.format;
866 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700867 AudioParameter outputCmd = AudioParameter();
868 outputCmd.addInt(String8("set_id"),mCurOutput);
869 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
870 addOutput(mTestOutputs[mCurOutput], outputDesc);
871 }
872 }
873 return mTestOutputs[mCurOutput];
874 }
875#endif //AUDIO_POLICY_TEST
876
877 // open a direct output if required by specified parameters
878 //force direct flag if offload flag is set: offloading implies a direct output stream
879 // and all common behaviors are driven by checking only the direct flag
880 // this should normally be set appropriately in the policy configuration file
881 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700882 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700883 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700884 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
885 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
886 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800887 // only allow deep buffering for music stream type
888 if (stream != AUDIO_STREAM_MUSIC) {
889 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
890 }
Eric Laurente552edb2014-03-10 17:42:56 -0700891
Eric Laurentb732cf52014-09-24 19:08:21 -0700892 sp<IOProfile> profile;
893
894 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700895 // and not explicitly requested
896 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
897 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700898 audio_channel_count_from_out_mask(channelMask) <= 2) {
899 goto non_direct_output;
900 }
901
Eric Laurente552edb2014-03-10 17:42:56 -0700902 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
903 // creating an offloaded track and tearing it down immediately after start when audioflinger
904 // detects there is an active non offloadable effect.
905 // FIXME: We should check the audio session here but we do not have it in this context.
906 // This may prevent offloading in rare situations where effects are left active by apps
907 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700908
Eric Laurente552edb2014-03-10 17:42:56 -0700909 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
910 !isNonOffloadableEffectEnabled()) {
911 profile = getProfileForDirectOutput(device,
912 samplingRate,
913 format,
914 channelMask,
915 (audio_output_flags_t)flags);
916 }
917
Eric Laurent1c333e22014-05-20 10:48:17 -0700918 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700919 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700920
921 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700922 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700923 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
924 outputDesc = desc;
925 // reuse direct output if currently open and configured with same parameters
926 if ((samplingRate == outputDesc->mSamplingRate) &&
927 (format == outputDesc->mFormat) &&
928 (channelMask == outputDesc->mChannelMask)) {
929 outputDesc->mDirectOpenCount++;
930 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
931 return mOutputs.keyAt(i);
932 }
933 }
934 }
935 // close direct output if currently open and configured with different parameters
936 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700937 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700938 }
939 outputDesc = new AudioOutputDescriptor(profile);
940 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700941 outputDesc->mLatency = 0;
942 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700943 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
944 config.sample_rate = samplingRate;
945 config.channel_mask = channelMask;
946 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700947 if (offloadInfo != NULL) {
948 config.offload_info = *offloadInfo;
949 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700950 status = mpClientInterface->openOutput(profile->mModule->mHandle,
951 &output,
952 &config,
953 &outputDesc->mDevice,
954 String8(""),
955 &outputDesc->mLatency,
956 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700957
958 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700959 if (status != NO_ERROR ||
960 (samplingRate != 0 && samplingRate != config.sample_rate) ||
961 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
962 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700963 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
964 "format %d %d, channelMask %04x %04x", output, samplingRate,
965 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
966 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700967 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700968 mpClientInterface->closeOutput(output);
969 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800970 // fall back to mixer output if possible when the direct output could not be open
971 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
972 goto non_direct_output;
973 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800974 // fall back to mixer output if possible when the direct output could not be open
975 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
976 goto non_direct_output;
977 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700978 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700979 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700980 outputDesc->mSamplingRate = config.sample_rate;
981 outputDesc->mChannelMask = config.channel_mask;
982 outputDesc->mFormat = config.format;
983 outputDesc->mRefCount[stream] = 0;
984 outputDesc->mStopTime[stream] = 0;
985 outputDesc->mDirectOpenCount = 1;
986
Eric Laurente552edb2014-03-10 17:42:56 -0700987 audio_io_handle_t srcOutput = getOutputForEffect();
988 addOutput(output, outputDesc);
989 audio_io_handle_t dstOutput = getOutputForEffect();
990 if (dstOutput == output) {
991 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
992 }
993 mPreviousOutputs = mOutputs;
994 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700995 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700996 return output;
997 }
998
Eric Laurentb732cf52014-09-24 19:08:21 -0700999non_direct_output:
1000
Eric Laurente552edb2014-03-10 17:42:56 -07001001 // ignoring channel mask due to downmix capability in mixer
1002
1003 // open a non direct output
1004
1005 // for non direct outputs, only PCM is supported
1006 if (audio_is_linear_pcm(format)) {
1007 // get which output is suitable for the specified stream. The actual
1008 // routing change will happen when startOutput() will be called
1009 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1010
Eric Laurent8838a382014-09-08 16:44:28 -07001011 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1012 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1013 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001014 }
1015 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1016 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1017
1018 ALOGV("getOutput() returns output %d", output);
1019
1020 return output;
1021}
1022
Eric Laurente0720872014-03-11 09:30:41 -07001023audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001024 audio_output_flags_t flags,
1025 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001026{
1027 // select one output among several that provide a path to a particular device or set of
1028 // devices (the list was previously build by getOutputsForDevice()).
1029 // The priority is as follows:
1030 // 1: the output with the highest number of requested policy flags
1031 // 2: the primary output
1032 // 3: the first output in the list
1033
1034 if (outputs.size() == 0) {
1035 return 0;
1036 }
1037 if (outputs.size() == 1) {
1038 return outputs[0];
1039 }
1040
1041 int maxCommonFlags = 0;
1042 audio_io_handle_t outputFlags = 0;
1043 audio_io_handle_t outputPrimary = 0;
1044
1045 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001046 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001047 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001048 // if a valid format is specified, skip output if not compatible
1049 if (format != AUDIO_FORMAT_INVALID) {
1050 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1051 if (format != outputDesc->mFormat) {
1052 continue;
1053 }
1054 } else if (!audio_is_linear_pcm(format)) {
1055 continue;
1056 }
1057 }
1058
Eric Laurent3b73df72014-03-11 09:06:29 -07001059 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001060 if (commonFlags > maxCommonFlags) {
1061 outputFlags = outputs[i];
1062 maxCommonFlags = commonFlags;
1063 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1064 }
1065 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1066 outputPrimary = outputs[i];
1067 }
1068 }
1069 }
1070
1071 if (outputFlags != 0) {
1072 return outputFlags;
1073 }
1074 if (outputPrimary != 0) {
1075 return outputPrimary;
1076 }
1077
1078 return outputs[0];
1079}
1080
Eric Laurente0720872014-03-11 09:30:41 -07001081status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001082 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001083 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001084{
1085 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
1086 ssize_t index = mOutputs.indexOfKey(output);
1087 if (index < 0) {
1088 ALOGW("startOutput() unknown output %d", output);
1089 return BAD_VALUE;
1090 }
1091
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001092 // cannot start playback of STREAM_TTS if any other output is being used
1093 uint32_t beaconMuteLatency = 0;
1094 if (stream == AUDIO_STREAM_TTS) {
1095 ALOGV("\t found BEACON stream");
1096 if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1097 return INVALID_OPERATION;
1098 } else {
1099 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1100 }
1101 } else {
1102 // some playback other than beacon starts
1103 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1104 }
1105
Eric Laurent1f2f2232014-06-02 12:01:23 -07001106 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001107
1108 // increment usage count for this stream on the requested output:
1109 // NOTE that the usage count is the same for duplicated output and hardware output which is
1110 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1111 outputDesc->changeRefCount(stream, 1);
1112
1113 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001114 // starting an output being rerouted?
1115 audio_devices_t newDevice;
Eric Laurentc722f302014-12-10 11:21:49 -08001116 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001117 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1118 } else {
1119 newDevice = getNewOutputDevice(output, false /*fromCache*/);
1120 }
Eric Laurente552edb2014-03-10 17:42:56 -07001121 routing_strategy strategy = getStrategy(stream);
1122 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001123 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1124 (beaconMuteLatency > 0);
1125 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001126 bool force = false;
1127 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001128 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001129 if (desc != outputDesc) {
1130 // force a device change if any other output is managed by the same hw
1131 // module and has a current device selection that differs from selected device.
1132 // In this case, the audio HAL must receive the new device selection so that it can
1133 // change the device currently selected by the other active output.
1134 if (outputDesc->sharesHwModuleWith(desc) &&
1135 desc->device() != newDevice) {
1136 force = true;
1137 }
1138 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001139 // a notification so that audio focus effect can propagate, or that a mute/unmute
1140 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001141 uint32_t latency = desc->latency();
1142 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1143 waitMs = latency;
1144 }
1145 }
1146 }
1147 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
1148
1149 // handle special case for sonification while in call
1150 if (isInCall()) {
1151 handleIncallSonification(stream, true, false);
1152 }
1153
1154 // apply volume rules for current stream and device if necessary
1155 checkAndSetVolume(stream,
1156 mStreams[stream].getVolumeIndex(newDevice),
1157 output,
1158 newDevice);
1159
1160 // update the outputs if starting an output with a stream that can affect notification
1161 // routing
1162 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001163
1164 // Automatically enable the remote submix input when output is started on a re routing mix
1165 // of type MIX_TYPE_RECORDERS
1166 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1167 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001168 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001169 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001170 outputDesc->mPolicyMix->mRegistrationId,
1171 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001172 }
1173
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001174 // force reevaluating accessibility routing when ringtone or alarm starts
1175 if (strategy == STRATEGY_SONIFICATION) {
1176 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1177 }
1178
Eric Laurente552edb2014-03-10 17:42:56 -07001179 if (waitMs > muteWaitMs) {
1180 usleep((waitMs - muteWaitMs) * 2 * 1000);
1181 }
1182 }
1183 return NO_ERROR;
1184}
1185
1186
Eric Laurente0720872014-03-11 09:30:41 -07001187status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001188 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001189 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001190{
1191 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1192 ssize_t index = mOutputs.indexOfKey(output);
1193 if (index < 0) {
1194 ALOGW("stopOutput() unknown output %d", output);
1195 return BAD_VALUE;
1196 }
1197
Eric Laurent1f2f2232014-06-02 12:01:23 -07001198 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001199
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001200 // always handle stream stop, check which stream type is stopping
1201 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1202
Eric Laurente552edb2014-03-10 17:42:56 -07001203 // handle special case for sonification while in call
1204 if (isInCall()) {
1205 handleIncallSonification(stream, false, false);
1206 }
1207
1208 if (outputDesc->mRefCount[stream] > 0) {
1209 // decrement usage count of this stream on the output
1210 outputDesc->changeRefCount(stream, -1);
1211 // store time at which the stream was stopped - see isStreamActive()
1212 if (outputDesc->mRefCount[stream] == 0) {
Eric Laurentc722f302014-12-10 11:21:49 -08001213 // Automatically disable the remote submix input when output is stopped on a
1214 // re routing mix of type MIX_TYPE_RECORDERS
1215 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1216 outputDesc->mPolicyMix != NULL &&
1217 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001218 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001219 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001220 outputDesc->mPolicyMix->mRegistrationId,
1221 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001222 }
1223
Eric Laurente552edb2014-03-10 17:42:56 -07001224 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -07001225 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001226 // delay the device switch by twice the latency because stopOutput() is executed when
1227 // the track stop() command is received and at that time the audio track buffer can
1228 // still contain data that needs to be drained. The latency only covers the audio HAL
1229 // and kernel buffers. Also the latency does not always include additional delay in the
1230 // audio path (audio DSP, CODEC ...)
1231 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1232
1233 // force restoring the device selection on other active outputs if it differs from the
1234 // one being selected for this output
1235 for (size_t i = 0; i < mOutputs.size(); i++) {
1236 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001237 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001238 if (curOutput != output &&
1239 desc->isActive() &&
1240 outputDesc->sharesHwModuleWith(desc) &&
1241 (newDevice != desc->device())) {
1242 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -07001243 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001244 true,
1245 outputDesc->mLatency*2);
1246 }
1247 }
1248 // update the outputs if stopping one with a stream that can affect notification routing
1249 handleNotificationRoutingForStream(stream);
1250 }
1251 return NO_ERROR;
1252 } else {
1253 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1254 return INVALID_OPERATION;
1255 }
1256}
1257
Eric Laurente83b55d2014-11-14 10:06:21 -08001258void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001259 audio_stream_type_t stream __unused,
1260 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001261{
1262 ALOGV("releaseOutput() %d", output);
1263 ssize_t index = mOutputs.indexOfKey(output);
1264 if (index < 0) {
1265 ALOGW("releaseOutput() releasing unknown output %d", output);
1266 return;
1267 }
1268
1269#ifdef AUDIO_POLICY_TEST
1270 int testIndex = testOutputIndex(output);
1271 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001272 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001273 if (outputDesc->isActive()) {
1274 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001275 mOutputs.removeItem(output);
1276 mTestOutputs[testIndex] = 0;
1277 }
1278 return;
1279 }
1280#endif //AUDIO_POLICY_TEST
1281
Eric Laurent1f2f2232014-06-02 12:01:23 -07001282 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001283 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001284 if (desc->mDirectOpenCount <= 0) {
1285 ALOGW("releaseOutput() invalid open count %d for output %d",
1286 desc->mDirectOpenCount, output);
1287 return;
1288 }
1289 if (--desc->mDirectOpenCount == 0) {
1290 closeOutput(output);
1291 // If effects where present on the output, audioflinger moved them to the primary
1292 // output by default: move them back to the appropriate output.
1293 audio_io_handle_t dstOutput = getOutputForEffect();
1294 if (dstOutput != mPrimaryOutput) {
1295 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1296 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001297 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001298 }
1299 }
1300}
1301
1302
Eric Laurentcaf7f482014-11-25 17:50:47 -08001303status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1304 audio_io_handle_t *input,
1305 audio_session_t session,
1306 uint32_t samplingRate,
1307 audio_format_t format,
1308 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001309 audio_input_flags_t flags,
1310 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001311{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001312 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1313 "session %d, flags %#x",
1314 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001315
Eric Laurentcaf7f482014-11-25 17:50:47 -08001316 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001317 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001318 audio_devices_t device;
1319 // handle legacy remote submix case where the address was not always specified
1320 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001321 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001322 audio_source_t inputSource = attr->source;
1323 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001324 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001325
Eric Laurentc447ded2015-01-06 08:47:05 -08001326 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1327 inputSource = AUDIO_SOURCE_MIC;
1328 }
1329 halInputSource = inputSource;
1330
1331 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001332 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
1333 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1334 address = String8(attr->tags + strlen("addr="));
1335 ssize_t index = mPolicyMixes.indexOfKey(address);
1336 if (index < 0) {
1337 ALOGW("getInputForAttr() no policy for address %s", address.string());
1338 return BAD_VALUE;
1339 }
Eric Laurentc722f302014-12-10 11:21:49 -08001340 if (mPolicyMixes[index]->mMix.mMixType != MIX_TYPE_PLAYERS) {
1341 ALOGW("getInputForAttr() bad policy mix type for address %s", address.string());
1342 return BAD_VALUE;
1343 }
1344 policyMix = &mPolicyMixes[index]->mMix;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001345 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001346 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001347 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001348 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001349 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001350 return BAD_VALUE;
1351 }
Eric Laurentc722f302014-12-10 11:21:49 -08001352 if (policyMix != NULL) {
1353 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001354 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1355 // there is an external policy, but this input is attached to a mix of recorders,
1356 // meaning it receives audio injected into the framework, so the recorder doesn't
1357 // know about it and is therefore considered "legacy"
1358 *inputType = API_INPUT_LEGACY;
1359 } else {
1360 // recording a mix of players defined by an external policy, we're rerouting for
1361 // an external policy
1362 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1363 }
Eric Laurentc722f302014-12-10 11:21:49 -08001364 } else if (audio_is_remote_submix_device(device)) {
1365 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001366 *inputType = API_INPUT_MIX_CAPTURE;
1367 } else {
1368 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001369 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001370 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001371 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001372 case AUDIO_SOURCE_VOICE_UPLINK:
1373 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1374 break;
1375 case AUDIO_SOURCE_VOICE_DOWNLINK:
1376 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1377 break;
1378 case AUDIO_SOURCE_VOICE_CALL:
1379 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1380 break;
1381 default:
1382 break;
1383 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001384 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001385 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1386 if (index >= 0) {
1387 *input = mSoundTriggerSessions.valueFor(session);
1388 isSoundTrigger = true;
1389 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1390 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1391 } else {
1392 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1393 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001394 }
1395 }
1396
Eric Laurent275e8e92014-11-30 15:14:47 -08001397 sp<IOProfile> profile = getInputProfile(device, address,
1398 samplingRate, format, channelMask,
1399 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001400 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001401 //retry without flags
1402 audio_input_flags_t log_flags = flags;
1403 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001404 profile = getInputProfile(device, address,
1405 samplingRate, format, channelMask,
1406 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001407 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1409 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001410 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001412 }
Eric Laurente552edb2014-03-10 17:42:56 -07001413 }
1414
1415 if (profile->mModule->mHandle == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001416 ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName);
1417 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001418 }
1419
1420 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1421 config.sample_rate = samplingRate;
1422 config.channel_mask = channelMask;
1423 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001424
Eric Laurentcf2c0212014-07-25 16:20:43 -07001425 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001426 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001427 &config,
1428 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001429 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001430 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001431 flags);
1432
1433 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001434 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001435 (samplingRate != config.sample_rate) ||
1436 (format != config.format) ||
1437 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001438 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001439 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001440 if (*input != AUDIO_IO_HANDLE_NONE) {
1441 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001442 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001443 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001444 }
1445
Eric Laurent1f2f2232014-06-02 12:01:23 -07001446 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001447 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001448 inputDesc->mRefCount = 0;
1449 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001450 inputDesc->mSamplingRate = samplingRate;
1451 inputDesc->mFormat = format;
1452 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001453 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001454 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001455 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001456 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001457
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001458 ALOGV("getInputForAttr() returns input type = %d", inputType);
1459
Eric Laurentcaf7f482014-11-25 17:50:47 -08001460 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001461 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001462 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001463}
1464
Eric Laurent4dc68062014-07-28 17:26:49 -07001465status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1466 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001467{
1468 ALOGV("startInput() input %d", input);
1469 ssize_t index = mInputs.indexOfKey(input);
1470 if (index < 0) {
1471 ALOGW("startInput() unknown input %d", input);
1472 return BAD_VALUE;
1473 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001474 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001475
Eric Laurentc722f302014-12-10 11:21:49 -08001476 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001477 if (index < 0) {
1478 ALOGW("startInput() unknown session %d on input %d", session, input);
1479 return BAD_VALUE;
1480 }
1481
Glenn Kasten74a8e252014-07-24 14:09:55 -07001482 // virtual input devices are compatible with other input devices
1483 if (!isVirtualInputDevice(inputDesc->mDevice)) {
1484
1485 // for a non-virtual input device, check if there is another (non-virtual) active input
Eric Laurente552edb2014-03-10 17:42:56 -07001486 audio_io_handle_t activeInput = getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001487 if (activeInput != 0 && activeInput != input) {
1488
1489 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1490 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001491 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001492 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001493 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001494 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1495 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001496 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001497 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001498 return INVALID_OPERATION;
1499 }
1500 }
1501 }
1502
Glenn Kasten74a8e252014-07-24 14:09:55 -07001503 if (inputDesc->mRefCount == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001504 if (activeInputsCount() == 0) {
1505 SoundTrigger::setCaptureState(true);
1506 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001507 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001508
Eric Laurentc722f302014-12-10 11:21:49 -08001509 // automatically enable the remote submix output when input is started if not
1510 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001511 // For remote submix (a virtual device), we open only one input per capture request.
1512 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001513 String8 address = String8("");
1514 if (inputDesc->mPolicyMix == NULL) {
1515 address = String8("0");
1516 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1517 address = inputDesc->mPolicyMix->mRegistrationId;
1518 }
1519 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001520 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001521 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001522 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001523 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001524 }
Eric Laurente552edb2014-03-10 17:42:56 -07001525 }
1526
Eric Laurente552edb2014-03-10 17:42:56 -07001527 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1528
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001529 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001530 return NO_ERROR;
1531}
1532
Eric Laurent4dc68062014-07-28 17:26:49 -07001533status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1534 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001535{
1536 ALOGV("stopInput() input %d", input);
1537 ssize_t index = mInputs.indexOfKey(input);
1538 if (index < 0) {
1539 ALOGW("stopInput() unknown input %d", input);
1540 return BAD_VALUE;
1541 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001542 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001543
Eric Laurentc722f302014-12-10 11:21:49 -08001544 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001545 if (index < 0) {
1546 ALOGW("stopInput() unknown session %d on input %d", session, input);
1547 return BAD_VALUE;
1548 }
1549
Eric Laurente552edb2014-03-10 17:42:56 -07001550 if (inputDesc->mRefCount == 0) {
1551 ALOGW("stopInput() input %d already stopped", input);
1552 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001553 }
1554
1555 inputDesc->mRefCount--;
1556 if (inputDesc->mRefCount == 0) {
1557
Eric Laurentc722f302014-12-10 11:21:49 -08001558 // automatically disable the remote submix output when input is stopped if not
1559 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001560 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001561 String8 address = String8("");
1562 if (inputDesc->mPolicyMix == NULL) {
1563 address = String8("0");
1564 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1565 address = inputDesc->mPolicyMix->mRegistrationId;
1566 }
1567 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001568 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001569 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001570 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001571 }
Eric Laurente552edb2014-03-10 17:42:56 -07001572 }
1573
Eric Laurent1c333e22014-05-20 10:48:17 -07001574 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001575
1576 if (activeInputsCount() == 0) {
1577 SoundTrigger::setCaptureState(false);
1578 }
Eric Laurente552edb2014-03-10 17:42:56 -07001579 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001580 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001581}
1582
Eric Laurent4dc68062014-07-28 17:26:49 -07001583void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1584 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001585{
1586 ALOGV("releaseInput() %d", input);
1587 ssize_t index = mInputs.indexOfKey(input);
1588 if (index < 0) {
1589 ALOGW("releaseInput() releasing unknown input %d", input);
1590 return;
1591 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001592 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1593 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001594
Eric Laurentc722f302014-12-10 11:21:49 -08001595 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001596 if (index < 0) {
1597 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1598 return;
1599 }
Eric Laurentc722f302014-12-10 11:21:49 -08001600 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001601 if (inputDesc->mOpenRefCount == 0) {
1602 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1603 return;
1604 }
1605 inputDesc->mOpenRefCount--;
1606 if (inputDesc->mOpenRefCount > 0) {
1607 ALOGV("releaseInput() exit > 0");
1608 return;
1609 }
1610
Eric Laurent05b90f82014-08-27 15:32:29 -07001611 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001612 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001613 ALOGV("releaseInput() exit");
1614}
1615
Eric Laurentd4692962014-05-05 18:13:44 -07001616void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001617 bool patchRemoved = false;
1618
Eric Laurentd4692962014-05-05 18:13:44 -07001619 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001620 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1621 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1622 if (patch_index >= 0) {
1623 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1624 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1625 mAudioPatches.removeItemsAt(patch_index);
1626 patchRemoved = true;
1627 }
Eric Laurentd4692962014-05-05 18:13:44 -07001628 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1629 }
1630 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001631 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001632
1633 if (patchRemoved) {
1634 mpClientInterface->onAudioPatchListUpdate();
1635 }
Eric Laurentd4692962014-05-05 18:13:44 -07001636}
1637
Eric Laurente0720872014-03-11 09:30:41 -07001638void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001639 int indexMin,
1640 int indexMax)
1641{
1642 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1643 if (indexMin < 0 || indexMin >= indexMax) {
1644 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1645 return;
1646 }
1647 mStreams[stream].mIndexMin = indexMin;
1648 mStreams[stream].mIndexMax = indexMax;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001649 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1650 if (stream == AUDIO_STREAM_MUSIC) {
1651 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin;
1652 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax;
1653 }
Eric Laurente552edb2014-03-10 17:42:56 -07001654}
1655
Eric Laurente0720872014-03-11 09:30:41 -07001656status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001657 int index,
1658 audio_devices_t device)
1659{
1660
1661 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1662 return BAD_VALUE;
1663 }
1664 if (!audio_is_output_device(device)) {
1665 return BAD_VALUE;
1666 }
1667
1668 // Force max volume if stream cannot be muted
1669 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1670
1671 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1672 stream, device, index);
1673
1674 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1675 // clear all device specific values
1676 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1677 mStreams[stream].mIndexCur.clear();
1678 }
1679 mStreams[stream].mIndexCur.add(device, index);
1680
Eric Laurent31551f82014-10-10 18:21:56 -07001681 // update volume on all outputs whose current device is also selected by the same
1682 // strategy as the device specified by the caller
1683 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001684
1685
1686 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1687 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1688 if (stream == AUDIO_STREAM_MUSIC) {
1689 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index);
1690 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1691 }
1692 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1693 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001694 return NO_ERROR;
1695 }
Eric Laurente552edb2014-03-10 17:42:56 -07001696 status_t status = NO_ERROR;
1697 for (size_t i = 0; i < mOutputs.size(); i++) {
1698 audio_devices_t curDevice =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001699 ApmGains::getDeviceForVolume(mOutputs.valueAt(i)->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001700 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001701 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1702 if (volStatus != NO_ERROR) {
1703 status = volStatus;
1704 }
1705 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001706 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1707 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
1708 index, mOutputs.keyAt(i), curDevice);
1709 }
Eric Laurente552edb2014-03-10 17:42:56 -07001710 }
1711 return status;
1712}
1713
Eric Laurente0720872014-03-11 09:30:41 -07001714status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001715 int *index,
1716 audio_devices_t device)
1717{
1718 if (index == NULL) {
1719 return BAD_VALUE;
1720 }
1721 if (!audio_is_output_device(device)) {
1722 return BAD_VALUE;
1723 }
1724 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1725 // the strategy the stream belongs to.
1726 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1727 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1728 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001729 device = ApmGains::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001730
1731 *index = mStreams[stream].getVolumeIndex(device);
1732 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1733 return NO_ERROR;
1734}
1735
Eric Laurente0720872014-03-11 09:30:41 -07001736audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001737 const SortedVector<audio_io_handle_t>& outputs)
1738{
1739 // select one output among several suitable for global effects.
1740 // The priority is as follows:
1741 // 1: An offloaded output. If the effect ends up not being offloadable,
1742 // AudioFlinger will invalidate the track and the offloaded output
1743 // will be closed causing the effect to be moved to a PCM output.
1744 // 2: A deep buffer output
1745 // 3: the first output in the list
1746
1747 if (outputs.size() == 0) {
1748 return 0;
1749 }
1750
1751 audio_io_handle_t outputOffloaded = 0;
1752 audio_io_handle_t outputDeepBuffer = 0;
1753
1754 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001755 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001756 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001757 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1758 outputOffloaded = outputs[i];
1759 }
1760 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1761 outputDeepBuffer = outputs[i];
1762 }
1763 }
1764
1765 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1766 outputOffloaded, outputDeepBuffer);
1767 if (outputOffloaded != 0) {
1768 return outputOffloaded;
1769 }
1770 if (outputDeepBuffer != 0) {
1771 return outputDeepBuffer;
1772 }
1773
1774 return outputs[0];
1775}
1776
Eric Laurente0720872014-03-11 09:30:41 -07001777audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001778{
1779 // apply simple rule where global effects are attached to the same output as MUSIC streams
1780
Eric Laurent3b73df72014-03-11 09:06:29 -07001781 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001782 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1783 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1784
1785 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1786 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1787 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1788
1789 return output;
1790}
1791
Eric Laurente0720872014-03-11 09:30:41 -07001792status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001793 audio_io_handle_t io,
1794 uint32_t strategy,
1795 int session,
1796 int id)
1797{
1798 ssize_t index = mOutputs.indexOfKey(io);
1799 if (index < 0) {
1800 index = mInputs.indexOfKey(io);
1801 if (index < 0) {
1802 ALOGW("registerEffect() unknown io %d", io);
1803 return INVALID_OPERATION;
1804 }
1805 }
1806
1807 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1808 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1809 desc->name, desc->memoryUsage);
1810 return INVALID_OPERATION;
1811 }
1812 mTotalEffectsMemory += desc->memoryUsage;
1813 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1814 desc->name, io, strategy, session, id);
1815 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1816
Eric Laurent1f2f2232014-06-02 12:01:23 -07001817 sp<EffectDescriptor> effectDesc = new EffectDescriptor();
1818 memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t));
1819 effectDesc->mIo = io;
1820 effectDesc->mStrategy = (routing_strategy)strategy;
1821 effectDesc->mSession = session;
1822 effectDesc->mEnabled = false;
Eric Laurente552edb2014-03-10 17:42:56 -07001823
Eric Laurent1f2f2232014-06-02 12:01:23 -07001824 mEffects.add(id, effectDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001825
1826 return NO_ERROR;
1827}
1828
Eric Laurente0720872014-03-11 09:30:41 -07001829status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001830{
1831 ssize_t index = mEffects.indexOfKey(id);
1832 if (index < 0) {
1833 ALOGW("unregisterEffect() unknown effect ID %d", id);
1834 return INVALID_OPERATION;
1835 }
1836
Eric Laurent1f2f2232014-06-02 12:01:23 -07001837 sp<EffectDescriptor> effectDesc = mEffects.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001838
Eric Laurent1f2f2232014-06-02 12:01:23 -07001839 setEffectEnabled(effectDesc, false);
Eric Laurente552edb2014-03-10 17:42:56 -07001840
Eric Laurent1f2f2232014-06-02 12:01:23 -07001841 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
Eric Laurente552edb2014-03-10 17:42:56 -07001842 ALOGW("unregisterEffect() memory %d too big for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001843 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1844 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
Eric Laurente552edb2014-03-10 17:42:56 -07001845 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001846 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
Eric Laurente552edb2014-03-10 17:42:56 -07001847 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001848 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
Eric Laurente552edb2014-03-10 17:42:56 -07001849
1850 mEffects.removeItem(id);
Eric Laurente552edb2014-03-10 17:42:56 -07001851
1852 return NO_ERROR;
1853}
1854
Eric Laurente0720872014-03-11 09:30:41 -07001855status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001856{
1857 ssize_t index = mEffects.indexOfKey(id);
1858 if (index < 0) {
1859 ALOGW("unregisterEffect() unknown effect ID %d", id);
1860 return INVALID_OPERATION;
1861 }
1862
1863 return setEffectEnabled(mEffects.valueAt(index), enabled);
1864}
1865
Eric Laurent1f2f2232014-06-02 12:01:23 -07001866status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001867{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001868 if (enabled == effectDesc->mEnabled) {
Eric Laurente552edb2014-03-10 17:42:56 -07001869 ALOGV("setEffectEnabled(%s) effect already %s",
1870 enabled?"true":"false", enabled?"enabled":"disabled");
1871 return INVALID_OPERATION;
1872 }
1873
1874 if (enabled) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001875 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001876 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001877 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
Eric Laurente552edb2014-03-10 17:42:56 -07001878 return INVALID_OPERATION;
1879 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001880 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001881 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1882 } else {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001883 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
Eric Laurente552edb2014-03-10 17:42:56 -07001884 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001885 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1886 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001887 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001888 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001889 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1890 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001891 effectDesc->mEnabled = enabled;
Eric Laurente552edb2014-03-10 17:42:56 -07001892 return NO_ERROR;
1893}
1894
Eric Laurente0720872014-03-11 09:30:41 -07001895bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001896{
1897 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001898 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
1899 if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) &&
1900 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001901 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001902 effectDesc->mDesc.name, effectDesc->mSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001903 return true;
1904 }
1905 }
1906 return false;
1907}
1908
Eric Laurente0720872014-03-11 09:30:41 -07001909bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001910{
1911 nsecs_t sysTime = systemTime();
1912 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001913 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001914 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001915 return true;
1916 }
1917 }
1918 return false;
1919}
1920
Eric Laurente0720872014-03-11 09:30:41 -07001921bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001922 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001923{
1924 nsecs_t sysTime = systemTime();
1925 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001926 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001927 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001928 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001929 // do not consider re routing (when the output is going to a dynamic policy)
1930 // as "remote playback"
1931 if (outputDesc->mPolicyMix == NULL) {
Jean-Michel Trivi1767df72014-12-09 18:11:49 -08001932 return true;
1933 }
Eric Laurente552edb2014-03-10 17:42:56 -07001934 }
1935 }
1936 return false;
1937}
1938
Eric Laurente0720872014-03-11 09:30:41 -07001939bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001940{
1941 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001942 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001943 if (inputDescriptor->mRefCount == 0) {
1944 continue;
1945 }
1946 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001947 return true;
1948 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001949 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1950 // corresponds to an active capture triggered by a hardware hotword recognition
1951 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1952 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1953 // FIXME: we should not assume that the first session is the active one and keep
1954 // activity count per session. Same in startInput().
1955 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1956 if (index >= 0) {
1957 return true;
1958 }
1959 }
Eric Laurente552edb2014-03-10 17:42:56 -07001960 }
1961 return false;
1962}
1963
Eric Laurent275e8e92014-11-30 15:14:47 -08001964// Register a list of custom mixes with their attributes and format.
1965// When a mix is registered, corresponding input and output profiles are
1966// added to the remote submix hw module. The profile contains only the
1967// parameters (sampling rate, format...) specified by the mix.
1968// The corresponding input remote submix device is also connected.
1969//
1970// When a remote submix device is connected, the address is checked to select the
1971// appropriate profile and the corresponding input or output stream is opened.
1972//
1973// When capture starts, getInputForAttr() will:
1974// - 1 look for a mix matching the address passed in attribtutes tags if any
1975// - 2 if none found, getDeviceForInputSource() will:
1976// - 2.1 look for a mix matching the attributes source
1977// - 2.2 if none found, default to device selection by policy rules
1978// At this time, the corresponding output remote submix device is also connected
1979// and active playback use cases can be transferred to this mix if needed when reconnecting
1980// after AudioTracks are invalidated
1981//
1982// When playback starts, getOutputForAttr() will:
1983// - 1 look for a mix matching the address passed in attribtutes tags if any
1984// - 2 if none found, look for a mix matching the attributes usage
1985// - 3 if none found, default to device and output selection by policy rules.
1986
1987status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1988{
1989 sp<HwModule> module;
1990 for (size_t i = 0; i < mHwModules.size(); i++) {
1991 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1992 mHwModules[i]->mHandle != 0) {
1993 module = mHwModules[i];
1994 break;
1995 }
1996 }
1997
1998 if (module == 0) {
1999 return INVALID_OPERATION;
2000 }
2001
2002 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
2003
2004 for (size_t i = 0; i < mixes.size(); i++) {
2005 String8 address = mixes[i].mRegistrationId;
2006 ssize_t index = mPolicyMixes.indexOfKey(address);
2007 if (index >= 0) {
2008 ALOGE("registerPolicyMixes(): mix for address %s already registered", address.string());
2009 continue;
2010 }
2011 audio_config_t outputConfig = mixes[i].mFormat;
2012 audio_config_t inputConfig = mixes[i].mFormat;
2013 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2014 // stereo and let audio flinger do the channel conversion if needed.
2015 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2016 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2017 module->addOutputProfile(address, &outputConfig,
2018 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2019 module->addInputProfile(address, &inputConfig,
2020 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
2021 sp<AudioPolicyMix> policyMix = new AudioPolicyMix();
2022 policyMix->mMix = mixes[i];
2023 mPolicyMixes.add(address, policyMix);
Eric Laurentc722f302014-12-10 11:21:49 -08002024 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002025 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002026 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002027 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002028 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002029 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002030 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002031 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002032 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002033 }
2034 return NO_ERROR;
2035}
2036
2037status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2038{
2039 sp<HwModule> module;
2040 for (size_t i = 0; i < mHwModules.size(); i++) {
2041 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2042 mHwModules[i]->mHandle != 0) {
2043 module = mHwModules[i];
2044 break;
2045 }
2046 }
2047
2048 if (module == 0) {
2049 return INVALID_OPERATION;
2050 }
2051
2052 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
2053
2054 for (size_t i = 0; i < mixes.size(); i++) {
2055 String8 address = mixes[i].mRegistrationId;
2056 ssize_t index = mPolicyMixes.indexOfKey(address);
2057 if (index < 0) {
2058 ALOGE("unregisterPolicyMixes(): mix for address %s not registered", address.string());
2059 continue;
2060 }
2061
2062 mPolicyMixes.removeItemsAt(index);
2063
Eric Laurentc722f302014-12-10 11:21:49 -08002064 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
2065 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2066 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002067 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08002068 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002069 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08002070 }
Eric Laurent275e8e92014-11-30 15:14:47 -08002071
2072 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2073 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2074 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002075 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002076 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002077 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002078 }
2079 module->removeOutputProfile(address);
2080 module->removeInputProfile(address);
2081 }
2082 return NO_ERROR;
2083}
2084
Eric Laurente552edb2014-03-10 17:42:56 -07002085
Eric Laurente0720872014-03-11 09:30:41 -07002086status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002087{
2088 const size_t SIZE = 256;
2089 char buffer[SIZE];
2090 String8 result;
2091
2092 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2093 result.append(buffer);
2094
2095 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
2096 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002097 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
2098 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002099 snprintf(buffer, SIZE, " Force use for communications %d\n",
2100 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07002101 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002102 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
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 record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
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 dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
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 system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07002109 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002110 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
2111 mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]);
2112 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002113
Eric Laurent3a4311c2014-03-17 12:00:47 -07002114 snprintf(buffer, SIZE, " Available output devices:\n");
2115 result.append(buffer);
2116 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002117 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002118 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002119 }
2120 snprintf(buffer, SIZE, "\n Available input devices:\n");
2121 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07002122 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002123 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002124 }
Eric Laurente552edb2014-03-10 17:42:56 -07002125
2126 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
2127 write(fd, buffer, strlen(buffer));
2128 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002129 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002130 write(fd, buffer, strlen(buffer));
2131 mHwModules[i]->dump(fd);
2132 }
2133
2134 snprintf(buffer, SIZE, "\nOutputs dump:\n");
2135 write(fd, buffer, strlen(buffer));
2136 for (size_t i = 0; i < mOutputs.size(); i++) {
2137 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
2138 write(fd, buffer, strlen(buffer));
2139 mOutputs.valueAt(i)->dump(fd);
2140 }
2141
2142 snprintf(buffer, SIZE, "\nInputs dump:\n");
2143 write(fd, buffer, strlen(buffer));
2144 for (size_t i = 0; i < mInputs.size(); i++) {
2145 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
2146 write(fd, buffer, strlen(buffer));
2147 mInputs.valueAt(i)->dump(fd);
2148 }
2149
2150 snprintf(buffer, SIZE, "\nStreams dump:\n");
2151 write(fd, buffer, strlen(buffer));
2152 snprintf(buffer, SIZE,
2153 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
2154 write(fd, buffer, strlen(buffer));
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002155 for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002156 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07002157 write(fd, buffer, strlen(buffer));
2158 mStreams[i].dump(fd);
2159 }
2160
2161 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
2162 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
2163 write(fd, buffer, strlen(buffer));
2164
2165 snprintf(buffer, SIZE, "Registered effects:\n");
2166 write(fd, buffer, strlen(buffer));
2167 for (size_t i = 0; i < mEffects.size(); i++) {
2168 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
2169 write(fd, buffer, strlen(buffer));
2170 mEffects.valueAt(i)->dump(fd);
2171 }
2172
Eric Laurent4d416952014-08-10 14:07:09 -07002173 snprintf(buffer, SIZE, "\nAudio Patches:\n");
2174 write(fd, buffer, strlen(buffer));
2175 for (size_t i = 0; i < mAudioPatches.size(); i++) {
2176 mAudioPatches[i]->dump(fd, 2, i);
2177 }
Eric Laurente552edb2014-03-10 17:42:56 -07002178
2179 return NO_ERROR;
2180}
2181
2182// This function checks for the parameters which can be offloaded.
2183// This can be enhanced depending on the capability of the DSP and policy
2184// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002185bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002186{
2187 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002188 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002189 offloadInfo.sample_rate, offloadInfo.channel_mask,
2190 offloadInfo.format,
2191 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2192 offloadInfo.has_video);
2193
2194 // Check if offload has been disabled
2195 char propValue[PROPERTY_VALUE_MAX];
2196 if (property_get("audio.offload.disable", propValue, "0")) {
2197 if (atoi(propValue) != 0) {
2198 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2199 return false;
2200 }
2201 }
2202
2203 // Check if stream type is music, then only allow offload as of now.
2204 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2205 {
2206 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2207 return false;
2208 }
2209
2210 //TODO: enable audio offloading with video when ready
2211 if (offloadInfo.has_video)
2212 {
2213 ALOGV("isOffloadSupported: has_video == true, returning false");
2214 return false;
2215 }
2216
2217 //If duration is less than minimum value defined in property, return false
2218 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2219 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2220 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2221 return false;
2222 }
2223 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2224 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2225 return false;
2226 }
2227
2228 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2229 // creating an offloaded track and tearing it down immediately after start when audioflinger
2230 // detects there is an active non offloadable effect.
2231 // FIXME: We should check the audio session here but we do not have it in this context.
2232 // This may prevent offloading in rare situations where effects are left active by apps
2233 // in the background.
2234 if (isNonOffloadableEffectEnabled()) {
2235 return false;
2236 }
2237
2238 // See if there is a profile to support this.
2239 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002240 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002241 offloadInfo.sample_rate,
2242 offloadInfo.format,
2243 offloadInfo.channel_mask,
2244 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002245 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2246 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002247}
2248
Eric Laurent6a94d692014-05-20 11:18:06 -07002249status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2250 audio_port_type_t type,
2251 unsigned int *num_ports,
2252 struct audio_port *ports,
2253 unsigned int *generation)
2254{
2255 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2256 generation == NULL) {
2257 return BAD_VALUE;
2258 }
2259 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2260 if (ports == NULL) {
2261 *num_ports = 0;
2262 }
2263
2264 size_t portsWritten = 0;
2265 size_t portsMax = *num_ports;
2266 *num_ports = 0;
2267 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2268 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2269 for (size_t i = 0;
2270 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2271 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2272 }
2273 *num_ports += mAvailableOutputDevices.size();
2274 }
2275 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2276 for (size_t i = 0;
2277 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2278 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2279 }
2280 *num_ports += mAvailableInputDevices.size();
2281 }
2282 }
2283 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2284 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2285 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2286 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2287 }
2288 *num_ports += mInputs.size();
2289 }
2290 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002291 size_t numOutputs = 0;
2292 for (size_t i = 0; i < mOutputs.size(); i++) {
2293 if (!mOutputs[i]->isDuplicated()) {
2294 numOutputs++;
2295 if (portsWritten < portsMax) {
2296 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2297 }
2298 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002299 }
Eric Laurent84c70242014-06-23 08:46:27 -07002300 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002301 }
2302 }
2303 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002304 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002305 return NO_ERROR;
2306}
2307
2308status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2309{
2310 return NO_ERROR;
2311}
2312
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002313sp<AudioOutputDescriptor> AudioPolicyManager::getOutputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002314 audio_port_handle_t id) const
2315{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002316 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002317 for (size_t i = 0; i < mOutputs.size(); i++) {
2318 outputDesc = mOutputs.valueAt(i);
2319 if (outputDesc->mId == id) {
2320 break;
2321 }
2322 }
2323 return outputDesc;
2324}
2325
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002326sp<AudioInputDescriptor> AudioPolicyManager::getInputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002327 audio_port_handle_t id) const
2328{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002329 sp<AudioInputDescriptor> inputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002330 for (size_t i = 0; i < mInputs.size(); i++) {
2331 inputDesc = mInputs.valueAt(i);
2332 if (inputDesc->mId == id) {
2333 break;
2334 }
2335 }
2336 return inputDesc;
2337}
2338
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002339sp <HwModule> AudioPolicyManager::getModuleForDevice(
Eric Laurent1f2f2232014-06-02 12:01:23 -07002340 audio_devices_t device) const
Eric Laurent6a94d692014-05-20 11:18:06 -07002341{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002342 sp <HwModule> module;
2343
Eric Laurent6a94d692014-05-20 11:18:06 -07002344 for (size_t i = 0; i < mHwModules.size(); i++) {
2345 if (mHwModules[i]->mHandle == 0) {
2346 continue;
2347 }
2348 if (audio_is_output_device(device)) {
2349 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2350 {
2351 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
2352 return mHwModules[i];
2353 }
2354 }
2355 } else {
2356 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2357 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
2358 device & ~AUDIO_DEVICE_BIT_IN) {
2359 return mHwModules[i];
2360 }
2361 }
2362 }
2363 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002364 return module;
Eric Laurent6a94d692014-05-20 11:18:06 -07002365}
2366
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08002367sp <HwModule> AudioPolicyManager::getModuleFromName(const char *name) const
Eric Laurent1afeecb2014-05-14 08:52:28 -07002368{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002369 sp <HwModule> module;
2370
Eric Laurent1afeecb2014-05-14 08:52:28 -07002371 for (size_t i = 0; i < mHwModules.size(); i++)
2372 {
2373 if (strcmp(mHwModules[i]->mName, name) == 0) {
2374 return mHwModules[i];
2375 }
2376 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002377 return module;
Eric Laurent1afeecb2014-05-14 08:52:28 -07002378}
2379
Eric Laurentc2730ba2014-07-20 15:47:07 -07002380audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices()
2381{
2382 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
2383 audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types();
2384 return devices & mAvailableOutputDevices.types();
2385}
2386
2387audio_devices_t AudioPolicyManager::availablePrimaryInputDevices()
2388{
2389 audio_module_handle_t primaryHandle =
2390 mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle;
2391 audio_devices_t devices = AUDIO_DEVICE_NONE;
2392 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2393 if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) {
2394 devices |= mAvailableInputDevices[i]->mDeviceType;
2395 }
2396 }
2397 return devices;
2398}
Eric Laurent1afeecb2014-05-14 08:52:28 -07002399
Eric Laurent6a94d692014-05-20 11:18:06 -07002400status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2401 audio_patch_handle_t *handle,
2402 uid_t uid)
2403{
2404 ALOGV("createAudioPatch()");
2405
2406 if (handle == NULL || patch == NULL) {
2407 return BAD_VALUE;
2408 }
2409 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2410
Eric Laurent874c42872014-08-08 15:13:39 -07002411 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2412 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2413 return BAD_VALUE;
2414 }
2415 // only one source per audio patch supported for now
2416 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002417 return INVALID_OPERATION;
2418 }
Eric Laurent874c42872014-08-08 15:13:39 -07002419
2420 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002421 return INVALID_OPERATION;
2422 }
Eric Laurent874c42872014-08-08 15:13:39 -07002423 for (size_t i = 0; i < patch->num_sinks; i++) {
2424 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2425 return INVALID_OPERATION;
2426 }
2427 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002428
2429 sp<AudioPatch> patchDesc;
2430 ssize_t index = mAudioPatches.indexOfKey(*handle);
2431
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2433 patch->sources[0].role,
2434 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002435#if LOG_NDEBUG == 0
2436 for (size_t i = 0; i < patch->num_sinks; i++) {
2437 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2438 patch->sinks[i].role,
2439 patch->sinks[i].type);
2440 }
2441#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002442
2443 if (index >= 0) {
2444 patchDesc = mAudioPatches.valueAt(index);
2445 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2446 mUidCached, patchDesc->mUid, uid);
2447 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2448 return INVALID_OPERATION;
2449 }
2450 } else {
2451 *handle = 0;
2452 }
2453
2454 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002455 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002456 if (outputDesc == NULL) {
2457 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2458 return BAD_VALUE;
2459 }
Eric Laurent84c70242014-06-23 08:46:27 -07002460 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2461 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002462 if (patchDesc != 0) {
2463 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2464 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2465 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2466 return BAD_VALUE;
2467 }
2468 }
Eric Laurent874c42872014-08-08 15:13:39 -07002469 DeviceVector devices;
2470 for (size_t i = 0; i < patch->num_sinks; i++) {
2471 // Only support mix to devices connection
2472 // TODO add support for mix to mix connection
2473 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2474 ALOGV("createAudioPatch() source mix but sink is not a device");
2475 return INVALID_OPERATION;
2476 }
2477 sp<DeviceDescriptor> devDesc =
2478 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2479 if (devDesc == 0) {
2480 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2481 return BAD_VALUE;
2482 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002483
Eric Laurent874c42872014-08-08 15:13:39 -07002484 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002485 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002486 patch->sources[0].sample_rate,
2487 NULL, // updatedSamplingRate
2488 patch->sources[0].format,
2489 patch->sources[0].channel_mask,
2490 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2491 ALOGV("createAudioPatch() profile not supported for device %08x",
2492 devDesc->mDeviceType);
2493 return INVALID_OPERATION;
2494 }
2495 devices.add(devDesc);
2496 }
2497 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002498 return INVALID_OPERATION;
2499 }
Eric Laurent874c42872014-08-08 15:13:39 -07002500
Eric Laurent6a94d692014-05-20 11:18:06 -07002501 // TODO: reconfigure output format and channels here
2502 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002503 devices.types(), outputDesc->mIoHandle);
2504 setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002505 index = mAudioPatches.indexOfKey(*handle);
2506 if (index >= 0) {
2507 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2508 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2509 }
2510 patchDesc = mAudioPatches.valueAt(index);
2511 patchDesc->mUid = uid;
2512 ALOGV("createAudioPatch() success");
2513 } else {
2514 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2515 return INVALID_OPERATION;
2516 }
2517 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2518 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2519 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002520 // only one sink supported when connecting an input device to a mix
2521 if (patch->num_sinks > 1) {
2522 return INVALID_OPERATION;
2523 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002524 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002525 if (inputDesc == NULL) {
2526 return BAD_VALUE;
2527 }
2528 if (patchDesc != 0) {
2529 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2530 return BAD_VALUE;
2531 }
2532 }
2533 sp<DeviceDescriptor> devDesc =
2534 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2535 if (devDesc == 0) {
2536 return BAD_VALUE;
2537 }
2538
Eric Laurent84c70242014-06-23 08:46:27 -07002539 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002540 devDesc->mAddress,
2541 patch->sinks[0].sample_rate,
2542 NULL, /*updatedSampleRate*/
2543 patch->sinks[0].format,
2544 patch->sinks[0].channel_mask,
2545 // FIXME for the parameter type,
2546 // and the NONE
2547 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002548 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002549 return INVALID_OPERATION;
2550 }
2551 // TODO: reconfigure output format and channels here
2552 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent84c70242014-06-23 08:46:27 -07002553 devDesc->mDeviceType, inputDesc->mIoHandle);
Eric Laurent874c42872014-08-08 15:13:39 -07002554 setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002555 index = mAudioPatches.indexOfKey(*handle);
2556 if (index >= 0) {
2557 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2558 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2559 }
2560 patchDesc = mAudioPatches.valueAt(index);
2561 patchDesc->mUid = uid;
2562 ALOGV("createAudioPatch() success");
2563 } else {
2564 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2565 return INVALID_OPERATION;
2566 }
2567 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2568 // device to device connection
2569 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002570 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002571 return BAD_VALUE;
2572 }
2573 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002574 sp<DeviceDescriptor> srcDeviceDesc =
2575 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002576 if (srcDeviceDesc == 0) {
2577 return BAD_VALUE;
2578 }
Eric Laurent874c42872014-08-08 15:13:39 -07002579
Eric Laurent6a94d692014-05-20 11:18:06 -07002580 //update source and sink with our own data as the data passed in the patch may
2581 // be incomplete.
2582 struct audio_patch newPatch = *patch;
2583 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002584
Eric Laurent874c42872014-08-08 15:13:39 -07002585 for (size_t i = 0; i < patch->num_sinks; i++) {
2586 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2587 ALOGV("createAudioPatch() source device but one sink is not a device");
2588 return INVALID_OPERATION;
2589 }
2590
2591 sp<DeviceDescriptor> sinkDeviceDesc =
2592 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2593 if (sinkDeviceDesc == 0) {
2594 return BAD_VALUE;
2595 }
2596 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2597
2598 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
2599 // only one sink supported when connected devices across HW modules
2600 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002601 return INVALID_OPERATION;
2602 }
Eric Laurent874c42872014-08-08 15:13:39 -07002603 SortedVector<audio_io_handle_t> outputs =
2604 getOutputsForDevice(sinkDeviceDesc->mDeviceType,
2605 mOutputs);
2606 // if the sink device is reachable via an opened output stream, request to go via
2607 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002608 audio_io_handle_t output = selectOutput(outputs,
2609 AUDIO_OUTPUT_FLAG_NONE,
2610 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002611 if (output != AUDIO_IO_HANDLE_NONE) {
2612 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2613 if (outputDesc->isDuplicated()) {
2614 return INVALID_OPERATION;
2615 }
2616 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
2617 newPatch.num_sources = 2;
2618 }
Eric Laurent83b88082014-06-20 18:31:16 -07002619 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002620 }
2621 // TODO: check from routing capabilities in config file and other conflicting patches
2622
2623 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2624 if (index >= 0) {
2625 afPatchHandle = patchDesc->mAfPatchHandle;
2626 }
2627
2628 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2629 &afPatchHandle,
2630 0);
2631 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2632 status, afPatchHandle);
2633 if (status == NO_ERROR) {
2634 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002635 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002636 addAudioPatch(patchDesc->mHandle, patchDesc);
2637 } else {
2638 patchDesc->mPatch = newPatch;
2639 }
2640 patchDesc->mAfPatchHandle = afPatchHandle;
2641 *handle = patchDesc->mHandle;
2642 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002643 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002644 } else {
2645 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2646 status);
2647 return INVALID_OPERATION;
2648 }
2649 } else {
2650 return BAD_VALUE;
2651 }
2652 } else {
2653 return BAD_VALUE;
2654 }
2655 return NO_ERROR;
2656}
2657
2658status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2659 uid_t uid)
2660{
2661 ALOGV("releaseAudioPatch() patch %d", handle);
2662
2663 ssize_t index = mAudioPatches.indexOfKey(handle);
2664
2665 if (index < 0) {
2666 return BAD_VALUE;
2667 }
2668 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2669 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2670 mUidCached, patchDesc->mUid, uid);
2671 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2672 return INVALID_OPERATION;
2673 }
2674
2675 struct audio_patch *patch = &patchDesc->mPatch;
2676 patchDesc->mUid = mUidCached;
2677 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002678 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002679 if (outputDesc == NULL) {
2680 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2681 return BAD_VALUE;
2682 }
2683
2684 setOutputDevice(outputDesc->mIoHandle,
2685 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2686 true,
2687 0,
2688 NULL);
2689 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2690 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002691 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002692 if (inputDesc == NULL) {
2693 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2694 return BAD_VALUE;
2695 }
2696 setInputDevice(inputDesc->mIoHandle,
2697 getNewInputDevice(inputDesc->mIoHandle),
2698 true,
2699 NULL);
2700 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2701 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2702 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2703 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2704 status, patchDesc->mAfPatchHandle);
2705 removeAudioPatch(patchDesc->mHandle);
2706 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002707 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002708 } else {
2709 return BAD_VALUE;
2710 }
2711 } else {
2712 return BAD_VALUE;
2713 }
2714 return NO_ERROR;
2715}
2716
2717status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2718 struct audio_patch *patches,
2719 unsigned int *generation)
2720{
2721 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
2722 generation == NULL) {
2723 return BAD_VALUE;
2724 }
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002725 ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
Eric Laurent6a94d692014-05-20 11:18:06 -07002726 *num_patches, patches, mAudioPatches.size());
2727 if (patches == NULL) {
2728 *num_patches = 0;
2729 }
2730
2731 size_t patchesWritten = 0;
2732 size_t patchesMax = *num_patches;
2733 for (size_t i = 0;
2734 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2735 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2736 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002737 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07002738 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2739 }
2740 *num_patches = mAudioPatches.size();
2741
2742 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002743 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002744 return NO_ERROR;
2745}
2746
Eric Laurente1715a42014-05-20 11:30:42 -07002747status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002748{
Eric Laurente1715a42014-05-20 11:30:42 -07002749 ALOGV("setAudioPortConfig()");
2750
2751 if (config == NULL) {
2752 return BAD_VALUE;
2753 }
2754 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2755 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002756 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2757 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002758 }
2759
Eric Laurenta121f902014-06-03 13:32:54 -07002760 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002761 if (config->type == AUDIO_PORT_TYPE_MIX) {
2762 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002763 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002764 if (outputDesc == NULL) {
2765 return BAD_VALUE;
2766 }
Eric Laurent84c70242014-06-23 08:46:27 -07002767 ALOG_ASSERT(!outputDesc->isDuplicated(),
2768 "setAudioPortConfig() called on duplicated output %d",
2769 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002770 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002771 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002772 sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002773 if (inputDesc == NULL) {
2774 return BAD_VALUE;
2775 }
Eric Laurenta121f902014-06-03 13:32:54 -07002776 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002777 } else {
2778 return BAD_VALUE;
2779 }
2780 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2781 sp<DeviceDescriptor> deviceDesc;
2782 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2783 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2784 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2785 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2786 } else {
2787 return BAD_VALUE;
2788 }
2789 if (deviceDesc == NULL) {
2790 return BAD_VALUE;
2791 }
Eric Laurenta121f902014-06-03 13:32:54 -07002792 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002793 } else {
2794 return BAD_VALUE;
2795 }
2796
Eric Laurenta121f902014-06-03 13:32:54 -07002797 struct audio_port_config backupConfig;
2798 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2799 if (status == NO_ERROR) {
2800 struct audio_port_config newConfig;
2801 audioPortConfig->toAudioPortConfig(&newConfig, config);
2802 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002803 }
Eric Laurenta121f902014-06-03 13:32:54 -07002804 if (status != NO_ERROR) {
2805 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002806 }
Eric Laurente1715a42014-05-20 11:30:42 -07002807
2808 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002809}
2810
2811void AudioPolicyManager::clearAudioPatches(uid_t uid)
2812{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002813 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002814 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2815 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002816 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002817 }
2818 }
2819}
2820
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002821status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2822 audio_io_handle_t *ioHandle,
2823 audio_devices_t *device)
2824{
2825 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2826 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002827 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002828
2829 mSoundTriggerSessions.add(*session, *ioHandle);
2830
2831 return NO_ERROR;
2832}
2833
2834status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session)
2835{
2836 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
2837 if (index < 0) {
2838 ALOGW("acquireSoundTriggerSession() session %d not registered", session);
2839 return BAD_VALUE;
2840 }
2841
2842 mSoundTriggerSessions.removeItem(session);
2843 return NO_ERROR;
2844}
2845
Eric Laurent6a94d692014-05-20 11:18:06 -07002846status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2847 const sp<AudioPatch>& patch)
2848{
2849 ssize_t index = mAudioPatches.indexOfKey(handle);
2850
2851 if (index >= 0) {
2852 ALOGW("addAudioPatch() patch %d already in", handle);
2853 return ALREADY_EXISTS;
2854 }
2855 mAudioPatches.add(handle, patch);
2856 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2857 "sink handle %d",
2858 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2859 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2860 return NO_ERROR;
2861}
2862
2863status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2864{
2865 ssize_t index = mAudioPatches.indexOfKey(handle);
2866
2867 if (index < 0) {
2868 ALOGW("removeAudioPatch() patch %d not in", handle);
2869 return ALREADY_EXISTS;
2870 }
2871 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2872 mAudioPatches.valueAt(index)->mAfPatchHandle);
2873 mAudioPatches.removeItemsAt(index);
2874 return NO_ERROR;
2875}
2876
Eric Laurente552edb2014-03-10 17:42:56 -07002877// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002878// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002879// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002880uint32_t AudioPolicyManager::nextAudioPortGeneration()
2881{
2882 return android_atomic_inc(&mAudioPortGeneration);
2883}
2884
Eric Laurente0720872014-03-11 09:30:41 -07002885AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002886 :
2887#ifdef AUDIO_POLICY_TEST
2888 Thread(false),
2889#endif //AUDIO_POLICY_TEST
2890 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002891 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002892 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2893 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002894 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002895 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002896 mAudioPortGeneration(1),
2897 mBeaconMuteRefCount(0),
2898 mBeaconPlayingRefCount(0),
2899 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002900{
Eric Laurent6a94d692014-05-20 11:18:06 -07002901 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002902 mpClientInterface = clientInterface;
2903
Eric Laurent3b73df72014-03-11 09:06:29 -07002904 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2905 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002906 }
2907
Paul McLeane743a472015-01-28 11:07:31 -08002908 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002909 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2910 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2911 ALOGE("could not load audio policy configuration file, setting defaults");
2912 defaultAudioPolicyConfig();
2913 }
2914 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002915 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002916
2917 // must be done after reading the policy
2918 initializeVolumeCurves();
2919
2920 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002921 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2922 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002923 for (size_t i = 0; i < mHwModules.size(); i++) {
2924 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2925 if (mHwModules[i]->mHandle == 0) {
2926 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2927 continue;
2928 }
2929 // open all output streams needed to access attached devices
2930 // except for direct output streams that are only opened when they are actually
2931 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002932 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002933 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2934 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002935 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002936
Eric Laurent3a4311c2014-03-17 12:00:47 -07002937 if (outProfile->mSupportedDevices.isEmpty()) {
2938 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2939 continue;
2940 }
2941
Eric Laurentd78f1532014-09-16 16:38:20 -07002942 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2943 continue;
2944 }
Eric Laurent83b88082014-06-20 18:31:16 -07002945 audio_devices_t profileType = outProfile->mSupportedDevices.types();
2946 if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) {
2947 profileType = mDefaultOutputDevice->mDeviceType;
2948 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002949 // chose first device present in mSupportedDevices also part of
2950 // outputDeviceTypes
2951 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
2952 profileType = outProfile->mSupportedDevices[k]->mDeviceType;
2953 if ((profileType & outputDeviceTypes) != 0) {
2954 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002955 }
Eric Laurente552edb2014-03-10 17:42:56 -07002956 }
2957 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002958 if ((profileType & outputDeviceTypes) == 0) {
2959 continue;
2960 }
2961 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
2962
2963 outputDesc->mDevice = profileType;
2964 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2965 config.sample_rate = outputDesc->mSamplingRate;
2966 config.channel_mask = outputDesc->mChannelMask;
2967 config.format = outputDesc->mFormat;
2968 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2969 status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle,
2970 &output,
2971 &config,
2972 &outputDesc->mDevice,
2973 String8(""),
2974 &outputDesc->mLatency,
2975 outputDesc->mFlags);
2976
2977 if (status != NO_ERROR) {
2978 ALOGW("Cannot open output stream for device %08x on hw module %s",
2979 outputDesc->mDevice,
2980 mHwModules[i]->mName);
2981 } else {
2982 outputDesc->mSamplingRate = config.sample_rate;
2983 outputDesc->mChannelMask = config.channel_mask;
2984 outputDesc->mFormat = config.format;
2985
2986 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
2987 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
2988 ssize_t index =
2989 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2990 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002991 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2992 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002993 }
2994 }
2995 if (mPrimaryOutput == 0 &&
2996 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2997 mPrimaryOutput = output;
2998 }
2999 addOutput(output, outputDesc);
3000 setOutputDevice(output,
3001 outputDesc->mDevice,
3002 true);
3003 }
Eric Laurente552edb2014-03-10 17:42:56 -07003004 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003005 // open input streams needed to access attached devices to validate
3006 // mAvailableInputDevices list
3007 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3008 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003009 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003010
Eric Laurent3a4311c2014-03-17 12:00:47 -07003011 if (inProfile->mSupportedDevices.isEmpty()) {
3012 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
3013 continue;
3014 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003015 // chose first device present in mSupportedDevices also part of
3016 // inputDeviceTypes
3017 audio_devices_t profileType = AUDIO_DEVICE_NONE;
3018 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3019 profileType = inProfile->mSupportedDevices[k]->mDeviceType;
3020 if (profileType & inputDeviceTypes) {
3021 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003022 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003023 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003024 if ((profileType & inputDeviceTypes) == 0) {
3025 continue;
3026 }
3027 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
3028
3029 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
3030 inputDesc->mDevice = profileType;
3031
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003032 // find the address
3033 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3034 // the inputs vector must be of size 1, but we don't want to crash here
3035 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3036 : String8("");
3037 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3038 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3039
Eric Laurentd78f1532014-09-16 16:38:20 -07003040 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3041 config.sample_rate = inputDesc->mSamplingRate;
3042 config.channel_mask = inputDesc->mChannelMask;
3043 config.format = inputDesc->mFormat;
3044 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3045 status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle,
3046 &input,
3047 &config,
3048 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003049 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003050 AUDIO_SOURCE_MIC,
3051 AUDIO_INPUT_FLAG_NONE);
3052
3053 if (status == NO_ERROR) {
3054 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3055 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
3056 ssize_t index =
3057 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
3058 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08003059 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
3060 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07003061 }
3062 }
3063 mpClientInterface->closeInput(input);
3064 } else {
3065 ALOGW("Cannot open input stream for device %08x on hw module %s",
3066 inputDesc->mDevice,
3067 mHwModules[i]->mName);
3068 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003069 }
3070 }
3071 // make sure all attached devices have been allocated a unique ID
3072 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003073 if (!mAvailableOutputDevices[i]->isAttached()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003074 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003075 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3076 continue;
3077 }
3078 i++;
3079 }
3080 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08003081 if (!mAvailableInputDevices[i]->isAttached()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003082 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003083 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3084 continue;
3085 }
3086 i++;
3087 }
3088 // make sure default device is reachable
3089 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003090 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003091 }
Eric Laurente552edb2014-03-10 17:42:56 -07003092
3093 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3094
3095 updateDevicesAndOutputs();
3096
3097#ifdef AUDIO_POLICY_TEST
3098 if (mPrimaryOutput != 0) {
3099 AudioParameter outputCmd = AudioParameter();
3100 outputCmd.addInt(String8("set_id"), 0);
3101 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3102
3103 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3104 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003105 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3106 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003107 mTestLatencyMs = 0;
3108 mCurOutput = 0;
3109 mDirectOutput = false;
3110 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3111 mTestOutputs[i] = 0;
3112 }
3113
3114 const size_t SIZE = 256;
3115 char buffer[SIZE];
3116 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3117 run(buffer, ANDROID_PRIORITY_AUDIO);
3118 }
3119#endif //AUDIO_POLICY_TEST
3120}
3121
Eric Laurente0720872014-03-11 09:30:41 -07003122AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003123{
3124#ifdef AUDIO_POLICY_TEST
3125 exit();
3126#endif //AUDIO_POLICY_TEST
3127 for (size_t i = 0; i < mOutputs.size(); i++) {
3128 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003129 }
3130 for (size_t i = 0; i < mInputs.size(); i++) {
3131 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003132 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003133 mAvailableOutputDevices.clear();
3134 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003135 mOutputs.clear();
3136 mInputs.clear();
3137 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003138}
3139
Eric Laurente0720872014-03-11 09:30:41 -07003140status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003141{
3142 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
3143}
3144
3145#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003146bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003147{
3148 ALOGV("entering threadLoop()");
3149 while (!exitPending())
3150 {
3151 String8 command;
3152 int valueInt;
3153 String8 value;
3154
3155 Mutex::Autolock _l(mLock);
3156 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3157
3158 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3159 AudioParameter param = AudioParameter(command);
3160
3161 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3162 valueInt != 0) {
3163 ALOGV("Test command %s received", command.string());
3164 String8 target;
3165 if (param.get(String8("target"), target) != NO_ERROR) {
3166 target = "Manager";
3167 }
3168 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3169 param.remove(String8("test_cmd_policy_output"));
3170 mCurOutput = valueInt;
3171 }
3172 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3173 param.remove(String8("test_cmd_policy_direct"));
3174 if (value == "false") {
3175 mDirectOutput = false;
3176 } else if (value == "true") {
3177 mDirectOutput = true;
3178 }
3179 }
3180 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3181 param.remove(String8("test_cmd_policy_input"));
3182 mTestInput = valueInt;
3183 }
3184
3185 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3186 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003187 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003188 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003189 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003190 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003191 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003192 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003193 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003194 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003195 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003196 if (target == "Manager") {
3197 mTestFormat = format;
3198 } else if (mTestOutputs[mCurOutput] != 0) {
3199 AudioParameter outputParam = AudioParameter();
3200 outputParam.addInt(String8("format"), format);
3201 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3202 }
3203 }
3204 }
3205 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3206 param.remove(String8("test_cmd_policy_channels"));
3207 int channels = 0;
3208
3209 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003210 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003211 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003212 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003213 }
3214 if (channels != 0) {
3215 if (target == "Manager") {
3216 mTestChannels = channels;
3217 } else if (mTestOutputs[mCurOutput] != 0) {
3218 AudioParameter outputParam = AudioParameter();
3219 outputParam.addInt(String8("channels"), channels);
3220 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3221 }
3222 }
3223 }
3224 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3225 param.remove(String8("test_cmd_policy_sampleRate"));
3226 if (valueInt >= 0 && valueInt <= 96000) {
3227 int samplingRate = valueInt;
3228 if (target == "Manager") {
3229 mTestSamplingRate = samplingRate;
3230 } else if (mTestOutputs[mCurOutput] != 0) {
3231 AudioParameter outputParam = AudioParameter();
3232 outputParam.addInt(String8("sampling_rate"), samplingRate);
3233 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3234 }
3235 }
3236 }
3237
3238 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3239 param.remove(String8("test_cmd_policy_reopen"));
3240
Eric Laurent1f2f2232014-06-02 12:01:23 -07003241 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003242 mpClientInterface->closeOutput(mPrimaryOutput);
3243
3244 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
3245
Eric Laurente552edb2014-03-10 17:42:56 -07003246 mOutputs.removeItem(mPrimaryOutput);
3247
Eric Laurent1f2f2232014-06-02 12:01:23 -07003248 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07003249 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003250 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3251 config.sample_rate = outputDesc->mSamplingRate;
3252 config.channel_mask = outputDesc->mChannelMask;
3253 config.format = outputDesc->mFormat;
3254 status_t status = mpClientInterface->openOutput(moduleHandle,
3255 &mPrimaryOutput,
3256 &config,
3257 &outputDesc->mDevice,
3258 String8(""),
3259 &outputDesc->mLatency,
3260 outputDesc->mFlags);
3261 if (status != NO_ERROR) {
3262 ALOGE("Failed to reopen hardware output stream, "
3263 "samplingRate: %d, format %d, channels %d",
3264 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003265 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003266 outputDesc->mSamplingRate = config.sample_rate;
3267 outputDesc->mChannelMask = config.channel_mask;
3268 outputDesc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003269 AudioParameter outputCmd = AudioParameter();
3270 outputCmd.addInt(String8("set_id"), 0);
3271 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3272 addOutput(mPrimaryOutput, outputDesc);
3273 }
3274 }
3275
3276
3277 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3278 }
3279 }
3280 return false;
3281}
3282
Eric Laurente0720872014-03-11 09:30:41 -07003283void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003284{
3285 {
3286 AutoMutex _l(mLock);
3287 requestExit();
3288 mWaitWorkCV.signal();
3289 }
3290 requestExitAndWait();
3291}
3292
Eric Laurente0720872014-03-11 09:30:41 -07003293int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003294{
3295 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3296 if (output == mTestOutputs[i]) return i;
3297 }
3298 return 0;
3299}
3300#endif //AUDIO_POLICY_TEST
3301
3302// ---
3303
Eric Laurent1f2f2232014-06-02 12:01:23 -07003304void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003305{
François Gaffie98cc1912015-03-18 17:52:40 +01003306 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003307 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003308 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003309}
3310
Eric Laurent1f2f2232014-06-02 12:01:23 -07003311void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003312{
François Gaffie98cc1912015-03-18 17:52:40 +01003313 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003314 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003315 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003316}
Eric Laurente552edb2014-03-10 17:42:56 -07003317
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003318void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003319 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003320 const String8 address /*in*/,
3321 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003322 sp<DeviceDescriptor> devDesc =
3323 desc->mProfile->mSupportedDevices.getDevice(device, address);
3324 if (devDesc != 0) {
3325 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3326 desc->mIoHandle, address.string());
3327 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003328 }
3329}
3330
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003331status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
Eric Laurent3b73df72014-03-11 09:06:29 -07003332 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07003333 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07003334 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003335{
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003336 audio_devices_t device = devDesc->mDeviceType;
Eric Laurent1f2f2232014-06-02 12:01:23 -07003337 sp<AudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003338 // erase all current sample rates, formats and channel masks
3339 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003340
Eric Laurent3b73df72014-03-11 09:06:29 -07003341 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003342 // first list already open outputs that can be routed to this device
3343 for (size_t i = 0; i < mOutputs.size(); i++) {
3344 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003345 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003346 if (!deviceDistinguishesOnAddress(device)) {
3347 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3348 outputs.add(mOutputs.keyAt(i));
3349 } else {
3350 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003351 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003352 }
Eric Laurente552edb2014-03-10 17:42:56 -07003353 }
3354 }
3355 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003356 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003357 for (size_t i = 0; i < mHwModules.size(); i++)
3358 {
3359 if (mHwModules[i]->mHandle == 0) {
3360 continue;
3361 }
3362 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3363 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003364 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3365 if (profile->mSupportedDevices.types() & device) {
3366 if (!deviceDistinguishesOnAddress(device) ||
3367 address == profile->mSupportedDevices[0]->mAddress) {
3368 profiles.add(profile);
3369 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3370 }
Eric Laurente552edb2014-03-10 17:42:56 -07003371 }
3372 }
3373 }
3374
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003375 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3376
Eric Laurente552edb2014-03-10 17:42:56 -07003377 if (profiles.isEmpty() && outputs.isEmpty()) {
3378 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3379 return BAD_VALUE;
3380 }
3381
3382 // open outputs for matching profiles if needed. Direct outputs are also opened to
3383 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3384 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003385 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003386
3387 // nothing to do if one output is already opened for this profile
3388 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003389 for (j = 0; j < outputs.size(); j++) {
3390 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003391 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003392 // matching profile: save the sample rates, format and channel masks supported
3393 // by the profile in our device descriptor
3394 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003395 break;
3396 }
3397 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003398 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003399 continue;
3400 }
3401
Eric Laurent83b88082014-06-20 18:31:16 -07003402 ALOGV("opening output for device %08x with params %s profile %p",
3403 device, address.string(), profile.get());
Eric Laurente552edb2014-03-10 17:42:56 -07003404 desc = new AudioOutputDescriptor(profile);
3405 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003406 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3407 config.sample_rate = desc->mSamplingRate;
3408 config.channel_mask = desc->mChannelMask;
3409 config.format = desc->mFormat;
3410 config.offload_info.sample_rate = desc->mSamplingRate;
3411 config.offload_info.channel_mask = desc->mChannelMask;
3412 config.offload_info.format = desc->mFormat;
3413 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
3414 status_t status = mpClientInterface->openOutput(profile->mModule->mHandle,
3415 &output,
3416 &config,
3417 &desc->mDevice,
3418 address,
3419 &desc->mLatency,
3420 desc->mFlags);
3421 if (status == NO_ERROR) {
3422 desc->mSamplingRate = config.sample_rate;
3423 desc->mChannelMask = config.channel_mask;
3424 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003425
Eric Laurentd4692962014-05-05 18:13:44 -07003426 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003427 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003428 char *param = audio_device_address_to_parameter(device, address);
3429 mpClientInterface->setParameters(output, String8(param));
3430 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003431 }
3432
Eric Laurentd4692962014-05-05 18:13:44 -07003433 // Here is where we step through and resolve any "dynamic" fields
3434 String8 reply;
3435 char *value;
3436 if (profile->mSamplingRates[0] == 0) {
3437 reply = mpClientInterface->getParameters(output,
3438 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003439 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003440 reply.string());
3441 value = strpbrk((char *)reply.string(), "=");
3442 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003443 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003444 }
Eric Laurentd4692962014-05-05 18:13:44 -07003445 }
3446 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3447 reply = mpClientInterface->getParameters(output,
3448 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003449 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003450 reply.string());
3451 value = strpbrk((char *)reply.string(), "=");
3452 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003453 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003454 }
Eric Laurentd4692962014-05-05 18:13:44 -07003455 }
3456 if (profile->mChannelMasks[0] == 0) {
3457 reply = mpClientInterface->getParameters(output,
3458 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003459 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003460 reply.string());
3461 value = strpbrk((char *)reply.string(), "=");
3462 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003463 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003464 }
Eric Laurentd4692962014-05-05 18:13:44 -07003465 }
3466 if (((profile->mSamplingRates[0] == 0) &&
3467 (profile->mSamplingRates.size() < 2)) ||
3468 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3469 (profile->mFormats.size() < 2)) ||
3470 ((profile->mChannelMasks[0] == 0) &&
3471 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003472 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003473 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003474 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003475 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3476 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003477 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003478 config.sample_rate = profile->pickSamplingRate();
3479 config.channel_mask = profile->pickChannelMask();
3480 config.format = profile->pickFormat();
3481 config.offload_info.sample_rate = config.sample_rate;
3482 config.offload_info.channel_mask = config.channel_mask;
3483 config.offload_info.format = config.format;
3484 status = mpClientInterface->openOutput(profile->mModule->mHandle,
3485 &output,
3486 &config,
3487 &desc->mDevice,
3488 address,
3489 &desc->mLatency,
3490 desc->mFlags);
3491 if (status == NO_ERROR) {
3492 desc->mSamplingRate = config.sample_rate;
3493 desc->mChannelMask = config.channel_mask;
3494 desc->mFormat = config.format;
3495 } else {
3496 output = AUDIO_IO_HANDLE_NONE;
3497 }
Eric Laurentd4692962014-05-05 18:13:44 -07003498 }
3499
Eric Laurentcf2c0212014-07-25 16:20:43 -07003500 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003501 addOutput(output, desc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003502 if (deviceDistinguishesOnAddress(device) && address != "0") {
3503 ssize_t index = mPolicyMixes.indexOfKey(address);
3504 if (index >= 0) {
3505 mPolicyMixes[index]->mOutput = desc;
Eric Laurentc722f302014-12-10 11:21:49 -08003506 desc->mPolicyMix = &mPolicyMixes[index]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08003507 } else {
3508 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3509 address.string());
3510 }
Eric Laurentc722f302014-12-10 11:21:49 -08003511 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3512 // no duplicated output for direct outputs and
3513 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003514 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003515
Eric Laurentd4692962014-05-05 18:13:44 -07003516 // set initial stream volume for device
3517 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003518
Eric Laurentd4692962014-05-05 18:13:44 -07003519 //TODO: configure audio effect output stage here
3520
3521 // open a duplicating output thread for the new output and the primary output
3522 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
3523 mPrimaryOutput);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003524 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003525 // add duplicated output descriptor
Eric Laurentcf2c0212014-07-25 16:20:43 -07003526 sp<AudioOutputDescriptor> dupOutputDesc =
3527 new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07003528 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
3529 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
3530 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3531 dupOutputDesc->mFormat = desc->mFormat;
3532 dupOutputDesc->mChannelMask = desc->mChannelMask;
3533 dupOutputDesc->mLatency = desc->mLatency;
3534 addOutput(duplicatedOutput, dupOutputDesc);
3535 applyStreamVolumes(duplicatedOutput, device, 0, true);
3536 } else {
3537 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
3538 mPrimaryOutput, output);
3539 mpClientInterface->closeOutput(output);
3540 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003541 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003542 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003543 }
Eric Laurente552edb2014-03-10 17:42:56 -07003544 }
3545 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003546 } else {
3547 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003548 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003549 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003550 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003551 profiles.removeAt(profile_index);
3552 profile_index--;
3553 } else {
3554 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003555 devDesc->importAudioPort(profile);
3556
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003557 if (deviceDistinguishesOnAddress(device)) {
3558 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3559 device, address.string());
3560 setOutputDevice(output, device, true/*force*/, 0/*delay*/,
3561 NULL/*patch handle*/, address.string());
3562 }
Eric Laurente552edb2014-03-10 17:42:56 -07003563 ALOGV("checkOutputsForDevice(): adding output %d", output);
3564 }
3565 }
3566
3567 if (profiles.isEmpty()) {
3568 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3569 return BAD_VALUE;
3570 }
Eric Laurentd4692962014-05-05 18:13:44 -07003571 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003572 // check if one opened output is not needed any more after disconnecting one device
3573 for (size_t i = 0; i < mOutputs.size(); i++) {
3574 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003575 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003576 // exact match on device
3577 if (deviceDistinguishesOnAddress(device) &&
3578 (desc->mProfile->mSupportedDevices.types() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003579 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurent275e8e92014-11-30 15:14:47 -08003580 } else if (!(desc->mProfile->mSupportedDevices.types()
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003581 & mAvailableOutputDevices.types())) {
3582 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3583 mOutputs.keyAt(i));
3584 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003585 }
Eric Laurente552edb2014-03-10 17:42:56 -07003586 }
3587 }
Eric Laurentd4692962014-05-05 18:13:44 -07003588 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003589 for (size_t i = 0; i < mHwModules.size(); i++)
3590 {
3591 if (mHwModules[i]->mHandle == 0) {
3592 continue;
3593 }
3594 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3595 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003596 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003597 if (profile->mSupportedDevices.types() & device) {
3598 ALOGV("checkOutputsForDevice(): "
3599 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003600 if (profile->mSamplingRates[0] == 0) {
3601 profile->mSamplingRates.clear();
3602 profile->mSamplingRates.add(0);
3603 }
3604 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3605 profile->mFormats.clear();
3606 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3607 }
3608 if (profile->mChannelMasks[0] == 0) {
3609 profile->mChannelMasks.clear();
3610 profile->mChannelMasks.add(0);
3611 }
3612 }
3613 }
3614 }
3615 }
3616 return NO_ERROR;
3617}
3618
Eric Laurentd4692962014-05-05 18:13:44 -07003619status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
3620 audio_policy_dev_state_t state,
3621 SortedVector<audio_io_handle_t>& inputs,
3622 const String8 address)
3623{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003624 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003625 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3626 // first list already open inputs that can be routed to this device
3627 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3628 desc = mInputs.valueAt(input_index);
3629 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3630 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3631 inputs.add(mInputs.keyAt(input_index));
3632 }
3633 }
3634
3635 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003636 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003637 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3638 {
3639 if (mHwModules[module_idx]->mHandle == 0) {
3640 continue;
3641 }
3642 for (size_t profile_index = 0;
3643 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3644 profile_index++)
3645 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003646 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3647
3648 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3649 if (!deviceDistinguishesOnAddress(device) ||
3650 address == profile->mSupportedDevices[0]->mAddress) {
3651 profiles.add(profile);
3652 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3653 profile_index, module_idx);
3654 }
Eric Laurentd4692962014-05-05 18:13:44 -07003655 }
3656 }
3657 }
3658
3659 if (profiles.isEmpty() && inputs.isEmpty()) {
3660 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3661 return BAD_VALUE;
3662 }
3663
3664 // open inputs for matching profiles if needed. Direct inputs are also opened to
3665 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3666 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3667
Eric Laurent1c333e22014-05-20 10:48:17 -07003668 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003669 // nothing to do if one input is already opened for this profile
3670 size_t input_index;
3671 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3672 desc = mInputs.valueAt(input_index);
3673 if (desc->mProfile == profile) {
3674 break;
3675 }
3676 }
3677 if (input_index != mInputs.size()) {
3678 continue;
3679 }
3680
3681 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3682 desc = new AudioInputDescriptor(profile);
3683 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003684 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3685 config.sample_rate = desc->mSamplingRate;
3686 config.channel_mask = desc->mChannelMask;
3687 config.format = desc->mFormat;
3688 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3689 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
3690 &input,
3691 &config,
3692 &desc->mDevice,
3693 address,
3694 AUDIO_SOURCE_MIC,
3695 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003696
Eric Laurentcf2c0212014-07-25 16:20:43 -07003697 if (status == NO_ERROR) {
3698 desc->mSamplingRate = config.sample_rate;
3699 desc->mChannelMask = config.channel_mask;
3700 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003701
Eric Laurentd4692962014-05-05 18:13:44 -07003702 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003703 char *param = audio_device_address_to_parameter(device, address);
3704 mpClientInterface->setParameters(input, String8(param));
3705 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003706 }
3707
3708 // Here is where we step through and resolve any "dynamic" fields
3709 String8 reply;
3710 char *value;
3711 if (profile->mSamplingRates[0] == 0) {
3712 reply = mpClientInterface->getParameters(input,
3713 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3714 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3715 reply.string());
3716 value = strpbrk((char *)reply.string(), "=");
3717 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003718 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003719 }
3720 }
3721 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3722 reply = mpClientInterface->getParameters(input,
3723 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3724 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3725 value = strpbrk((char *)reply.string(), "=");
3726 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003727 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003728 }
3729 }
3730 if (profile->mChannelMasks[0] == 0) {
3731 reply = mpClientInterface->getParameters(input,
3732 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3733 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3734 reply.string());
3735 value = strpbrk((char *)reply.string(), "=");
3736 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003737 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003738 }
3739 }
3740 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3741 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3742 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3743 ALOGW("checkInputsForDevice() direct input missing param");
3744 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003745 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003746 }
3747
3748 if (input != 0) {
3749 addInput(input, desc);
3750 }
3751 } // endif input != 0
3752
Eric Laurentcf2c0212014-07-25 16:20:43 -07003753 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003754 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003755 profiles.removeAt(profile_index);
3756 profile_index--;
3757 } else {
3758 inputs.add(input);
3759 ALOGV("checkInputsForDevice(): adding input %d", input);
3760 }
3761 } // end scan profiles
3762
3763 if (profiles.isEmpty()) {
3764 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3765 return BAD_VALUE;
3766 }
3767 } else {
3768 // Disconnect
3769 // check if one opened input is not needed any more after disconnecting one device
3770 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3771 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003772 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3773 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003774 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3775 mInputs.keyAt(input_index));
3776 inputs.add(mInputs.keyAt(input_index));
3777 }
3778 }
3779 // Clear any profiles associated with the disconnected device.
3780 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3781 if (mHwModules[module_index]->mHandle == 0) {
3782 continue;
3783 }
3784 for (size_t profile_index = 0;
3785 profile_index < mHwModules[module_index]->mInputProfiles.size();
3786 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003787 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003788 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003789 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003790 profile_index, module_index);
3791 if (profile->mSamplingRates[0] == 0) {
3792 profile->mSamplingRates.clear();
3793 profile->mSamplingRates.add(0);
3794 }
3795 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3796 profile->mFormats.clear();
3797 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3798 }
3799 if (profile->mChannelMasks[0] == 0) {
3800 profile->mChannelMasks.clear();
3801 profile->mChannelMasks.add(0);
3802 }
3803 }
3804 }
3805 }
3806 } // end disconnect
3807
3808 return NO_ERROR;
3809}
3810
3811
Eric Laurente0720872014-03-11 09:30:41 -07003812void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003813{
3814 ALOGV("closeOutput(%d)", output);
3815
Eric Laurent1f2f2232014-06-02 12:01:23 -07003816 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003817 if (outputDesc == NULL) {
3818 ALOGW("closeOutput() unknown output %d", output);
3819 return;
3820 }
3821
Eric Laurent275e8e92014-11-30 15:14:47 -08003822 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
3823 if (mPolicyMixes[i]->mOutput == outputDesc) {
3824 mPolicyMixes[i]->mOutput.clear();
3825 }
3826 }
3827
Eric Laurente552edb2014-03-10 17:42:56 -07003828 // look for duplicated outputs connected to the output being removed.
3829 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003830 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003831 if (dupOutputDesc->isDuplicated() &&
3832 (dupOutputDesc->mOutput1 == outputDesc ||
3833 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003834 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003835 if (dupOutputDesc->mOutput1 == outputDesc) {
3836 outputDesc2 = dupOutputDesc->mOutput2;
3837 } else {
3838 outputDesc2 = dupOutputDesc->mOutput1;
3839 }
3840 // As all active tracks on duplicated output will be deleted,
3841 // and as they were also referenced on the other output, the reference
3842 // count for their stream type must be adjusted accordingly on
3843 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003844 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003845 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003846 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003847 }
3848 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3849 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3850
3851 mpClientInterface->closeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003852 mOutputs.removeItem(duplicatedOutput);
3853 }
3854 }
3855
Eric Laurent05b90f82014-08-27 15:32:29 -07003856 nextAudioPortGeneration();
3857
3858 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3859 if (index >= 0) {
3860 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3861 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3862 mAudioPatches.removeItemsAt(index);
3863 mpClientInterface->onAudioPatchListUpdate();
3864 }
3865
Eric Laurente552edb2014-03-10 17:42:56 -07003866 AudioParameter param;
3867 param.add(String8("closing"), String8("true"));
3868 mpClientInterface->setParameters(output, param.toString());
3869
3870 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003871 mOutputs.removeItem(output);
3872 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003873}
3874
3875void AudioPolicyManager::closeInput(audio_io_handle_t input)
3876{
3877 ALOGV("closeInput(%d)", input);
3878
3879 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3880 if (inputDesc == NULL) {
3881 ALOGW("closeInput() unknown input %d", input);
3882 return;
3883 }
3884
Eric Laurent6a94d692014-05-20 11:18:06 -07003885 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003886
3887 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3888 if (index >= 0) {
3889 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3890 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3891 mAudioPatches.removeItemsAt(index);
3892 mpClientInterface->onAudioPatchListUpdate();
3893 }
3894
3895 mpClientInterface->closeInput(input);
3896 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003897}
3898
Eric Laurente0720872014-03-11 09:30:41 -07003899SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -07003900 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003901{
3902 SortedVector<audio_io_handle_t> outputs;
3903
3904 ALOGVV("getOutputsForDevice() device %04x", device);
3905 for (size_t i = 0; i < openOutputs.size(); i++) {
3906 ALOGVV("output %d isDuplicated=%d device=%04x",
3907 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3908 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3909 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3910 outputs.add(openOutputs.keyAt(i));
3911 }
3912 }
3913 return outputs;
3914}
3915
Eric Laurente0720872014-03-11 09:30:41 -07003916bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07003917 SortedVector<audio_io_handle_t>& outputs2)
3918{
3919 if (outputs1.size() != outputs2.size()) {
3920 return false;
3921 }
3922 for (size_t i = 0; i < outputs1.size(); i++) {
3923 if (outputs1[i] != outputs2[i]) {
3924 return false;
3925 }
3926 }
3927 return true;
3928}
3929
Eric Laurente0720872014-03-11 09:30:41 -07003930void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003931{
3932 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3933 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3934 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3935 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3936
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003937 // also take into account external policy-related changes: add all outputs which are
3938 // associated with policies in the "before" and "after" output vectors
3939 ALOGVV("checkOutputForStrategy(): policy related outputs");
3940 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
3941 const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
3942 if (desc != 0 && desc->mPolicyMix != NULL) {
3943 srcOutputs.add(desc->mIoHandle);
3944 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3945 }
3946 }
3947 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
3948 const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3949 if (desc != 0 && desc->mPolicyMix != NULL) {
3950 dstOutputs.add(desc->mIoHandle);
3951 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3952 }
3953 }
3954
Eric Laurente552edb2014-03-10 17:42:56 -07003955 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3956 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3957 strategy, srcOutputs[0], dstOutputs[0]);
3958 // mute strategy while moving tracks from one output to another
3959 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003960 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003961 if (isStrategyActive(desc, strategy)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003962 setStrategyMute(strategy, true, srcOutputs[i]);
3963 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3964 }
3965 }
3966
3967 // Move effects associated to this strategy from previous output to new output
3968 if (strategy == STRATEGY_MEDIA) {
3969 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3970 SortedVector<audio_io_handle_t> moved;
3971 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003972 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3973 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3974 effectDesc->mIo != fxOutput) {
3975 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003976 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3977 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003978 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003979 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003980 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003981 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003982 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003983 }
3984 }
3985 }
3986 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003987 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003988 if (i == AUDIO_STREAM_PATCH) {
3989 continue;
3990 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003991 if (getStrategy((audio_stream_type_t)i) == strategy) {
3992 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003993 }
3994 }
3995 }
3996}
3997
Eric Laurente0720872014-03-11 09:30:41 -07003998void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003999{
Jon Eklund966095e2014-09-09 15:39:49 -05004000 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4001 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004002 checkOutputForStrategy(STRATEGY_PHONE);
Jon Eklund966095e2014-09-09 15:39:49 -05004003 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4004 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004005 checkOutputForStrategy(STRATEGY_SONIFICATION);
4006 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004007 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004008 checkOutputForStrategy(STRATEGY_MEDIA);
4009 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004010 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004011}
4012
Eric Laurente0720872014-03-11 09:30:41 -07004013audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07004014{
Eric Laurente552edb2014-03-10 17:42:56 -07004015 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004016 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004017 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
4018 return mOutputs.keyAt(i);
4019 }
4020 }
4021
4022 return 0;
4023}
4024
Eric Laurente0720872014-03-11 09:30:41 -07004025void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004026{
Eric Laurente552edb2014-03-10 17:42:56 -07004027 audio_io_handle_t a2dpOutput = getA2dpOutput();
4028 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004029 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004030 return;
4031 }
4032
Eric Laurent3a4311c2014-03-17 12:00:47 -07004033 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004034 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4035 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4036 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004037 // suspend A2DP output if:
4038 // (NOT already suspended) &&
4039 // ((SCO device is connected &&
4040 // (forced usage for communication || for record is SCO))) ||
4041 // (phone state is ringing || in call)
4042 //
4043 // restore A2DP output if:
4044 // (Already suspended) &&
4045 // ((SCO device is NOT connected ||
4046 // (forced usage NOT for communication && NOT for record is SCO))) &&
4047 // (phone state is NOT ringing && NOT in call)
4048 //
4049 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004050 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004051 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
4052 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
4053 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
4054 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004055
4056 mpClientInterface->restoreOutput(a2dpOutput);
4057 mA2dpSuspended = false;
4058 }
4059 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004060 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004061 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4062 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
4063 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
4064 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004065
4066 mpClientInterface->suspendOutput(a2dpOutput);
4067 mA2dpSuspended = true;
4068 }
4069 }
4070}
4071
Eric Laurent1c333e22014-05-20 10:48:17 -07004072audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004073{
4074 audio_devices_t device = AUDIO_DEVICE_NONE;
4075
Eric Laurent1f2f2232014-06-02 12:01:23 -07004076 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004077
4078 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4079 if (index >= 0) {
4080 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4081 if (patchDesc->mUid != mUidCached) {
4082 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
4083 outputDesc->device(), outputDesc->mPatchHandle);
4084 return outputDesc->device();
4085 }
4086 }
4087
Eric Laurente552edb2014-03-10 17:42:56 -07004088 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004089 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004090 // use device for strategy enforced audible
4091 // 2: we are in call or the strategy phone is active on the output:
4092 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004093 // 3: the strategy for enforced audible is active but not enforced on the output:
4094 // use the device for strategy enforced audible
4095 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004096 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05004097 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004098 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004099 // 6: the strategy accessibility is active on the output:
4100 // use device for strategy accessibility
4101 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004102 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004103 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004104 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004105 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004106 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01004107 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
Jon Eklund966095e2014-09-09 15:39:49 -05004108 mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004109 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4110 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01004111 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004112 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004113 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05004114 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004115 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004116 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004117 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004118 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004119 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004120 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004121 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004122 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004123 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004124 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004125 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004126 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01004127 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004128 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004129 }
4130
Eric Laurent1c333e22014-05-20 10:48:17 -07004131 ALOGV("getNewOutputDevice() selected device %x", device);
4132 return device;
4133}
4134
4135audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
4136{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004137 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004138
4139 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4140 if (index >= 0) {
4141 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4142 if (patchDesc->mUid != mUidCached) {
4143 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4144 inputDesc->mDevice, inputDesc->mPatchHandle);
4145 return inputDesc->mDevice;
4146 }
4147 }
4148
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004149 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07004150
4151 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004152 return device;
4153}
4154
Eric Laurente0720872014-03-11 09:30:41 -07004155uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004156 return (uint32_t)getStrategy(stream);
4157}
4158
Eric Laurente0720872014-03-11 09:30:41 -07004159audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004160 // By checking the range of stream before calling getStrategy, we avoid
4161 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4162 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004163 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004164 return AUDIO_DEVICE_NONE;
4165 }
4166 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004167 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004168 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4169 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4170 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004171 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004172 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004173 devices = outputDesc->device();
4174 break;
4175 }
Eric Laurente552edb2014-03-10 17:42:56 -07004176 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004177
4178 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4179 and doesn't really need to.*/
4180 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4181 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4182 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4183 }
4184
Eric Laurente552edb2014-03-10 17:42:56 -07004185 return devices;
4186}
4187
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004188routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07004189 audio_stream_type_t stream) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004190
4191 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
4192
Eric Laurente552edb2014-03-10 17:42:56 -07004193 // stream to strategy mapping
4194 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004195 case AUDIO_STREAM_VOICE_CALL:
4196 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004197 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004198 case AUDIO_STREAM_RING:
4199 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07004200 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07004201 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07004202 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07004203 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07004204 return STRATEGY_DTMF;
4205 default:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004206 ALOGE("unknown stream type %d", stream);
Eric Laurent3b73df72014-03-11 09:06:29 -07004207 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07004208 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
4209 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07004210 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004211 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07004212 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07004213 return STRATEGY_ENFORCED_AUDIBLE;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004214 case AUDIO_STREAM_TTS:
4215 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004216 case AUDIO_STREAM_ACCESSIBILITY:
4217 return STRATEGY_ACCESSIBILITY;
4218 case AUDIO_STREAM_REROUTING:
4219 return STRATEGY_REROUTING;
Eric Laurente552edb2014-03-10 17:42:56 -07004220 }
4221}
4222
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004223uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4224 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004225 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4226 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4227 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004228 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4229 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4230 }
4231
4232 // usage to strategy mapping
4233 switch (attr->usage) {
Eric Laurent29e6cec2014-11-13 18:17:55 -08004234 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4235 if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) {
4236 return (uint32_t) STRATEGY_SONIFICATION;
4237 }
4238 if (isInCall()) {
4239 return (uint32_t) STRATEGY_PHONE;
4240 }
Eric Laurent0f78eab2014-11-25 11:01:34 -08004241 return (uint32_t) STRATEGY_ACCESSIBILITY;
Eric Laurent29e6cec2014-11-13 18:17:55 -08004242
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004243 case AUDIO_USAGE_MEDIA:
4244 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004245 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4246 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4247 return (uint32_t) STRATEGY_MEDIA;
4248
4249 case AUDIO_USAGE_VOICE_COMMUNICATION:
4250 return (uint32_t) STRATEGY_PHONE;
4251
4252 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4253 return (uint32_t) STRATEGY_DTMF;
4254
4255 case AUDIO_USAGE_ALARM:
4256 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4257 return (uint32_t) STRATEGY_SONIFICATION;
4258
4259 case AUDIO_USAGE_NOTIFICATION:
4260 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4261 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4262 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4263 case AUDIO_USAGE_NOTIFICATION_EVENT:
4264 return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL;
4265
4266 case AUDIO_USAGE_UNKNOWN:
4267 default:
4268 return (uint32_t) STRATEGY_MEDIA;
4269 }
4270}
4271
Eric Laurente0720872014-03-11 09:30:41 -07004272void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004273 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004274 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004275 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4276 updateDevicesAndOutputs();
4277 break;
4278 default:
4279 break;
4280 }
4281}
4282
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004283bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) {
4284 for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) {
4285 if (s == (size_t) streamToIgnore) {
4286 continue;
4287 }
4288 for (size_t i = 0; i < mOutputs.size(); i++) {
4289 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
4290 if (outputDesc->mRefCount[s] != 0) {
4291 return true;
4292 }
4293 }
4294 }
4295 return false;
4296}
4297
4298uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4299 switch(event) {
4300 case STARTING_OUTPUT:
4301 mBeaconMuteRefCount++;
4302 break;
4303 case STOPPING_OUTPUT:
4304 if (mBeaconMuteRefCount > 0) {
4305 mBeaconMuteRefCount--;
4306 }
4307 break;
4308 case STARTING_BEACON:
4309 mBeaconPlayingRefCount++;
4310 break;
4311 case STOPPING_BEACON:
4312 if (mBeaconPlayingRefCount > 0) {
4313 mBeaconPlayingRefCount--;
4314 }
4315 break;
4316 }
4317
4318 if (mBeaconMuteRefCount > 0) {
4319 // any playback causes beacon to be muted
4320 return setBeaconMute(true);
4321 } else {
4322 // no other playback: unmute when beacon starts playing, mute when it stops
4323 return setBeaconMute(mBeaconPlayingRefCount == 0);
4324 }
4325}
4326
4327uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4328 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4329 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4330 // keep track of muted state to avoid repeating mute/unmute operations
4331 if (mBeaconMuted != mute) {
4332 // mute/unmute AUDIO_STREAM_TTS on all outputs
4333 ALOGV("\t muting %d", mute);
4334 uint32_t maxLatency = 0;
4335 for (size_t i = 0; i < mOutputs.size(); i++) {
4336 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4337 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
4338 desc->mIoHandle,
4339 0 /*delay*/, AUDIO_DEVICE_NONE);
4340 const uint32_t latency = desc->latency() * 2;
4341 if (latency > maxLatency) {
4342 maxLatency = latency;
4343 }
4344 }
4345 mBeaconMuted = mute;
4346 return maxLatency;
4347 }
4348 return 0;
4349}
4350
Eric Laurente0720872014-03-11 09:30:41 -07004351audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004352 bool fromCache)
4353{
4354 uint32_t device = AUDIO_DEVICE_NONE;
4355
4356 if (fromCache) {
4357 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4358 strategy, mDeviceForStrategy[strategy]);
4359 return mDeviceForStrategy[strategy];
4360 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004361 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004362 switch (strategy) {
4363
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004364 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
4365 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4366 if (!device) {
4367 ALOGE("getDeviceForStrategy() no device found for "\
4368 "STRATEGY_TRANSMITTED_THROUGH_SPEAKER");
4369 }
4370 break;
4371
Eric Laurente552edb2014-03-10 17:42:56 -07004372 case STRATEGY_SONIFICATION_RESPECTFUL:
4373 if (isInCall()) {
4374 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004375 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07004376 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
4377 // while media is playing on a remote device, use the the sonification behavior.
4378 // Note that we test this usecase before testing if media is playing because
4379 // the isStreamActive() method only informs about the activity of a stream, not
4380 // if it's for local playback. Note also that we use the same delay between both tests
4381 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004382 //user "safe" speaker if available instead of normal speaker to avoid triggering
4383 //other acoustic safety mechanisms for notification
4384 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4385 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004386 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004387 // while media is playing (or has recently played), use the same device
4388 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4389 } else {
4390 // when media is not playing anymore, fall back on the sonification behavior
4391 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004392 //user "safe" speaker if available instead of normal speaker to avoid triggering
4393 //other acoustic safety mechanisms for notification
4394 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4395 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurente552edb2014-03-10 17:42:56 -07004396 }
4397
4398 break;
4399
4400 case STRATEGY_DTMF:
4401 if (!isInCall()) {
4402 // when off call, DTMF strategy follows the same rules as MEDIA strategy
4403 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4404 break;
4405 }
4406 // when in call, DTMF and PHONE strategies follow the same rules
4407 // FALL THROUGH
4408
4409 case STRATEGY_PHONE:
Eric Laurentc2730ba2014-07-20 15:47:07 -07004410 // Force use of only devices on primary output if:
4411 // - in call AND
4412 // - cannot route from voice call RX OR
4413 // - audio HAL version is < 3.0 and TX device is on the primary HW module
4414 if (mPhoneState == AUDIO_MODE_IN_CALL) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004415 audio_devices_t txDevice =
4416 getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -07004417 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
4418 if (((mAvailableInputDevices.types() &
4419 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
4420 (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Marco Nelissen961ec212014-08-25 15:58:39 -07004421 (hwOutputDesc->getAudioPort()->mModule->mHalVersion <
Eric Laurentc2730ba2014-07-20 15:47:07 -07004422 AUDIO_DEVICE_API_VERSION_3_0))) {
4423 availableOutputDeviceTypes = availablePrimaryOutputDevices();
4424 }
4425 }
Eric Laurente552edb2014-03-10 17:42:56 -07004426 // for phone strategy, we first consider the forced use and then the available devices by order
4427 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07004428 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
4429 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004430 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004431 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07004432 if (device) break;
4433 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004434 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004435 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004436 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07004437 if (device) break;
4438 // if SCO device is requested but no SCO device is available, fall back to default case
4439 // FALL THROUGH
4440
4441 default: // FORCE_NONE
4442 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07004443 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004444 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004445 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004446 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004447 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004448 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004449 if (device) break;
4450 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004451 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004452 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004453 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004454 if (device) break;
Eric Laurentc2730ba2014-07-20 15:47:07 -07004455 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
4456 if (device) break;
Eric Laurentacc930d2015-03-04 10:08:29 -08004457 if (!isInCall()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004458 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004459 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004460 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004461 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004462 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004463 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004464 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004465 if (device) break;
4466 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004467 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07004468 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004469 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004470 if (device == AUDIO_DEVICE_NONE) {
4471 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
4472 }
4473 break;
4474
Eric Laurent3b73df72014-03-11 09:06:29 -07004475 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07004476 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
4477 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07004478 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004479 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004480 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004481 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004482 if (device) break;
4483 }
Eric Laurentcd71a692014-12-16 12:01:12 -08004484 if (!isInCall()) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004485 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004486 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004487 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004488 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004489 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004490 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004491 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004492 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004493 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004494 if (device) break;
4495 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004496 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4497 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004498 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004499 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004500 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004501 if (device == AUDIO_DEVICE_NONE) {
4502 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
4503 }
4504 break;
4505 }
4506 break;
4507
4508 case STRATEGY_SONIFICATION:
4509
4510 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
4511 // handleIncallSonification().
4512 if (isInCall()) {
4513 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
4514 break;
4515 }
4516 // FALL THROUGH
4517
4518 case STRATEGY_ENFORCED_AUDIBLE:
4519 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
4520 // except:
4521 // - when in call where it doesn't default to STRATEGY_PHONE behavior
4522 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
4523
4524 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004525 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004526 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004527 if (device == AUDIO_DEVICE_NONE) {
4528 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
4529 }
4530 }
4531 // The second device used for sonification is the same as the device used by media strategy
4532 // FALL THROUGH
4533
Eric Laurent223fd5c2014-11-11 13:43:36 -08004534 // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now
4535 case STRATEGY_ACCESSIBILITY:
Eric Laurent066ceec2014-11-25 12:35:01 -08004536 if (strategy == STRATEGY_ACCESSIBILITY) {
4537 // do not route accessibility prompts to a digital output currently configured with a
4538 // compressed format as they would likely not be mixed and dropped.
4539 for (size_t i = 0; i < mOutputs.size(); i++) {
4540 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4541 audio_devices_t devices = desc->device() &
4542 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
4543 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
4544 devices != AUDIO_DEVICE_NONE) {
4545 availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices;
4546 }
4547 }
4548 }
4549 // FALL THROUGH
4550
Eric Laurent223fd5c2014-11-11 13:43:36 -08004551 case STRATEGY_REROUTING:
Eric Laurente552edb2014-03-10 17:42:56 -07004552 case STRATEGY_MEDIA: {
4553 uint32_t device2 = AUDIO_DEVICE_NONE;
4554 if (strategy != STRATEGY_SONIFICATION) {
4555 // no sonification on remote submix (e.g. WFD)
Eric Laurent275e8e92014-11-30 15:14:47 -08004556 if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
4557 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
4558 }
Eric Laurente552edb2014-03-10 17:42:56 -07004559 }
4560 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004561 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004562 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004563 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004564 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004565 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004566 }
4567 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004568 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004569 }
4570 }
Hochi Huang327cb702014-09-21 09:47:31 +08004571 if ((device2 == AUDIO_DEVICE_NONE) &&
4572 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
4573 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4574 }
Eric Laurente552edb2014-03-10 17:42:56 -07004575 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004576 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004577 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004578 if ((device2 == AUDIO_DEVICE_NONE)) {
4579 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4580 }
Eric Laurente552edb2014-03-10 17:42:56 -07004581 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004582 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004583 }
4584 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004585 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004586 }
4587 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004588 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004589 }
4590 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004591 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004592 }
4593 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
4594 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07004595 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004596 }
4597 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004598 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004599 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004600 }
4601 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004602 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004603 }
Jungshik Jang839e4f32014-06-26 17:23:40 +09004604 int device3 = AUDIO_DEVICE_NONE;
4605 if (strategy == STRATEGY_MEDIA) {
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004606 // ARC, SPDIF and AUX_LINE can co-exist with others.
Jungshik Jang0c943092014-07-08 22:11:24 +09004607 device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC;
4608 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004609 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE);
Jungshik Jang839e4f32014-06-26 17:23:40 +09004610 }
Eric Laurente552edb2014-03-10 17:42:56 -07004611
Jungshik Jang839e4f32014-06-26 17:23:40 +09004612 device2 |= device3;
Eric Laurente552edb2014-03-10 17:42:56 -07004613 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
4614 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
4615 device |= device2;
Jungshik Jang839e4f32014-06-26 17:23:40 +09004616
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004617 // If hdmi system audio mode is on, remove speaker out of output list.
4618 if ((strategy == STRATEGY_MEDIA) &&
4619 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
4620 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
4621 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
4622 }
4623
Eric Laurente552edb2014-03-10 17:42:56 -07004624 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004625 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004626 if (device == AUDIO_DEVICE_NONE) {
4627 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
4628 }
4629 } break;
4630
4631 default:
4632 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
4633 break;
4634 }
4635
4636 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
4637 return device;
4638}
4639
Eric Laurente0720872014-03-11 09:30:41 -07004640void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004641{
4642 for (int i = 0; i < NUM_STRATEGIES; i++) {
4643 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4644 }
4645 mPreviousOutputs = mOutputs;
4646}
4647
Eric Laurent1f2f2232014-06-02 12:01:23 -07004648uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004649 audio_devices_t prevDevice,
4650 uint32_t delayMs)
4651{
4652 // mute/unmute strategies using an incompatible device combination
4653 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4654 // if unmuting, unmute only after the specified delay
4655 if (outputDesc->isDuplicated()) {
4656 return 0;
4657 }
4658
4659 uint32_t muteWaitMs = 0;
4660 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004661 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004662
4663 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4664 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurent31551f82014-10-10 18:21:56 -07004665 curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004666 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4667 bool doMute = false;
4668
4669 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4670 doMute = true;
4671 outputDesc->mStrategyMutedByDevice[i] = true;
4672 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4673 doMute = true;
4674 outputDesc->mStrategyMutedByDevice[i] = false;
4675 }
Eric Laurent99401132014-05-07 19:48:15 -07004676 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004677 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004678 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004679 // skip output if it does not share any device with current output
4680 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4681 == AUDIO_DEVICE_NONE) {
4682 continue;
4683 }
4684 audio_io_handle_t curOutput = mOutputs.keyAt(j);
4685 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
4686 mute ? "muting" : "unmuting", i, curDevice, curOutput);
4687 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004688 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004689 if (mute) {
4690 // FIXME: should not need to double latency if volume could be applied
4691 // immediately by the audioflinger mixer. We must account for the delay
4692 // between now and the next time the audioflinger thread for this output
4693 // will process a buffer (which corresponds to one buffer size,
4694 // usually 1/2 or 1/4 of the latency).
4695 if (muteWaitMs < desc->latency() * 2) {
4696 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004697 }
4698 }
4699 }
4700 }
4701 }
4702 }
4703
Eric Laurent99401132014-05-07 19:48:15 -07004704 // temporary mute output if device selection changes to avoid volume bursts due to
4705 // different per device volumes
4706 if (outputDesc->isActive() && (device != prevDevice)) {
4707 if (muteWaitMs < outputDesc->latency() * 2) {
4708 muteWaitMs = outputDesc->latency() * 2;
4709 }
4710 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004711 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004712 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07004713 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07004714 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07004715 muteWaitMs *2, device);
4716 }
4717 }
4718 }
4719
Eric Laurente552edb2014-03-10 17:42:56 -07004720 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4721 if (muteWaitMs > delayMs) {
4722 muteWaitMs -= delayMs;
4723 usleep(muteWaitMs * 1000);
4724 return muteWaitMs;
4725 }
4726 return 0;
4727}
4728
Eric Laurente0720872014-03-11 09:30:41 -07004729uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004730 audio_devices_t device,
4731 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004732 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004733 audio_patch_handle_t *patchHandle,
4734 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004735{
4736 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004737 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004738 AudioParameter param;
4739 uint32_t muteWaitMs;
4740
4741 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004742 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
4743 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004744 return muteWaitMs;
4745 }
4746 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4747 // output profile
4748 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004749 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004750 return 0;
4751 }
4752
4753 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07004754 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004755
4756 audio_devices_t prevDevice = outputDesc->mDevice;
4757
4758 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
4759
4760 if (device != AUDIO_DEVICE_NONE) {
4761 outputDesc->mDevice = device;
4762 }
4763 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4764
4765 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004766 // the requested device is AUDIO_DEVICE_NONE
4767 // OR the requested device is the same as current device
4768 // AND force is not specified
4769 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004770 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentb80a2a82014-10-27 16:07:59 -07004771 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force &&
4772 outputDesc->mPatchHandle != 0) {
4773 ALOGV("setOutputDevice() setting same device %04x or null device for output %d",
4774 device, output);
Eric Laurente552edb2014-03-10 17:42:56 -07004775 return muteWaitMs;
4776 }
4777
4778 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004779
Eric Laurente552edb2014-03-10 17:42:56 -07004780 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004781 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004782 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004783 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004784 DeviceVector deviceList = (address == NULL) ?
4785 mAvailableOutputDevices.getDevicesFromType(device)
4786 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004787 if (!deviceList.isEmpty()) {
4788 struct audio_patch patch;
4789 outputDesc->toAudioPortConfig(&patch.sources[0]);
4790 patch.num_sources = 1;
4791 patch.num_sinks = 0;
4792 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4793 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004794 patch.num_sinks++;
4795 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004796 ssize_t index;
4797 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4798 index = mAudioPatches.indexOfKey(*patchHandle);
4799 } else {
4800 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4801 }
4802 sp< AudioPatch> patchDesc;
4803 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4804 if (index >= 0) {
4805 patchDesc = mAudioPatches.valueAt(index);
4806 afPatchHandle = patchDesc->mAfPatchHandle;
4807 }
4808
Eric Laurent1c333e22014-05-20 10:48:17 -07004809 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004810 &afPatchHandle,
4811 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004812 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4813 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004814 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004815 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004816 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004817 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004818 addAudioPatch(patchDesc->mHandle, patchDesc);
4819 } else {
4820 patchDesc->mPatch = patch;
4821 }
4822 patchDesc->mAfPatchHandle = afPatchHandle;
4823 patchDesc->mUid = mUidCached;
4824 if (patchHandle) {
4825 *patchHandle = patchDesc->mHandle;
4826 }
4827 outputDesc->mPatchHandle = patchDesc->mHandle;
4828 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004829 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004830 }
4831 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004832
4833 // inform all input as well
4834 for (size_t i = 0; i < mInputs.size(); i++) {
4835 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
4836 if (!isVirtualInputDevice(inputDescriptor->mDevice)) {
4837 AudioParameter inputCmd = AudioParameter();
4838 ALOGV("%s: inform input %d of device:%d", __func__,
4839 inputDescriptor->mIoHandle, device);
4840 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4841 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4842 inputCmd.toString(),
4843 delayMs);
4844 }
4845 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004846 }
Eric Laurente552edb2014-03-10 17:42:56 -07004847
4848 // update stream volumes according to new device
4849 applyStreamVolumes(output, device, delayMs);
4850
4851 return muteWaitMs;
4852}
4853
Eric Laurent1c333e22014-05-20 10:48:17 -07004854status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07004855 int delayMs,
4856 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004857{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004858 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004859 ssize_t index;
4860 if (patchHandle) {
4861 index = mAudioPatches.indexOfKey(*patchHandle);
4862 } else {
4863 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4864 }
4865 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 return INVALID_OPERATION;
4867 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004868 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4869 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004870 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4871 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004872 removeAudioPatch(patchDesc->mHandle);
4873 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004874 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004875 return status;
4876}
4877
4878status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4879 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004880 bool force,
4881 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004882{
4883 status_t status = NO_ERROR;
4884
Eric Laurent1f2f2232014-06-02 12:01:23 -07004885 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004886 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4887 inputDesc->mDevice = device;
4888
4889 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4890 if (!deviceList.isEmpty()) {
4891 struct audio_patch patch;
4892 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004893 // AUDIO_SOURCE_HOTWORD is for internal use only:
4894 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004895 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4896 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004897 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4898 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004899 patch.num_sinks = 1;
4900 //only one input device for now
4901 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004902 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004903 ssize_t index;
4904 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4905 index = mAudioPatches.indexOfKey(*patchHandle);
4906 } else {
4907 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4908 }
4909 sp< AudioPatch> patchDesc;
4910 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4911 if (index >= 0) {
4912 patchDesc = mAudioPatches.valueAt(index);
4913 afPatchHandle = patchDesc->mAfPatchHandle;
4914 }
4915
Eric Laurent1c333e22014-05-20 10:48:17 -07004916 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004917 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004918 0);
4919 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004920 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004921 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004922 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004923 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004924 addAudioPatch(patchDesc->mHandle, patchDesc);
4925 } else {
4926 patchDesc->mPatch = patch;
4927 }
4928 patchDesc->mAfPatchHandle = afPatchHandle;
4929 patchDesc->mUid = mUidCached;
4930 if (patchHandle) {
4931 *patchHandle = patchDesc->mHandle;
4932 }
4933 inputDesc->mPatchHandle = patchDesc->mHandle;
4934 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004935 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004936 }
4937 }
4938 }
4939 return status;
4940}
4941
Eric Laurent6a94d692014-05-20 11:18:06 -07004942status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4943 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004944{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004945 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004946 ssize_t index;
4947 if (patchHandle) {
4948 index = mAudioPatches.indexOfKey(*patchHandle);
4949 } else {
4950 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4951 }
4952 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004953 return INVALID_OPERATION;
4954 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004955 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4956 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004957 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4958 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004959 removeAudioPatch(patchDesc->mHandle);
4960 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004961 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004962 return status;
4963}
4964
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004965sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -08004966 String8 address,
Glenn Kastencbd48022014-07-24 13:46:44 -07004967 uint32_t& samplingRate,
Eric Laurente552edb2014-03-10 17:42:56 -07004968 audio_format_t format,
Glenn Kasten6a8ab052014-07-24 14:08:35 -07004969 audio_channel_mask_t channelMask,
Glenn Kastencbd48022014-07-24 13:46:44 -07004970 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004971{
4972 // Choose an input profile based on the requested capture parameters: select the first available
4973 // profile supporting all requested parameters.
4974
4975 for (size_t i = 0; i < mHwModules.size(); i++)
4976 {
4977 if (mHwModules[i]->mHandle == 0) {
4978 continue;
4979 }
4980 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4981 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004982 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004983 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004984 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004985 &samplingRate /*updatedSamplingRate*/,
4986 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004987
Eric Laurente552edb2014-03-10 17:42:56 -07004988 return profile;
4989 }
4990 }
4991 }
4992 return NULL;
4993}
4994
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004995
4996audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
Eric Laurentc722f302014-12-10 11:21:49 -08004997 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004998{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004999 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
5000 ~AUDIO_DEVICE_BIT_IN;
Eric Laurent275e8e92014-11-30 15:14:47 -08005001
5002 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
5003 if (mPolicyMixes[i]->mMix.mMixType != MIX_TYPE_RECORDERS) {
5004 continue;
5005 }
5006 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
5007 if ((RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5008 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource == inputSource) ||
5009 (RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5010 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource != inputSource)) {
5011 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurentc722f302014-12-10 11:21:49 -08005012 if (policyMix != NULL) {
5013 *policyMix = &mPolicyMixes[i]->mMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08005014 }
5015 return AUDIO_DEVICE_IN_REMOTE_SUBMIX;
5016 }
5017 break;
5018 }
5019 }
5020 }
5021
Eric Laurentc73ca6e2014-12-12 14:34:22 -08005022 return getDeviceForInputSource(inputSource);
5023}
5024
5025audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
5026{
5027 uint32_t device = AUDIO_DEVICE_NONE;
5028 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
5029 ~AUDIO_DEVICE_BIT_IN;
5030
Eric Laurente552edb2014-03-10 17:42:56 -07005031 switch (inputSource) {
5032 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005033 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005034 device = AUDIO_DEVICE_IN_VOICE_CALL;
5035 break;
5036 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005037 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005038
5039 case AUDIO_SOURCE_DEFAULT:
5040 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07005041 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
5042 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
Eric Laurentdc136ff2014-12-16 12:24:18 -08005043 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
5044 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
5045 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurentc2730ba2014-07-20 15:47:07 -07005046 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5047 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5048 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5049 device = AUDIO_DEVICE_IN_USB_DEVICE;
5050 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5051 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005052 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005053 break;
5054
5055 case AUDIO_SOURCE_VOICE_COMMUNICATION:
5056 // Allow only use of devices on primary input if in call and HAL does not support routing
5057 // to voice call path.
5058 if ((mPhoneState == AUDIO_MODE_IN_CALL) &&
5059 (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
5060 availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN;
5061 }
5062
5063 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
5064 case AUDIO_POLICY_FORCE_BT_SCO:
5065 // if SCO device is requested but no SCO device is available, fall back to default case
5066 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
5067 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
5068 break;
5069 }
5070 // FALL THROUGH
5071
5072 default: // FORCE_NONE
5073 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5074 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5075 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5076 device = AUDIO_DEVICE_IN_USB_DEVICE;
5077 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5078 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5079 }
5080 break;
5081
5082 case AUDIO_POLICY_FORCE_SPEAKER:
5083 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
5084 device = AUDIO_DEVICE_IN_BACK_MIC;
5085 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5086 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5087 }
5088 break;
5089 }
5090 break;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005091
Eric Laurente552edb2014-03-10 17:42:56 -07005092 case AUDIO_SOURCE_VOICE_RECOGNITION:
5093 case AUDIO_SOURCE_HOTWORD:
Eric Laurent3b73df72014-03-11 09:06:29 -07005094 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005095 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005096 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005097 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005098 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07005099 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5100 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005101 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005102 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5103 }
5104 break;
5105 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005106 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005107 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005108 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005109 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5110 }
5111 break;
5112 case AUDIO_SOURCE_VOICE_DOWNLINK:
5113 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005114 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005115 device = AUDIO_DEVICE_IN_VOICE_CALL;
5116 }
5117 break;
5118 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005119 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07005120 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
5121 }
5122 break;
Hochi Huang327cb702014-09-21 09:47:31 +08005123 case AUDIO_SOURCE_FM_TUNER:
5124 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
5125 device = AUDIO_DEVICE_IN_FM_TUNER;
5126 }
5127 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005128 default:
5129 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
5130 break;
5131 }
5132 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
5133 return device;
5134}
5135
Eric Laurente0720872014-03-11 09:30:41 -07005136bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005137{
5138 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
5139 device &= ~AUDIO_DEVICE_BIT_IN;
5140 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
5141 return true;
5142 }
5143 return false;
5144}
5145
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005146bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005147 return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL & ~AUDIO_DEVICE_BIT_IN) != 0);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005148}
5149
Eric Laurente0720872014-03-11 09:30:41 -07005150audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07005151{
5152 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005153 const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07005154 if ((input_descriptor->mRefCount > 0)
5155 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
5156 return mInputs.keyAt(i);
5157 }
5158 }
5159 return 0;
5160}
5161
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005162uint32_t AudioPolicyManager::activeInputsCount() const
5163{
5164 uint32_t count = 0;
5165 for (size_t i = 0; i < mInputs.size(); i++) {
5166 const sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
5167 if (desc->mRefCount > 0) {
Eric Laurenta34c9ce2014-12-19 11:16:32 -08005168 count++;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005169 }
5170 }
5171 return count;
5172}
5173
Eric Laurente552edb2014-03-10 17:42:56 -07005174
Eric Laurente0720872014-03-11 09:30:41 -07005175void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07005176{
5177 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005178 for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07005179 mStreams[i].mVolumeCurve[j] =
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005180 ApmGains::sVolumeProfiles[i][j];
Eric Laurente552edb2014-03-10 17:42:56 -07005181 }
5182 }
5183
5184 // Check availability of DRC on speaker path: if available, override some of the speaker curves
5185 if (mSpeakerDrcEnabled) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005186 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5187 ApmGains::sDefaultSystemVolumeCurveDrc;
5188 mStreams[AUDIO_STREAM_RING].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5189 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5190 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5191 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5192 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5193 ApmGains::sSpeakerSonificationVolumeCurveDrc;
5194 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5195 ApmGains::sSpeakerMediaVolumeCurveDrc;
5196 mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] =
5197 ApmGains::sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07005198 }
5199}
5200
Eric Laurente0720872014-03-11 09:30:41 -07005201float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005202 int index,
5203 audio_io_handle_t output,
5204 audio_devices_t device)
5205{
5206 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005207 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005208 StreamDescriptor &streamDesc = mStreams[stream];
5209
5210 if (device == AUDIO_DEVICE_NONE) {
5211 device = outputDesc->device();
5212 }
5213
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005214 volume = ApmGains::volIndexToAmpl(device, streamDesc, index);
Eric Laurente552edb2014-03-10 17:42:56 -07005215
5216 // if a headset is connected, apply the following rules to ring tones and notifications
5217 // to avoid sound level bursts in user's ears:
5218 // - always attenuate ring tones and notifications volume by 6dB
5219 // - if music is playing, always limit the volume to current music volume,
5220 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005221 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005222 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5223 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5224 AUDIO_DEVICE_OUT_WIRED_HEADSET |
5225 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
5226 ((stream_strategy == STRATEGY_SONIFICATION)
5227 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005228 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005229 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005230 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005231 streamDesc.mCanBeMuted) {
5232 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
5233 // when the phone is ringing we must consider that music could have been paused just before
5234 // by the music application and behave as if music was active if the last music track was
5235 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005236 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005237 mLimitRingtoneVolume) {
5238 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07005239 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
5240 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07005241 output,
5242 musicDevice);
5243 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
5244 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
5245 if (volume > minVol) {
5246 volume = minVol;
5247 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
5248 }
5249 }
5250 }
5251
5252 return volume;
5253}
5254
Eric Laurente0720872014-03-11 09:30:41 -07005255status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005256 int index,
5257 audio_io_handle_t output,
5258 audio_devices_t device,
5259 int delayMs,
5260 bool force)
5261{
5262
5263 // do not change actual stream volume if the stream is muted
5264 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
5265 ALOGVV("checkAndSetVolume() stream %d muted count %d",
5266 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
5267 return NO_ERROR;
5268 }
5269
5270 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07005271 if ((stream == AUDIO_STREAM_VOICE_CALL &&
5272 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
5273 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
5274 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005275 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07005276 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07005277 return INVALID_OPERATION;
5278 }
5279
5280 float volume = computeVolume(stream, index, output, device);
Eric Laurent275e8e92014-11-30 15:14:47 -08005281 // unit gain if rerouting to external policy
5282 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
5283 ssize_t index = mOutputs.indexOfKey(output);
5284 if (index >= 0) {
5285 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurentc722f302014-12-10 11:21:49 -08005286 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005287 ALOGV("max gain when rerouting for output=%d", output);
5288 volume = 1.0f;
5289 }
5290 }
5291
5292 }
Eric Laurente552edb2014-03-10 17:42:56 -07005293 // We actually change the volume if:
5294 // - the float value returned by computeVolume() changed
5295 // - the force flag is set
5296 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
5297 force) {
5298 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
5299 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
5300 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
5301 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07005302 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
5303 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005304 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005305 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005306 }
5307
Eric Laurent3b73df72014-03-11 09:06:29 -07005308 if (stream == AUDIO_STREAM_VOICE_CALL ||
5309 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005310 float voiceVolume;
5311 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005312 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005313 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
5314 } else {
5315 voiceVolume = 1.0;
5316 }
5317
5318 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
5319 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5320 mLastVoiceVolume = voiceVolume;
5321 }
5322 }
5323
5324 return NO_ERROR;
5325}
5326
Eric Laurente0720872014-03-11 09:30:41 -07005327void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07005328 audio_devices_t device,
5329 int delayMs,
5330 bool force)
5331{
5332 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
5333
Eric Laurent3b73df72014-03-11 09:06:29 -07005334 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005335 if (stream == AUDIO_STREAM_PATCH) {
5336 continue;
5337 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005338 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005339 mStreams[stream].getVolumeIndex(device),
5340 output,
5341 device,
5342 delayMs,
5343 force);
5344 }
5345}
5346
Eric Laurente0720872014-03-11 09:30:41 -07005347void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07005348 bool on,
5349 audio_io_handle_t output,
5350 int delayMs,
5351 audio_devices_t device)
5352{
5353 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07005354 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005355 if (stream == AUDIO_STREAM_PATCH) {
5356 continue;
5357 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005358 if (getStrategy((audio_stream_type_t)stream) == strategy) {
5359 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005360 }
5361 }
5362}
5363
Eric Laurente0720872014-03-11 09:30:41 -07005364void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005365 bool on,
5366 audio_io_handle_t output,
5367 int delayMs,
5368 audio_devices_t device)
5369{
5370 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07005371 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005372 if (device == AUDIO_DEVICE_NONE) {
5373 device = outputDesc->device();
5374 }
5375
5376 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
5377 stream, on, output, outputDesc->mMuteCount[stream], device);
5378
5379 if (on) {
5380 if (outputDesc->mMuteCount[stream] == 0) {
5381 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005382 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
5383 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07005384 checkAndSetVolume(stream, 0, output, device, delayMs);
5385 }
5386 }
5387 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5388 outputDesc->mMuteCount[stream]++;
5389 } else {
5390 if (outputDesc->mMuteCount[stream] == 0) {
5391 ALOGV("setStreamMute() unmuting non muted stream!");
5392 return;
5393 }
5394 if (--outputDesc->mMuteCount[stream] == 0) {
5395 checkAndSetVolume(stream,
5396 streamDesc.getVolumeIndex(device),
5397 output,
5398 device,
5399 delayMs);
5400 }
5401 }
5402}
5403
Eric Laurente0720872014-03-11 09:30:41 -07005404void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005405 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005406{
5407 // if the stream pertains to sonification strategy and we are in call we must
5408 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5409 // in the device used for phone strategy and play the tone if the selected device does not
5410 // interfere with the device used for phone strategy
5411 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5412 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005413 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005414 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5415 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005416 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07005417 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5418 stream, starting, outputDesc->mDevice, stateChange);
5419 if (outputDesc->mRefCount[stream]) {
5420 int muteCount = 1;
5421 if (stateChange) {
5422 muteCount = outputDesc->mRefCount[stream];
5423 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005424 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005425 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5426 for (int i = 0; i < muteCount; i++) {
5427 setStreamMute(stream, starting, mPrimaryOutput);
5428 }
5429 } else {
5430 ALOGV("handleIncallSonification() high visibility");
5431 if (outputDesc->device() &
5432 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5433 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5434 for (int i = 0; i < muteCount; i++) {
5435 setStreamMute(stream, starting, mPrimaryOutput);
5436 }
5437 }
5438 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005439 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5440 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005441 } else {
5442 mpClientInterface->stopTone();
5443 }
5444 }
5445 }
5446 }
5447}
5448
Eric Laurente0720872014-03-11 09:30:41 -07005449bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07005450{
5451 return isStateInCall(mPhoneState);
5452}
5453
Eric Laurente0720872014-03-11 09:30:41 -07005454bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005455 return ((state == AUDIO_MODE_IN_CALL) ||
5456 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07005457}
5458
Eric Laurente0720872014-03-11 09:30:41 -07005459uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07005460{
5461 return MAX_EFFECTS_CPU_LOAD;
5462}
5463
Eric Laurente0720872014-03-11 09:30:41 -07005464uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07005465{
5466 return MAX_EFFECTS_MEMORY;
5467}
5468
Eric Laurent6a94d692014-05-20 11:18:06 -07005469
Eric Laurente552edb2014-03-10 17:42:56 -07005470// --- EffectDescriptor class implementation
5471
Eric Laurente0720872014-03-11 09:30:41 -07005472status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005473{
5474 const size_t SIZE = 256;
5475 char buffer[SIZE];
5476 String8 result;
5477
5478 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
5479 result.append(buffer);
5480 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
5481 result.append(buffer);
5482 snprintf(buffer, SIZE, " Session: %d\n", mSession);
5483 result.append(buffer);
5484 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
5485 result.append(buffer);
5486 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
5487 result.append(buffer);
5488 write(fd, result.string(), result.size());
5489
5490 return NO_ERROR;
5491}
5492
Eric Laurent3a4311c2014-03-17 12:00:47 -07005493
5494// --- audio_policy.conf file parsing
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005495// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005496void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005497{
Eric Laurente552edb2014-03-10 17:42:56 -07005498 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005499 cnode *node;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005500 sp<HwModule> module = new HwModule(root->name);
Eric Laurente552edb2014-03-10 17:42:56 -07005501
Eric Laurent1afeecb2014-05-14 08:52:28 -07005502 node = config_find(root, DEVICES_TAG);
5503 if (node != NULL) {
5504 node = node->first_child;
5505 while (node) {
5506 ALOGV("loadHwModule() loading device %s", node->name);
5507 status_t tmpStatus = module->loadDevice(node);
5508 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5509 status = tmpStatus;
5510 }
5511 node = node->next;
5512 }
5513 }
5514 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07005515 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005516 node = node->first_child;
5517 while (node) {
5518 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005519 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005520 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5521 status = tmpStatus;
5522 }
5523 node = node->next;
5524 }
5525 }
5526 node = config_find(root, INPUTS_TAG);
5527 if (node != NULL) {
5528 node = node->first_child;
5529 while (node) {
5530 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005531 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005532 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5533 status = tmpStatus;
5534 }
5535 node = node->next;
5536 }
5537 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005538 loadGlobalConfig(root, module);
5539
Eric Laurente552edb2014-03-10 17:42:56 -07005540 if (status == NO_ERROR) {
5541 mHwModules.add(module);
Eric Laurente552edb2014-03-10 17:42:56 -07005542 }
5543}
5544
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005545// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005546void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005547{
5548 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
5549 if (node == NULL) {
5550 return;
5551 }
5552
5553 node = node->first_child;
5554 while (node) {
5555 ALOGV("loadHwModules() loading module %s", node->name);
5556 loadHwModule(node);
5557 node = node->next;
5558 }
5559}
5560
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005561// TODO candidate to be moved to ConfigParsingUtils
Eric Laurent1f2f2232014-06-02 12:01:23 -07005562void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module)
Eric Laurente552edb2014-03-10 17:42:56 -07005563{
5564 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
Eric Laurenteb108a42014-06-06 14:56:52 -07005565
Eric Laurente552edb2014-03-10 17:42:56 -07005566 if (node == NULL) {
5567 return;
5568 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005569 DeviceVector declaredDevices;
5570 if (module != NULL) {
5571 declaredDevices = module->mDeclaredDevices;
5572 }
5573
Eric Laurente552edb2014-03-10 17:42:56 -07005574 node = node->first_child;
5575 while (node) {
5576 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005577 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
5578 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005579 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
5580 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005581 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005582 audio_devices_t device = (audio_devices_t)ConfigParsingUtils::stringToEnum(
5583 sDeviceNameToEnumTable,
5584 ARRAY_SIZE(sDeviceNameToEnumTable),
5585 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005586 if (device != AUDIO_DEVICE_NONE) {
Paul McLeane743a472015-01-28 11:07:31 -08005587 mDefaultOutputDevice = new DeviceDescriptor(String8("default-output"), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005588 } else {
5589 ALOGW("loadGlobalConfig() default device not specified");
5590 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005591 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07005592 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005593 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
5594 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005595 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005596 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005597 mSpeakerDrcEnabled = ConfigParsingUtils::stringToBool((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07005598 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
Eric Laurenteb108a42014-06-06 14:56:52 -07005599 } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) {
5600 uint32_t major, minor;
5601 sscanf((char *)node->value, "%u.%u", &major, &minor);
5602 module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor);
5603 ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u",
5604 module->mHalVersion, major, minor);
Eric Laurente552edb2014-03-10 17:42:56 -07005605 }
5606 node = node->next;
5607 }
5608}
5609
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08005610// TODO candidate to be moved to ConfigParsingUtils
Eric Laurente0720872014-03-11 09:30:41 -07005611status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07005612{
5613 cnode *root;
5614 char *data;
5615
5616 data = (char *)load_file(path, NULL);
5617 if (data == NULL) {
5618 return -ENODEV;
5619 }
5620 root = config_node("", "");
5621 config_load(root, data);
5622
Eric Laurente552edb2014-03-10 17:42:56 -07005623 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005624 // legacy audio_policy.conf files have one global_configuration section
5625 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07005626 config_free(root);
5627 free(root);
5628 free(data);
5629
5630 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
5631
5632 return NO_ERROR;
5633}
5634
Eric Laurente0720872014-03-11 09:30:41 -07005635void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07005636{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005637 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07005638 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08005639 sp<DeviceDescriptor> defaultInputDevice =
5640 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005641 mAvailableOutputDevices.add(mDefaultOutputDevice);
5642 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005643
5644 module = new HwModule("primary");
5645
Eric Laurent1afeecb2014-05-14 08:52:28 -07005646 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005647 profile->mSamplingRates.add(44100);
5648 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5649 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005650 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005651 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
5652 module->mOutputProfiles.add(profile);
5653
Eric Laurent1afeecb2014-05-14 08:52:28 -07005654 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005655 profile->mSamplingRates.add(8000);
5656 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5657 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005658 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005659 module->mInputProfiles.add(profile);
5660
5661 mHwModules.add(module);
5662}
5663
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005664audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
5665{
5666 // flags to stream type mapping
5667 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
5668 return AUDIO_STREAM_ENFORCED_AUDIBLE;
5669 }
5670 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
5671 return AUDIO_STREAM_BLUETOOTH_SCO;
5672 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08005673 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
5674 return AUDIO_STREAM_TTS;
5675 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005676
5677 // usage to stream type mapping
5678 switch (attr->usage) {
5679 case AUDIO_USAGE_MEDIA:
5680 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005681 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5682 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005683 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08005684 if (isStreamActive(AUDIO_STREAM_ALARM)) {
5685 return AUDIO_STREAM_ALARM;
5686 }
5687 if (isStreamActive(AUDIO_STREAM_RING)) {
5688 return AUDIO_STREAM_RING;
5689 }
5690 if (isInCall()) {
5691 return AUDIO_STREAM_VOICE_CALL;
5692 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08005693 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07005694 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5695 return AUDIO_STREAM_SYSTEM;
5696 case AUDIO_USAGE_VOICE_COMMUNICATION:
5697 return AUDIO_STREAM_VOICE_CALL;
5698
5699 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5700 return AUDIO_STREAM_DTMF;
5701
5702 case AUDIO_USAGE_ALARM:
5703 return AUDIO_STREAM_ALARM;
5704 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5705 return AUDIO_STREAM_RING;
5706
5707 case AUDIO_USAGE_NOTIFICATION:
5708 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5709 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5710 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5711 case AUDIO_USAGE_NOTIFICATION_EVENT:
5712 return AUDIO_STREAM_NOTIFICATION;
5713
5714 case AUDIO_USAGE_UNKNOWN:
5715 default:
5716 return AUDIO_STREAM_MUSIC;
5717 }
5718}
Eric Laurente83b55d2014-11-14 10:06:21 -08005719
5720bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) {
5721 // has flags that map to a strategy?
5722 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
5723 return true;
5724 }
5725
5726 // has known usage?
5727 switch (paa->usage) {
5728 case AUDIO_USAGE_UNKNOWN:
5729 case AUDIO_USAGE_MEDIA:
5730 case AUDIO_USAGE_VOICE_COMMUNICATION:
5731 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
5732 case AUDIO_USAGE_ALARM:
5733 case AUDIO_USAGE_NOTIFICATION:
5734 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
5735 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
5736 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
5737 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
5738 case AUDIO_USAGE_NOTIFICATION_EVENT:
5739 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
5740 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
5741 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
5742 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08005743 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08005744 break;
5745 default:
5746 return false;
5747 }
5748 return true;
5749}
5750
François Gaffiead3183e2015-03-18 16:55:35 +01005751
5752bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
5753 routing_strategy strategy, uint32_t inPastMs,
5754 nsecs_t sysTime) const
5755{
5756 if ((sysTime == 0) && (inPastMs != 0)) {
5757 sysTime = systemTime();
5758 }
5759 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5760 if (i == AUDIO_STREAM_PATCH) {
5761 continue;
5762 }
5763 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
5764 (NUM_STRATEGIES == strategy)) &&
5765 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
5766 return true;
5767 }
5768 }
5769 return false;
5770}
5771
Eric Laurente552edb2014-03-10 17:42:56 -07005772}; // namespace android