blob: aa4486d4a86d6e8dee305a27838c37bd466fecbf [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
405 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
406 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
407 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
408 status);
409 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100410 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 mCallTxPatch->mAfPatchHandle = afPatchHandle;
412 mCallTxPatch->mUid = mUidCached;
413 }
414 }
415}
416
Eric Laurente0720872014-03-11 09:30:41 -0700417void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700418{
419 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100420 // store previous phone state for management of sonification strategy below
421 int oldState = mEngine->getPhoneState();
422
423 if (mEngine->setPhoneState(state) != NO_ERROR) {
424 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700425 return;
426 }
François Gaffie2110e042015-03-24 08:41:51 +0100427 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700428 // if leaving call state, handle special case of active streams
429 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700430 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700431 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (stream == AUDIO_STREAM_PATCH) {
434 continue;
435 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700436 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800438
Eric Laurent63dea1d2015-07-02 17:10:28 -0700439 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800440 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 }
442
François Gaffie2110e042015-03-24 08:41:51 +0100443 /**
444 * Switching to or from incall state or switching between telephony and VoIP lead to force
445 * routing command.
446 */
447 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
448 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700449
450 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700451 checkA2dpSuspend();
452 checkOutputForAllStrategies();
453 updateDevicesAndOutputs();
454
Eric Laurente552edb2014-03-10 17:42:56 -0700455 int delayMs = 0;
456 if (isStateInCall(state)) {
457 nsecs_t sysTime = systemTime();
458 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700459 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // mute media and sonification strategies and delay device switch by the largest
461 // latency of any output where either strategy is active.
462 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100463 if ((isStrategyActive(desc, STRATEGY_MEDIA,
464 SONIFICATION_HEADSET_MUSIC_DELAY,
465 sysTime) ||
466 isStrategyActive(desc, STRATEGY_SONIFICATION,
467 SONIFICATION_HEADSET_MUSIC_DELAY,
468 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700469 (delayMs < (int)desc->latency()*2)) {
470 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700472 setStrategyMute(STRATEGY_MEDIA, true, desc);
473 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700474 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700475 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
476 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700477 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
478 }
479 }
480
Eric Laurent87ffa392015-05-22 10:32:38 -0700481 if (hasPrimaryOutput()) {
482 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
483 // the device returned is not necessarily reachable via this output
484 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
485 // force routing command to audio hardware when ending call
486 // even if no device change is needed
487 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
488 rxDevice = mPrimaryOutput->device();
489 }
Eric Laurente552edb2014-03-10 17:42:56 -0700490
Eric Laurent87ffa392015-05-22 10:32:38 -0700491 if (state == AUDIO_MODE_IN_CALL) {
492 updateCallRouting(rxDevice, delayMs);
493 } else if (oldState == AUDIO_MODE_IN_CALL) {
494 if (mCallRxPatch != 0) {
495 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
496 mCallRxPatch.clear();
497 }
498 if (mCallTxPatch != 0) {
499 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
500 mCallTxPatch.clear();
501 }
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 } else {
504 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700506 }
Eric Laurente552edb2014-03-10 17:42:56 -0700507 // if entering in call state, handle special case of active streams
508 // pertaining to sonification strategy see handleIncallSonification()
509 if (isStateInCall(state)) {
510 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800512 if (stream == AUDIO_STREAM_PATCH) {
513 continue;
514 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700515 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700517
518 // force reevaluating accessibility routing when call starts
519 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700520 }
521
522 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700523 if (state == AUDIO_MODE_RINGTONE &&
524 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700525 mLimitRingtoneVolume = true;
526 } else {
527 mLimitRingtoneVolume = false;
528 }
529}
530
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700531audio_mode_t AudioPolicyManager::getPhoneState() {
532 return mEngine->getPhoneState();
533}
534
Eric Laurente0720872014-03-11 09:30:41 -0700535void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700536 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700537{
François Gaffie2110e042015-03-24 08:41:51 +0100538 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700539
François Gaffie2110e042015-03-24 08:41:51 +0100540 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
541 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
542 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700543 }
François Gaffie2110e042015-03-24 08:41:51 +0100544 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
545 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
546 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700547
548 // check for device and output changes triggered by new force usage
549 checkA2dpSuspend();
550 checkOutputForAllStrategies();
551 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700552 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700553 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
554 updateCallRouting(newDevice);
555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
558 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
559 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
560 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700561 }
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700563 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565 }
566
François Gaffie53615e22015-03-19 09:24:12 +0100567 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700568 if (activeInput != 0) {
Eric Laurentc171c7c2015-09-25 12:21:06 -0700569 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
570 audio_devices_t newDevice = getNewInputDevice(activeInput);
571 // Force new input selection if the new device can not be reached via current input
572 if (activeDesc->mProfile->mSupportedDevices.types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
573 setInputDevice(activeInput, newDevice);
574 } else {
575 closeInput(activeInput);
576 }
Eric Laurente552edb2014-03-10 17:42:56 -0700577 }
Eric Laurente552edb2014-03-10 17:42:56 -0700578}
579
Eric Laurente0720872014-03-11 09:30:41 -0700580void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700581{
582 ALOGV("setSystemProperty() property %s, value %s", property, value);
583}
584
585// Find a direct output profile compatible with the parameters passed, even if the input flags do
586// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800587sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700588 audio_devices_t device,
589 uint32_t samplingRate,
590 audio_format_t format,
591 audio_channel_mask_t channelMask,
592 audio_output_flags_t flags)
593{
Eric Laurent861a6282015-05-18 15:40:16 -0700594 // only retain flags that will drive the direct output profile selection
595 // if explicitly requested
596 static const uint32_t kRelevantFlags =
597 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
598 flags =
599 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
600
601 sp<IOProfile> profile;
602
Eric Laurente552edb2014-03-10 17:42:56 -0700603 for (size_t i = 0; i < mHwModules.size(); i++) {
604 if (mHwModules[i]->mHandle == 0) {
605 continue;
606 }
607 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700608 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
609 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700610 samplingRate, NULL /*updatedSamplingRate*/,
611 format, NULL /*updatedFormat*/,
612 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700613 flags)) {
614 continue;
615 }
616 // reject profiles not corresponding to a device currently available
617 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
618 continue;
619 }
620 // if several profiles are compatible, give priority to one with offload capability
621 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
622 continue;
623 }
624 profile = curProfile;
625 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
626 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700627 }
Eric Laurente552edb2014-03-10 17:42:56 -0700628 }
629 }
Eric Laurent861a6282015-05-18 15:40:16 -0700630 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700631}
632
Eric Laurente0720872014-03-11 09:30:41 -0700633audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100634 uint32_t samplingRate,
635 audio_format_t format,
636 audio_channel_mask_t channelMask,
637 audio_output_flags_t flags,
638 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700639{
Eric Laurent3b73df72014-03-11 09:06:29 -0700640 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700641 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
642 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
643 device, stream, samplingRate, format, channelMask, flags);
644
Eric Laurente83b55d2014-11-14 10:06:21 -0800645 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
646 stream, samplingRate,format, channelMask,
647 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700648}
649
Eric Laurente83b55d2014-11-14 10:06:21 -0800650status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
651 audio_io_handle_t *output,
652 audio_session_t session,
653 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700654 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800655 uint32_t samplingRate,
656 audio_format_t format,
657 audio_channel_mask_t channelMask,
658 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700659 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800660 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700661{
Eric Laurente83b55d2014-11-14 10:06:21 -0800662 audio_attributes_t attributes;
663 if (attr != NULL) {
664 if (!isValidAttributes(attr)) {
665 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
666 attr->usage, attr->content_type, attr->flags,
667 attr->tags);
668 return BAD_VALUE;
669 }
670 attributes = *attr;
671 } else {
672 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
673 ALOGE("getOutputForAttr(): invalid stream type");
674 return BAD_VALUE;
675 }
676 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700677 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700678 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100679 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
680 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
681 if (!audio_is_linear_pcm(format)) {
682 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800683 }
François Gaffie036e1e92015-03-19 10:16:24 +0100684 *stream = streamTypefromAttributesInt(&attributes);
685 *output = desc->mIoHandle;
686 ALOGV("getOutputForAttr() returns output %d", *output);
687 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800688 }
689 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
690 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
691 return BAD_VALUE;
692 }
693
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700694 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
695 " session %d selectedDeviceId %d",
696 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
697 session, selectedDeviceId);
698
699 *stream = streamTypefromAttributesInt(&attributes);
700
701 // Explicit routing?
702 sp<DeviceDescriptor> deviceDesc;
703 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
704 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
705 deviceDesc = mAvailableOutputDevices[i];
706 break;
707 }
708 }
709 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710
Eric Laurente83b55d2014-11-14 10:06:21 -0800711 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700712 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700713
Eric Laurente83b55d2014-11-14 10:06:21 -0800714 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700715 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
716 }
717
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700718 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700719 device, samplingRate, format, channelMask, flags);
720
Eric Laurente83b55d2014-11-14 10:06:21 -0800721 *output = getOutputForDevice(device, session, *stream,
722 samplingRate, format, channelMask,
723 flags, offloadInfo);
724 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700725 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800726 return INVALID_OPERATION;
727 }
Paul McLeanaa981192015-03-21 09:55:15 -0700728
Eric Laurente83b55d2014-11-14 10:06:21 -0800729 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700730}
731
732audio_io_handle_t AudioPolicyManager::getOutputForDevice(
733 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800734 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700735 audio_stream_type_t stream,
736 uint32_t samplingRate,
737 audio_format_t format,
738 audio_channel_mask_t channelMask,
739 audio_output_flags_t flags,
740 const audio_offload_info_t *offloadInfo)
741{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700742 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700743 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700744 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700745
Eric Laurente552edb2014-03-10 17:42:56 -0700746#ifdef AUDIO_POLICY_TEST
747 if (mCurOutput != 0) {
748 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
749 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
750
751 if (mTestOutputs[mCurOutput] == 0) {
752 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700753 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
754 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700755 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700756 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700757 outputDesc->mFlags =
758 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700759 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700760 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
761 config.sample_rate = mTestSamplingRate;
762 config.channel_mask = mTestChannels;
763 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700764 if (offloadInfo != NULL) {
765 config.offload_info = *offloadInfo;
766 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700767 status = mpClientInterface->openOutput(0,
768 &mTestOutputs[mCurOutput],
769 &config,
770 &outputDesc->mDevice,
771 String8(""),
772 &outputDesc->mLatency,
773 outputDesc->mFlags);
774 if (status == NO_ERROR) {
775 outputDesc->mSamplingRate = config.sample_rate;
776 outputDesc->mFormat = config.format;
777 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700778 AudioParameter outputCmd = AudioParameter();
779 outputCmd.addInt(String8("set_id"),mCurOutput);
780 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
781 addOutput(mTestOutputs[mCurOutput], outputDesc);
782 }
783 }
784 return mTestOutputs[mCurOutput];
785 }
786#endif //AUDIO_POLICY_TEST
787
788 // open a direct output if required by specified parameters
789 //force direct flag if offload flag is set: offloading implies a direct output stream
790 // and all common behaviors are driven by checking only the direct flag
791 // this should normally be set appropriately in the policy configuration file
792 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700793 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700794 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700795 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
796 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
797 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800798 // only allow deep buffering for music stream type
799 if (stream != AUDIO_STREAM_MUSIC) {
800 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700801 } else if (/* stream == AUDIO_STREAM_MUSIC && */
802 flags == AUDIO_OUTPUT_FLAG_NONE &&
803 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
804 // use DEEP_BUFFER as default output for music stream type
805 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800806 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700807 if (stream == AUDIO_STREAM_TTS) {
808 flags = AUDIO_OUTPUT_FLAG_TTS;
809 }
Eric Laurente552edb2014-03-10 17:42:56 -0700810
Eric Laurentb732cf52014-09-24 19:08:21 -0700811 sp<IOProfile> profile;
812
813 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700814 // and not explicitly requested
815 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
816 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700817 audio_channel_count_from_out_mask(channelMask) <= 2) {
818 goto non_direct_output;
819 }
820
Eric Laurente552edb2014-03-10 17:42:56 -0700821 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
822 // creating an offloaded track and tearing it down immediately after start when audioflinger
823 // detects there is an active non offloadable effect.
824 // FIXME: We should check the audio session here but we do not have it in this context.
825 // This may prevent offloading in rare situations where effects are left active by apps
826 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700827
Eric Laurente552edb2014-03-10 17:42:56 -0700828 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100829 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700830 profile = getProfileForDirectOutput(device,
831 samplingRate,
832 format,
833 channelMask,
834 (audio_output_flags_t)flags);
835 }
836
Eric Laurent1c333e22014-05-20 10:48:17 -0700837 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700838 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700839
840 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700841 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700842 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
843 outputDesc = desc;
844 // reuse direct output if currently open and configured with same parameters
845 if ((samplingRate == outputDesc->mSamplingRate) &&
846 (format == outputDesc->mFormat) &&
847 (channelMask == outputDesc->mChannelMask)) {
848 outputDesc->mDirectOpenCount++;
849 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
850 return mOutputs.keyAt(i);
851 }
852 }
853 }
854 // close direct output if currently open and configured with different parameters
855 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700856 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700857 }
Eric Laurent861a6282015-05-18 15:40:16 -0700858
859 // if the selected profile is offloaded and no offload info was specified,
860 // create a default one
861 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
862 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
863 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
864 defaultOffloadInfo.sample_rate = samplingRate;
865 defaultOffloadInfo.channel_mask = channelMask;
866 defaultOffloadInfo.format = format;
867 defaultOffloadInfo.stream_type = stream;
868 defaultOffloadInfo.bit_rate = 0;
869 defaultOffloadInfo.duration_us = -1;
870 defaultOffloadInfo.has_video = true; // conservative
871 defaultOffloadInfo.is_streaming = true; // likely
872 offloadInfo = &defaultOffloadInfo;
873 }
874
Eric Laurentc75307b2015-03-17 15:29:32 -0700875 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700876 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700877 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700878 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700879 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
880 config.sample_rate = samplingRate;
881 config.channel_mask = channelMask;
882 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700883 if (offloadInfo != NULL) {
884 config.offload_info = *offloadInfo;
885 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700886 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700887 &output,
888 &config,
889 &outputDesc->mDevice,
890 String8(""),
891 &outputDesc->mLatency,
892 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700893
894 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700895 if (status != NO_ERROR ||
896 (samplingRate != 0 && samplingRate != config.sample_rate) ||
897 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
898 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700899 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
900 "format %d %d, channelMask %04x %04x", output, samplingRate,
901 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
902 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700903 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700904 mpClientInterface->closeOutput(output);
905 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800906 // fall back to mixer output if possible when the direct output could not be open
907 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
908 goto non_direct_output;
909 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700910 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700911 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700912 outputDesc->mSamplingRate = config.sample_rate;
913 outputDesc->mChannelMask = config.channel_mask;
914 outputDesc->mFormat = config.format;
915 outputDesc->mRefCount[stream] = 0;
916 outputDesc->mStopTime[stream] = 0;
917 outputDesc->mDirectOpenCount = 1;
918
Eric Laurente552edb2014-03-10 17:42:56 -0700919 audio_io_handle_t srcOutput = getOutputForEffect();
920 addOutput(output, outputDesc);
921 audio_io_handle_t dstOutput = getOutputForEffect();
922 if (dstOutput == output) {
923 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
924 }
925 mPreviousOutputs = mOutputs;
926 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700927 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700928 return output;
929 }
930
Eric Laurentb732cf52014-09-24 19:08:21 -0700931non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700932 // ignoring channel mask due to downmix capability in mixer
933
934 // open a non direct output
935
936 // for non direct outputs, only PCM is supported
937 if (audio_is_linear_pcm(format)) {
938 // get which output is suitable for the specified stream. The actual
939 // routing change will happen when startOutput() will be called
940 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
941
Eric Laurent8838a382014-09-08 16:44:28 -0700942 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
943 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
944 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700945 }
946 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
947 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
948
Paul McLeanaa981192015-03-21 09:55:15 -0700949 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700950
951 return output;
952}
953
Eric Laurente0720872014-03-11 09:30:41 -0700954audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700955 audio_output_flags_t flags,
956 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700957{
958 // select one output among several that provide a path to a particular device or set of
959 // devices (the list was previously build by getOutputsForDevice()).
960 // The priority is as follows:
961 // 1: the output with the highest number of requested policy flags
962 // 2: the primary output
963 // 3: the first output in the list
964
965 if (outputs.size() == 0) {
966 return 0;
967 }
968 if (outputs.size() == 1) {
969 return outputs[0];
970 }
971
972 int maxCommonFlags = 0;
973 audio_io_handle_t outputFlags = 0;
974 audio_io_handle_t outputPrimary = 0;
975
976 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700977 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700978 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700979 // if a valid format is specified, skip output if not compatible
980 if (format != AUDIO_FORMAT_INVALID) {
981 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
982 if (format != outputDesc->mFormat) {
983 continue;
984 }
985 } else if (!audio_is_linear_pcm(format)) {
986 continue;
987 }
988 }
989
Eric Laurent3b73df72014-03-11 09:06:29 -0700990 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700991 if (commonFlags > maxCommonFlags) {
992 outputFlags = outputs[i];
993 maxCommonFlags = commonFlags;
994 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
995 }
996 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
997 outputPrimary = outputs[i];
998 }
999 }
1000 }
1001
1002 if (outputFlags != 0) {
1003 return outputFlags;
1004 }
1005 if (outputPrimary != 0) {
1006 return outputPrimary;
1007 }
1008
1009 return outputs[0];
1010}
1011
Eric Laurente0720872014-03-11 09:30:41 -07001012status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001013 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001014 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001015{
Paul McLeanaa981192015-03-21 09:55:15 -07001016 ALOGV("startOutput() output %d, stream %d, session %d",
1017 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001018 ssize_t index = mOutputs.indexOfKey(output);
1019 if (index < 0) {
1020 ALOGW("startOutput() unknown output %d", output);
1021 return BAD_VALUE;
1022 }
1023
Eric Laurentc75307b2015-03-17 15:29:32 -07001024 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1025
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001026 // Routing?
1027 mOutputRoutes.incRouteActivity(session);
1028
Eric Laurentc75307b2015-03-17 15:29:32 -07001029 audio_devices_t newDevice;
1030 if (outputDesc->mPolicyMix != NULL) {
1031 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001032 } else if (mOutputRoutes.hasRouteChanged(session)) {
1033 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001034 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001035 } else {
1036 newDevice = AUDIO_DEVICE_NONE;
1037 }
1038
1039 uint32_t delayMs = 0;
1040
Eric Laurentc75307b2015-03-17 15:29:32 -07001041 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1042
1043 if (status != NO_ERROR) {
1044 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001045 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001046 }
1047 // Automatically enable the remote submix input when output is started on a re routing mix
1048 // of type MIX_TYPE_RECORDERS
1049 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1050 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1051 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1052 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1053 outputDesc->mPolicyMix->mRegistrationId,
1054 "remote-submix");
1055 }
1056
1057 if (delayMs != 0) {
1058 usleep(delayMs * 1000);
1059 }
1060
1061 return status;
1062}
1063
1064status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1065 audio_stream_type_t stream,
1066 audio_devices_t device,
1067 uint32_t *delayMs)
1068{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001069 // cannot start playback of STREAM_TTS if any other output is being used
1070 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001071
1072 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001073 if (stream == AUDIO_STREAM_TTS) {
1074 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001075 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001076 return INVALID_OPERATION;
1077 } else {
1078 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1079 }
1080 } else {
1081 // some playback other than beacon starts
1082 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1083 }
1084
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001085 // check active before incrementing usage count
1086 bool force = !outputDesc->isActive();
1087
Eric Laurente552edb2014-03-10 17:42:56 -07001088 // increment usage count for this stream on the requested output:
1089 // NOTE that the usage count is the same for duplicated output and hardware output which is
1090 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1091 outputDesc->changeRefCount(stream, 1);
1092
Eric Laurent493404d2015-04-21 15:07:36 -07001093 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001094 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001095 if (device == AUDIO_DEVICE_NONE) {
1096 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001097 }
Eric Laurente552edb2014-03-10 17:42:56 -07001098 routing_strategy strategy = getStrategy(stream);
1099 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001100 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1101 (beaconMuteLatency > 0);
1102 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001103 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001104 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001105 if (desc != outputDesc) {
1106 // force a device change if any other output is managed by the same hw
1107 // module and has a current device selection that differs from selected device.
1108 // In this case, the audio HAL must receive the new device selection so that it can
1109 // change the device currently selected by the other active output.
1110 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001111 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001112 force = true;
1113 }
1114 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001115 // a notification so that audio focus effect can propagate, or that a mute/unmute
1116 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001117 uint32_t latency = desc->latency();
1118 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1119 waitMs = latency;
1120 }
1121 }
1122 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001123 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001124
1125 // handle special case for sonification while in call
1126 if (isInCall()) {
1127 handleIncallSonification(stream, true, false);
1128 }
1129
1130 // apply volume rules for current stream and device if necessary
1131 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001132 mStreams.valueFor(stream).getVolumeIndex(device),
1133 outputDesc,
1134 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001135
1136 // update the outputs if starting an output with a stream that can affect notification
1137 // routing
1138 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001139
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001140 // force reevaluating accessibility routing when ringtone or alarm starts
1141 if (strategy == STRATEGY_SONIFICATION) {
1142 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1143 }
Eric Laurente552edb2014-03-10 17:42:56 -07001144 }
1145 return NO_ERROR;
1146}
1147
1148
Eric Laurente0720872014-03-11 09:30:41 -07001149status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001150 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001151 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001152{
1153 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1154 ssize_t index = mOutputs.indexOfKey(output);
1155 if (index < 0) {
1156 ALOGW("stopOutput() unknown output %d", output);
1157 return BAD_VALUE;
1158 }
1159
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001161
Eric Laurentc75307b2015-03-17 15:29:32 -07001162 if (outputDesc->mRefCount[stream] == 1) {
1163 // Automatically disable the remote submix input when output is stopped on a
1164 // re routing mix of type MIX_TYPE_RECORDERS
1165 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1166 outputDesc->mPolicyMix != NULL &&
1167 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1168 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1169 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1170 outputDesc->mPolicyMix->mRegistrationId,
1171 "remote-submix");
1172 }
1173 }
1174
1175 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001176 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001177 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001178 int activityCount = mOutputRoutes.decRouteActivity(session);
1179 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1180
1181 if (forceDeviceUpdate) {
1182 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1183 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001184 }
1185
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001186 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001187}
1188
1189status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001190 audio_stream_type_t stream,
1191 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001192{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001193 // always handle stream stop, check which stream type is stopping
1194 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1195
Eric Laurente552edb2014-03-10 17:42:56 -07001196 // handle special case for sonification while in call
1197 if (isInCall()) {
1198 handleIncallSonification(stream, false, false);
1199 }
1200
1201 if (outputDesc->mRefCount[stream] > 0) {
1202 // decrement usage count of this stream on the output
1203 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001204
Eric Laurente552edb2014-03-10 17:42:56 -07001205 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001206 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001207 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001209 // delay the device switch by twice the latency because stopOutput() is executed when
1210 // the track stop() command is received and at that time the audio track buffer can
1211 // still contain data that needs to be drained. The latency only covers the audio HAL
1212 // and kernel buffers. Also the latency does not always include additional delay in the
1213 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001215
1216 // force restoring the device selection on other active outputs if it differs from the
1217 // one being selected for this output
1218 for (size_t i = 0; i < mOutputs.size(); i++) {
1219 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001220 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001221 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001222 desc->isActive() &&
1223 outputDesc->sharesHwModuleWith(desc) &&
1224 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001225 setOutputDevice(desc,
1226 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001227 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001228 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001229 }
1230 }
1231 // update the outputs if stopping one with a stream that can affect notification routing
1232 handleNotificationRoutingForStream(stream);
1233 }
1234 return NO_ERROR;
1235 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001236 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001237 return INVALID_OPERATION;
1238 }
1239}
1240
Eric Laurente83b55d2014-11-14 10:06:21 -08001241void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001242 audio_stream_type_t stream __unused,
1243 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001244{
1245 ALOGV("releaseOutput() %d", output);
1246 ssize_t index = mOutputs.indexOfKey(output);
1247 if (index < 0) {
1248 ALOGW("releaseOutput() releasing unknown output %d", output);
1249 return;
1250 }
1251
1252#ifdef AUDIO_POLICY_TEST
1253 int testIndex = testOutputIndex(output);
1254 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001255 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001256 if (outputDesc->isActive()) {
1257 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001258 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001259 mTestOutputs[testIndex] = 0;
1260 }
1261 return;
1262 }
1263#endif //AUDIO_POLICY_TEST
1264
Paul McLeanaa981192015-03-21 09:55:15 -07001265 // Routing
1266 mOutputRoutes.removeRoute(session);
1267
Eric Laurentc75307b2015-03-17 15:29:32 -07001268 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001269 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001270 if (desc->mDirectOpenCount <= 0) {
1271 ALOGW("releaseOutput() invalid open count %d for output %d",
1272 desc->mDirectOpenCount, output);
1273 return;
1274 }
1275 if (--desc->mDirectOpenCount == 0) {
1276 closeOutput(output);
1277 // If effects where present on the output, audioflinger moved them to the primary
1278 // output by default: move them back to the appropriate output.
1279 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001280 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001281 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1282 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001283 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001284 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001285 }
1286 }
1287}
1288
1289
Eric Laurentcaf7f482014-11-25 17:50:47 -08001290status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1291 audio_io_handle_t *input,
1292 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001293 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001294 uint32_t samplingRate,
1295 audio_format_t format,
1296 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001297 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001298 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001299 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001300{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001301 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1302 "session %d, flags %#x",
1303 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001304
Eric Laurentcaf7f482014-11-25 17:50:47 -08001305 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001306 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001307 audio_devices_t device;
1308 // handle legacy remote submix case where the address was not always specified
1309 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001310 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001311 audio_source_t inputSource = attr->source;
1312 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001313 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001314
Eric Laurentc447ded2015-01-06 08:47:05 -08001315 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1316 inputSource = AUDIO_SOURCE_MIC;
1317 }
1318 halInputSource = inputSource;
1319
Paul McLean466dc8e2015-04-17 13:15:36 -06001320 // Explicit routing?
1321 sp<DeviceDescriptor> deviceDesc;
1322 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1323 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1324 deviceDesc = mAvailableInputDevices[i];
1325 break;
1326 }
1327 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001328 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001329
Eric Laurentc447ded2015-01-06 08:47:05 -08001330 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001331 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001332 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001333 if (ret != NO_ERROR) {
1334 return ret;
1335 }
1336 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001337 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1338 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001339 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001340 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001341 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001342 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001343 return BAD_VALUE;
1344 }
Eric Laurentc722f302014-12-10 11:21:49 -08001345 if (policyMix != NULL) {
1346 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001347 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1348 // there is an external policy, but this input is attached to a mix of recorders,
1349 // meaning it receives audio injected into the framework, so the recorder doesn't
1350 // know about it and is therefore considered "legacy"
1351 *inputType = API_INPUT_LEGACY;
1352 } else {
1353 // recording a mix of players defined by an external policy, we're rerouting for
1354 // an external policy
1355 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1356 }
Eric Laurentc722f302014-12-10 11:21:49 -08001357 } else if (audio_is_remote_submix_device(device)) {
1358 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001359 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001360 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1361 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001362 } else {
1363 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001364 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001365 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001366 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001367 case AUDIO_SOURCE_VOICE_UPLINK:
1368 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1369 break;
1370 case AUDIO_SOURCE_VOICE_DOWNLINK:
1371 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1372 break;
1373 case AUDIO_SOURCE_VOICE_CALL:
1374 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1375 break;
1376 default:
1377 break;
1378 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001379 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001380 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1381 if (index >= 0) {
1382 *input = mSoundTriggerSessions.valueFor(session);
1383 isSoundTrigger = true;
1384 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1385 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1386 } else {
1387 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1388 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001389 }
1390 }
1391
Andy Hungf129b032015-04-07 13:45:50 -07001392 // find a compatible input profile (not necessarily identical in parameters)
1393 sp<IOProfile> profile;
1394 // samplingRate and flags may be updated by getInputProfile
1395 uint32_t profileSamplingRate = samplingRate;
1396 audio_format_t profileFormat = format;
1397 audio_channel_mask_t profileChannelMask = channelMask;
1398 audio_input_flags_t profileFlags = flags;
1399 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001400 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001401 profileSamplingRate, profileFormat, profileChannelMask,
1402 profileFlags);
1403 if (profile != 0) {
1404 break; // success
1405 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1406 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1407 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1409 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001410 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001412 }
Eric Laurente552edb2014-03-10 17:42:56 -07001413 }
1414
Eric Laurent322b4d22015-04-03 15:57:54 -07001415 if (profile->getModuleHandle() == 0) {
1416 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001417 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001418 }
1419
1420 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001421 config.sample_rate = profileSamplingRate;
1422 config.channel_mask = profileChannelMask;
1423 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001424
Eric Laurent322b4d22015-04-03 15:57:54 -07001425 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001426 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001427 &config,
1428 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001429 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001430 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001431 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001432
1433 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001434 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001435 (profileSamplingRate != config.sample_rate) ||
1436 (profileFormat != config.format) ||
1437 (profileChannelMask != config.channel_mask)) {
1438 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1439 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001440 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001441 if (*input != AUDIO_IO_HANDLE_NONE) {
1442 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001443 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001444 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001445 }
1446
Eric Laurent1f2f2232014-06-02 12:01:23 -07001447 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001448 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001449 inputDesc->mRefCount = 0;
1450 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001451 inputDesc->mSamplingRate = profileSamplingRate;
1452 inputDesc->mFormat = profileFormat;
1453 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001454 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001455 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001456 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001457 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001458
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001459 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001460
Eric Laurentcaf7f482014-11-25 17:50:47 -08001461 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001462 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001463
Eric Laurentcaf7f482014-11-25 17:50:47 -08001464 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001465}
1466
Eric Laurent4dc68062014-07-28 17:26:49 -07001467status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1468 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001469{
1470 ALOGV("startInput() input %d", input);
1471 ssize_t index = mInputs.indexOfKey(input);
1472 if (index < 0) {
1473 ALOGW("startInput() unknown input %d", input);
1474 return BAD_VALUE;
1475 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001476 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001477
Eric Laurentc722f302014-12-10 11:21:49 -08001478 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001479 if (index < 0) {
1480 ALOGW("startInput() unknown session %d on input %d", session, input);
1481 return BAD_VALUE;
1482 }
1483
Glenn Kasten74a8e252014-07-24 14:09:55 -07001484 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001485 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001486
1487 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001488 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001489 if (activeInput != 0 && activeInput != input) {
1490
1491 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1492 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001493 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001494 if ((activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) &&
1495 !activeDesc->hasPreemptedSession(session)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001496 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001497 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
1498 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1499 sessions.add(activeSession);
1500 inputDesc->setPreemptedSessions(sessions);
1501 stopInput(activeInput, activeSession);
1502 releaseInput(activeInput, activeSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001503 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001504 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001505 return INVALID_OPERATION;
1506 }
1507 }
1508 }
1509
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001510 // Routing?
1511 mInputRoutes.incRouteActivity(session);
1512
Paul McLean466dc8e2015-04-17 13:15:36 -06001513 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001514 // if input maps to a dynamic policy with an activity listener, notify of state change
1515 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001516 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001517 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1518 MIX_STATE_MIXING);
1519 }
1520
François Gaffie53615e22015-03-19 09:24:12 +01001521 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001522 SoundTrigger::setCaptureState(true);
1523 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001524 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001525
Eric Laurentc722f302014-12-10 11:21:49 -08001526 // automatically enable the remote submix output when input is started if not
1527 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001528 // For remote submix (a virtual device), we open only one input per capture request.
1529 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001530 String8 address = String8("");
1531 if (inputDesc->mPolicyMix == NULL) {
1532 address = String8("0");
1533 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1534 address = inputDesc->mPolicyMix->mRegistrationId;
1535 }
1536 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001537 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001538 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001539 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001540 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001541 }
Eric Laurente552edb2014-03-10 17:42:56 -07001542 }
1543
Eric Laurente552edb2014-03-10 17:42:56 -07001544 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1545
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001546 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001547 return NO_ERROR;
1548}
1549
Eric Laurent4dc68062014-07-28 17:26:49 -07001550status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1551 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001552{
1553 ALOGV("stopInput() input %d", input);
1554 ssize_t index = mInputs.indexOfKey(input);
1555 if (index < 0) {
1556 ALOGW("stopInput() unknown input %d", input);
1557 return BAD_VALUE;
1558 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001559 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001560
Eric Laurentc722f302014-12-10 11:21:49 -08001561 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001562 if (index < 0) {
1563 ALOGW("stopInput() unknown session %d on input %d", session, input);
1564 return BAD_VALUE;
1565 }
1566
Eric Laurente552edb2014-03-10 17:42:56 -07001567 if (inputDesc->mRefCount == 0) {
1568 ALOGW("stopInput() input %d already stopped", input);
1569 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001570 }
1571
1572 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001573
1574 // Routing?
1575 mInputRoutes.decRouteActivity(session);
1576
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001577 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001578 // if input maps to a dynamic policy with an activity listener, notify of state change
1579 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001580 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001581 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1582 MIX_STATE_IDLE);
1583 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001584
Eric Laurentc722f302014-12-10 11:21:49 -08001585 // automatically disable the remote submix output when input is stopped if not
1586 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001587 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001588 String8 address = String8("");
1589 if (inputDesc->mPolicyMix == NULL) {
1590 address = String8("0");
1591 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1592 address = inputDesc->mPolicyMix->mRegistrationId;
1593 }
1594 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001595 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001596 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001597 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001598 }
Eric Laurente552edb2014-03-10 17:42:56 -07001599 }
1600
Eric Laurent1c333e22014-05-20 10:48:17 -07001601 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001602
François Gaffie53615e22015-03-19 09:24:12 +01001603 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001604 SoundTrigger::setCaptureState(false);
1605 }
Eric Laurent64265b22015-09-18 18:32:07 -07001606 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001607 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001608 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001609}
1610
Eric Laurent4dc68062014-07-28 17:26:49 -07001611void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1612 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001613{
1614 ALOGV("releaseInput() %d", input);
1615 ssize_t index = mInputs.indexOfKey(input);
1616 if (index < 0) {
1617 ALOGW("releaseInput() releasing unknown input %d", input);
1618 return;
1619 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001620
1621 // Routing
1622 mInputRoutes.removeRoute(session);
1623
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001624 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1625 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001626
Eric Laurentc722f302014-12-10 11:21:49 -08001627 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001628 if (index < 0) {
1629 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1630 return;
1631 }
Eric Laurentc722f302014-12-10 11:21:49 -08001632 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001633 if (inputDesc->mOpenRefCount == 0) {
1634 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1635 return;
1636 }
1637 inputDesc->mOpenRefCount--;
1638 if (inputDesc->mOpenRefCount > 0) {
1639 ALOGV("releaseInput() exit > 0");
1640 return;
1641 }
1642
Eric Laurent05b90f82014-08-27 15:32:29 -07001643 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001644 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001645 ALOGV("releaseInput() exit");
1646}
1647
Eric Laurentd4692962014-05-05 18:13:44 -07001648void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001649 bool patchRemoved = false;
1650
Eric Laurentd4692962014-05-05 18:13:44 -07001651 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001652 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1653 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1654 if (patch_index >= 0) {
1655 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1656 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1657 mAudioPatches.removeItemsAt(patch_index);
1658 patchRemoved = true;
1659 }
Eric Laurentd4692962014-05-05 18:13:44 -07001660 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1661 }
1662 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001663 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001664
1665 if (patchRemoved) {
1666 mpClientInterface->onAudioPatchListUpdate();
1667 }
Eric Laurentd4692962014-05-05 18:13:44 -07001668}
1669
Eric Laurente0720872014-03-11 09:30:41 -07001670void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001671 int indexMin,
1672 int indexMax)
1673{
1674 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001675 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001676 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1677 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001678 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001679 }
Eric Laurente552edb2014-03-10 17:42:56 -07001680}
1681
Eric Laurente0720872014-03-11 09:30:41 -07001682status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001683 int index,
1684 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001685{
1686
Eric Laurentc75307b2015-03-17 15:29:32 -07001687 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1688 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001689 return BAD_VALUE;
1690 }
1691 if (!audio_is_output_device(device)) {
1692 return BAD_VALUE;
1693 }
1694
1695 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001696 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001697
1698 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1699 stream, device, index);
1700
1701 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1702 // clear all device specific values
1703 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001704 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001705 }
François Gaffiedfd74092015-03-19 12:10:59 +01001706 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001707
Eric Laurent31551f82014-10-10 18:21:56 -07001708 // update volume on all outputs whose current device is also selected by the same
1709 // strategy as the device specified by the caller
1710 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001711
1712
1713 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1714 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1715 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001716 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001717 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1718 }
1719 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1720 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001721 return NO_ERROR;
1722 }
Eric Laurente552edb2014-03-10 17:42:56 -07001723 status_t status = NO_ERROR;
1724 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001725 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1726 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001727 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001728 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001729 if (volStatus != NO_ERROR) {
1730 status = volStatus;
1731 }
1732 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001733 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1734 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1735 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001736 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001737 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001738 }
Eric Laurente552edb2014-03-10 17:42:56 -07001739 }
1740 return status;
1741}
1742
Eric Laurente0720872014-03-11 09:30:41 -07001743status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001744 int *index,
1745 audio_devices_t device)
1746{
1747 if (index == NULL) {
1748 return BAD_VALUE;
1749 }
1750 if (!audio_is_output_device(device)) {
1751 return BAD_VALUE;
1752 }
1753 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1754 // the strategy the stream belongs to.
1755 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1756 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1757 }
François Gaffiedfd74092015-03-19 12:10:59 +01001758 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001759
Eric Laurentc75307b2015-03-17 15:29:32 -07001760 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001761 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1762 return NO_ERROR;
1763}
1764
Eric Laurente0720872014-03-11 09:30:41 -07001765audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001766 const SortedVector<audio_io_handle_t>& outputs)
1767{
1768 // select one output among several suitable for global effects.
1769 // The priority is as follows:
1770 // 1: An offloaded output. If the effect ends up not being offloadable,
1771 // AudioFlinger will invalidate the track and the offloaded output
1772 // will be closed causing the effect to be moved to a PCM output.
1773 // 2: A deep buffer output
1774 // 3: the first output in the list
1775
1776 if (outputs.size() == 0) {
1777 return 0;
1778 }
1779
1780 audio_io_handle_t outputOffloaded = 0;
1781 audio_io_handle_t outputDeepBuffer = 0;
1782
1783 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001784 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001785 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001786 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1787 outputOffloaded = outputs[i];
1788 }
1789 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1790 outputDeepBuffer = outputs[i];
1791 }
1792 }
1793
1794 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1795 outputOffloaded, outputDeepBuffer);
1796 if (outputOffloaded != 0) {
1797 return outputOffloaded;
1798 }
1799 if (outputDeepBuffer != 0) {
1800 return outputDeepBuffer;
1801 }
1802
1803 return outputs[0];
1804}
1805
Eric Laurente0720872014-03-11 09:30:41 -07001806audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001807{
1808 // apply simple rule where global effects are attached to the same output as MUSIC streams
1809
Eric Laurent3b73df72014-03-11 09:06:29 -07001810 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001811 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1812 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1813
1814 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1815 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1816 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1817
1818 return output;
1819}
1820
Eric Laurente0720872014-03-11 09:30:41 -07001821status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001822 audio_io_handle_t io,
1823 uint32_t strategy,
1824 int session,
1825 int id)
1826{
1827 ssize_t index = mOutputs.indexOfKey(io);
1828 if (index < 0) {
1829 index = mInputs.indexOfKey(io);
1830 if (index < 0) {
1831 ALOGW("registerEffect() unknown io %d", io);
1832 return INVALID_OPERATION;
1833 }
1834 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001835 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001836}
1837
Eric Laurentc75307b2015-03-17 15:29:32 -07001838bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1839{
1840 return mOutputs.isStreamActive(stream, inPastMs);
1841}
1842
1843bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1844{
1845 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1846}
1847
Eric Laurente0720872014-03-11 09:30:41 -07001848bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001849{
1850 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001851 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001852 if (inputDescriptor->mRefCount == 0) {
1853 continue;
1854 }
1855 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001856 return true;
1857 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001858 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1859 // corresponds to an active capture triggered by a hardware hotword recognition
1860 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1861 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1862 // FIXME: we should not assume that the first session is the active one and keep
1863 // activity count per session. Same in startInput().
1864 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1865 if (index >= 0) {
1866 return true;
1867 }
1868 }
Eric Laurente552edb2014-03-10 17:42:56 -07001869 }
1870 return false;
1871}
1872
Eric Laurent275e8e92014-11-30 15:14:47 -08001873// Register a list of custom mixes with their attributes and format.
1874// When a mix is registered, corresponding input and output profiles are
1875// added to the remote submix hw module. The profile contains only the
1876// parameters (sampling rate, format...) specified by the mix.
1877// The corresponding input remote submix device is also connected.
1878//
1879// When a remote submix device is connected, the address is checked to select the
1880// appropriate profile and the corresponding input or output stream is opened.
1881//
1882// When capture starts, getInputForAttr() will:
1883// - 1 look for a mix matching the address passed in attribtutes tags if any
1884// - 2 if none found, getDeviceForInputSource() will:
1885// - 2.1 look for a mix matching the attributes source
1886// - 2.2 if none found, default to device selection by policy rules
1887// At this time, the corresponding output remote submix device is also connected
1888// and active playback use cases can be transferred to this mix if needed when reconnecting
1889// after AudioTracks are invalidated
1890//
1891// When playback starts, getOutputForAttr() will:
1892// - 1 look for a mix matching the address passed in attribtutes tags if any
1893// - 2 if none found, look for a mix matching the attributes usage
1894// - 3 if none found, default to device and output selection by policy rules.
1895
1896status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1897{
1898 sp<HwModule> module;
1899 for (size_t i = 0; i < mHwModules.size(); i++) {
1900 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1901 mHwModules[i]->mHandle != 0) {
1902 module = mHwModules[i];
1903 break;
1904 }
1905 }
1906
1907 if (module == 0) {
1908 return INVALID_OPERATION;
1909 }
1910
1911 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1912
1913 for (size_t i = 0; i < mixes.size(); i++) {
1914 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001915
1916 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001917 continue;
1918 }
1919 audio_config_t outputConfig = mixes[i].mFormat;
1920 audio_config_t inputConfig = mixes[i].mFormat;
1921 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1922 // stereo and let audio flinger do the channel conversion if needed.
1923 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1924 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1925 module->addOutputProfile(address, &outputConfig,
1926 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1927 module->addInputProfile(address, &inputConfig,
1928 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001929
Eric Laurentc722f302014-12-10 11:21:49 -08001930 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001931 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001932 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001933 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001934 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001935 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001936 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001937 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001938 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001939 }
1940 return NO_ERROR;
1941}
1942
1943status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1944{
1945 sp<HwModule> module;
1946 for (size_t i = 0; i < mHwModules.size(); i++) {
1947 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1948 mHwModules[i]->mHandle != 0) {
1949 module = mHwModules[i];
1950 break;
1951 }
1952 }
1953
1954 if (module == 0) {
1955 return INVALID_OPERATION;
1956 }
1957
1958 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1959
1960 for (size_t i = 0; i < mixes.size(); i++) {
1961 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001962
1963 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001964 continue;
1965 }
1966
Eric Laurentc722f302014-12-10 11:21:49 -08001967 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1968 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1969 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001970 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001971 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001972 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001973 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001974
1975 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1976 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1977 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001978 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001979 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001980 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001981 }
1982 module->removeOutputProfile(address);
1983 module->removeInputProfile(address);
1984 }
1985 return NO_ERROR;
1986}
1987
Eric Laurente552edb2014-03-10 17:42:56 -07001988
Eric Laurente0720872014-03-11 09:30:41 -07001989status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001990{
1991 const size_t SIZE = 256;
1992 char buffer[SIZE];
1993 String8 result;
1994
1995 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1996 result.append(buffer);
1997
Eric Laurent87ffa392015-05-22 10:32:38 -07001998 snprintf(buffer, SIZE, " Primary Output: %d\n",
1999 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002000 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002001 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002002 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002003 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002004 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002005 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002006 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002007 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002008 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002009 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002010 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002011 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002012 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002013 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002014 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002015 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002016 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002017 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2018 result.append(buffer);
2019
François Gaffie2110e042015-03-24 08:41:51 +01002020 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002021
François Gaffie53615e22015-03-19 09:24:12 +01002022 mAvailableOutputDevices.dump(fd, String8("output"));
2023 mAvailableInputDevices.dump(fd, String8("input"));
2024 mHwModules.dump(fd);
2025 mOutputs.dump(fd);
2026 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002027 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002028 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002029 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002030
2031 return NO_ERROR;
2032}
2033
2034// This function checks for the parameters which can be offloaded.
2035// This can be enhanced depending on the capability of the DSP and policy
2036// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002037bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002038{
2039 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002040 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002041 offloadInfo.sample_rate, offloadInfo.channel_mask,
2042 offloadInfo.format,
2043 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2044 offloadInfo.has_video);
2045
2046 // Check if offload has been disabled
2047 char propValue[PROPERTY_VALUE_MAX];
2048 if (property_get("audio.offload.disable", propValue, "0")) {
2049 if (atoi(propValue) != 0) {
2050 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2051 return false;
2052 }
2053 }
2054
2055 // Check if stream type is music, then only allow offload as of now.
2056 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2057 {
2058 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2059 return false;
2060 }
2061
2062 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002063 const bool allowOffloadWithVideo =
2064 property_get_bool("audio.offload.video", false /* default_value */);
2065 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002066 ALOGV("isOffloadSupported: has_video == true, returning false");
2067 return false;
2068 }
2069
2070 //If duration is less than minimum value defined in property, return false
2071 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2072 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2073 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2074 return false;
2075 }
2076 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2077 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2078 return false;
2079 }
2080
2081 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2082 // creating an offloaded track and tearing it down immediately after start when audioflinger
2083 // detects there is an active non offloadable effect.
2084 // FIXME: We should check the audio session here but we do not have it in this context.
2085 // This may prevent offloading in rare situations where effects are left active by apps
2086 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002087 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002088 return false;
2089 }
2090
2091 // See if there is a profile to support this.
2092 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002093 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002094 offloadInfo.sample_rate,
2095 offloadInfo.format,
2096 offloadInfo.channel_mask,
2097 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002098 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2099 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002100}
2101
Eric Laurent6a94d692014-05-20 11:18:06 -07002102status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2103 audio_port_type_t type,
2104 unsigned int *num_ports,
2105 struct audio_port *ports,
2106 unsigned int *generation)
2107{
2108 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2109 generation == NULL) {
2110 return BAD_VALUE;
2111 }
2112 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2113 if (ports == NULL) {
2114 *num_ports = 0;
2115 }
2116
2117 size_t portsWritten = 0;
2118 size_t portsMax = *num_ports;
2119 *num_ports = 0;
2120 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2121 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2122 for (size_t i = 0;
2123 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2124 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2125 }
2126 *num_ports += mAvailableOutputDevices.size();
2127 }
2128 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2129 for (size_t i = 0;
2130 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2131 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2132 }
2133 *num_ports += mAvailableInputDevices.size();
2134 }
2135 }
2136 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2137 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2138 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2139 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2140 }
2141 *num_ports += mInputs.size();
2142 }
2143 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002144 size_t numOutputs = 0;
2145 for (size_t i = 0; i < mOutputs.size(); i++) {
2146 if (!mOutputs[i]->isDuplicated()) {
2147 numOutputs++;
2148 if (portsWritten < portsMax) {
2149 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2150 }
2151 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002152 }
Eric Laurent84c70242014-06-23 08:46:27 -07002153 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002154 }
2155 }
2156 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002157 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002158 return NO_ERROR;
2159}
2160
2161status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2162{
2163 return NO_ERROR;
2164}
2165
Eric Laurent6a94d692014-05-20 11:18:06 -07002166status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2167 audio_patch_handle_t *handle,
2168 uid_t uid)
2169{
2170 ALOGV("createAudioPatch()");
2171
2172 if (handle == NULL || patch == NULL) {
2173 return BAD_VALUE;
2174 }
2175 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2176
Eric Laurent874c42872014-08-08 15:13:39 -07002177 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2178 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2179 return BAD_VALUE;
2180 }
2181 // only one source per audio patch supported for now
2182 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002183 return INVALID_OPERATION;
2184 }
Eric Laurent874c42872014-08-08 15:13:39 -07002185
2186 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002187 return INVALID_OPERATION;
2188 }
Eric Laurent874c42872014-08-08 15:13:39 -07002189 for (size_t i = 0; i < patch->num_sinks; i++) {
2190 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2191 return INVALID_OPERATION;
2192 }
2193 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002194
2195 sp<AudioPatch> patchDesc;
2196 ssize_t index = mAudioPatches.indexOfKey(*handle);
2197
Eric Laurent6a94d692014-05-20 11:18:06 -07002198 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2199 patch->sources[0].role,
2200 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002201#if LOG_NDEBUG == 0
2202 for (size_t i = 0; i < patch->num_sinks; i++) {
2203 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2204 patch->sinks[i].role,
2205 patch->sinks[i].type);
2206 }
2207#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002208
2209 if (index >= 0) {
2210 patchDesc = mAudioPatches.valueAt(index);
2211 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2212 mUidCached, patchDesc->mUid, uid);
2213 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2214 return INVALID_OPERATION;
2215 }
2216 } else {
2217 *handle = 0;
2218 }
2219
2220 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002221 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002222 if (outputDesc == NULL) {
2223 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2224 return BAD_VALUE;
2225 }
Eric Laurent84c70242014-06-23 08:46:27 -07002226 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2227 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002228 if (patchDesc != 0) {
2229 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2230 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2231 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2232 return BAD_VALUE;
2233 }
2234 }
Eric Laurent874c42872014-08-08 15:13:39 -07002235 DeviceVector devices;
2236 for (size_t i = 0; i < patch->num_sinks; i++) {
2237 // Only support mix to devices connection
2238 // TODO add support for mix to mix connection
2239 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2240 ALOGV("createAudioPatch() source mix but sink is not a device");
2241 return INVALID_OPERATION;
2242 }
2243 sp<DeviceDescriptor> devDesc =
2244 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2245 if (devDesc == 0) {
2246 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2247 return BAD_VALUE;
2248 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002249
François Gaffie53615e22015-03-19 09:24:12 +01002250 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002251 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002252 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002253 NULL, // updatedSamplingRate
2254 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002255 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002256 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002257 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002258 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002259 ALOGV("createAudioPatch() profile not supported for device %08x",
2260 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002261 return INVALID_OPERATION;
2262 }
2263 devices.add(devDesc);
2264 }
2265 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002266 return INVALID_OPERATION;
2267 }
Eric Laurent874c42872014-08-08 15:13:39 -07002268
Eric Laurent6a94d692014-05-20 11:18:06 -07002269 // TODO: reconfigure output format and channels here
2270 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002271 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002272 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002273 index = mAudioPatches.indexOfKey(*handle);
2274 if (index >= 0) {
2275 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2276 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2277 }
2278 patchDesc = mAudioPatches.valueAt(index);
2279 patchDesc->mUid = uid;
2280 ALOGV("createAudioPatch() success");
2281 } else {
2282 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2283 return INVALID_OPERATION;
2284 }
2285 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2286 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2287 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002288 // only one sink supported when connecting an input device to a mix
2289 if (patch->num_sinks > 1) {
2290 return INVALID_OPERATION;
2291 }
François Gaffie53615e22015-03-19 09:24:12 +01002292 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002293 if (inputDesc == NULL) {
2294 return BAD_VALUE;
2295 }
2296 if (patchDesc != 0) {
2297 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2298 return BAD_VALUE;
2299 }
2300 }
2301 sp<DeviceDescriptor> devDesc =
2302 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2303 if (devDesc == 0) {
2304 return BAD_VALUE;
2305 }
2306
François Gaffie53615e22015-03-19 09:24:12 +01002307 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002308 devDesc->mAddress,
2309 patch->sinks[0].sample_rate,
2310 NULL, /*updatedSampleRate*/
2311 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002312 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002313 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002314 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002315 // FIXME for the parameter type,
2316 // and the NONE
2317 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002318 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002319 return INVALID_OPERATION;
2320 }
2321 // TODO: reconfigure output format and channels here
2322 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002323 devDesc->type(), inputDesc->mIoHandle);
2324 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002325 index = mAudioPatches.indexOfKey(*handle);
2326 if (index >= 0) {
2327 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2328 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2329 }
2330 patchDesc = mAudioPatches.valueAt(index);
2331 patchDesc->mUid = uid;
2332 ALOGV("createAudioPatch() success");
2333 } else {
2334 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2335 return INVALID_OPERATION;
2336 }
2337 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2338 // device to device connection
2339 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002340 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002341 return BAD_VALUE;
2342 }
2343 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002344 sp<DeviceDescriptor> srcDeviceDesc =
2345 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002346 if (srcDeviceDesc == 0) {
2347 return BAD_VALUE;
2348 }
Eric Laurent874c42872014-08-08 15:13:39 -07002349
Eric Laurent6a94d692014-05-20 11:18:06 -07002350 //update source and sink with our own data as the data passed in the patch may
2351 // be incomplete.
2352 struct audio_patch newPatch = *patch;
2353 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002354
Eric Laurent874c42872014-08-08 15:13:39 -07002355 for (size_t i = 0; i < patch->num_sinks; i++) {
2356 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2357 ALOGV("createAudioPatch() source device but one sink is not a device");
2358 return INVALID_OPERATION;
2359 }
2360
2361 sp<DeviceDescriptor> sinkDeviceDesc =
2362 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2363 if (sinkDeviceDesc == 0) {
2364 return BAD_VALUE;
2365 }
2366 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2367
Eric Laurent3bcf8592015-04-03 12:13:24 -07002368 // create a software bridge in PatchPanel if:
2369 // - source and sink devices are on differnt HW modules OR
2370 // - audio HAL version is < 3.0
2371 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2372 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2373 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002374 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002375 return INVALID_OPERATION;
2376 }
Eric Laurent874c42872014-08-08 15:13:39 -07002377 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002378 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002379 // if the sink device is reachable via an opened output stream, request to go via
2380 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002381 audio_io_handle_t output = selectOutput(outputs,
2382 AUDIO_OUTPUT_FLAG_NONE,
2383 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002384 if (output != AUDIO_IO_HANDLE_NONE) {
2385 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2386 if (outputDesc->isDuplicated()) {
2387 return INVALID_OPERATION;
2388 }
2389 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002390 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002391 newPatch.num_sources = 2;
2392 }
Eric Laurent83b88082014-06-20 18:31:16 -07002393 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002394 }
2395 // TODO: check from routing capabilities in config file and other conflicting patches
2396
2397 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2398 if (index >= 0) {
2399 afPatchHandle = patchDesc->mAfPatchHandle;
2400 }
2401
2402 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2403 &afPatchHandle,
2404 0);
2405 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2406 status, afPatchHandle);
2407 if (status == NO_ERROR) {
2408 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002409 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002410 addAudioPatch(patchDesc->mHandle, patchDesc);
2411 } else {
2412 patchDesc->mPatch = newPatch;
2413 }
2414 patchDesc->mAfPatchHandle = afPatchHandle;
2415 *handle = patchDesc->mHandle;
2416 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002417 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002418 } else {
2419 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2420 status);
2421 return INVALID_OPERATION;
2422 }
2423 } else {
2424 return BAD_VALUE;
2425 }
2426 } else {
2427 return BAD_VALUE;
2428 }
2429 return NO_ERROR;
2430}
2431
2432status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2433 uid_t uid)
2434{
2435 ALOGV("releaseAudioPatch() patch %d", handle);
2436
2437 ssize_t index = mAudioPatches.indexOfKey(handle);
2438
2439 if (index < 0) {
2440 return BAD_VALUE;
2441 }
2442 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2443 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2444 mUidCached, patchDesc->mUid, uid);
2445 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2446 return INVALID_OPERATION;
2447 }
2448
2449 struct audio_patch *patch = &patchDesc->mPatch;
2450 patchDesc->mUid = mUidCached;
2451 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002452 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002453 if (outputDesc == NULL) {
2454 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2455 return BAD_VALUE;
2456 }
2457
Eric Laurentc75307b2015-03-17 15:29:32 -07002458 setOutputDevice(outputDesc,
2459 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002460 true,
2461 0,
2462 NULL);
2463 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2464 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002465 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002466 if (inputDesc == NULL) {
2467 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2468 return BAD_VALUE;
2469 }
2470 setInputDevice(inputDesc->mIoHandle,
2471 getNewInputDevice(inputDesc->mIoHandle),
2472 true,
2473 NULL);
2474 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2475 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2476 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2477 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2478 status, patchDesc->mAfPatchHandle);
2479 removeAudioPatch(patchDesc->mHandle);
2480 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002481 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002482 } else {
2483 return BAD_VALUE;
2484 }
2485 } else {
2486 return BAD_VALUE;
2487 }
2488 return NO_ERROR;
2489}
2490
2491status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2492 struct audio_patch *patches,
2493 unsigned int *generation)
2494{
François Gaffie53615e22015-03-19 09:24:12 +01002495 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002496 return BAD_VALUE;
2497 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002498 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002499 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002500}
2501
Eric Laurente1715a42014-05-20 11:30:42 -07002502status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002503{
Eric Laurente1715a42014-05-20 11:30:42 -07002504 ALOGV("setAudioPortConfig()");
2505
2506 if (config == NULL) {
2507 return BAD_VALUE;
2508 }
2509 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2510 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002511 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2512 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002513 }
2514
Eric Laurenta121f902014-06-03 13:32:54 -07002515 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002516 if (config->type == AUDIO_PORT_TYPE_MIX) {
2517 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002518 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002519 if (outputDesc == NULL) {
2520 return BAD_VALUE;
2521 }
Eric Laurent84c70242014-06-23 08:46:27 -07002522 ALOG_ASSERT(!outputDesc->isDuplicated(),
2523 "setAudioPortConfig() called on duplicated output %d",
2524 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002525 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002526 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002527 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002528 if (inputDesc == NULL) {
2529 return BAD_VALUE;
2530 }
Eric Laurenta121f902014-06-03 13:32:54 -07002531 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002532 } else {
2533 return BAD_VALUE;
2534 }
2535 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2536 sp<DeviceDescriptor> deviceDesc;
2537 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2538 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2539 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2540 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2541 } else {
2542 return BAD_VALUE;
2543 }
2544 if (deviceDesc == NULL) {
2545 return BAD_VALUE;
2546 }
Eric Laurenta121f902014-06-03 13:32:54 -07002547 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002548 } else {
2549 return BAD_VALUE;
2550 }
2551
Eric Laurenta121f902014-06-03 13:32:54 -07002552 struct audio_port_config backupConfig;
2553 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2554 if (status == NO_ERROR) {
2555 struct audio_port_config newConfig;
2556 audioPortConfig->toAudioPortConfig(&newConfig, config);
2557 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002558 }
Eric Laurenta121f902014-06-03 13:32:54 -07002559 if (status != NO_ERROR) {
2560 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002561 }
Eric Laurente1715a42014-05-20 11:30:42 -07002562
2563 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002564}
2565
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002566void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2567{
2568 clearAudioPatches(uid);
2569 clearSessionRoutes(uid);
2570}
2571
Eric Laurent6a94d692014-05-20 11:18:06 -07002572void AudioPolicyManager::clearAudioPatches(uid_t uid)
2573{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002574 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002575 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2576 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002577 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002578 }
2579 }
2580}
2581
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002582
2583void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2584 audio_io_handle_t ouptutToSkip)
2585{
2586 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2587 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2588 for (size_t j = 0; j < mOutputs.size(); j++) {
2589 if (mOutputs.keyAt(j) == ouptutToSkip) {
2590 continue;
2591 }
2592 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2593 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2594 continue;
2595 }
2596 // If the default device for this strategy is on another output mix,
2597 // invalidate all tracks in this strategy to force re connection.
2598 // Otherwise select new device on the output mix.
2599 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2600 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2601 if (stream == AUDIO_STREAM_PATCH) {
2602 continue;
2603 }
2604 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2605 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2606 }
2607 }
2608 } else {
2609 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2610 setOutputDevice(outputDesc, newDevice, false);
2611 }
2612 }
2613}
2614
2615void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2616{
2617 // remove output routes associated with this uid
2618 SortedVector<routing_strategy> affectedStrategies;
2619 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2620 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2621 if (route->mUid == uid) {
2622 mOutputRoutes.removeItemsAt(i);
2623 if (route->mDeviceDescriptor != 0) {
2624 affectedStrategies.add(getStrategy(route->mStreamType));
2625 }
2626 }
2627 }
2628 // reroute outputs if necessary
2629 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2630 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2631 }
2632
2633 // remove input routes associated with this uid
2634 SortedVector<audio_source_t> affectedSources;
2635 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2636 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2637 if (route->mUid == uid) {
2638 mInputRoutes.removeItemsAt(i);
2639 if (route->mDeviceDescriptor != 0) {
2640 affectedSources.add(route->mSource);
2641 }
2642 }
2643 }
2644 // reroute inputs if necessary
2645 SortedVector<audio_io_handle_t> inputsToClose;
2646 for (size_t i = 0; i < mInputs.size(); i++) {
2647 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2648 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2649 inputsToClose.add(inputDesc->mIoHandle);
2650 }
2651 }
2652 for (size_t i = 0; i < inputsToClose.size(); i++) {
2653 closeInput(inputsToClose[i]);
2654 }
2655}
2656
2657
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002658status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2659 audio_io_handle_t *ioHandle,
2660 audio_devices_t *device)
2661{
2662 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2663 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002664 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002665
François Gaffiedf372692015-03-19 10:43:27 +01002666 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002667}
2668
Eric Laurent493404d2015-04-21 15:07:36 -07002669status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2670 const audio_attributes_t *attributes __unused,
2671 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002672{
2673 return INVALID_OPERATION;
2674}
2675
Eric Laurent493404d2015-04-21 15:07:36 -07002676status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002677{
2678 return INVALID_OPERATION;
2679}
2680
Eric Laurente552edb2014-03-10 17:42:56 -07002681// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002682// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002683// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002684uint32_t AudioPolicyManager::nextAudioPortGeneration()
2685{
2686 return android_atomic_inc(&mAudioPortGeneration);
2687}
2688
Eric Laurente0720872014-03-11 09:30:41 -07002689AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002690 :
2691#ifdef AUDIO_POLICY_TEST
2692 Thread(false),
2693#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002694 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002695 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002696 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002697 mAudioPortGeneration(1),
2698 mBeaconMuteRefCount(0),
2699 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07002700 mBeaconMuted(false),
2701 mTtsOutputAvailable(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002702{
François Gaffie2110e042015-03-24 08:41:51 +01002703 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2704 if (!engineInstance) {
2705 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2706 return;
2707 }
2708 // Retrieve the Policy Manager Interface
2709 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2710 if (mEngine == NULL) {
2711 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2712 return;
2713 }
2714 mEngine->setObserver(this);
2715 status_t status = mEngine->initCheck();
2716 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2717
Eric Laurent6a94d692014-05-20 11:18:06 -07002718 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002719 mpClientInterface = clientInterface;
2720
Eric Laurent7288ab82015-05-06 18:44:02 -07002721 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002722 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2723 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2724 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2725 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2726 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2727 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002728 ALOGE("could not load audio policy configuration file, setting defaults");
2729 defaultAudioPolicyConfig();
2730 }
2731 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002732 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002733
François Gaffie2110e042015-03-24 08:41:51 +01002734 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2735 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002736
2737 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002738 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2739 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002740 for (size_t i = 0; i < mHwModules.size(); i++) {
2741 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2742 if (mHwModules[i]->mHandle == 0) {
2743 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2744 continue;
2745 }
2746 // open all output streams needed to access attached devices
2747 // except for direct output streams that are only opened when they are actually
2748 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002749 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002750 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2751 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002752 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002753
Eric Laurent3a4311c2014-03-17 12:00:47 -07002754 if (outProfile->mSupportedDevices.isEmpty()) {
2755 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2756 continue;
2757 }
Eric Laurent9459fb02015-08-12 18:36:32 -07002758 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_TTS) != 0) {
2759 mTtsOutputAvailable = true;
2760 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002761
Eric Laurentd78f1532014-09-16 16:38:20 -07002762 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2763 continue;
2764 }
Eric Laurent83b88082014-06-20 18:31:16 -07002765 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002766 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2767 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002768 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002769 // chose first device present in mSupportedDevices also part of
2770 // outputDeviceTypes
2771 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002772 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002773 if ((profileType & outputDeviceTypes) != 0) {
2774 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002775 }
Eric Laurente552edb2014-03-10 17:42:56 -07002776 }
2777 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002778 if ((profileType & outputDeviceTypes) == 0) {
2779 continue;
2780 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002781 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2782 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002783
2784 outputDesc->mDevice = profileType;
2785 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2786 config.sample_rate = outputDesc->mSamplingRate;
2787 config.channel_mask = outputDesc->mChannelMask;
2788 config.format = outputDesc->mFormat;
2789 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002790 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002791 &output,
2792 &config,
2793 &outputDesc->mDevice,
2794 String8(""),
2795 &outputDesc->mLatency,
2796 outputDesc->mFlags);
2797
2798 if (status != NO_ERROR) {
2799 ALOGW("Cannot open output stream for device %08x on hw module %s",
2800 outputDesc->mDevice,
2801 mHwModules[i]->mName);
2802 } else {
2803 outputDesc->mSamplingRate = config.sample_rate;
2804 outputDesc->mChannelMask = config.channel_mask;
2805 outputDesc->mFormat = config.format;
2806
2807 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002808 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002809 ssize_t index =
2810 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2811 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002812 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2813 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002814 }
2815 }
2816 if (mPrimaryOutput == 0 &&
2817 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002818 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002819 }
2820 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002821 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002822 outputDesc->mDevice,
2823 true);
2824 }
Eric Laurente552edb2014-03-10 17:42:56 -07002825 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002826 // open input streams needed to access attached devices to validate
2827 // mAvailableInputDevices list
2828 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2829 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002830 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002831
Eric Laurent3a4311c2014-03-17 12:00:47 -07002832 if (inProfile->mSupportedDevices.isEmpty()) {
2833 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2834 continue;
2835 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002836 // chose first device present in mSupportedDevices also part of
2837 // inputDeviceTypes
2838 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2839 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002840 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002841 if (profileType & inputDeviceTypes) {
2842 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002843 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002844 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002845 if ((profileType & inputDeviceTypes) == 0) {
2846 continue;
2847 }
2848 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2849
2850 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2851 inputDesc->mDevice = profileType;
2852
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002853 // find the address
2854 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2855 // the inputs vector must be of size 1, but we don't want to crash here
2856 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2857 : String8("");
2858 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2859 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2860
Eric Laurentd78f1532014-09-16 16:38:20 -07002861 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2862 config.sample_rate = inputDesc->mSamplingRate;
2863 config.channel_mask = inputDesc->mChannelMask;
2864 config.format = inputDesc->mFormat;
2865 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002866 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002867 &input,
2868 &config,
2869 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002870 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002871 AUDIO_SOURCE_MIC,
2872 AUDIO_INPUT_FLAG_NONE);
2873
2874 if (status == NO_ERROR) {
2875 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002876 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002877 ssize_t index =
2878 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2879 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07002880 if (index >= 0) {
2881 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
2882 if (!devDesc->isAttached()) {
2883 devDesc->attach(mHwModules[i]);
2884 devDesc->importAudioPort(inProfile);
2885 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002886 }
2887 }
2888 mpClientInterface->closeInput(input);
2889 } else {
2890 ALOGW("Cannot open input stream for device %08x on hw module %s",
2891 inputDesc->mDevice,
2892 mHwModules[i]->mName);
2893 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002894 }
2895 }
2896 // make sure all attached devices have been allocated a unique ID
2897 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002898 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002899 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002900 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2901 continue;
2902 }
François Gaffie2110e042015-03-24 08:41:51 +01002903 // The device is now validated and can be appended to the available devices of the engine
2904 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2905 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002906 i++;
2907 }
2908 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002909 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002910 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002911 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2912 continue;
2913 }
François Gaffie2110e042015-03-24 08:41:51 +01002914 // The device is now validated and can be appended to the available devices of the engine
2915 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2916 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002917 i++;
2918 }
2919 // make sure default device is reachable
2920 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002921 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002922 }
Eric Laurente552edb2014-03-10 17:42:56 -07002923
2924 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2925
2926 updateDevicesAndOutputs();
2927
2928#ifdef AUDIO_POLICY_TEST
2929 if (mPrimaryOutput != 0) {
2930 AudioParameter outputCmd = AudioParameter();
2931 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002932 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002933
2934 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2935 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002936 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2937 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002938 mTestLatencyMs = 0;
2939 mCurOutput = 0;
2940 mDirectOutput = false;
2941 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2942 mTestOutputs[i] = 0;
2943 }
2944
2945 const size_t SIZE = 256;
2946 char buffer[SIZE];
2947 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2948 run(buffer, ANDROID_PRIORITY_AUDIO);
2949 }
2950#endif //AUDIO_POLICY_TEST
2951}
2952
Eric Laurente0720872014-03-11 09:30:41 -07002953AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002954{
2955#ifdef AUDIO_POLICY_TEST
2956 exit();
2957#endif //AUDIO_POLICY_TEST
2958 for (size_t i = 0; i < mOutputs.size(); i++) {
2959 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002960 }
2961 for (size_t i = 0; i < mInputs.size(); i++) {
2962 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002963 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002964 mAvailableOutputDevices.clear();
2965 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002966 mOutputs.clear();
2967 mInputs.clear();
2968 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002969}
2970
Eric Laurente0720872014-03-11 09:30:41 -07002971status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002972{
Eric Laurent87ffa392015-05-22 10:32:38 -07002973 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002974}
2975
2976#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002977bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002978{
2979 ALOGV("entering threadLoop()");
2980 while (!exitPending())
2981 {
2982 String8 command;
2983 int valueInt;
2984 String8 value;
2985
2986 Mutex::Autolock _l(mLock);
2987 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2988
2989 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2990 AudioParameter param = AudioParameter(command);
2991
2992 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2993 valueInt != 0) {
2994 ALOGV("Test command %s received", command.string());
2995 String8 target;
2996 if (param.get(String8("target"), target) != NO_ERROR) {
2997 target = "Manager";
2998 }
2999 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3000 param.remove(String8("test_cmd_policy_output"));
3001 mCurOutput = valueInt;
3002 }
3003 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3004 param.remove(String8("test_cmd_policy_direct"));
3005 if (value == "false") {
3006 mDirectOutput = false;
3007 } else if (value == "true") {
3008 mDirectOutput = true;
3009 }
3010 }
3011 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3012 param.remove(String8("test_cmd_policy_input"));
3013 mTestInput = valueInt;
3014 }
3015
3016 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3017 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003018 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003019 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003020 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003021 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003022 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003023 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003024 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003025 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003026 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003027 if (target == "Manager") {
3028 mTestFormat = format;
3029 } else if (mTestOutputs[mCurOutput] != 0) {
3030 AudioParameter outputParam = AudioParameter();
3031 outputParam.addInt(String8("format"), format);
3032 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3033 }
3034 }
3035 }
3036 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3037 param.remove(String8("test_cmd_policy_channels"));
3038 int channels = 0;
3039
3040 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003041 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003042 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003043 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003044 }
3045 if (channels != 0) {
3046 if (target == "Manager") {
3047 mTestChannels = channels;
3048 } else if (mTestOutputs[mCurOutput] != 0) {
3049 AudioParameter outputParam = AudioParameter();
3050 outputParam.addInt(String8("channels"), channels);
3051 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3052 }
3053 }
3054 }
3055 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3056 param.remove(String8("test_cmd_policy_sampleRate"));
3057 if (valueInt >= 0 && valueInt <= 96000) {
3058 int samplingRate = valueInt;
3059 if (target == "Manager") {
3060 mTestSamplingRate = samplingRate;
3061 } else if (mTestOutputs[mCurOutput] != 0) {
3062 AudioParameter outputParam = AudioParameter();
3063 outputParam.addInt(String8("sampling_rate"), samplingRate);
3064 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3065 }
3066 }
3067 }
3068
3069 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3070 param.remove(String8("test_cmd_policy_reopen"));
3071
Eric Laurentc75307b2015-03-17 15:29:32 -07003072 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003073
Eric Laurentc75307b2015-03-17 15:29:32 -07003074 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003075
Eric Laurentc75307b2015-03-17 15:29:32 -07003076 removeOutput(mPrimaryOutput->mIoHandle);
3077 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3078 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003079 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003080 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3081 config.sample_rate = outputDesc->mSamplingRate;
3082 config.channel_mask = outputDesc->mChannelMask;
3083 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003084 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003085 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003086 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003087 &config,
3088 &outputDesc->mDevice,
3089 String8(""),
3090 &outputDesc->mLatency,
3091 outputDesc->mFlags);
3092 if (status != NO_ERROR) {
3093 ALOGE("Failed to reopen hardware output stream, "
3094 "samplingRate: %d, format %d, channels %d",
3095 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003096 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003097 outputDesc->mSamplingRate = config.sample_rate;
3098 outputDesc->mChannelMask = config.channel_mask;
3099 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003100 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003101 AudioParameter outputCmd = AudioParameter();
3102 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003103 mpClientInterface->setParameters(handle, outputCmd.toString());
3104 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003105 }
3106 }
3107
3108
3109 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3110 }
3111 }
3112 return false;
3113}
3114
Eric Laurente0720872014-03-11 09:30:41 -07003115void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003116{
3117 {
3118 AutoMutex _l(mLock);
3119 requestExit();
3120 mWaitWorkCV.signal();
3121 }
3122 requestExitAndWait();
3123}
3124
Eric Laurente0720872014-03-11 09:30:41 -07003125int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003126{
3127 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3128 if (output == mTestOutputs[i]) return i;
3129 }
3130 return 0;
3131}
3132#endif //AUDIO_POLICY_TEST
3133
3134// ---
3135
Eric Laurentc75307b2015-03-17 15:29:32 -07003136void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003137{
François Gaffie98cc1912015-03-18 17:52:40 +01003138 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003139 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003140 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003141}
3142
François Gaffie53615e22015-03-19 09:24:12 +01003143void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3144{
3145 mOutputs.removeItem(output);
3146}
3147
Eric Laurent1f2f2232014-06-02 12:01:23 -07003148void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003149{
François Gaffie98cc1912015-03-18 17:52:40 +01003150 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003151 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003152 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003153}
Eric Laurente552edb2014-03-10 17:42:56 -07003154
Eric Laurentc75307b2015-03-17 15:29:32 -07003155void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003156 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003157 const String8 address /*in*/,
3158 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003159 sp<DeviceDescriptor> devDesc =
3160 desc->mProfile->mSupportedDevices.getDevice(device, address);
3161 if (devDesc != 0) {
3162 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3163 desc->mIoHandle, address.string());
3164 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003165 }
3166}
3167
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003168status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003169 audio_policy_dev_state_t state,
3170 SortedVector<audio_io_handle_t>& outputs,
3171 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003172{
François Gaffie53615e22015-03-19 09:24:12 +01003173 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003174 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003175
3176 if (audio_device_is_digital(device)) {
3177 // erase all current sample rates, formats and channel masks
3178 devDesc->clearCapabilities();
3179 }
Eric Laurente552edb2014-03-10 17:42:56 -07003180
Eric Laurent3b73df72014-03-11 09:06:29 -07003181 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003182 // first list already open outputs that can be routed to this device
3183 for (size_t i = 0; i < mOutputs.size(); i++) {
3184 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003185 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003186 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003187 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3188 outputs.add(mOutputs.keyAt(i));
3189 } else {
3190 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003191 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003192 }
Eric Laurente552edb2014-03-10 17:42:56 -07003193 }
3194 }
3195 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003196 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003197 for (size_t i = 0; i < mHwModules.size(); i++)
3198 {
3199 if (mHwModules[i]->mHandle == 0) {
3200 continue;
3201 }
3202 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3203 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003204 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3205 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003206 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003207 address == profile->mSupportedDevices[0]->mAddress) {
3208 profiles.add(profile);
3209 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3210 }
Eric Laurente552edb2014-03-10 17:42:56 -07003211 }
3212 }
3213 }
3214
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003215 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3216
Eric Laurente552edb2014-03-10 17:42:56 -07003217 if (profiles.isEmpty() && outputs.isEmpty()) {
3218 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3219 return BAD_VALUE;
3220 }
3221
3222 // open outputs for matching profiles if needed. Direct outputs are also opened to
3223 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3224 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003225 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003226
3227 // nothing to do if one output is already opened for this profile
3228 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003229 for (j = 0; j < outputs.size(); j++) {
3230 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003231 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003232 // matching profile: save the sample rates, format and channel masks supported
3233 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003234 if (audio_device_is_digital(device)) {
3235 devDesc->importAudioPort(profile);
3236 }
Eric Laurente552edb2014-03-10 17:42:56 -07003237 break;
3238 }
3239 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003240 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003241 continue;
3242 }
3243
Eric Laurent83b88082014-06-20 18:31:16 -07003244 ALOGV("opening output for device %08x with params %s profile %p",
3245 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003246 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003247 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003248 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3249 config.sample_rate = desc->mSamplingRate;
3250 config.channel_mask = desc->mChannelMask;
3251 config.format = desc->mFormat;
3252 config.offload_info.sample_rate = desc->mSamplingRate;
3253 config.offload_info.channel_mask = desc->mChannelMask;
3254 config.offload_info.format = desc->mFormat;
3255 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003256 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003257 &output,
3258 &config,
3259 &desc->mDevice,
3260 address,
3261 &desc->mLatency,
3262 desc->mFlags);
3263 if (status == NO_ERROR) {
3264 desc->mSamplingRate = config.sample_rate;
3265 desc->mChannelMask = config.channel_mask;
3266 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003267
Eric Laurentd4692962014-05-05 18:13:44 -07003268 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003269 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003270 char *param = audio_device_address_to_parameter(device, address);
3271 mpClientInterface->setParameters(output, String8(param));
3272 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003273 }
3274
Eric Laurentd4692962014-05-05 18:13:44 -07003275 // Here is where we step through and resolve any "dynamic" fields
3276 String8 reply;
3277 char *value;
3278 if (profile->mSamplingRates[0] == 0) {
3279 reply = mpClientInterface->getParameters(output,
3280 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003281 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003282 reply.string());
3283 value = strpbrk((char *)reply.string(), "=");
3284 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003285 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003286 }
Eric Laurentd4692962014-05-05 18:13:44 -07003287 }
3288 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3289 reply = mpClientInterface->getParameters(output,
3290 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003291 ALOGV("checkOutputsForDevice() supported formats %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->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003296 }
Eric Laurentd4692962014-05-05 18:13:44 -07003297 }
3298 if (profile->mChannelMasks[0] == 0) {
3299 reply = mpClientInterface->getParameters(output,
3300 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003301 ALOGV("checkOutputsForDevice() supported channel masks %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->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003306 }
Eric Laurentd4692962014-05-05 18:13:44 -07003307 }
3308 if (((profile->mSamplingRates[0] == 0) &&
3309 (profile->mSamplingRates.size() < 2)) ||
3310 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3311 (profile->mFormats.size() < 2)) ||
3312 ((profile->mChannelMasks[0] == 0) &&
3313 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003314 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003315 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003316 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003317 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3318 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003319 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003320 config.sample_rate = profile->pickSamplingRate();
3321 config.channel_mask = profile->pickChannelMask();
3322 config.format = profile->pickFormat();
3323 config.offload_info.sample_rate = config.sample_rate;
3324 config.offload_info.channel_mask = config.channel_mask;
3325 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003326 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003327 &output,
3328 &config,
3329 &desc->mDevice,
3330 address,
3331 &desc->mLatency,
3332 desc->mFlags);
3333 if (status == NO_ERROR) {
3334 desc->mSamplingRate = config.sample_rate;
3335 desc->mChannelMask = config.channel_mask;
3336 desc->mFormat = config.format;
3337 } else {
3338 output = AUDIO_IO_HANDLE_NONE;
3339 }
Eric Laurentd4692962014-05-05 18:13:44 -07003340 }
3341
Eric Laurentcf2c0212014-07-25 16:20:43 -07003342 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003343 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003344 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003345 sp<AudioPolicyMix> policyMix;
3346 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003347 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3348 address.string());
3349 }
François Gaffie036e1e92015-03-19 10:16:24 +01003350 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003351 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003352
Eric Laurent87ffa392015-05-22 10:32:38 -07003353 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3354 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003355 // no duplicated output for direct outputs and
3356 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003357 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003358
Eric Laurentd4692962014-05-05 18:13:44 -07003359 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003360 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003361
Eric Laurentd4692962014-05-05 18:13:44 -07003362 //TODO: configure audio effect output stage here
3363
3364 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003365 duplicatedOutput =
3366 mpClientInterface->openDuplicateOutput(output,
3367 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003368 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003369 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003370 sp<SwAudioOutputDescriptor> dupOutputDesc =
3371 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3372 dupOutputDesc->mOutput1 = mPrimaryOutput;
3373 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003374 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3375 dupOutputDesc->mFormat = desc->mFormat;
3376 dupOutputDesc->mChannelMask = desc->mChannelMask;
3377 dupOutputDesc->mLatency = desc->mLatency;
3378 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003379 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003380 } else {
3381 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003382 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003383 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003384 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003385 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003386 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003387 }
Eric Laurente552edb2014-03-10 17:42:56 -07003388 }
3389 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003390 } else {
3391 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003392 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003393 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003394 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003395 profiles.removeAt(profile_index);
3396 profile_index--;
3397 } else {
3398 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003399 // Load digital format info only for digital devices
3400 if (audio_device_is_digital(device)) {
3401 devDesc->importAudioPort(profile);
3402 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003403
François Gaffie53615e22015-03-19 09:24:12 +01003404 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003405 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3406 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003407 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003408 NULL/*patch handle*/, address.string());
3409 }
Eric Laurente552edb2014-03-10 17:42:56 -07003410 ALOGV("checkOutputsForDevice(): adding output %d", output);
3411 }
3412 }
3413
3414 if (profiles.isEmpty()) {
3415 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3416 return BAD_VALUE;
3417 }
Eric Laurentd4692962014-05-05 18:13:44 -07003418 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003419 // check if one opened output is not needed any more after disconnecting one device
3420 for (size_t i = 0; i < mOutputs.size(); i++) {
3421 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003422 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003423 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003424 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003425 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003426 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003427 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003428 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3429 mOutputs.keyAt(i));
3430 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003431 }
Eric Laurente552edb2014-03-10 17:42:56 -07003432 }
3433 }
Eric Laurentd4692962014-05-05 18:13:44 -07003434 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003435 for (size_t i = 0; i < mHwModules.size(); i++)
3436 {
3437 if (mHwModules[i]->mHandle == 0) {
3438 continue;
3439 }
3440 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3441 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003442 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003443 if (profile->mSupportedDevices.types() & device) {
3444 ALOGV("checkOutputsForDevice(): "
3445 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003446 if (profile->mSamplingRates[0] == 0) {
3447 profile->mSamplingRates.clear();
3448 profile->mSamplingRates.add(0);
3449 }
3450 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3451 profile->mFormats.clear();
3452 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3453 }
3454 if (profile->mChannelMasks[0] == 0) {
3455 profile->mChannelMasks.clear();
3456 profile->mChannelMasks.add(0);
3457 }
3458 }
3459 }
3460 }
3461 }
3462 return NO_ERROR;
3463}
3464
Paul McLean9080a4c2015-06-18 08:24:02 -07003465status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003466 audio_policy_dev_state_t state,
3467 SortedVector<audio_io_handle_t>& inputs,
3468 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003469{
Paul McLean9080a4c2015-06-18 08:24:02 -07003470 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003471 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003472
3473 if (audio_device_is_digital(device)) {
3474 // erase all current sample rates, formats and channel masks
3475 devDesc->clearCapabilities();
3476 }
3477
Eric Laurentd4692962014-05-05 18:13:44 -07003478 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3479 // first list already open inputs that can be routed to this device
3480 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3481 desc = mInputs.valueAt(input_index);
3482 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3483 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3484 inputs.add(mInputs.keyAt(input_index));
3485 }
3486 }
3487
3488 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003489 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003490 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3491 {
3492 if (mHwModules[module_idx]->mHandle == 0) {
3493 continue;
3494 }
3495 for (size_t profile_index = 0;
3496 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3497 profile_index++)
3498 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003499 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3500
3501 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003502 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003503 address == profile->mSupportedDevices[0]->mAddress) {
3504 profiles.add(profile);
3505 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3506 profile_index, module_idx);
3507 }
Eric Laurentd4692962014-05-05 18:13:44 -07003508 }
3509 }
3510 }
3511
3512 if (profiles.isEmpty() && inputs.isEmpty()) {
3513 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3514 return BAD_VALUE;
3515 }
3516
3517 // open inputs for matching profiles if needed. Direct inputs are also opened to
3518 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3519 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3520
Eric Laurent1c333e22014-05-20 10:48:17 -07003521 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003522 // nothing to do if one input is already opened for this profile
3523 size_t input_index;
3524 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3525 desc = mInputs.valueAt(input_index);
3526 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003527 if (audio_device_is_digital(device)) {
3528 devDesc->importAudioPort(profile);
3529 }
Eric Laurentd4692962014-05-05 18:13:44 -07003530 break;
3531 }
3532 }
3533 if (input_index != mInputs.size()) {
3534 continue;
3535 }
3536
3537 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3538 desc = new AudioInputDescriptor(profile);
3539 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003540 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3541 config.sample_rate = desc->mSamplingRate;
3542 config.channel_mask = desc->mChannelMask;
3543 config.format = desc->mFormat;
3544 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003545 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003546 &input,
3547 &config,
3548 &desc->mDevice,
3549 address,
3550 AUDIO_SOURCE_MIC,
3551 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003552
Eric Laurentcf2c0212014-07-25 16:20:43 -07003553 if (status == NO_ERROR) {
3554 desc->mSamplingRate = config.sample_rate;
3555 desc->mChannelMask = config.channel_mask;
3556 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003557
Eric Laurentd4692962014-05-05 18:13:44 -07003558 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003559 char *param = audio_device_address_to_parameter(device, address);
3560 mpClientInterface->setParameters(input, String8(param));
3561 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003562 }
3563
3564 // Here is where we step through and resolve any "dynamic" fields
3565 String8 reply;
3566 char *value;
3567 if (profile->mSamplingRates[0] == 0) {
3568 reply = mpClientInterface->getParameters(input,
3569 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3570 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3571 reply.string());
3572 value = strpbrk((char *)reply.string(), "=");
3573 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003574 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003575 }
3576 }
3577 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3578 reply = mpClientInterface->getParameters(input,
3579 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3580 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3581 value = strpbrk((char *)reply.string(), "=");
3582 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003583 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003584 }
3585 }
3586 if (profile->mChannelMasks[0] == 0) {
3587 reply = mpClientInterface->getParameters(input,
3588 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3589 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3590 reply.string());
3591 value = strpbrk((char *)reply.string(), "=");
3592 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003593 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003594 }
3595 }
3596 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3597 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3598 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3599 ALOGW("checkInputsForDevice() direct input missing param");
3600 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003601 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003602 }
3603
3604 if (input != 0) {
3605 addInput(input, desc);
3606 }
3607 } // endif input != 0
3608
Eric Laurentcf2c0212014-07-25 16:20:43 -07003609 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003610 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003611 profiles.removeAt(profile_index);
3612 profile_index--;
3613 } else {
3614 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003615 if (audio_device_is_digital(device)) {
3616 devDesc->importAudioPort(profile);
3617 }
Eric Laurentd4692962014-05-05 18:13:44 -07003618 ALOGV("checkInputsForDevice(): adding input %d", input);
3619 }
3620 } // end scan profiles
3621
3622 if (profiles.isEmpty()) {
3623 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3624 return BAD_VALUE;
3625 }
3626 } else {
3627 // Disconnect
3628 // check if one opened input is not needed any more after disconnecting one device
3629 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3630 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003631 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3632 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003633 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3634 mInputs.keyAt(input_index));
3635 inputs.add(mInputs.keyAt(input_index));
3636 }
3637 }
3638 // Clear any profiles associated with the disconnected device.
3639 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3640 if (mHwModules[module_index]->mHandle == 0) {
3641 continue;
3642 }
3643 for (size_t profile_index = 0;
3644 profile_index < mHwModules[module_index]->mInputProfiles.size();
3645 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003646 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Paul McLean9080a4c2015-06-18 08:24:02 -07003647 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003648 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003649 profile_index, module_index);
3650 if (profile->mSamplingRates[0] == 0) {
3651 profile->mSamplingRates.clear();
3652 profile->mSamplingRates.add(0);
3653 }
3654 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3655 profile->mFormats.clear();
3656 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3657 }
3658 if (profile->mChannelMasks[0] == 0) {
3659 profile->mChannelMasks.clear();
3660 profile->mChannelMasks.add(0);
3661 }
3662 }
3663 }
3664 }
3665 } // end disconnect
3666
3667 return NO_ERROR;
3668}
3669
3670
Eric Laurente0720872014-03-11 09:30:41 -07003671void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003672{
3673 ALOGV("closeOutput(%d)", output);
3674
Eric Laurentc75307b2015-03-17 15:29:32 -07003675 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003676 if (outputDesc == NULL) {
3677 ALOGW("closeOutput() unknown output %d", output);
3678 return;
3679 }
François Gaffie036e1e92015-03-19 10:16:24 +01003680 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003681
Eric Laurente552edb2014-03-10 17:42:56 -07003682 // look for duplicated outputs connected to the output being removed.
3683 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003684 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003685 if (dupOutputDesc->isDuplicated() &&
3686 (dupOutputDesc->mOutput1 == outputDesc ||
3687 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003688 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003689 if (dupOutputDesc->mOutput1 == outputDesc) {
3690 outputDesc2 = dupOutputDesc->mOutput2;
3691 } else {
3692 outputDesc2 = dupOutputDesc->mOutput1;
3693 }
3694 // As all active tracks on duplicated output will be deleted,
3695 // and as they were also referenced on the other output, the reference
3696 // count for their stream type must be adjusted accordingly on
3697 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003698 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003699 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003700 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003701 }
3702 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3703 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3704
3705 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003706 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003707 }
3708 }
3709
Eric Laurent05b90f82014-08-27 15:32:29 -07003710 nextAudioPortGeneration();
3711
3712 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3713 if (index >= 0) {
3714 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3715 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3716 mAudioPatches.removeItemsAt(index);
3717 mpClientInterface->onAudioPatchListUpdate();
3718 }
3719
Eric Laurente552edb2014-03-10 17:42:56 -07003720 AudioParameter param;
3721 param.add(String8("closing"), String8("true"));
3722 mpClientInterface->setParameters(output, param.toString());
3723
3724 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003725 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003726 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003727}
3728
3729void AudioPolicyManager::closeInput(audio_io_handle_t input)
3730{
3731 ALOGV("closeInput(%d)", input);
3732
3733 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3734 if (inputDesc == NULL) {
3735 ALOGW("closeInput() unknown input %d", input);
3736 return;
3737 }
3738
Eric Laurent6a94d692014-05-20 11:18:06 -07003739 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003740
3741 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3742 if (index >= 0) {
3743 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3744 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3745 mAudioPatches.removeItemsAt(index);
3746 mpClientInterface->onAudioPatchListUpdate();
3747 }
3748
3749 mpClientInterface->closeInput(input);
3750 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003751}
3752
Eric Laurentc75307b2015-03-17 15:29:32 -07003753SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3754 audio_devices_t device,
3755 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003756{
3757 SortedVector<audio_io_handle_t> outputs;
3758
3759 ALOGVV("getOutputsForDevice() device %04x", device);
3760 for (size_t i = 0; i < openOutputs.size(); i++) {
3761 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003762 i, openOutputs.valueAt(i)->isDuplicated(),
3763 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003764 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3765 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3766 outputs.add(openOutputs.keyAt(i));
3767 }
3768 }
3769 return outputs;
3770}
3771
Eric Laurente0720872014-03-11 09:30:41 -07003772bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003773 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003774{
3775 if (outputs1.size() != outputs2.size()) {
3776 return false;
3777 }
3778 for (size_t i = 0; i < outputs1.size(); i++) {
3779 if (outputs1[i] != outputs2[i]) {
3780 return false;
3781 }
3782 }
3783 return true;
3784}
3785
Eric Laurente0720872014-03-11 09:30:41 -07003786void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003787{
3788 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3789 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3790 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3791 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3792
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003793 // also take into account external policy-related changes: add all outputs which are
3794 // associated with policies in the "before" and "after" output vectors
3795 ALOGVV("checkOutputForStrategy(): policy related outputs");
3796 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003797 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003798 if (desc != 0 && desc->mPolicyMix != NULL) {
3799 srcOutputs.add(desc->mIoHandle);
3800 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3801 }
3802 }
3803 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003804 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003805 if (desc != 0 && desc->mPolicyMix != NULL) {
3806 dstOutputs.add(desc->mIoHandle);
3807 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3808 }
3809 }
3810
Eric Laurente552edb2014-03-10 17:42:56 -07003811 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3812 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3813 strategy, srcOutputs[0], dstOutputs[0]);
3814 // mute strategy while moving tracks from one output to another
3815 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003816 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003817 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003818 setStrategyMute(strategy, true, desc);
3819 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003820 }
3821 }
3822
3823 // Move effects associated to this strategy from previous output to new output
3824 if (strategy == STRATEGY_MEDIA) {
3825 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3826 SortedVector<audio_io_handle_t> moved;
3827 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003828 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3829 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3830 effectDesc->mIo != fxOutput) {
3831 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003832 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3833 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003834 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003835 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003836 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003837 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003838 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003839 }
3840 }
3841 }
3842 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003843 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003844 if (i == AUDIO_STREAM_PATCH) {
3845 continue;
3846 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003847 if (getStrategy((audio_stream_type_t)i) == strategy) {
3848 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 }
3850 }
3851 }
3852}
3853
Eric Laurente0720872014-03-11 09:30:41 -07003854void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003855{
François Gaffie2110e042015-03-24 08:41:51 +01003856 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003857 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003858 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003859 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003860 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003861 checkOutputForStrategy(STRATEGY_SONIFICATION);
3862 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003863 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003864 checkOutputForStrategy(STRATEGY_MEDIA);
3865 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003866 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003867}
3868
Eric Laurente0720872014-03-11 09:30:41 -07003869void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003870{
François Gaffie53615e22015-03-19 09:24:12 +01003871 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003872 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003873 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003874 return;
3875 }
3876
Eric Laurent3a4311c2014-03-17 12:00:47 -07003877 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003878 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3879 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3880 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003881 // suspend A2DP output if:
3882 // (NOT already suspended) &&
3883 // ((SCO device is connected &&
3884 // (forced usage for communication || for record is SCO))) ||
3885 // (phone state is ringing || in call)
3886 //
3887 // restore A2DP output if:
3888 // (Already suspended) &&
3889 // ((SCO device is NOT connected ||
3890 // (forced usage NOT for communication && NOT for record is SCO))) &&
3891 // (phone state is NOT ringing && NOT in call)
3892 //
3893 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003894 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003895 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3896 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3897 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3898 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003899
3900 mpClientInterface->restoreOutput(a2dpOutput);
3901 mA2dpSuspended = false;
3902 }
3903 } else {
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->suspendOutput(a2dpOutput);
3911 mA2dpSuspended = true;
3912 }
3913 }
3914}
3915
Eric Laurentc75307b2015-03-17 15:29:32 -07003916audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3917 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003918{
3919 audio_devices_t device = AUDIO_DEVICE_NONE;
3920
Eric Laurent6a94d692014-05-20 11:18:06 -07003921 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3922 if (index >= 0) {
3923 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3924 if (patchDesc->mUid != mUidCached) {
3925 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3926 outputDesc->device(), outputDesc->mPatchHandle);
3927 return outputDesc->device();
3928 }
3929 }
3930
Eric Laurente552edb2014-03-10 17:42:56 -07003931 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003932 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003933 // use device for strategy enforced audible
3934 // 2: we are in call or the strategy phone is active on the output:
3935 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003936 // 3: the strategy for enforced audible is active but not enforced on the output:
3937 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003938 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08003939 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003940 // 5: the strategy sonification is active on the output:
3941 // use device for strategy sonification
3942 // 6: the strategy "respectful" sonification is active on the output:
3943 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003944 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003945 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003946 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003947 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003948 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003949 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003950 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003951 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003952 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3953 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003954 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003955 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003956 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003957 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003958 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
3959 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003960 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003961 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003962 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003963 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003964 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003965 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003966 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003967 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003968 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003969 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003970 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003971 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003972 }
3973
Eric Laurent1c333e22014-05-20 10:48:17 -07003974 ALOGV("getNewOutputDevice() selected device %x", device);
3975 return device;
3976}
3977
3978audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3979{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003980 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003981
3982 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3983 if (index >= 0) {
3984 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3985 if (patchDesc->mUid != mUidCached) {
3986 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3987 inputDesc->mDevice, inputDesc->mPatchHandle);
3988 return inputDesc->mDevice;
3989 }
3990 }
3991
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003992 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003993
Eric Laurente552edb2014-03-10 17:42:56 -07003994 return device;
3995}
3996
Eric Laurente0720872014-03-11 09:30:41 -07003997uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003998 return (uint32_t)getStrategy(stream);
3999}
4000
Eric Laurente0720872014-03-11 09:30:41 -07004001audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004002 // By checking the range of stream before calling getStrategy, we avoid
4003 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4004 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004005 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004006 return AUDIO_DEVICE_NONE;
4007 }
4008 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004009 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004010 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4011 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4012 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004013 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004014 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004015 devices = outputDesc->device();
4016 break;
4017 }
Eric Laurente552edb2014-03-10 17:42:56 -07004018 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004019
4020 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4021 and doesn't really need to.*/
4022 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4023 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4024 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4025 }
4026
Eric Laurente552edb2014-03-10 17:42:56 -07004027 return devices;
4028}
4029
François Gaffie2110e042015-03-24 08:41:51 +01004030routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4031{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004032 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004033 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004034}
4035
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004036uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4037 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004038 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4039 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4040 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004041 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4042 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4043 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004044 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004045 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004046}
4047
Eric Laurente0720872014-03-11 09:30:41 -07004048void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004049 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004050 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004051 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4052 updateDevicesAndOutputs();
4053 break;
4054 default:
4055 break;
4056 }
4057}
4058
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004059uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004060
4061 // skip beacon mute management if a dedicated TTS output is available
4062 if (mTtsOutputAvailable) {
4063 return 0;
4064 }
4065
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004066 switch(event) {
4067 case STARTING_OUTPUT:
4068 mBeaconMuteRefCount++;
4069 break;
4070 case STOPPING_OUTPUT:
4071 if (mBeaconMuteRefCount > 0) {
4072 mBeaconMuteRefCount--;
4073 }
4074 break;
4075 case STARTING_BEACON:
4076 mBeaconPlayingRefCount++;
4077 break;
4078 case STOPPING_BEACON:
4079 if (mBeaconPlayingRefCount > 0) {
4080 mBeaconPlayingRefCount--;
4081 }
4082 break;
4083 }
4084
4085 if (mBeaconMuteRefCount > 0) {
4086 // any playback causes beacon to be muted
4087 return setBeaconMute(true);
4088 } else {
4089 // no other playback: unmute when beacon starts playing, mute when it stops
4090 return setBeaconMute(mBeaconPlayingRefCount == 0);
4091 }
4092}
4093
4094uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4095 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4096 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4097 // keep track of muted state to avoid repeating mute/unmute operations
4098 if (mBeaconMuted != mute) {
4099 // mute/unmute AUDIO_STREAM_TTS on all outputs
4100 ALOGV("\t muting %d", mute);
4101 uint32_t maxLatency = 0;
4102 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004103 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004104 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004105 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004106 0 /*delay*/, AUDIO_DEVICE_NONE);
4107 const uint32_t latency = desc->latency() * 2;
4108 if (latency > maxLatency) {
4109 maxLatency = latency;
4110 }
4111 }
4112 mBeaconMuted = mute;
4113 return maxLatency;
4114 }
4115 return 0;
4116}
4117
Eric Laurente0720872014-03-11 09:30:41 -07004118audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004119 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004120{
Paul McLeanaa981192015-03-21 09:55:15 -07004121 // Routing
4122 // see if we have an explicit route
4123 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4124 // (getStrategy(stream)).
4125 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4126 // and activity count is non-zero
4127 // the device = the device from the descriptor in the RouteMap, and exit.
4128 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4129 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4130 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004131 // Special case for accessibility strategy which must follow any strategy it is
4132 // currently remapped to
4133 bool strategyMatch = (strat == strategy) ||
4134 ((strategy == STRATEGY_ACCESSIBILITY) &&
4135 ((mEngine->getStrategyForUsage(
4136 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4137 (strat == STRATEGY_MEDIA)));
4138 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004139 return route->mDeviceDescriptor->type();
4140 }
4141 }
4142
Eric Laurente552edb2014-03-10 17:42:56 -07004143 if (fromCache) {
4144 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4145 strategy, mDeviceForStrategy[strategy]);
4146 return mDeviceForStrategy[strategy];
4147 }
François Gaffie2110e042015-03-24 08:41:51 +01004148 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004149}
4150
Eric Laurente0720872014-03-11 09:30:41 -07004151void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004152{
4153 for (int i = 0; i < NUM_STRATEGIES; i++) {
4154 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4155 }
4156 mPreviousOutputs = mOutputs;
4157}
4158
Eric Laurent1f2f2232014-06-02 12:01:23 -07004159uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004160 audio_devices_t prevDevice,
4161 uint32_t delayMs)
4162{
4163 // mute/unmute strategies using an incompatible device combination
4164 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4165 // if unmuting, unmute only after the specified delay
4166 if (outputDesc->isDuplicated()) {
4167 return 0;
4168 }
4169
4170 uint32_t muteWaitMs = 0;
4171 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004172 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004173
4174 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4175 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004176 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004177 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4178 bool doMute = false;
4179
4180 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4181 doMute = true;
4182 outputDesc->mStrategyMutedByDevice[i] = true;
4183 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4184 doMute = true;
4185 outputDesc->mStrategyMutedByDevice[i] = false;
4186 }
Eric Laurent99401132014-05-07 19:48:15 -07004187 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004188 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004189 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004190 // skip output if it does not share any device with current output
4191 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4192 == AUDIO_DEVICE_NONE) {
4193 continue;
4194 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004195 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4196 mute ? "muting" : "unmuting", i, curDevice);
4197 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004198 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004199 if (mute) {
4200 // FIXME: should not need to double latency if volume could be applied
4201 // immediately by the audioflinger mixer. We must account for the delay
4202 // between now and the next time the audioflinger thread for this output
4203 // will process a buffer (which corresponds to one buffer size,
4204 // usually 1/2 or 1/4 of the latency).
4205 if (muteWaitMs < desc->latency() * 2) {
4206 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004207 }
4208 }
4209 }
4210 }
4211 }
4212 }
4213
Eric Laurent99401132014-05-07 19:48:15 -07004214 // temporary mute output if device selection changes to avoid volume bursts due to
4215 // different per device volumes
4216 if (outputDesc->isActive() && (device != prevDevice)) {
4217 if (muteWaitMs < outputDesc->latency() * 2) {
4218 muteWaitMs = outputDesc->latency() * 2;
4219 }
4220 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004221 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004222 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004223 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004224 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004225 muteWaitMs *2, device);
4226 }
4227 }
4228 }
4229
Eric Laurente552edb2014-03-10 17:42:56 -07004230 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4231 if (muteWaitMs > delayMs) {
4232 muteWaitMs -= delayMs;
4233 usleep(muteWaitMs * 1000);
4234 return muteWaitMs;
4235 }
4236 return 0;
4237}
4238
Eric Laurentc75307b2015-03-17 15:29:32 -07004239uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004240 audio_devices_t device,
4241 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004242 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004243 audio_patch_handle_t *patchHandle,
4244 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004245{
Eric Laurentc75307b2015-03-17 15:29:32 -07004246 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004247 AudioParameter param;
4248 uint32_t muteWaitMs;
4249
4250 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004251 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4252 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004253 return muteWaitMs;
4254 }
4255 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4256 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004257 if ((device != AUDIO_DEVICE_NONE) &&
4258 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004259 return 0;
4260 }
4261
4262 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004263 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004264
4265 audio_devices_t prevDevice = outputDesc->mDevice;
4266
Paul McLeanaa981192015-03-21 09:55:15 -07004267 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004268
4269 if (device != AUDIO_DEVICE_NONE) {
4270 outputDesc->mDevice = device;
4271 }
4272 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4273
4274 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004275 // the requested device is AUDIO_DEVICE_NONE
4276 // OR the requested device is the same as current device
4277 // AND force is not specified
4278 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004279 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004280 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4281 !force &&
4282 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004283 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004284 return muteWaitMs;
4285 }
4286
4287 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004288
Eric Laurente552edb2014-03-10 17:42:56 -07004289 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004290 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004291 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004292 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004293 DeviceVector deviceList = (address == NULL) ?
4294 mAvailableOutputDevices.getDevicesFromType(device)
4295 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004296 if (!deviceList.isEmpty()) {
4297 struct audio_patch patch;
4298 outputDesc->toAudioPortConfig(&patch.sources[0]);
4299 patch.num_sources = 1;
4300 patch.num_sinks = 0;
4301 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4302 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004303 patch.num_sinks++;
4304 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004305 ssize_t index;
4306 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4307 index = mAudioPatches.indexOfKey(*patchHandle);
4308 } else {
4309 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4310 }
4311 sp< AudioPatch> patchDesc;
4312 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4313 if (index >= 0) {
4314 patchDesc = mAudioPatches.valueAt(index);
4315 afPatchHandle = patchDesc->mAfPatchHandle;
4316 }
4317
Eric Laurent1c333e22014-05-20 10:48:17 -07004318 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004319 &afPatchHandle,
4320 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004321 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4322 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004323 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004324 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004325 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004326 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004327 addAudioPatch(patchDesc->mHandle, patchDesc);
4328 } else {
4329 patchDesc->mPatch = patch;
4330 }
4331 patchDesc->mAfPatchHandle = afPatchHandle;
4332 patchDesc->mUid = mUidCached;
4333 if (patchHandle) {
4334 *patchHandle = patchDesc->mHandle;
4335 }
4336 outputDesc->mPatchHandle = patchDesc->mHandle;
4337 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004338 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004339 }
4340 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004341
4342 // inform all input as well
4343 for (size_t i = 0; i < mInputs.size(); i++) {
4344 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004345 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004346 AudioParameter inputCmd = AudioParameter();
4347 ALOGV("%s: inform input %d of device:%d", __func__,
4348 inputDescriptor->mIoHandle, device);
4349 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4350 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4351 inputCmd.toString(),
4352 delayMs);
4353 }
4354 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004355 }
Eric Laurente552edb2014-03-10 17:42:56 -07004356
4357 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004358 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004359
4360 return muteWaitMs;
4361}
4362
Eric Laurentc75307b2015-03-17 15:29:32 -07004363status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004364 int delayMs,
4365 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004366{
Eric Laurent6a94d692014-05-20 11:18:06 -07004367 ssize_t index;
4368 if (patchHandle) {
4369 index = mAudioPatches.indexOfKey(*patchHandle);
4370 } else {
4371 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4372 }
4373 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004374 return INVALID_OPERATION;
4375 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004376 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4377 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004378 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4379 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004380 removeAudioPatch(patchDesc->mHandle);
4381 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004382 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004383 return status;
4384}
4385
4386status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4387 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004388 bool force,
4389 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004390{
4391 status_t status = NO_ERROR;
4392
Eric Laurent1f2f2232014-06-02 12:01:23 -07004393 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004394 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4395 inputDesc->mDevice = device;
4396
4397 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4398 if (!deviceList.isEmpty()) {
4399 struct audio_patch patch;
4400 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004401 // AUDIO_SOURCE_HOTWORD is for internal use only:
4402 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004403 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4404 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004405 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4406 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004407 patch.num_sinks = 1;
4408 //only one input device for now
4409 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004410 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004411 ssize_t index;
4412 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4413 index = mAudioPatches.indexOfKey(*patchHandle);
4414 } else {
4415 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4416 }
4417 sp< AudioPatch> patchDesc;
4418 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4419 if (index >= 0) {
4420 patchDesc = mAudioPatches.valueAt(index);
4421 afPatchHandle = patchDesc->mAfPatchHandle;
4422 }
4423
Eric Laurent1c333e22014-05-20 10:48:17 -07004424 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004425 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004426 0);
4427 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004428 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004429 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004430 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004431 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004432 addAudioPatch(patchDesc->mHandle, patchDesc);
4433 } else {
4434 patchDesc->mPatch = patch;
4435 }
4436 patchDesc->mAfPatchHandle = afPatchHandle;
4437 patchDesc->mUid = mUidCached;
4438 if (patchHandle) {
4439 *patchHandle = patchDesc->mHandle;
4440 }
4441 inputDesc->mPatchHandle = patchDesc->mHandle;
4442 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004443 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004444 }
4445 }
4446 }
4447 return status;
4448}
4449
Eric Laurent6a94d692014-05-20 11:18:06 -07004450status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4451 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004452{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004453 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004454 ssize_t index;
4455 if (patchHandle) {
4456 index = mAudioPatches.indexOfKey(*patchHandle);
4457 } else {
4458 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4459 }
4460 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004461 return INVALID_OPERATION;
4462 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004463 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4464 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004465 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4466 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004467 removeAudioPatch(patchDesc->mHandle);
4468 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004469 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004470 return status;
4471}
4472
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004473sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004474 String8 address,
4475 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004476 audio_format_t& format,
4477 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004478 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004479{
4480 // Choose an input profile based on the requested capture parameters: select the first available
4481 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004482 //
4483 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4484 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004485
4486 for (size_t i = 0; i < mHwModules.size(); i++)
4487 {
4488 if (mHwModules[i]->mHandle == 0) {
4489 continue;
4490 }
4491 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4492 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004493 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004494 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004495 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004496 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004497 format,
4498 &format /*updatedFormat*/,
4499 channelMask,
4500 &channelMask /*updatedChannelMask*/,
4501 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004502
Eric Laurente552edb2014-03-10 17:42:56 -07004503 return profile;
4504 }
4505 }
4506 }
4507 return NULL;
4508}
4509
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004510
4511audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004512 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004513{
François Gaffie036e1e92015-03-19 10:16:24 +01004514 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4515 audio_devices_t selectedDeviceFromMix =
4516 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004517
François Gaffie036e1e92015-03-19 10:16:24 +01004518 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4519 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004520 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004521 return getDeviceForInputSource(inputSource);
4522}
4523
4524audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4525{
Paul McLean466dc8e2015-04-17 13:15:36 -06004526 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4527 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004528 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004529 return route->mDeviceDescriptor->type();
4530 }
4531 }
4532
4533 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004534}
4535
Eric Laurente0720872014-03-11 09:30:41 -07004536float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004537 int index,
4538 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004539{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004540 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004541
4542 // if a headset is connected, apply the following rules to ring tones and notifications
4543 // to avoid sound level bursts in user's ears:
4544 // - always attenuate ring tones and notifications volume by 6dB
4545 // - if music is playing, always limit the volume to current music volume,
4546 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004547 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004548 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4549 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4550 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4551 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4552 ((stream_strategy == STRATEGY_SONIFICATION)
4553 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004554 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004555 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004556 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4557 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004558 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004559 // when the phone is ringing we must consider that music could have been paused just before
4560 // by the music application and behave as if music was active if the last music track was
4561 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004562 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004563 mLimitRingtoneVolume) {
4564 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004565 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4566 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004567 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004568 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4569 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4570 if (volumeDb > minVolDB) {
4571 volumeDb = minVolDB;
4572 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004573 }
4574 }
4575 }
4576
Eric Laurentffbc80f2015-03-18 18:30:19 -07004577 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004578}
4579
Eric Laurente0720872014-03-11 09:30:41 -07004580status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004581 int index,
4582 const sp<AudioOutputDescriptor>& outputDesc,
4583 audio_devices_t device,
4584 int delayMs,
4585 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004586{
Eric Laurente552edb2014-03-10 17:42:56 -07004587 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004588 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004589 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004590 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004591 return NO_ERROR;
4592 }
François Gaffie2110e042015-03-24 08:41:51 +01004593 audio_policy_forced_cfg_t forceUseForComm =
4594 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004595 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004596 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4597 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004598 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004599 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004600 return INVALID_OPERATION;
4601 }
4602
Eric Laurentc75307b2015-03-17 15:29:32 -07004603 if (device == AUDIO_DEVICE_NONE) {
4604 device = outputDesc->device();
4605 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004606
Eric Laurentffbc80f2015-03-18 18:30:19 -07004607 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004608 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004609 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004610 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004611
Eric Laurentffbc80f2015-03-18 18:30:19 -07004612 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004613
Eric Laurent3b73df72014-03-11 09:06:29 -07004614 if (stream == AUDIO_STREAM_VOICE_CALL ||
4615 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004616 float voiceVolume;
4617 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004618 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004619 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004620 } else {
4621 voiceVolume = 1.0;
4622 }
4623
Eric Laurentc75307b2015-03-17 15:29:32 -07004624 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004625 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4626 mLastVoiceVolume = voiceVolume;
4627 }
4628 }
4629
4630 return NO_ERROR;
4631}
4632
Eric Laurentc75307b2015-03-17 15:29:32 -07004633void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4634 audio_devices_t device,
4635 int delayMs,
4636 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004637{
Eric Laurentc75307b2015-03-17 15:29:32 -07004638 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004639
Eric Laurent3b73df72014-03-11 09:06:29 -07004640 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004641 if (stream == AUDIO_STREAM_PATCH) {
4642 continue;
4643 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004644 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004645 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4646 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004647 device,
4648 delayMs,
4649 force);
4650 }
4651}
4652
Eric Laurente0720872014-03-11 09:30:41 -07004653void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004654 bool on,
4655 const sp<AudioOutputDescriptor>& outputDesc,
4656 int delayMs,
4657 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004658{
Eric Laurent72249d52015-06-08 11:12:15 -07004659 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4660 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004661 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004662 if (stream == AUDIO_STREAM_PATCH) {
4663 continue;
4664 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004665 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004666 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004667 }
4668 }
4669}
4670
Eric Laurente0720872014-03-11 09:30:41 -07004671void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004672 bool on,
4673 const sp<AudioOutputDescriptor>& outputDesc,
4674 int delayMs,
4675 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004676{
Eric Laurentc75307b2015-03-17 15:29:32 -07004677 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004678 if (device == AUDIO_DEVICE_NONE) {
4679 device = outputDesc->device();
4680 }
4681
Eric Laurentc75307b2015-03-17 15:29:32 -07004682 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4683 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004684
4685 if (on) {
4686 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004687 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004688 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004689 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004690 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004691 }
4692 }
4693 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4694 outputDesc->mMuteCount[stream]++;
4695 } else {
4696 if (outputDesc->mMuteCount[stream] == 0) {
4697 ALOGV("setStreamMute() unmuting non muted stream!");
4698 return;
4699 }
4700 if (--outputDesc->mMuteCount[stream] == 0) {
4701 checkAndSetVolume(stream,
4702 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004703 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004704 device,
4705 delayMs);
4706 }
4707 }
4708}
4709
Eric Laurente0720872014-03-11 09:30:41 -07004710void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004711 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004712{
Eric Laurent87ffa392015-05-22 10:32:38 -07004713 if(!hasPrimaryOutput()) {
4714 return;
4715 }
4716
Eric Laurente552edb2014-03-10 17:42:56 -07004717 // if the stream pertains to sonification strategy and we are in call we must
4718 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4719 // in the device used for phone strategy and play the tone if the selected device does not
4720 // interfere with the device used for phone strategy
4721 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4722 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004723 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004724 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4725 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004726 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004727 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4728 stream, starting, outputDesc->mDevice, stateChange);
4729 if (outputDesc->mRefCount[stream]) {
4730 int muteCount = 1;
4731 if (stateChange) {
4732 muteCount = outputDesc->mRefCount[stream];
4733 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004734 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004735 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4736 for (int i = 0; i < muteCount; i++) {
4737 setStreamMute(stream, starting, mPrimaryOutput);
4738 }
4739 } else {
4740 ALOGV("handleIncallSonification() high visibility");
4741 if (outputDesc->device() &
4742 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4743 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4744 for (int i = 0; i < muteCount; i++) {
4745 setStreamMute(stream, starting, mPrimaryOutput);
4746 }
4747 }
4748 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004749 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4750 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004751 } else {
4752 mpClientInterface->stopTone();
4753 }
4754 }
4755 }
4756 }
4757}
4758
Paul McLeanaa981192015-03-21 09:55:15 -07004759
Paul McLean466dc8e2015-04-17 13:15:36 -06004760
Eric Laurente0720872014-03-11 09:30:41 -07004761void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004762{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004763 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004764 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004765 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004766 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004767 mAvailableOutputDevices.add(mDefaultOutputDevice);
4768 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004769
4770 module = new HwModule("primary");
4771
Eric Laurent322b4d22015-04-03 15:57:54 -07004772 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4773 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004774 profile->mSamplingRates.add(44100);
4775 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4776 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004777 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004778 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4779 module->mOutputProfiles.add(profile);
4780
Eric Laurent322b4d22015-04-03 15:57:54 -07004781 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4782 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004783 profile->mSamplingRates.add(8000);
4784 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4785 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004786 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004787 module->mInputProfiles.add(profile);
4788
4789 mHwModules.add(module);
4790}
4791
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004792audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4793{
4794 // flags to stream type mapping
4795 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4796 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4797 }
4798 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4799 return AUDIO_STREAM_BLUETOOTH_SCO;
4800 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004801 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4802 return AUDIO_STREAM_TTS;
4803 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004804
4805 // usage to stream type mapping
4806 switch (attr->usage) {
4807 case AUDIO_USAGE_MEDIA:
4808 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004809 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4810 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004811 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004812 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4813 return AUDIO_STREAM_ALARM;
4814 }
4815 if (isStreamActive(AUDIO_STREAM_RING)) {
4816 return AUDIO_STREAM_RING;
4817 }
4818 if (isInCall()) {
4819 return AUDIO_STREAM_VOICE_CALL;
4820 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004821 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004822 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4823 return AUDIO_STREAM_SYSTEM;
4824 case AUDIO_USAGE_VOICE_COMMUNICATION:
4825 return AUDIO_STREAM_VOICE_CALL;
4826
4827 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4828 return AUDIO_STREAM_DTMF;
4829
4830 case AUDIO_USAGE_ALARM:
4831 return AUDIO_STREAM_ALARM;
4832 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4833 return AUDIO_STREAM_RING;
4834
4835 case AUDIO_USAGE_NOTIFICATION:
4836 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4837 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4838 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4839 case AUDIO_USAGE_NOTIFICATION_EVENT:
4840 return AUDIO_STREAM_NOTIFICATION;
4841
4842 case AUDIO_USAGE_UNKNOWN:
4843 default:
4844 return AUDIO_STREAM_MUSIC;
4845 }
4846}
Eric Laurente83b55d2014-11-14 10:06:21 -08004847
François Gaffie53615e22015-03-19 09:24:12 +01004848bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4849{
Eric Laurente83b55d2014-11-14 10:06:21 -08004850 // has flags that map to a strategy?
4851 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4852 return true;
4853 }
4854
4855 // has known usage?
4856 switch (paa->usage) {
4857 case AUDIO_USAGE_UNKNOWN:
4858 case AUDIO_USAGE_MEDIA:
4859 case AUDIO_USAGE_VOICE_COMMUNICATION:
4860 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4861 case AUDIO_USAGE_ALARM:
4862 case AUDIO_USAGE_NOTIFICATION:
4863 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4864 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4865 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4866 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4867 case AUDIO_USAGE_NOTIFICATION_EVENT:
4868 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4869 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4870 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4871 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004872 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004873 break;
4874 default:
4875 return false;
4876 }
4877 return true;
4878}
4879
François Gaffiead3183e2015-03-18 16:55:35 +01004880bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4881 routing_strategy strategy, uint32_t inPastMs,
4882 nsecs_t sysTime) const
4883{
4884 if ((sysTime == 0) && (inPastMs != 0)) {
4885 sysTime = systemTime();
4886 }
4887 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4888 if (i == AUDIO_STREAM_PATCH) {
4889 continue;
4890 }
4891 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4892 (NUM_STRATEGIES == strategy)) &&
4893 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4894 return true;
4895 }
4896 }
4897 return false;
4898}
4899
François Gaffie2110e042015-03-24 08:41:51 +01004900audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4901{
4902 return mEngine->getForceUse(usage);
4903}
4904
4905bool AudioPolicyManager::isInCall()
4906{
4907 return isStateInCall(mEngine->getPhoneState());
4908}
4909
4910bool AudioPolicyManager::isStateInCall(int state)
4911{
4912 return is_state_in_call(state);
4913}
4914
Eric Laurente552edb2014-03-10 17:42:56 -07004915}; // namespace android