blob: 3ea6a11bb54a80dd7536736bab5b38b4f7415071 [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();
François Gaffie2110e042015-03-24 08:41:51 +0100174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
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 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, 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
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, 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
François Gaffie2110e042015-03-24 08:41:51 +0100264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
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 Laurentc73ca6e2014-12-12 14:34:22 -0800305 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700306 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
307
308 // release existing RX patch if any
309 if (mCallRxPatch != 0) {
310 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
311 mCallRxPatch.clear();
312 }
313 // release TX patch if any
314 if (mCallTxPatch != 0) {
315 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
316 mCallTxPatch.clear();
317 }
318
319 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
320 // via setOutputDevice() on primary output.
321 // Otherwise, create two audio patches for TX and RX path.
322 if (availablePrimaryOutputDevices() & rxDevice) {
323 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
324 // If the TX device is also on the primary HW module, setOutputDevice() will take care
325 // of it due to legacy implementation. If not, create a patch.
326 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
327 == AUDIO_DEVICE_NONE) {
328 createTxPatch = true;
329 }
330 } else {
331 // create RX path audio patch
332 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
333 ALOG_ASSERT(!deviceList.isEmpty(),
334 "updateCallRouting() selected device not in output device list");
335 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
336 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
337 ALOG_ASSERT(!deviceList.isEmpty(),
338 "updateCallRouting() no telephony RX device");
339 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
340
341 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
342 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
343
344 // request to reuse existing output stream if one is already opened to reach the RX device
345 SortedVector<audio_io_handle_t> outputs =
346 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700347 audio_io_handle_t output = selectOutput(outputs,
348 AUDIO_OUTPUT_FLAG_NONE,
349 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700350 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 ALOG_ASSERT(!outputDesc->isDuplicated(),
353 "updateCallRouting() RX device output is duplicated");
354 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700355 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700356 patch.num_sources = 2;
357 }
358
359 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
360 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
361 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
362 status);
363 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100364 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700365 mCallRxPatch->mAfPatchHandle = afPatchHandle;
366 mCallRxPatch->mUid = mUidCached;
367 }
368 createTxPatch = true;
369 }
370 if (createTxPatch) {
371
372 struct audio_patch patch;
373 patch.num_sources = 1;
374 patch.num_sinks = 1;
375 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
376 ALOG_ASSERT(!deviceList.isEmpty(),
377 "updateCallRouting() selected device not in input device list");
378 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
379 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
381 ALOG_ASSERT(!deviceList.isEmpty(),
382 "updateCallRouting() no telephony TX device");
383 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
384 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
385
386 SortedVector<audio_io_handle_t> outputs =
387 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700388 audio_io_handle_t output = selectOutput(outputs,
389 AUDIO_OUTPUT_FLAG_NONE,
390 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700391 // request to reuse existing output stream if one is already opened to reach the TX
392 // path output device
393 if (output != AUDIO_IO_HANDLE_NONE) {
394 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
395 ALOG_ASSERT(!outputDesc->isDuplicated(),
396 "updateCallRouting() RX device output is duplicated");
397 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700398 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 patch.num_sources = 2;
400 }
401
402 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
403 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
404 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
405 status);
406 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100407 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 mCallTxPatch->mAfPatchHandle = afPatchHandle;
409 mCallTxPatch->mUid = mUidCached;
410 }
411 }
412}
413
Eric Laurente0720872014-03-11 09:30:41 -0700414void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700415{
416 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100417 // store previous phone state for management of sonification strategy below
418 int oldState = mEngine->getPhoneState();
419
420 if (mEngine->setPhoneState(state) != NO_ERROR) {
421 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700422 return;
423 }
François Gaffie2110e042015-03-24 08:41:51 +0100424 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700425 // if leaving call state, handle special case of active streams
426 // pertaining to sonification strategy see handleIncallSonification()
427 if (isInCall()) {
428 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (stream == AUDIO_STREAM_PATCH) {
431 continue;
432 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700433 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800435
436 // force reevaluating accessibility routing when call starts
437 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 /**
441 * Switching to or from incall state or switching between telephony and VoIP lead to force
442 * routing command.
443 */
444 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
445 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700446
447 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700448 checkA2dpSuspend();
449 checkOutputForAllStrategies();
450 updateDevicesAndOutputs();
451
Eric Laurentc75307b2015-03-17 15:29:32 -0700452 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -0700453
Eric Laurente552edb2014-03-10 17:42:56 -0700454 int delayMs = 0;
455 if (isStateInCall(state)) {
456 nsecs_t sysTime = systemTime();
457 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700458 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700459 // mute media and sonification strategies and delay device switch by the largest
460 // latency of any output where either strategy is active.
461 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100462 if ((isStrategyActive(desc, STRATEGY_MEDIA,
463 SONIFICATION_HEADSET_MUSIC_DELAY,
464 sysTime) ||
465 isStrategyActive(desc, STRATEGY_SONIFICATION,
466 SONIFICATION_HEADSET_MUSIC_DELAY,
467 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700468 (delayMs < (int)desc->latency()*2)) {
469 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700471 setStrategyMute(STRATEGY_MEDIA, true, desc);
472 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700473 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700474 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
475 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700476 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
477 }
478 }
479
Eric Laurentc2730ba2014-07-20 15:47:07 -0700480 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
481 // the device returned is not necessarily reachable via this output
482 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
483 // force routing command to audio hardware when ending call
484 // even if no device change is needed
485 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
486 rxDevice = hwOutputDesc->device();
487 }
Eric Laurente552edb2014-03-10 17:42:56 -0700488
Eric Laurentc2730ba2014-07-20 15:47:07 -0700489 if (state == AUDIO_MODE_IN_CALL) {
490 updateCallRouting(rxDevice, delayMs);
491 } else if (oldState == AUDIO_MODE_IN_CALL) {
492 if (mCallRxPatch != 0) {
493 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
494 mCallRxPatch.clear();
495 }
496 if (mCallTxPatch != 0) {
497 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
498 mCallTxPatch.clear();
499 }
500 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
501 } else {
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504 // if entering in call state, handle special case of active streams
505 // pertaining to sonification strategy see handleIncallSonification()
506 if (isStateInCall(state)) {
507 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700512 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700513 }
514 }
515
516 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700517 if (state == AUDIO_MODE_RINGTONE &&
518 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 mLimitRingtoneVolume = true;
520 } else {
521 mLimitRingtoneVolume = false;
522 }
523}
524
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700525audio_mode_t AudioPolicyManager::getPhoneState() {
526 return mEngine->getPhoneState();
527}
528
Eric Laurente0720872014-03-11 09:30:41 -0700529void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
François Gaffie2110e042015-03-24 08:41:51 +0100532 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700533
François Gaffie2110e042015-03-24 08:41:51 +0100534 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
535 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
536 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700537 }
François Gaffie2110e042015-03-24 08:41:51 +0100538 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
539 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
540 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700541
542 // check for device and output changes triggered by new force usage
543 checkA2dpSuspend();
544 checkOutputForAllStrategies();
545 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100546 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700547 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
548 updateCallRouting(newDevice);
549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700551 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
552 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
553 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
554 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700558 }
559 }
560
François Gaffie53615e22015-03-19 09:24:12 +0100561 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700563 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565
566}
567
Eric Laurente0720872014-03-11 09:30:41 -0700568void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
570 ALOGV("setSystemProperty() property %s, value %s", property, value);
571}
572
573// Find a direct output profile compatible with the parameters passed, even if the input flags do
574// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700576 audio_devices_t device,
577 uint32_t samplingRate,
578 audio_format_t format,
579 audio_channel_mask_t channelMask,
580 audio_output_flags_t flags)
581{
582 for (size_t i = 0; i < mHwModules.size(); i++) {
583 if (mHwModules[i]->mHandle == 0) {
584 continue;
585 }
586 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700587 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Andy Hungf129b032015-04-07 13:45:50 -0700588 bool found = profile->isCompatibleProfile(device, String8(""),
589 samplingRate, NULL /*updatedSamplingRate*/,
590 format, NULL /*updatedFormat*/,
591 channelMask, NULL /*updatedChannelMask*/,
Glenn Kastencbd48022014-07-24 13:46:44 -0700592 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
593 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700594 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
595 return profile;
596 }
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
598 }
599 return 0;
600}
601
Eric Laurente0720872014-03-11 09:30:41 -0700602audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags,
607 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700608{
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
611 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
612 device, stream, samplingRate, format, channelMask, flags);
613
Eric Laurente83b55d2014-11-14 10:06:21 -0800614 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
615 stream, samplingRate,format, channelMask,
616 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700617}
618
Eric Laurente83b55d2014-11-14 10:06:21 -0800619status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
620 audio_io_handle_t *output,
621 audio_session_t session,
622 audio_stream_type_t *stream,
623 uint32_t samplingRate,
624 audio_format_t format,
625 audio_channel_mask_t channelMask,
626 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700627 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800628 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700629{
Eric Laurente83b55d2014-11-14 10:06:21 -0800630 audio_attributes_t attributes;
631 if (attr != NULL) {
632 if (!isValidAttributes(attr)) {
633 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
634 attr->usage, attr->content_type, attr->flags,
635 attr->tags);
636 return BAD_VALUE;
637 }
638 attributes = *attr;
639 } else {
640 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
641 ALOGE("getOutputForAttr(): invalid stream type");
642 return BAD_VALUE;
643 }
644 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700645 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700646 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100647 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
648 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
649 if (!audio_is_linear_pcm(format)) {
650 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800651 }
François Gaffie036e1e92015-03-19 10:16:24 +0100652 *stream = streamTypefromAttributesInt(&attributes);
653 *output = desc->mIoHandle;
654 ALOGV("getOutputForAttr() returns output %d", *output);
655 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800656 }
657 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
658 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
659 return BAD_VALUE;
660 }
661
Eric Laurent93c3d412014-08-01 14:48:35 -0700662 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
François Gaffie53615e22015-03-19 09:24:12 +0100663 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700664
Eric Laurente83b55d2014-11-14 10:06:21 -0800665 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700666 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700667
Eric Laurente83b55d2014-11-14 10:06:21 -0800668 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700669 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
670 }
671
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700672 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700673 device, samplingRate, format, channelMask, flags);
674
Eric Laurente83b55d2014-11-14 10:06:21 -0800675 *stream = streamTypefromAttributesInt(&attributes);
676 *output = getOutputForDevice(device, session, *stream,
677 samplingRate, format, channelMask,
678 flags, offloadInfo);
679 if (*output == AUDIO_IO_HANDLE_NONE) {
680 return INVALID_OPERATION;
681 }
Paul McLeanaa981192015-03-21 09:55:15 -0700682
683 // Explicit routing?
684 sp<DeviceDescriptor> deviceDesc;
685
686 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent322b4d22015-04-03 15:57:54 -0700687 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
Paul McLeanaa981192015-03-21 09:55:15 -0700688 deviceDesc = mAvailableOutputDevices[i];
689 break;
690 }
691 }
692 mOutputRoutes.addRoute(session, *stream, deviceDesc);
Eric Laurente83b55d2014-11-14 10:06:21 -0800693 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700694}
695
696audio_io_handle_t AudioPolicyManager::getOutputForDevice(
697 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800698 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700699 audio_stream_type_t stream,
700 uint32_t samplingRate,
701 audio_format_t format,
702 audio_channel_mask_t channelMask,
703 audio_output_flags_t flags,
704 const audio_offload_info_t *offloadInfo)
705{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700706 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700708 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700709
Eric Laurente552edb2014-03-10 17:42:56 -0700710#ifdef AUDIO_POLICY_TEST
711 if (mCurOutput != 0) {
712 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
713 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
714
715 if (mTestOutputs[mCurOutput] == 0) {
716 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700717 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
718 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700719 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700720 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700721 outputDesc->mFlags =
722 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700723 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700724 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
725 config.sample_rate = mTestSamplingRate;
726 config.channel_mask = mTestChannels;
727 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700728 if (offloadInfo != NULL) {
729 config.offload_info = *offloadInfo;
730 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700731 status = mpClientInterface->openOutput(0,
732 &mTestOutputs[mCurOutput],
733 &config,
734 &outputDesc->mDevice,
735 String8(""),
736 &outputDesc->mLatency,
737 outputDesc->mFlags);
738 if (status == NO_ERROR) {
739 outputDesc->mSamplingRate = config.sample_rate;
740 outputDesc->mFormat = config.format;
741 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700742 AudioParameter outputCmd = AudioParameter();
743 outputCmd.addInt(String8("set_id"),mCurOutput);
744 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
745 addOutput(mTestOutputs[mCurOutput], outputDesc);
746 }
747 }
748 return mTestOutputs[mCurOutput];
749 }
750#endif //AUDIO_POLICY_TEST
751
752 // open a direct output if required by specified parameters
753 //force direct flag if offload flag is set: offloading implies a direct output stream
754 // and all common behaviors are driven by checking only the direct flag
755 // this should normally be set appropriately in the policy configuration file
756 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700757 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700758 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700759 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
760 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
761 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800762 // only allow deep buffering for music stream type
763 if (stream != AUDIO_STREAM_MUSIC) {
764 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
765 }
Eric Laurente552edb2014-03-10 17:42:56 -0700766
Eric Laurentb732cf52014-09-24 19:08:21 -0700767 sp<IOProfile> profile;
768
769 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700770 // and not explicitly requested
771 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
772 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700773 audio_channel_count_from_out_mask(channelMask) <= 2) {
774 goto non_direct_output;
775 }
776
Eric Laurente552edb2014-03-10 17:42:56 -0700777 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
778 // creating an offloaded track and tearing it down immediately after start when audioflinger
779 // detects there is an active non offloadable effect.
780 // FIXME: We should check the audio session here but we do not have it in this context.
781 // This may prevent offloading in rare situations where effects are left active by apps
782 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700783
Eric Laurente552edb2014-03-10 17:42:56 -0700784 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100785 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700786 profile = getProfileForDirectOutput(device,
787 samplingRate,
788 format,
789 channelMask,
790 (audio_output_flags_t)flags);
791 }
792
Eric Laurent1c333e22014-05-20 10:48:17 -0700793 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700794 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700795
796 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700797 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700798 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
799 outputDesc = desc;
800 // reuse direct output if currently open and configured with same parameters
801 if ((samplingRate == outputDesc->mSamplingRate) &&
802 (format == outputDesc->mFormat) &&
803 (channelMask == outputDesc->mChannelMask)) {
804 outputDesc->mDirectOpenCount++;
805 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
806 return mOutputs.keyAt(i);
807 }
808 }
809 }
810 // close direct output if currently open and configured with different parameters
811 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700812 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700813 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700814 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700815 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700816 outputDesc->mLatency = 0;
817 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700818 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
819 config.sample_rate = samplingRate;
820 config.channel_mask = channelMask;
821 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700822 if (offloadInfo != NULL) {
823 config.offload_info = *offloadInfo;
824 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700825 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700826 &output,
827 &config,
828 &outputDesc->mDevice,
829 String8(""),
830 &outputDesc->mLatency,
831 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700832
833 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700834 if (status != NO_ERROR ||
835 (samplingRate != 0 && samplingRate != config.sample_rate) ||
836 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
837 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700838 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
839 "format %d %d, channelMask %04x %04x", output, samplingRate,
840 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
841 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700842 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700843 mpClientInterface->closeOutput(output);
844 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800845 // fall back to mixer output if possible when the direct output could not be open
846 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
847 goto non_direct_output;
848 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800849 // fall back to mixer output if possible when the direct output could not be open
850 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
851 goto non_direct_output;
852 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700853 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700854 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700855 outputDesc->mSamplingRate = config.sample_rate;
856 outputDesc->mChannelMask = config.channel_mask;
857 outputDesc->mFormat = config.format;
858 outputDesc->mRefCount[stream] = 0;
859 outputDesc->mStopTime[stream] = 0;
860 outputDesc->mDirectOpenCount = 1;
861
Eric Laurente552edb2014-03-10 17:42:56 -0700862 audio_io_handle_t srcOutput = getOutputForEffect();
863 addOutput(output, outputDesc);
864 audio_io_handle_t dstOutput = getOutputForEffect();
865 if (dstOutput == output) {
866 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
867 }
868 mPreviousOutputs = mOutputs;
869 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700870 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700871 return output;
872 }
873
Eric Laurentb732cf52014-09-24 19:08:21 -0700874non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700875 // ignoring channel mask due to downmix capability in mixer
876
877 // open a non direct output
878
879 // for non direct outputs, only PCM is supported
880 if (audio_is_linear_pcm(format)) {
881 // get which output is suitable for the specified stream. The actual
882 // routing change will happen when startOutput() will be called
883 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
884
Eric Laurent8838a382014-09-08 16:44:28 -0700885 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
886 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
887 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700888 }
889 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
890 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
891
Paul McLeanaa981192015-03-21 09:55:15 -0700892 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700893
894 return output;
895}
896
Eric Laurente0720872014-03-11 09:30:41 -0700897audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700898 audio_output_flags_t flags,
899 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700900{
901 // select one output among several that provide a path to a particular device or set of
902 // devices (the list was previously build by getOutputsForDevice()).
903 // The priority is as follows:
904 // 1: the output with the highest number of requested policy flags
905 // 2: the primary output
906 // 3: the first output in the list
907
908 if (outputs.size() == 0) {
909 return 0;
910 }
911 if (outputs.size() == 1) {
912 return outputs[0];
913 }
914
915 int maxCommonFlags = 0;
916 audio_io_handle_t outputFlags = 0;
917 audio_io_handle_t outputPrimary = 0;
918
919 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700920 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700921 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700922 // if a valid format is specified, skip output if not compatible
923 if (format != AUDIO_FORMAT_INVALID) {
924 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
925 if (format != outputDesc->mFormat) {
926 continue;
927 }
928 } else if (!audio_is_linear_pcm(format)) {
929 continue;
930 }
931 }
932
Eric Laurent3b73df72014-03-11 09:06:29 -0700933 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700934 if (commonFlags > maxCommonFlags) {
935 outputFlags = outputs[i];
936 maxCommonFlags = commonFlags;
937 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
938 }
939 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
940 outputPrimary = outputs[i];
941 }
942 }
943 }
944
945 if (outputFlags != 0) {
946 return outputFlags;
947 }
948 if (outputPrimary != 0) {
949 return outputPrimary;
950 }
951
952 return outputs[0];
953}
954
Eric Laurente0720872014-03-11 09:30:41 -0700955status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700956 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800957 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700958{
Paul McLeanaa981192015-03-21 09:55:15 -0700959 ALOGV("startOutput() output %d, stream %d, session %d",
960 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700961 ssize_t index = mOutputs.indexOfKey(output);
962 if (index < 0) {
963 ALOGW("startOutput() unknown output %d", output);
964 return BAD_VALUE;
965 }
966
Eric Laurentc75307b2015-03-17 15:29:32 -0700967 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
968
969 audio_devices_t newDevice;
970 if (outputDesc->mPolicyMix != NULL) {
971 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
972 } else {
973 newDevice = AUDIO_DEVICE_NONE;
974 }
975
976 uint32_t delayMs = 0;
977
978 // Routing?
979 mOutputRoutes.incRouteActivity(session);
980
981 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
982
983 if (status != NO_ERROR) {
984 mOutputRoutes.decRouteActivity(session);
985 }
986 // Automatically enable the remote submix input when output is started on a re routing mix
987 // of type MIX_TYPE_RECORDERS
988 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
989 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
990 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
991 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
992 outputDesc->mPolicyMix->mRegistrationId,
993 "remote-submix");
994 }
995
996 if (delayMs != 0) {
997 usleep(delayMs * 1000);
998 }
999
1000 return status;
1001}
1002
1003status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1004 audio_stream_type_t stream,
1005 audio_devices_t device,
1006 uint32_t *delayMs)
1007{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001008 // cannot start playback of STREAM_TTS if any other output is being used
1009 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001010
1011 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001012 if (stream == AUDIO_STREAM_TTS) {
1013 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001014 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001015 return INVALID_OPERATION;
1016 } else {
1017 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1018 }
1019 } else {
1020 // some playback other than beacon starts
1021 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1022 }
1023
Eric Laurente552edb2014-03-10 17:42:56 -07001024 // increment usage count for this stream on the requested output:
1025 // NOTE that the usage count is the same for duplicated output and hardware output which is
1026 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1027 outputDesc->changeRefCount(stream, 1);
1028
1029 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001030 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001031 if (device == AUDIO_DEVICE_NONE) {
1032 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001033 }
Eric Laurente552edb2014-03-10 17:42:56 -07001034 routing_strategy strategy = getStrategy(stream);
1035 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001036 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1037 (beaconMuteLatency > 0);
1038 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001039 bool force = false;
1040 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001041 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001042 if (desc != outputDesc) {
1043 // force a device change if any other output is managed by the same hw
1044 // module and has a current device selection that differs from selected device.
1045 // In this case, the audio HAL must receive the new device selection so that it can
1046 // change the device currently selected by the other active output.
1047 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001048 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001049 force = true;
1050 }
1051 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001052 // a notification so that audio focus effect can propagate, or that a mute/unmute
1053 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001054 uint32_t latency = desc->latency();
1055 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1056 waitMs = latency;
1057 }
1058 }
1059 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001060 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001061
1062 // handle special case for sonification while in call
1063 if (isInCall()) {
1064 handleIncallSonification(stream, true, false);
1065 }
1066
1067 // apply volume rules for current stream and device if necessary
1068 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001069 mStreams.valueFor(stream).getVolumeIndex(device),
1070 outputDesc,
1071 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001072
1073 // update the outputs if starting an output with a stream that can affect notification
1074 // routing
1075 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001076
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001077 // force reevaluating accessibility routing when ringtone or alarm starts
1078 if (strategy == STRATEGY_SONIFICATION) {
1079 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1080 }
Eric Laurente552edb2014-03-10 17:42:56 -07001081 }
1082 return NO_ERROR;
1083}
1084
1085
Eric Laurente0720872014-03-11 09:30:41 -07001086status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001087 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001088 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001089{
1090 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1091 ssize_t index = mOutputs.indexOfKey(output);
1092 if (index < 0) {
1093 ALOGW("stopOutput() unknown output %d", output);
1094 return BAD_VALUE;
1095 }
1096
Eric Laurentc75307b2015-03-17 15:29:32 -07001097 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001098
Eric Laurentc75307b2015-03-17 15:29:32 -07001099 if (outputDesc->mRefCount[stream] == 1) {
1100 // Automatically disable the remote submix input when output is stopped on a
1101 // re routing mix of type MIX_TYPE_RECORDERS
1102 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1103 outputDesc->mPolicyMix != NULL &&
1104 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1105 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1106 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1107 outputDesc->mPolicyMix->mRegistrationId,
1108 "remote-submix");
1109 }
1110 }
1111
1112 // Routing?
1113 if (outputDesc->mRefCount[stream] > 0) {
1114 mOutputRoutes.decRouteActivity(session);
1115 }
1116
1117 return stopSource(outputDesc, stream);
1118}
1119
1120status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
1121 audio_stream_type_t stream)
1122{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001123 // always handle stream stop, check which stream type is stopping
1124 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1125
Eric Laurente552edb2014-03-10 17:42:56 -07001126 // handle special case for sonification while in call
1127 if (isInCall()) {
1128 handleIncallSonification(stream, false, false);
1129 }
1130
1131 if (outputDesc->mRefCount[stream] > 0) {
1132 // decrement usage count of this stream on the output
1133 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001134
Eric Laurente552edb2014-03-10 17:42:56 -07001135 // store time at which the stream was stopped - see isStreamActive()
1136 if (outputDesc->mRefCount[stream] == 0) {
1137 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001138 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001139 // delay the device switch by twice the latency because stopOutput() is executed when
1140 // the track stop() command is received and at that time the audio track buffer can
1141 // still contain data that needs to be drained. The latency only covers the audio HAL
1142 // and kernel buffers. Also the latency does not always include additional delay in the
1143 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001144 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001145
1146 // force restoring the device selection on other active outputs if it differs from the
1147 // one being selected for this output
1148 for (size_t i = 0; i < mOutputs.size(); i++) {
1149 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001150 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001151 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001152 desc->isActive() &&
1153 outputDesc->sharesHwModuleWith(desc) &&
1154 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001155 setOutputDevice(desc,
1156 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001157 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001158 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001159 }
1160 }
1161 // update the outputs if stopping one with a stream that can affect notification routing
1162 handleNotificationRoutingForStream(stream);
1163 }
1164 return NO_ERROR;
1165 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001166 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001167 return INVALID_OPERATION;
1168 }
1169}
1170
Eric Laurente83b55d2014-11-14 10:06:21 -08001171void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001172 audio_stream_type_t stream __unused,
1173 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001174{
1175 ALOGV("releaseOutput() %d", output);
1176 ssize_t index = mOutputs.indexOfKey(output);
1177 if (index < 0) {
1178 ALOGW("releaseOutput() releasing unknown output %d", output);
1179 return;
1180 }
1181
1182#ifdef AUDIO_POLICY_TEST
1183 int testIndex = testOutputIndex(output);
1184 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001185 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001186 if (outputDesc->isActive()) {
1187 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001188 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001189 mTestOutputs[testIndex] = 0;
1190 }
1191 return;
1192 }
1193#endif //AUDIO_POLICY_TEST
1194
Paul McLeanaa981192015-03-21 09:55:15 -07001195 // Routing
1196 mOutputRoutes.removeRoute(session);
1197
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001199 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001200 if (desc->mDirectOpenCount <= 0) {
1201 ALOGW("releaseOutput() invalid open count %d for output %d",
1202 desc->mDirectOpenCount, output);
1203 return;
1204 }
1205 if (--desc->mDirectOpenCount == 0) {
1206 closeOutput(output);
1207 // If effects where present on the output, audioflinger moved them to the primary
1208 // output by default: move them back to the appropriate output.
1209 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001210 if (dstOutput != mPrimaryOutput->mIoHandle) {
1211 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1212 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001213 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001214 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001215 }
1216 }
1217}
1218
1219
Eric Laurentcaf7f482014-11-25 17:50:47 -08001220status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1221 audio_io_handle_t *input,
1222 audio_session_t session,
1223 uint32_t samplingRate,
1224 audio_format_t format,
1225 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001226 audio_input_flags_t flags,
1227 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001228{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001229 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1230 "session %d, flags %#x",
1231 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001232
Eric Laurentcaf7f482014-11-25 17:50:47 -08001233 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001234 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001235 audio_devices_t device;
1236 // handle legacy remote submix case where the address was not always specified
1237 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001238 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001239 audio_source_t inputSource = attr->source;
1240 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001241 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001242
Eric Laurentc447ded2015-01-06 08:47:05 -08001243 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1244 inputSource = AUDIO_SOURCE_MIC;
1245 }
1246 halInputSource = inputSource;
1247
1248 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001249 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001250 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001251 if (ret != NO_ERROR) {
1252 return ret;
1253 }
1254 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001255 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1256 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001257 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001258 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001260 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001261 return BAD_VALUE;
1262 }
Eric Laurentc722f302014-12-10 11:21:49 -08001263 if (policyMix != NULL) {
1264 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001265 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1266 // there is an external policy, but this input is attached to a mix of recorders,
1267 // meaning it receives audio injected into the framework, so the recorder doesn't
1268 // know about it and is therefore considered "legacy"
1269 *inputType = API_INPUT_LEGACY;
1270 } else {
1271 // recording a mix of players defined by an external policy, we're rerouting for
1272 // an external policy
1273 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1274 }
Eric Laurentc722f302014-12-10 11:21:49 -08001275 } else if (audio_is_remote_submix_device(device)) {
1276 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001277 *inputType = API_INPUT_MIX_CAPTURE;
1278 } else {
1279 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001280 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001281 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001282 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001283 case AUDIO_SOURCE_VOICE_UPLINK:
1284 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1285 break;
1286 case AUDIO_SOURCE_VOICE_DOWNLINK:
1287 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1288 break;
1289 case AUDIO_SOURCE_VOICE_CALL:
1290 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1291 break;
1292 default:
1293 break;
1294 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001295 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001296 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1297 if (index >= 0) {
1298 *input = mSoundTriggerSessions.valueFor(session);
1299 isSoundTrigger = true;
1300 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1301 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1302 } else {
1303 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1304 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001305 }
1306 }
1307
Andy Hungf129b032015-04-07 13:45:50 -07001308 // find a compatible input profile (not necessarily identical in parameters)
1309 sp<IOProfile> profile;
1310 // samplingRate and flags may be updated by getInputProfile
1311 uint32_t profileSamplingRate = samplingRate;
1312 audio_format_t profileFormat = format;
1313 audio_channel_mask_t profileChannelMask = channelMask;
1314 audio_input_flags_t profileFlags = flags;
1315 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001316 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001317 profileSamplingRate, profileFormat, profileChannelMask,
1318 profileFlags);
1319 if (profile != 0) {
1320 break; // success
1321 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1322 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1323 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001324 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1325 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001326 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001327 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001328 }
Eric Laurente552edb2014-03-10 17:42:56 -07001329 }
1330
Eric Laurent322b4d22015-04-03 15:57:54 -07001331 if (profile->getModuleHandle() == 0) {
1332 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001333 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001334 }
1335
1336 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001337 config.sample_rate = profileSamplingRate;
1338 config.channel_mask = profileChannelMask;
1339 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001340
Eric Laurent322b4d22015-04-03 15:57:54 -07001341 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001342 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001343 &config,
1344 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001345 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001346 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001347 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001348
1349 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001350 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001351 (profileSamplingRate != config.sample_rate) ||
1352 (profileFormat != config.format) ||
1353 (profileChannelMask != config.channel_mask)) {
1354 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1355 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001356 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001357 if (*input != AUDIO_IO_HANDLE_NONE) {
1358 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001359 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001360 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001361 }
1362
Eric Laurent1f2f2232014-06-02 12:01:23 -07001363 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001364 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001365 inputDesc->mRefCount = 0;
1366 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001367 inputDesc->mSamplingRate = profileSamplingRate;
1368 inputDesc->mFormat = profileFormat;
1369 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001370 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001371 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001372 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001373 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001374
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001375 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001376
Eric Laurentcaf7f482014-11-25 17:50:47 -08001377 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001378 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001379 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001380}
1381
Eric Laurent4dc68062014-07-28 17:26:49 -07001382status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1383 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001384{
1385 ALOGV("startInput() input %d", input);
1386 ssize_t index = mInputs.indexOfKey(input);
1387 if (index < 0) {
1388 ALOGW("startInput() unknown input %d", input);
1389 return BAD_VALUE;
1390 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001391 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001392
Eric Laurentc722f302014-12-10 11:21:49 -08001393 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001394 if (index < 0) {
1395 ALOGW("startInput() unknown session %d on input %d", session, input);
1396 return BAD_VALUE;
1397 }
1398
Glenn Kasten74a8e252014-07-24 14:09:55 -07001399 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001400 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001401
1402 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001403 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001404 if (activeInput != 0 && activeInput != input) {
1405
1406 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1407 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001408 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001409 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001410 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001411 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1412 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001413 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001414 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001415 return INVALID_OPERATION;
1416 }
1417 }
1418 }
1419
Glenn Kasten74a8e252014-07-24 14:09:55 -07001420 if (inputDesc->mRefCount == 0) {
François Gaffie53615e22015-03-19 09:24:12 +01001421 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001422 SoundTrigger::setCaptureState(true);
1423 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001424 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001425
Eric Laurentc722f302014-12-10 11:21:49 -08001426 // automatically enable the remote submix output when input is started if not
1427 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001428 // For remote submix (a virtual device), we open only one input per capture request.
1429 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001430 String8 address = String8("");
1431 if (inputDesc->mPolicyMix == NULL) {
1432 address = String8("0");
1433 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1434 address = inputDesc->mPolicyMix->mRegistrationId;
1435 }
1436 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001437 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001438 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001439 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001440 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001441 }
Eric Laurente552edb2014-03-10 17:42:56 -07001442 }
1443
Eric Laurente552edb2014-03-10 17:42:56 -07001444 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1445
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001446 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001447 return NO_ERROR;
1448}
1449
Eric Laurent4dc68062014-07-28 17:26:49 -07001450status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1451 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001452{
1453 ALOGV("stopInput() input %d", input);
1454 ssize_t index = mInputs.indexOfKey(input);
1455 if (index < 0) {
1456 ALOGW("stopInput() unknown input %d", input);
1457 return BAD_VALUE;
1458 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001459 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001460
Eric Laurentc722f302014-12-10 11:21:49 -08001461 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001462 if (index < 0) {
1463 ALOGW("stopInput() unknown session %d on input %d", session, input);
1464 return BAD_VALUE;
1465 }
1466
Eric Laurente552edb2014-03-10 17:42:56 -07001467 if (inputDesc->mRefCount == 0) {
1468 ALOGW("stopInput() input %d already stopped", input);
1469 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001470 }
1471
1472 inputDesc->mRefCount--;
1473 if (inputDesc->mRefCount == 0) {
1474
Eric Laurentc722f302014-12-10 11:21:49 -08001475 // automatically disable the remote submix output when input is stopped if not
1476 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001477 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001478 String8 address = String8("");
1479 if (inputDesc->mPolicyMix == NULL) {
1480 address = String8("0");
1481 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1482 address = inputDesc->mPolicyMix->mRegistrationId;
1483 }
1484 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001485 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001486 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001487 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001488 }
Eric Laurente552edb2014-03-10 17:42:56 -07001489 }
1490
Eric Laurent1c333e22014-05-20 10:48:17 -07001491 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001492
François Gaffie53615e22015-03-19 09:24:12 +01001493 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001494 SoundTrigger::setCaptureState(false);
1495 }
Eric Laurente552edb2014-03-10 17:42:56 -07001496 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001497 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001498}
1499
Eric Laurent4dc68062014-07-28 17:26:49 -07001500void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1501 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001502{
1503 ALOGV("releaseInput() %d", input);
1504 ssize_t index = mInputs.indexOfKey(input);
1505 if (index < 0) {
1506 ALOGW("releaseInput() releasing unknown input %d", input);
1507 return;
1508 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001509 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1510 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001511
Eric Laurentc722f302014-12-10 11:21:49 -08001512 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001513 if (index < 0) {
1514 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1515 return;
1516 }
Eric Laurentc722f302014-12-10 11:21:49 -08001517 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001518 if (inputDesc->mOpenRefCount == 0) {
1519 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1520 return;
1521 }
1522 inputDesc->mOpenRefCount--;
1523 if (inputDesc->mOpenRefCount > 0) {
1524 ALOGV("releaseInput() exit > 0");
1525 return;
1526 }
1527
Eric Laurent05b90f82014-08-27 15:32:29 -07001528 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001529 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001530 ALOGV("releaseInput() exit");
1531}
1532
Eric Laurentd4692962014-05-05 18:13:44 -07001533void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001534 bool patchRemoved = false;
1535
Eric Laurentd4692962014-05-05 18:13:44 -07001536 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001537 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1538 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1539 if (patch_index >= 0) {
1540 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1541 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1542 mAudioPatches.removeItemsAt(patch_index);
1543 patchRemoved = true;
1544 }
Eric Laurentd4692962014-05-05 18:13:44 -07001545 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1546 }
1547 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001548 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001549
1550 if (patchRemoved) {
1551 mpClientInterface->onAudioPatchListUpdate();
1552 }
Eric Laurentd4692962014-05-05 18:13:44 -07001553}
1554
Eric Laurente0720872014-03-11 09:30:41 -07001555void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001556 int indexMin,
1557 int indexMax)
1558{
1559 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001560 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001561 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1562 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001563 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001564 }
Eric Laurente552edb2014-03-10 17:42:56 -07001565}
1566
Eric Laurente0720872014-03-11 09:30:41 -07001567status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001568 int index,
1569 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001570{
1571
Eric Laurentc75307b2015-03-17 15:29:32 -07001572 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1573 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001574 return BAD_VALUE;
1575 }
1576 if (!audio_is_output_device(device)) {
1577 return BAD_VALUE;
1578 }
1579
1580 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001581 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001582
1583 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1584 stream, device, index);
1585
1586 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1587 // clear all device specific values
1588 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001589 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001590 }
François Gaffiedfd74092015-03-19 12:10:59 +01001591 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001592
Eric Laurent31551f82014-10-10 18:21:56 -07001593 // update volume on all outputs whose current device is also selected by the same
1594 // strategy as the device specified by the caller
1595 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001596
1597
1598 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1599 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1600 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001601 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001602 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1603 }
1604 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1605 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001606 return NO_ERROR;
1607 }
Eric Laurente552edb2014-03-10 17:42:56 -07001608 status_t status = NO_ERROR;
1609 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001610 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1611 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001612 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001613 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001614 if (volStatus != NO_ERROR) {
1615 status = volStatus;
1616 }
1617 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001618 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1619 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001620 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001621 }
Eric Laurente552edb2014-03-10 17:42:56 -07001622 }
1623 return status;
1624}
1625
Eric Laurente0720872014-03-11 09:30:41 -07001626status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001627 int *index,
1628 audio_devices_t device)
1629{
1630 if (index == NULL) {
1631 return BAD_VALUE;
1632 }
1633 if (!audio_is_output_device(device)) {
1634 return BAD_VALUE;
1635 }
1636 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1637 // the strategy the stream belongs to.
1638 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1639 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1640 }
François Gaffiedfd74092015-03-19 12:10:59 +01001641 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001642
Eric Laurentc75307b2015-03-17 15:29:32 -07001643 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001644 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1645 return NO_ERROR;
1646}
1647
Eric Laurente0720872014-03-11 09:30:41 -07001648audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001649 const SortedVector<audio_io_handle_t>& outputs)
1650{
1651 // select one output among several suitable for global effects.
1652 // The priority is as follows:
1653 // 1: An offloaded output. If the effect ends up not being offloadable,
1654 // AudioFlinger will invalidate the track and the offloaded output
1655 // will be closed causing the effect to be moved to a PCM output.
1656 // 2: A deep buffer output
1657 // 3: the first output in the list
1658
1659 if (outputs.size() == 0) {
1660 return 0;
1661 }
1662
1663 audio_io_handle_t outputOffloaded = 0;
1664 audio_io_handle_t outputDeepBuffer = 0;
1665
1666 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001667 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001668 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001669 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1670 outputOffloaded = outputs[i];
1671 }
1672 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1673 outputDeepBuffer = outputs[i];
1674 }
1675 }
1676
1677 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1678 outputOffloaded, outputDeepBuffer);
1679 if (outputOffloaded != 0) {
1680 return outputOffloaded;
1681 }
1682 if (outputDeepBuffer != 0) {
1683 return outputDeepBuffer;
1684 }
1685
1686 return outputs[0];
1687}
1688
Eric Laurente0720872014-03-11 09:30:41 -07001689audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001690{
1691 // apply simple rule where global effects are attached to the same output as MUSIC streams
1692
Eric Laurent3b73df72014-03-11 09:06:29 -07001693 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001694 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1695 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1696
1697 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1698 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1699 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1700
1701 return output;
1702}
1703
Eric Laurente0720872014-03-11 09:30:41 -07001704status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001705 audio_io_handle_t io,
1706 uint32_t strategy,
1707 int session,
1708 int id)
1709{
1710 ssize_t index = mOutputs.indexOfKey(io);
1711 if (index < 0) {
1712 index = mInputs.indexOfKey(io);
1713 if (index < 0) {
1714 ALOGW("registerEffect() unknown io %d", io);
1715 return INVALID_OPERATION;
1716 }
1717 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001718 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001719}
1720
Eric Laurentc75307b2015-03-17 15:29:32 -07001721bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1722{
1723 return mOutputs.isStreamActive(stream, inPastMs);
1724}
1725
1726bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1727{
1728 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1729}
1730
Eric Laurente0720872014-03-11 09:30:41 -07001731bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001732{
1733 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001734 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001735 if (inputDescriptor->mRefCount == 0) {
1736 continue;
1737 }
1738 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001739 return true;
1740 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001741 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1742 // corresponds to an active capture triggered by a hardware hotword recognition
1743 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1744 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1745 // FIXME: we should not assume that the first session is the active one and keep
1746 // activity count per session. Same in startInput().
1747 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1748 if (index >= 0) {
1749 return true;
1750 }
1751 }
Eric Laurente552edb2014-03-10 17:42:56 -07001752 }
1753 return false;
1754}
1755
Eric Laurent275e8e92014-11-30 15:14:47 -08001756// Register a list of custom mixes with their attributes and format.
1757// When a mix is registered, corresponding input and output profiles are
1758// added to the remote submix hw module. The profile contains only the
1759// parameters (sampling rate, format...) specified by the mix.
1760// The corresponding input remote submix device is also connected.
1761//
1762// When a remote submix device is connected, the address is checked to select the
1763// appropriate profile and the corresponding input or output stream is opened.
1764//
1765// When capture starts, getInputForAttr() will:
1766// - 1 look for a mix matching the address passed in attribtutes tags if any
1767// - 2 if none found, getDeviceForInputSource() will:
1768// - 2.1 look for a mix matching the attributes source
1769// - 2.2 if none found, default to device selection by policy rules
1770// At this time, the corresponding output remote submix device is also connected
1771// and active playback use cases can be transferred to this mix if needed when reconnecting
1772// after AudioTracks are invalidated
1773//
1774// When playback starts, getOutputForAttr() will:
1775// - 1 look for a mix matching the address passed in attribtutes tags if any
1776// - 2 if none found, look for a mix matching the attributes usage
1777// - 3 if none found, default to device and output selection by policy rules.
1778
1779status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1780{
1781 sp<HwModule> module;
1782 for (size_t i = 0; i < mHwModules.size(); i++) {
1783 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1784 mHwModules[i]->mHandle != 0) {
1785 module = mHwModules[i];
1786 break;
1787 }
1788 }
1789
1790 if (module == 0) {
1791 return INVALID_OPERATION;
1792 }
1793
1794 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1795
1796 for (size_t i = 0; i < mixes.size(); i++) {
1797 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001798
1799 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001800 continue;
1801 }
1802 audio_config_t outputConfig = mixes[i].mFormat;
1803 audio_config_t inputConfig = mixes[i].mFormat;
1804 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1805 // stereo and let audio flinger do the channel conversion if needed.
1806 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1807 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1808 module->addOutputProfile(address, &outputConfig,
1809 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1810 module->addInputProfile(address, &inputConfig,
1811 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001812
Eric Laurentc722f302014-12-10 11:21:49 -08001813 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001814 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001815 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001816 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001817 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001818 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001819 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001820 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001821 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001822 }
1823 return NO_ERROR;
1824}
1825
1826status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1827{
1828 sp<HwModule> module;
1829 for (size_t i = 0; i < mHwModules.size(); i++) {
1830 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1831 mHwModules[i]->mHandle != 0) {
1832 module = mHwModules[i];
1833 break;
1834 }
1835 }
1836
1837 if (module == 0) {
1838 return INVALID_OPERATION;
1839 }
1840
1841 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1842
1843 for (size_t i = 0; i < mixes.size(); i++) {
1844 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001845
1846 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001847 continue;
1848 }
1849
Eric Laurentc722f302014-12-10 11:21:49 -08001850 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1851 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1852 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001853 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001854 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001855 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001856 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001857
1858 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1859 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1860 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001861 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001862 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001863 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001864 }
1865 module->removeOutputProfile(address);
1866 module->removeInputProfile(address);
1867 }
1868 return NO_ERROR;
1869}
1870
Eric Laurente552edb2014-03-10 17:42:56 -07001871
Eric Laurente0720872014-03-11 09:30:41 -07001872status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001873{
1874 const size_t SIZE = 256;
1875 char buffer[SIZE];
1876 String8 result;
1877
1878 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1879 result.append(buffer);
1880
Eric Laurentc75307b2015-03-17 15:29:32 -07001881 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001882 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001883 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001884 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001885 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001886 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001887 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001888 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001889 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001890 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001891 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001892 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001893 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001894 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001895 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001896 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001897 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001898 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001899 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001900
François Gaffie53615e22015-03-19 09:24:12 +01001901 mAvailableOutputDevices.dump(fd, String8("output"));
1902 mAvailableInputDevices.dump(fd, String8("input"));
1903 mHwModules.dump(fd);
1904 mOutputs.dump(fd);
1905 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001906 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001907 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001908 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001909
1910 return NO_ERROR;
1911}
1912
1913// This function checks for the parameters which can be offloaded.
1914// This can be enhanced depending on the capability of the DSP and policy
1915// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001916bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001917{
1918 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001919 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001920 offloadInfo.sample_rate, offloadInfo.channel_mask,
1921 offloadInfo.format,
1922 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1923 offloadInfo.has_video);
1924
1925 // Check if offload has been disabled
1926 char propValue[PROPERTY_VALUE_MAX];
1927 if (property_get("audio.offload.disable", propValue, "0")) {
1928 if (atoi(propValue) != 0) {
1929 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1930 return false;
1931 }
1932 }
1933
1934 // Check if stream type is music, then only allow offload as of now.
1935 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1936 {
1937 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1938 return false;
1939 }
1940
1941 //TODO: enable audio offloading with video when ready
1942 if (offloadInfo.has_video)
1943 {
1944 ALOGV("isOffloadSupported: has_video == true, returning false");
1945 return false;
1946 }
1947
1948 //If duration is less than minimum value defined in property, return false
1949 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1950 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1951 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1952 return false;
1953 }
1954 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1955 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1956 return false;
1957 }
1958
1959 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1960 // creating an offloaded track and tearing it down immediately after start when audioflinger
1961 // detects there is an active non offloadable effect.
1962 // FIXME: We should check the audio session here but we do not have it in this context.
1963 // This may prevent offloading in rare situations where effects are left active by apps
1964 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01001965 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001966 return false;
1967 }
1968
1969 // See if there is a profile to support this.
1970 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001971 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001972 offloadInfo.sample_rate,
1973 offloadInfo.format,
1974 offloadInfo.channel_mask,
1975 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001976 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1977 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001978}
1979
Eric Laurent6a94d692014-05-20 11:18:06 -07001980status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1981 audio_port_type_t type,
1982 unsigned int *num_ports,
1983 struct audio_port *ports,
1984 unsigned int *generation)
1985{
1986 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1987 generation == NULL) {
1988 return BAD_VALUE;
1989 }
1990 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1991 if (ports == NULL) {
1992 *num_ports = 0;
1993 }
1994
1995 size_t portsWritten = 0;
1996 size_t portsMax = *num_ports;
1997 *num_ports = 0;
1998 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1999 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2000 for (size_t i = 0;
2001 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2002 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2003 }
2004 *num_ports += mAvailableOutputDevices.size();
2005 }
2006 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2007 for (size_t i = 0;
2008 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2009 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2010 }
2011 *num_ports += mAvailableInputDevices.size();
2012 }
2013 }
2014 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2015 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2016 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2017 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2018 }
2019 *num_ports += mInputs.size();
2020 }
2021 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002022 size_t numOutputs = 0;
2023 for (size_t i = 0; i < mOutputs.size(); i++) {
2024 if (!mOutputs[i]->isDuplicated()) {
2025 numOutputs++;
2026 if (portsWritten < portsMax) {
2027 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2028 }
2029 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002030 }
Eric Laurent84c70242014-06-23 08:46:27 -07002031 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002032 }
2033 }
2034 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002035 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002036 return NO_ERROR;
2037}
2038
2039status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2040{
2041 return NO_ERROR;
2042}
2043
Eric Laurent6a94d692014-05-20 11:18:06 -07002044status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2045 audio_patch_handle_t *handle,
2046 uid_t uid)
2047{
2048 ALOGV("createAudioPatch()");
2049
2050 if (handle == NULL || patch == NULL) {
2051 return BAD_VALUE;
2052 }
2053 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2054
Eric Laurent874c42872014-08-08 15:13:39 -07002055 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2056 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2057 return BAD_VALUE;
2058 }
2059 // only one source per audio patch supported for now
2060 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002061 return INVALID_OPERATION;
2062 }
Eric Laurent874c42872014-08-08 15:13:39 -07002063
2064 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002065 return INVALID_OPERATION;
2066 }
Eric Laurent874c42872014-08-08 15:13:39 -07002067 for (size_t i = 0; i < patch->num_sinks; i++) {
2068 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2069 return INVALID_OPERATION;
2070 }
2071 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002072
2073 sp<AudioPatch> patchDesc;
2074 ssize_t index = mAudioPatches.indexOfKey(*handle);
2075
Eric Laurent6a94d692014-05-20 11:18:06 -07002076 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2077 patch->sources[0].role,
2078 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002079#if LOG_NDEBUG == 0
2080 for (size_t i = 0; i < patch->num_sinks; i++) {
2081 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2082 patch->sinks[i].role,
2083 patch->sinks[i].type);
2084 }
2085#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002086
2087 if (index >= 0) {
2088 patchDesc = mAudioPatches.valueAt(index);
2089 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2090 mUidCached, patchDesc->mUid, uid);
2091 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2092 return INVALID_OPERATION;
2093 }
2094 } else {
2095 *handle = 0;
2096 }
2097
2098 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002099 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002100 if (outputDesc == NULL) {
2101 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2102 return BAD_VALUE;
2103 }
Eric Laurent84c70242014-06-23 08:46:27 -07002104 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2105 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002106 if (patchDesc != 0) {
2107 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2108 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2109 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2110 return BAD_VALUE;
2111 }
2112 }
Eric Laurent874c42872014-08-08 15:13:39 -07002113 DeviceVector devices;
2114 for (size_t i = 0; i < patch->num_sinks; i++) {
2115 // Only support mix to devices connection
2116 // TODO add support for mix to mix connection
2117 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2118 ALOGV("createAudioPatch() source mix but sink is not a device");
2119 return INVALID_OPERATION;
2120 }
2121 sp<DeviceDescriptor> devDesc =
2122 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2123 if (devDesc == 0) {
2124 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2125 return BAD_VALUE;
2126 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002127
François Gaffie53615e22015-03-19 09:24:12 +01002128 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002129 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002130 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002131 NULL, // updatedSamplingRate
2132 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002133 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002134 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002135 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002136 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002137 ALOGV("createAudioPatch() profile not supported for device %08x",
2138 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002139 return INVALID_OPERATION;
2140 }
2141 devices.add(devDesc);
2142 }
2143 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002144 return INVALID_OPERATION;
2145 }
Eric Laurent874c42872014-08-08 15:13:39 -07002146
Eric Laurent6a94d692014-05-20 11:18:06 -07002147 // TODO: reconfigure output format and channels here
2148 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002149 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002150 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002151 index = mAudioPatches.indexOfKey(*handle);
2152 if (index >= 0) {
2153 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2154 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2155 }
2156 patchDesc = mAudioPatches.valueAt(index);
2157 patchDesc->mUid = uid;
2158 ALOGV("createAudioPatch() success");
2159 } else {
2160 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2161 return INVALID_OPERATION;
2162 }
2163 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2164 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2165 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002166 // only one sink supported when connecting an input device to a mix
2167 if (patch->num_sinks > 1) {
2168 return INVALID_OPERATION;
2169 }
François Gaffie53615e22015-03-19 09:24:12 +01002170 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002171 if (inputDesc == NULL) {
2172 return BAD_VALUE;
2173 }
2174 if (patchDesc != 0) {
2175 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2176 return BAD_VALUE;
2177 }
2178 }
2179 sp<DeviceDescriptor> devDesc =
2180 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2181 if (devDesc == 0) {
2182 return BAD_VALUE;
2183 }
2184
François Gaffie53615e22015-03-19 09:24:12 +01002185 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002186 devDesc->mAddress,
2187 patch->sinks[0].sample_rate,
2188 NULL, /*updatedSampleRate*/
2189 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002190 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002191 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002192 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002193 // FIXME for the parameter type,
2194 // and the NONE
2195 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002196 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002197 return INVALID_OPERATION;
2198 }
2199 // TODO: reconfigure output format and channels here
2200 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002201 devDesc->type(), inputDesc->mIoHandle);
2202 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002203 index = mAudioPatches.indexOfKey(*handle);
2204 if (index >= 0) {
2205 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2206 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2207 }
2208 patchDesc = mAudioPatches.valueAt(index);
2209 patchDesc->mUid = uid;
2210 ALOGV("createAudioPatch() success");
2211 } else {
2212 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2213 return INVALID_OPERATION;
2214 }
2215 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2216 // device to device connection
2217 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002218 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002219 return BAD_VALUE;
2220 }
2221 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002222 sp<DeviceDescriptor> srcDeviceDesc =
2223 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002224 if (srcDeviceDesc == 0) {
2225 return BAD_VALUE;
2226 }
Eric Laurent874c42872014-08-08 15:13:39 -07002227
Eric Laurent6a94d692014-05-20 11:18:06 -07002228 //update source and sink with our own data as the data passed in the patch may
2229 // be incomplete.
2230 struct audio_patch newPatch = *patch;
2231 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002232
Eric Laurent874c42872014-08-08 15:13:39 -07002233 for (size_t i = 0; i < patch->num_sinks; i++) {
2234 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2235 ALOGV("createAudioPatch() source device but one sink is not a device");
2236 return INVALID_OPERATION;
2237 }
2238
2239 sp<DeviceDescriptor> sinkDeviceDesc =
2240 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2241 if (sinkDeviceDesc == 0) {
2242 return BAD_VALUE;
2243 }
2244 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2245
Eric Laurent3bcf8592015-04-03 12:13:24 -07002246 // create a software bridge in PatchPanel if:
2247 // - source and sink devices are on differnt HW modules OR
2248 // - audio HAL version is < 3.0
2249 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2250 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2251 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002252 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002253 return INVALID_OPERATION;
2254 }
Eric Laurent874c42872014-08-08 15:13:39 -07002255 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002256 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002257 // if the sink device is reachable via an opened output stream, request to go via
2258 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002259 audio_io_handle_t output = selectOutput(outputs,
2260 AUDIO_OUTPUT_FLAG_NONE,
2261 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002262 if (output != AUDIO_IO_HANDLE_NONE) {
2263 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2264 if (outputDesc->isDuplicated()) {
2265 return INVALID_OPERATION;
2266 }
2267 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002268 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002269 newPatch.num_sources = 2;
2270 }
Eric Laurent83b88082014-06-20 18:31:16 -07002271 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002272 }
2273 // TODO: check from routing capabilities in config file and other conflicting patches
2274
2275 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2276 if (index >= 0) {
2277 afPatchHandle = patchDesc->mAfPatchHandle;
2278 }
2279
2280 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2281 &afPatchHandle,
2282 0);
2283 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2284 status, afPatchHandle);
2285 if (status == NO_ERROR) {
2286 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002287 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002288 addAudioPatch(patchDesc->mHandle, patchDesc);
2289 } else {
2290 patchDesc->mPatch = newPatch;
2291 }
2292 patchDesc->mAfPatchHandle = afPatchHandle;
2293 *handle = patchDesc->mHandle;
2294 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002295 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002296 } else {
2297 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2298 status);
2299 return INVALID_OPERATION;
2300 }
2301 } else {
2302 return BAD_VALUE;
2303 }
2304 } else {
2305 return BAD_VALUE;
2306 }
2307 return NO_ERROR;
2308}
2309
2310status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2311 uid_t uid)
2312{
2313 ALOGV("releaseAudioPatch() patch %d", handle);
2314
2315 ssize_t index = mAudioPatches.indexOfKey(handle);
2316
2317 if (index < 0) {
2318 return BAD_VALUE;
2319 }
2320 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2321 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2322 mUidCached, patchDesc->mUid, uid);
2323 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2324 return INVALID_OPERATION;
2325 }
2326
2327 struct audio_patch *patch = &patchDesc->mPatch;
2328 patchDesc->mUid = mUidCached;
2329 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002330 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002331 if (outputDesc == NULL) {
2332 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2333 return BAD_VALUE;
2334 }
2335
Eric Laurentc75307b2015-03-17 15:29:32 -07002336 setOutputDevice(outputDesc,
2337 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002338 true,
2339 0,
2340 NULL);
2341 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2342 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002343 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002344 if (inputDesc == NULL) {
2345 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2346 return BAD_VALUE;
2347 }
2348 setInputDevice(inputDesc->mIoHandle,
2349 getNewInputDevice(inputDesc->mIoHandle),
2350 true,
2351 NULL);
2352 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2353 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2354 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2355 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2356 status, patchDesc->mAfPatchHandle);
2357 removeAudioPatch(patchDesc->mHandle);
2358 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002359 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002360 } else {
2361 return BAD_VALUE;
2362 }
2363 } else {
2364 return BAD_VALUE;
2365 }
2366 return NO_ERROR;
2367}
2368
2369status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2370 struct audio_patch *patches,
2371 unsigned int *generation)
2372{
François Gaffie53615e22015-03-19 09:24:12 +01002373 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002374 return BAD_VALUE;
2375 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002376 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002377 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002378}
2379
Eric Laurente1715a42014-05-20 11:30:42 -07002380status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002381{
Eric Laurente1715a42014-05-20 11:30:42 -07002382 ALOGV("setAudioPortConfig()");
2383
2384 if (config == NULL) {
2385 return BAD_VALUE;
2386 }
2387 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2388 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002389 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2390 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002391 }
2392
Eric Laurenta121f902014-06-03 13:32:54 -07002393 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002394 if (config->type == AUDIO_PORT_TYPE_MIX) {
2395 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002396 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002397 if (outputDesc == NULL) {
2398 return BAD_VALUE;
2399 }
Eric Laurent84c70242014-06-23 08:46:27 -07002400 ALOG_ASSERT(!outputDesc->isDuplicated(),
2401 "setAudioPortConfig() called on duplicated output %d",
2402 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002403 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002404 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002405 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002406 if (inputDesc == NULL) {
2407 return BAD_VALUE;
2408 }
Eric Laurenta121f902014-06-03 13:32:54 -07002409 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002410 } else {
2411 return BAD_VALUE;
2412 }
2413 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2414 sp<DeviceDescriptor> deviceDesc;
2415 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2416 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2417 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2418 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2419 } else {
2420 return BAD_VALUE;
2421 }
2422 if (deviceDesc == NULL) {
2423 return BAD_VALUE;
2424 }
Eric Laurenta121f902014-06-03 13:32:54 -07002425 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002426 } else {
2427 return BAD_VALUE;
2428 }
2429
Eric Laurenta121f902014-06-03 13:32:54 -07002430 struct audio_port_config backupConfig;
2431 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2432 if (status == NO_ERROR) {
2433 struct audio_port_config newConfig;
2434 audioPortConfig->toAudioPortConfig(&newConfig, config);
2435 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002436 }
Eric Laurenta121f902014-06-03 13:32:54 -07002437 if (status != NO_ERROR) {
2438 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002439 }
Eric Laurente1715a42014-05-20 11:30:42 -07002440
2441 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002442}
2443
2444void AudioPolicyManager::clearAudioPatches(uid_t uid)
2445{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002446 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002447 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2448 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002449 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002450 }
2451 }
2452}
2453
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002454status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2455 audio_io_handle_t *ioHandle,
2456 audio_devices_t *device)
2457{
2458 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2459 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002460 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002461
François Gaffiedf372692015-03-19 10:43:27 +01002462 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002463}
2464
Eric Laurent554a2772015-04-10 11:29:24 -07002465status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source,
2466 const audio_attributes_t *attributes,
2467 audio_io_handle_t *handle)
2468{
2469 return INVALID_OPERATION;
2470}
2471
2472status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle)
2473{
2474 return INVALID_OPERATION;
2475}
2476
Eric Laurente552edb2014-03-10 17:42:56 -07002477// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002478// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002479// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002480uint32_t AudioPolicyManager::nextAudioPortGeneration()
2481{
2482 return android_atomic_inc(&mAudioPortGeneration);
2483}
2484
Eric Laurente0720872014-03-11 09:30:41 -07002485AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002486 :
2487#ifdef AUDIO_POLICY_TEST
2488 Thread(false),
2489#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002490 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002491 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002492 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002493 mAudioPortGeneration(1),
2494 mBeaconMuteRefCount(0),
2495 mBeaconPlayingRefCount(0),
2496 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002497{
François Gaffie2110e042015-03-24 08:41:51 +01002498 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2499 if (!engineInstance) {
2500 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2501 return;
2502 }
2503 // Retrieve the Policy Manager Interface
2504 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2505 if (mEngine == NULL) {
2506 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2507 return;
2508 }
2509 mEngine->setObserver(this);
2510 status_t status = mEngine->initCheck();
2511 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2512
Eric Laurent6a94d692014-05-20 11:18:06 -07002513 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002514 mpClientInterface = clientInterface;
2515
Paul McLeane743a472015-01-28 11:07:31 -08002516 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002517 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2518 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2519 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2520 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2521 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2522 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002523 ALOGE("could not load audio policy configuration file, setting defaults");
2524 defaultAudioPolicyConfig();
2525 }
2526 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002527 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002528
François Gaffie2110e042015-03-24 08:41:51 +01002529 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2530 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002531
2532 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002533 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2534 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002535 for (size_t i = 0; i < mHwModules.size(); i++) {
2536 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2537 if (mHwModules[i]->mHandle == 0) {
2538 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2539 continue;
2540 }
2541 // open all output streams needed to access attached devices
2542 // except for direct output streams that are only opened when they are actually
2543 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002544 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002545 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2546 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002547 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002548
Eric Laurent3a4311c2014-03-17 12:00:47 -07002549 if (outProfile->mSupportedDevices.isEmpty()) {
2550 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2551 continue;
2552 }
2553
Eric Laurentd78f1532014-09-16 16:38:20 -07002554 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2555 continue;
2556 }
Eric Laurent83b88082014-06-20 18:31:16 -07002557 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002558 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2559 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002560 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002561 // chose first device present in mSupportedDevices also part of
2562 // outputDeviceTypes
2563 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002564 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002565 if ((profileType & outputDeviceTypes) != 0) {
2566 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002567 }
Eric Laurente552edb2014-03-10 17:42:56 -07002568 }
2569 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002570 if ((profileType & outputDeviceTypes) == 0) {
2571 continue;
2572 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002573 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2574 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002575
2576 outputDesc->mDevice = profileType;
2577 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2578 config.sample_rate = outputDesc->mSamplingRate;
2579 config.channel_mask = outputDesc->mChannelMask;
2580 config.format = outputDesc->mFormat;
2581 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002582 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002583 &output,
2584 &config,
2585 &outputDesc->mDevice,
2586 String8(""),
2587 &outputDesc->mLatency,
2588 outputDesc->mFlags);
2589
2590 if (status != NO_ERROR) {
2591 ALOGW("Cannot open output stream for device %08x on hw module %s",
2592 outputDesc->mDevice,
2593 mHwModules[i]->mName);
2594 } else {
2595 outputDesc->mSamplingRate = config.sample_rate;
2596 outputDesc->mChannelMask = config.channel_mask;
2597 outputDesc->mFormat = config.format;
2598
2599 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002600 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002601 ssize_t index =
2602 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2603 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002604 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2605 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002606 }
2607 }
2608 if (mPrimaryOutput == 0 &&
2609 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002610 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002611 }
2612 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002613 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002614 outputDesc->mDevice,
2615 true);
2616 }
Eric Laurente552edb2014-03-10 17:42:56 -07002617 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002618 // open input streams needed to access attached devices to validate
2619 // mAvailableInputDevices list
2620 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2621 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002622 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002623
Eric Laurent3a4311c2014-03-17 12:00:47 -07002624 if (inProfile->mSupportedDevices.isEmpty()) {
2625 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2626 continue;
2627 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002628 // chose first device present in mSupportedDevices also part of
2629 // inputDeviceTypes
2630 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2631 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002632 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002633 if (profileType & inputDeviceTypes) {
2634 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002635 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002636 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002637 if ((profileType & inputDeviceTypes) == 0) {
2638 continue;
2639 }
2640 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2641
2642 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2643 inputDesc->mDevice = profileType;
2644
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002645 // find the address
2646 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2647 // the inputs vector must be of size 1, but we don't want to crash here
2648 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2649 : String8("");
2650 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2651 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2652
Eric Laurentd78f1532014-09-16 16:38:20 -07002653 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2654 config.sample_rate = inputDesc->mSamplingRate;
2655 config.channel_mask = inputDesc->mChannelMask;
2656 config.format = inputDesc->mFormat;
2657 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002658 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002659 &input,
2660 &config,
2661 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002662 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002663 AUDIO_SOURCE_MIC,
2664 AUDIO_INPUT_FLAG_NONE);
2665
2666 if (status == NO_ERROR) {
2667 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002668 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002669 ssize_t index =
2670 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2671 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002672 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2673 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002674 }
2675 }
2676 mpClientInterface->closeInput(input);
2677 } else {
2678 ALOGW("Cannot open input stream for device %08x on hw module %s",
2679 inputDesc->mDevice,
2680 mHwModules[i]->mName);
2681 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002682 }
2683 }
2684 // make sure all attached devices have been allocated a unique ID
2685 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002686 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002687 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002688 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2689 continue;
2690 }
François Gaffie2110e042015-03-24 08:41:51 +01002691 // The device is now validated and can be appended to the available devices of the engine
2692 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2693 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002694 i++;
2695 }
2696 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002697 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002698 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002699 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2700 continue;
2701 }
François Gaffie2110e042015-03-24 08:41:51 +01002702 // The device is now validated and can be appended to the available devices of the engine
2703 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2704 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002705 i++;
2706 }
2707 // make sure default device is reachable
2708 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002709 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002710 }
Eric Laurente552edb2014-03-10 17:42:56 -07002711
2712 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2713
2714 updateDevicesAndOutputs();
2715
2716#ifdef AUDIO_POLICY_TEST
2717 if (mPrimaryOutput != 0) {
2718 AudioParameter outputCmd = AudioParameter();
2719 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002720 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002721
2722 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2723 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002724 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2725 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002726 mTestLatencyMs = 0;
2727 mCurOutput = 0;
2728 mDirectOutput = false;
2729 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2730 mTestOutputs[i] = 0;
2731 }
2732
2733 const size_t SIZE = 256;
2734 char buffer[SIZE];
2735 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2736 run(buffer, ANDROID_PRIORITY_AUDIO);
2737 }
2738#endif //AUDIO_POLICY_TEST
2739}
2740
Eric Laurente0720872014-03-11 09:30:41 -07002741AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002742{
2743#ifdef AUDIO_POLICY_TEST
2744 exit();
2745#endif //AUDIO_POLICY_TEST
2746 for (size_t i = 0; i < mOutputs.size(); i++) {
2747 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002748 }
2749 for (size_t i = 0; i < mInputs.size(); i++) {
2750 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002751 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002752 mAvailableOutputDevices.clear();
2753 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002754 mOutputs.clear();
2755 mInputs.clear();
2756 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002757}
2758
Eric Laurente0720872014-03-11 09:30:41 -07002759status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002760{
2761 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2762}
2763
2764#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002765bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002766{
2767 ALOGV("entering threadLoop()");
2768 while (!exitPending())
2769 {
2770 String8 command;
2771 int valueInt;
2772 String8 value;
2773
2774 Mutex::Autolock _l(mLock);
2775 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2776
2777 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2778 AudioParameter param = AudioParameter(command);
2779
2780 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2781 valueInt != 0) {
2782 ALOGV("Test command %s received", command.string());
2783 String8 target;
2784 if (param.get(String8("target"), target) != NO_ERROR) {
2785 target = "Manager";
2786 }
2787 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2788 param.remove(String8("test_cmd_policy_output"));
2789 mCurOutput = valueInt;
2790 }
2791 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2792 param.remove(String8("test_cmd_policy_direct"));
2793 if (value == "false") {
2794 mDirectOutput = false;
2795 } else if (value == "true") {
2796 mDirectOutput = true;
2797 }
2798 }
2799 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2800 param.remove(String8("test_cmd_policy_input"));
2801 mTestInput = valueInt;
2802 }
2803
2804 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2805 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002806 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002807 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002808 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002809 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002810 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002811 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002812 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002813 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002814 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002815 if (target == "Manager") {
2816 mTestFormat = format;
2817 } else if (mTestOutputs[mCurOutput] != 0) {
2818 AudioParameter outputParam = AudioParameter();
2819 outputParam.addInt(String8("format"), format);
2820 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2821 }
2822 }
2823 }
2824 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2825 param.remove(String8("test_cmd_policy_channels"));
2826 int channels = 0;
2827
2828 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002829 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002830 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002831 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002832 }
2833 if (channels != 0) {
2834 if (target == "Manager") {
2835 mTestChannels = channels;
2836 } else if (mTestOutputs[mCurOutput] != 0) {
2837 AudioParameter outputParam = AudioParameter();
2838 outputParam.addInt(String8("channels"), channels);
2839 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2840 }
2841 }
2842 }
2843 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2844 param.remove(String8("test_cmd_policy_sampleRate"));
2845 if (valueInt >= 0 && valueInt <= 96000) {
2846 int samplingRate = valueInt;
2847 if (target == "Manager") {
2848 mTestSamplingRate = samplingRate;
2849 } else if (mTestOutputs[mCurOutput] != 0) {
2850 AudioParameter outputParam = AudioParameter();
2851 outputParam.addInt(String8("sampling_rate"), samplingRate);
2852 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2853 }
2854 }
2855 }
2856
2857 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2858 param.remove(String8("test_cmd_policy_reopen"));
2859
Eric Laurentc75307b2015-03-17 15:29:32 -07002860 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07002861
Eric Laurentc75307b2015-03-17 15:29:32 -07002862 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07002863
Eric Laurentc75307b2015-03-17 15:29:32 -07002864 removeOutput(mPrimaryOutput->mIoHandle);
2865 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
2866 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07002867 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002868 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2869 config.sample_rate = outputDesc->mSamplingRate;
2870 config.channel_mask = outputDesc->mChannelMask;
2871 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07002872 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002873 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07002874 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002875 &config,
2876 &outputDesc->mDevice,
2877 String8(""),
2878 &outputDesc->mLatency,
2879 outputDesc->mFlags);
2880 if (status != NO_ERROR) {
2881 ALOGE("Failed to reopen hardware output stream, "
2882 "samplingRate: %d, format %d, channels %d",
2883 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07002884 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002885 outputDesc->mSamplingRate = config.sample_rate;
2886 outputDesc->mChannelMask = config.channel_mask;
2887 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07002888 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07002889 AudioParameter outputCmd = AudioParameter();
2890 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002891 mpClientInterface->setParameters(handle, outputCmd.toString());
2892 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07002893 }
2894 }
2895
2896
2897 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2898 }
2899 }
2900 return false;
2901}
2902
Eric Laurente0720872014-03-11 09:30:41 -07002903void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002904{
2905 {
2906 AutoMutex _l(mLock);
2907 requestExit();
2908 mWaitWorkCV.signal();
2909 }
2910 requestExitAndWait();
2911}
2912
Eric Laurente0720872014-03-11 09:30:41 -07002913int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002914{
2915 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2916 if (output == mTestOutputs[i]) return i;
2917 }
2918 return 0;
2919}
2920#endif //AUDIO_POLICY_TEST
2921
2922// ---
2923
Eric Laurentc75307b2015-03-17 15:29:32 -07002924void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002925{
François Gaffie98cc1912015-03-18 17:52:40 +01002926 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07002927 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002928 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002929}
2930
François Gaffie53615e22015-03-19 09:24:12 +01002931void AudioPolicyManager::removeOutput(audio_io_handle_t output)
2932{
2933 mOutputs.removeItem(output);
2934}
2935
Eric Laurent1f2f2232014-06-02 12:01:23 -07002936void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002937{
François Gaffie98cc1912015-03-18 17:52:40 +01002938 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07002939 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002940 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002941}
Eric Laurente552edb2014-03-10 17:42:56 -07002942
Eric Laurentc75307b2015-03-17 15:29:32 -07002943void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08002944 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002945 const String8 address /*in*/,
2946 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08002947 sp<DeviceDescriptor> devDesc =
2948 desc->mProfile->mSupportedDevices.getDevice(device, address);
2949 if (devDesc != 0) {
2950 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
2951 desc->mIoHandle, address.string());
2952 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002953 }
2954}
2955
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002956status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01002957 audio_policy_dev_state_t state,
2958 SortedVector<audio_io_handle_t>& outputs,
2959 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002960{
François Gaffie53615e22015-03-19 09:24:12 +01002961 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07002962 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002963 // erase all current sample rates, formats and channel masks
2964 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07002965
Eric Laurent3b73df72014-03-11 09:06:29 -07002966 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002967 // first list already open outputs that can be routed to this device
2968 for (size_t i = 0; i < mOutputs.size(); i++) {
2969 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07002970 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01002971 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002972 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2973 outputs.add(mOutputs.keyAt(i));
2974 } else {
2975 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08002976 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002977 }
Eric Laurente552edb2014-03-10 17:42:56 -07002978 }
2979 }
2980 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002981 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002982 for (size_t i = 0; i < mHwModules.size(); i++)
2983 {
2984 if (mHwModules[i]->mHandle == 0) {
2985 continue;
2986 }
2987 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2988 {
Eric Laurent275e8e92014-11-30 15:14:47 -08002989 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
2990 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01002991 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08002992 address == profile->mSupportedDevices[0]->mAddress) {
2993 profiles.add(profile);
2994 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
2995 }
Eric Laurente552edb2014-03-10 17:42:56 -07002996 }
2997 }
2998 }
2999
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003000 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3001
Eric Laurente552edb2014-03-10 17:42:56 -07003002 if (profiles.isEmpty() && outputs.isEmpty()) {
3003 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3004 return BAD_VALUE;
3005 }
3006
3007 // open outputs for matching profiles if needed. Direct outputs are also opened to
3008 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3009 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003010 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003011
3012 // nothing to do if one output is already opened for this profile
3013 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003014 for (j = 0; j < outputs.size(); j++) {
3015 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003016 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003017 // matching profile: save the sample rates, format and channel masks supported
3018 // by the profile in our device descriptor
3019 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003020 break;
3021 }
3022 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003023 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003024 continue;
3025 }
3026
Eric Laurent83b88082014-06-20 18:31:16 -07003027 ALOGV("opening output for device %08x with params %s profile %p",
3028 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003029 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003030 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003031 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3032 config.sample_rate = desc->mSamplingRate;
3033 config.channel_mask = desc->mChannelMask;
3034 config.format = desc->mFormat;
3035 config.offload_info.sample_rate = desc->mSamplingRate;
3036 config.offload_info.channel_mask = desc->mChannelMask;
3037 config.offload_info.format = desc->mFormat;
3038 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003039 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003040 &output,
3041 &config,
3042 &desc->mDevice,
3043 address,
3044 &desc->mLatency,
3045 desc->mFlags);
3046 if (status == NO_ERROR) {
3047 desc->mSamplingRate = config.sample_rate;
3048 desc->mChannelMask = config.channel_mask;
3049 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003050
Eric Laurentd4692962014-05-05 18:13:44 -07003051 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003052 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003053 char *param = audio_device_address_to_parameter(device, address);
3054 mpClientInterface->setParameters(output, String8(param));
3055 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003056 }
3057
Eric Laurentd4692962014-05-05 18:13:44 -07003058 // Here is where we step through and resolve any "dynamic" fields
3059 String8 reply;
3060 char *value;
3061 if (profile->mSamplingRates[0] == 0) {
3062 reply = mpClientInterface->getParameters(output,
3063 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003064 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003065 reply.string());
3066 value = strpbrk((char *)reply.string(), "=");
3067 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003068 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003069 }
Eric Laurentd4692962014-05-05 18:13:44 -07003070 }
3071 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3072 reply = mpClientInterface->getParameters(output,
3073 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003074 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003075 reply.string());
3076 value = strpbrk((char *)reply.string(), "=");
3077 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003078 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003079 }
Eric Laurentd4692962014-05-05 18:13:44 -07003080 }
3081 if (profile->mChannelMasks[0] == 0) {
3082 reply = mpClientInterface->getParameters(output,
3083 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003084 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003085 reply.string());
3086 value = strpbrk((char *)reply.string(), "=");
3087 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003088 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003089 }
Eric Laurentd4692962014-05-05 18:13:44 -07003090 }
3091 if (((profile->mSamplingRates[0] == 0) &&
3092 (profile->mSamplingRates.size() < 2)) ||
3093 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3094 (profile->mFormats.size() < 2)) ||
3095 ((profile->mChannelMasks[0] == 0) &&
3096 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003097 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003098 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003099 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003100 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3101 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003102 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003103 config.sample_rate = profile->pickSamplingRate();
3104 config.channel_mask = profile->pickChannelMask();
3105 config.format = profile->pickFormat();
3106 config.offload_info.sample_rate = config.sample_rate;
3107 config.offload_info.channel_mask = config.channel_mask;
3108 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003109 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003110 &output,
3111 &config,
3112 &desc->mDevice,
3113 address,
3114 &desc->mLatency,
3115 desc->mFlags);
3116 if (status == NO_ERROR) {
3117 desc->mSamplingRate = config.sample_rate;
3118 desc->mChannelMask = config.channel_mask;
3119 desc->mFormat = config.format;
3120 } else {
3121 output = AUDIO_IO_HANDLE_NONE;
3122 }
Eric Laurentd4692962014-05-05 18:13:44 -07003123 }
3124
Eric Laurentcf2c0212014-07-25 16:20:43 -07003125 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003126 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003127 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003128 sp<AudioPolicyMix> policyMix;
3129 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003130 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3131 address.string());
3132 }
François Gaffie036e1e92015-03-19 10:16:24 +01003133 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003134 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003135
Eric Laurentc722f302014-12-10 11:21:49 -08003136 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3137 // no duplicated output for direct outputs and
3138 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003139 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003140
Eric Laurentd4692962014-05-05 18:13:44 -07003141 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003142 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003143
Eric Laurentd4692962014-05-05 18:13:44 -07003144 //TODO: configure audio effect output stage here
3145
3146 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003147 duplicatedOutput =
3148 mpClientInterface->openDuplicateOutput(output,
3149 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003150 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003151 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003152 sp<SwAudioOutputDescriptor> dupOutputDesc =
3153 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3154 dupOutputDesc->mOutput1 = mPrimaryOutput;
3155 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003156 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3157 dupOutputDesc->mFormat = desc->mFormat;
3158 dupOutputDesc->mChannelMask = desc->mChannelMask;
3159 dupOutputDesc->mLatency = desc->mLatency;
3160 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003161 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003162 } else {
3163 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003164 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003165 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003166 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003167 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003168 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003169 }
Eric Laurente552edb2014-03-10 17:42:56 -07003170 }
3171 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003172 } else {
3173 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003174 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003175 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003176 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003177 profiles.removeAt(profile_index);
3178 profile_index--;
3179 } else {
3180 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003181 devDesc->importAudioPort(profile);
3182
François Gaffie53615e22015-03-19 09:24:12 +01003183 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003184 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3185 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003186 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003187 NULL/*patch handle*/, address.string());
3188 }
Eric Laurente552edb2014-03-10 17:42:56 -07003189 ALOGV("checkOutputsForDevice(): adding output %d", output);
3190 }
3191 }
3192
3193 if (profiles.isEmpty()) {
3194 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3195 return BAD_VALUE;
3196 }
Eric Laurentd4692962014-05-05 18:13:44 -07003197 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003198 // check if one opened output is not needed any more after disconnecting one device
3199 for (size_t i = 0; i < mOutputs.size(); i++) {
3200 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003201 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003202 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003203 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003204 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003205 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003206 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003207 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3208 mOutputs.keyAt(i));
3209 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003210 }
Eric Laurente552edb2014-03-10 17:42:56 -07003211 }
3212 }
Eric Laurentd4692962014-05-05 18:13:44 -07003213 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003214 for (size_t i = 0; i < mHwModules.size(); i++)
3215 {
3216 if (mHwModules[i]->mHandle == 0) {
3217 continue;
3218 }
3219 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3220 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003221 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003222 if (profile->mSupportedDevices.types() & device) {
3223 ALOGV("checkOutputsForDevice(): "
3224 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003225 if (profile->mSamplingRates[0] == 0) {
3226 profile->mSamplingRates.clear();
3227 profile->mSamplingRates.add(0);
3228 }
3229 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3230 profile->mFormats.clear();
3231 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3232 }
3233 if (profile->mChannelMasks[0] == 0) {
3234 profile->mChannelMasks.clear();
3235 profile->mChannelMasks.add(0);
3236 }
3237 }
3238 }
3239 }
3240 }
3241 return NO_ERROR;
3242}
3243
Eric Laurentd4692962014-05-05 18:13:44 -07003244status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003245 audio_policy_dev_state_t state,
3246 SortedVector<audio_io_handle_t>& inputs,
3247 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003248{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003249 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003250 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3251 // first list already open inputs that can be routed to this device
3252 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3253 desc = mInputs.valueAt(input_index);
3254 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3255 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3256 inputs.add(mInputs.keyAt(input_index));
3257 }
3258 }
3259
3260 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003261 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003262 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3263 {
3264 if (mHwModules[module_idx]->mHandle == 0) {
3265 continue;
3266 }
3267 for (size_t profile_index = 0;
3268 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3269 profile_index++)
3270 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003271 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3272
3273 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003274 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003275 address == profile->mSupportedDevices[0]->mAddress) {
3276 profiles.add(profile);
3277 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3278 profile_index, module_idx);
3279 }
Eric Laurentd4692962014-05-05 18:13:44 -07003280 }
3281 }
3282 }
3283
3284 if (profiles.isEmpty() && inputs.isEmpty()) {
3285 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3286 return BAD_VALUE;
3287 }
3288
3289 // open inputs for matching profiles if needed. Direct inputs are also opened to
3290 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3291 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3292
Eric Laurent1c333e22014-05-20 10:48:17 -07003293 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003294 // nothing to do if one input is already opened for this profile
3295 size_t input_index;
3296 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3297 desc = mInputs.valueAt(input_index);
3298 if (desc->mProfile == profile) {
3299 break;
3300 }
3301 }
3302 if (input_index != mInputs.size()) {
3303 continue;
3304 }
3305
3306 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3307 desc = new AudioInputDescriptor(profile);
3308 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003309 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3310 config.sample_rate = desc->mSamplingRate;
3311 config.channel_mask = desc->mChannelMask;
3312 config.format = desc->mFormat;
3313 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003314 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003315 &input,
3316 &config,
3317 &desc->mDevice,
3318 address,
3319 AUDIO_SOURCE_MIC,
3320 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003321
Eric Laurentcf2c0212014-07-25 16:20:43 -07003322 if (status == NO_ERROR) {
3323 desc->mSamplingRate = config.sample_rate;
3324 desc->mChannelMask = config.channel_mask;
3325 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003326
Eric Laurentd4692962014-05-05 18:13:44 -07003327 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003328 char *param = audio_device_address_to_parameter(device, address);
3329 mpClientInterface->setParameters(input, String8(param));
3330 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003331 }
3332
3333 // Here is where we step through and resolve any "dynamic" fields
3334 String8 reply;
3335 char *value;
3336 if (profile->mSamplingRates[0] == 0) {
3337 reply = mpClientInterface->getParameters(input,
3338 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3339 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3340 reply.string());
3341 value = strpbrk((char *)reply.string(), "=");
3342 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003343 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003344 }
3345 }
3346 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3347 reply = mpClientInterface->getParameters(input,
3348 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3349 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3350 value = strpbrk((char *)reply.string(), "=");
3351 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003352 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003353 }
3354 }
3355 if (profile->mChannelMasks[0] == 0) {
3356 reply = mpClientInterface->getParameters(input,
3357 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3358 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3359 reply.string());
3360 value = strpbrk((char *)reply.string(), "=");
3361 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003362 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003363 }
3364 }
3365 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3366 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3367 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3368 ALOGW("checkInputsForDevice() direct input missing param");
3369 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003370 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003371 }
3372
3373 if (input != 0) {
3374 addInput(input, desc);
3375 }
3376 } // endif input != 0
3377
Eric Laurentcf2c0212014-07-25 16:20:43 -07003378 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003379 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003380 profiles.removeAt(profile_index);
3381 profile_index--;
3382 } else {
3383 inputs.add(input);
3384 ALOGV("checkInputsForDevice(): adding input %d", input);
3385 }
3386 } // end scan profiles
3387
3388 if (profiles.isEmpty()) {
3389 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3390 return BAD_VALUE;
3391 }
3392 } else {
3393 // Disconnect
3394 // check if one opened input is not needed any more after disconnecting one device
3395 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3396 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003397 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3398 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003399 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3400 mInputs.keyAt(input_index));
3401 inputs.add(mInputs.keyAt(input_index));
3402 }
3403 }
3404 // Clear any profiles associated with the disconnected device.
3405 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3406 if (mHwModules[module_index]->mHandle == 0) {
3407 continue;
3408 }
3409 for (size_t profile_index = 0;
3410 profile_index < mHwModules[module_index]->mInputProfiles.size();
3411 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003412 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003413 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003414 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003415 profile_index, module_index);
3416 if (profile->mSamplingRates[0] == 0) {
3417 profile->mSamplingRates.clear();
3418 profile->mSamplingRates.add(0);
3419 }
3420 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3421 profile->mFormats.clear();
3422 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3423 }
3424 if (profile->mChannelMasks[0] == 0) {
3425 profile->mChannelMasks.clear();
3426 profile->mChannelMasks.add(0);
3427 }
3428 }
3429 }
3430 }
3431 } // end disconnect
3432
3433 return NO_ERROR;
3434}
3435
3436
Eric Laurente0720872014-03-11 09:30:41 -07003437void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003438{
3439 ALOGV("closeOutput(%d)", output);
3440
Eric Laurentc75307b2015-03-17 15:29:32 -07003441 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003442 if (outputDesc == NULL) {
3443 ALOGW("closeOutput() unknown output %d", output);
3444 return;
3445 }
François Gaffie036e1e92015-03-19 10:16:24 +01003446 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003447
Eric Laurente552edb2014-03-10 17:42:56 -07003448 // look for duplicated outputs connected to the output being removed.
3449 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003450 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003451 if (dupOutputDesc->isDuplicated() &&
3452 (dupOutputDesc->mOutput1 == outputDesc ||
3453 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003454 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003455 if (dupOutputDesc->mOutput1 == outputDesc) {
3456 outputDesc2 = dupOutputDesc->mOutput2;
3457 } else {
3458 outputDesc2 = dupOutputDesc->mOutput1;
3459 }
3460 // As all active tracks on duplicated output will be deleted,
3461 // and as they were also referenced on the other output, the reference
3462 // count for their stream type must be adjusted accordingly on
3463 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003464 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003465 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003466 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003467 }
3468 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3469 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3470
3471 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003472 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003473 }
3474 }
3475
Eric Laurent05b90f82014-08-27 15:32:29 -07003476 nextAudioPortGeneration();
3477
3478 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3479 if (index >= 0) {
3480 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3481 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3482 mAudioPatches.removeItemsAt(index);
3483 mpClientInterface->onAudioPatchListUpdate();
3484 }
3485
Eric Laurente552edb2014-03-10 17:42:56 -07003486 AudioParameter param;
3487 param.add(String8("closing"), String8("true"));
3488 mpClientInterface->setParameters(output, param.toString());
3489
3490 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003491 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003492 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003493}
3494
3495void AudioPolicyManager::closeInput(audio_io_handle_t input)
3496{
3497 ALOGV("closeInput(%d)", input);
3498
3499 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3500 if (inputDesc == NULL) {
3501 ALOGW("closeInput() unknown input %d", input);
3502 return;
3503 }
3504
Eric Laurent6a94d692014-05-20 11:18:06 -07003505 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003506
3507 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3508 if (index >= 0) {
3509 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3510 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3511 mAudioPatches.removeItemsAt(index);
3512 mpClientInterface->onAudioPatchListUpdate();
3513 }
3514
3515 mpClientInterface->closeInput(input);
3516 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003517}
3518
Eric Laurentc75307b2015-03-17 15:29:32 -07003519SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3520 audio_devices_t device,
3521 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003522{
3523 SortedVector<audio_io_handle_t> outputs;
3524
3525 ALOGVV("getOutputsForDevice() device %04x", device);
3526 for (size_t i = 0; i < openOutputs.size(); i++) {
3527 ALOGVV("output %d isDuplicated=%d device=%04x",
3528 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3529 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3530 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3531 outputs.add(openOutputs.keyAt(i));
3532 }
3533 }
3534 return outputs;
3535}
3536
Eric Laurente0720872014-03-11 09:30:41 -07003537bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003538 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003539{
3540 if (outputs1.size() != outputs2.size()) {
3541 return false;
3542 }
3543 for (size_t i = 0; i < outputs1.size(); i++) {
3544 if (outputs1[i] != outputs2[i]) {
3545 return false;
3546 }
3547 }
3548 return true;
3549}
3550
Eric Laurente0720872014-03-11 09:30:41 -07003551void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003552{
3553 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3554 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3555 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3556 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3557
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003558 // also take into account external policy-related changes: add all outputs which are
3559 // associated with policies in the "before" and "after" output vectors
3560 ALOGVV("checkOutputForStrategy(): policy related outputs");
3561 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003562 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003563 if (desc != 0 && desc->mPolicyMix != NULL) {
3564 srcOutputs.add(desc->mIoHandle);
3565 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3566 }
3567 }
3568 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003569 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003570 if (desc != 0 && desc->mPolicyMix != NULL) {
3571 dstOutputs.add(desc->mIoHandle);
3572 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3573 }
3574 }
3575
Eric Laurente552edb2014-03-10 17:42:56 -07003576 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3577 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3578 strategy, srcOutputs[0], dstOutputs[0]);
3579 // mute strategy while moving tracks from one output to another
3580 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003581 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003582 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003583 setStrategyMute(strategy, true, desc);
3584 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003585 }
3586 }
3587
3588 // Move effects associated to this strategy from previous output to new output
3589 if (strategy == STRATEGY_MEDIA) {
3590 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3591 SortedVector<audio_io_handle_t> moved;
3592 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003593 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3594 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3595 effectDesc->mIo != fxOutput) {
3596 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003597 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3598 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003599 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003600 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003601 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003602 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003603 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003604 }
3605 }
3606 }
3607 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003608 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003609 if (i == AUDIO_STREAM_PATCH) {
3610 continue;
3611 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003612 if (getStrategy((audio_stream_type_t)i) == strategy) {
3613 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003614 }
3615 }
3616 }
3617}
3618
Eric Laurente0720872014-03-11 09:30:41 -07003619void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003620{
François Gaffie2110e042015-03-24 08:41:51 +01003621 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003622 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003623 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003624 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003625 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003626 checkOutputForStrategy(STRATEGY_SONIFICATION);
3627 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003628 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003629 checkOutputForStrategy(STRATEGY_MEDIA);
3630 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003631 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003632}
3633
Eric Laurente0720872014-03-11 09:30:41 -07003634void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003635{
François Gaffie53615e22015-03-19 09:24:12 +01003636 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003637 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003638 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003639 return;
3640 }
3641
Eric Laurent3a4311c2014-03-17 12:00:47 -07003642 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003643 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3644 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3645 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003646 // suspend A2DP output if:
3647 // (NOT already suspended) &&
3648 // ((SCO device is connected &&
3649 // (forced usage for communication || for record is SCO))) ||
3650 // (phone state is ringing || in call)
3651 //
3652 // restore A2DP output if:
3653 // (Already suspended) &&
3654 // ((SCO device is NOT connected ||
3655 // (forced usage NOT for communication && NOT for record is SCO))) &&
3656 // (phone state is NOT ringing && NOT in call)
3657 //
3658 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003659 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003660 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3661 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3662 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3663 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003664
3665 mpClientInterface->restoreOutput(a2dpOutput);
3666 mA2dpSuspended = false;
3667 }
3668 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003669 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003670 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3671 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3672 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3673 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003674
3675 mpClientInterface->suspendOutput(a2dpOutput);
3676 mA2dpSuspended = true;
3677 }
3678 }
3679}
3680
Eric Laurentc75307b2015-03-17 15:29:32 -07003681audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3682 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003683{
3684 audio_devices_t device = AUDIO_DEVICE_NONE;
3685
Eric Laurent6a94d692014-05-20 11:18:06 -07003686 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3687 if (index >= 0) {
3688 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3689 if (patchDesc->mUid != mUidCached) {
3690 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3691 outputDesc->device(), outputDesc->mPatchHandle);
3692 return outputDesc->device();
3693 }
3694 }
3695
Eric Laurente552edb2014-03-10 17:42:56 -07003696 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003697 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003698 // use device for strategy enforced audible
3699 // 2: we are in call or the strategy phone is active on the output:
3700 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003701 // 3: the strategy for enforced audible is active but not enforced on the output:
3702 // use the device for strategy enforced audible
3703 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003704 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003705 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003706 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003707 // 6: the strategy accessibility is active on the output:
3708 // use device for strategy accessibility
3709 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003710 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003711 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003712 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003713 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003714 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003715 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003716 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003717 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3718 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003719 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003720 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003721 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003722 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003723 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003724 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003725 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003726 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003727 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003728 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003729 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003730 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003731 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003732 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003733 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003734 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003735 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003736 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003737 }
3738
Eric Laurent1c333e22014-05-20 10:48:17 -07003739 ALOGV("getNewOutputDevice() selected device %x", device);
3740 return device;
3741}
3742
3743audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3744{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003745 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003746
3747 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3748 if (index >= 0) {
3749 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3750 if (patchDesc->mUid != mUidCached) {
3751 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3752 inputDesc->mDevice, inputDesc->mPatchHandle);
3753 return inputDesc->mDevice;
3754 }
3755 }
3756
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003757 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003758
3759 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003760 return device;
3761}
3762
Eric Laurente0720872014-03-11 09:30:41 -07003763uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003764 return (uint32_t)getStrategy(stream);
3765}
3766
Eric Laurente0720872014-03-11 09:30:41 -07003767audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003768 // By checking the range of stream before calling getStrategy, we avoid
3769 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3770 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003771 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003772 return AUDIO_DEVICE_NONE;
3773 }
3774 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003775 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003776 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3777 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3778 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003779 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003780 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003781 devices = outputDesc->device();
3782 break;
3783 }
Eric Laurente552edb2014-03-10 17:42:56 -07003784 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003785
3786 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3787 and doesn't really need to.*/
3788 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3789 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3790 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3791 }
3792
Eric Laurente552edb2014-03-10 17:42:56 -07003793 return devices;
3794}
3795
François Gaffie2110e042015-03-24 08:41:51 +01003796routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3797{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003798 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003799 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003800}
3801
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003802uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3803 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003804 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3805 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3806 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003807 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3808 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3809 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003810 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003811 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003812}
3813
Eric Laurente0720872014-03-11 09:30:41 -07003814void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003815 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003816 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003817 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3818 updateDevicesAndOutputs();
3819 break;
3820 default:
3821 break;
3822 }
3823}
3824
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003825uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3826 switch(event) {
3827 case STARTING_OUTPUT:
3828 mBeaconMuteRefCount++;
3829 break;
3830 case STOPPING_OUTPUT:
3831 if (mBeaconMuteRefCount > 0) {
3832 mBeaconMuteRefCount--;
3833 }
3834 break;
3835 case STARTING_BEACON:
3836 mBeaconPlayingRefCount++;
3837 break;
3838 case STOPPING_BEACON:
3839 if (mBeaconPlayingRefCount > 0) {
3840 mBeaconPlayingRefCount--;
3841 }
3842 break;
3843 }
3844
3845 if (mBeaconMuteRefCount > 0) {
3846 // any playback causes beacon to be muted
3847 return setBeaconMute(true);
3848 } else {
3849 // no other playback: unmute when beacon starts playing, mute when it stops
3850 return setBeaconMute(mBeaconPlayingRefCount == 0);
3851 }
3852}
3853
3854uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3855 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3856 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3857 // keep track of muted state to avoid repeating mute/unmute operations
3858 if (mBeaconMuted != mute) {
3859 // mute/unmute AUDIO_STREAM_TTS on all outputs
3860 ALOGV("\t muting %d", mute);
3861 uint32_t maxLatency = 0;
3862 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003863 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003864 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07003865 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003866 0 /*delay*/, AUDIO_DEVICE_NONE);
3867 const uint32_t latency = desc->latency() * 2;
3868 if (latency > maxLatency) {
3869 maxLatency = latency;
3870 }
3871 }
3872 mBeaconMuted = mute;
3873 return maxLatency;
3874 }
3875 return 0;
3876}
3877
Eric Laurente0720872014-03-11 09:30:41 -07003878audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01003879 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003880{
Paul McLeanaa981192015-03-21 09:55:15 -07003881 // Routing
3882 // see if we have an explicit route
3883 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
3884 // (getStrategy(stream)).
3885 // if the strategy from the stream type in the RouteMap is the same as the argument above,
3886 // and activity count is non-zero
3887 // the device = the device from the descriptor in the RouteMap, and exit.
3888 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
3889 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
3890 routing_strategy strat = getStrategy(route->mStreamType);
3891 if (strat == strategy && route->mDeviceDescriptor != 0 /*&& route->mActivityCount != 0*/) {
3892 return route->mDeviceDescriptor->type();
3893 }
3894 }
3895
Eric Laurente552edb2014-03-10 17:42:56 -07003896 if (fromCache) {
3897 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3898 strategy, mDeviceForStrategy[strategy]);
3899 return mDeviceForStrategy[strategy];
3900 }
François Gaffie2110e042015-03-24 08:41:51 +01003901 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07003902}
3903
Eric Laurente0720872014-03-11 09:30:41 -07003904void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003905{
3906 for (int i = 0; i < NUM_STRATEGIES; i++) {
3907 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3908 }
3909 mPreviousOutputs = mOutputs;
3910}
3911
Eric Laurent1f2f2232014-06-02 12:01:23 -07003912uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003913 audio_devices_t prevDevice,
3914 uint32_t delayMs)
3915{
3916 // mute/unmute strategies using an incompatible device combination
3917 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3918 // if unmuting, unmute only after the specified delay
3919 if (outputDesc->isDuplicated()) {
3920 return 0;
3921 }
3922
3923 uint32_t muteWaitMs = 0;
3924 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003925 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003926
3927 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3928 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07003929 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07003930 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3931 bool doMute = false;
3932
3933 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3934 doMute = true;
3935 outputDesc->mStrategyMutedByDevice[i] = true;
3936 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3937 doMute = true;
3938 outputDesc->mStrategyMutedByDevice[i] = false;
3939 }
Eric Laurent99401132014-05-07 19:48:15 -07003940 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003941 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003942 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003943 // skip output if it does not share any device with current output
3944 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3945 == AUDIO_DEVICE_NONE) {
3946 continue;
3947 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003948 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
3949 mute ? "muting" : "unmuting", i, curDevice);
3950 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01003951 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003952 if (mute) {
3953 // FIXME: should not need to double latency if volume could be applied
3954 // immediately by the audioflinger mixer. We must account for the delay
3955 // between now and the next time the audioflinger thread for this output
3956 // will process a buffer (which corresponds to one buffer size,
3957 // usually 1/2 or 1/4 of the latency).
3958 if (muteWaitMs < desc->latency() * 2) {
3959 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003960 }
3961 }
3962 }
3963 }
3964 }
3965 }
3966
Eric Laurent99401132014-05-07 19:48:15 -07003967 // temporary mute output if device selection changes to avoid volume bursts due to
3968 // different per device volumes
3969 if (outputDesc->isActive() && (device != prevDevice)) {
3970 if (muteWaitMs < outputDesc->latency() * 2) {
3971 muteWaitMs = outputDesc->latency() * 2;
3972 }
3973 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01003974 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003975 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07003976 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07003977 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07003978 muteWaitMs *2, device);
3979 }
3980 }
3981 }
3982
Eric Laurente552edb2014-03-10 17:42:56 -07003983 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3984 if (muteWaitMs > delayMs) {
3985 muteWaitMs -= delayMs;
3986 usleep(muteWaitMs * 1000);
3987 return muteWaitMs;
3988 }
3989 return 0;
3990}
3991
Eric Laurentc75307b2015-03-17 15:29:32 -07003992uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003993 audio_devices_t device,
3994 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003995 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003996 audio_patch_handle_t *patchHandle,
3997 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07003998{
Eric Laurentc75307b2015-03-17 15:29:32 -07003999 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004000 AudioParameter param;
4001 uint32_t muteWaitMs;
4002
4003 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004004 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4005 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004006 return muteWaitMs;
4007 }
4008 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4009 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004010 if ((device != AUDIO_DEVICE_NONE) &&
4011 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004012 return 0;
4013 }
4014
4015 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004016 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004017
4018 audio_devices_t prevDevice = outputDesc->mDevice;
4019
Paul McLeanaa981192015-03-21 09:55:15 -07004020 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004021
4022 if (device != AUDIO_DEVICE_NONE) {
4023 outputDesc->mDevice = device;
4024 }
4025 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4026
4027 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004028 // the requested device is AUDIO_DEVICE_NONE
4029 // OR the requested device is the same as current device
4030 // AND force is not specified
4031 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004032 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004033 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4034 !force &&
4035 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004036 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004037 return muteWaitMs;
4038 }
4039
4040 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004041
Eric Laurente552edb2014-03-10 17:42:56 -07004042 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004043 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004044 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004045 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004046 DeviceVector deviceList = (address == NULL) ?
4047 mAvailableOutputDevices.getDevicesFromType(device)
4048 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004049 if (!deviceList.isEmpty()) {
4050 struct audio_patch patch;
4051 outputDesc->toAudioPortConfig(&patch.sources[0]);
4052 patch.num_sources = 1;
4053 patch.num_sinks = 0;
4054 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4055 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004056 patch.num_sinks++;
4057 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004058 ssize_t index;
4059 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4060 index = mAudioPatches.indexOfKey(*patchHandle);
4061 } else {
4062 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4063 }
4064 sp< AudioPatch> patchDesc;
4065 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4066 if (index >= 0) {
4067 patchDesc = mAudioPatches.valueAt(index);
4068 afPatchHandle = patchDesc->mAfPatchHandle;
4069 }
4070
Eric Laurent1c333e22014-05-20 10:48:17 -07004071 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004072 &afPatchHandle,
4073 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004074 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4075 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004076 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004077 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004078 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004079 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004080 addAudioPatch(patchDesc->mHandle, patchDesc);
4081 } else {
4082 patchDesc->mPatch = patch;
4083 }
4084 patchDesc->mAfPatchHandle = afPatchHandle;
4085 patchDesc->mUid = mUidCached;
4086 if (patchHandle) {
4087 *patchHandle = patchDesc->mHandle;
4088 }
4089 outputDesc->mPatchHandle = patchDesc->mHandle;
4090 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004091 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004092 }
4093 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004094
4095 // inform all input as well
4096 for (size_t i = 0; i < mInputs.size(); i++) {
4097 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004098 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004099 AudioParameter inputCmd = AudioParameter();
4100 ALOGV("%s: inform input %d of device:%d", __func__,
4101 inputDescriptor->mIoHandle, device);
4102 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4103 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4104 inputCmd.toString(),
4105 delayMs);
4106 }
4107 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004108 }
Eric Laurente552edb2014-03-10 17:42:56 -07004109
4110 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004111 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004112
4113 return muteWaitMs;
4114}
4115
Eric Laurentc75307b2015-03-17 15:29:32 -07004116status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004117 int delayMs,
4118 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004119{
Eric Laurent6a94d692014-05-20 11:18:06 -07004120 ssize_t index;
4121 if (patchHandle) {
4122 index = mAudioPatches.indexOfKey(*patchHandle);
4123 } else {
4124 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4125 }
4126 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004127 return INVALID_OPERATION;
4128 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004129 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4130 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004131 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4132 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004133 removeAudioPatch(patchDesc->mHandle);
4134 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004135 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004136 return status;
4137}
4138
4139status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4140 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004141 bool force,
4142 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004143{
4144 status_t status = NO_ERROR;
4145
Eric Laurent1f2f2232014-06-02 12:01:23 -07004146 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004147 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4148 inputDesc->mDevice = device;
4149
4150 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4151 if (!deviceList.isEmpty()) {
4152 struct audio_patch patch;
4153 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004154 // AUDIO_SOURCE_HOTWORD is for internal use only:
4155 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004156 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4157 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004158 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4159 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004160 patch.num_sinks = 1;
4161 //only one input device for now
4162 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004163 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004164 ssize_t index;
4165 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4166 index = mAudioPatches.indexOfKey(*patchHandle);
4167 } else {
4168 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4169 }
4170 sp< AudioPatch> patchDesc;
4171 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4172 if (index >= 0) {
4173 patchDesc = mAudioPatches.valueAt(index);
4174 afPatchHandle = patchDesc->mAfPatchHandle;
4175 }
4176
Eric Laurent1c333e22014-05-20 10:48:17 -07004177 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004178 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004179 0);
4180 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004181 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004182 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004183 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004184 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004185 addAudioPatch(patchDesc->mHandle, patchDesc);
4186 } else {
4187 patchDesc->mPatch = patch;
4188 }
4189 patchDesc->mAfPatchHandle = afPatchHandle;
4190 patchDesc->mUid = mUidCached;
4191 if (patchHandle) {
4192 *patchHandle = patchDesc->mHandle;
4193 }
4194 inputDesc->mPatchHandle = patchDesc->mHandle;
4195 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004196 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004197 }
4198 }
4199 }
4200 return status;
4201}
4202
Eric Laurent6a94d692014-05-20 11:18:06 -07004203status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4204 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004205{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004206 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004207 ssize_t index;
4208 if (patchHandle) {
4209 index = mAudioPatches.indexOfKey(*patchHandle);
4210 } else {
4211 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4212 }
4213 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004214 return INVALID_OPERATION;
4215 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004216 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4217 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004218 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4219 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004220 removeAudioPatch(patchDesc->mHandle);
4221 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004222 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004223 return status;
4224}
4225
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004226sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004227 String8 address,
4228 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004229 audio_format_t& format,
4230 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004231 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004232{
4233 // Choose an input profile based on the requested capture parameters: select the first available
4234 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004235 //
4236 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4237 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004238
4239 for (size_t i = 0; i < mHwModules.size(); i++)
4240 {
4241 if (mHwModules[i]->mHandle == 0) {
4242 continue;
4243 }
4244 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4245 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004246 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004247 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004248 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004249 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004250 format,
4251 &format /*updatedFormat*/,
4252 channelMask,
4253 &channelMask /*updatedChannelMask*/,
4254 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004255
Eric Laurente552edb2014-03-10 17:42:56 -07004256 return profile;
4257 }
4258 }
4259 }
4260 return NULL;
4261}
4262
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004263
4264audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004265 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004266{
François Gaffie036e1e92015-03-19 10:16:24 +01004267 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4268 audio_devices_t selectedDeviceFromMix =
4269 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004270
François Gaffie036e1e92015-03-19 10:16:24 +01004271 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4272 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004273 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004274 return getDeviceForInputSource(inputSource);
4275}
4276
4277audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4278{
François Gaffie2110e042015-03-24 08:41:51 +01004279 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004280}
4281
Eric Laurente0720872014-03-11 09:30:41 -07004282float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004283 int index,
4284 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004285{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004286 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004287
4288 // if a headset is connected, apply the following rules to ring tones and notifications
4289 // to avoid sound level bursts in user's ears:
4290 // - always attenuate ring tones and notifications volume by 6dB
4291 // - if music is playing, always limit the volume to current music volume,
4292 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004293 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004294 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4295 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4296 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4297 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4298 ((stream_strategy == STRATEGY_SONIFICATION)
4299 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004300 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004301 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004302 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4303 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004304 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004305 // when the phone is ringing we must consider that music could have been paused just before
4306 // by the music application and behave as if music was active if the last music track was
4307 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004308 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004309 mLimitRingtoneVolume) {
4310 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004311 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4312 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004313 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004314 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4315 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4316 if (volumeDb > minVolDB) {
4317 volumeDb = minVolDB;
4318 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004319 }
4320 }
4321 }
4322
Eric Laurentffbc80f2015-03-18 18:30:19 -07004323 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004324}
4325
Eric Laurente0720872014-03-11 09:30:41 -07004326status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004327 int index,
4328 const sp<AudioOutputDescriptor>& outputDesc,
4329 audio_devices_t device,
4330 int delayMs,
4331 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004332{
Eric Laurente552edb2014-03-10 17:42:56 -07004333 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004334 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004335 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004336 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004337 return NO_ERROR;
4338 }
François Gaffie2110e042015-03-24 08:41:51 +01004339 audio_policy_forced_cfg_t forceUseForComm =
4340 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004341 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004342 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4343 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004344 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004345 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004346 return INVALID_OPERATION;
4347 }
4348
Eric Laurentc75307b2015-03-17 15:29:32 -07004349 if (device == AUDIO_DEVICE_NONE) {
4350 device = outputDesc->device();
4351 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004352
Eric Laurentffbc80f2015-03-18 18:30:19 -07004353 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004354 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004355 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004356 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004357
Eric Laurentffbc80f2015-03-18 18:30:19 -07004358 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004359
Eric Laurent3b73df72014-03-11 09:06:29 -07004360 if (stream == AUDIO_STREAM_VOICE_CALL ||
4361 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004362 float voiceVolume;
4363 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004364 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004365 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004366 } else {
4367 voiceVolume = 1.0;
4368 }
4369
Eric Laurentc75307b2015-03-17 15:29:32 -07004370 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004371 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4372 mLastVoiceVolume = voiceVolume;
4373 }
4374 }
4375
4376 return NO_ERROR;
4377}
4378
Eric Laurentc75307b2015-03-17 15:29:32 -07004379void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4380 audio_devices_t device,
4381 int delayMs,
4382 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004383{
Eric Laurentc75307b2015-03-17 15:29:32 -07004384 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004385
Eric Laurent3b73df72014-03-11 09:06:29 -07004386 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004387 if (stream == AUDIO_STREAM_PATCH) {
4388 continue;
4389 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004390 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004391 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4392 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004393 device,
4394 delayMs,
4395 force);
4396 }
4397}
4398
Eric Laurente0720872014-03-11 09:30:41 -07004399void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004400 bool on,
4401 const sp<AudioOutputDescriptor>& outputDesc,
4402 int delayMs,
4403 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004404{
4405 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004406 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004407 if (stream == AUDIO_STREAM_PATCH) {
4408 continue;
4409 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004410 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004411 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004412 }
4413 }
4414}
4415
Eric Laurente0720872014-03-11 09:30:41 -07004416void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004417 bool on,
4418 const sp<AudioOutputDescriptor>& outputDesc,
4419 int delayMs,
4420 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004421{
Eric Laurentc75307b2015-03-17 15:29:32 -07004422 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004423 if (device == AUDIO_DEVICE_NONE) {
4424 device = outputDesc->device();
4425 }
4426
Eric Laurentc75307b2015-03-17 15:29:32 -07004427 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4428 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004429
4430 if (on) {
4431 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004432 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004433 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004434 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004435 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004436 }
4437 }
4438 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4439 outputDesc->mMuteCount[stream]++;
4440 } else {
4441 if (outputDesc->mMuteCount[stream] == 0) {
4442 ALOGV("setStreamMute() unmuting non muted stream!");
4443 return;
4444 }
4445 if (--outputDesc->mMuteCount[stream] == 0) {
4446 checkAndSetVolume(stream,
4447 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004448 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004449 device,
4450 delayMs);
4451 }
4452 }
4453}
4454
Eric Laurente0720872014-03-11 09:30:41 -07004455void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004456 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004457{
4458 // if the stream pertains to sonification strategy and we are in call we must
4459 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4460 // in the device used for phone strategy and play the tone if the selected device does not
4461 // interfere with the device used for phone strategy
4462 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4463 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004464 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004465 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4466 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004467 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004468 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4469 stream, starting, outputDesc->mDevice, stateChange);
4470 if (outputDesc->mRefCount[stream]) {
4471 int muteCount = 1;
4472 if (stateChange) {
4473 muteCount = outputDesc->mRefCount[stream];
4474 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004475 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004476 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4477 for (int i = 0; i < muteCount; i++) {
4478 setStreamMute(stream, starting, mPrimaryOutput);
4479 }
4480 } else {
4481 ALOGV("handleIncallSonification() high visibility");
4482 if (outputDesc->device() &
4483 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4484 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4485 for (int i = 0; i < muteCount; i++) {
4486 setStreamMute(stream, starting, mPrimaryOutput);
4487 }
4488 }
4489 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004490 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4491 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004492 } else {
4493 mpClientInterface->stopTone();
4494 }
4495 }
4496 }
4497 }
4498}
4499
Paul McLeanaa981192015-03-21 09:55:15 -07004500// --- SessionRoute class implementation
4501void AudioPolicyManager::SessionRoute::log(const char* prefix) {
4502 ALOGI("%s[SessionRoute strm:0x%X, sess:0x%X, dev:0x%X refs:%d act:%d",
4503 prefix, mStreamType, mSession,
4504 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4505 mRefCount, mActivityCount);
4506}
4507
4508// --- SessionRouteMap class implementation
4509bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4510{
4511 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4512}
4513
4514void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4515 audio_stream_type_t streamType,
4516 sp<DeviceDescriptor> deviceDescriptor)
4517{
4518 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4519 if (route != NULL) {
4520 route->mRefCount++;
4521 route->mDeviceDescriptor = deviceDescriptor;
4522 } else {
4523 route = new AudioPolicyManager::SessionRoute(session, streamType, deviceDescriptor);
4524 route->mRefCount++;
4525 add(session, route);
4526 }
4527}
4528
4529void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4530{
4531 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4532 if (route != 0) {
4533 ALOG_ASSERT(route->mRefCount > 0);
4534 --route->mRefCount;
4535 if (route->mRefCount <= 0) {
4536 removeItem(session);
4537 }
4538 }
4539}
4540
4541int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4542{
4543 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4544 return route != 0 ? ++(route->mActivityCount) : -1;
4545}
4546
4547int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4548{
4549 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4550 if (route != 0 && route->mActivityCount > 0) {
4551 return --(route->mActivityCount);
4552 } else {
4553 return -1;
4554 }
4555}
4556
4557void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4558 ALOGI("%s ----", caption);
4559 for(size_t index = 0; index < size(); index++) {
4560 valueAt(index)->log(" ");
4561 }
4562}
4563
Eric Laurente0720872014-03-11 09:30:41 -07004564void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004565{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004566 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004567 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004568 sp<DeviceDescriptor> defaultInputDevice =
4569 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004570 mAvailableOutputDevices.add(mDefaultOutputDevice);
4571 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004572
4573 module = new HwModule("primary");
4574
Eric Laurent322b4d22015-04-03 15:57:54 -07004575 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4576 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004577 profile->mSamplingRates.add(44100);
4578 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4579 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004580 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004581 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4582 module->mOutputProfiles.add(profile);
4583
Eric Laurent322b4d22015-04-03 15:57:54 -07004584 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4585 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004586 profile->mSamplingRates.add(8000);
4587 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4588 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004589 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004590 module->mInputProfiles.add(profile);
4591
4592 mHwModules.add(module);
4593}
4594
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004595audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4596{
4597 // flags to stream type mapping
4598 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4599 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4600 }
4601 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4602 return AUDIO_STREAM_BLUETOOTH_SCO;
4603 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004604 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4605 return AUDIO_STREAM_TTS;
4606 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004607
4608 // usage to stream type mapping
4609 switch (attr->usage) {
4610 case AUDIO_USAGE_MEDIA:
4611 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004612 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4613 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004614 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004615 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4616 return AUDIO_STREAM_ALARM;
4617 }
4618 if (isStreamActive(AUDIO_STREAM_RING)) {
4619 return AUDIO_STREAM_RING;
4620 }
4621 if (isInCall()) {
4622 return AUDIO_STREAM_VOICE_CALL;
4623 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004624 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004625 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4626 return AUDIO_STREAM_SYSTEM;
4627 case AUDIO_USAGE_VOICE_COMMUNICATION:
4628 return AUDIO_STREAM_VOICE_CALL;
4629
4630 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4631 return AUDIO_STREAM_DTMF;
4632
4633 case AUDIO_USAGE_ALARM:
4634 return AUDIO_STREAM_ALARM;
4635 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4636 return AUDIO_STREAM_RING;
4637
4638 case AUDIO_USAGE_NOTIFICATION:
4639 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4640 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4641 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4642 case AUDIO_USAGE_NOTIFICATION_EVENT:
4643 return AUDIO_STREAM_NOTIFICATION;
4644
4645 case AUDIO_USAGE_UNKNOWN:
4646 default:
4647 return AUDIO_STREAM_MUSIC;
4648 }
4649}
Eric Laurente83b55d2014-11-14 10:06:21 -08004650
François Gaffie53615e22015-03-19 09:24:12 +01004651bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4652{
Eric Laurente83b55d2014-11-14 10:06:21 -08004653 // has flags that map to a strategy?
4654 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4655 return true;
4656 }
4657
4658 // has known usage?
4659 switch (paa->usage) {
4660 case AUDIO_USAGE_UNKNOWN:
4661 case AUDIO_USAGE_MEDIA:
4662 case AUDIO_USAGE_VOICE_COMMUNICATION:
4663 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4664 case AUDIO_USAGE_ALARM:
4665 case AUDIO_USAGE_NOTIFICATION:
4666 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4667 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4668 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4669 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4670 case AUDIO_USAGE_NOTIFICATION_EVENT:
4671 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4672 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4673 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4674 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004675 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004676 break;
4677 default:
4678 return false;
4679 }
4680 return true;
4681}
4682
François Gaffiead3183e2015-03-18 16:55:35 +01004683bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4684 routing_strategy strategy, uint32_t inPastMs,
4685 nsecs_t sysTime) const
4686{
4687 if ((sysTime == 0) && (inPastMs != 0)) {
4688 sysTime = systemTime();
4689 }
4690 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4691 if (i == AUDIO_STREAM_PATCH) {
4692 continue;
4693 }
4694 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4695 (NUM_STRATEGIES == strategy)) &&
4696 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4697 return true;
4698 }
4699 }
4700 return false;
4701}
4702
François Gaffie2110e042015-03-24 08:41:51 +01004703audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4704{
4705 return mEngine->getForceUse(usage);
4706}
4707
4708bool AudioPolicyManager::isInCall()
4709{
4710 return isStateInCall(mEngine->getPhoneState());
4711}
4712
4713bool AudioPolicyManager::isStateInCall(int state)
4714{
4715 return is_state_in_call(state);
4716}
4717
Eric Laurente552edb2014-03-10 17:42:56 -07004718}; // namespace android