blob: a5d4a0e4f174a4a2e07d05e616a2865a6b3f5792 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
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 Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> 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 Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 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 Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700216 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Paul McLean9080a4c2015-06-18 08:24:02 -0700250 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
Eric Laurentc0a889f2015-10-14 14:36:34 -0700405 // terminate active capture if on the same HW module as the call TX source device
406 // FIXME: would be better to refine to only inputs whose profile connects to the
407 // call TX device but this information is not in the audio patch and logic here must be
408 // symmetric to the one in startInput()
409 audio_io_handle_t activeInput = mInputs.getActiveInput();
410 if (activeInput != 0) {
411 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
412 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
413 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
414 stopInput(activeInput, activeSession);
415 releaseInput(activeInput, activeSession);
416 }
417 }
418
Eric Laurentc2730ba2014-07-20 15:47:07 -0700419 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
420 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
421 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
422 status);
423 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100424 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700425 mCallTxPatch->mAfPatchHandle = afPatchHandle;
426 mCallTxPatch->mUid = mUidCached;
427 }
428 }
429}
430
Eric Laurente0720872014-03-11 09:30:41 -0700431void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700432{
433 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100434 // store previous phone state for management of sonification strategy below
435 int oldState = mEngine->getPhoneState();
436
437 if (mEngine->setPhoneState(state) != NO_ERROR) {
438 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700439 return;
440 }
François Gaffie2110e042015-03-24 08:41:51 +0100441 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700442 // if leaving call state, handle special case of active streams
443 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700444 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700445 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700446 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800447 if (stream == AUDIO_STREAM_PATCH) {
448 continue;
449 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700450 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700451 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800452
Eric Laurent63dea1d2015-07-02 17:10:28 -0700453 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800454 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700455 }
456
François Gaffie2110e042015-03-24 08:41:51 +0100457 /**
458 * Switching to or from incall state or switching between telephony and VoIP lead to force
459 * routing command.
460 */
461 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
462 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700463
464 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700465 checkA2dpSuspend();
466 checkOutputForAllStrategies();
467 updateDevicesAndOutputs();
468
Eric Laurente552edb2014-03-10 17:42:56 -0700469 int delayMs = 0;
470 if (isStateInCall(state)) {
471 nsecs_t sysTime = systemTime();
472 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700473 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700474 // mute media and sonification strategies and delay device switch by the largest
475 // latency of any output where either strategy is active.
476 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100477 if ((isStrategyActive(desc, STRATEGY_MEDIA,
478 SONIFICATION_HEADSET_MUSIC_DELAY,
479 sysTime) ||
480 isStrategyActive(desc, STRATEGY_SONIFICATION,
481 SONIFICATION_HEADSET_MUSIC_DELAY,
482 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700483 (delayMs < (int)desc->latency()*2)) {
484 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700485 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700486 setStrategyMute(STRATEGY_MEDIA, true, desc);
487 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700488 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700489 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
490 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700491 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
492 }
493 }
494
Eric Laurent87ffa392015-05-22 10:32:38 -0700495 if (hasPrimaryOutput()) {
496 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
497 // the device returned is not necessarily reachable via this output
498 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
499 // force routing command to audio hardware when ending call
500 // even if no device change is needed
501 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
502 rxDevice = mPrimaryOutput->device();
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504
Eric Laurent87ffa392015-05-22 10:32:38 -0700505 if (state == AUDIO_MODE_IN_CALL) {
506 updateCallRouting(rxDevice, delayMs);
507 } else if (oldState == AUDIO_MODE_IN_CALL) {
508 if (mCallRxPatch != 0) {
509 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
510 mCallRxPatch.clear();
511 }
512 if (mCallTxPatch != 0) {
513 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
514 mCallTxPatch.clear();
515 }
516 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
517 } else {
518 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700519 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700520 }
Eric Laurente552edb2014-03-10 17:42:56 -0700521 // if entering in call state, handle special case of active streams
522 // pertaining to sonification strategy see handleIncallSonification()
523 if (isStateInCall(state)) {
524 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700525 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800526 if (stream == AUDIO_STREAM_PATCH) {
527 continue;
528 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700529 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700530 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700531
532 // force reevaluating accessibility routing when call starts
533 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700534 }
535
536 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700537 if (state == AUDIO_MODE_RINGTONE &&
538 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700539 mLimitRingtoneVolume = true;
540 } else {
541 mLimitRingtoneVolume = false;
542 }
543}
544
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700545audio_mode_t AudioPolicyManager::getPhoneState() {
546 return mEngine->getPhoneState();
547}
548
Eric Laurente0720872014-03-11 09:30:41 -0700549void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700550 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700551{
François Gaffie2110e042015-03-24 08:41:51 +0100552 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700553
François Gaffie2110e042015-03-24 08:41:51 +0100554 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
555 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
556 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700557 }
François Gaffie2110e042015-03-24 08:41:51 +0100558 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
559 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
560 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700561
562 // check for device and output changes triggered by new force usage
563 checkA2dpSuspend();
564 checkOutputForAllStrategies();
565 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700566 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700567 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
568 updateCallRouting(newDevice);
569 }
Eric Laurente552edb2014-03-10 17:42:56 -0700570 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700571 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
572 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
573 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
574 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700575 }
Eric Laurente552edb2014-03-10 17:42:56 -0700576 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700577 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700578 }
579 }
580
François Gaffie53615e22015-03-19 09:24:12 +0100581 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700582 if (activeInput != 0) {
Eric Laurentc171c7c2015-09-25 12:21:06 -0700583 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
584 audio_devices_t newDevice = getNewInputDevice(activeInput);
585 // Force new input selection if the new device can not be reached via current input
586 if (activeDesc->mProfile->mSupportedDevices.types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
587 setInputDevice(activeInput, newDevice);
588 } else {
589 closeInput(activeInput);
590 }
Eric Laurente552edb2014-03-10 17:42:56 -0700591 }
Eric Laurente552edb2014-03-10 17:42:56 -0700592}
593
Eric Laurente0720872014-03-11 09:30:41 -0700594void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700595{
596 ALOGV("setSystemProperty() property %s, value %s", property, value);
597}
598
599// Find a direct output profile compatible with the parameters passed, even if the input flags do
600// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800601sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700602 audio_devices_t device,
603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags)
607{
Eric Laurent861a6282015-05-18 15:40:16 -0700608 // only retain flags that will drive the direct output profile selection
609 // if explicitly requested
610 static const uint32_t kRelevantFlags =
611 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
612 flags =
613 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
614
615 sp<IOProfile> profile;
616
Eric Laurente552edb2014-03-10 17:42:56 -0700617 for (size_t i = 0; i < mHwModules.size(); i++) {
618 if (mHwModules[i]->mHandle == 0) {
619 continue;
620 }
621 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700622 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
623 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700624 samplingRate, NULL /*updatedSamplingRate*/,
625 format, NULL /*updatedFormat*/,
626 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700627 flags)) {
628 continue;
629 }
630 // reject profiles not corresponding to a device currently available
631 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
632 continue;
633 }
634 // if several profiles are compatible, give priority to one with offload capability
635 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
636 continue;
637 }
638 profile = curProfile;
639 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
640 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700641 }
Eric Laurente552edb2014-03-10 17:42:56 -0700642 }
643 }
Eric Laurent861a6282015-05-18 15:40:16 -0700644 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700645}
646
Eric Laurente0720872014-03-11 09:30:41 -0700647audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100648 uint32_t samplingRate,
649 audio_format_t format,
650 audio_channel_mask_t channelMask,
651 audio_output_flags_t flags,
652 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700653{
Eric Laurent3b73df72014-03-11 09:06:29 -0700654 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700655 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
656 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
657 device, stream, samplingRate, format, channelMask, flags);
658
Eric Laurente83b55d2014-11-14 10:06:21 -0800659 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
660 stream, samplingRate,format, channelMask,
661 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700662}
663
Eric Laurente83b55d2014-11-14 10:06:21 -0800664status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
665 audio_io_handle_t *output,
666 audio_session_t session,
667 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700668 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800669 uint32_t samplingRate,
670 audio_format_t format,
671 audio_channel_mask_t channelMask,
672 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700673 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800674 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700675{
Eric Laurente83b55d2014-11-14 10:06:21 -0800676 audio_attributes_t attributes;
677 if (attr != NULL) {
678 if (!isValidAttributes(attr)) {
679 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
680 attr->usage, attr->content_type, attr->flags,
681 attr->tags);
682 return BAD_VALUE;
683 }
684 attributes = *attr;
685 } else {
686 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
687 ALOGE("getOutputForAttr(): invalid stream type");
688 return BAD_VALUE;
689 }
690 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700691 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700692 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100693 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
694 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
695 if (!audio_is_linear_pcm(format)) {
696 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800697 }
François Gaffie036e1e92015-03-19 10:16:24 +0100698 *stream = streamTypefromAttributesInt(&attributes);
699 *output = desc->mIoHandle;
700 ALOGV("getOutputForAttr() returns output %d", *output);
701 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800702 }
703 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
704 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
705 return BAD_VALUE;
706 }
707
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700708 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
709 " session %d selectedDeviceId %d",
710 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
711 session, selectedDeviceId);
712
713 *stream = streamTypefromAttributesInt(&attributes);
714
715 // Explicit routing?
716 sp<DeviceDescriptor> deviceDesc;
717 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
718 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
719 deviceDesc = mAvailableOutputDevices[i];
720 break;
721 }
722 }
723 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700724
Eric Laurente83b55d2014-11-14 10:06:21 -0800725 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700727
Eric Laurente83b55d2014-11-14 10:06:21 -0800728 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700729 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
730 }
731
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700732 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700733 device, samplingRate, format, channelMask, flags);
734
Eric Laurente83b55d2014-11-14 10:06:21 -0800735 *output = getOutputForDevice(device, session, *stream,
736 samplingRate, format, channelMask,
737 flags, offloadInfo);
738 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700739 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800740 return INVALID_OPERATION;
741 }
Paul McLeanaa981192015-03-21 09:55:15 -0700742
Eric Laurente83b55d2014-11-14 10:06:21 -0800743 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700744}
745
746audio_io_handle_t AudioPolicyManager::getOutputForDevice(
747 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800748 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700749 audio_stream_type_t stream,
750 uint32_t samplingRate,
751 audio_format_t format,
752 audio_channel_mask_t channelMask,
753 audio_output_flags_t flags,
754 const audio_offload_info_t *offloadInfo)
755{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700756 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700757 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700758 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700759
Eric Laurente552edb2014-03-10 17:42:56 -0700760#ifdef AUDIO_POLICY_TEST
761 if (mCurOutput != 0) {
762 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
763 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
764
765 if (mTestOutputs[mCurOutput] == 0) {
766 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700767 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
768 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700769 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700770 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700771 outputDesc->mFlags =
772 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700773 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700774 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
775 config.sample_rate = mTestSamplingRate;
776 config.channel_mask = mTestChannels;
777 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700778 if (offloadInfo != NULL) {
779 config.offload_info = *offloadInfo;
780 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700781 status = mpClientInterface->openOutput(0,
782 &mTestOutputs[mCurOutput],
783 &config,
784 &outputDesc->mDevice,
785 String8(""),
786 &outputDesc->mLatency,
787 outputDesc->mFlags);
788 if (status == NO_ERROR) {
789 outputDesc->mSamplingRate = config.sample_rate;
790 outputDesc->mFormat = config.format;
791 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700792 AudioParameter outputCmd = AudioParameter();
793 outputCmd.addInt(String8("set_id"),mCurOutput);
794 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
795 addOutput(mTestOutputs[mCurOutput], outputDesc);
796 }
797 }
798 return mTestOutputs[mCurOutput];
799 }
800#endif //AUDIO_POLICY_TEST
801
802 // open a direct output if required by specified parameters
803 //force direct flag if offload flag is set: offloading implies a direct output stream
804 // and all common behaviors are driven by checking only the direct flag
805 // this should normally be set appropriately in the policy configuration file
806 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700807 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700808 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700809 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
810 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
811 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800812 // only allow deep buffering for music stream type
813 if (stream != AUDIO_STREAM_MUSIC) {
814 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700815 } else if (/* stream == AUDIO_STREAM_MUSIC && */
816 flags == AUDIO_OUTPUT_FLAG_NONE &&
817 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
818 // use DEEP_BUFFER as default output for music stream type
819 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800820 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700821 if (stream == AUDIO_STREAM_TTS) {
822 flags = AUDIO_OUTPUT_FLAG_TTS;
823 }
Eric Laurente552edb2014-03-10 17:42:56 -0700824
Eric Laurentb732cf52014-09-24 19:08:21 -0700825 sp<IOProfile> profile;
826
827 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700828 // and not explicitly requested
829 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
830 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700831 audio_channel_count_from_out_mask(channelMask) <= 2) {
832 goto non_direct_output;
833 }
834
Eric Laurente552edb2014-03-10 17:42:56 -0700835 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
836 // creating an offloaded track and tearing it down immediately after start when audioflinger
837 // detects there is an active non offloadable effect.
838 // FIXME: We should check the audio session here but we do not have it in this context.
839 // This may prevent offloading in rare situations where effects are left active by apps
840 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700841
Eric Laurente552edb2014-03-10 17:42:56 -0700842 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100843 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700844 profile = getProfileForDirectOutput(device,
845 samplingRate,
846 format,
847 channelMask,
848 (audio_output_flags_t)flags);
849 }
850
Eric Laurent1c333e22014-05-20 10:48:17 -0700851 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700852 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700853
854 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700855 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700856 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
857 outputDesc = desc;
858 // reuse direct output if currently open and configured with same parameters
859 if ((samplingRate == outputDesc->mSamplingRate) &&
860 (format == outputDesc->mFormat) &&
861 (channelMask == outputDesc->mChannelMask)) {
862 outputDesc->mDirectOpenCount++;
863 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
864 return mOutputs.keyAt(i);
865 }
866 }
867 }
868 // close direct output if currently open and configured with different parameters
869 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700870 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700871 }
Eric Laurent861a6282015-05-18 15:40:16 -0700872
873 // if the selected profile is offloaded and no offload info was specified,
874 // create a default one
875 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
876 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
877 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
878 defaultOffloadInfo.sample_rate = samplingRate;
879 defaultOffloadInfo.channel_mask = channelMask;
880 defaultOffloadInfo.format = format;
881 defaultOffloadInfo.stream_type = stream;
882 defaultOffloadInfo.bit_rate = 0;
883 defaultOffloadInfo.duration_us = -1;
884 defaultOffloadInfo.has_video = true; // conservative
885 defaultOffloadInfo.is_streaming = true; // likely
886 offloadInfo = &defaultOffloadInfo;
887 }
888
Eric Laurentc75307b2015-03-17 15:29:32 -0700889 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700890 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700891 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700892 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700893 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
894 config.sample_rate = samplingRate;
895 config.channel_mask = channelMask;
896 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700897 if (offloadInfo != NULL) {
898 config.offload_info = *offloadInfo;
899 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700900 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700901 &output,
902 &config,
903 &outputDesc->mDevice,
904 String8(""),
905 &outputDesc->mLatency,
906 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700907
908 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700909 if (status != NO_ERROR ||
910 (samplingRate != 0 && samplingRate != config.sample_rate) ||
911 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
912 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700913 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
914 "format %d %d, channelMask %04x %04x", output, samplingRate,
915 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
916 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700917 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700918 mpClientInterface->closeOutput(output);
919 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800920 // fall back to mixer output if possible when the direct output could not be open
921 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
922 goto non_direct_output;
923 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700924 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700925 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700926 outputDesc->mSamplingRate = config.sample_rate;
927 outputDesc->mChannelMask = config.channel_mask;
928 outputDesc->mFormat = config.format;
929 outputDesc->mRefCount[stream] = 0;
930 outputDesc->mStopTime[stream] = 0;
931 outputDesc->mDirectOpenCount = 1;
932
Eric Laurente552edb2014-03-10 17:42:56 -0700933 audio_io_handle_t srcOutput = getOutputForEffect();
934 addOutput(output, outputDesc);
935 audio_io_handle_t dstOutput = getOutputForEffect();
936 if (dstOutput == output) {
937 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
938 }
939 mPreviousOutputs = mOutputs;
940 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700941 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700942 return output;
943 }
944
Eric Laurentb732cf52014-09-24 19:08:21 -0700945non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700946 // ignoring channel mask due to downmix capability in mixer
947
948 // open a non direct output
949
950 // for non direct outputs, only PCM is supported
951 if (audio_is_linear_pcm(format)) {
952 // get which output is suitable for the specified stream. The actual
953 // routing change will happen when startOutput() will be called
954 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
955
Eric Laurent8838a382014-09-08 16:44:28 -0700956 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
957 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
958 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700959 }
960 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
961 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
962
Paul McLeanaa981192015-03-21 09:55:15 -0700963 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700964
965 return output;
966}
967
Eric Laurente0720872014-03-11 09:30:41 -0700968audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700969 audio_output_flags_t flags,
970 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700971{
972 // select one output among several that provide a path to a particular device or set of
973 // devices (the list was previously build by getOutputsForDevice()).
974 // The priority is as follows:
975 // 1: the output with the highest number of requested policy flags
976 // 2: the primary output
977 // 3: the first output in the list
978
979 if (outputs.size() == 0) {
980 return 0;
981 }
982 if (outputs.size() == 1) {
983 return outputs[0];
984 }
985
986 int maxCommonFlags = 0;
987 audio_io_handle_t outputFlags = 0;
988 audio_io_handle_t outputPrimary = 0;
989
990 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700991 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700992 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700993 // if a valid format is specified, skip output if not compatible
994 if (format != AUDIO_FORMAT_INVALID) {
995 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
996 if (format != outputDesc->mFormat) {
997 continue;
998 }
999 } else if (!audio_is_linear_pcm(format)) {
1000 continue;
1001 }
1002 }
1003
Eric Laurent3b73df72014-03-11 09:06:29 -07001004 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001005 if (commonFlags > maxCommonFlags) {
1006 outputFlags = outputs[i];
1007 maxCommonFlags = commonFlags;
1008 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1009 }
1010 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1011 outputPrimary = outputs[i];
1012 }
1013 }
1014 }
1015
1016 if (outputFlags != 0) {
1017 return outputFlags;
1018 }
1019 if (outputPrimary != 0) {
1020 return outputPrimary;
1021 }
1022
1023 return outputs[0];
1024}
1025
Eric Laurente0720872014-03-11 09:30:41 -07001026status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001027 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001028 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001029{
Paul McLeanaa981192015-03-21 09:55:15 -07001030 ALOGV("startOutput() output %d, stream %d, session %d",
1031 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001032 ssize_t index = mOutputs.indexOfKey(output);
1033 if (index < 0) {
1034 ALOGW("startOutput() unknown output %d", output);
1035 return BAD_VALUE;
1036 }
1037
Eric Laurentc75307b2015-03-17 15:29:32 -07001038 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1039
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001040 // Routing?
1041 mOutputRoutes.incRouteActivity(session);
1042
Eric Laurentc75307b2015-03-17 15:29:32 -07001043 audio_devices_t newDevice;
1044 if (outputDesc->mPolicyMix != NULL) {
1045 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001046 } else if (mOutputRoutes.hasRouteChanged(session)) {
1047 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001048 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001049 } else {
1050 newDevice = AUDIO_DEVICE_NONE;
1051 }
1052
1053 uint32_t delayMs = 0;
1054
Eric Laurentc75307b2015-03-17 15:29:32 -07001055 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1056
1057 if (status != NO_ERROR) {
1058 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001059 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001060 }
1061 // Automatically enable the remote submix input when output is started on a re routing mix
1062 // of type MIX_TYPE_RECORDERS
1063 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1064 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1065 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1066 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1067 outputDesc->mPolicyMix->mRegistrationId,
1068 "remote-submix");
1069 }
1070
1071 if (delayMs != 0) {
1072 usleep(delayMs * 1000);
1073 }
1074
1075 return status;
1076}
1077
1078status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1079 audio_stream_type_t stream,
1080 audio_devices_t device,
1081 uint32_t *delayMs)
1082{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001083 // cannot start playback of STREAM_TTS if any other output is being used
1084 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001085
1086 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001087 if (stream == AUDIO_STREAM_TTS) {
1088 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001089 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001090 return INVALID_OPERATION;
1091 } else {
1092 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1093 }
1094 } else {
1095 // some playback other than beacon starts
1096 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1097 }
1098
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001099 // check active before incrementing usage count
1100 bool force = !outputDesc->isActive();
1101
Eric Laurente552edb2014-03-10 17:42:56 -07001102 // increment usage count for this stream on the requested output:
1103 // NOTE that the usage count is the same for duplicated output and hardware output which is
1104 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1105 outputDesc->changeRefCount(stream, 1);
1106
Eric Laurent493404d2015-04-21 15:07:36 -07001107 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001108 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001109 if (device == AUDIO_DEVICE_NONE) {
1110 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001111 }
Eric Laurente552edb2014-03-10 17:42:56 -07001112 routing_strategy strategy = getStrategy(stream);
1113 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001114 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1115 (beaconMuteLatency > 0);
1116 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001117 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001118 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001119 if (desc != outputDesc) {
1120 // force a device change if any other output is managed by the same hw
1121 // module and has a current device selection that differs from selected device.
1122 // In this case, the audio HAL must receive the new device selection so that it can
1123 // change the device currently selected by the other active output.
1124 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001125 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001126 force = true;
1127 }
1128 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001129 // a notification so that audio focus effect can propagate, or that a mute/unmute
1130 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001131 uint32_t latency = desc->latency();
1132 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1133 waitMs = latency;
1134 }
1135 }
1136 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001137 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001138
1139 // handle special case for sonification while in call
1140 if (isInCall()) {
1141 handleIncallSonification(stream, true, false);
1142 }
1143
1144 // apply volume rules for current stream and device if necessary
1145 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001146 mStreams.valueFor(stream).getVolumeIndex(device),
1147 outputDesc,
1148 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001149
1150 // update the outputs if starting an output with a stream that can affect notification
1151 // routing
1152 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001153
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001154 // force reevaluating accessibility routing when ringtone or alarm starts
1155 if (strategy == STRATEGY_SONIFICATION) {
1156 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1157 }
Eric Laurente552edb2014-03-10 17:42:56 -07001158 }
1159 return NO_ERROR;
1160}
1161
1162
Eric Laurente0720872014-03-11 09:30:41 -07001163status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001164 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001165 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001166{
1167 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1168 ssize_t index = mOutputs.indexOfKey(output);
1169 if (index < 0) {
1170 ALOGW("stopOutput() unknown output %d", output);
1171 return BAD_VALUE;
1172 }
1173
Eric Laurentc75307b2015-03-17 15:29:32 -07001174 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001175
Eric Laurentc75307b2015-03-17 15:29:32 -07001176 if (outputDesc->mRefCount[stream] == 1) {
1177 // Automatically disable the remote submix input when output is stopped on a
1178 // re routing mix of type MIX_TYPE_RECORDERS
1179 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1180 outputDesc->mPolicyMix != NULL &&
1181 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1182 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1183 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1184 outputDesc->mPolicyMix->mRegistrationId,
1185 "remote-submix");
1186 }
1187 }
1188
1189 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001190 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001191 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001192 int activityCount = mOutputRoutes.decRouteActivity(session);
1193 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1194
1195 if (forceDeviceUpdate) {
1196 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1197 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 }
1199
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001200 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001201}
1202
1203status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001204 audio_stream_type_t stream,
1205 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001206{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001207 // always handle stream stop, check which stream type is stopping
1208 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1209
Eric Laurente552edb2014-03-10 17:42:56 -07001210 // handle special case for sonification while in call
1211 if (isInCall()) {
1212 handleIncallSonification(stream, false, false);
1213 }
1214
1215 if (outputDesc->mRefCount[stream] > 0) {
1216 // decrement usage count of this stream on the output
1217 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001218
Eric Laurente552edb2014-03-10 17:42:56 -07001219 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001220 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001221 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001222 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001223 // delay the device switch by twice the latency because stopOutput() is executed when
1224 // the track stop() command is received and at that time the audio track buffer can
1225 // still contain data that needs to be drained. The latency only covers the audio HAL
1226 // and kernel buffers. Also the latency does not always include additional delay in the
1227 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001228 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001229
1230 // force restoring the device selection on other active outputs if it differs from the
1231 // one being selected for this output
1232 for (size_t i = 0; i < mOutputs.size(); i++) {
1233 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001234 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001235 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001236 desc->isActive() &&
1237 outputDesc->sharesHwModuleWith(desc) &&
1238 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001239 setOutputDevice(desc,
1240 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001241 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001242 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001243 }
1244 }
1245 // update the outputs if stopping one with a stream that can affect notification routing
1246 handleNotificationRoutingForStream(stream);
1247 }
1248 return NO_ERROR;
1249 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001250 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001251 return INVALID_OPERATION;
1252 }
1253}
1254
Eric Laurente83b55d2014-11-14 10:06:21 -08001255void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001256 audio_stream_type_t stream __unused,
1257 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001258{
1259 ALOGV("releaseOutput() %d", output);
1260 ssize_t index = mOutputs.indexOfKey(output);
1261 if (index < 0) {
1262 ALOGW("releaseOutput() releasing unknown output %d", output);
1263 return;
1264 }
1265
1266#ifdef AUDIO_POLICY_TEST
1267 int testIndex = testOutputIndex(output);
1268 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001269 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001270 if (outputDesc->isActive()) {
1271 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001272 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001273 mTestOutputs[testIndex] = 0;
1274 }
1275 return;
1276 }
1277#endif //AUDIO_POLICY_TEST
1278
Paul McLeanaa981192015-03-21 09:55:15 -07001279 // Routing
1280 mOutputRoutes.removeRoute(session);
1281
Eric Laurentc75307b2015-03-17 15:29:32 -07001282 sp<SwAudioOutputDescriptor> 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();
Eric Laurent87ffa392015-05-22 10:32:38 -07001294 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001295 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1296 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001297 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001298 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001299 }
1300 }
1301}
1302
1303
Eric Laurentcaf7f482014-11-25 17:50:47 -08001304status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1305 audio_io_handle_t *input,
1306 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001307 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001308 uint32_t samplingRate,
1309 audio_format_t format,
1310 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001311 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001312 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001313 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001314{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001315 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1316 "session %d, flags %#x",
1317 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001318
Eric Laurentcaf7f482014-11-25 17:50:47 -08001319 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001320 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001321 audio_devices_t device;
1322 // handle legacy remote submix case where the address was not always specified
1323 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001324 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001325 audio_source_t inputSource = attr->source;
1326 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001327 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001328
Eric Laurentc447ded2015-01-06 08:47:05 -08001329 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1330 inputSource = AUDIO_SOURCE_MIC;
1331 }
1332 halInputSource = inputSource;
1333
Paul McLean466dc8e2015-04-17 13:15:36 -06001334 // Explicit routing?
1335 sp<DeviceDescriptor> deviceDesc;
1336 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1337 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1338 deviceDesc = mAvailableInputDevices[i];
1339 break;
1340 }
1341 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001342 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001343
Eric Laurentc447ded2015-01-06 08:47:05 -08001344 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001345 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001346 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001347 if (ret != NO_ERROR) {
1348 return ret;
1349 }
1350 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001351 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1352 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001353 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001354 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001355 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001356 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001357 return BAD_VALUE;
1358 }
Eric Laurentc722f302014-12-10 11:21:49 -08001359 if (policyMix != NULL) {
1360 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001361 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1362 // there is an external policy, but this input is attached to a mix of recorders,
1363 // meaning it receives audio injected into the framework, so the recorder doesn't
1364 // know about it and is therefore considered "legacy"
1365 *inputType = API_INPUT_LEGACY;
1366 } else {
1367 // recording a mix of players defined by an external policy, we're rerouting for
1368 // an external policy
1369 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1370 }
Eric Laurentc722f302014-12-10 11:21:49 -08001371 } else if (audio_is_remote_submix_device(device)) {
1372 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001373 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001374 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1375 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001376 } else {
1377 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001378 }
Ravi Kumar Alamandab367f5b2015-08-25 08:21:37 -07001379
Eric Laurentc447ded2015-01-06 08:47:05 -08001380 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001381 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1382 if (index >= 0) {
1383 *input = mSoundTriggerSessions.valueFor(session);
1384 isSoundTrigger = true;
1385 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1386 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1387 } else {
1388 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1389 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001390 }
1391 }
1392
Andy Hungf129b032015-04-07 13:45:50 -07001393 // find a compatible input profile (not necessarily identical in parameters)
1394 sp<IOProfile> profile;
1395 // samplingRate and flags may be updated by getInputProfile
1396 uint32_t profileSamplingRate = samplingRate;
1397 audio_format_t profileFormat = format;
1398 audio_channel_mask_t profileChannelMask = channelMask;
1399 audio_input_flags_t profileFlags = flags;
1400 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001401 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001402 profileSamplingRate, profileFormat, profileChannelMask,
1403 profileFlags);
1404 if (profile != 0) {
1405 break; // success
1406 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1407 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1408 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001409 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1410 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001411 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001412 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001413 }
Eric Laurente552edb2014-03-10 17:42:56 -07001414 }
1415
Eric Laurent322b4d22015-04-03 15:57:54 -07001416 if (profile->getModuleHandle() == 0) {
1417 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001418 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001419 }
1420
1421 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001422 config.sample_rate = profileSamplingRate;
1423 config.channel_mask = profileChannelMask;
1424 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001425
Eric Laurent322b4d22015-04-03 15:57:54 -07001426 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001427 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001428 &config,
1429 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001430 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001431 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001432 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001433
1434 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001435 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001436 (profileSamplingRate != config.sample_rate) ||
1437 (profileFormat != config.format) ||
1438 (profileChannelMask != config.channel_mask)) {
1439 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1440 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001441 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001442 if (*input != AUDIO_IO_HANDLE_NONE) {
1443 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001444 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001445 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001446 }
1447
Eric Laurent1f2f2232014-06-02 12:01:23 -07001448 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001449 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001450 inputDesc->mRefCount = 0;
1451 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001452 inputDesc->mSamplingRate = profileSamplingRate;
1453 inputDesc->mFormat = profileFormat;
1454 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001455 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001456 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001457 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001458 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001459
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001460 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001461
Eric Laurentcaf7f482014-11-25 17:50:47 -08001462 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001463 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001464
Eric Laurentcaf7f482014-11-25 17:50:47 -08001465 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001466}
1467
Eric Laurent4dc68062014-07-28 17:26:49 -07001468status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1469 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001470{
1471 ALOGV("startInput() input %d", input);
1472 ssize_t index = mInputs.indexOfKey(input);
1473 if (index < 0) {
1474 ALOGW("startInput() unknown input %d", input);
1475 return BAD_VALUE;
1476 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001477 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001478
Eric Laurentc722f302014-12-10 11:21:49 -08001479 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001480 if (index < 0) {
1481 ALOGW("startInput() unknown session %d on input %d", session, input);
1482 return BAD_VALUE;
1483 }
1484
Glenn Kasten74a8e252014-07-24 14:09:55 -07001485 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001486 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001487
1488 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001489 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001490 if (activeInput != 0 && activeInput != input) {
1491
1492 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1493 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001494 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001495 if ((activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) &&
1496 !activeDesc->hasPreemptedSession(session)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001497 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001498 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
1499 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1500 sessions.add(activeSession);
1501 inputDesc->setPreemptedSessions(sessions);
1502 stopInput(activeInput, activeSession);
1503 releaseInput(activeInput, activeSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001504 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001505 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001506 return INVALID_OPERATION;
1507 }
1508 }
Eric Laurentc0a889f2015-10-14 14:36:34 -07001509
1510 // Do not allow capture if an active voice call is using a software patch and
1511 // the call TX source device is on the same HW module.
1512 // FIXME: would be better to refine to only inputs whose profile connects to the
1513 // call TX device but this information is not in the audio patch
1514 if (mCallTxPatch != 0 &&
1515 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1516 return INVALID_OPERATION;
1517 }
Eric Laurente552edb2014-03-10 17:42:56 -07001518 }
1519
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001520 // Routing?
1521 mInputRoutes.incRouteActivity(session);
1522
Paul McLean466dc8e2015-04-17 13:15:36 -06001523 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001524 // if input maps to a dynamic policy with an activity listener, notify of state change
1525 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001526 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001527 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1528 MIX_STATE_MIXING);
1529 }
1530
François Gaffie53615e22015-03-19 09:24:12 +01001531 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001532 SoundTrigger::setCaptureState(true);
1533 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001534 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001535
Eric Laurentc722f302014-12-10 11:21:49 -08001536 // automatically enable the remote submix output when input is started if not
1537 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001538 // For remote submix (a virtual device), we open only one input per capture request.
1539 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001540 String8 address = String8("");
1541 if (inputDesc->mPolicyMix == NULL) {
1542 address = String8("0");
1543 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1544 address = inputDesc->mPolicyMix->mRegistrationId;
1545 }
1546 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001547 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001548 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001549 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001550 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001551 }
Eric Laurente552edb2014-03-10 17:42:56 -07001552 }
1553
Eric Laurente552edb2014-03-10 17:42:56 -07001554 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1555
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001556 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001557 return NO_ERROR;
1558}
1559
Eric Laurent4dc68062014-07-28 17:26:49 -07001560status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1561 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001562{
1563 ALOGV("stopInput() input %d", input);
1564 ssize_t index = mInputs.indexOfKey(input);
1565 if (index < 0) {
1566 ALOGW("stopInput() unknown input %d", input);
1567 return BAD_VALUE;
1568 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001569 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001570
Eric Laurentc722f302014-12-10 11:21:49 -08001571 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001572 if (index < 0) {
1573 ALOGW("stopInput() unknown session %d on input %d", session, input);
1574 return BAD_VALUE;
1575 }
1576
Eric Laurente552edb2014-03-10 17:42:56 -07001577 if (inputDesc->mRefCount == 0) {
1578 ALOGW("stopInput() input %d already stopped", input);
1579 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001580 }
1581
1582 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001583
1584 // Routing?
1585 mInputRoutes.decRouteActivity(session);
1586
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001587 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001588 // if input maps to a dynamic policy with an activity listener, notify of state change
1589 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001590 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001591 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1592 MIX_STATE_IDLE);
1593 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001594
Eric Laurentc722f302014-12-10 11:21:49 -08001595 // automatically disable the remote submix output when input is stopped if not
1596 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001597 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001598 String8 address = String8("");
1599 if (inputDesc->mPolicyMix == NULL) {
1600 address = String8("0");
1601 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1602 address = inputDesc->mPolicyMix->mRegistrationId;
1603 }
1604 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001605 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001606 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001607 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001608 }
Eric Laurente552edb2014-03-10 17:42:56 -07001609 }
1610
Eric Laurent1c333e22014-05-20 10:48:17 -07001611 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001612
François Gaffie53615e22015-03-19 09:24:12 +01001613 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001614 SoundTrigger::setCaptureState(false);
1615 }
Eric Laurent64265b22015-09-18 18:32:07 -07001616 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001617 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001618 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001619}
1620
Eric Laurent4dc68062014-07-28 17:26:49 -07001621void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1622 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001623{
1624 ALOGV("releaseInput() %d", input);
1625 ssize_t index = mInputs.indexOfKey(input);
1626 if (index < 0) {
1627 ALOGW("releaseInput() releasing unknown input %d", input);
1628 return;
1629 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001630
1631 // Routing
1632 mInputRoutes.removeRoute(session);
1633
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001634 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1635 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001636
Eric Laurentc722f302014-12-10 11:21:49 -08001637 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001638 if (index < 0) {
1639 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1640 return;
1641 }
Eric Laurentc722f302014-12-10 11:21:49 -08001642 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001643 if (inputDesc->mOpenRefCount == 0) {
1644 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1645 return;
1646 }
1647 inputDesc->mOpenRefCount--;
1648 if (inputDesc->mOpenRefCount > 0) {
1649 ALOGV("releaseInput() exit > 0");
1650 return;
1651 }
1652
Eric Laurent05b90f82014-08-27 15:32:29 -07001653 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001654 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001655 ALOGV("releaseInput() exit");
1656}
1657
Eric Laurentd4692962014-05-05 18:13:44 -07001658void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001659 bool patchRemoved = false;
1660
Eric Laurentd4692962014-05-05 18:13:44 -07001661 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001662 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1663 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1664 if (patch_index >= 0) {
1665 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1666 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1667 mAudioPatches.removeItemsAt(patch_index);
1668 patchRemoved = true;
1669 }
Eric Laurentd4692962014-05-05 18:13:44 -07001670 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1671 }
1672 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001673 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001674
1675 if (patchRemoved) {
1676 mpClientInterface->onAudioPatchListUpdate();
1677 }
Eric Laurentd4692962014-05-05 18:13:44 -07001678}
1679
Eric Laurente0720872014-03-11 09:30:41 -07001680void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001681 int indexMin,
1682 int indexMax)
1683{
1684 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001685 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001686 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1687 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001688 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001689 }
Eric Laurente552edb2014-03-10 17:42:56 -07001690}
1691
Eric Laurente0720872014-03-11 09:30:41 -07001692status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001693 int index,
1694 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001695{
1696
Eric Laurentc75307b2015-03-17 15:29:32 -07001697 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1698 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001699 return BAD_VALUE;
1700 }
1701 if (!audio_is_output_device(device)) {
1702 return BAD_VALUE;
1703 }
1704
1705 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001706 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001707
1708 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1709 stream, device, index);
1710
1711 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1712 // clear all device specific values
1713 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001714 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001715 }
François Gaffiedfd74092015-03-19 12:10:59 +01001716 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001717
Eric Laurent31551f82014-10-10 18:21:56 -07001718 // update volume on all outputs whose current device is also selected by the same
1719 // strategy as the device specified by the caller
1720 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001721
1722
1723 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1724 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1725 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001726 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001727 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1728 }
1729 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1730 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001731 return NO_ERROR;
1732 }
Eric Laurente552edb2014-03-10 17:42:56 -07001733 status_t status = NO_ERROR;
1734 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001735 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1736 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001737 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001738 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001739 if (volStatus != NO_ERROR) {
1740 status = volStatus;
1741 }
1742 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001743 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1744 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1745 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001746 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001747 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001748 }
Eric Laurente552edb2014-03-10 17:42:56 -07001749 }
1750 return status;
1751}
1752
Eric Laurente0720872014-03-11 09:30:41 -07001753status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001754 int *index,
1755 audio_devices_t device)
1756{
1757 if (index == NULL) {
1758 return BAD_VALUE;
1759 }
1760 if (!audio_is_output_device(device)) {
1761 return BAD_VALUE;
1762 }
1763 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1764 // the strategy the stream belongs to.
1765 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1766 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1767 }
François Gaffiedfd74092015-03-19 12:10:59 +01001768 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001769
Eric Laurentc75307b2015-03-17 15:29:32 -07001770 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001771 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1772 return NO_ERROR;
1773}
1774
Eric Laurente0720872014-03-11 09:30:41 -07001775audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001776 const SortedVector<audio_io_handle_t>& outputs)
1777{
1778 // select one output among several suitable for global effects.
1779 // The priority is as follows:
1780 // 1: An offloaded output. If the effect ends up not being offloadable,
1781 // AudioFlinger will invalidate the track and the offloaded output
1782 // will be closed causing the effect to be moved to a PCM output.
1783 // 2: A deep buffer output
1784 // 3: the first output in the list
1785
1786 if (outputs.size() == 0) {
1787 return 0;
1788 }
1789
1790 audio_io_handle_t outputOffloaded = 0;
1791 audio_io_handle_t outputDeepBuffer = 0;
1792
1793 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001794 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001795 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001796 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1797 outputOffloaded = outputs[i];
1798 }
1799 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1800 outputDeepBuffer = outputs[i];
1801 }
1802 }
1803
1804 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1805 outputOffloaded, outputDeepBuffer);
1806 if (outputOffloaded != 0) {
1807 return outputOffloaded;
1808 }
1809 if (outputDeepBuffer != 0) {
1810 return outputDeepBuffer;
1811 }
1812
1813 return outputs[0];
1814}
1815
Eric Laurente0720872014-03-11 09:30:41 -07001816audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001817{
1818 // apply simple rule where global effects are attached to the same output as MUSIC streams
1819
Eric Laurent3b73df72014-03-11 09:06:29 -07001820 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001821 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1822 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1823
1824 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1825 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1826 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1827
1828 return output;
1829}
1830
Eric Laurente0720872014-03-11 09:30:41 -07001831status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001832 audio_io_handle_t io,
1833 uint32_t strategy,
1834 int session,
1835 int id)
1836{
1837 ssize_t index = mOutputs.indexOfKey(io);
1838 if (index < 0) {
1839 index = mInputs.indexOfKey(io);
1840 if (index < 0) {
1841 ALOGW("registerEffect() unknown io %d", io);
1842 return INVALID_OPERATION;
1843 }
1844 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001845 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001846}
1847
Eric Laurentc75307b2015-03-17 15:29:32 -07001848bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1849{
1850 return mOutputs.isStreamActive(stream, inPastMs);
1851}
1852
1853bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1854{
1855 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1856}
1857
Eric Laurente0720872014-03-11 09:30:41 -07001858bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001859{
1860 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001861 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001862 if (inputDescriptor->mRefCount == 0) {
1863 continue;
1864 }
1865 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001866 return true;
1867 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001868 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1869 // corresponds to an active capture triggered by a hardware hotword recognition
1870 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1871 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1872 // FIXME: we should not assume that the first session is the active one and keep
1873 // activity count per session. Same in startInput().
1874 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1875 if (index >= 0) {
1876 return true;
1877 }
1878 }
Eric Laurente552edb2014-03-10 17:42:56 -07001879 }
1880 return false;
1881}
1882
Eric Laurent275e8e92014-11-30 15:14:47 -08001883// Register a list of custom mixes with their attributes and format.
1884// When a mix is registered, corresponding input and output profiles are
1885// added to the remote submix hw module. The profile contains only the
1886// parameters (sampling rate, format...) specified by the mix.
1887// The corresponding input remote submix device is also connected.
1888//
1889// When a remote submix device is connected, the address is checked to select the
1890// appropriate profile and the corresponding input or output stream is opened.
1891//
1892// When capture starts, getInputForAttr() will:
1893// - 1 look for a mix matching the address passed in attribtutes tags if any
1894// - 2 if none found, getDeviceForInputSource() will:
1895// - 2.1 look for a mix matching the attributes source
1896// - 2.2 if none found, default to device selection by policy rules
1897// At this time, the corresponding output remote submix device is also connected
1898// and active playback use cases can be transferred to this mix if needed when reconnecting
1899// after AudioTracks are invalidated
1900//
1901// When playback starts, getOutputForAttr() will:
1902// - 1 look for a mix matching the address passed in attribtutes tags if any
1903// - 2 if none found, look for a mix matching the attributes usage
1904// - 3 if none found, default to device and output selection by policy rules.
1905
1906status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1907{
1908 sp<HwModule> module;
1909 for (size_t i = 0; i < mHwModules.size(); i++) {
1910 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1911 mHwModules[i]->mHandle != 0) {
1912 module = mHwModules[i];
1913 break;
1914 }
1915 }
1916
1917 if (module == 0) {
1918 return INVALID_OPERATION;
1919 }
1920
1921 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1922
1923 for (size_t i = 0; i < mixes.size(); i++) {
1924 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001925
1926 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001927 continue;
1928 }
1929 audio_config_t outputConfig = mixes[i].mFormat;
1930 audio_config_t inputConfig = mixes[i].mFormat;
1931 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1932 // stereo and let audio flinger do the channel conversion if needed.
1933 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1934 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1935 module->addOutputProfile(address, &outputConfig,
1936 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1937 module->addInputProfile(address, &inputConfig,
1938 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001939
Eric Laurentc722f302014-12-10 11:21:49 -08001940 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001941 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001942 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001943 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001944 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001945 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001946 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001947 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001948 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001949 }
1950 return NO_ERROR;
1951}
1952
1953status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1954{
1955 sp<HwModule> module;
1956 for (size_t i = 0; i < mHwModules.size(); i++) {
1957 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1958 mHwModules[i]->mHandle != 0) {
1959 module = mHwModules[i];
1960 break;
1961 }
1962 }
1963
1964 if (module == 0) {
1965 return INVALID_OPERATION;
1966 }
1967
1968 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1969
1970 for (size_t i = 0; i < mixes.size(); i++) {
1971 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001972
1973 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001974 continue;
1975 }
1976
Eric Laurentc722f302014-12-10 11:21:49 -08001977 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1978 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1979 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001980 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001981 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001982 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001983 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001984
1985 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1986 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1987 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001988 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001989 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001990 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001991 }
1992 module->removeOutputProfile(address);
1993 module->removeInputProfile(address);
1994 }
1995 return NO_ERROR;
1996}
1997
Eric Laurente552edb2014-03-10 17:42:56 -07001998
Eric Laurente0720872014-03-11 09:30:41 -07001999status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002000{
2001 const size_t SIZE = 256;
2002 char buffer[SIZE];
2003 String8 result;
2004
2005 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2006 result.append(buffer);
2007
Eric Laurent87ffa392015-05-22 10:32:38 -07002008 snprintf(buffer, SIZE, " Primary Output: %d\n",
2009 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002010 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002011 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002012 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002013 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002014 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002015 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002016 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002017 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002018 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002019 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002020 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002021 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002022 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002023 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002024 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002025 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002026 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002027 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2028 result.append(buffer);
2029
François Gaffie2110e042015-03-24 08:41:51 +01002030 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002031
François Gaffie53615e22015-03-19 09:24:12 +01002032 mAvailableOutputDevices.dump(fd, String8("output"));
2033 mAvailableInputDevices.dump(fd, String8("input"));
2034 mHwModules.dump(fd);
2035 mOutputs.dump(fd);
2036 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002037 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002038 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002039 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002040
2041 return NO_ERROR;
2042}
2043
2044// This function checks for the parameters which can be offloaded.
2045// This can be enhanced depending on the capability of the DSP and policy
2046// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002047bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002048{
2049 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002050 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002051 offloadInfo.sample_rate, offloadInfo.channel_mask,
2052 offloadInfo.format,
2053 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2054 offloadInfo.has_video);
2055
2056 // Check if offload has been disabled
2057 char propValue[PROPERTY_VALUE_MAX];
2058 if (property_get("audio.offload.disable", propValue, "0")) {
2059 if (atoi(propValue) != 0) {
2060 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2061 return false;
2062 }
2063 }
2064
2065 // Check if stream type is music, then only allow offload as of now.
2066 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2067 {
2068 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2069 return false;
2070 }
2071
2072 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002073 const bool allowOffloadWithVideo =
2074 property_get_bool("audio.offload.video", false /* default_value */);
2075 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002076 ALOGV("isOffloadSupported: has_video == true, returning false");
2077 return false;
2078 }
2079
2080 //If duration is less than minimum value defined in property, return false
2081 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2082 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2083 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2084 return false;
2085 }
2086 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2087 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2088 return false;
2089 }
2090
2091 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2092 // creating an offloaded track and tearing it down immediately after start when audioflinger
2093 // detects there is an active non offloadable effect.
2094 // FIXME: We should check the audio session here but we do not have it in this context.
2095 // This may prevent offloading in rare situations where effects are left active by apps
2096 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002097 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002098 return false;
2099 }
2100
2101 // See if there is a profile to support this.
2102 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002103 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002104 offloadInfo.sample_rate,
2105 offloadInfo.format,
2106 offloadInfo.channel_mask,
2107 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002108 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2109 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002110}
2111
Eric Laurent6a94d692014-05-20 11:18:06 -07002112status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2113 audio_port_type_t type,
2114 unsigned int *num_ports,
2115 struct audio_port *ports,
2116 unsigned int *generation)
2117{
2118 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2119 generation == NULL) {
2120 return BAD_VALUE;
2121 }
2122 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2123 if (ports == NULL) {
2124 *num_ports = 0;
2125 }
2126
2127 size_t portsWritten = 0;
2128 size_t portsMax = *num_ports;
2129 *num_ports = 0;
2130 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2131 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2132 for (size_t i = 0;
2133 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2134 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2135 }
2136 *num_ports += mAvailableOutputDevices.size();
2137 }
2138 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2139 for (size_t i = 0;
2140 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2141 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2142 }
2143 *num_ports += mAvailableInputDevices.size();
2144 }
2145 }
2146 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2147 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2148 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2149 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2150 }
2151 *num_ports += mInputs.size();
2152 }
2153 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002154 size_t numOutputs = 0;
2155 for (size_t i = 0; i < mOutputs.size(); i++) {
2156 if (!mOutputs[i]->isDuplicated()) {
2157 numOutputs++;
2158 if (portsWritten < portsMax) {
2159 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2160 }
2161 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002162 }
Eric Laurent84c70242014-06-23 08:46:27 -07002163 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002164 }
2165 }
2166 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002167 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002168 return NO_ERROR;
2169}
2170
2171status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2172{
2173 return NO_ERROR;
2174}
2175
Eric Laurent6a94d692014-05-20 11:18:06 -07002176status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2177 audio_patch_handle_t *handle,
2178 uid_t uid)
2179{
2180 ALOGV("createAudioPatch()");
2181
2182 if (handle == NULL || patch == NULL) {
2183 return BAD_VALUE;
2184 }
2185 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2186
Eric Laurent874c42872014-08-08 15:13:39 -07002187 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2188 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2189 return BAD_VALUE;
2190 }
2191 // only one source per audio patch supported for now
2192 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002193 return INVALID_OPERATION;
2194 }
Eric Laurent874c42872014-08-08 15:13:39 -07002195
2196 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002197 return INVALID_OPERATION;
2198 }
Eric Laurent874c42872014-08-08 15:13:39 -07002199 for (size_t i = 0; i < patch->num_sinks; i++) {
2200 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2201 return INVALID_OPERATION;
2202 }
2203 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002204
2205 sp<AudioPatch> patchDesc;
2206 ssize_t index = mAudioPatches.indexOfKey(*handle);
2207
Eric Laurent6a94d692014-05-20 11:18:06 -07002208 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2209 patch->sources[0].role,
2210 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002211#if LOG_NDEBUG == 0
2212 for (size_t i = 0; i < patch->num_sinks; i++) {
2213 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2214 patch->sinks[i].role,
2215 patch->sinks[i].type);
2216 }
2217#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002218
2219 if (index >= 0) {
2220 patchDesc = mAudioPatches.valueAt(index);
2221 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2222 mUidCached, patchDesc->mUid, uid);
2223 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2224 return INVALID_OPERATION;
2225 }
2226 } else {
2227 *handle = 0;
2228 }
2229
2230 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002231 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002232 if (outputDesc == NULL) {
2233 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2234 return BAD_VALUE;
2235 }
Eric Laurent84c70242014-06-23 08:46:27 -07002236 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2237 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002238 if (patchDesc != 0) {
2239 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2240 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2241 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2242 return BAD_VALUE;
2243 }
2244 }
Eric Laurent874c42872014-08-08 15:13:39 -07002245 DeviceVector devices;
2246 for (size_t i = 0; i < patch->num_sinks; i++) {
2247 // Only support mix to devices connection
2248 // TODO add support for mix to mix connection
2249 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2250 ALOGV("createAudioPatch() source mix but sink is not a device");
2251 return INVALID_OPERATION;
2252 }
2253 sp<DeviceDescriptor> devDesc =
2254 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2255 if (devDesc == 0) {
2256 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2257 return BAD_VALUE;
2258 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002259
François Gaffie53615e22015-03-19 09:24:12 +01002260 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002261 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002262 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002263 NULL, // updatedSamplingRate
2264 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002265 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002266 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002267 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002268 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002269 ALOGV("createAudioPatch() profile not supported for device %08x",
2270 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002271 return INVALID_OPERATION;
2272 }
2273 devices.add(devDesc);
2274 }
2275 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002276 return INVALID_OPERATION;
2277 }
Eric Laurent874c42872014-08-08 15:13:39 -07002278
Eric Laurent6a94d692014-05-20 11:18:06 -07002279 // TODO: reconfigure output format and channels here
2280 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002281 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002282 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002283 index = mAudioPatches.indexOfKey(*handle);
2284 if (index >= 0) {
2285 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2286 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2287 }
2288 patchDesc = mAudioPatches.valueAt(index);
2289 patchDesc->mUid = uid;
2290 ALOGV("createAudioPatch() success");
2291 } else {
2292 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2293 return INVALID_OPERATION;
2294 }
2295 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2296 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2297 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002298 // only one sink supported when connecting an input device to a mix
2299 if (patch->num_sinks > 1) {
2300 return INVALID_OPERATION;
2301 }
François Gaffie53615e22015-03-19 09:24:12 +01002302 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002303 if (inputDesc == NULL) {
2304 return BAD_VALUE;
2305 }
2306 if (patchDesc != 0) {
2307 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2308 return BAD_VALUE;
2309 }
2310 }
2311 sp<DeviceDescriptor> devDesc =
2312 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2313 if (devDesc == 0) {
2314 return BAD_VALUE;
2315 }
2316
François Gaffie53615e22015-03-19 09:24:12 +01002317 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002318 devDesc->mAddress,
2319 patch->sinks[0].sample_rate,
2320 NULL, /*updatedSampleRate*/
2321 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002322 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002323 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002324 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002325 // FIXME for the parameter type,
2326 // and the NONE
2327 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002328 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002329 return INVALID_OPERATION;
2330 }
2331 // TODO: reconfigure output format and channels here
2332 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002333 devDesc->type(), inputDesc->mIoHandle);
2334 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002335 index = mAudioPatches.indexOfKey(*handle);
2336 if (index >= 0) {
2337 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2338 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2339 }
2340 patchDesc = mAudioPatches.valueAt(index);
2341 patchDesc->mUid = uid;
2342 ALOGV("createAudioPatch() success");
2343 } else {
2344 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2345 return INVALID_OPERATION;
2346 }
2347 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2348 // device to device connection
2349 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002350 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002351 return BAD_VALUE;
2352 }
2353 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002354 sp<DeviceDescriptor> srcDeviceDesc =
2355 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002356 if (srcDeviceDesc == 0) {
2357 return BAD_VALUE;
2358 }
Eric Laurent874c42872014-08-08 15:13:39 -07002359
Eric Laurent6a94d692014-05-20 11:18:06 -07002360 //update source and sink with our own data as the data passed in the patch may
2361 // be incomplete.
2362 struct audio_patch newPatch = *patch;
2363 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002364
Eric Laurent874c42872014-08-08 15:13:39 -07002365 for (size_t i = 0; i < patch->num_sinks; i++) {
2366 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2367 ALOGV("createAudioPatch() source device but one sink is not a device");
2368 return INVALID_OPERATION;
2369 }
2370
2371 sp<DeviceDescriptor> sinkDeviceDesc =
2372 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2373 if (sinkDeviceDesc == 0) {
2374 return BAD_VALUE;
2375 }
2376 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2377
Eric Laurent3bcf8592015-04-03 12:13:24 -07002378 // create a software bridge in PatchPanel if:
2379 // - source and sink devices are on differnt HW modules OR
2380 // - audio HAL version is < 3.0
2381 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2382 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2383 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002384 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002385 return INVALID_OPERATION;
2386 }
Eric Laurent874c42872014-08-08 15:13:39 -07002387 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002388 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002389 // if the sink device is reachable via an opened output stream, request to go via
2390 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002391 audio_io_handle_t output = selectOutput(outputs,
2392 AUDIO_OUTPUT_FLAG_NONE,
2393 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002394 if (output != AUDIO_IO_HANDLE_NONE) {
2395 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2396 if (outputDesc->isDuplicated()) {
2397 return INVALID_OPERATION;
2398 }
2399 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002400 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002401 newPatch.num_sources = 2;
2402 }
Eric Laurent83b88082014-06-20 18:31:16 -07002403 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002404 }
2405 // TODO: check from routing capabilities in config file and other conflicting patches
2406
2407 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2408 if (index >= 0) {
2409 afPatchHandle = patchDesc->mAfPatchHandle;
2410 }
2411
2412 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2413 &afPatchHandle,
2414 0);
2415 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2416 status, afPatchHandle);
2417 if (status == NO_ERROR) {
2418 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002419 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002420 addAudioPatch(patchDesc->mHandle, patchDesc);
2421 } else {
2422 patchDesc->mPatch = newPatch;
2423 }
2424 patchDesc->mAfPatchHandle = afPatchHandle;
2425 *handle = patchDesc->mHandle;
2426 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002427 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002428 } else {
2429 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2430 status);
2431 return INVALID_OPERATION;
2432 }
2433 } else {
2434 return BAD_VALUE;
2435 }
2436 } else {
2437 return BAD_VALUE;
2438 }
2439 return NO_ERROR;
2440}
2441
2442status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2443 uid_t uid)
2444{
2445 ALOGV("releaseAudioPatch() patch %d", handle);
2446
2447 ssize_t index = mAudioPatches.indexOfKey(handle);
2448
2449 if (index < 0) {
2450 return BAD_VALUE;
2451 }
2452 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2453 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2454 mUidCached, patchDesc->mUid, uid);
2455 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2456 return INVALID_OPERATION;
2457 }
2458
2459 struct audio_patch *patch = &patchDesc->mPatch;
2460 patchDesc->mUid = mUidCached;
2461 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002462 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002463 if (outputDesc == NULL) {
2464 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2465 return BAD_VALUE;
2466 }
2467
Eric Laurentc75307b2015-03-17 15:29:32 -07002468 setOutputDevice(outputDesc,
2469 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002470 true,
2471 0,
2472 NULL);
2473 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2474 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002475 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002476 if (inputDesc == NULL) {
2477 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2478 return BAD_VALUE;
2479 }
2480 setInputDevice(inputDesc->mIoHandle,
2481 getNewInputDevice(inputDesc->mIoHandle),
2482 true,
2483 NULL);
2484 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2485 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2486 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2487 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2488 status, patchDesc->mAfPatchHandle);
2489 removeAudioPatch(patchDesc->mHandle);
2490 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002491 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002492 } else {
2493 return BAD_VALUE;
2494 }
2495 } else {
2496 return BAD_VALUE;
2497 }
2498 return NO_ERROR;
2499}
2500
2501status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2502 struct audio_patch *patches,
2503 unsigned int *generation)
2504{
François Gaffie53615e22015-03-19 09:24:12 +01002505 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002506 return BAD_VALUE;
2507 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002508 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002509 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002510}
2511
Eric Laurente1715a42014-05-20 11:30:42 -07002512status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002513{
Eric Laurente1715a42014-05-20 11:30:42 -07002514 ALOGV("setAudioPortConfig()");
2515
2516 if (config == NULL) {
2517 return BAD_VALUE;
2518 }
2519 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2520 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002521 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2522 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002523 }
2524
Eric Laurenta121f902014-06-03 13:32:54 -07002525 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002526 if (config->type == AUDIO_PORT_TYPE_MIX) {
2527 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002528 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002529 if (outputDesc == NULL) {
2530 return BAD_VALUE;
2531 }
Eric Laurent84c70242014-06-23 08:46:27 -07002532 ALOG_ASSERT(!outputDesc->isDuplicated(),
2533 "setAudioPortConfig() called on duplicated output %d",
2534 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002535 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002536 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002537 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002538 if (inputDesc == NULL) {
2539 return BAD_VALUE;
2540 }
Eric Laurenta121f902014-06-03 13:32:54 -07002541 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002542 } else {
2543 return BAD_VALUE;
2544 }
2545 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2546 sp<DeviceDescriptor> deviceDesc;
2547 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2548 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2549 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2550 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2551 } else {
2552 return BAD_VALUE;
2553 }
2554 if (deviceDesc == NULL) {
2555 return BAD_VALUE;
2556 }
Eric Laurenta121f902014-06-03 13:32:54 -07002557 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002558 } else {
2559 return BAD_VALUE;
2560 }
2561
Eric Laurenta121f902014-06-03 13:32:54 -07002562 struct audio_port_config backupConfig;
2563 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2564 if (status == NO_ERROR) {
2565 struct audio_port_config newConfig;
2566 audioPortConfig->toAudioPortConfig(&newConfig, config);
2567 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002568 }
Eric Laurenta121f902014-06-03 13:32:54 -07002569 if (status != NO_ERROR) {
2570 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002571 }
Eric Laurente1715a42014-05-20 11:30:42 -07002572
2573 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002574}
2575
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002576void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2577{
2578 clearAudioPatches(uid);
2579 clearSessionRoutes(uid);
2580}
2581
Eric Laurent6a94d692014-05-20 11:18:06 -07002582void AudioPolicyManager::clearAudioPatches(uid_t uid)
2583{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002584 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002585 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2586 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002587 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002588 }
2589 }
2590}
2591
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002592
2593void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2594 audio_io_handle_t ouptutToSkip)
2595{
2596 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2597 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2598 for (size_t j = 0; j < mOutputs.size(); j++) {
2599 if (mOutputs.keyAt(j) == ouptutToSkip) {
2600 continue;
2601 }
2602 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2603 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2604 continue;
2605 }
2606 // If the default device for this strategy is on another output mix,
2607 // invalidate all tracks in this strategy to force re connection.
2608 // Otherwise select new device on the output mix.
2609 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2610 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2611 if (stream == AUDIO_STREAM_PATCH) {
2612 continue;
2613 }
2614 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2615 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2616 }
2617 }
2618 } else {
2619 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2620 setOutputDevice(outputDesc, newDevice, false);
2621 }
2622 }
2623}
2624
2625void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2626{
2627 // remove output routes associated with this uid
2628 SortedVector<routing_strategy> affectedStrategies;
2629 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2630 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2631 if (route->mUid == uid) {
2632 mOutputRoutes.removeItemsAt(i);
2633 if (route->mDeviceDescriptor != 0) {
2634 affectedStrategies.add(getStrategy(route->mStreamType));
2635 }
2636 }
2637 }
2638 // reroute outputs if necessary
2639 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2640 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2641 }
2642
2643 // remove input routes associated with this uid
2644 SortedVector<audio_source_t> affectedSources;
2645 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2646 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2647 if (route->mUid == uid) {
2648 mInputRoutes.removeItemsAt(i);
2649 if (route->mDeviceDescriptor != 0) {
2650 affectedSources.add(route->mSource);
2651 }
2652 }
2653 }
2654 // reroute inputs if necessary
2655 SortedVector<audio_io_handle_t> inputsToClose;
2656 for (size_t i = 0; i < mInputs.size(); i++) {
2657 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2658 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2659 inputsToClose.add(inputDesc->mIoHandle);
2660 }
2661 }
2662 for (size_t i = 0; i < inputsToClose.size(); i++) {
2663 closeInput(inputsToClose[i]);
2664 }
2665}
2666
2667
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002668status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2669 audio_io_handle_t *ioHandle,
2670 audio_devices_t *device)
2671{
2672 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2673 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002674 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002675
François Gaffiedf372692015-03-19 10:43:27 +01002676 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002677}
2678
Eric Laurent493404d2015-04-21 15:07:36 -07002679status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2680 const audio_attributes_t *attributes __unused,
2681 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002682{
2683 return INVALID_OPERATION;
2684}
2685
Eric Laurent493404d2015-04-21 15:07:36 -07002686status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002687{
2688 return INVALID_OPERATION;
2689}
2690
Eric Laurente552edb2014-03-10 17:42:56 -07002691// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002692// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002693// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002694uint32_t AudioPolicyManager::nextAudioPortGeneration()
2695{
2696 return android_atomic_inc(&mAudioPortGeneration);
2697}
2698
Eric Laurente0720872014-03-11 09:30:41 -07002699AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002700 :
2701#ifdef AUDIO_POLICY_TEST
2702 Thread(false),
2703#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002704 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002705 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002706 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002707 mAudioPortGeneration(1),
2708 mBeaconMuteRefCount(0),
2709 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07002710 mBeaconMuted(false),
2711 mTtsOutputAvailable(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002712{
François Gaffie2110e042015-03-24 08:41:51 +01002713 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2714 if (!engineInstance) {
2715 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2716 return;
2717 }
2718 // Retrieve the Policy Manager Interface
2719 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2720 if (mEngine == NULL) {
2721 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2722 return;
2723 }
2724 mEngine->setObserver(this);
2725 status_t status = mEngine->initCheck();
2726 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2727
Eric Laurent6a94d692014-05-20 11:18:06 -07002728 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002729 mpClientInterface = clientInterface;
2730
Eric Laurent7288ab82015-05-06 18:44:02 -07002731 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002732 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2733 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2734 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2735 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2736 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2737 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002738 ALOGE("could not load audio policy configuration file, setting defaults");
2739 defaultAudioPolicyConfig();
2740 }
2741 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002742 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002743
François Gaffie2110e042015-03-24 08:41:51 +01002744 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2745 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002746
2747 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002748 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2749 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002750 for (size_t i = 0; i < mHwModules.size(); i++) {
2751 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2752 if (mHwModules[i]->mHandle == 0) {
2753 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2754 continue;
2755 }
2756 // open all output streams needed to access attached devices
2757 // except for direct output streams that are only opened when they are actually
2758 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002759 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002760 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2761 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002762 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002763
Eric Laurent3a4311c2014-03-17 12:00:47 -07002764 if (outProfile->mSupportedDevices.isEmpty()) {
2765 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2766 continue;
2767 }
Eric Laurent9459fb02015-08-12 18:36:32 -07002768 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_TTS) != 0) {
2769 mTtsOutputAvailable = true;
2770 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002771
Eric Laurentd78f1532014-09-16 16:38:20 -07002772 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2773 continue;
2774 }
Eric Laurent83b88082014-06-20 18:31:16 -07002775 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002776 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2777 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002778 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002779 // chose first device present in mSupportedDevices also part of
2780 // outputDeviceTypes
2781 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002782 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002783 if ((profileType & outputDeviceTypes) != 0) {
2784 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002785 }
Eric Laurente552edb2014-03-10 17:42:56 -07002786 }
2787 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002788 if ((profileType & outputDeviceTypes) == 0) {
2789 continue;
2790 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002791 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2792 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002793
2794 outputDesc->mDevice = profileType;
2795 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2796 config.sample_rate = outputDesc->mSamplingRate;
2797 config.channel_mask = outputDesc->mChannelMask;
2798 config.format = outputDesc->mFormat;
2799 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002800 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002801 &output,
2802 &config,
2803 &outputDesc->mDevice,
2804 String8(""),
2805 &outputDesc->mLatency,
2806 outputDesc->mFlags);
2807
2808 if (status != NO_ERROR) {
2809 ALOGW("Cannot open output stream for device %08x on hw module %s",
2810 outputDesc->mDevice,
2811 mHwModules[i]->mName);
2812 } else {
2813 outputDesc->mSamplingRate = config.sample_rate;
2814 outputDesc->mChannelMask = config.channel_mask;
2815 outputDesc->mFormat = config.format;
2816
2817 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002818 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002819 ssize_t index =
2820 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2821 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002822 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2823 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002824 }
2825 }
2826 if (mPrimaryOutput == 0 &&
2827 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002828 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002829 }
2830 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002831 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002832 outputDesc->mDevice,
2833 true);
2834 }
Eric Laurente552edb2014-03-10 17:42:56 -07002835 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002836 // open input streams needed to access attached devices to validate
2837 // mAvailableInputDevices list
2838 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2839 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002840 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002841
Eric Laurent3a4311c2014-03-17 12:00:47 -07002842 if (inProfile->mSupportedDevices.isEmpty()) {
2843 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2844 continue;
2845 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002846 // chose first device present in mSupportedDevices also part of
2847 // inputDeviceTypes
2848 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2849 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002850 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002851 if (profileType & inputDeviceTypes) {
2852 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002853 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002854 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002855 if ((profileType & inputDeviceTypes) == 0) {
2856 continue;
2857 }
2858 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2859
2860 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2861 inputDesc->mDevice = profileType;
2862
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002863 // find the address
2864 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2865 // the inputs vector must be of size 1, but we don't want to crash here
2866 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2867 : String8("");
2868 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2869 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2870
Eric Laurentd78f1532014-09-16 16:38:20 -07002871 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2872 config.sample_rate = inputDesc->mSamplingRate;
2873 config.channel_mask = inputDesc->mChannelMask;
2874 config.format = inputDesc->mFormat;
2875 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002876 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002877 &input,
2878 &config,
2879 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002880 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002881 AUDIO_SOURCE_MIC,
2882 AUDIO_INPUT_FLAG_NONE);
2883
2884 if (status == NO_ERROR) {
2885 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002886 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002887 ssize_t index =
2888 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2889 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07002890 if (index >= 0) {
2891 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
2892 if (!devDesc->isAttached()) {
2893 devDesc->attach(mHwModules[i]);
2894 devDesc->importAudioPort(inProfile);
2895 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002896 }
2897 }
2898 mpClientInterface->closeInput(input);
2899 } else {
2900 ALOGW("Cannot open input stream for device %08x on hw module %s",
2901 inputDesc->mDevice,
2902 mHwModules[i]->mName);
2903 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002904 }
2905 }
2906 // make sure all attached devices have been allocated a unique ID
2907 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002908 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002909 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002910 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2911 continue;
2912 }
François Gaffie2110e042015-03-24 08:41:51 +01002913 // The device is now validated and can be appended to the available devices of the engine
2914 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2915 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002916 i++;
2917 }
2918 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002919 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002920 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002921 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2922 continue;
2923 }
François Gaffie2110e042015-03-24 08:41:51 +01002924 // The device is now validated and can be appended to the available devices of the engine
2925 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2926 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002927 i++;
2928 }
2929 // make sure default device is reachable
2930 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002931 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002932 }
Eric Laurente552edb2014-03-10 17:42:56 -07002933
2934 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2935
2936 updateDevicesAndOutputs();
2937
2938#ifdef AUDIO_POLICY_TEST
2939 if (mPrimaryOutput != 0) {
2940 AudioParameter outputCmd = AudioParameter();
2941 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002942 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002943
2944 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2945 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002946 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2947 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002948 mTestLatencyMs = 0;
2949 mCurOutput = 0;
2950 mDirectOutput = false;
2951 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2952 mTestOutputs[i] = 0;
2953 }
2954
2955 const size_t SIZE = 256;
2956 char buffer[SIZE];
2957 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2958 run(buffer, ANDROID_PRIORITY_AUDIO);
2959 }
2960#endif //AUDIO_POLICY_TEST
2961}
2962
Eric Laurente0720872014-03-11 09:30:41 -07002963AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002964{
2965#ifdef AUDIO_POLICY_TEST
2966 exit();
2967#endif //AUDIO_POLICY_TEST
2968 for (size_t i = 0; i < mOutputs.size(); i++) {
2969 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002970 }
2971 for (size_t i = 0; i < mInputs.size(); i++) {
2972 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002973 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002974 mAvailableOutputDevices.clear();
2975 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002976 mOutputs.clear();
2977 mInputs.clear();
2978 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002979}
2980
Eric Laurente0720872014-03-11 09:30:41 -07002981status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002982{
Eric Laurent87ffa392015-05-22 10:32:38 -07002983 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002984}
2985
2986#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002987bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002988{
2989 ALOGV("entering threadLoop()");
2990 while (!exitPending())
2991 {
2992 String8 command;
2993 int valueInt;
2994 String8 value;
2995
2996 Mutex::Autolock _l(mLock);
2997 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2998
2999 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3000 AudioParameter param = AudioParameter(command);
3001
3002 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3003 valueInt != 0) {
3004 ALOGV("Test command %s received", command.string());
3005 String8 target;
3006 if (param.get(String8("target"), target) != NO_ERROR) {
3007 target = "Manager";
3008 }
3009 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3010 param.remove(String8("test_cmd_policy_output"));
3011 mCurOutput = valueInt;
3012 }
3013 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3014 param.remove(String8("test_cmd_policy_direct"));
3015 if (value == "false") {
3016 mDirectOutput = false;
3017 } else if (value == "true") {
3018 mDirectOutput = true;
3019 }
3020 }
3021 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3022 param.remove(String8("test_cmd_policy_input"));
3023 mTestInput = valueInt;
3024 }
3025
3026 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3027 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003028 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003029 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003030 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003031 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003032 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003033 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003034 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003035 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003036 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003037 if (target == "Manager") {
3038 mTestFormat = format;
3039 } else if (mTestOutputs[mCurOutput] != 0) {
3040 AudioParameter outputParam = AudioParameter();
3041 outputParam.addInt(String8("format"), format);
3042 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3043 }
3044 }
3045 }
3046 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3047 param.remove(String8("test_cmd_policy_channels"));
3048 int channels = 0;
3049
3050 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003051 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003052 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003053 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003054 }
3055 if (channels != 0) {
3056 if (target == "Manager") {
3057 mTestChannels = channels;
3058 } else if (mTestOutputs[mCurOutput] != 0) {
3059 AudioParameter outputParam = AudioParameter();
3060 outputParam.addInt(String8("channels"), channels);
3061 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3062 }
3063 }
3064 }
3065 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3066 param.remove(String8("test_cmd_policy_sampleRate"));
3067 if (valueInt >= 0 && valueInt <= 96000) {
3068 int samplingRate = valueInt;
3069 if (target == "Manager") {
3070 mTestSamplingRate = samplingRate;
3071 } else if (mTestOutputs[mCurOutput] != 0) {
3072 AudioParameter outputParam = AudioParameter();
3073 outputParam.addInt(String8("sampling_rate"), samplingRate);
3074 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3075 }
3076 }
3077 }
3078
3079 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3080 param.remove(String8("test_cmd_policy_reopen"));
3081
Eric Laurentc75307b2015-03-17 15:29:32 -07003082 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003083
Eric Laurentc75307b2015-03-17 15:29:32 -07003084 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003085
Eric Laurentc75307b2015-03-17 15:29:32 -07003086 removeOutput(mPrimaryOutput->mIoHandle);
3087 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3088 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003089 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003090 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3091 config.sample_rate = outputDesc->mSamplingRate;
3092 config.channel_mask = outputDesc->mChannelMask;
3093 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003094 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003095 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003096 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003097 &config,
3098 &outputDesc->mDevice,
3099 String8(""),
3100 &outputDesc->mLatency,
3101 outputDesc->mFlags);
3102 if (status != NO_ERROR) {
3103 ALOGE("Failed to reopen hardware output stream, "
3104 "samplingRate: %d, format %d, channels %d",
3105 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003106 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003107 outputDesc->mSamplingRate = config.sample_rate;
3108 outputDesc->mChannelMask = config.channel_mask;
3109 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003110 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003111 AudioParameter outputCmd = AudioParameter();
3112 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003113 mpClientInterface->setParameters(handle, outputCmd.toString());
3114 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003115 }
3116 }
3117
3118
3119 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3120 }
3121 }
3122 return false;
3123}
3124
Eric Laurente0720872014-03-11 09:30:41 -07003125void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003126{
3127 {
3128 AutoMutex _l(mLock);
3129 requestExit();
3130 mWaitWorkCV.signal();
3131 }
3132 requestExitAndWait();
3133}
3134
Eric Laurente0720872014-03-11 09:30:41 -07003135int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003136{
3137 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3138 if (output == mTestOutputs[i]) return i;
3139 }
3140 return 0;
3141}
3142#endif //AUDIO_POLICY_TEST
3143
3144// ---
3145
Eric Laurentc75307b2015-03-17 15:29:32 -07003146void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003147{
François Gaffie98cc1912015-03-18 17:52:40 +01003148 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003149 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003150 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003151}
3152
François Gaffie53615e22015-03-19 09:24:12 +01003153void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3154{
3155 mOutputs.removeItem(output);
3156}
3157
Eric Laurent1f2f2232014-06-02 12:01:23 -07003158void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003159{
François Gaffie98cc1912015-03-18 17:52:40 +01003160 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003161 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003162 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003163}
Eric Laurente552edb2014-03-10 17:42:56 -07003164
Eric Laurentc75307b2015-03-17 15:29:32 -07003165void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003166 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003167 const String8 address /*in*/,
3168 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003169 sp<DeviceDescriptor> devDesc =
3170 desc->mProfile->mSupportedDevices.getDevice(device, address);
3171 if (devDesc != 0) {
3172 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3173 desc->mIoHandle, address.string());
3174 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003175 }
3176}
3177
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003178status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003179 audio_policy_dev_state_t state,
3180 SortedVector<audio_io_handle_t>& outputs,
3181 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003182{
François Gaffie53615e22015-03-19 09:24:12 +01003183 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003184 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003185
3186 if (audio_device_is_digital(device)) {
3187 // erase all current sample rates, formats and channel masks
3188 devDesc->clearCapabilities();
3189 }
Eric Laurente552edb2014-03-10 17:42:56 -07003190
Eric Laurent3b73df72014-03-11 09:06:29 -07003191 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003192 // first list already open outputs that can be routed to this device
3193 for (size_t i = 0; i < mOutputs.size(); i++) {
3194 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003195 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003196 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003197 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3198 outputs.add(mOutputs.keyAt(i));
3199 } else {
3200 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003201 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003202 }
Eric Laurente552edb2014-03-10 17:42:56 -07003203 }
3204 }
3205 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003206 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003207 for (size_t i = 0; i < mHwModules.size(); i++)
3208 {
3209 if (mHwModules[i]->mHandle == 0) {
3210 continue;
3211 }
3212 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3213 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003214 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3215 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003216 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003217 address == profile->mSupportedDevices[0]->mAddress) {
3218 profiles.add(profile);
3219 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3220 }
Eric Laurente552edb2014-03-10 17:42:56 -07003221 }
3222 }
3223 }
3224
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003225 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3226
Eric Laurente552edb2014-03-10 17:42:56 -07003227 if (profiles.isEmpty() && outputs.isEmpty()) {
3228 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3229 return BAD_VALUE;
3230 }
3231
3232 // open outputs for matching profiles if needed. Direct outputs are also opened to
3233 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3234 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003235 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003236
3237 // nothing to do if one output is already opened for this profile
3238 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003239 for (j = 0; j < outputs.size(); j++) {
3240 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003241 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003242 // matching profile: save the sample rates, format and channel masks supported
3243 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003244 if (audio_device_is_digital(device)) {
3245 devDesc->importAudioPort(profile);
3246 }
Eric Laurente552edb2014-03-10 17:42:56 -07003247 break;
3248 }
3249 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003250 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003251 continue;
3252 }
3253
Eric Laurent83b88082014-06-20 18:31:16 -07003254 ALOGV("opening output for device %08x with params %s profile %p",
3255 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003256 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003257 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003258 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3259 config.sample_rate = desc->mSamplingRate;
3260 config.channel_mask = desc->mChannelMask;
3261 config.format = desc->mFormat;
3262 config.offload_info.sample_rate = desc->mSamplingRate;
3263 config.offload_info.channel_mask = desc->mChannelMask;
3264 config.offload_info.format = desc->mFormat;
3265 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003266 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003267 &output,
3268 &config,
3269 &desc->mDevice,
3270 address,
3271 &desc->mLatency,
3272 desc->mFlags);
3273 if (status == NO_ERROR) {
3274 desc->mSamplingRate = config.sample_rate;
3275 desc->mChannelMask = config.channel_mask;
3276 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003277
Eric Laurentd4692962014-05-05 18:13:44 -07003278 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003279 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003280 char *param = audio_device_address_to_parameter(device, address);
3281 mpClientInterface->setParameters(output, String8(param));
3282 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003283 }
3284
Eric Laurentd4692962014-05-05 18:13:44 -07003285 // Here is where we step through and resolve any "dynamic" fields
3286 String8 reply;
3287 char *value;
3288 if (profile->mSamplingRates[0] == 0) {
3289 reply = mpClientInterface->getParameters(output,
3290 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003291 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003292 reply.string());
3293 value = strpbrk((char *)reply.string(), "=");
3294 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003295 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003296 }
Eric Laurentd4692962014-05-05 18:13:44 -07003297 }
3298 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3299 reply = mpClientInterface->getParameters(output,
3300 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003301 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003302 reply.string());
3303 value = strpbrk((char *)reply.string(), "=");
3304 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003305 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003306 }
Eric Laurentd4692962014-05-05 18:13:44 -07003307 }
3308 if (profile->mChannelMasks[0] == 0) {
3309 reply = mpClientInterface->getParameters(output,
3310 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003311 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003312 reply.string());
3313 value = strpbrk((char *)reply.string(), "=");
3314 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003315 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003316 }
Eric Laurentd4692962014-05-05 18:13:44 -07003317 }
3318 if (((profile->mSamplingRates[0] == 0) &&
3319 (profile->mSamplingRates.size() < 2)) ||
3320 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3321 (profile->mFormats.size() < 2)) ||
3322 ((profile->mChannelMasks[0] == 0) &&
3323 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003324 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003325 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003326 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003327 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3328 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003329 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003330 config.sample_rate = profile->pickSamplingRate();
3331 config.channel_mask = profile->pickChannelMask();
3332 config.format = profile->pickFormat();
3333 config.offload_info.sample_rate = config.sample_rate;
3334 config.offload_info.channel_mask = config.channel_mask;
3335 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003336 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003337 &output,
3338 &config,
3339 &desc->mDevice,
3340 address,
3341 &desc->mLatency,
3342 desc->mFlags);
3343 if (status == NO_ERROR) {
3344 desc->mSamplingRate = config.sample_rate;
3345 desc->mChannelMask = config.channel_mask;
3346 desc->mFormat = config.format;
3347 } else {
3348 output = AUDIO_IO_HANDLE_NONE;
3349 }
Eric Laurentd4692962014-05-05 18:13:44 -07003350 }
3351
Eric Laurentcf2c0212014-07-25 16:20:43 -07003352 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003353 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003354 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003355 sp<AudioPolicyMix> policyMix;
3356 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003357 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3358 address.string());
3359 }
François Gaffie036e1e92015-03-19 10:16:24 +01003360 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003361 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003362
Eric Laurent87ffa392015-05-22 10:32:38 -07003363 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3364 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003365 // no duplicated output for direct outputs and
3366 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003367 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003368
Eric Laurentd4692962014-05-05 18:13:44 -07003369 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003370 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003371
Eric Laurentd4692962014-05-05 18:13:44 -07003372 //TODO: configure audio effect output stage here
3373
3374 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003375 duplicatedOutput =
3376 mpClientInterface->openDuplicateOutput(output,
3377 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003378 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003379 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003380 sp<SwAudioOutputDescriptor> dupOutputDesc =
3381 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3382 dupOutputDesc->mOutput1 = mPrimaryOutput;
3383 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003384 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3385 dupOutputDesc->mFormat = desc->mFormat;
3386 dupOutputDesc->mChannelMask = desc->mChannelMask;
3387 dupOutputDesc->mLatency = desc->mLatency;
3388 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003389 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003390 } else {
3391 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003392 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003393 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003394 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003395 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003396 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003397 }
Eric Laurente552edb2014-03-10 17:42:56 -07003398 }
3399 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003400 } else {
3401 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003402 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003403 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003404 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003405 profiles.removeAt(profile_index);
3406 profile_index--;
3407 } else {
3408 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003409 // Load digital format info only for digital devices
3410 if (audio_device_is_digital(device)) {
3411 devDesc->importAudioPort(profile);
3412 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003413
François Gaffie53615e22015-03-19 09:24:12 +01003414 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003415 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3416 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003417 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003418 NULL/*patch handle*/, address.string());
3419 }
Eric Laurente552edb2014-03-10 17:42:56 -07003420 ALOGV("checkOutputsForDevice(): adding output %d", output);
3421 }
3422 }
3423
3424 if (profiles.isEmpty()) {
3425 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3426 return BAD_VALUE;
3427 }
Eric Laurentd4692962014-05-05 18:13:44 -07003428 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003429 // check if one opened output is not needed any more after disconnecting one device
3430 for (size_t i = 0; i < mOutputs.size(); i++) {
3431 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003432 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003433 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003434 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003435 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003436 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003437 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003438 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3439 mOutputs.keyAt(i));
3440 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003441 }
Eric Laurente552edb2014-03-10 17:42:56 -07003442 }
3443 }
Eric Laurentd4692962014-05-05 18:13:44 -07003444 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003445 for (size_t i = 0; i < mHwModules.size(); i++)
3446 {
3447 if (mHwModules[i]->mHandle == 0) {
3448 continue;
3449 }
3450 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3451 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003452 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003453 if (profile->mSupportedDevices.types() & device) {
3454 ALOGV("checkOutputsForDevice(): "
3455 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003456 if (profile->mSamplingRates[0] == 0) {
3457 profile->mSamplingRates.clear();
3458 profile->mSamplingRates.add(0);
3459 }
3460 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3461 profile->mFormats.clear();
3462 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3463 }
3464 if (profile->mChannelMasks[0] == 0) {
3465 profile->mChannelMasks.clear();
3466 profile->mChannelMasks.add(0);
3467 }
3468 }
3469 }
3470 }
3471 }
3472 return NO_ERROR;
3473}
3474
Paul McLean9080a4c2015-06-18 08:24:02 -07003475status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003476 audio_policy_dev_state_t state,
3477 SortedVector<audio_io_handle_t>& inputs,
3478 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003479{
Paul McLean9080a4c2015-06-18 08:24:02 -07003480 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003481 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003482
3483 if (audio_device_is_digital(device)) {
3484 // erase all current sample rates, formats and channel masks
3485 devDesc->clearCapabilities();
3486 }
3487
Eric Laurentd4692962014-05-05 18:13:44 -07003488 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3489 // first list already open inputs that can be routed to this device
3490 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3491 desc = mInputs.valueAt(input_index);
3492 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3493 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3494 inputs.add(mInputs.keyAt(input_index));
3495 }
3496 }
3497
3498 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003499 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003500 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3501 {
3502 if (mHwModules[module_idx]->mHandle == 0) {
3503 continue;
3504 }
3505 for (size_t profile_index = 0;
3506 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3507 profile_index++)
3508 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003509 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3510
3511 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003512 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003513 address == profile->mSupportedDevices[0]->mAddress) {
3514 profiles.add(profile);
3515 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3516 profile_index, module_idx);
3517 }
Eric Laurentd4692962014-05-05 18:13:44 -07003518 }
3519 }
3520 }
3521
3522 if (profiles.isEmpty() && inputs.isEmpty()) {
3523 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3524 return BAD_VALUE;
3525 }
3526
3527 // open inputs for matching profiles if needed. Direct inputs are also opened to
3528 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3529 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3530
Eric Laurent1c333e22014-05-20 10:48:17 -07003531 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003532 // nothing to do if one input is already opened for this profile
3533 size_t input_index;
3534 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3535 desc = mInputs.valueAt(input_index);
3536 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003537 if (audio_device_is_digital(device)) {
3538 devDesc->importAudioPort(profile);
3539 }
Eric Laurentd4692962014-05-05 18:13:44 -07003540 break;
3541 }
3542 }
3543 if (input_index != mInputs.size()) {
3544 continue;
3545 }
3546
3547 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3548 desc = new AudioInputDescriptor(profile);
3549 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003550 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3551 config.sample_rate = desc->mSamplingRate;
3552 config.channel_mask = desc->mChannelMask;
3553 config.format = desc->mFormat;
3554 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003555 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003556 &input,
3557 &config,
3558 &desc->mDevice,
3559 address,
3560 AUDIO_SOURCE_MIC,
3561 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003562
Eric Laurentcf2c0212014-07-25 16:20:43 -07003563 if (status == NO_ERROR) {
3564 desc->mSamplingRate = config.sample_rate;
3565 desc->mChannelMask = config.channel_mask;
3566 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003567
Eric Laurentd4692962014-05-05 18:13:44 -07003568 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003569 char *param = audio_device_address_to_parameter(device, address);
3570 mpClientInterface->setParameters(input, String8(param));
3571 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003572 }
3573
3574 // Here is where we step through and resolve any "dynamic" fields
3575 String8 reply;
3576 char *value;
3577 if (profile->mSamplingRates[0] == 0) {
3578 reply = mpClientInterface->getParameters(input,
3579 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3580 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3581 reply.string());
3582 value = strpbrk((char *)reply.string(), "=");
3583 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003584 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003585 }
3586 }
3587 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3588 reply = mpClientInterface->getParameters(input,
3589 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3590 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3591 value = strpbrk((char *)reply.string(), "=");
3592 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003593 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003594 }
3595 }
3596 if (profile->mChannelMasks[0] == 0) {
3597 reply = mpClientInterface->getParameters(input,
3598 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3599 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3600 reply.string());
3601 value = strpbrk((char *)reply.string(), "=");
3602 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003603 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003604 }
3605 }
3606 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3607 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3608 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3609 ALOGW("checkInputsForDevice() direct input missing param");
3610 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003611 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003612 }
3613
3614 if (input != 0) {
3615 addInput(input, desc);
3616 }
3617 } // endif input != 0
3618
Eric Laurentcf2c0212014-07-25 16:20:43 -07003619 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003620 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003621 profiles.removeAt(profile_index);
3622 profile_index--;
3623 } else {
3624 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003625 if (audio_device_is_digital(device)) {
3626 devDesc->importAudioPort(profile);
3627 }
Eric Laurentd4692962014-05-05 18:13:44 -07003628 ALOGV("checkInputsForDevice(): adding input %d", input);
3629 }
3630 } // end scan profiles
3631
3632 if (profiles.isEmpty()) {
3633 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3634 return BAD_VALUE;
3635 }
3636 } else {
3637 // Disconnect
3638 // check if one opened input is not needed any more after disconnecting one device
3639 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3640 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003641 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3642 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003643 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3644 mInputs.keyAt(input_index));
3645 inputs.add(mInputs.keyAt(input_index));
3646 }
3647 }
3648 // Clear any profiles associated with the disconnected device.
3649 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3650 if (mHwModules[module_index]->mHandle == 0) {
3651 continue;
3652 }
3653 for (size_t profile_index = 0;
3654 profile_index < mHwModules[module_index]->mInputProfiles.size();
3655 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003656 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Paul McLean9080a4c2015-06-18 08:24:02 -07003657 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003658 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003659 profile_index, module_index);
3660 if (profile->mSamplingRates[0] == 0) {
3661 profile->mSamplingRates.clear();
3662 profile->mSamplingRates.add(0);
3663 }
3664 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3665 profile->mFormats.clear();
3666 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3667 }
3668 if (profile->mChannelMasks[0] == 0) {
3669 profile->mChannelMasks.clear();
3670 profile->mChannelMasks.add(0);
3671 }
3672 }
3673 }
3674 }
3675 } // end disconnect
3676
3677 return NO_ERROR;
3678}
3679
3680
Eric Laurente0720872014-03-11 09:30:41 -07003681void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003682{
3683 ALOGV("closeOutput(%d)", output);
3684
Eric Laurentc75307b2015-03-17 15:29:32 -07003685 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003686 if (outputDesc == NULL) {
3687 ALOGW("closeOutput() unknown output %d", output);
3688 return;
3689 }
François Gaffie036e1e92015-03-19 10:16:24 +01003690 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003691
Eric Laurente552edb2014-03-10 17:42:56 -07003692 // look for duplicated outputs connected to the output being removed.
3693 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003694 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003695 if (dupOutputDesc->isDuplicated() &&
3696 (dupOutputDesc->mOutput1 == outputDesc ||
3697 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003698 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003699 if (dupOutputDesc->mOutput1 == outputDesc) {
3700 outputDesc2 = dupOutputDesc->mOutput2;
3701 } else {
3702 outputDesc2 = dupOutputDesc->mOutput1;
3703 }
3704 // As all active tracks on duplicated output will be deleted,
3705 // and as they were also referenced on the other output, the reference
3706 // count for their stream type must be adjusted accordingly on
3707 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003708 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003709 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003710 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003711 }
3712 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3713 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3714
3715 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003716 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003717 }
3718 }
3719
Eric Laurent05b90f82014-08-27 15:32:29 -07003720 nextAudioPortGeneration();
3721
3722 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3723 if (index >= 0) {
3724 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3725 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3726 mAudioPatches.removeItemsAt(index);
3727 mpClientInterface->onAudioPatchListUpdate();
3728 }
3729
Eric Laurente552edb2014-03-10 17:42:56 -07003730 AudioParameter param;
3731 param.add(String8("closing"), String8("true"));
3732 mpClientInterface->setParameters(output, param.toString());
3733
3734 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003735 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003736 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003737}
3738
3739void AudioPolicyManager::closeInput(audio_io_handle_t input)
3740{
3741 ALOGV("closeInput(%d)", input);
3742
3743 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3744 if (inputDesc == NULL) {
3745 ALOGW("closeInput() unknown input %d", input);
3746 return;
3747 }
3748
Eric Laurent6a94d692014-05-20 11:18:06 -07003749 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003750
3751 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3752 if (index >= 0) {
3753 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3754 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3755 mAudioPatches.removeItemsAt(index);
3756 mpClientInterface->onAudioPatchListUpdate();
3757 }
3758
3759 mpClientInterface->closeInput(input);
3760 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003761}
3762
Eric Laurentc75307b2015-03-17 15:29:32 -07003763SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3764 audio_devices_t device,
3765 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003766{
3767 SortedVector<audio_io_handle_t> outputs;
3768
3769 ALOGVV("getOutputsForDevice() device %04x", device);
3770 for (size_t i = 0; i < openOutputs.size(); i++) {
3771 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003772 i, openOutputs.valueAt(i)->isDuplicated(),
3773 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003774 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3775 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3776 outputs.add(openOutputs.keyAt(i));
3777 }
3778 }
3779 return outputs;
3780}
3781
Eric Laurente0720872014-03-11 09:30:41 -07003782bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003783 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003784{
3785 if (outputs1.size() != outputs2.size()) {
3786 return false;
3787 }
3788 for (size_t i = 0; i < outputs1.size(); i++) {
3789 if (outputs1[i] != outputs2[i]) {
3790 return false;
3791 }
3792 }
3793 return true;
3794}
3795
Eric Laurente0720872014-03-11 09:30:41 -07003796void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003797{
3798 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3799 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3800 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3801 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3802
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003803 // also take into account external policy-related changes: add all outputs which are
3804 // associated with policies in the "before" and "after" output vectors
3805 ALOGVV("checkOutputForStrategy(): policy related outputs");
3806 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003807 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003808 if (desc != 0 && desc->mPolicyMix != NULL) {
3809 srcOutputs.add(desc->mIoHandle);
3810 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3811 }
3812 }
3813 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003814 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003815 if (desc != 0 && desc->mPolicyMix != NULL) {
3816 dstOutputs.add(desc->mIoHandle);
3817 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3818 }
3819 }
3820
Eric Laurente552edb2014-03-10 17:42:56 -07003821 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3822 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3823 strategy, srcOutputs[0], dstOutputs[0]);
3824 // mute strategy while moving tracks from one output to another
3825 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003826 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003827 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003828 setStrategyMute(strategy, true, desc);
3829 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003830 }
3831 }
3832
3833 // Move effects associated to this strategy from previous output to new output
3834 if (strategy == STRATEGY_MEDIA) {
3835 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3836 SortedVector<audio_io_handle_t> moved;
3837 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003838 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3839 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3840 effectDesc->mIo != fxOutput) {
3841 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003842 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3843 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003844 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003845 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003846 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003847 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003848 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003849 }
3850 }
3851 }
3852 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003853 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003854 if (i == AUDIO_STREAM_PATCH) {
3855 continue;
3856 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003857 if (getStrategy((audio_stream_type_t)i) == strategy) {
3858 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003859 }
3860 }
3861 }
3862}
3863
Eric Laurente0720872014-03-11 09:30:41 -07003864void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003865{
François Gaffie2110e042015-03-24 08:41:51 +01003866 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003867 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003868 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003869 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003870 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003871 checkOutputForStrategy(STRATEGY_SONIFICATION);
3872 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003873 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003874 checkOutputForStrategy(STRATEGY_MEDIA);
3875 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003876 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003877}
3878
Eric Laurente0720872014-03-11 09:30:41 -07003879void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003880{
François Gaffie53615e22015-03-19 09:24:12 +01003881 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003882 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003883 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003884 return;
3885 }
3886
Eric Laurent3a4311c2014-03-17 12:00:47 -07003887 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003888 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3889 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3890 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003891 // suspend A2DP output if:
3892 // (NOT already suspended) &&
3893 // ((SCO device is connected &&
3894 // (forced usage for communication || for record is SCO))) ||
3895 // (phone state is ringing || in call)
3896 //
3897 // restore A2DP output if:
3898 // (Already suspended) &&
3899 // ((SCO device is NOT connected ||
3900 // (forced usage NOT for communication && NOT for record is SCO))) &&
3901 // (phone state is NOT ringing && NOT in call)
3902 //
3903 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003904 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003905 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3906 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3907 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3908 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003909
3910 mpClientInterface->restoreOutput(a2dpOutput);
3911 mA2dpSuspended = false;
3912 }
3913 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003914 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003915 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3916 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3917 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3918 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003919
3920 mpClientInterface->suspendOutput(a2dpOutput);
3921 mA2dpSuspended = true;
3922 }
3923 }
3924}
3925
Eric Laurentc75307b2015-03-17 15:29:32 -07003926audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3927 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003928{
3929 audio_devices_t device = AUDIO_DEVICE_NONE;
3930
Eric Laurent6a94d692014-05-20 11:18:06 -07003931 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3932 if (index >= 0) {
3933 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3934 if (patchDesc->mUid != mUidCached) {
3935 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3936 outputDesc->device(), outputDesc->mPatchHandle);
3937 return outputDesc->device();
3938 }
3939 }
3940
Eric Laurente552edb2014-03-10 17:42:56 -07003941 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003942 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003943 // use device for strategy enforced audible
3944 // 2: we are in call or the strategy phone is active on the output:
3945 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003946 // 3: the strategy for enforced audible is active but not enforced on the output:
3947 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003948 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08003949 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003950 // 5: the strategy sonification is active on the output:
3951 // use device for strategy sonification
3952 // 6: the strategy "respectful" sonification is active on the output:
3953 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003954 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003955 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003956 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003957 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003958 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003959 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003960 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003961 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003962 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3963 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003964 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003965 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003966 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003967 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003968 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
3969 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003970 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003971 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003972 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003973 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003974 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003975 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003976 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003977 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003978 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003979 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003980 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003981 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003982 }
3983
Eric Laurent1c333e22014-05-20 10:48:17 -07003984 ALOGV("getNewOutputDevice() selected device %x", device);
3985 return device;
3986}
3987
3988audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3989{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003990 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003991
3992 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3993 if (index >= 0) {
3994 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3995 if (patchDesc->mUid != mUidCached) {
3996 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3997 inputDesc->mDevice, inputDesc->mPatchHandle);
3998 return inputDesc->mDevice;
3999 }
4000 }
4001
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004002 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07004003
Eric Laurente552edb2014-03-10 17:42:56 -07004004 return device;
4005}
4006
Eric Laurente0720872014-03-11 09:30:41 -07004007uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004008 return (uint32_t)getStrategy(stream);
4009}
4010
Eric Laurente0720872014-03-11 09:30:41 -07004011audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004012 // By checking the range of stream before calling getStrategy, we avoid
4013 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4014 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004015 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004016 return AUDIO_DEVICE_NONE;
4017 }
4018 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004019 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004020 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4021 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4022 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004023 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004024 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004025 devices = outputDesc->device();
4026 break;
4027 }
Eric Laurente552edb2014-03-10 17:42:56 -07004028 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004029
4030 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4031 and doesn't really need to.*/
4032 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4033 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4034 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4035 }
4036
Eric Laurente552edb2014-03-10 17:42:56 -07004037 return devices;
4038}
4039
François Gaffie2110e042015-03-24 08:41:51 +01004040routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4041{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004042 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004043 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004044}
4045
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004046uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4047 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004048 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4049 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4050 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004051 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4052 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4053 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004054 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004055 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004056}
4057
Eric Laurente0720872014-03-11 09:30:41 -07004058void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004059 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004060 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004061 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4062 updateDevicesAndOutputs();
4063 break;
4064 default:
4065 break;
4066 }
4067}
4068
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004069uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004070
4071 // skip beacon mute management if a dedicated TTS output is available
4072 if (mTtsOutputAvailable) {
4073 return 0;
4074 }
4075
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004076 switch(event) {
4077 case STARTING_OUTPUT:
4078 mBeaconMuteRefCount++;
4079 break;
4080 case STOPPING_OUTPUT:
4081 if (mBeaconMuteRefCount > 0) {
4082 mBeaconMuteRefCount--;
4083 }
4084 break;
4085 case STARTING_BEACON:
4086 mBeaconPlayingRefCount++;
4087 break;
4088 case STOPPING_BEACON:
4089 if (mBeaconPlayingRefCount > 0) {
4090 mBeaconPlayingRefCount--;
4091 }
4092 break;
4093 }
4094
4095 if (mBeaconMuteRefCount > 0) {
4096 // any playback causes beacon to be muted
4097 return setBeaconMute(true);
4098 } else {
4099 // no other playback: unmute when beacon starts playing, mute when it stops
4100 return setBeaconMute(mBeaconPlayingRefCount == 0);
4101 }
4102}
4103
4104uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4105 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4106 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4107 // keep track of muted state to avoid repeating mute/unmute operations
4108 if (mBeaconMuted != mute) {
4109 // mute/unmute AUDIO_STREAM_TTS on all outputs
4110 ALOGV("\t muting %d", mute);
4111 uint32_t maxLatency = 0;
4112 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004113 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004114 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004115 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004116 0 /*delay*/, AUDIO_DEVICE_NONE);
4117 const uint32_t latency = desc->latency() * 2;
4118 if (latency > maxLatency) {
4119 maxLatency = latency;
4120 }
4121 }
4122 mBeaconMuted = mute;
4123 return maxLatency;
4124 }
4125 return 0;
4126}
4127
Eric Laurente0720872014-03-11 09:30:41 -07004128audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004129 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004130{
Paul McLeanaa981192015-03-21 09:55:15 -07004131 // Routing
4132 // see if we have an explicit route
4133 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4134 // (getStrategy(stream)).
4135 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4136 // and activity count is non-zero
4137 // the device = the device from the descriptor in the RouteMap, and exit.
4138 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4139 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4140 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004141 // Special case for accessibility strategy which must follow any strategy it is
4142 // currently remapped to
4143 bool strategyMatch = (strat == strategy) ||
4144 ((strategy == STRATEGY_ACCESSIBILITY) &&
4145 ((mEngine->getStrategyForUsage(
4146 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4147 (strat == STRATEGY_MEDIA)));
4148 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004149 return route->mDeviceDescriptor->type();
4150 }
4151 }
4152
Eric Laurente552edb2014-03-10 17:42:56 -07004153 if (fromCache) {
4154 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4155 strategy, mDeviceForStrategy[strategy]);
4156 return mDeviceForStrategy[strategy];
4157 }
François Gaffie2110e042015-03-24 08:41:51 +01004158 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004159}
4160
Eric Laurente0720872014-03-11 09:30:41 -07004161void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004162{
4163 for (int i = 0; i < NUM_STRATEGIES; i++) {
4164 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4165 }
4166 mPreviousOutputs = mOutputs;
4167}
4168
Eric Laurent1f2f2232014-06-02 12:01:23 -07004169uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004170 audio_devices_t prevDevice,
4171 uint32_t delayMs)
4172{
4173 // mute/unmute strategies using an incompatible device combination
4174 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4175 // if unmuting, unmute only after the specified delay
4176 if (outputDesc->isDuplicated()) {
4177 return 0;
4178 }
4179
4180 uint32_t muteWaitMs = 0;
4181 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004182 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004183
4184 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4185 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004186 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004187 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4188 bool doMute = false;
4189
4190 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4191 doMute = true;
4192 outputDesc->mStrategyMutedByDevice[i] = true;
4193 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4194 doMute = true;
4195 outputDesc->mStrategyMutedByDevice[i] = false;
4196 }
Eric Laurent99401132014-05-07 19:48:15 -07004197 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004198 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004199 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004200 // skip output if it does not share any device with current output
4201 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4202 == AUDIO_DEVICE_NONE) {
4203 continue;
4204 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004205 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4206 mute ? "muting" : "unmuting", i, curDevice);
4207 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004208 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004209 if (mute) {
4210 // FIXME: should not need to double latency if volume could be applied
4211 // immediately by the audioflinger mixer. We must account for the delay
4212 // between now and the next time the audioflinger thread for this output
4213 // will process a buffer (which corresponds to one buffer size,
4214 // usually 1/2 or 1/4 of the latency).
4215 if (muteWaitMs < desc->latency() * 2) {
4216 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004217 }
4218 }
4219 }
4220 }
4221 }
4222 }
4223
Eric Laurent99401132014-05-07 19:48:15 -07004224 // temporary mute output if device selection changes to avoid volume bursts due to
4225 // different per device volumes
4226 if (outputDesc->isActive() && (device != prevDevice)) {
4227 if (muteWaitMs < outputDesc->latency() * 2) {
4228 muteWaitMs = outputDesc->latency() * 2;
4229 }
4230 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004231 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004232 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004233 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004234 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004235 muteWaitMs *2, device);
4236 }
4237 }
4238 }
4239
Eric Laurente552edb2014-03-10 17:42:56 -07004240 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4241 if (muteWaitMs > delayMs) {
4242 muteWaitMs -= delayMs;
4243 usleep(muteWaitMs * 1000);
4244 return muteWaitMs;
4245 }
4246 return 0;
4247}
4248
Eric Laurentc75307b2015-03-17 15:29:32 -07004249uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004250 audio_devices_t device,
4251 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004252 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004253 audio_patch_handle_t *patchHandle,
4254 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004255{
Eric Laurentc75307b2015-03-17 15:29:32 -07004256 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004257 AudioParameter param;
4258 uint32_t muteWaitMs;
4259
4260 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004261 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4262 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004263 return muteWaitMs;
4264 }
4265 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4266 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004267 if ((device != AUDIO_DEVICE_NONE) &&
4268 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004269 return 0;
4270 }
4271
4272 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004273 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004274
4275 audio_devices_t prevDevice = outputDesc->mDevice;
4276
Paul McLeanaa981192015-03-21 09:55:15 -07004277 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004278
4279 if (device != AUDIO_DEVICE_NONE) {
4280 outputDesc->mDevice = device;
4281 }
4282 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4283
4284 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004285 // the requested device is AUDIO_DEVICE_NONE
4286 // OR the requested device is the same as current device
4287 // AND force is not specified
4288 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004289 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004290 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4291 !force &&
4292 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004293 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004294 return muteWaitMs;
4295 }
4296
4297 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004298
Eric Laurente552edb2014-03-10 17:42:56 -07004299 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004300 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004301 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004302 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004303 DeviceVector deviceList = (address == NULL) ?
4304 mAvailableOutputDevices.getDevicesFromType(device)
4305 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 if (!deviceList.isEmpty()) {
4307 struct audio_patch patch;
4308 outputDesc->toAudioPortConfig(&patch.sources[0]);
4309 patch.num_sources = 1;
4310 patch.num_sinks = 0;
4311 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4312 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004313 patch.num_sinks++;
4314 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004315 ssize_t index;
4316 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4317 index = mAudioPatches.indexOfKey(*patchHandle);
4318 } else {
4319 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4320 }
4321 sp< AudioPatch> patchDesc;
4322 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4323 if (index >= 0) {
4324 patchDesc = mAudioPatches.valueAt(index);
4325 afPatchHandle = patchDesc->mAfPatchHandle;
4326 }
4327
Eric Laurent1c333e22014-05-20 10:48:17 -07004328 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004329 &afPatchHandle,
4330 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004331 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4332 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004333 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004334 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004335 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004336 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004337 addAudioPatch(patchDesc->mHandle, patchDesc);
4338 } else {
4339 patchDesc->mPatch = patch;
4340 }
4341 patchDesc->mAfPatchHandle = afPatchHandle;
4342 patchDesc->mUid = mUidCached;
4343 if (patchHandle) {
4344 *patchHandle = patchDesc->mHandle;
4345 }
4346 outputDesc->mPatchHandle = patchDesc->mHandle;
4347 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004348 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004349 }
4350 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004351
4352 // inform all input as well
4353 for (size_t i = 0; i < mInputs.size(); i++) {
4354 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004355 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004356 AudioParameter inputCmd = AudioParameter();
4357 ALOGV("%s: inform input %d of device:%d", __func__,
4358 inputDescriptor->mIoHandle, device);
4359 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4360 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4361 inputCmd.toString(),
4362 delayMs);
4363 }
4364 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004365 }
Eric Laurente552edb2014-03-10 17:42:56 -07004366
4367 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004368 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004369
4370 return muteWaitMs;
4371}
4372
Eric Laurentc75307b2015-03-17 15:29:32 -07004373status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004374 int delayMs,
4375 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004376{
Eric Laurent6a94d692014-05-20 11:18:06 -07004377 ssize_t index;
4378 if (patchHandle) {
4379 index = mAudioPatches.indexOfKey(*patchHandle);
4380 } else {
4381 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4382 }
4383 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004384 return INVALID_OPERATION;
4385 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004386 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4387 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004388 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4389 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004390 removeAudioPatch(patchDesc->mHandle);
4391 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004392 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004393 return status;
4394}
4395
4396status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4397 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004398 bool force,
4399 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004400{
4401 status_t status = NO_ERROR;
4402
Eric Laurent1f2f2232014-06-02 12:01:23 -07004403 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004404 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4405 inputDesc->mDevice = device;
4406
4407 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4408 if (!deviceList.isEmpty()) {
4409 struct audio_patch patch;
4410 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004411 // AUDIO_SOURCE_HOTWORD is for internal use only:
4412 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004413 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4414 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004415 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4416 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004417 patch.num_sinks = 1;
4418 //only one input device for now
4419 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004420 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004421 ssize_t index;
4422 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4423 index = mAudioPatches.indexOfKey(*patchHandle);
4424 } else {
4425 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4426 }
4427 sp< AudioPatch> patchDesc;
4428 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4429 if (index >= 0) {
4430 patchDesc = mAudioPatches.valueAt(index);
4431 afPatchHandle = patchDesc->mAfPatchHandle;
4432 }
4433
Eric Laurent1c333e22014-05-20 10:48:17 -07004434 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004435 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004436 0);
4437 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004438 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004439 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004440 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004441 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004442 addAudioPatch(patchDesc->mHandle, patchDesc);
4443 } else {
4444 patchDesc->mPatch = patch;
4445 }
4446 patchDesc->mAfPatchHandle = afPatchHandle;
4447 patchDesc->mUid = mUidCached;
4448 if (patchHandle) {
4449 *patchHandle = patchDesc->mHandle;
4450 }
4451 inputDesc->mPatchHandle = patchDesc->mHandle;
4452 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004453 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004454 }
4455 }
4456 }
4457 return status;
4458}
4459
Eric Laurent6a94d692014-05-20 11:18:06 -07004460status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4461 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004462{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004463 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004464 ssize_t index;
4465 if (patchHandle) {
4466 index = mAudioPatches.indexOfKey(*patchHandle);
4467 } else {
4468 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4469 }
4470 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004471 return INVALID_OPERATION;
4472 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004473 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4474 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004475 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4476 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004477 removeAudioPatch(patchDesc->mHandle);
4478 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004479 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004480 return status;
4481}
4482
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004483sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004484 String8 address,
4485 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004486 audio_format_t& format,
4487 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004488 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004489{
4490 // Choose an input profile based on the requested capture parameters: select the first available
4491 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004492 //
4493 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4494 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004495
4496 for (size_t i = 0; i < mHwModules.size(); i++)
4497 {
4498 if (mHwModules[i]->mHandle == 0) {
4499 continue;
4500 }
4501 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4502 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004503 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004504 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004505 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004506 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004507 format,
4508 &format /*updatedFormat*/,
4509 channelMask,
4510 &channelMask /*updatedChannelMask*/,
4511 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004512
Eric Laurente552edb2014-03-10 17:42:56 -07004513 return profile;
4514 }
4515 }
4516 }
4517 return NULL;
4518}
4519
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004520
4521audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004522 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004523{
François Gaffie036e1e92015-03-19 10:16:24 +01004524 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4525 audio_devices_t selectedDeviceFromMix =
4526 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004527
François Gaffie036e1e92015-03-19 10:16:24 +01004528 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4529 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004530 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004531 return getDeviceForInputSource(inputSource);
4532}
4533
4534audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4535{
Paul McLean466dc8e2015-04-17 13:15:36 -06004536 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4537 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004538 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004539 return route->mDeviceDescriptor->type();
4540 }
4541 }
4542
4543 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004544}
4545
Eric Laurente0720872014-03-11 09:30:41 -07004546float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004547 int index,
4548 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004549{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004550 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004551
4552 // if a headset is connected, apply the following rules to ring tones and notifications
4553 // to avoid sound level bursts in user's ears:
4554 // - always attenuate ring tones and notifications volume by 6dB
4555 // - if music is playing, always limit the volume to current music volume,
4556 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004557 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004558 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4559 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4560 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4561 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4562 ((stream_strategy == STRATEGY_SONIFICATION)
4563 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004564 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004565 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004566 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4567 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004568 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004569 // when the phone is ringing we must consider that music could have been paused just before
4570 // by the music application and behave as if music was active if the last music track was
4571 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004572 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004573 mLimitRingtoneVolume) {
4574 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004575 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4576 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004577 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004578 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4579 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4580 if (volumeDb > minVolDB) {
4581 volumeDb = minVolDB;
4582 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004583 }
4584 }
4585 }
4586
Eric Laurentffbc80f2015-03-18 18:30:19 -07004587 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004588}
4589
Eric Laurente0720872014-03-11 09:30:41 -07004590status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004591 int index,
4592 const sp<AudioOutputDescriptor>& outputDesc,
4593 audio_devices_t device,
4594 int delayMs,
4595 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004596{
Eric Laurente552edb2014-03-10 17:42:56 -07004597 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004598 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004599 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004600 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004601 return NO_ERROR;
4602 }
François Gaffie2110e042015-03-24 08:41:51 +01004603 audio_policy_forced_cfg_t forceUseForComm =
4604 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004605 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004606 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4607 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004608 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004609 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004610 return INVALID_OPERATION;
4611 }
4612
Eric Laurentc75307b2015-03-17 15:29:32 -07004613 if (device == AUDIO_DEVICE_NONE) {
4614 device = outputDesc->device();
4615 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004616
Eric Laurentffbc80f2015-03-18 18:30:19 -07004617 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004618 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004619 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004620 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004621
Eric Laurentffbc80f2015-03-18 18:30:19 -07004622 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004623
Eric Laurent3b73df72014-03-11 09:06:29 -07004624 if (stream == AUDIO_STREAM_VOICE_CALL ||
4625 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004626 float voiceVolume;
4627 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004628 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004629 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004630 } else {
4631 voiceVolume = 1.0;
4632 }
4633
Eric Laurentc75307b2015-03-17 15:29:32 -07004634 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004635 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4636 mLastVoiceVolume = voiceVolume;
4637 }
4638 }
4639
4640 return NO_ERROR;
4641}
4642
Eric Laurentc75307b2015-03-17 15:29:32 -07004643void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4644 audio_devices_t device,
4645 int delayMs,
4646 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004647{
Eric Laurentc75307b2015-03-17 15:29:32 -07004648 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004649
Eric Laurent3b73df72014-03-11 09:06:29 -07004650 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004651 if (stream == AUDIO_STREAM_PATCH) {
4652 continue;
4653 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004654 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004655 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4656 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004657 device,
4658 delayMs,
4659 force);
4660 }
4661}
4662
Eric Laurente0720872014-03-11 09:30:41 -07004663void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004664 bool on,
4665 const sp<AudioOutputDescriptor>& outputDesc,
4666 int delayMs,
4667 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004668{
Eric Laurent72249d52015-06-08 11:12:15 -07004669 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4670 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004671 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004672 if (stream == AUDIO_STREAM_PATCH) {
4673 continue;
4674 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004675 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004676 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004677 }
4678 }
4679}
4680
Eric Laurente0720872014-03-11 09:30:41 -07004681void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004682 bool on,
4683 const sp<AudioOutputDescriptor>& outputDesc,
4684 int delayMs,
4685 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004686{
Eric Laurentc75307b2015-03-17 15:29:32 -07004687 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004688 if (device == AUDIO_DEVICE_NONE) {
4689 device = outputDesc->device();
4690 }
4691
Eric Laurentc75307b2015-03-17 15:29:32 -07004692 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4693 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004694
4695 if (on) {
4696 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004697 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004698 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004699 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004700 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004701 }
4702 }
4703 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4704 outputDesc->mMuteCount[stream]++;
4705 } else {
4706 if (outputDesc->mMuteCount[stream] == 0) {
4707 ALOGV("setStreamMute() unmuting non muted stream!");
4708 return;
4709 }
4710 if (--outputDesc->mMuteCount[stream] == 0) {
4711 checkAndSetVolume(stream,
4712 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004713 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004714 device,
4715 delayMs);
4716 }
4717 }
4718}
4719
Eric Laurente0720872014-03-11 09:30:41 -07004720void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004721 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004722{
Eric Laurent87ffa392015-05-22 10:32:38 -07004723 if(!hasPrimaryOutput()) {
4724 return;
4725 }
4726
Eric Laurente552edb2014-03-10 17:42:56 -07004727 // if the stream pertains to sonification strategy and we are in call we must
4728 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4729 // in the device used for phone strategy and play the tone if the selected device does not
4730 // interfere with the device used for phone strategy
4731 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4732 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004733 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004734 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4735 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004736 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004737 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4738 stream, starting, outputDesc->mDevice, stateChange);
4739 if (outputDesc->mRefCount[stream]) {
4740 int muteCount = 1;
4741 if (stateChange) {
4742 muteCount = outputDesc->mRefCount[stream];
4743 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004744 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004745 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4746 for (int i = 0; i < muteCount; i++) {
4747 setStreamMute(stream, starting, mPrimaryOutput);
4748 }
4749 } else {
4750 ALOGV("handleIncallSonification() high visibility");
4751 if (outputDesc->device() &
4752 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4753 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4754 for (int i = 0; i < muteCount; i++) {
4755 setStreamMute(stream, starting, mPrimaryOutput);
4756 }
4757 }
4758 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004759 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4760 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004761 } else {
4762 mpClientInterface->stopTone();
4763 }
4764 }
4765 }
4766 }
4767}
4768
Paul McLeanaa981192015-03-21 09:55:15 -07004769
Paul McLean466dc8e2015-04-17 13:15:36 -06004770
Eric Laurente0720872014-03-11 09:30:41 -07004771void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004772{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004773 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004774 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004775 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004776 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004777 mAvailableOutputDevices.add(mDefaultOutputDevice);
4778 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004779
4780 module = new HwModule("primary");
4781
Eric Laurent322b4d22015-04-03 15:57:54 -07004782 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4783 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004784 profile->mSamplingRates.add(44100);
4785 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4786 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004787 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004788 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4789 module->mOutputProfiles.add(profile);
4790
Eric Laurent322b4d22015-04-03 15:57:54 -07004791 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4792 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004793 profile->mSamplingRates.add(8000);
4794 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4795 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004796 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004797 module->mInputProfiles.add(profile);
4798
4799 mHwModules.add(module);
4800}
4801
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004802audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4803{
4804 // flags to stream type mapping
4805 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4806 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4807 }
4808 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4809 return AUDIO_STREAM_BLUETOOTH_SCO;
4810 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004811 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4812 return AUDIO_STREAM_TTS;
4813 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004814
4815 // usage to stream type mapping
4816 switch (attr->usage) {
4817 case AUDIO_USAGE_MEDIA:
4818 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004819 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4820 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004821 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004822 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4823 return AUDIO_STREAM_ALARM;
4824 }
4825 if (isStreamActive(AUDIO_STREAM_RING)) {
4826 return AUDIO_STREAM_RING;
4827 }
4828 if (isInCall()) {
4829 return AUDIO_STREAM_VOICE_CALL;
4830 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004831 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004832 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4833 return AUDIO_STREAM_SYSTEM;
4834 case AUDIO_USAGE_VOICE_COMMUNICATION:
4835 return AUDIO_STREAM_VOICE_CALL;
4836
4837 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4838 return AUDIO_STREAM_DTMF;
4839
4840 case AUDIO_USAGE_ALARM:
4841 return AUDIO_STREAM_ALARM;
4842 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4843 return AUDIO_STREAM_RING;
4844
4845 case AUDIO_USAGE_NOTIFICATION:
4846 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4847 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4848 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4849 case AUDIO_USAGE_NOTIFICATION_EVENT:
4850 return AUDIO_STREAM_NOTIFICATION;
4851
4852 case AUDIO_USAGE_UNKNOWN:
4853 default:
4854 return AUDIO_STREAM_MUSIC;
4855 }
4856}
Eric Laurente83b55d2014-11-14 10:06:21 -08004857
François Gaffie53615e22015-03-19 09:24:12 +01004858bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4859{
Eric Laurente83b55d2014-11-14 10:06:21 -08004860 // has flags that map to a strategy?
4861 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4862 return true;
4863 }
4864
4865 // has known usage?
4866 switch (paa->usage) {
4867 case AUDIO_USAGE_UNKNOWN:
4868 case AUDIO_USAGE_MEDIA:
4869 case AUDIO_USAGE_VOICE_COMMUNICATION:
4870 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4871 case AUDIO_USAGE_ALARM:
4872 case AUDIO_USAGE_NOTIFICATION:
4873 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4874 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4875 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4876 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4877 case AUDIO_USAGE_NOTIFICATION_EVENT:
4878 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4879 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4880 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4881 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004882 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004883 break;
4884 default:
4885 return false;
4886 }
4887 return true;
4888}
4889
François Gaffiead3183e2015-03-18 16:55:35 +01004890bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4891 routing_strategy strategy, uint32_t inPastMs,
4892 nsecs_t sysTime) const
4893{
4894 if ((sysTime == 0) && (inPastMs != 0)) {
4895 sysTime = systemTime();
4896 }
4897 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4898 if (i == AUDIO_STREAM_PATCH) {
4899 continue;
4900 }
4901 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4902 (NUM_STRATEGIES == strategy)) &&
4903 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4904 return true;
4905 }
4906 }
4907 return false;
4908}
4909
François Gaffie2110e042015-03-24 08:41:51 +01004910audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4911{
4912 return mEngine->getForceUse(usage);
4913}
4914
4915bool AudioPolicyManager::isInCall()
4916{
4917 return isStateInCall(mEngine->getPhoneState());
4918}
4919
4920bool AudioPolicyManager::isStateInCall(int state)
4921{
4922 return is_state_in_call(state);
4923}
4924
Eric Laurente552edb2014-03-10 17:42:56 -07004925}; // namespace android