blob: 5ff1c0bbd30ccb7d809b42a34fb613a23689397e [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700216 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Paul McLean9080a4c2015-06-18 08:24:02 -0700250 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
Eric Laurentc0a889f2015-10-14 14:36:34 -0700405 // terminate active capture if on the same HW module as the call TX source device
406 // FIXME: would be better to refine to only inputs whose profile connects to the
407 // call TX device but this information is not in the audio patch and logic here must be
408 // symmetric to the one in startInput()
409 audio_io_handle_t activeInput = mInputs.getActiveInput();
410 if (activeInput != 0) {
411 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
412 if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) {
413 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
414 stopInput(activeInput, activeSession);
415 releaseInput(activeInput, activeSession);
416 }
417 }
418
Eric Laurentc2730ba2014-07-20 15:47:07 -0700419 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
420 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
421 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
422 status);
423 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100424 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700425 mCallTxPatch->mAfPatchHandle = afPatchHandle;
426 mCallTxPatch->mUid = mUidCached;
427 }
428 }
429}
430
Eric Laurente0720872014-03-11 09:30:41 -0700431void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700432{
433 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100434 // store previous phone state for management of sonification strategy below
435 int oldState = mEngine->getPhoneState();
436
437 if (mEngine->setPhoneState(state) != NO_ERROR) {
438 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700439 return;
440 }
François Gaffie2110e042015-03-24 08:41:51 +0100441 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700442 // if leaving call state, handle special case of active streams
443 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700444 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700445 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700446 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800447 if (stream == AUDIO_STREAM_PATCH) {
448 continue;
449 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700450 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700451 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800452
Eric Laurent63dea1d2015-07-02 17:10:28 -0700453 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800454 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700455 }
456
François Gaffie2110e042015-03-24 08:41:51 +0100457 /**
458 * Switching to or from incall state or switching between telephony and VoIP lead to force
459 * routing command.
460 */
461 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
462 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700463
464 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700465 checkA2dpSuspend();
466 checkOutputForAllStrategies();
467 updateDevicesAndOutputs();
468
Eric Laurente552edb2014-03-10 17:42:56 -0700469 int delayMs = 0;
470 if (isStateInCall(state)) {
471 nsecs_t sysTime = systemTime();
472 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700473 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700474 // mute media and sonification strategies and delay device switch by the largest
475 // latency of any output where either strategy is active.
476 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100477 if ((isStrategyActive(desc, STRATEGY_MEDIA,
478 SONIFICATION_HEADSET_MUSIC_DELAY,
479 sysTime) ||
480 isStrategyActive(desc, STRATEGY_SONIFICATION,
481 SONIFICATION_HEADSET_MUSIC_DELAY,
482 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700483 (delayMs < (int)desc->latency()*2)) {
484 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700485 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700486 setStrategyMute(STRATEGY_MEDIA, true, desc);
487 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700488 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700489 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
490 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700491 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
492 }
493 }
494
Eric Laurent87ffa392015-05-22 10:32:38 -0700495 if (hasPrimaryOutput()) {
496 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
497 // the device returned is not necessarily reachable via this output
498 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
499 // force routing command to audio hardware when ending call
500 // even if no device change is needed
501 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
502 rxDevice = mPrimaryOutput->device();
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504
Eric Laurent87ffa392015-05-22 10:32:38 -0700505 if (state == AUDIO_MODE_IN_CALL) {
506 updateCallRouting(rxDevice, delayMs);
507 } else if (oldState == AUDIO_MODE_IN_CALL) {
508 if (mCallRxPatch != 0) {
509 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
510 mCallRxPatch.clear();
511 }
512 if (mCallTxPatch != 0) {
513 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
514 mCallTxPatch.clear();
515 }
516 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
517 } else {
518 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700519 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700520 }
Eric Laurente552edb2014-03-10 17:42:56 -0700521 // if entering in call state, handle special case of active streams
522 // pertaining to sonification strategy see handleIncallSonification()
523 if (isStateInCall(state)) {
524 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700525 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800526 if (stream == AUDIO_STREAM_PATCH) {
527 continue;
528 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700529 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700530 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700531
532 // force reevaluating accessibility routing when call starts
533 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700534 }
535
536 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700537 if (state == AUDIO_MODE_RINGTONE &&
538 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700539 mLimitRingtoneVolume = true;
540 } else {
541 mLimitRingtoneVolume = false;
542 }
543}
544
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700545audio_mode_t AudioPolicyManager::getPhoneState() {
546 return mEngine->getPhoneState();
547}
548
Eric Laurente0720872014-03-11 09:30:41 -0700549void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700550 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700551{
François Gaffie2110e042015-03-24 08:41:51 +0100552 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700553
François Gaffie2110e042015-03-24 08:41:51 +0100554 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
555 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
556 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700557 }
François Gaffie2110e042015-03-24 08:41:51 +0100558 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
559 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
560 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700561
562 // check for device and output changes triggered by new force usage
563 checkA2dpSuspend();
564 checkOutputForAllStrategies();
565 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700566 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700567 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
568 updateCallRouting(newDevice);
569 }
Eric Laurente552edb2014-03-10 17:42:56 -0700570 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700571 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
572 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
573 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
574 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700575 }
Eric Laurente552edb2014-03-10 17:42:56 -0700576 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700577 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700578 }
579 }
580
François Gaffie53615e22015-03-19 09:24:12 +0100581 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700582 if (activeInput != 0) {
Eric Laurentc171c7c2015-09-25 12:21:06 -0700583 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
584 audio_devices_t newDevice = getNewInputDevice(activeInput);
585 // Force new input selection if the new device can not be reached via current input
586 if (activeDesc->mProfile->mSupportedDevices.types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) {
587 setInputDevice(activeInput, newDevice);
588 } else {
589 closeInput(activeInput);
590 }
Eric Laurente552edb2014-03-10 17:42:56 -0700591 }
Eric Laurente552edb2014-03-10 17:42:56 -0700592}
593
Eric Laurente0720872014-03-11 09:30:41 -0700594void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700595{
596 ALOGV("setSystemProperty() property %s, value %s", property, value);
597}
598
599// Find a direct output profile compatible with the parameters passed, even if the input flags do
600// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800601sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700602 audio_devices_t device,
603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags)
607{
Eric Laurent861a6282015-05-18 15:40:16 -0700608 // only retain flags that will drive the direct output profile selection
609 // if explicitly requested
610 static const uint32_t kRelevantFlags =
611 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
612 flags =
613 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
614
615 sp<IOProfile> profile;
616
Eric Laurente552edb2014-03-10 17:42:56 -0700617 for (size_t i = 0; i < mHwModules.size(); i++) {
618 if (mHwModules[i]->mHandle == 0) {
619 continue;
620 }
621 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700622 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
623 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700624 samplingRate, NULL /*updatedSamplingRate*/,
625 format, NULL /*updatedFormat*/,
626 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700627 flags)) {
628 continue;
629 }
630 // reject profiles not corresponding to a device currently available
631 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
632 continue;
633 }
634 // if several profiles are compatible, give priority to one with offload capability
635 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
636 continue;
637 }
638 profile = curProfile;
639 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
640 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700641 }
Eric Laurente552edb2014-03-10 17:42:56 -0700642 }
643 }
Eric Laurent861a6282015-05-18 15:40:16 -0700644 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700645}
646
Eric Laurente0720872014-03-11 09:30:41 -0700647audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100648 uint32_t samplingRate,
649 audio_format_t format,
650 audio_channel_mask_t channelMask,
651 audio_output_flags_t flags,
652 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700653{
Eric Laurent3b73df72014-03-11 09:06:29 -0700654 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700655 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
656 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
657 device, stream, samplingRate, format, channelMask, flags);
658
Eric Laurente83b55d2014-11-14 10:06:21 -0800659 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
660 stream, samplingRate,format, channelMask,
661 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700662}
663
Eric Laurente83b55d2014-11-14 10:06:21 -0800664status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
665 audio_io_handle_t *output,
666 audio_session_t session,
667 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700668 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800669 uint32_t samplingRate,
670 audio_format_t format,
671 audio_channel_mask_t channelMask,
672 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700673 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800674 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700675{
Eric Laurente83b55d2014-11-14 10:06:21 -0800676 audio_attributes_t attributes;
677 if (attr != NULL) {
678 if (!isValidAttributes(attr)) {
679 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
680 attr->usage, attr->content_type, attr->flags,
681 attr->tags);
682 return BAD_VALUE;
683 }
684 attributes = *attr;
685 } else {
686 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
687 ALOGE("getOutputForAttr(): invalid stream type");
688 return BAD_VALUE;
689 }
690 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700691 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700692 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100693 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
694 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
695 if (!audio_is_linear_pcm(format)) {
696 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800697 }
François Gaffie036e1e92015-03-19 10:16:24 +0100698 *stream = streamTypefromAttributesInt(&attributes);
699 *output = desc->mIoHandle;
700 ALOGV("getOutputForAttr() returns output %d", *output);
701 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800702 }
703 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
704 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
705 return BAD_VALUE;
706 }
707
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700708 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
709 " session %d selectedDeviceId %d",
710 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
711 session, selectedDeviceId);
712
713 *stream = streamTypefromAttributesInt(&attributes);
714
715 // Explicit routing?
716 sp<DeviceDescriptor> deviceDesc;
717 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
718 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
719 deviceDesc = mAvailableOutputDevices[i];
720 break;
721 }
722 }
723 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700724
Eric Laurente83b55d2014-11-14 10:06:21 -0800725 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700727
Eric Laurente83b55d2014-11-14 10:06:21 -0800728 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700729 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
730 }
731
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700732 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700733 device, samplingRate, format, channelMask, flags);
734
Eric Laurente83b55d2014-11-14 10:06:21 -0800735 *output = getOutputForDevice(device, session, *stream,
736 samplingRate, format, channelMask,
737 flags, offloadInfo);
738 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700739 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800740 return INVALID_OPERATION;
741 }
Paul McLeanaa981192015-03-21 09:55:15 -0700742
Eric Laurente83b55d2014-11-14 10:06:21 -0800743 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700744}
745
746audio_io_handle_t AudioPolicyManager::getOutputForDevice(
747 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800748 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700749 audio_stream_type_t stream,
750 uint32_t samplingRate,
751 audio_format_t format,
752 audio_channel_mask_t channelMask,
753 audio_output_flags_t flags,
754 const audio_offload_info_t *offloadInfo)
755{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700756 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700757 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700758 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700759
Eric Laurente552edb2014-03-10 17:42:56 -0700760#ifdef AUDIO_POLICY_TEST
761 if (mCurOutput != 0) {
762 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
763 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
764
765 if (mTestOutputs[mCurOutput] == 0) {
766 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700767 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
768 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700769 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700770 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700771 outputDesc->mFlags =
772 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700773 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700774 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
775 config.sample_rate = mTestSamplingRate;
776 config.channel_mask = mTestChannels;
777 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700778 if (offloadInfo != NULL) {
779 config.offload_info = *offloadInfo;
780 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700781 status = mpClientInterface->openOutput(0,
782 &mTestOutputs[mCurOutput],
783 &config,
784 &outputDesc->mDevice,
785 String8(""),
786 &outputDesc->mLatency,
787 outputDesc->mFlags);
788 if (status == NO_ERROR) {
789 outputDesc->mSamplingRate = config.sample_rate;
790 outputDesc->mFormat = config.format;
791 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700792 AudioParameter outputCmd = AudioParameter();
793 outputCmd.addInt(String8("set_id"),mCurOutput);
794 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
795 addOutput(mTestOutputs[mCurOutput], outputDesc);
796 }
797 }
798 return mTestOutputs[mCurOutput];
799 }
800#endif //AUDIO_POLICY_TEST
801
802 // open a direct output if required by specified parameters
803 //force direct flag if offload flag is set: offloading implies a direct output stream
804 // and all common behaviors are driven by checking only the direct flag
805 // this should normally be set appropriately in the policy configuration file
806 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700807 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700808 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700809 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
810 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
811 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800812 // only allow deep buffering for music stream type
813 if (stream != AUDIO_STREAM_MUSIC) {
814 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700815 } else if (/* stream == AUDIO_STREAM_MUSIC && */
816 flags == AUDIO_OUTPUT_FLAG_NONE &&
817 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
818 // use DEEP_BUFFER as default output for music stream type
819 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800820 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700821 if (stream == AUDIO_STREAM_TTS) {
822 flags = AUDIO_OUTPUT_FLAG_TTS;
823 }
Eric Laurente552edb2014-03-10 17:42:56 -0700824
Eric Laurentb732cf52014-09-24 19:08:21 -0700825 sp<IOProfile> profile;
826
827 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700828 // and not explicitly requested
829 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
830 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700831 audio_channel_count_from_out_mask(channelMask) <= 2) {
832 goto non_direct_output;
833 }
834
Eric Laurente552edb2014-03-10 17:42:56 -0700835 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
836 // creating an offloaded track and tearing it down immediately after start when audioflinger
837 // detects there is an active non offloadable effect.
838 // FIXME: We should check the audio session here but we do not have it in this context.
839 // This may prevent offloading in rare situations where effects are left active by apps
840 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700841
Eric Laurente552edb2014-03-10 17:42:56 -0700842 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100843 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700844 profile = getProfileForDirectOutput(device,
845 samplingRate,
846 format,
847 channelMask,
848 (audio_output_flags_t)flags);
849 }
850
Eric Laurent1c333e22014-05-20 10:48:17 -0700851 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700852 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700853
854 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700855 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700856 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
857 outputDesc = desc;
858 // reuse direct output if currently open and configured with same parameters
859 if ((samplingRate == outputDesc->mSamplingRate) &&
860 (format == outputDesc->mFormat) &&
861 (channelMask == outputDesc->mChannelMask)) {
862 outputDesc->mDirectOpenCount++;
863 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
864 return mOutputs.keyAt(i);
865 }
866 }
867 }
868 // close direct output if currently open and configured with different parameters
869 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700870 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700871 }
Eric Laurent861a6282015-05-18 15:40:16 -0700872
873 // if the selected profile is offloaded and no offload info was specified,
874 // create a default one
875 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
876 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
877 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
878 defaultOffloadInfo.sample_rate = samplingRate;
879 defaultOffloadInfo.channel_mask = channelMask;
880 defaultOffloadInfo.format = format;
881 defaultOffloadInfo.stream_type = stream;
882 defaultOffloadInfo.bit_rate = 0;
883 defaultOffloadInfo.duration_us = -1;
884 defaultOffloadInfo.has_video = true; // conservative
885 defaultOffloadInfo.is_streaming = true; // likely
886 offloadInfo = &defaultOffloadInfo;
887 }
888
Eric Laurentc75307b2015-03-17 15:29:32 -0700889 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700890 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700891 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700892 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700893 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
894 config.sample_rate = samplingRate;
895 config.channel_mask = channelMask;
896 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700897 if (offloadInfo != NULL) {
898 config.offload_info = *offloadInfo;
899 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700900 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700901 &output,
902 &config,
903 &outputDesc->mDevice,
904 String8(""),
905 &outputDesc->mLatency,
906 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700907
908 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700909 if (status != NO_ERROR ||
910 (samplingRate != 0 && samplingRate != config.sample_rate) ||
911 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
912 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700913 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
914 "format %d %d, channelMask %04x %04x", output, samplingRate,
915 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
916 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700917 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700918 mpClientInterface->closeOutput(output);
919 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800920 // fall back to mixer output if possible when the direct output could not be open
921 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
922 goto non_direct_output;
923 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700924 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700925 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700926 outputDesc->mSamplingRate = config.sample_rate;
927 outputDesc->mChannelMask = config.channel_mask;
928 outputDesc->mFormat = config.format;
929 outputDesc->mRefCount[stream] = 0;
930 outputDesc->mStopTime[stream] = 0;
931 outputDesc->mDirectOpenCount = 1;
932
Eric Laurente552edb2014-03-10 17:42:56 -0700933 audio_io_handle_t srcOutput = getOutputForEffect();
934 addOutput(output, outputDesc);
935 audio_io_handle_t dstOutput = getOutputForEffect();
936 if (dstOutput == output) {
937 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
938 }
939 mPreviousOutputs = mOutputs;
940 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700941 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700942 return output;
943 }
944
Eric Laurentb732cf52014-09-24 19:08:21 -0700945non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700946 // ignoring channel mask due to downmix capability in mixer
947
948 // open a non direct output
949
950 // for non direct outputs, only PCM is supported
951 if (audio_is_linear_pcm(format)) {
952 // get which output is suitable for the specified stream. The actual
953 // routing change will happen when startOutput() will be called
954 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
955
Eric Laurent8838a382014-09-08 16:44:28 -0700956 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
957 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
958 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700959 }
960 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
961 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
962
Paul McLeanaa981192015-03-21 09:55:15 -0700963 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700964
965 return output;
966}
967
Eric Laurente0720872014-03-11 09:30:41 -0700968audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700969 audio_output_flags_t flags,
970 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700971{
972 // select one output among several that provide a path to a particular device or set of
973 // devices (the list was previously build by getOutputsForDevice()).
974 // The priority is as follows:
975 // 1: the output with the highest number of requested policy flags
976 // 2: the primary output
977 // 3: the first output in the list
978
979 if (outputs.size() == 0) {
980 return 0;
981 }
982 if (outputs.size() == 1) {
983 return outputs[0];
984 }
985
986 int maxCommonFlags = 0;
987 audio_io_handle_t outputFlags = 0;
988 audio_io_handle_t outputPrimary = 0;
989
990 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700991 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700992 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700993 // if a valid format is specified, skip output if not compatible
994 if (format != AUDIO_FORMAT_INVALID) {
995 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
996 if (format != outputDesc->mFormat) {
997 continue;
998 }
999 } else if (!audio_is_linear_pcm(format)) {
1000 continue;
1001 }
1002 }
1003
Eric Laurent3b73df72014-03-11 09:06:29 -07001004 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001005 if (commonFlags > maxCommonFlags) {
1006 outputFlags = outputs[i];
1007 maxCommonFlags = commonFlags;
1008 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1009 }
1010 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1011 outputPrimary = outputs[i];
1012 }
1013 }
1014 }
1015
1016 if (outputFlags != 0) {
1017 return outputFlags;
1018 }
1019 if (outputPrimary != 0) {
1020 return outputPrimary;
1021 }
1022
1023 return outputs[0];
1024}
1025
Eric Laurente0720872014-03-11 09:30:41 -07001026status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001027 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001028 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001029{
Paul McLeanaa981192015-03-21 09:55:15 -07001030 ALOGV("startOutput() output %d, stream %d, session %d",
1031 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001032 ssize_t index = mOutputs.indexOfKey(output);
1033 if (index < 0) {
1034 ALOGW("startOutput() unknown output %d", output);
1035 return BAD_VALUE;
1036 }
1037
Eric Laurentc75307b2015-03-17 15:29:32 -07001038 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1039
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001040 // Routing?
1041 mOutputRoutes.incRouteActivity(session);
1042
Eric Laurentc75307b2015-03-17 15:29:32 -07001043 audio_devices_t newDevice;
1044 if (outputDesc->mPolicyMix != NULL) {
1045 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001046 } else if (mOutputRoutes.hasRouteChanged(session)) {
1047 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001048 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001049 } else {
1050 newDevice = AUDIO_DEVICE_NONE;
1051 }
1052
1053 uint32_t delayMs = 0;
1054
Eric Laurentc75307b2015-03-17 15:29:32 -07001055 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1056
1057 if (status != NO_ERROR) {
1058 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001059 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001060 }
1061 // Automatically enable the remote submix input when output is started on a re routing mix
1062 // of type MIX_TYPE_RECORDERS
1063 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1064 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1065 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1066 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1067 outputDesc->mPolicyMix->mRegistrationId,
1068 "remote-submix");
1069 }
1070
1071 if (delayMs != 0) {
1072 usleep(delayMs * 1000);
1073 }
1074
1075 return status;
1076}
1077
1078status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1079 audio_stream_type_t stream,
1080 audio_devices_t device,
1081 uint32_t *delayMs)
1082{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001083 // cannot start playback of STREAM_TTS if any other output is being used
1084 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001085
1086 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001087 if (stream == AUDIO_STREAM_TTS) {
1088 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001089 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001090 return INVALID_OPERATION;
1091 } else {
1092 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1093 }
1094 } else {
1095 // some playback other than beacon starts
1096 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1097 }
1098
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001099 // check active before incrementing usage count
1100 bool force = !outputDesc->isActive();
1101
Eric Laurente552edb2014-03-10 17:42:56 -07001102 // increment usage count for this stream on the requested output:
1103 // NOTE that the usage count is the same for duplicated output and hardware output which is
1104 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1105 outputDesc->changeRefCount(stream, 1);
1106
Eric Laurent493404d2015-04-21 15:07:36 -07001107 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001108 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001109 if (device == AUDIO_DEVICE_NONE) {
1110 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001111 }
Eric Laurente552edb2014-03-10 17:42:56 -07001112 routing_strategy strategy = getStrategy(stream);
1113 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001114 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1115 (beaconMuteLatency > 0);
1116 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001117 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001118 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001119 if (desc != outputDesc) {
1120 // force a device change if any other output is managed by the same hw
1121 // module and has a current device selection that differs from selected device.
1122 // In this case, the audio HAL must receive the new device selection so that it can
1123 // change the device currently selected by the other active output.
1124 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001125 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001126 force = true;
1127 }
1128 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001129 // a notification so that audio focus effect can propagate, or that a mute/unmute
1130 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001131 uint32_t latency = desc->latency();
1132 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1133 waitMs = latency;
1134 }
1135 }
1136 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001137 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001138
1139 // handle special case for sonification while in call
1140 if (isInCall()) {
1141 handleIncallSonification(stream, true, false);
1142 }
1143
1144 // apply volume rules for current stream and device if necessary
1145 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001146 mStreams.valueFor(stream).getVolumeIndex(device),
1147 outputDesc,
1148 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001149
1150 // update the outputs if starting an output with a stream that can affect notification
1151 // routing
1152 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001153
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001154 // force reevaluating accessibility routing when ringtone or alarm starts
1155 if (strategy == STRATEGY_SONIFICATION) {
1156 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1157 }
Eric Laurente552edb2014-03-10 17:42:56 -07001158 }
1159 return NO_ERROR;
1160}
1161
1162
Eric Laurente0720872014-03-11 09:30:41 -07001163status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001164 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001165 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001166{
1167 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1168 ssize_t index = mOutputs.indexOfKey(output);
1169 if (index < 0) {
1170 ALOGW("stopOutput() unknown output %d", output);
1171 return BAD_VALUE;
1172 }
1173
Eric Laurentc75307b2015-03-17 15:29:32 -07001174 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001175
Eric Laurentc75307b2015-03-17 15:29:32 -07001176 if (outputDesc->mRefCount[stream] == 1) {
1177 // Automatically disable the remote submix input when output is stopped on a
1178 // re routing mix of type MIX_TYPE_RECORDERS
1179 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1180 outputDesc->mPolicyMix != NULL &&
1181 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1182 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1183 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1184 outputDesc->mPolicyMix->mRegistrationId,
1185 "remote-submix");
1186 }
1187 }
1188
1189 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001190 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001191 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001192 int activityCount = mOutputRoutes.decRouteActivity(session);
1193 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1194
1195 if (forceDeviceUpdate) {
1196 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1197 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 }
1199
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001200 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001201}
1202
1203status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001204 audio_stream_type_t stream,
1205 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001206{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001207 // always handle stream stop, check which stream type is stopping
1208 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1209
Eric Laurente552edb2014-03-10 17:42:56 -07001210 // handle special case for sonification while in call
1211 if (isInCall()) {
1212 handleIncallSonification(stream, false, false);
1213 }
1214
1215 if (outputDesc->mRefCount[stream] > 0) {
1216 // decrement usage count of this stream on the output
1217 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001218
Eric Laurente552edb2014-03-10 17:42:56 -07001219 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001220 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001221 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001222 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001223 // delay the device switch by twice the latency because stopOutput() is executed when
1224 // the track stop() command is received and at that time the audio track buffer can
1225 // still contain data that needs to be drained. The latency only covers the audio HAL
1226 // and kernel buffers. Also the latency does not always include additional delay in the
1227 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001228 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001229
1230 // force restoring the device selection on other active outputs if it differs from the
1231 // one being selected for this output
1232 for (size_t i = 0; i < mOutputs.size(); i++) {
1233 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001234 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001235 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001236 desc->isActive() &&
1237 outputDesc->sharesHwModuleWith(desc) &&
1238 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001239 setOutputDevice(desc,
1240 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001241 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001242 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001243 }
1244 }
1245 // update the outputs if stopping one with a stream that can affect notification routing
1246 handleNotificationRoutingForStream(stream);
1247 }
1248 return NO_ERROR;
1249 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001250 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001251 return INVALID_OPERATION;
1252 }
1253}
1254
Eric Laurente83b55d2014-11-14 10:06:21 -08001255void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001256 audio_stream_type_t stream __unused,
1257 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001258{
1259 ALOGV("releaseOutput() %d", output);
1260 ssize_t index = mOutputs.indexOfKey(output);
1261 if (index < 0) {
1262 ALOGW("releaseOutput() releasing unknown output %d", output);
1263 return;
1264 }
1265
1266#ifdef AUDIO_POLICY_TEST
1267 int testIndex = testOutputIndex(output);
1268 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001269 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001270 if (outputDesc->isActive()) {
1271 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001272 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001273 mTestOutputs[testIndex] = 0;
1274 }
1275 return;
1276 }
1277#endif //AUDIO_POLICY_TEST
1278
Paul McLeanaa981192015-03-21 09:55:15 -07001279 // Routing
1280 mOutputRoutes.removeRoute(session);
1281
Eric Laurentc75307b2015-03-17 15:29:32 -07001282 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001283 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001284 if (desc->mDirectOpenCount <= 0) {
1285 ALOGW("releaseOutput() invalid open count %d for output %d",
1286 desc->mDirectOpenCount, output);
1287 return;
1288 }
1289 if (--desc->mDirectOpenCount == 0) {
1290 closeOutput(output);
1291 // If effects where present on the output, audioflinger moved them to the primary
1292 // output by default: move them back to the appropriate output.
1293 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001294 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001295 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1296 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001297 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001298 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001299 }
1300 }
1301}
1302
1303
Eric Laurentcaf7f482014-11-25 17:50:47 -08001304status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1305 audio_io_handle_t *input,
1306 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001307 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001308 uint32_t samplingRate,
1309 audio_format_t format,
1310 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001311 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001312 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001313 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001314{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001315 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1316 "session %d, flags %#x",
1317 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001318
Eric Laurentcaf7f482014-11-25 17:50:47 -08001319 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001320 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001321 audio_devices_t device;
1322 // handle legacy remote submix case where the address was not always specified
1323 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001324 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001325 audio_source_t inputSource = attr->source;
1326 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001327 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001328
Eric Laurentc447ded2015-01-06 08:47:05 -08001329 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1330 inputSource = AUDIO_SOURCE_MIC;
1331 }
1332 halInputSource = inputSource;
1333
Paul McLean466dc8e2015-04-17 13:15:36 -06001334 // Explicit routing?
1335 sp<DeviceDescriptor> deviceDesc;
1336 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1337 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1338 deviceDesc = mAvailableInputDevices[i];
1339 break;
1340 }
1341 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001342 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001343
Eric Laurentc447ded2015-01-06 08:47:05 -08001344 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001345 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001346 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001347 if (ret != NO_ERROR) {
1348 return ret;
1349 }
1350 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001351 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1352 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001353 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001354 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001355 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001356 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001357 return BAD_VALUE;
1358 }
Eric Laurentc722f302014-12-10 11:21:49 -08001359 if (policyMix != NULL) {
1360 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001361 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1362 // there is an external policy, but this input is attached to a mix of recorders,
1363 // meaning it receives audio injected into the framework, so the recorder doesn't
1364 // know about it and is therefore considered "legacy"
1365 *inputType = API_INPUT_LEGACY;
1366 } else {
1367 // recording a mix of players defined by an external policy, we're rerouting for
1368 // an external policy
1369 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1370 }
Eric Laurentc722f302014-12-10 11:21:49 -08001371 } else if (audio_is_remote_submix_device(device)) {
1372 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001373 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001374 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1375 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001376 } else {
1377 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001378 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001379 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001380 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001381 case AUDIO_SOURCE_VOICE_UPLINK:
1382 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1383 break;
1384 case AUDIO_SOURCE_VOICE_DOWNLINK:
1385 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1386 break;
1387 case AUDIO_SOURCE_VOICE_CALL:
1388 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1389 break;
1390 default:
1391 break;
1392 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001393 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001394 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1395 if (index >= 0) {
1396 *input = mSoundTriggerSessions.valueFor(session);
1397 isSoundTrigger = true;
1398 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1399 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1400 } else {
1401 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1402 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001403 }
1404 }
1405
Andy Hungf129b032015-04-07 13:45:50 -07001406 // find a compatible input profile (not necessarily identical in parameters)
1407 sp<IOProfile> profile;
1408 // samplingRate and flags may be updated by getInputProfile
1409 uint32_t profileSamplingRate = samplingRate;
1410 audio_format_t profileFormat = format;
1411 audio_channel_mask_t profileChannelMask = channelMask;
1412 audio_input_flags_t profileFlags = flags;
1413 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001414 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001415 profileSamplingRate, profileFormat, profileChannelMask,
1416 profileFlags);
1417 if (profile != 0) {
1418 break; // success
1419 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1420 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1421 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001422 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1423 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001424 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001425 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001426 }
Eric Laurente552edb2014-03-10 17:42:56 -07001427 }
1428
Eric Laurent322b4d22015-04-03 15:57:54 -07001429 if (profile->getModuleHandle() == 0) {
1430 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001431 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001432 }
1433
1434 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001435 config.sample_rate = profileSamplingRate;
1436 config.channel_mask = profileChannelMask;
1437 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001438
Eric Laurent322b4d22015-04-03 15:57:54 -07001439 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001440 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001441 &config,
1442 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001443 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001444 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001445 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001446
1447 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001448 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001449 (profileSamplingRate != config.sample_rate) ||
1450 (profileFormat != config.format) ||
1451 (profileChannelMask != config.channel_mask)) {
1452 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1453 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001454 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001455 if (*input != AUDIO_IO_HANDLE_NONE) {
1456 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001457 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001458 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001459 }
1460
Eric Laurent1f2f2232014-06-02 12:01:23 -07001461 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001462 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001463 inputDesc->mRefCount = 0;
1464 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001465 inputDesc->mSamplingRate = profileSamplingRate;
1466 inputDesc->mFormat = profileFormat;
1467 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001468 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001469 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001470 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001471 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001472
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001473 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001474
Eric Laurentcaf7f482014-11-25 17:50:47 -08001475 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001476 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001477
Eric Laurentcaf7f482014-11-25 17:50:47 -08001478 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001479}
1480
Eric Laurent4dc68062014-07-28 17:26:49 -07001481status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1482 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001483{
1484 ALOGV("startInput() input %d", input);
1485 ssize_t index = mInputs.indexOfKey(input);
1486 if (index < 0) {
1487 ALOGW("startInput() unknown input %d", input);
1488 return BAD_VALUE;
1489 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001490 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001491
Eric Laurentc722f302014-12-10 11:21:49 -08001492 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001493 if (index < 0) {
1494 ALOGW("startInput() unknown session %d on input %d", session, input);
1495 return BAD_VALUE;
1496 }
1497
Glenn Kasten74a8e252014-07-24 14:09:55 -07001498 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001499 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001500
1501 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001502 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001503 if (activeInput != 0 && activeInput != input) {
1504
1505 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1506 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001507 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001508 if ((activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) &&
1509 !activeDesc->hasPreemptedSession(session)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001510 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurent64265b22015-09-18 18:32:07 -07001511 audio_session_t activeSession = activeDesc->mSessions.itemAt(0);
1512 SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions();
1513 sessions.add(activeSession);
1514 inputDesc->setPreemptedSessions(sessions);
1515 stopInput(activeInput, activeSession);
1516 releaseInput(activeInput, activeSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001517 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001518 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001519 return INVALID_OPERATION;
1520 }
1521 }
Eric Laurentc0a889f2015-10-14 14:36:34 -07001522
1523 // Do not allow capture if an active voice call is using a software patch and
1524 // the call TX source device is on the same HW module.
1525 // FIXME: would be better to refine to only inputs whose profile connects to the
1526 // call TX device but this information is not in the audio patch
1527 if (mCallTxPatch != 0 &&
1528 inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) {
1529 return INVALID_OPERATION;
1530 }
Eric Laurente552edb2014-03-10 17:42:56 -07001531 }
1532
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001533 // Routing?
1534 mInputRoutes.incRouteActivity(session);
1535
Paul McLean466dc8e2015-04-17 13:15:36 -06001536 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001537 // if input maps to a dynamic policy with an activity listener, notify of state change
1538 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001539 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001540 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1541 MIX_STATE_MIXING);
1542 }
1543
François Gaffie53615e22015-03-19 09:24:12 +01001544 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001545 SoundTrigger::setCaptureState(true);
1546 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001547 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001548
Eric Laurentc722f302014-12-10 11:21:49 -08001549 // automatically enable the remote submix output when input is started if not
1550 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001551 // For remote submix (a virtual device), we open only one input per capture request.
1552 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001553 String8 address = String8("");
1554 if (inputDesc->mPolicyMix == NULL) {
1555 address = String8("0");
1556 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1557 address = inputDesc->mPolicyMix->mRegistrationId;
1558 }
1559 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001560 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001561 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001562 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001563 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001564 }
Eric Laurente552edb2014-03-10 17:42:56 -07001565 }
1566
Eric Laurente552edb2014-03-10 17:42:56 -07001567 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1568
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001569 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001570 return NO_ERROR;
1571}
1572
Eric Laurent4dc68062014-07-28 17:26:49 -07001573status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1574 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001575{
1576 ALOGV("stopInput() input %d", input);
1577 ssize_t index = mInputs.indexOfKey(input);
1578 if (index < 0) {
1579 ALOGW("stopInput() unknown input %d", input);
1580 return BAD_VALUE;
1581 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001582 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001583
Eric Laurentc722f302014-12-10 11:21:49 -08001584 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001585 if (index < 0) {
1586 ALOGW("stopInput() unknown session %d on input %d", session, input);
1587 return BAD_VALUE;
1588 }
1589
Eric Laurente552edb2014-03-10 17:42:56 -07001590 if (inputDesc->mRefCount == 0) {
1591 ALOGW("stopInput() input %d already stopped", input);
1592 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001593 }
1594
1595 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001596
1597 // Routing?
1598 mInputRoutes.decRouteActivity(session);
1599
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001600 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001601 // if input maps to a dynamic policy with an activity listener, notify of state change
1602 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001603 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001604 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1605 MIX_STATE_IDLE);
1606 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001607
Eric Laurentc722f302014-12-10 11:21:49 -08001608 // automatically disable the remote submix output when input is stopped if not
1609 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001610 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001611 String8 address = String8("");
1612 if (inputDesc->mPolicyMix == NULL) {
1613 address = String8("0");
1614 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1615 address = inputDesc->mPolicyMix->mRegistrationId;
1616 }
1617 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001618 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001619 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001620 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001621 }
Eric Laurente552edb2014-03-10 17:42:56 -07001622 }
1623
Eric Laurent1c333e22014-05-20 10:48:17 -07001624 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001625
François Gaffie53615e22015-03-19 09:24:12 +01001626 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001627 SoundTrigger::setCaptureState(false);
1628 }
Eric Laurent64265b22015-09-18 18:32:07 -07001629 inputDesc->clearPreemptedSessions();
Eric Laurente552edb2014-03-10 17:42:56 -07001630 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001631 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001632}
1633
Eric Laurent4dc68062014-07-28 17:26:49 -07001634void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1635 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001636{
1637 ALOGV("releaseInput() %d", input);
1638 ssize_t index = mInputs.indexOfKey(input);
1639 if (index < 0) {
1640 ALOGW("releaseInput() releasing unknown input %d", input);
1641 return;
1642 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001643
1644 // Routing
1645 mInputRoutes.removeRoute(session);
1646
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001647 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1648 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001649
Eric Laurentc722f302014-12-10 11:21:49 -08001650 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001651 if (index < 0) {
1652 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1653 return;
1654 }
Eric Laurentc722f302014-12-10 11:21:49 -08001655 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001656 if (inputDesc->mOpenRefCount == 0) {
1657 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1658 return;
1659 }
1660 inputDesc->mOpenRefCount--;
1661 if (inputDesc->mOpenRefCount > 0) {
1662 ALOGV("releaseInput() exit > 0");
1663 return;
1664 }
1665
Eric Laurent05b90f82014-08-27 15:32:29 -07001666 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001667 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001668 ALOGV("releaseInput() exit");
1669}
1670
Eric Laurentd4692962014-05-05 18:13:44 -07001671void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001672 bool patchRemoved = false;
1673
Eric Laurentd4692962014-05-05 18:13:44 -07001674 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001675 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1676 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1677 if (patch_index >= 0) {
1678 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1679 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1680 mAudioPatches.removeItemsAt(patch_index);
1681 patchRemoved = true;
1682 }
Eric Laurentd4692962014-05-05 18:13:44 -07001683 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1684 }
1685 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001686 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001687
1688 if (patchRemoved) {
1689 mpClientInterface->onAudioPatchListUpdate();
1690 }
Eric Laurentd4692962014-05-05 18:13:44 -07001691}
1692
Eric Laurente0720872014-03-11 09:30:41 -07001693void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001694 int indexMin,
1695 int indexMax)
1696{
1697 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001698 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001699 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1700 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001701 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001702 }
Eric Laurente552edb2014-03-10 17:42:56 -07001703}
1704
Eric Laurente0720872014-03-11 09:30:41 -07001705status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001706 int index,
1707 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001708{
1709
Eric Laurentc75307b2015-03-17 15:29:32 -07001710 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1711 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001712 return BAD_VALUE;
1713 }
1714 if (!audio_is_output_device(device)) {
1715 return BAD_VALUE;
1716 }
1717
1718 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001719 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001720
1721 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1722 stream, device, index);
1723
1724 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1725 // clear all device specific values
1726 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001727 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001728 }
François Gaffiedfd74092015-03-19 12:10:59 +01001729 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001730
Eric Laurent31551f82014-10-10 18:21:56 -07001731 // update volume on all outputs whose current device is also selected by the same
1732 // strategy as the device specified by the caller
1733 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001734
1735
1736 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1737 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1738 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001739 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001740 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1741 }
1742 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1743 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001744 return NO_ERROR;
1745 }
Eric Laurente552edb2014-03-10 17:42:56 -07001746 status_t status = NO_ERROR;
1747 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001748 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1749 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001750 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001751 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001752 if (volStatus != NO_ERROR) {
1753 status = volStatus;
1754 }
1755 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001756 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1757 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1758 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001759 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001760 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001761 }
Eric Laurente552edb2014-03-10 17:42:56 -07001762 }
1763 return status;
1764}
1765
Eric Laurente0720872014-03-11 09:30:41 -07001766status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001767 int *index,
1768 audio_devices_t device)
1769{
1770 if (index == NULL) {
1771 return BAD_VALUE;
1772 }
1773 if (!audio_is_output_device(device)) {
1774 return BAD_VALUE;
1775 }
1776 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1777 // the strategy the stream belongs to.
1778 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1779 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1780 }
François Gaffiedfd74092015-03-19 12:10:59 +01001781 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001782
Eric Laurentc75307b2015-03-17 15:29:32 -07001783 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001784 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1785 return NO_ERROR;
1786}
1787
Eric Laurente0720872014-03-11 09:30:41 -07001788audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001789 const SortedVector<audio_io_handle_t>& outputs)
1790{
1791 // select one output among several suitable for global effects.
1792 // The priority is as follows:
1793 // 1: An offloaded output. If the effect ends up not being offloadable,
1794 // AudioFlinger will invalidate the track and the offloaded output
1795 // will be closed causing the effect to be moved to a PCM output.
1796 // 2: A deep buffer output
1797 // 3: the first output in the list
1798
1799 if (outputs.size() == 0) {
1800 return 0;
1801 }
1802
1803 audio_io_handle_t outputOffloaded = 0;
1804 audio_io_handle_t outputDeepBuffer = 0;
1805
1806 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001807 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001808 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001809 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1810 outputOffloaded = outputs[i];
1811 }
1812 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1813 outputDeepBuffer = outputs[i];
1814 }
1815 }
1816
1817 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1818 outputOffloaded, outputDeepBuffer);
1819 if (outputOffloaded != 0) {
1820 return outputOffloaded;
1821 }
1822 if (outputDeepBuffer != 0) {
1823 return outputDeepBuffer;
1824 }
1825
1826 return outputs[0];
1827}
1828
Eric Laurente0720872014-03-11 09:30:41 -07001829audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001830{
1831 // apply simple rule where global effects are attached to the same output as MUSIC streams
1832
Eric Laurent3b73df72014-03-11 09:06:29 -07001833 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001834 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1835 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1836
1837 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1838 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1839 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1840
1841 return output;
1842}
1843
Eric Laurente0720872014-03-11 09:30:41 -07001844status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001845 audio_io_handle_t io,
1846 uint32_t strategy,
1847 int session,
1848 int id)
1849{
1850 ssize_t index = mOutputs.indexOfKey(io);
1851 if (index < 0) {
1852 index = mInputs.indexOfKey(io);
1853 if (index < 0) {
1854 ALOGW("registerEffect() unknown io %d", io);
1855 return INVALID_OPERATION;
1856 }
1857 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001858 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001859}
1860
Eric Laurentc75307b2015-03-17 15:29:32 -07001861bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1862{
1863 return mOutputs.isStreamActive(stream, inPastMs);
1864}
1865
1866bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1867{
1868 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1869}
1870
Eric Laurente0720872014-03-11 09:30:41 -07001871bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001872{
1873 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001874 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001875 if (inputDescriptor->mRefCount == 0) {
1876 continue;
1877 }
1878 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001879 return true;
1880 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001881 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1882 // corresponds to an active capture triggered by a hardware hotword recognition
1883 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1884 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1885 // FIXME: we should not assume that the first session is the active one and keep
1886 // activity count per session. Same in startInput().
1887 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1888 if (index >= 0) {
1889 return true;
1890 }
1891 }
Eric Laurente552edb2014-03-10 17:42:56 -07001892 }
1893 return false;
1894}
1895
Eric Laurent275e8e92014-11-30 15:14:47 -08001896// Register a list of custom mixes with their attributes and format.
1897// When a mix is registered, corresponding input and output profiles are
1898// added to the remote submix hw module. The profile contains only the
1899// parameters (sampling rate, format...) specified by the mix.
1900// The corresponding input remote submix device is also connected.
1901//
1902// When a remote submix device is connected, the address is checked to select the
1903// appropriate profile and the corresponding input or output stream is opened.
1904//
1905// When capture starts, getInputForAttr() will:
1906// - 1 look for a mix matching the address passed in attribtutes tags if any
1907// - 2 if none found, getDeviceForInputSource() will:
1908// - 2.1 look for a mix matching the attributes source
1909// - 2.2 if none found, default to device selection by policy rules
1910// At this time, the corresponding output remote submix device is also connected
1911// and active playback use cases can be transferred to this mix if needed when reconnecting
1912// after AudioTracks are invalidated
1913//
1914// When playback starts, getOutputForAttr() will:
1915// - 1 look for a mix matching the address passed in attribtutes tags if any
1916// - 2 if none found, look for a mix matching the attributes usage
1917// - 3 if none found, default to device and output selection by policy rules.
1918
1919status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1920{
1921 sp<HwModule> module;
1922 for (size_t i = 0; i < mHwModules.size(); i++) {
1923 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1924 mHwModules[i]->mHandle != 0) {
1925 module = mHwModules[i];
1926 break;
1927 }
1928 }
1929
1930 if (module == 0) {
1931 return INVALID_OPERATION;
1932 }
1933
1934 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1935
1936 for (size_t i = 0; i < mixes.size(); i++) {
1937 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001938
1939 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001940 continue;
1941 }
1942 audio_config_t outputConfig = mixes[i].mFormat;
1943 audio_config_t inputConfig = mixes[i].mFormat;
1944 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1945 // stereo and let audio flinger do the channel conversion if needed.
1946 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1947 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1948 module->addOutputProfile(address, &outputConfig,
1949 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1950 module->addInputProfile(address, &inputConfig,
1951 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001952
Eric Laurentc722f302014-12-10 11:21:49 -08001953 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001954 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001955 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001956 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001957 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001958 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001959 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001960 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001961 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001962 }
1963 return NO_ERROR;
1964}
1965
1966status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1967{
1968 sp<HwModule> module;
1969 for (size_t i = 0; i < mHwModules.size(); i++) {
1970 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1971 mHwModules[i]->mHandle != 0) {
1972 module = mHwModules[i];
1973 break;
1974 }
1975 }
1976
1977 if (module == 0) {
1978 return INVALID_OPERATION;
1979 }
1980
1981 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1982
1983 for (size_t i = 0; i < mixes.size(); i++) {
1984 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001985
1986 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001987 continue;
1988 }
1989
Eric Laurentc722f302014-12-10 11:21:49 -08001990 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1991 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1992 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001993 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001994 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001995 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001996 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001997
1998 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1999 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2000 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002001 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08002002 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08002003 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08002004 }
2005 module->removeOutputProfile(address);
2006 module->removeInputProfile(address);
2007 }
2008 return NO_ERROR;
2009}
2010
Eric Laurente552edb2014-03-10 17:42:56 -07002011
Eric Laurente0720872014-03-11 09:30:41 -07002012status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002013{
2014 const size_t SIZE = 256;
2015 char buffer[SIZE];
2016 String8 result;
2017
2018 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2019 result.append(buffer);
2020
Eric Laurent87ffa392015-05-22 10:32:38 -07002021 snprintf(buffer, SIZE, " Primary Output: %d\n",
2022 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07002023 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002024 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07002025 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002026 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002027 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07002028 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002029 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07002030 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002031 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07002032 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002033 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07002034 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002035 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002036 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002037 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002038 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002039 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002040 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2041 result.append(buffer);
2042
François Gaffie2110e042015-03-24 08:41:51 +01002043 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002044
François Gaffie53615e22015-03-19 09:24:12 +01002045 mAvailableOutputDevices.dump(fd, String8("output"));
2046 mAvailableInputDevices.dump(fd, String8("input"));
2047 mHwModules.dump(fd);
2048 mOutputs.dump(fd);
2049 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002050 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002051 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002052 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002053
2054 return NO_ERROR;
2055}
2056
2057// This function checks for the parameters which can be offloaded.
2058// This can be enhanced depending on the capability of the DSP and policy
2059// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002060bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002061{
2062 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002063 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002064 offloadInfo.sample_rate, offloadInfo.channel_mask,
2065 offloadInfo.format,
2066 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2067 offloadInfo.has_video);
2068
2069 // Check if offload has been disabled
2070 char propValue[PROPERTY_VALUE_MAX];
2071 if (property_get("audio.offload.disable", propValue, "0")) {
2072 if (atoi(propValue) != 0) {
2073 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2074 return false;
2075 }
2076 }
2077
2078 // Check if stream type is music, then only allow offload as of now.
2079 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2080 {
2081 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2082 return false;
2083 }
2084
2085 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002086 const bool allowOffloadWithVideo =
2087 property_get_bool("audio.offload.video", false /* default_value */);
2088 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002089 ALOGV("isOffloadSupported: has_video == true, returning false");
2090 return false;
2091 }
2092
2093 //If duration is less than minimum value defined in property, return false
2094 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2095 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2096 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2097 return false;
2098 }
2099 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2100 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2101 return false;
2102 }
2103
2104 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2105 // creating an offloaded track and tearing it down immediately after start when audioflinger
2106 // detects there is an active non offloadable effect.
2107 // FIXME: We should check the audio session here but we do not have it in this context.
2108 // This may prevent offloading in rare situations where effects are left active by apps
2109 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002110 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002111 return false;
2112 }
2113
2114 // See if there is a profile to support this.
2115 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002116 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002117 offloadInfo.sample_rate,
2118 offloadInfo.format,
2119 offloadInfo.channel_mask,
2120 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002121 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2122 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002123}
2124
Eric Laurent6a94d692014-05-20 11:18:06 -07002125status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2126 audio_port_type_t type,
2127 unsigned int *num_ports,
2128 struct audio_port *ports,
2129 unsigned int *generation)
2130{
2131 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2132 generation == NULL) {
2133 return BAD_VALUE;
2134 }
2135 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2136 if (ports == NULL) {
2137 *num_ports = 0;
2138 }
2139
2140 size_t portsWritten = 0;
2141 size_t portsMax = *num_ports;
2142 *num_ports = 0;
2143 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2144 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2145 for (size_t i = 0;
2146 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2147 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2148 }
2149 *num_ports += mAvailableOutputDevices.size();
2150 }
2151 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2152 for (size_t i = 0;
2153 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2154 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2155 }
2156 *num_ports += mAvailableInputDevices.size();
2157 }
2158 }
2159 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2160 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2161 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2162 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2163 }
2164 *num_ports += mInputs.size();
2165 }
2166 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002167 size_t numOutputs = 0;
2168 for (size_t i = 0; i < mOutputs.size(); i++) {
2169 if (!mOutputs[i]->isDuplicated()) {
2170 numOutputs++;
2171 if (portsWritten < portsMax) {
2172 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2173 }
2174 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002175 }
Eric Laurent84c70242014-06-23 08:46:27 -07002176 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002177 }
2178 }
2179 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002180 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002181 return NO_ERROR;
2182}
2183
2184status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2185{
2186 return NO_ERROR;
2187}
2188
Eric Laurent6a94d692014-05-20 11:18:06 -07002189status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2190 audio_patch_handle_t *handle,
2191 uid_t uid)
2192{
2193 ALOGV("createAudioPatch()");
2194
2195 if (handle == NULL || patch == NULL) {
2196 return BAD_VALUE;
2197 }
2198 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2199
Eric Laurent874c42872014-08-08 15:13:39 -07002200 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2201 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2202 return BAD_VALUE;
2203 }
2204 // only one source per audio patch supported for now
2205 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002206 return INVALID_OPERATION;
2207 }
Eric Laurent874c42872014-08-08 15:13:39 -07002208
2209 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002210 return INVALID_OPERATION;
2211 }
Eric Laurent874c42872014-08-08 15:13:39 -07002212 for (size_t i = 0; i < patch->num_sinks; i++) {
2213 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2214 return INVALID_OPERATION;
2215 }
2216 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002217
2218 sp<AudioPatch> patchDesc;
2219 ssize_t index = mAudioPatches.indexOfKey(*handle);
2220
Eric Laurent6a94d692014-05-20 11:18:06 -07002221 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2222 patch->sources[0].role,
2223 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002224#if LOG_NDEBUG == 0
2225 for (size_t i = 0; i < patch->num_sinks; i++) {
2226 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2227 patch->sinks[i].role,
2228 patch->sinks[i].type);
2229 }
2230#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002231
2232 if (index >= 0) {
2233 patchDesc = mAudioPatches.valueAt(index);
2234 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2235 mUidCached, patchDesc->mUid, uid);
2236 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2237 return INVALID_OPERATION;
2238 }
2239 } else {
2240 *handle = 0;
2241 }
2242
2243 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002244 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002245 if (outputDesc == NULL) {
2246 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2247 return BAD_VALUE;
2248 }
Eric Laurent84c70242014-06-23 08:46:27 -07002249 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2250 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002251 if (patchDesc != 0) {
2252 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2253 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2254 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2255 return BAD_VALUE;
2256 }
2257 }
Eric Laurent874c42872014-08-08 15:13:39 -07002258 DeviceVector devices;
2259 for (size_t i = 0; i < patch->num_sinks; i++) {
2260 // Only support mix to devices connection
2261 // TODO add support for mix to mix connection
2262 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2263 ALOGV("createAudioPatch() source mix but sink is not a device");
2264 return INVALID_OPERATION;
2265 }
2266 sp<DeviceDescriptor> devDesc =
2267 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2268 if (devDesc == 0) {
2269 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2270 return BAD_VALUE;
2271 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002272
François Gaffie53615e22015-03-19 09:24:12 +01002273 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002274 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002275 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002276 NULL, // updatedSamplingRate
2277 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002278 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002279 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002280 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002281 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002282 ALOGV("createAudioPatch() profile not supported for device %08x",
2283 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002284 return INVALID_OPERATION;
2285 }
2286 devices.add(devDesc);
2287 }
2288 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002289 return INVALID_OPERATION;
2290 }
Eric Laurent874c42872014-08-08 15:13:39 -07002291
Eric Laurent6a94d692014-05-20 11:18:06 -07002292 // TODO: reconfigure output format and channels here
2293 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002294 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002295 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002296 index = mAudioPatches.indexOfKey(*handle);
2297 if (index >= 0) {
2298 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2299 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2300 }
2301 patchDesc = mAudioPatches.valueAt(index);
2302 patchDesc->mUid = uid;
2303 ALOGV("createAudioPatch() success");
2304 } else {
2305 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2306 return INVALID_OPERATION;
2307 }
2308 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2309 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2310 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002311 // only one sink supported when connecting an input device to a mix
2312 if (patch->num_sinks > 1) {
2313 return INVALID_OPERATION;
2314 }
François Gaffie53615e22015-03-19 09:24:12 +01002315 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002316 if (inputDesc == NULL) {
2317 return BAD_VALUE;
2318 }
2319 if (patchDesc != 0) {
2320 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2321 return BAD_VALUE;
2322 }
2323 }
2324 sp<DeviceDescriptor> devDesc =
2325 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2326 if (devDesc == 0) {
2327 return BAD_VALUE;
2328 }
2329
François Gaffie53615e22015-03-19 09:24:12 +01002330 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002331 devDesc->mAddress,
2332 patch->sinks[0].sample_rate,
2333 NULL, /*updatedSampleRate*/
2334 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002335 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002336 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002337 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002338 // FIXME for the parameter type,
2339 // and the NONE
2340 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002341 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002342 return INVALID_OPERATION;
2343 }
2344 // TODO: reconfigure output format and channels here
2345 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002346 devDesc->type(), inputDesc->mIoHandle);
2347 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002348 index = mAudioPatches.indexOfKey(*handle);
2349 if (index >= 0) {
2350 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2351 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2352 }
2353 patchDesc = mAudioPatches.valueAt(index);
2354 patchDesc->mUid = uid;
2355 ALOGV("createAudioPatch() success");
2356 } else {
2357 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2358 return INVALID_OPERATION;
2359 }
2360 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2361 // device to device connection
2362 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002363 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002364 return BAD_VALUE;
2365 }
2366 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002367 sp<DeviceDescriptor> srcDeviceDesc =
2368 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002369 if (srcDeviceDesc == 0) {
2370 return BAD_VALUE;
2371 }
Eric Laurent874c42872014-08-08 15:13:39 -07002372
Eric Laurent6a94d692014-05-20 11:18:06 -07002373 //update source and sink with our own data as the data passed in the patch may
2374 // be incomplete.
2375 struct audio_patch newPatch = *patch;
2376 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002377
Eric Laurent874c42872014-08-08 15:13:39 -07002378 for (size_t i = 0; i < patch->num_sinks; i++) {
2379 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2380 ALOGV("createAudioPatch() source device but one sink is not a device");
2381 return INVALID_OPERATION;
2382 }
2383
2384 sp<DeviceDescriptor> sinkDeviceDesc =
2385 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2386 if (sinkDeviceDesc == 0) {
2387 return BAD_VALUE;
2388 }
2389 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2390
Eric Laurent3bcf8592015-04-03 12:13:24 -07002391 // create a software bridge in PatchPanel if:
2392 // - source and sink devices are on differnt HW modules OR
2393 // - audio HAL version is < 3.0
2394 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2395 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2396 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002397 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002398 return INVALID_OPERATION;
2399 }
Eric Laurent874c42872014-08-08 15:13:39 -07002400 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002401 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002402 // if the sink device is reachable via an opened output stream, request to go via
2403 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002404 audio_io_handle_t output = selectOutput(outputs,
2405 AUDIO_OUTPUT_FLAG_NONE,
2406 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002407 if (output != AUDIO_IO_HANDLE_NONE) {
2408 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2409 if (outputDesc->isDuplicated()) {
2410 return INVALID_OPERATION;
2411 }
2412 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002413 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002414 newPatch.num_sources = 2;
2415 }
Eric Laurent83b88082014-06-20 18:31:16 -07002416 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002417 }
2418 // TODO: check from routing capabilities in config file and other conflicting patches
2419
2420 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2421 if (index >= 0) {
2422 afPatchHandle = patchDesc->mAfPatchHandle;
2423 }
2424
2425 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2426 &afPatchHandle,
2427 0);
2428 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2429 status, afPatchHandle);
2430 if (status == NO_ERROR) {
2431 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002432 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002433 addAudioPatch(patchDesc->mHandle, patchDesc);
2434 } else {
2435 patchDesc->mPatch = newPatch;
2436 }
2437 patchDesc->mAfPatchHandle = afPatchHandle;
2438 *handle = patchDesc->mHandle;
2439 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002440 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002441 } else {
2442 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2443 status);
2444 return INVALID_OPERATION;
2445 }
2446 } else {
2447 return BAD_VALUE;
2448 }
2449 } else {
2450 return BAD_VALUE;
2451 }
2452 return NO_ERROR;
2453}
2454
2455status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2456 uid_t uid)
2457{
2458 ALOGV("releaseAudioPatch() patch %d", handle);
2459
2460 ssize_t index = mAudioPatches.indexOfKey(handle);
2461
2462 if (index < 0) {
2463 return BAD_VALUE;
2464 }
2465 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2466 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2467 mUidCached, patchDesc->mUid, uid);
2468 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2469 return INVALID_OPERATION;
2470 }
2471
2472 struct audio_patch *patch = &patchDesc->mPatch;
2473 patchDesc->mUid = mUidCached;
2474 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002475 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002476 if (outputDesc == NULL) {
2477 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2478 return BAD_VALUE;
2479 }
2480
Eric Laurentc75307b2015-03-17 15:29:32 -07002481 setOutputDevice(outputDesc,
2482 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002483 true,
2484 0,
2485 NULL);
2486 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2487 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002488 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002489 if (inputDesc == NULL) {
2490 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2491 return BAD_VALUE;
2492 }
2493 setInputDevice(inputDesc->mIoHandle,
2494 getNewInputDevice(inputDesc->mIoHandle),
2495 true,
2496 NULL);
2497 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2498 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2499 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2500 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2501 status, patchDesc->mAfPatchHandle);
2502 removeAudioPatch(patchDesc->mHandle);
2503 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002504 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002505 } else {
2506 return BAD_VALUE;
2507 }
2508 } else {
2509 return BAD_VALUE;
2510 }
2511 return NO_ERROR;
2512}
2513
2514status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2515 struct audio_patch *patches,
2516 unsigned int *generation)
2517{
François Gaffie53615e22015-03-19 09:24:12 +01002518 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002519 return BAD_VALUE;
2520 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002521 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002522 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002523}
2524
Eric Laurente1715a42014-05-20 11:30:42 -07002525status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002526{
Eric Laurente1715a42014-05-20 11:30:42 -07002527 ALOGV("setAudioPortConfig()");
2528
2529 if (config == NULL) {
2530 return BAD_VALUE;
2531 }
2532 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2533 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002534 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2535 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002536 }
2537
Eric Laurenta121f902014-06-03 13:32:54 -07002538 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002539 if (config->type == AUDIO_PORT_TYPE_MIX) {
2540 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002541 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002542 if (outputDesc == NULL) {
2543 return BAD_VALUE;
2544 }
Eric Laurent84c70242014-06-23 08:46:27 -07002545 ALOG_ASSERT(!outputDesc->isDuplicated(),
2546 "setAudioPortConfig() called on duplicated output %d",
2547 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002548 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002549 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002550 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002551 if (inputDesc == NULL) {
2552 return BAD_VALUE;
2553 }
Eric Laurenta121f902014-06-03 13:32:54 -07002554 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002555 } else {
2556 return BAD_VALUE;
2557 }
2558 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2559 sp<DeviceDescriptor> deviceDesc;
2560 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2561 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2562 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2563 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2564 } else {
2565 return BAD_VALUE;
2566 }
2567 if (deviceDesc == NULL) {
2568 return BAD_VALUE;
2569 }
Eric Laurenta121f902014-06-03 13:32:54 -07002570 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002571 } else {
2572 return BAD_VALUE;
2573 }
2574
Eric Laurenta121f902014-06-03 13:32:54 -07002575 struct audio_port_config backupConfig;
2576 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2577 if (status == NO_ERROR) {
2578 struct audio_port_config newConfig;
2579 audioPortConfig->toAudioPortConfig(&newConfig, config);
2580 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002581 }
Eric Laurenta121f902014-06-03 13:32:54 -07002582 if (status != NO_ERROR) {
2583 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002584 }
Eric Laurente1715a42014-05-20 11:30:42 -07002585
2586 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002587}
2588
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002589void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2590{
2591 clearAudioPatches(uid);
2592 clearSessionRoutes(uid);
2593}
2594
Eric Laurent6a94d692014-05-20 11:18:06 -07002595void AudioPolicyManager::clearAudioPatches(uid_t uid)
2596{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002597 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002598 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2599 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002600 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002601 }
2602 }
2603}
2604
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002605
2606void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2607 audio_io_handle_t ouptutToSkip)
2608{
2609 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2610 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2611 for (size_t j = 0; j < mOutputs.size(); j++) {
2612 if (mOutputs.keyAt(j) == ouptutToSkip) {
2613 continue;
2614 }
2615 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2616 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2617 continue;
2618 }
2619 // If the default device for this strategy is on another output mix,
2620 // invalidate all tracks in this strategy to force re connection.
2621 // Otherwise select new device on the output mix.
2622 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2623 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2624 if (stream == AUDIO_STREAM_PATCH) {
2625 continue;
2626 }
2627 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2628 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2629 }
2630 }
2631 } else {
2632 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2633 setOutputDevice(outputDesc, newDevice, false);
2634 }
2635 }
2636}
2637
2638void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2639{
2640 // remove output routes associated with this uid
2641 SortedVector<routing_strategy> affectedStrategies;
2642 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2643 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2644 if (route->mUid == uid) {
2645 mOutputRoutes.removeItemsAt(i);
2646 if (route->mDeviceDescriptor != 0) {
2647 affectedStrategies.add(getStrategy(route->mStreamType));
2648 }
2649 }
2650 }
2651 // reroute outputs if necessary
2652 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2653 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2654 }
2655
2656 // remove input routes associated with this uid
2657 SortedVector<audio_source_t> affectedSources;
2658 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2659 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2660 if (route->mUid == uid) {
2661 mInputRoutes.removeItemsAt(i);
2662 if (route->mDeviceDescriptor != 0) {
2663 affectedSources.add(route->mSource);
2664 }
2665 }
2666 }
2667 // reroute inputs if necessary
2668 SortedVector<audio_io_handle_t> inputsToClose;
2669 for (size_t i = 0; i < mInputs.size(); i++) {
2670 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2671 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2672 inputsToClose.add(inputDesc->mIoHandle);
2673 }
2674 }
2675 for (size_t i = 0; i < inputsToClose.size(); i++) {
2676 closeInput(inputsToClose[i]);
2677 }
2678}
2679
2680
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002681status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2682 audio_io_handle_t *ioHandle,
2683 audio_devices_t *device)
2684{
2685 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2686 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002687 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002688
François Gaffiedf372692015-03-19 10:43:27 +01002689 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002690}
2691
Eric Laurent493404d2015-04-21 15:07:36 -07002692status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2693 const audio_attributes_t *attributes __unused,
2694 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002695{
2696 return INVALID_OPERATION;
2697}
2698
Eric Laurent493404d2015-04-21 15:07:36 -07002699status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002700{
2701 return INVALID_OPERATION;
2702}
2703
Eric Laurente552edb2014-03-10 17:42:56 -07002704// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002705// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002706// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002707uint32_t AudioPolicyManager::nextAudioPortGeneration()
2708{
2709 return android_atomic_inc(&mAudioPortGeneration);
2710}
2711
Eric Laurente0720872014-03-11 09:30:41 -07002712AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002713 :
2714#ifdef AUDIO_POLICY_TEST
2715 Thread(false),
2716#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002717 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002718 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002719 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002720 mAudioPortGeneration(1),
2721 mBeaconMuteRefCount(0),
2722 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07002723 mBeaconMuted(false),
2724 mTtsOutputAvailable(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002725{
François Gaffie2110e042015-03-24 08:41:51 +01002726 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2727 if (!engineInstance) {
2728 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2729 return;
2730 }
2731 // Retrieve the Policy Manager Interface
2732 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2733 if (mEngine == NULL) {
2734 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2735 return;
2736 }
2737 mEngine->setObserver(this);
2738 status_t status = mEngine->initCheck();
2739 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2740
Eric Laurent6a94d692014-05-20 11:18:06 -07002741 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002742 mpClientInterface = clientInterface;
2743
Eric Laurent7288ab82015-05-06 18:44:02 -07002744 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002745 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2746 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2747 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2748 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2749 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2750 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002751 ALOGE("could not load audio policy configuration file, setting defaults");
2752 defaultAudioPolicyConfig();
2753 }
2754 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002755 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002756
François Gaffie2110e042015-03-24 08:41:51 +01002757 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2758 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002759
2760 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002761 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2762 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002763 for (size_t i = 0; i < mHwModules.size(); i++) {
2764 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2765 if (mHwModules[i]->mHandle == 0) {
2766 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2767 continue;
2768 }
2769 // open all output streams needed to access attached devices
2770 // except for direct output streams that are only opened when they are actually
2771 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002772 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002773 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2774 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002775 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002776
Eric Laurent3a4311c2014-03-17 12:00:47 -07002777 if (outProfile->mSupportedDevices.isEmpty()) {
2778 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2779 continue;
2780 }
Eric Laurent9459fb02015-08-12 18:36:32 -07002781 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_TTS) != 0) {
2782 mTtsOutputAvailable = true;
2783 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002784
Eric Laurentd78f1532014-09-16 16:38:20 -07002785 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2786 continue;
2787 }
Eric Laurent83b88082014-06-20 18:31:16 -07002788 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002789 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2790 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002791 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002792 // chose first device present in mSupportedDevices also part of
2793 // outputDeviceTypes
2794 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002795 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002796 if ((profileType & outputDeviceTypes) != 0) {
2797 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002798 }
Eric Laurente552edb2014-03-10 17:42:56 -07002799 }
2800 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002801 if ((profileType & outputDeviceTypes) == 0) {
2802 continue;
2803 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002804 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2805 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002806
2807 outputDesc->mDevice = profileType;
2808 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2809 config.sample_rate = outputDesc->mSamplingRate;
2810 config.channel_mask = outputDesc->mChannelMask;
2811 config.format = outputDesc->mFormat;
2812 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002813 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002814 &output,
2815 &config,
2816 &outputDesc->mDevice,
2817 String8(""),
2818 &outputDesc->mLatency,
2819 outputDesc->mFlags);
2820
2821 if (status != NO_ERROR) {
2822 ALOGW("Cannot open output stream for device %08x on hw module %s",
2823 outputDesc->mDevice,
2824 mHwModules[i]->mName);
2825 } else {
2826 outputDesc->mSamplingRate = config.sample_rate;
2827 outputDesc->mChannelMask = config.channel_mask;
2828 outputDesc->mFormat = config.format;
2829
2830 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002831 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002832 ssize_t index =
2833 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2834 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002835 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2836 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002837 }
2838 }
2839 if (mPrimaryOutput == 0 &&
2840 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002841 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002842 }
2843 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002844 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002845 outputDesc->mDevice,
2846 true);
2847 }
Eric Laurente552edb2014-03-10 17:42:56 -07002848 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002849 // open input streams needed to access attached devices to validate
2850 // mAvailableInputDevices list
2851 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2852 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002853 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002854
Eric Laurent3a4311c2014-03-17 12:00:47 -07002855 if (inProfile->mSupportedDevices.isEmpty()) {
2856 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2857 continue;
2858 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002859 // chose first device present in mSupportedDevices also part of
2860 // inputDeviceTypes
2861 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2862 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002863 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002864 if (profileType & inputDeviceTypes) {
2865 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002866 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002867 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002868 if ((profileType & inputDeviceTypes) == 0) {
2869 continue;
2870 }
2871 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2872
2873 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2874 inputDesc->mDevice = profileType;
2875
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002876 // find the address
2877 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2878 // the inputs vector must be of size 1, but we don't want to crash here
2879 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2880 : String8("");
2881 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2882 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2883
Eric Laurentd78f1532014-09-16 16:38:20 -07002884 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2885 config.sample_rate = inputDesc->mSamplingRate;
2886 config.channel_mask = inputDesc->mChannelMask;
2887 config.format = inputDesc->mFormat;
2888 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002889 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002890 &input,
2891 &config,
2892 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002893 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002894 AUDIO_SOURCE_MIC,
2895 AUDIO_INPUT_FLAG_NONE);
2896
2897 if (status == NO_ERROR) {
2898 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002899 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002900 ssize_t index =
2901 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2902 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07002903 if (index >= 0) {
2904 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
2905 if (!devDesc->isAttached()) {
2906 devDesc->attach(mHwModules[i]);
2907 devDesc->importAudioPort(inProfile);
2908 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002909 }
2910 }
2911 mpClientInterface->closeInput(input);
2912 } else {
2913 ALOGW("Cannot open input stream for device %08x on hw module %s",
2914 inputDesc->mDevice,
2915 mHwModules[i]->mName);
2916 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002917 }
2918 }
2919 // make sure all attached devices have been allocated a unique ID
2920 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002921 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002922 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002923 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2924 continue;
2925 }
François Gaffie2110e042015-03-24 08:41:51 +01002926 // The device is now validated and can be appended to the available devices of the engine
2927 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2928 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002929 i++;
2930 }
2931 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002932 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002933 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002934 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2935 continue;
2936 }
François Gaffie2110e042015-03-24 08:41:51 +01002937 // The device is now validated and can be appended to the available devices of the engine
2938 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2939 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002940 i++;
2941 }
2942 // make sure default device is reachable
2943 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002944 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002945 }
Eric Laurente552edb2014-03-10 17:42:56 -07002946
2947 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2948
2949 updateDevicesAndOutputs();
2950
2951#ifdef AUDIO_POLICY_TEST
2952 if (mPrimaryOutput != 0) {
2953 AudioParameter outputCmd = AudioParameter();
2954 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002955 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002956
2957 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2958 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002959 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2960 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002961 mTestLatencyMs = 0;
2962 mCurOutput = 0;
2963 mDirectOutput = false;
2964 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2965 mTestOutputs[i] = 0;
2966 }
2967
2968 const size_t SIZE = 256;
2969 char buffer[SIZE];
2970 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2971 run(buffer, ANDROID_PRIORITY_AUDIO);
2972 }
2973#endif //AUDIO_POLICY_TEST
2974}
2975
Eric Laurente0720872014-03-11 09:30:41 -07002976AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002977{
2978#ifdef AUDIO_POLICY_TEST
2979 exit();
2980#endif //AUDIO_POLICY_TEST
2981 for (size_t i = 0; i < mOutputs.size(); i++) {
2982 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002983 }
2984 for (size_t i = 0; i < mInputs.size(); i++) {
2985 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002986 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002987 mAvailableOutputDevices.clear();
2988 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002989 mOutputs.clear();
2990 mInputs.clear();
2991 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002992}
2993
Eric Laurente0720872014-03-11 09:30:41 -07002994status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002995{
Eric Laurent87ffa392015-05-22 10:32:38 -07002996 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002997}
2998
2999#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003000bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003001{
3002 ALOGV("entering threadLoop()");
3003 while (!exitPending())
3004 {
3005 String8 command;
3006 int valueInt;
3007 String8 value;
3008
3009 Mutex::Autolock _l(mLock);
3010 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3011
3012 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3013 AudioParameter param = AudioParameter(command);
3014
3015 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3016 valueInt != 0) {
3017 ALOGV("Test command %s received", command.string());
3018 String8 target;
3019 if (param.get(String8("target"), target) != NO_ERROR) {
3020 target = "Manager";
3021 }
3022 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3023 param.remove(String8("test_cmd_policy_output"));
3024 mCurOutput = valueInt;
3025 }
3026 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3027 param.remove(String8("test_cmd_policy_direct"));
3028 if (value == "false") {
3029 mDirectOutput = false;
3030 } else if (value == "true") {
3031 mDirectOutput = true;
3032 }
3033 }
3034 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3035 param.remove(String8("test_cmd_policy_input"));
3036 mTestInput = valueInt;
3037 }
3038
3039 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3040 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003041 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003042 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003043 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003044 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003045 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003046 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003047 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003048 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003049 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003050 if (target == "Manager") {
3051 mTestFormat = format;
3052 } else if (mTestOutputs[mCurOutput] != 0) {
3053 AudioParameter outputParam = AudioParameter();
3054 outputParam.addInt(String8("format"), format);
3055 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3056 }
3057 }
3058 }
3059 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3060 param.remove(String8("test_cmd_policy_channels"));
3061 int channels = 0;
3062
3063 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003064 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003065 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003066 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003067 }
3068 if (channels != 0) {
3069 if (target == "Manager") {
3070 mTestChannels = channels;
3071 } else if (mTestOutputs[mCurOutput] != 0) {
3072 AudioParameter outputParam = AudioParameter();
3073 outputParam.addInt(String8("channels"), channels);
3074 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3075 }
3076 }
3077 }
3078 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3079 param.remove(String8("test_cmd_policy_sampleRate"));
3080 if (valueInt >= 0 && valueInt <= 96000) {
3081 int samplingRate = valueInt;
3082 if (target == "Manager") {
3083 mTestSamplingRate = samplingRate;
3084 } else if (mTestOutputs[mCurOutput] != 0) {
3085 AudioParameter outputParam = AudioParameter();
3086 outputParam.addInt(String8("sampling_rate"), samplingRate);
3087 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3088 }
3089 }
3090 }
3091
3092 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3093 param.remove(String8("test_cmd_policy_reopen"));
3094
Eric Laurentc75307b2015-03-17 15:29:32 -07003095 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003096
Eric Laurentc75307b2015-03-17 15:29:32 -07003097 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003098
Eric Laurentc75307b2015-03-17 15:29:32 -07003099 removeOutput(mPrimaryOutput->mIoHandle);
3100 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3101 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003102 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003103 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3104 config.sample_rate = outputDesc->mSamplingRate;
3105 config.channel_mask = outputDesc->mChannelMask;
3106 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003107 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003108 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003109 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003110 &config,
3111 &outputDesc->mDevice,
3112 String8(""),
3113 &outputDesc->mLatency,
3114 outputDesc->mFlags);
3115 if (status != NO_ERROR) {
3116 ALOGE("Failed to reopen hardware output stream, "
3117 "samplingRate: %d, format %d, channels %d",
3118 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003119 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003120 outputDesc->mSamplingRate = config.sample_rate;
3121 outputDesc->mChannelMask = config.channel_mask;
3122 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003123 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003124 AudioParameter outputCmd = AudioParameter();
3125 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003126 mpClientInterface->setParameters(handle, outputCmd.toString());
3127 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003128 }
3129 }
3130
3131
3132 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3133 }
3134 }
3135 return false;
3136}
3137
Eric Laurente0720872014-03-11 09:30:41 -07003138void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003139{
3140 {
3141 AutoMutex _l(mLock);
3142 requestExit();
3143 mWaitWorkCV.signal();
3144 }
3145 requestExitAndWait();
3146}
3147
Eric Laurente0720872014-03-11 09:30:41 -07003148int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003149{
3150 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3151 if (output == mTestOutputs[i]) return i;
3152 }
3153 return 0;
3154}
3155#endif //AUDIO_POLICY_TEST
3156
3157// ---
3158
Eric Laurentc75307b2015-03-17 15:29:32 -07003159void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003160{
François Gaffie98cc1912015-03-18 17:52:40 +01003161 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003162 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003163 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003164}
3165
François Gaffie53615e22015-03-19 09:24:12 +01003166void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3167{
3168 mOutputs.removeItem(output);
3169}
3170
Eric Laurent1f2f2232014-06-02 12:01:23 -07003171void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003172{
François Gaffie98cc1912015-03-18 17:52:40 +01003173 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003174 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003175 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003176}
Eric Laurente552edb2014-03-10 17:42:56 -07003177
Eric Laurentc75307b2015-03-17 15:29:32 -07003178void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003179 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003180 const String8 address /*in*/,
3181 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003182 sp<DeviceDescriptor> devDesc =
3183 desc->mProfile->mSupportedDevices.getDevice(device, address);
3184 if (devDesc != 0) {
3185 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3186 desc->mIoHandle, address.string());
3187 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003188 }
3189}
3190
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003191status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003192 audio_policy_dev_state_t state,
3193 SortedVector<audio_io_handle_t>& outputs,
3194 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003195{
François Gaffie53615e22015-03-19 09:24:12 +01003196 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003197 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003198
3199 if (audio_device_is_digital(device)) {
3200 // erase all current sample rates, formats and channel masks
3201 devDesc->clearCapabilities();
3202 }
Eric Laurente552edb2014-03-10 17:42:56 -07003203
Eric Laurent3b73df72014-03-11 09:06:29 -07003204 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003205 // first list already open outputs that can be routed to this device
3206 for (size_t i = 0; i < mOutputs.size(); i++) {
3207 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003208 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003209 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003210 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3211 outputs.add(mOutputs.keyAt(i));
3212 } else {
3213 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003214 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003215 }
Eric Laurente552edb2014-03-10 17:42:56 -07003216 }
3217 }
3218 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003219 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003220 for (size_t i = 0; i < mHwModules.size(); i++)
3221 {
3222 if (mHwModules[i]->mHandle == 0) {
3223 continue;
3224 }
3225 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3226 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003227 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3228 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003229 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003230 address == profile->mSupportedDevices[0]->mAddress) {
3231 profiles.add(profile);
3232 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3233 }
Eric Laurente552edb2014-03-10 17:42:56 -07003234 }
3235 }
3236 }
3237
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003238 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3239
Eric Laurente552edb2014-03-10 17:42:56 -07003240 if (profiles.isEmpty() && outputs.isEmpty()) {
3241 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3242 return BAD_VALUE;
3243 }
3244
3245 // open outputs for matching profiles if needed. Direct outputs are also opened to
3246 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3247 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003248 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003249
3250 // nothing to do if one output is already opened for this profile
3251 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003252 for (j = 0; j < outputs.size(); j++) {
3253 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003254 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003255 // matching profile: save the sample rates, format and channel masks supported
3256 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003257 if (audio_device_is_digital(device)) {
3258 devDesc->importAudioPort(profile);
3259 }
Eric Laurente552edb2014-03-10 17:42:56 -07003260 break;
3261 }
3262 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003263 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003264 continue;
3265 }
3266
Eric Laurent83b88082014-06-20 18:31:16 -07003267 ALOGV("opening output for device %08x with params %s profile %p",
3268 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003269 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003270 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003271 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3272 config.sample_rate = desc->mSamplingRate;
3273 config.channel_mask = desc->mChannelMask;
3274 config.format = desc->mFormat;
3275 config.offload_info.sample_rate = desc->mSamplingRate;
3276 config.offload_info.channel_mask = desc->mChannelMask;
3277 config.offload_info.format = desc->mFormat;
3278 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003279 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003280 &output,
3281 &config,
3282 &desc->mDevice,
3283 address,
3284 &desc->mLatency,
3285 desc->mFlags);
3286 if (status == NO_ERROR) {
3287 desc->mSamplingRate = config.sample_rate;
3288 desc->mChannelMask = config.channel_mask;
3289 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003290
Eric Laurentd4692962014-05-05 18:13:44 -07003291 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003292 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003293 char *param = audio_device_address_to_parameter(device, address);
3294 mpClientInterface->setParameters(output, String8(param));
3295 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003296 }
3297
Eric Laurentd4692962014-05-05 18:13:44 -07003298 // Here is where we step through and resolve any "dynamic" fields
3299 String8 reply;
3300 char *value;
3301 if (profile->mSamplingRates[0] == 0) {
3302 reply = mpClientInterface->getParameters(output,
3303 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003304 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003305 reply.string());
3306 value = strpbrk((char *)reply.string(), "=");
3307 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003308 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003309 }
Eric Laurentd4692962014-05-05 18:13:44 -07003310 }
3311 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3312 reply = mpClientInterface->getParameters(output,
3313 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003314 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003315 reply.string());
3316 value = strpbrk((char *)reply.string(), "=");
3317 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003318 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003319 }
Eric Laurentd4692962014-05-05 18:13:44 -07003320 }
3321 if (profile->mChannelMasks[0] == 0) {
3322 reply = mpClientInterface->getParameters(output,
3323 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003324 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003325 reply.string());
3326 value = strpbrk((char *)reply.string(), "=");
3327 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003328 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003329 }
Eric Laurentd4692962014-05-05 18:13:44 -07003330 }
3331 if (((profile->mSamplingRates[0] == 0) &&
3332 (profile->mSamplingRates.size() < 2)) ||
3333 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3334 (profile->mFormats.size() < 2)) ||
3335 ((profile->mChannelMasks[0] == 0) &&
3336 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003337 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003338 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003339 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003340 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3341 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003342 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003343 config.sample_rate = profile->pickSamplingRate();
3344 config.channel_mask = profile->pickChannelMask();
3345 config.format = profile->pickFormat();
3346 config.offload_info.sample_rate = config.sample_rate;
3347 config.offload_info.channel_mask = config.channel_mask;
3348 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003349 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003350 &output,
3351 &config,
3352 &desc->mDevice,
3353 address,
3354 &desc->mLatency,
3355 desc->mFlags);
3356 if (status == NO_ERROR) {
3357 desc->mSamplingRate = config.sample_rate;
3358 desc->mChannelMask = config.channel_mask;
3359 desc->mFormat = config.format;
3360 } else {
3361 output = AUDIO_IO_HANDLE_NONE;
3362 }
Eric Laurentd4692962014-05-05 18:13:44 -07003363 }
3364
Eric Laurentcf2c0212014-07-25 16:20:43 -07003365 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003366 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003367 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003368 sp<AudioPolicyMix> policyMix;
3369 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003370 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3371 address.string());
3372 }
François Gaffie036e1e92015-03-19 10:16:24 +01003373 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003374 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003375
Eric Laurent87ffa392015-05-22 10:32:38 -07003376 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3377 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003378 // no duplicated output for direct outputs and
3379 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003380 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003381
Eric Laurentd4692962014-05-05 18:13:44 -07003382 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003383 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003384
Eric Laurentd4692962014-05-05 18:13:44 -07003385 //TODO: configure audio effect output stage here
3386
3387 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003388 duplicatedOutput =
3389 mpClientInterface->openDuplicateOutput(output,
3390 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003391 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003392 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003393 sp<SwAudioOutputDescriptor> dupOutputDesc =
3394 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3395 dupOutputDesc->mOutput1 = mPrimaryOutput;
3396 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003397 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3398 dupOutputDesc->mFormat = desc->mFormat;
3399 dupOutputDesc->mChannelMask = desc->mChannelMask;
3400 dupOutputDesc->mLatency = desc->mLatency;
3401 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003402 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003403 } else {
3404 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003405 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003406 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003407 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003408 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003409 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003410 }
Eric Laurente552edb2014-03-10 17:42:56 -07003411 }
3412 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003413 } else {
3414 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003415 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003416 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003417 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003418 profiles.removeAt(profile_index);
3419 profile_index--;
3420 } else {
3421 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003422 // Load digital format info only for digital devices
3423 if (audio_device_is_digital(device)) {
3424 devDesc->importAudioPort(profile);
3425 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003426
François Gaffie53615e22015-03-19 09:24:12 +01003427 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003428 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3429 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003430 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003431 NULL/*patch handle*/, address.string());
3432 }
Eric Laurente552edb2014-03-10 17:42:56 -07003433 ALOGV("checkOutputsForDevice(): adding output %d", output);
3434 }
3435 }
3436
3437 if (profiles.isEmpty()) {
3438 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3439 return BAD_VALUE;
3440 }
Eric Laurentd4692962014-05-05 18:13:44 -07003441 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003442 // check if one opened output is not needed any more after disconnecting one device
3443 for (size_t i = 0; i < mOutputs.size(); i++) {
3444 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003445 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003446 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003447 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003448 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003449 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003450 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003451 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3452 mOutputs.keyAt(i));
3453 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003454 }
Eric Laurente552edb2014-03-10 17:42:56 -07003455 }
3456 }
Eric Laurentd4692962014-05-05 18:13:44 -07003457 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003458 for (size_t i = 0; i < mHwModules.size(); i++)
3459 {
3460 if (mHwModules[i]->mHandle == 0) {
3461 continue;
3462 }
3463 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3464 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003465 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003466 if (profile->mSupportedDevices.types() & device) {
3467 ALOGV("checkOutputsForDevice(): "
3468 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003469 if (profile->mSamplingRates[0] == 0) {
3470 profile->mSamplingRates.clear();
3471 profile->mSamplingRates.add(0);
3472 }
3473 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3474 profile->mFormats.clear();
3475 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3476 }
3477 if (profile->mChannelMasks[0] == 0) {
3478 profile->mChannelMasks.clear();
3479 profile->mChannelMasks.add(0);
3480 }
3481 }
3482 }
3483 }
3484 }
3485 return NO_ERROR;
3486}
3487
Paul McLean9080a4c2015-06-18 08:24:02 -07003488status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003489 audio_policy_dev_state_t state,
3490 SortedVector<audio_io_handle_t>& inputs,
3491 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003492{
Paul McLean9080a4c2015-06-18 08:24:02 -07003493 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003494 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003495
3496 if (audio_device_is_digital(device)) {
3497 // erase all current sample rates, formats and channel masks
3498 devDesc->clearCapabilities();
3499 }
3500
Eric Laurentd4692962014-05-05 18:13:44 -07003501 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3502 // first list already open inputs that can be routed to this device
3503 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3504 desc = mInputs.valueAt(input_index);
3505 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3506 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3507 inputs.add(mInputs.keyAt(input_index));
3508 }
3509 }
3510
3511 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003512 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003513 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3514 {
3515 if (mHwModules[module_idx]->mHandle == 0) {
3516 continue;
3517 }
3518 for (size_t profile_index = 0;
3519 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3520 profile_index++)
3521 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003522 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3523
3524 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003525 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003526 address == profile->mSupportedDevices[0]->mAddress) {
3527 profiles.add(profile);
3528 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3529 profile_index, module_idx);
3530 }
Eric Laurentd4692962014-05-05 18:13:44 -07003531 }
3532 }
3533 }
3534
3535 if (profiles.isEmpty() && inputs.isEmpty()) {
3536 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3537 return BAD_VALUE;
3538 }
3539
3540 // open inputs for matching profiles if needed. Direct inputs are also opened to
3541 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3542 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3543
Eric Laurent1c333e22014-05-20 10:48:17 -07003544 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003545 // nothing to do if one input is already opened for this profile
3546 size_t input_index;
3547 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3548 desc = mInputs.valueAt(input_index);
3549 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003550 if (audio_device_is_digital(device)) {
3551 devDesc->importAudioPort(profile);
3552 }
Eric Laurentd4692962014-05-05 18:13:44 -07003553 break;
3554 }
3555 }
3556 if (input_index != mInputs.size()) {
3557 continue;
3558 }
3559
3560 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3561 desc = new AudioInputDescriptor(profile);
3562 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003563 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3564 config.sample_rate = desc->mSamplingRate;
3565 config.channel_mask = desc->mChannelMask;
3566 config.format = desc->mFormat;
3567 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003568 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003569 &input,
3570 &config,
3571 &desc->mDevice,
3572 address,
3573 AUDIO_SOURCE_MIC,
3574 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003575
Eric Laurentcf2c0212014-07-25 16:20:43 -07003576 if (status == NO_ERROR) {
3577 desc->mSamplingRate = config.sample_rate;
3578 desc->mChannelMask = config.channel_mask;
3579 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003580
Eric Laurentd4692962014-05-05 18:13:44 -07003581 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003582 char *param = audio_device_address_to_parameter(device, address);
3583 mpClientInterface->setParameters(input, String8(param));
3584 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003585 }
3586
3587 // Here is where we step through and resolve any "dynamic" fields
3588 String8 reply;
3589 char *value;
3590 if (profile->mSamplingRates[0] == 0) {
3591 reply = mpClientInterface->getParameters(input,
3592 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3593 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3594 reply.string());
3595 value = strpbrk((char *)reply.string(), "=");
3596 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003597 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003598 }
3599 }
3600 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3601 reply = mpClientInterface->getParameters(input,
3602 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3603 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3604 value = strpbrk((char *)reply.string(), "=");
3605 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003606 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003607 }
3608 }
3609 if (profile->mChannelMasks[0] == 0) {
3610 reply = mpClientInterface->getParameters(input,
3611 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3612 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3613 reply.string());
3614 value = strpbrk((char *)reply.string(), "=");
3615 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003616 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003617 }
3618 }
3619 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3620 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3621 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3622 ALOGW("checkInputsForDevice() direct input missing param");
3623 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003624 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003625 }
3626
3627 if (input != 0) {
3628 addInput(input, desc);
3629 }
3630 } // endif input != 0
3631
Eric Laurentcf2c0212014-07-25 16:20:43 -07003632 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003633 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003634 profiles.removeAt(profile_index);
3635 profile_index--;
3636 } else {
3637 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003638 if (audio_device_is_digital(device)) {
3639 devDesc->importAudioPort(profile);
3640 }
Eric Laurentd4692962014-05-05 18:13:44 -07003641 ALOGV("checkInputsForDevice(): adding input %d", input);
3642 }
3643 } // end scan profiles
3644
3645 if (profiles.isEmpty()) {
3646 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3647 return BAD_VALUE;
3648 }
3649 } else {
3650 // Disconnect
3651 // check if one opened input is not needed any more after disconnecting one device
3652 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3653 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003654 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3655 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003656 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3657 mInputs.keyAt(input_index));
3658 inputs.add(mInputs.keyAt(input_index));
3659 }
3660 }
3661 // Clear any profiles associated with the disconnected device.
3662 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3663 if (mHwModules[module_index]->mHandle == 0) {
3664 continue;
3665 }
3666 for (size_t profile_index = 0;
3667 profile_index < mHwModules[module_index]->mInputProfiles.size();
3668 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003669 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Paul McLean9080a4c2015-06-18 08:24:02 -07003670 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003671 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003672 profile_index, module_index);
3673 if (profile->mSamplingRates[0] == 0) {
3674 profile->mSamplingRates.clear();
3675 profile->mSamplingRates.add(0);
3676 }
3677 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3678 profile->mFormats.clear();
3679 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3680 }
3681 if (profile->mChannelMasks[0] == 0) {
3682 profile->mChannelMasks.clear();
3683 profile->mChannelMasks.add(0);
3684 }
3685 }
3686 }
3687 }
3688 } // end disconnect
3689
3690 return NO_ERROR;
3691}
3692
3693
Eric Laurente0720872014-03-11 09:30:41 -07003694void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003695{
3696 ALOGV("closeOutput(%d)", output);
3697
Eric Laurentc75307b2015-03-17 15:29:32 -07003698 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003699 if (outputDesc == NULL) {
3700 ALOGW("closeOutput() unknown output %d", output);
3701 return;
3702 }
François Gaffie036e1e92015-03-19 10:16:24 +01003703 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003704
Eric Laurente552edb2014-03-10 17:42:56 -07003705 // look for duplicated outputs connected to the output being removed.
3706 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003707 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003708 if (dupOutputDesc->isDuplicated() &&
3709 (dupOutputDesc->mOutput1 == outputDesc ||
3710 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003711 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003712 if (dupOutputDesc->mOutput1 == outputDesc) {
3713 outputDesc2 = dupOutputDesc->mOutput2;
3714 } else {
3715 outputDesc2 = dupOutputDesc->mOutput1;
3716 }
3717 // As all active tracks on duplicated output will be deleted,
3718 // and as they were also referenced on the other output, the reference
3719 // count for their stream type must be adjusted accordingly on
3720 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003721 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003722 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003723 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003724 }
3725 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3726 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3727
3728 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003729 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003730 }
3731 }
3732
Eric Laurent05b90f82014-08-27 15:32:29 -07003733 nextAudioPortGeneration();
3734
3735 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3736 if (index >= 0) {
3737 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3738 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3739 mAudioPatches.removeItemsAt(index);
3740 mpClientInterface->onAudioPatchListUpdate();
3741 }
3742
Eric Laurente552edb2014-03-10 17:42:56 -07003743 AudioParameter param;
3744 param.add(String8("closing"), String8("true"));
3745 mpClientInterface->setParameters(output, param.toString());
3746
3747 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003748 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003749 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003750}
3751
3752void AudioPolicyManager::closeInput(audio_io_handle_t input)
3753{
3754 ALOGV("closeInput(%d)", input);
3755
3756 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3757 if (inputDesc == NULL) {
3758 ALOGW("closeInput() unknown input %d", input);
3759 return;
3760 }
3761
Eric Laurent6a94d692014-05-20 11:18:06 -07003762 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003763
3764 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3765 if (index >= 0) {
3766 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3767 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3768 mAudioPatches.removeItemsAt(index);
3769 mpClientInterface->onAudioPatchListUpdate();
3770 }
3771
3772 mpClientInterface->closeInput(input);
3773 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003774}
3775
Eric Laurentc75307b2015-03-17 15:29:32 -07003776SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3777 audio_devices_t device,
3778 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003779{
3780 SortedVector<audio_io_handle_t> outputs;
3781
3782 ALOGVV("getOutputsForDevice() device %04x", device);
3783 for (size_t i = 0; i < openOutputs.size(); i++) {
3784 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003785 i, openOutputs.valueAt(i)->isDuplicated(),
3786 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003787 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3788 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3789 outputs.add(openOutputs.keyAt(i));
3790 }
3791 }
3792 return outputs;
3793}
3794
Eric Laurente0720872014-03-11 09:30:41 -07003795bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003796 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003797{
3798 if (outputs1.size() != outputs2.size()) {
3799 return false;
3800 }
3801 for (size_t i = 0; i < outputs1.size(); i++) {
3802 if (outputs1[i] != outputs2[i]) {
3803 return false;
3804 }
3805 }
3806 return true;
3807}
3808
Eric Laurente0720872014-03-11 09:30:41 -07003809void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003810{
3811 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3812 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3813 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3814 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3815
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003816 // also take into account external policy-related changes: add all outputs which are
3817 // associated with policies in the "before" and "after" output vectors
3818 ALOGVV("checkOutputForStrategy(): policy related outputs");
3819 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003820 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003821 if (desc != 0 && desc->mPolicyMix != NULL) {
3822 srcOutputs.add(desc->mIoHandle);
3823 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3824 }
3825 }
3826 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003827 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003828 if (desc != 0 && desc->mPolicyMix != NULL) {
3829 dstOutputs.add(desc->mIoHandle);
3830 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3831 }
3832 }
3833
Eric Laurente552edb2014-03-10 17:42:56 -07003834 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3835 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3836 strategy, srcOutputs[0], dstOutputs[0]);
3837 // mute strategy while moving tracks from one output to another
3838 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003839 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003840 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003841 setStrategyMute(strategy, true, desc);
3842 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003843 }
3844 }
3845
3846 // Move effects associated to this strategy from previous output to new output
3847 if (strategy == STRATEGY_MEDIA) {
3848 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3849 SortedVector<audio_io_handle_t> moved;
3850 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003851 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3852 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3853 effectDesc->mIo != fxOutput) {
3854 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003855 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3856 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003857 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003858 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003859 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003860 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003861 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003862 }
3863 }
3864 }
3865 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003866 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003867 if (i == AUDIO_STREAM_PATCH) {
3868 continue;
3869 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003870 if (getStrategy((audio_stream_type_t)i) == strategy) {
3871 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003872 }
3873 }
3874 }
3875}
3876
Eric Laurente0720872014-03-11 09:30:41 -07003877void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003878{
François Gaffie2110e042015-03-24 08:41:51 +01003879 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003880 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003881 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003882 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003883 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003884 checkOutputForStrategy(STRATEGY_SONIFICATION);
3885 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003886 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003887 checkOutputForStrategy(STRATEGY_MEDIA);
3888 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003889 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003890}
3891
Eric Laurente0720872014-03-11 09:30:41 -07003892void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003893{
François Gaffie53615e22015-03-19 09:24:12 +01003894 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003895 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003896 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003897 return;
3898 }
3899
Eric Laurent3a4311c2014-03-17 12:00:47 -07003900 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003901 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3902 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3903 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003904 // suspend A2DP output if:
3905 // (NOT already suspended) &&
3906 // ((SCO device is connected &&
3907 // (forced usage for communication || for record is SCO))) ||
3908 // (phone state is ringing || in call)
3909 //
3910 // restore A2DP output if:
3911 // (Already suspended) &&
3912 // ((SCO device is NOT connected ||
3913 // (forced usage NOT for communication && NOT for record is SCO))) &&
3914 // (phone state is NOT ringing && NOT in call)
3915 //
3916 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003917 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003918 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3919 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3920 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3921 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003922
3923 mpClientInterface->restoreOutput(a2dpOutput);
3924 mA2dpSuspended = false;
3925 }
3926 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003927 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003928 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3929 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3930 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3931 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003932
3933 mpClientInterface->suspendOutput(a2dpOutput);
3934 mA2dpSuspended = true;
3935 }
3936 }
3937}
3938
Eric Laurentc75307b2015-03-17 15:29:32 -07003939audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3940 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003941{
3942 audio_devices_t device = AUDIO_DEVICE_NONE;
3943
Eric Laurent6a94d692014-05-20 11:18:06 -07003944 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3945 if (index >= 0) {
3946 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3947 if (patchDesc->mUid != mUidCached) {
3948 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3949 outputDesc->device(), outputDesc->mPatchHandle);
3950 return outputDesc->device();
3951 }
3952 }
3953
Eric Laurente552edb2014-03-10 17:42:56 -07003954 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003955 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003956 // use device for strategy enforced audible
3957 // 2: we are in call or the strategy phone is active on the output:
3958 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003959 // 3: the strategy for enforced audible is active but not enforced on the output:
3960 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003961 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08003962 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003963 // 5: the strategy sonification is active on the output:
3964 // use device for strategy sonification
3965 // 6: the strategy "respectful" sonification is active on the output:
3966 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003967 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003968 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003969 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003970 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003971 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003972 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003973 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003974 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003975 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3976 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003977 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003978 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003979 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003980 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003981 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
3982 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003983 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003984 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003985 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003986 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003987 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003988 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003989 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003990 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003991 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003992 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003993 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003994 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003995 }
3996
Eric Laurent1c333e22014-05-20 10:48:17 -07003997 ALOGV("getNewOutputDevice() selected device %x", device);
3998 return device;
3999}
4000
4001audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
4002{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004003 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004004
4005 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4006 if (index >= 0) {
4007 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4008 if (patchDesc->mUid != mUidCached) {
4009 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4010 inputDesc->mDevice, inputDesc->mPatchHandle);
4011 return inputDesc->mDevice;
4012 }
4013 }
4014
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004015 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07004016
Eric Laurente552edb2014-03-10 17:42:56 -07004017 return device;
4018}
4019
Eric Laurente0720872014-03-11 09:30:41 -07004020uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004021 return (uint32_t)getStrategy(stream);
4022}
4023
Eric Laurente0720872014-03-11 09:30:41 -07004024audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004025 // By checking the range of stream before calling getStrategy, we avoid
4026 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4027 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004028 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004029 return AUDIO_DEVICE_NONE;
4030 }
4031 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004032 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07004033 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4034 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4035 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004036 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004037 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004038 devices = outputDesc->device();
4039 break;
4040 }
Eric Laurente552edb2014-03-10 17:42:56 -07004041 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004042
4043 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4044 and doesn't really need to.*/
4045 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4046 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4047 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4048 }
4049
Eric Laurente552edb2014-03-10 17:42:56 -07004050 return devices;
4051}
4052
François Gaffie2110e042015-03-24 08:41:51 +01004053routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4054{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004055 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004056 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004057}
4058
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004059uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4060 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004061 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4062 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4063 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004064 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4065 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4066 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004067 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004068 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004069}
4070
Eric Laurente0720872014-03-11 09:30:41 -07004071void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004072 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004073 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004074 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4075 updateDevicesAndOutputs();
4076 break;
4077 default:
4078 break;
4079 }
4080}
4081
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004082uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004083
4084 // skip beacon mute management if a dedicated TTS output is available
4085 if (mTtsOutputAvailable) {
4086 return 0;
4087 }
4088
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004089 switch(event) {
4090 case STARTING_OUTPUT:
4091 mBeaconMuteRefCount++;
4092 break;
4093 case STOPPING_OUTPUT:
4094 if (mBeaconMuteRefCount > 0) {
4095 mBeaconMuteRefCount--;
4096 }
4097 break;
4098 case STARTING_BEACON:
4099 mBeaconPlayingRefCount++;
4100 break;
4101 case STOPPING_BEACON:
4102 if (mBeaconPlayingRefCount > 0) {
4103 mBeaconPlayingRefCount--;
4104 }
4105 break;
4106 }
4107
4108 if (mBeaconMuteRefCount > 0) {
4109 // any playback causes beacon to be muted
4110 return setBeaconMute(true);
4111 } else {
4112 // no other playback: unmute when beacon starts playing, mute when it stops
4113 return setBeaconMute(mBeaconPlayingRefCount == 0);
4114 }
4115}
4116
4117uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4118 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4119 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4120 // keep track of muted state to avoid repeating mute/unmute operations
4121 if (mBeaconMuted != mute) {
4122 // mute/unmute AUDIO_STREAM_TTS on all outputs
4123 ALOGV("\t muting %d", mute);
4124 uint32_t maxLatency = 0;
4125 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004126 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004127 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004128 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004129 0 /*delay*/, AUDIO_DEVICE_NONE);
4130 const uint32_t latency = desc->latency() * 2;
4131 if (latency > maxLatency) {
4132 maxLatency = latency;
4133 }
4134 }
4135 mBeaconMuted = mute;
4136 return maxLatency;
4137 }
4138 return 0;
4139}
4140
Eric Laurente0720872014-03-11 09:30:41 -07004141audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004142 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004143{
Paul McLeanaa981192015-03-21 09:55:15 -07004144 // Routing
4145 // see if we have an explicit route
4146 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4147 // (getStrategy(stream)).
4148 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4149 // and activity count is non-zero
4150 // the device = the device from the descriptor in the RouteMap, and exit.
4151 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4152 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4153 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004154 // Special case for accessibility strategy which must follow any strategy it is
4155 // currently remapped to
4156 bool strategyMatch = (strat == strategy) ||
4157 ((strategy == STRATEGY_ACCESSIBILITY) &&
4158 ((mEngine->getStrategyForUsage(
4159 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4160 (strat == STRATEGY_MEDIA)));
4161 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004162 return route->mDeviceDescriptor->type();
4163 }
4164 }
4165
Eric Laurente552edb2014-03-10 17:42:56 -07004166 if (fromCache) {
4167 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4168 strategy, mDeviceForStrategy[strategy]);
4169 return mDeviceForStrategy[strategy];
4170 }
François Gaffie2110e042015-03-24 08:41:51 +01004171 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004172}
4173
Eric Laurente0720872014-03-11 09:30:41 -07004174void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004175{
4176 for (int i = 0; i < NUM_STRATEGIES; i++) {
4177 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4178 }
4179 mPreviousOutputs = mOutputs;
4180}
4181
Eric Laurent1f2f2232014-06-02 12:01:23 -07004182uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004183 audio_devices_t prevDevice,
4184 uint32_t delayMs)
4185{
4186 // mute/unmute strategies using an incompatible device combination
4187 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4188 // if unmuting, unmute only after the specified delay
4189 if (outputDesc->isDuplicated()) {
4190 return 0;
4191 }
4192
4193 uint32_t muteWaitMs = 0;
4194 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004195 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004196
4197 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4198 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004199 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004200 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4201 bool doMute = false;
4202
4203 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4204 doMute = true;
4205 outputDesc->mStrategyMutedByDevice[i] = true;
4206 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4207 doMute = true;
4208 outputDesc->mStrategyMutedByDevice[i] = false;
4209 }
Eric Laurent99401132014-05-07 19:48:15 -07004210 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004211 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004212 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004213 // skip output if it does not share any device with current output
4214 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4215 == AUDIO_DEVICE_NONE) {
4216 continue;
4217 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004218 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4219 mute ? "muting" : "unmuting", i, curDevice);
4220 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004221 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004222 if (mute) {
4223 // FIXME: should not need to double latency if volume could be applied
4224 // immediately by the audioflinger mixer. We must account for the delay
4225 // between now and the next time the audioflinger thread for this output
4226 // will process a buffer (which corresponds to one buffer size,
4227 // usually 1/2 or 1/4 of the latency).
4228 if (muteWaitMs < desc->latency() * 2) {
4229 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004230 }
4231 }
4232 }
4233 }
4234 }
4235 }
4236
Eric Laurent99401132014-05-07 19:48:15 -07004237 // temporary mute output if device selection changes to avoid volume bursts due to
4238 // different per device volumes
4239 if (outputDesc->isActive() && (device != prevDevice)) {
4240 if (muteWaitMs < outputDesc->latency() * 2) {
4241 muteWaitMs = outputDesc->latency() * 2;
4242 }
4243 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004244 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004245 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004246 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004247 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004248 muteWaitMs *2, device);
4249 }
4250 }
4251 }
4252
Eric Laurente552edb2014-03-10 17:42:56 -07004253 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4254 if (muteWaitMs > delayMs) {
4255 muteWaitMs -= delayMs;
4256 usleep(muteWaitMs * 1000);
4257 return muteWaitMs;
4258 }
4259 return 0;
4260}
4261
Eric Laurentc75307b2015-03-17 15:29:32 -07004262uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004263 audio_devices_t device,
4264 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004265 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004266 audio_patch_handle_t *patchHandle,
4267 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004268{
Eric Laurentc75307b2015-03-17 15:29:32 -07004269 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004270 AudioParameter param;
4271 uint32_t muteWaitMs;
4272
4273 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004274 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4275 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004276 return muteWaitMs;
4277 }
4278 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4279 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004280 if ((device != AUDIO_DEVICE_NONE) &&
4281 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004282 return 0;
4283 }
4284
4285 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004286 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004287
4288 audio_devices_t prevDevice = outputDesc->mDevice;
4289
Paul McLeanaa981192015-03-21 09:55:15 -07004290 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004291
4292 if (device != AUDIO_DEVICE_NONE) {
4293 outputDesc->mDevice = device;
4294 }
4295 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4296
4297 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004298 // the requested device is AUDIO_DEVICE_NONE
4299 // OR the requested device is the same as current device
4300 // AND force is not specified
4301 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004302 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004303 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4304 !force &&
4305 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004306 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004307 return muteWaitMs;
4308 }
4309
4310 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004311
Eric Laurente552edb2014-03-10 17:42:56 -07004312 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004313 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004314 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004315 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004316 DeviceVector deviceList = (address == NULL) ?
4317 mAvailableOutputDevices.getDevicesFromType(device)
4318 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004319 if (!deviceList.isEmpty()) {
4320 struct audio_patch patch;
4321 outputDesc->toAudioPortConfig(&patch.sources[0]);
4322 patch.num_sources = 1;
4323 patch.num_sinks = 0;
4324 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4325 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004326 patch.num_sinks++;
4327 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004328 ssize_t index;
4329 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4330 index = mAudioPatches.indexOfKey(*patchHandle);
4331 } else {
4332 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4333 }
4334 sp< AudioPatch> patchDesc;
4335 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4336 if (index >= 0) {
4337 patchDesc = mAudioPatches.valueAt(index);
4338 afPatchHandle = patchDesc->mAfPatchHandle;
4339 }
4340
Eric Laurent1c333e22014-05-20 10:48:17 -07004341 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004342 &afPatchHandle,
4343 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004344 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4345 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004346 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004347 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004348 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004349 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004350 addAudioPatch(patchDesc->mHandle, patchDesc);
4351 } else {
4352 patchDesc->mPatch = patch;
4353 }
4354 patchDesc->mAfPatchHandle = afPatchHandle;
4355 patchDesc->mUid = mUidCached;
4356 if (patchHandle) {
4357 *patchHandle = patchDesc->mHandle;
4358 }
4359 outputDesc->mPatchHandle = patchDesc->mHandle;
4360 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004361 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004362 }
4363 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004364
4365 // inform all input as well
4366 for (size_t i = 0; i < mInputs.size(); i++) {
4367 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004368 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004369 AudioParameter inputCmd = AudioParameter();
4370 ALOGV("%s: inform input %d of device:%d", __func__,
4371 inputDescriptor->mIoHandle, device);
4372 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4373 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4374 inputCmd.toString(),
4375 delayMs);
4376 }
4377 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004378 }
Eric Laurente552edb2014-03-10 17:42:56 -07004379
4380 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004381 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004382
4383 return muteWaitMs;
4384}
4385
Eric Laurentc75307b2015-03-17 15:29:32 -07004386status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004387 int delayMs,
4388 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004389{
Eric Laurent6a94d692014-05-20 11:18:06 -07004390 ssize_t index;
4391 if (patchHandle) {
4392 index = mAudioPatches.indexOfKey(*patchHandle);
4393 } else {
4394 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4395 }
4396 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004397 return INVALID_OPERATION;
4398 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004399 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4400 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004401 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4402 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004403 removeAudioPatch(patchDesc->mHandle);
4404 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004405 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004406 return status;
4407}
4408
4409status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4410 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004411 bool force,
4412 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004413{
4414 status_t status = NO_ERROR;
4415
Eric Laurent1f2f2232014-06-02 12:01:23 -07004416 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004417 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4418 inputDesc->mDevice = device;
4419
4420 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4421 if (!deviceList.isEmpty()) {
4422 struct audio_patch patch;
4423 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004424 // AUDIO_SOURCE_HOTWORD is for internal use only:
4425 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004426 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4427 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004428 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4429 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004430 patch.num_sinks = 1;
4431 //only one input device for now
4432 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004433 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004434 ssize_t index;
4435 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4436 index = mAudioPatches.indexOfKey(*patchHandle);
4437 } else {
4438 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4439 }
4440 sp< AudioPatch> patchDesc;
4441 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4442 if (index >= 0) {
4443 patchDesc = mAudioPatches.valueAt(index);
4444 afPatchHandle = patchDesc->mAfPatchHandle;
4445 }
4446
Eric Laurent1c333e22014-05-20 10:48:17 -07004447 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004448 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004449 0);
4450 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004451 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004452 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004453 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004454 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004455 addAudioPatch(patchDesc->mHandle, patchDesc);
4456 } else {
4457 patchDesc->mPatch = patch;
4458 }
4459 patchDesc->mAfPatchHandle = afPatchHandle;
4460 patchDesc->mUid = mUidCached;
4461 if (patchHandle) {
4462 *patchHandle = patchDesc->mHandle;
4463 }
4464 inputDesc->mPatchHandle = patchDesc->mHandle;
4465 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004466 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004467 }
4468 }
4469 }
4470 return status;
4471}
4472
Eric Laurent6a94d692014-05-20 11:18:06 -07004473status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4474 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004475{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004476 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004477 ssize_t index;
4478 if (patchHandle) {
4479 index = mAudioPatches.indexOfKey(*patchHandle);
4480 } else {
4481 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4482 }
4483 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004484 return INVALID_OPERATION;
4485 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004486 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4487 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004488 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4489 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004490 removeAudioPatch(patchDesc->mHandle);
4491 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004492 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004493 return status;
4494}
4495
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004496sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004497 String8 address,
4498 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004499 audio_format_t& format,
4500 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004501 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004502{
4503 // Choose an input profile based on the requested capture parameters: select the first available
4504 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004505 //
4506 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4507 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004508
4509 for (size_t i = 0; i < mHwModules.size(); i++)
4510 {
4511 if (mHwModules[i]->mHandle == 0) {
4512 continue;
4513 }
4514 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4515 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004516 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004517 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004518 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004519 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004520 format,
4521 &format /*updatedFormat*/,
4522 channelMask,
4523 &channelMask /*updatedChannelMask*/,
4524 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004525
Eric Laurente552edb2014-03-10 17:42:56 -07004526 return profile;
4527 }
4528 }
4529 }
4530 return NULL;
4531}
4532
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004533
4534audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004535 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004536{
François Gaffie036e1e92015-03-19 10:16:24 +01004537 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4538 audio_devices_t selectedDeviceFromMix =
4539 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004540
François Gaffie036e1e92015-03-19 10:16:24 +01004541 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4542 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004543 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004544 return getDeviceForInputSource(inputSource);
4545}
4546
4547audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4548{
Paul McLean466dc8e2015-04-17 13:15:36 -06004549 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4550 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004551 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004552 return route->mDeviceDescriptor->type();
4553 }
4554 }
4555
4556 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004557}
4558
Eric Laurente0720872014-03-11 09:30:41 -07004559float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004560 int index,
4561 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004562{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004563 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004564
4565 // if a headset is connected, apply the following rules to ring tones and notifications
4566 // to avoid sound level bursts in user's ears:
4567 // - always attenuate ring tones and notifications volume by 6dB
4568 // - if music is playing, always limit the volume to current music volume,
4569 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004570 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004571 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4572 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4573 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4574 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4575 ((stream_strategy == STRATEGY_SONIFICATION)
4576 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004577 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004578 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004579 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4580 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004581 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004582 // when the phone is ringing we must consider that music could have been paused just before
4583 // by the music application and behave as if music was active if the last music track was
4584 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004585 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004586 mLimitRingtoneVolume) {
4587 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004588 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4589 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004590 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004591 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4592 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4593 if (volumeDb > minVolDB) {
4594 volumeDb = minVolDB;
4595 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004596 }
4597 }
4598 }
4599
Eric Laurentffbc80f2015-03-18 18:30:19 -07004600 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004601}
4602
Eric Laurente0720872014-03-11 09:30:41 -07004603status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004604 int index,
4605 const sp<AudioOutputDescriptor>& outputDesc,
4606 audio_devices_t device,
4607 int delayMs,
4608 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004609{
Eric Laurente552edb2014-03-10 17:42:56 -07004610 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004611 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004612 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004613 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004614 return NO_ERROR;
4615 }
François Gaffie2110e042015-03-24 08:41:51 +01004616 audio_policy_forced_cfg_t forceUseForComm =
4617 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004618 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004619 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4620 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004621 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004622 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004623 return INVALID_OPERATION;
4624 }
4625
Eric Laurentc75307b2015-03-17 15:29:32 -07004626 if (device == AUDIO_DEVICE_NONE) {
4627 device = outputDesc->device();
4628 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004629
Eric Laurentffbc80f2015-03-18 18:30:19 -07004630 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004631 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004632 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004633 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004634
Eric Laurentffbc80f2015-03-18 18:30:19 -07004635 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004636
Eric Laurent3b73df72014-03-11 09:06:29 -07004637 if (stream == AUDIO_STREAM_VOICE_CALL ||
4638 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004639 float voiceVolume;
4640 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004641 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004642 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004643 } else {
4644 voiceVolume = 1.0;
4645 }
4646
Eric Laurentc75307b2015-03-17 15:29:32 -07004647 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004648 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4649 mLastVoiceVolume = voiceVolume;
4650 }
4651 }
4652
4653 return NO_ERROR;
4654}
4655
Eric Laurentc75307b2015-03-17 15:29:32 -07004656void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4657 audio_devices_t device,
4658 int delayMs,
4659 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004660{
Eric Laurentc75307b2015-03-17 15:29:32 -07004661 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004662
Eric Laurent3b73df72014-03-11 09:06:29 -07004663 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004664 if (stream == AUDIO_STREAM_PATCH) {
4665 continue;
4666 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004667 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004668 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4669 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004670 device,
4671 delayMs,
4672 force);
4673 }
4674}
4675
Eric Laurente0720872014-03-11 09:30:41 -07004676void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004677 bool on,
4678 const sp<AudioOutputDescriptor>& outputDesc,
4679 int delayMs,
4680 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004681{
Eric Laurent72249d52015-06-08 11:12:15 -07004682 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4683 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004684 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004685 if (stream == AUDIO_STREAM_PATCH) {
4686 continue;
4687 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004688 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004689 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004690 }
4691 }
4692}
4693
Eric Laurente0720872014-03-11 09:30:41 -07004694void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004695 bool on,
4696 const sp<AudioOutputDescriptor>& outputDesc,
4697 int delayMs,
4698 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004699{
Eric Laurentc75307b2015-03-17 15:29:32 -07004700 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004701 if (device == AUDIO_DEVICE_NONE) {
4702 device = outputDesc->device();
4703 }
4704
Eric Laurentc75307b2015-03-17 15:29:32 -07004705 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4706 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004707
4708 if (on) {
4709 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004710 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004711 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004712 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004713 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004714 }
4715 }
4716 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4717 outputDesc->mMuteCount[stream]++;
4718 } else {
4719 if (outputDesc->mMuteCount[stream] == 0) {
4720 ALOGV("setStreamMute() unmuting non muted stream!");
4721 return;
4722 }
4723 if (--outputDesc->mMuteCount[stream] == 0) {
4724 checkAndSetVolume(stream,
4725 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004726 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004727 device,
4728 delayMs);
4729 }
4730 }
4731}
4732
Eric Laurente0720872014-03-11 09:30:41 -07004733void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004734 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004735{
Eric Laurent87ffa392015-05-22 10:32:38 -07004736 if(!hasPrimaryOutput()) {
4737 return;
4738 }
4739
Eric Laurente552edb2014-03-10 17:42:56 -07004740 // if the stream pertains to sonification strategy and we are in call we must
4741 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4742 // in the device used for phone strategy and play the tone if the selected device does not
4743 // interfere with the device used for phone strategy
4744 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4745 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004746 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004747 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4748 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004749 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004750 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4751 stream, starting, outputDesc->mDevice, stateChange);
4752 if (outputDesc->mRefCount[stream]) {
4753 int muteCount = 1;
4754 if (stateChange) {
4755 muteCount = outputDesc->mRefCount[stream];
4756 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004757 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004758 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4759 for (int i = 0; i < muteCount; i++) {
4760 setStreamMute(stream, starting, mPrimaryOutput);
4761 }
4762 } else {
4763 ALOGV("handleIncallSonification() high visibility");
4764 if (outputDesc->device() &
4765 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4766 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4767 for (int i = 0; i < muteCount; i++) {
4768 setStreamMute(stream, starting, mPrimaryOutput);
4769 }
4770 }
4771 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004772 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4773 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004774 } else {
4775 mpClientInterface->stopTone();
4776 }
4777 }
4778 }
4779 }
4780}
4781
Paul McLeanaa981192015-03-21 09:55:15 -07004782
Paul McLean466dc8e2015-04-17 13:15:36 -06004783
Eric Laurente0720872014-03-11 09:30:41 -07004784void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004785{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004786 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004787 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004788 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004789 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004790 mAvailableOutputDevices.add(mDefaultOutputDevice);
4791 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004792
4793 module = new HwModule("primary");
4794
Eric Laurent322b4d22015-04-03 15:57:54 -07004795 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4796 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004797 profile->mSamplingRates.add(44100);
4798 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4799 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004800 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004801 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4802 module->mOutputProfiles.add(profile);
4803
Eric Laurent322b4d22015-04-03 15:57:54 -07004804 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4805 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004806 profile->mSamplingRates.add(8000);
4807 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4808 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004809 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004810 module->mInputProfiles.add(profile);
4811
4812 mHwModules.add(module);
4813}
4814
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004815audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4816{
4817 // flags to stream type mapping
4818 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4819 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4820 }
4821 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4822 return AUDIO_STREAM_BLUETOOTH_SCO;
4823 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004824 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4825 return AUDIO_STREAM_TTS;
4826 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004827
4828 // usage to stream type mapping
4829 switch (attr->usage) {
4830 case AUDIO_USAGE_MEDIA:
4831 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004832 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4833 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004834 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004835 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4836 return AUDIO_STREAM_ALARM;
4837 }
4838 if (isStreamActive(AUDIO_STREAM_RING)) {
4839 return AUDIO_STREAM_RING;
4840 }
4841 if (isInCall()) {
4842 return AUDIO_STREAM_VOICE_CALL;
4843 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004844 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004845 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4846 return AUDIO_STREAM_SYSTEM;
4847 case AUDIO_USAGE_VOICE_COMMUNICATION:
4848 return AUDIO_STREAM_VOICE_CALL;
4849
4850 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4851 return AUDIO_STREAM_DTMF;
4852
4853 case AUDIO_USAGE_ALARM:
4854 return AUDIO_STREAM_ALARM;
4855 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4856 return AUDIO_STREAM_RING;
4857
4858 case AUDIO_USAGE_NOTIFICATION:
4859 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4860 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4861 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4862 case AUDIO_USAGE_NOTIFICATION_EVENT:
4863 return AUDIO_STREAM_NOTIFICATION;
4864
4865 case AUDIO_USAGE_UNKNOWN:
4866 default:
4867 return AUDIO_STREAM_MUSIC;
4868 }
4869}
Eric Laurente83b55d2014-11-14 10:06:21 -08004870
François Gaffie53615e22015-03-19 09:24:12 +01004871bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4872{
Eric Laurente83b55d2014-11-14 10:06:21 -08004873 // has flags that map to a strategy?
4874 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4875 return true;
4876 }
4877
4878 // has known usage?
4879 switch (paa->usage) {
4880 case AUDIO_USAGE_UNKNOWN:
4881 case AUDIO_USAGE_MEDIA:
4882 case AUDIO_USAGE_VOICE_COMMUNICATION:
4883 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4884 case AUDIO_USAGE_ALARM:
4885 case AUDIO_USAGE_NOTIFICATION:
4886 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4887 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4888 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4889 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4890 case AUDIO_USAGE_NOTIFICATION_EVENT:
4891 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4892 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4893 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4894 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004895 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004896 break;
4897 default:
4898 return false;
4899 }
4900 return true;
4901}
4902
François Gaffiead3183e2015-03-18 16:55:35 +01004903bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4904 routing_strategy strategy, uint32_t inPastMs,
4905 nsecs_t sysTime) const
4906{
4907 if ((sysTime == 0) && (inPastMs != 0)) {
4908 sysTime = systemTime();
4909 }
4910 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4911 if (i == AUDIO_STREAM_PATCH) {
4912 continue;
4913 }
4914 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4915 (NUM_STRATEGIES == strategy)) &&
4916 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4917 return true;
4918 }
4919 }
4920 return false;
4921}
4922
François Gaffie2110e042015-03-24 08:41:51 +01004923audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4924{
4925 return mEngine->getForceUse(usage);
4926}
4927
4928bool AudioPolicyManager::isInCall()
4929{
4930 return isStateInCall(mEngine->getPhoneState());
4931}
4932
4933bool AudioPolicyManager::isStateInCall(int state)
4934{
4935 return is_state_in_call(state);
4936}
4937
Eric Laurente552edb2014-03-10 17:42:56 -07004938}; // namespace android