blob: dfb477dc80c21e15f86b3ccd165019160755e12b [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700216 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Paul McLean9080a4c2015-06-18 08:24:02 -0700250 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
405 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
406 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
407 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
408 status);
409 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100410 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 mCallTxPatch->mAfPatchHandle = afPatchHandle;
412 mCallTxPatch->mUid = mUidCached;
413 }
414 }
415}
416
Eric Laurente0720872014-03-11 09:30:41 -0700417void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700418{
419 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100420 // store previous phone state for management of sonification strategy below
421 int oldState = mEngine->getPhoneState();
422
423 if (mEngine->setPhoneState(state) != NO_ERROR) {
424 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700425 return;
426 }
François Gaffie2110e042015-03-24 08:41:51 +0100427 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700428 // if leaving call state, handle special case of active streams
429 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700430 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700431 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (stream == AUDIO_STREAM_PATCH) {
434 continue;
435 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700436 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800438
Eric Laurent63dea1d2015-07-02 17:10:28 -0700439 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800440 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 }
442
François Gaffie2110e042015-03-24 08:41:51 +0100443 /**
444 * Switching to or from incall state or switching between telephony and VoIP lead to force
445 * routing command.
446 */
447 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
448 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700449
450 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700451 checkA2dpSuspend();
452 checkOutputForAllStrategies();
453 updateDevicesAndOutputs();
454
Eric Laurente552edb2014-03-10 17:42:56 -0700455 int delayMs = 0;
456 if (isStateInCall(state)) {
457 nsecs_t sysTime = systemTime();
458 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700459 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // mute media and sonification strategies and delay device switch by the largest
461 // latency of any output where either strategy is active.
462 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100463 if ((isStrategyActive(desc, STRATEGY_MEDIA,
464 SONIFICATION_HEADSET_MUSIC_DELAY,
465 sysTime) ||
466 isStrategyActive(desc, STRATEGY_SONIFICATION,
467 SONIFICATION_HEADSET_MUSIC_DELAY,
468 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700469 (delayMs < (int)desc->latency()*2)) {
470 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700472 setStrategyMute(STRATEGY_MEDIA, true, desc);
473 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700474 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700475 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
476 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700477 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
478 }
479 }
480
Eric Laurent87ffa392015-05-22 10:32:38 -0700481 if (hasPrimaryOutput()) {
482 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
483 // the device returned is not necessarily reachable via this output
484 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
485 // force routing command to audio hardware when ending call
486 // even if no device change is needed
487 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
488 rxDevice = mPrimaryOutput->device();
489 }
Eric Laurente552edb2014-03-10 17:42:56 -0700490
Eric Laurent87ffa392015-05-22 10:32:38 -0700491 if (state == AUDIO_MODE_IN_CALL) {
492 updateCallRouting(rxDevice, delayMs);
493 } else if (oldState == AUDIO_MODE_IN_CALL) {
494 if (mCallRxPatch != 0) {
495 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
496 mCallRxPatch.clear();
497 }
498 if (mCallTxPatch != 0) {
499 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
500 mCallTxPatch.clear();
501 }
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 } else {
504 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700506 }
Eric Laurente552edb2014-03-10 17:42:56 -0700507 // if entering in call state, handle special case of active streams
508 // pertaining to sonification strategy see handleIncallSonification()
509 if (isStateInCall(state)) {
510 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800512 if (stream == AUDIO_STREAM_PATCH) {
513 continue;
514 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700515 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700517
518 // force reevaluating accessibility routing when call starts
519 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700520 }
521
522 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700523 if (state == AUDIO_MODE_RINGTONE &&
524 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700525 mLimitRingtoneVolume = true;
526 } else {
527 mLimitRingtoneVolume = false;
528 }
529}
530
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700531audio_mode_t AudioPolicyManager::getPhoneState() {
532 return mEngine->getPhoneState();
533}
534
Eric Laurente0720872014-03-11 09:30:41 -0700535void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700536 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700537{
François Gaffie2110e042015-03-24 08:41:51 +0100538 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700539
François Gaffie2110e042015-03-24 08:41:51 +0100540 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
541 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
542 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700543 }
François Gaffie2110e042015-03-24 08:41:51 +0100544 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
545 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
546 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700547
548 // check for device and output changes triggered by new force usage
549 checkA2dpSuspend();
550 checkOutputForAllStrategies();
551 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700552 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700553 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
554 updateCallRouting(newDevice);
555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
558 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
559 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
560 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700561 }
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700563 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565 }
566
François Gaffie53615e22015-03-19 09:24:12 +0100567 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700568 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700569 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700570 }
571
572}
573
Eric Laurente0720872014-03-11 09:30:41 -0700574void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700575{
576 ALOGV("setSystemProperty() property %s, value %s", property, value);
577}
578
579// Find a direct output profile compatible with the parameters passed, even if the input flags do
580// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800581sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700582 audio_devices_t device,
583 uint32_t samplingRate,
584 audio_format_t format,
585 audio_channel_mask_t channelMask,
586 audio_output_flags_t flags)
587{
Eric Laurent861a6282015-05-18 15:40:16 -0700588 // only retain flags that will drive the direct output profile selection
589 // if explicitly requested
590 static const uint32_t kRelevantFlags =
591 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
592 flags =
593 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
594
595 sp<IOProfile> profile;
596
Eric Laurente552edb2014-03-10 17:42:56 -0700597 for (size_t i = 0; i < mHwModules.size(); i++) {
598 if (mHwModules[i]->mHandle == 0) {
599 continue;
600 }
601 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700602 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
603 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700604 samplingRate, NULL /*updatedSamplingRate*/,
605 format, NULL /*updatedFormat*/,
606 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700607 flags)) {
608 continue;
609 }
610 // reject profiles not corresponding to a device currently available
611 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
612 continue;
613 }
614 // if several profiles are compatible, give priority to one with offload capability
615 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
616 continue;
617 }
618 profile = curProfile;
619 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
620 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700621 }
Eric Laurente552edb2014-03-10 17:42:56 -0700622 }
623 }
Eric Laurent861a6282015-05-18 15:40:16 -0700624 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700625}
626
Eric Laurente0720872014-03-11 09:30:41 -0700627audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100628 uint32_t samplingRate,
629 audio_format_t format,
630 audio_channel_mask_t channelMask,
631 audio_output_flags_t flags,
632 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700633{
Eric Laurent3b73df72014-03-11 09:06:29 -0700634 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700635 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
636 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
637 device, stream, samplingRate, format, channelMask, flags);
638
Eric Laurente83b55d2014-11-14 10:06:21 -0800639 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
640 stream, samplingRate,format, channelMask,
641 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700642}
643
Eric Laurente83b55d2014-11-14 10:06:21 -0800644status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
645 audio_io_handle_t *output,
646 audio_session_t session,
647 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700648 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800649 uint32_t samplingRate,
650 audio_format_t format,
651 audio_channel_mask_t channelMask,
652 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700653 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800654 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700655{
Eric Laurente83b55d2014-11-14 10:06:21 -0800656 audio_attributes_t attributes;
657 if (attr != NULL) {
658 if (!isValidAttributes(attr)) {
659 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
660 attr->usage, attr->content_type, attr->flags,
661 attr->tags);
662 return BAD_VALUE;
663 }
664 attributes = *attr;
665 } else {
666 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
667 ALOGE("getOutputForAttr(): invalid stream type");
668 return BAD_VALUE;
669 }
670 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700671 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700672 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100673 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
674 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
675 if (!audio_is_linear_pcm(format)) {
676 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800677 }
François Gaffie036e1e92015-03-19 10:16:24 +0100678 *stream = streamTypefromAttributesInt(&attributes);
679 *output = desc->mIoHandle;
680 ALOGV("getOutputForAttr() returns output %d", *output);
681 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800682 }
683 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
684 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
685 return BAD_VALUE;
686 }
687
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700688 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
689 " session %d selectedDeviceId %d",
690 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
691 session, selectedDeviceId);
692
693 *stream = streamTypefromAttributesInt(&attributes);
694
695 // Explicit routing?
696 sp<DeviceDescriptor> deviceDesc;
697 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
698 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
699 deviceDesc = mAvailableOutputDevices[i];
700 break;
701 }
702 }
703 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700704
Eric Laurente83b55d2014-11-14 10:06:21 -0800705 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700706 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700707
Eric Laurente83b55d2014-11-14 10:06:21 -0800708 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700709 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
710 }
711
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700712 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700713 device, samplingRate, format, channelMask, flags);
714
Eric Laurente83b55d2014-11-14 10:06:21 -0800715 *output = getOutputForDevice(device, session, *stream,
716 samplingRate, format, channelMask,
717 flags, offloadInfo);
718 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700719 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800720 return INVALID_OPERATION;
721 }
Paul McLeanaa981192015-03-21 09:55:15 -0700722
Eric Laurente83b55d2014-11-14 10:06:21 -0800723 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700724}
725
726audio_io_handle_t AudioPolicyManager::getOutputForDevice(
727 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800728 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700729 audio_stream_type_t stream,
730 uint32_t samplingRate,
731 audio_format_t format,
732 audio_channel_mask_t channelMask,
733 audio_output_flags_t flags,
734 const audio_offload_info_t *offloadInfo)
735{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700736 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700737 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700738 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700739
Eric Laurente552edb2014-03-10 17:42:56 -0700740#ifdef AUDIO_POLICY_TEST
741 if (mCurOutput != 0) {
742 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
743 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
744
745 if (mTestOutputs[mCurOutput] == 0) {
746 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700747 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
748 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700749 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700750 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700751 outputDesc->mFlags =
752 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700753 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700754 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
755 config.sample_rate = mTestSamplingRate;
756 config.channel_mask = mTestChannels;
757 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700758 if (offloadInfo != NULL) {
759 config.offload_info = *offloadInfo;
760 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700761 status = mpClientInterface->openOutput(0,
762 &mTestOutputs[mCurOutput],
763 &config,
764 &outputDesc->mDevice,
765 String8(""),
766 &outputDesc->mLatency,
767 outputDesc->mFlags);
768 if (status == NO_ERROR) {
769 outputDesc->mSamplingRate = config.sample_rate;
770 outputDesc->mFormat = config.format;
771 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700772 AudioParameter outputCmd = AudioParameter();
773 outputCmd.addInt(String8("set_id"),mCurOutput);
774 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
775 addOutput(mTestOutputs[mCurOutput], outputDesc);
776 }
777 }
778 return mTestOutputs[mCurOutput];
779 }
780#endif //AUDIO_POLICY_TEST
781
782 // open a direct output if required by specified parameters
783 //force direct flag if offload flag is set: offloading implies a direct output stream
784 // and all common behaviors are driven by checking only the direct flag
785 // this should normally be set appropriately in the policy configuration file
786 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700787 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700788 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700789 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
790 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
791 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800792 // only allow deep buffering for music stream type
793 if (stream != AUDIO_STREAM_MUSIC) {
794 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700795 } else if (/* stream == AUDIO_STREAM_MUSIC && */
796 flags == AUDIO_OUTPUT_FLAG_NONE &&
797 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
798 // use DEEP_BUFFER as default output for music stream type
799 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800800 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700801 if (stream == AUDIO_STREAM_TTS) {
802 flags = AUDIO_OUTPUT_FLAG_TTS;
803 }
Eric Laurente552edb2014-03-10 17:42:56 -0700804
Eric Laurentb732cf52014-09-24 19:08:21 -0700805 sp<IOProfile> profile;
806
807 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700808 // and not explicitly requested
809 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
810 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700811 audio_channel_count_from_out_mask(channelMask) <= 2) {
812 goto non_direct_output;
813 }
814
Eric Laurente552edb2014-03-10 17:42:56 -0700815 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
816 // creating an offloaded track and tearing it down immediately after start when audioflinger
817 // detects there is an active non offloadable effect.
818 // FIXME: We should check the audio session here but we do not have it in this context.
819 // This may prevent offloading in rare situations where effects are left active by apps
820 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700821
Eric Laurente552edb2014-03-10 17:42:56 -0700822 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100823 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700824 profile = getProfileForDirectOutput(device,
825 samplingRate,
826 format,
827 channelMask,
828 (audio_output_flags_t)flags);
829 }
830
Eric Laurent1c333e22014-05-20 10:48:17 -0700831 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700832 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700833
834 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700835 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700836 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
837 outputDesc = desc;
838 // reuse direct output if currently open and configured with same parameters
839 if ((samplingRate == outputDesc->mSamplingRate) &&
840 (format == outputDesc->mFormat) &&
841 (channelMask == outputDesc->mChannelMask)) {
842 outputDesc->mDirectOpenCount++;
843 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
844 return mOutputs.keyAt(i);
845 }
846 }
847 }
848 // close direct output if currently open and configured with different parameters
849 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700850 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700851 }
Eric Laurent861a6282015-05-18 15:40:16 -0700852
853 // if the selected profile is offloaded and no offload info was specified,
854 // create a default one
855 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
856 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
857 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
858 defaultOffloadInfo.sample_rate = samplingRate;
859 defaultOffloadInfo.channel_mask = channelMask;
860 defaultOffloadInfo.format = format;
861 defaultOffloadInfo.stream_type = stream;
862 defaultOffloadInfo.bit_rate = 0;
863 defaultOffloadInfo.duration_us = -1;
864 defaultOffloadInfo.has_video = true; // conservative
865 defaultOffloadInfo.is_streaming = true; // likely
866 offloadInfo = &defaultOffloadInfo;
867 }
868
Eric Laurentc75307b2015-03-17 15:29:32 -0700869 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700870 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700871 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700872 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700873 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
874 config.sample_rate = samplingRate;
875 config.channel_mask = channelMask;
876 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700877 if (offloadInfo != NULL) {
878 config.offload_info = *offloadInfo;
879 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700880 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700881 &output,
882 &config,
883 &outputDesc->mDevice,
884 String8(""),
885 &outputDesc->mLatency,
886 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700887
888 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700889 if (status != NO_ERROR ||
890 (samplingRate != 0 && samplingRate != config.sample_rate) ||
891 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
892 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700893 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
894 "format %d %d, channelMask %04x %04x", output, samplingRate,
895 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
896 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700897 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700898 mpClientInterface->closeOutput(output);
899 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800900 // fall back to mixer output if possible when the direct output could not be open
901 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
902 goto non_direct_output;
903 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700904 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700905 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700906 outputDesc->mSamplingRate = config.sample_rate;
907 outputDesc->mChannelMask = config.channel_mask;
908 outputDesc->mFormat = config.format;
909 outputDesc->mRefCount[stream] = 0;
910 outputDesc->mStopTime[stream] = 0;
911 outputDesc->mDirectOpenCount = 1;
912
Eric Laurente552edb2014-03-10 17:42:56 -0700913 audio_io_handle_t srcOutput = getOutputForEffect();
914 addOutput(output, outputDesc);
915 audio_io_handle_t dstOutput = getOutputForEffect();
916 if (dstOutput == output) {
917 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
918 }
919 mPreviousOutputs = mOutputs;
920 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700921 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700922 return output;
923 }
924
Eric Laurentb732cf52014-09-24 19:08:21 -0700925non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700926 // ignoring channel mask due to downmix capability in mixer
927
928 // open a non direct output
929
930 // for non direct outputs, only PCM is supported
931 if (audio_is_linear_pcm(format)) {
932 // get which output is suitable for the specified stream. The actual
933 // routing change will happen when startOutput() will be called
934 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
935
Eric Laurent8838a382014-09-08 16:44:28 -0700936 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
937 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
938 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700939 }
940 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
941 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
942
Paul McLeanaa981192015-03-21 09:55:15 -0700943 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700944
945 return output;
946}
947
Eric Laurente0720872014-03-11 09:30:41 -0700948audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700949 audio_output_flags_t flags,
950 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700951{
952 // select one output among several that provide a path to a particular device or set of
953 // devices (the list was previously build by getOutputsForDevice()).
954 // The priority is as follows:
955 // 1: the output with the highest number of requested policy flags
956 // 2: the primary output
957 // 3: the first output in the list
958
959 if (outputs.size() == 0) {
960 return 0;
961 }
962 if (outputs.size() == 1) {
963 return outputs[0];
964 }
965
966 int maxCommonFlags = 0;
967 audio_io_handle_t outputFlags = 0;
968 audio_io_handle_t outputPrimary = 0;
969
970 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700971 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700972 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700973 // if a valid format is specified, skip output if not compatible
974 if (format != AUDIO_FORMAT_INVALID) {
975 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
976 if (format != outputDesc->mFormat) {
977 continue;
978 }
979 } else if (!audio_is_linear_pcm(format)) {
980 continue;
981 }
982 }
983
Eric Laurent3b73df72014-03-11 09:06:29 -0700984 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700985 if (commonFlags > maxCommonFlags) {
986 outputFlags = outputs[i];
987 maxCommonFlags = commonFlags;
988 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
989 }
990 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
991 outputPrimary = outputs[i];
992 }
993 }
994 }
995
996 if (outputFlags != 0) {
997 return outputFlags;
998 }
999 if (outputPrimary != 0) {
1000 return outputPrimary;
1001 }
1002
1003 return outputs[0];
1004}
1005
Eric Laurente0720872014-03-11 09:30:41 -07001006status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001007 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001008 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001009{
Paul McLeanaa981192015-03-21 09:55:15 -07001010 ALOGV("startOutput() output %d, stream %d, session %d",
1011 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001012 ssize_t index = mOutputs.indexOfKey(output);
1013 if (index < 0) {
1014 ALOGW("startOutput() unknown output %d", output);
1015 return BAD_VALUE;
1016 }
1017
Eric Laurentc75307b2015-03-17 15:29:32 -07001018 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1019
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001020 // Routing?
1021 mOutputRoutes.incRouteActivity(session);
1022
Eric Laurentc75307b2015-03-17 15:29:32 -07001023 audio_devices_t newDevice;
1024 if (outputDesc->mPolicyMix != NULL) {
1025 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001026 } else if (mOutputRoutes.hasRouteChanged(session)) {
1027 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001028 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001029 } else {
1030 newDevice = AUDIO_DEVICE_NONE;
1031 }
1032
1033 uint32_t delayMs = 0;
1034
Eric Laurentc75307b2015-03-17 15:29:32 -07001035 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1036
1037 if (status != NO_ERROR) {
1038 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001039 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001040 }
1041 // Automatically enable the remote submix input when output is started on a re routing mix
1042 // of type MIX_TYPE_RECORDERS
1043 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1044 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1045 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1046 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1047 outputDesc->mPolicyMix->mRegistrationId,
1048 "remote-submix");
1049 }
1050
1051 if (delayMs != 0) {
1052 usleep(delayMs * 1000);
1053 }
1054
1055 return status;
1056}
1057
1058status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1059 audio_stream_type_t stream,
1060 audio_devices_t device,
1061 uint32_t *delayMs)
1062{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001063 // cannot start playback of STREAM_TTS if any other output is being used
1064 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001065
1066 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001067 if (stream == AUDIO_STREAM_TTS) {
1068 ALOGV("\t found BEACON stream");
Eric Laurent9459fb02015-08-12 18:36:32 -07001069 if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001070 return INVALID_OPERATION;
1071 } else {
1072 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1073 }
1074 } else {
1075 // some playback other than beacon starts
1076 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1077 }
1078
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001079 // check active before incrementing usage count
1080 bool force = !outputDesc->isActive();
1081
Eric Laurente552edb2014-03-10 17:42:56 -07001082 // increment usage count for this stream on the requested output:
1083 // NOTE that the usage count is the same for duplicated output and hardware output which is
1084 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1085 outputDesc->changeRefCount(stream, 1);
1086
Eric Laurent493404d2015-04-21 15:07:36 -07001087 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001088 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001089 if (device == AUDIO_DEVICE_NONE) {
1090 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001091 }
Eric Laurente552edb2014-03-10 17:42:56 -07001092 routing_strategy strategy = getStrategy(stream);
1093 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001094 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1095 (beaconMuteLatency > 0);
1096 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001097 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001098 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001099 if (desc != outputDesc) {
1100 // force a device change if any other output is managed by the same hw
1101 // module and has a current device selection that differs from selected device.
1102 // In this case, the audio HAL must receive the new device selection so that it can
1103 // change the device currently selected by the other active output.
1104 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001105 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001106 force = true;
1107 }
1108 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001109 // a notification so that audio focus effect can propagate, or that a mute/unmute
1110 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001111 uint32_t latency = desc->latency();
1112 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1113 waitMs = latency;
1114 }
1115 }
1116 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001117 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001118
1119 // handle special case for sonification while in call
1120 if (isInCall()) {
1121 handleIncallSonification(stream, true, false);
1122 }
1123
1124 // apply volume rules for current stream and device if necessary
1125 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001126 mStreams.valueFor(stream).getVolumeIndex(device),
1127 outputDesc,
1128 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001129
1130 // update the outputs if starting an output with a stream that can affect notification
1131 // routing
1132 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001133
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001134 // force reevaluating accessibility routing when ringtone or alarm starts
1135 if (strategy == STRATEGY_SONIFICATION) {
1136 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1137 }
Eric Laurente552edb2014-03-10 17:42:56 -07001138 }
1139 return NO_ERROR;
1140}
1141
1142
Eric Laurente0720872014-03-11 09:30:41 -07001143status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001144 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001145 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001146{
1147 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1148 ssize_t index = mOutputs.indexOfKey(output);
1149 if (index < 0) {
1150 ALOGW("stopOutput() unknown output %d", output);
1151 return BAD_VALUE;
1152 }
1153
Eric Laurentc75307b2015-03-17 15:29:32 -07001154 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001155
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 if (outputDesc->mRefCount[stream] == 1) {
1157 // Automatically disable the remote submix input when output is stopped on a
1158 // re routing mix of type MIX_TYPE_RECORDERS
1159 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1160 outputDesc->mPolicyMix != NULL &&
1161 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1162 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1163 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1164 outputDesc->mPolicyMix->mRegistrationId,
1165 "remote-submix");
1166 }
1167 }
1168
1169 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001170 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001171 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001172 int activityCount = mOutputRoutes.decRouteActivity(session);
1173 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1174
1175 if (forceDeviceUpdate) {
1176 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1177 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001178 }
1179
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001180 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001181}
1182
1183status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001184 audio_stream_type_t stream,
1185 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001186{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001187 // always handle stream stop, check which stream type is stopping
1188 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1189
Eric Laurente552edb2014-03-10 17:42:56 -07001190 // handle special case for sonification while in call
1191 if (isInCall()) {
1192 handleIncallSonification(stream, false, false);
1193 }
1194
1195 if (outputDesc->mRefCount[stream] > 0) {
1196 // decrement usage count of this stream on the output
1197 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001198
Eric Laurente552edb2014-03-10 17:42:56 -07001199 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001200 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001201 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001202 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001203 // delay the device switch by twice the latency because stopOutput() is executed when
1204 // the track stop() command is received and at that time the audio track buffer can
1205 // still contain data that needs to be drained. The latency only covers the audio HAL
1206 // and kernel buffers. Also the latency does not always include additional delay in the
1207 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001209
1210 // force restoring the device selection on other active outputs if it differs from the
1211 // one being selected for this output
1212 for (size_t i = 0; i < mOutputs.size(); i++) {
1213 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001214 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001215 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001216 desc->isActive() &&
1217 outputDesc->sharesHwModuleWith(desc) &&
1218 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001219 setOutputDevice(desc,
1220 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001221 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001222 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001223 }
1224 }
1225 // update the outputs if stopping one with a stream that can affect notification routing
1226 handleNotificationRoutingForStream(stream);
1227 }
1228 return NO_ERROR;
1229 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001230 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001231 return INVALID_OPERATION;
1232 }
1233}
1234
Eric Laurente83b55d2014-11-14 10:06:21 -08001235void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001236 audio_stream_type_t stream __unused,
1237 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001238{
1239 ALOGV("releaseOutput() %d", output);
1240 ssize_t index = mOutputs.indexOfKey(output);
1241 if (index < 0) {
1242 ALOGW("releaseOutput() releasing unknown output %d", output);
1243 return;
1244 }
1245
1246#ifdef AUDIO_POLICY_TEST
1247 int testIndex = testOutputIndex(output);
1248 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001249 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001250 if (outputDesc->isActive()) {
1251 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001252 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001253 mTestOutputs[testIndex] = 0;
1254 }
1255 return;
1256 }
1257#endif //AUDIO_POLICY_TEST
1258
Paul McLeanaa981192015-03-21 09:55:15 -07001259 // Routing
1260 mOutputRoutes.removeRoute(session);
1261
Eric Laurentc75307b2015-03-17 15:29:32 -07001262 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001263 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001264 if (desc->mDirectOpenCount <= 0) {
1265 ALOGW("releaseOutput() invalid open count %d for output %d",
1266 desc->mDirectOpenCount, output);
1267 return;
1268 }
1269 if (--desc->mDirectOpenCount == 0) {
1270 closeOutput(output);
1271 // If effects where present on the output, audioflinger moved them to the primary
1272 // output by default: move them back to the appropriate output.
1273 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001274 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001275 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1276 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001277 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001278 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001279 }
1280 }
1281}
1282
1283
Eric Laurentcaf7f482014-11-25 17:50:47 -08001284status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1285 audio_io_handle_t *input,
1286 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001287 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001288 uint32_t samplingRate,
1289 audio_format_t format,
1290 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001291 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001292 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001293 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001294{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001295 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1296 "session %d, flags %#x",
1297 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001298
Eric Laurentcaf7f482014-11-25 17:50:47 -08001299 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001300 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001301 audio_devices_t device;
1302 // handle legacy remote submix case where the address was not always specified
1303 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001304 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001305 audio_source_t inputSource = attr->source;
1306 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001307 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001308
Eric Laurentc447ded2015-01-06 08:47:05 -08001309 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1310 inputSource = AUDIO_SOURCE_MIC;
1311 }
1312 halInputSource = inputSource;
1313
Paul McLean466dc8e2015-04-17 13:15:36 -06001314 // Explicit routing?
1315 sp<DeviceDescriptor> deviceDesc;
1316 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1317 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1318 deviceDesc = mAvailableInputDevices[i];
1319 break;
1320 }
1321 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001322 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001323
Eric Laurentc447ded2015-01-06 08:47:05 -08001324 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001325 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001326 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001327 if (ret != NO_ERROR) {
1328 return ret;
1329 }
1330 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001331 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1332 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001333 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001334 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001335 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001336 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001337 return BAD_VALUE;
1338 }
Eric Laurentc722f302014-12-10 11:21:49 -08001339 if (policyMix != NULL) {
1340 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001341 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1342 // there is an external policy, but this input is attached to a mix of recorders,
1343 // meaning it receives audio injected into the framework, so the recorder doesn't
1344 // know about it and is therefore considered "legacy"
1345 *inputType = API_INPUT_LEGACY;
1346 } else {
1347 // recording a mix of players defined by an external policy, we're rerouting for
1348 // an external policy
1349 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1350 }
Eric Laurentc722f302014-12-10 11:21:49 -08001351 } else if (audio_is_remote_submix_device(device)) {
1352 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001353 *inputType = API_INPUT_MIX_CAPTURE;
Eric Laurent82db2692015-08-07 13:59:42 -07001354 } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
1355 *inputType = API_INPUT_TELEPHONY_RX;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001356 } else {
1357 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001358 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001359 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001360 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001361 case AUDIO_SOURCE_VOICE_UPLINK:
1362 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1363 break;
1364 case AUDIO_SOURCE_VOICE_DOWNLINK:
1365 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1366 break;
1367 case AUDIO_SOURCE_VOICE_CALL:
1368 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1369 break;
1370 default:
1371 break;
1372 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001373 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001374 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1375 if (index >= 0) {
1376 *input = mSoundTriggerSessions.valueFor(session);
1377 isSoundTrigger = true;
1378 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1379 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1380 } else {
1381 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1382 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001383 }
1384 }
1385
Andy Hungf129b032015-04-07 13:45:50 -07001386 // find a compatible input profile (not necessarily identical in parameters)
1387 sp<IOProfile> profile;
1388 // samplingRate and flags may be updated by getInputProfile
1389 uint32_t profileSamplingRate = samplingRate;
1390 audio_format_t profileFormat = format;
1391 audio_channel_mask_t profileChannelMask = channelMask;
1392 audio_input_flags_t profileFlags = flags;
1393 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001394 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001395 profileSamplingRate, profileFormat, profileChannelMask,
1396 profileFlags);
1397 if (profile != 0) {
1398 break; // success
1399 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1400 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1401 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001402 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1403 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001404 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001405 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001406 }
Eric Laurente552edb2014-03-10 17:42:56 -07001407 }
1408
Eric Laurent322b4d22015-04-03 15:57:54 -07001409 if (profile->getModuleHandle() == 0) {
1410 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001412 }
1413
1414 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001415 config.sample_rate = profileSamplingRate;
1416 config.channel_mask = profileChannelMask;
1417 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001418
Eric Laurent322b4d22015-04-03 15:57:54 -07001419 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001420 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001421 &config,
1422 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001423 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001424 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001425 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001426
1427 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001428 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001429 (profileSamplingRate != config.sample_rate) ||
1430 (profileFormat != config.format) ||
1431 (profileChannelMask != config.channel_mask)) {
1432 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1433 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001434 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001435 if (*input != AUDIO_IO_HANDLE_NONE) {
1436 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001437 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001438 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001439 }
1440
Eric Laurent1f2f2232014-06-02 12:01:23 -07001441 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001442 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001443 inputDesc->mRefCount = 0;
1444 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001445 inputDesc->mSamplingRate = profileSamplingRate;
1446 inputDesc->mFormat = profileFormat;
1447 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001448 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001449 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001450 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001451 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001452
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001453 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001454
Eric Laurentcaf7f482014-11-25 17:50:47 -08001455 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001456 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001457
Eric Laurentcaf7f482014-11-25 17:50:47 -08001458 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001459}
1460
Eric Laurent4dc68062014-07-28 17:26:49 -07001461status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1462 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001463{
1464 ALOGV("startInput() input %d", input);
1465 ssize_t index = mInputs.indexOfKey(input);
1466 if (index < 0) {
1467 ALOGW("startInput() unknown input %d", input);
1468 return BAD_VALUE;
1469 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001470 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001471
Eric Laurentc722f302014-12-10 11:21:49 -08001472 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001473 if (index < 0) {
1474 ALOGW("startInput() unknown session %d on input %d", session, input);
1475 return BAD_VALUE;
1476 }
1477
Glenn Kasten74a8e252014-07-24 14:09:55 -07001478 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001479 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001480
1481 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001482 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001483 if (activeInput != 0 && activeInput != input) {
1484
1485 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1486 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001487 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001488 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001489 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001490 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1491 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001492 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001493 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001494 return INVALID_OPERATION;
1495 }
1496 }
1497 }
1498
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001499 // Routing?
1500 mInputRoutes.incRouteActivity(session);
1501
Paul McLean466dc8e2015-04-17 13:15:36 -06001502 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001503 // if input maps to a dynamic policy with an activity listener, notify of state change
1504 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001505 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001506 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1507 MIX_STATE_MIXING);
1508 }
1509
François Gaffie53615e22015-03-19 09:24:12 +01001510 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001511 SoundTrigger::setCaptureState(true);
1512 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001513 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001514
Eric Laurentc722f302014-12-10 11:21:49 -08001515 // automatically enable the remote submix output when input is started if not
1516 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001517 // For remote submix (a virtual device), we open only one input per capture request.
1518 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001519 String8 address = String8("");
1520 if (inputDesc->mPolicyMix == NULL) {
1521 address = String8("0");
1522 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1523 address = inputDesc->mPolicyMix->mRegistrationId;
1524 }
1525 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001526 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001527 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001528 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001529 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001530 }
Eric Laurente552edb2014-03-10 17:42:56 -07001531 }
1532
Eric Laurente552edb2014-03-10 17:42:56 -07001533 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1534
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001535 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001536 return NO_ERROR;
1537}
1538
Eric Laurent4dc68062014-07-28 17:26:49 -07001539status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1540 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001541{
1542 ALOGV("stopInput() input %d", input);
1543 ssize_t index = mInputs.indexOfKey(input);
1544 if (index < 0) {
1545 ALOGW("stopInput() unknown input %d", input);
1546 return BAD_VALUE;
1547 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001548 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001549
Eric Laurentc722f302014-12-10 11:21:49 -08001550 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001551 if (index < 0) {
1552 ALOGW("stopInput() unknown session %d on input %d", session, input);
1553 return BAD_VALUE;
1554 }
1555
Eric Laurente552edb2014-03-10 17:42:56 -07001556 if (inputDesc->mRefCount == 0) {
1557 ALOGW("stopInput() input %d already stopped", input);
1558 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001559 }
1560
1561 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001562
1563 // Routing?
1564 mInputRoutes.decRouteActivity(session);
1565
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001566 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001567 // if input maps to a dynamic policy with an activity listener, notify of state change
1568 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001569 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001570 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1571 MIX_STATE_IDLE);
1572 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001573
Eric Laurentc722f302014-12-10 11:21:49 -08001574 // automatically disable the remote submix output when input is stopped if not
1575 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001576 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001577 String8 address = String8("");
1578 if (inputDesc->mPolicyMix == NULL) {
1579 address = String8("0");
1580 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1581 address = inputDesc->mPolicyMix->mRegistrationId;
1582 }
1583 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001584 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001585 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001586 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001587 }
Eric Laurente552edb2014-03-10 17:42:56 -07001588 }
1589
Eric Laurent1c333e22014-05-20 10:48:17 -07001590 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001591
François Gaffie53615e22015-03-19 09:24:12 +01001592 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001593 SoundTrigger::setCaptureState(false);
1594 }
Eric Laurente552edb2014-03-10 17:42:56 -07001595 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001596 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001597}
1598
Eric Laurent4dc68062014-07-28 17:26:49 -07001599void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1600 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001601{
1602 ALOGV("releaseInput() %d", input);
1603 ssize_t index = mInputs.indexOfKey(input);
1604 if (index < 0) {
1605 ALOGW("releaseInput() releasing unknown input %d", input);
1606 return;
1607 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001608
1609 // Routing
1610 mInputRoutes.removeRoute(session);
1611
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001612 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1613 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001614
Eric Laurentc722f302014-12-10 11:21:49 -08001615 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001616 if (index < 0) {
1617 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1618 return;
1619 }
Eric Laurentc722f302014-12-10 11:21:49 -08001620 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001621 if (inputDesc->mOpenRefCount == 0) {
1622 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1623 return;
1624 }
1625 inputDesc->mOpenRefCount--;
1626 if (inputDesc->mOpenRefCount > 0) {
1627 ALOGV("releaseInput() exit > 0");
1628 return;
1629 }
1630
Eric Laurent05b90f82014-08-27 15:32:29 -07001631 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001632 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001633 ALOGV("releaseInput() exit");
1634}
1635
Eric Laurentd4692962014-05-05 18:13:44 -07001636void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001637 bool patchRemoved = false;
1638
Eric Laurentd4692962014-05-05 18:13:44 -07001639 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001640 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1641 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1642 if (patch_index >= 0) {
1643 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1644 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1645 mAudioPatches.removeItemsAt(patch_index);
1646 patchRemoved = true;
1647 }
Eric Laurentd4692962014-05-05 18:13:44 -07001648 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1649 }
1650 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001651 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001652
1653 if (patchRemoved) {
1654 mpClientInterface->onAudioPatchListUpdate();
1655 }
Eric Laurentd4692962014-05-05 18:13:44 -07001656}
1657
Eric Laurente0720872014-03-11 09:30:41 -07001658void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001659 int indexMin,
1660 int indexMax)
1661{
1662 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001663 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001664 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1665 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001666 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001667 }
Eric Laurente552edb2014-03-10 17:42:56 -07001668}
1669
Eric Laurente0720872014-03-11 09:30:41 -07001670status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001671 int index,
1672 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001673{
1674
Eric Laurentc75307b2015-03-17 15:29:32 -07001675 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1676 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001677 return BAD_VALUE;
1678 }
1679 if (!audio_is_output_device(device)) {
1680 return BAD_VALUE;
1681 }
1682
1683 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001684 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001685
1686 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1687 stream, device, index);
1688
1689 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1690 // clear all device specific values
1691 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001692 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001693 }
François Gaffiedfd74092015-03-19 12:10:59 +01001694 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001695
Eric Laurent31551f82014-10-10 18:21:56 -07001696 // update volume on all outputs whose current device is also selected by the same
1697 // strategy as the device specified by the caller
1698 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001699
1700
1701 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1702 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1703 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001704 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001705 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1706 }
1707 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1708 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001709 return NO_ERROR;
1710 }
Eric Laurente552edb2014-03-10 17:42:56 -07001711 status_t status = NO_ERROR;
1712 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001713 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1714 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001715 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001716 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001717 if (volStatus != NO_ERROR) {
1718 status = volStatus;
1719 }
1720 }
Jean-Michel Triviaf20bc22015-09-14 16:37:07 -07001721 if ((accessibilityDevice != AUDIO_DEVICE_NONE) &&
1722 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)))
1723 {
Eric Laurent223fd5c2014-11-11 13:43:36 -08001724 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001725 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001726 }
Eric Laurente552edb2014-03-10 17:42:56 -07001727 }
1728 return status;
1729}
1730
Eric Laurente0720872014-03-11 09:30:41 -07001731status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001732 int *index,
1733 audio_devices_t device)
1734{
1735 if (index == NULL) {
1736 return BAD_VALUE;
1737 }
1738 if (!audio_is_output_device(device)) {
1739 return BAD_VALUE;
1740 }
1741 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1742 // the strategy the stream belongs to.
1743 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1744 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1745 }
François Gaffiedfd74092015-03-19 12:10:59 +01001746 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001747
Eric Laurentc75307b2015-03-17 15:29:32 -07001748 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001749 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1750 return NO_ERROR;
1751}
1752
Eric Laurente0720872014-03-11 09:30:41 -07001753audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001754 const SortedVector<audio_io_handle_t>& outputs)
1755{
1756 // select one output among several suitable for global effects.
1757 // The priority is as follows:
1758 // 1: An offloaded output. If the effect ends up not being offloadable,
1759 // AudioFlinger will invalidate the track and the offloaded output
1760 // will be closed causing the effect to be moved to a PCM output.
1761 // 2: A deep buffer output
1762 // 3: the first output in the list
1763
1764 if (outputs.size() == 0) {
1765 return 0;
1766 }
1767
1768 audio_io_handle_t outputOffloaded = 0;
1769 audio_io_handle_t outputDeepBuffer = 0;
1770
1771 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001772 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001773 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001774 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1775 outputOffloaded = outputs[i];
1776 }
1777 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1778 outputDeepBuffer = outputs[i];
1779 }
1780 }
1781
1782 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1783 outputOffloaded, outputDeepBuffer);
1784 if (outputOffloaded != 0) {
1785 return outputOffloaded;
1786 }
1787 if (outputDeepBuffer != 0) {
1788 return outputDeepBuffer;
1789 }
1790
1791 return outputs[0];
1792}
1793
Eric Laurente0720872014-03-11 09:30:41 -07001794audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001795{
1796 // apply simple rule where global effects are attached to the same output as MUSIC streams
1797
Eric Laurent3b73df72014-03-11 09:06:29 -07001798 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001799 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1800 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1801
1802 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1803 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1804 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1805
1806 return output;
1807}
1808
Eric Laurente0720872014-03-11 09:30:41 -07001809status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001810 audio_io_handle_t io,
1811 uint32_t strategy,
1812 int session,
1813 int id)
1814{
1815 ssize_t index = mOutputs.indexOfKey(io);
1816 if (index < 0) {
1817 index = mInputs.indexOfKey(io);
1818 if (index < 0) {
1819 ALOGW("registerEffect() unknown io %d", io);
1820 return INVALID_OPERATION;
1821 }
1822 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001823 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001824}
1825
Eric Laurentc75307b2015-03-17 15:29:32 -07001826bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1827{
1828 return mOutputs.isStreamActive(stream, inPastMs);
1829}
1830
1831bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1832{
1833 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1834}
1835
Eric Laurente0720872014-03-11 09:30:41 -07001836bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001837{
1838 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001839 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001840 if (inputDescriptor->mRefCount == 0) {
1841 continue;
1842 }
1843 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001844 return true;
1845 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001846 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1847 // corresponds to an active capture triggered by a hardware hotword recognition
1848 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1849 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1850 // FIXME: we should not assume that the first session is the active one and keep
1851 // activity count per session. Same in startInput().
1852 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1853 if (index >= 0) {
1854 return true;
1855 }
1856 }
Eric Laurente552edb2014-03-10 17:42:56 -07001857 }
1858 return false;
1859}
1860
Eric Laurent275e8e92014-11-30 15:14:47 -08001861// Register a list of custom mixes with their attributes and format.
1862// When a mix is registered, corresponding input and output profiles are
1863// added to the remote submix hw module. The profile contains only the
1864// parameters (sampling rate, format...) specified by the mix.
1865// The corresponding input remote submix device is also connected.
1866//
1867// When a remote submix device is connected, the address is checked to select the
1868// appropriate profile and the corresponding input or output stream is opened.
1869//
1870// When capture starts, getInputForAttr() will:
1871// - 1 look for a mix matching the address passed in attribtutes tags if any
1872// - 2 if none found, getDeviceForInputSource() will:
1873// - 2.1 look for a mix matching the attributes source
1874// - 2.2 if none found, default to device selection by policy rules
1875// At this time, the corresponding output remote submix device is also connected
1876// and active playback use cases can be transferred to this mix if needed when reconnecting
1877// after AudioTracks are invalidated
1878//
1879// When playback starts, getOutputForAttr() will:
1880// - 1 look for a mix matching the address passed in attribtutes tags if any
1881// - 2 if none found, look for a mix matching the attributes usage
1882// - 3 if none found, default to device and output selection by policy rules.
1883
1884status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1885{
1886 sp<HwModule> module;
1887 for (size_t i = 0; i < mHwModules.size(); i++) {
1888 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1889 mHwModules[i]->mHandle != 0) {
1890 module = mHwModules[i];
1891 break;
1892 }
1893 }
1894
1895 if (module == 0) {
1896 return INVALID_OPERATION;
1897 }
1898
1899 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1900
1901 for (size_t i = 0; i < mixes.size(); i++) {
1902 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001903
1904 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001905 continue;
1906 }
1907 audio_config_t outputConfig = mixes[i].mFormat;
1908 audio_config_t inputConfig = mixes[i].mFormat;
1909 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1910 // stereo and let audio flinger do the channel conversion if needed.
1911 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1912 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1913 module->addOutputProfile(address, &outputConfig,
1914 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1915 module->addInputProfile(address, &inputConfig,
1916 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001917
Eric Laurentc722f302014-12-10 11:21:49 -08001918 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001919 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001920 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001921 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001922 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001923 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001924 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001925 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001926 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001927 }
1928 return NO_ERROR;
1929}
1930
1931status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1932{
1933 sp<HwModule> module;
1934 for (size_t i = 0; i < mHwModules.size(); i++) {
1935 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1936 mHwModules[i]->mHandle != 0) {
1937 module = mHwModules[i];
1938 break;
1939 }
1940 }
1941
1942 if (module == 0) {
1943 return INVALID_OPERATION;
1944 }
1945
1946 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1947
1948 for (size_t i = 0; i < mixes.size(); i++) {
1949 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001950
1951 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001952 continue;
1953 }
1954
Eric Laurentc722f302014-12-10 11:21:49 -08001955 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1956 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1957 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001958 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001959 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001960 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001961 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001962
1963 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1964 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1965 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001966 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001967 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001968 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001969 }
1970 module->removeOutputProfile(address);
1971 module->removeInputProfile(address);
1972 }
1973 return NO_ERROR;
1974}
1975
Eric Laurente552edb2014-03-10 17:42:56 -07001976
Eric Laurente0720872014-03-11 09:30:41 -07001977status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001978{
1979 const size_t SIZE = 256;
1980 char buffer[SIZE];
1981 String8 result;
1982
1983 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1984 result.append(buffer);
1985
Eric Laurent87ffa392015-05-22 10:32:38 -07001986 snprintf(buffer, SIZE, " Primary Output: %d\n",
1987 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07001988 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001989 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001990 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001991 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001992 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001993 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001994 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001995 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001996 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001997 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001998 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001999 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002000 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07002001 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002002 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01002003 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002004 result.append(buffer);
Eric Laurent9459fb02015-08-12 18:36:32 -07002005 snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available");
2006 result.append(buffer);
2007
François Gaffie2110e042015-03-24 08:41:51 +01002008 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002009
François Gaffie53615e22015-03-19 09:24:12 +01002010 mAvailableOutputDevices.dump(fd, String8("output"));
2011 mAvailableInputDevices.dump(fd, String8("input"));
2012 mHwModules.dump(fd);
2013 mOutputs.dump(fd);
2014 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002015 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002016 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002017 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002018
2019 return NO_ERROR;
2020}
2021
2022// This function checks for the parameters which can be offloaded.
2023// This can be enhanced depending on the capability of the DSP and policy
2024// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002025bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002026{
2027 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002028 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002029 offloadInfo.sample_rate, offloadInfo.channel_mask,
2030 offloadInfo.format,
2031 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2032 offloadInfo.has_video);
2033
2034 // Check if offload has been disabled
2035 char propValue[PROPERTY_VALUE_MAX];
2036 if (property_get("audio.offload.disable", propValue, "0")) {
2037 if (atoi(propValue) != 0) {
2038 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2039 return false;
2040 }
2041 }
2042
2043 // Check if stream type is music, then only allow offload as of now.
2044 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2045 {
2046 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2047 return false;
2048 }
2049
2050 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002051 const bool allowOffloadWithVideo =
2052 property_get_bool("audio.offload.video", false /* default_value */);
2053 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002054 ALOGV("isOffloadSupported: has_video == true, returning false");
2055 return false;
2056 }
2057
2058 //If duration is less than minimum value defined in property, return false
2059 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2060 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2061 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2062 return false;
2063 }
2064 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2065 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2066 return false;
2067 }
2068
2069 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2070 // creating an offloaded track and tearing it down immediately after start when audioflinger
2071 // detects there is an active non offloadable effect.
2072 // FIXME: We should check the audio session here but we do not have it in this context.
2073 // This may prevent offloading in rare situations where effects are left active by apps
2074 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002075 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002076 return false;
2077 }
2078
2079 // See if there is a profile to support this.
2080 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002081 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002082 offloadInfo.sample_rate,
2083 offloadInfo.format,
2084 offloadInfo.channel_mask,
2085 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002086 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2087 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002088}
2089
Eric Laurent6a94d692014-05-20 11:18:06 -07002090status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2091 audio_port_type_t type,
2092 unsigned int *num_ports,
2093 struct audio_port *ports,
2094 unsigned int *generation)
2095{
2096 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2097 generation == NULL) {
2098 return BAD_VALUE;
2099 }
2100 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2101 if (ports == NULL) {
2102 *num_ports = 0;
2103 }
2104
2105 size_t portsWritten = 0;
2106 size_t portsMax = *num_ports;
2107 *num_ports = 0;
2108 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2109 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2110 for (size_t i = 0;
2111 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2112 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2113 }
2114 *num_ports += mAvailableOutputDevices.size();
2115 }
2116 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2117 for (size_t i = 0;
2118 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2119 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2120 }
2121 *num_ports += mAvailableInputDevices.size();
2122 }
2123 }
2124 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2125 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2126 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2127 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2128 }
2129 *num_ports += mInputs.size();
2130 }
2131 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002132 size_t numOutputs = 0;
2133 for (size_t i = 0; i < mOutputs.size(); i++) {
2134 if (!mOutputs[i]->isDuplicated()) {
2135 numOutputs++;
2136 if (portsWritten < portsMax) {
2137 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2138 }
2139 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002140 }
Eric Laurent84c70242014-06-23 08:46:27 -07002141 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002142 }
2143 }
2144 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002145 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002146 return NO_ERROR;
2147}
2148
2149status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2150{
2151 return NO_ERROR;
2152}
2153
Eric Laurent6a94d692014-05-20 11:18:06 -07002154status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2155 audio_patch_handle_t *handle,
2156 uid_t uid)
2157{
2158 ALOGV("createAudioPatch()");
2159
2160 if (handle == NULL || patch == NULL) {
2161 return BAD_VALUE;
2162 }
2163 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2164
Eric Laurent874c42872014-08-08 15:13:39 -07002165 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2166 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2167 return BAD_VALUE;
2168 }
2169 // only one source per audio patch supported for now
2170 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002171 return INVALID_OPERATION;
2172 }
Eric Laurent874c42872014-08-08 15:13:39 -07002173
2174 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002175 return INVALID_OPERATION;
2176 }
Eric Laurent874c42872014-08-08 15:13:39 -07002177 for (size_t i = 0; i < patch->num_sinks; i++) {
2178 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2179 return INVALID_OPERATION;
2180 }
2181 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002182
2183 sp<AudioPatch> patchDesc;
2184 ssize_t index = mAudioPatches.indexOfKey(*handle);
2185
Eric Laurent6a94d692014-05-20 11:18:06 -07002186 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2187 patch->sources[0].role,
2188 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002189#if LOG_NDEBUG == 0
2190 for (size_t i = 0; i < patch->num_sinks; i++) {
2191 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2192 patch->sinks[i].role,
2193 patch->sinks[i].type);
2194 }
2195#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002196
2197 if (index >= 0) {
2198 patchDesc = mAudioPatches.valueAt(index);
2199 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2200 mUidCached, patchDesc->mUid, uid);
2201 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2202 return INVALID_OPERATION;
2203 }
2204 } else {
2205 *handle = 0;
2206 }
2207
2208 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002209 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002210 if (outputDesc == NULL) {
2211 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2212 return BAD_VALUE;
2213 }
Eric Laurent84c70242014-06-23 08:46:27 -07002214 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2215 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002216 if (patchDesc != 0) {
2217 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2218 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2219 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2220 return BAD_VALUE;
2221 }
2222 }
Eric Laurent874c42872014-08-08 15:13:39 -07002223 DeviceVector devices;
2224 for (size_t i = 0; i < patch->num_sinks; i++) {
2225 // Only support mix to devices connection
2226 // TODO add support for mix to mix connection
2227 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2228 ALOGV("createAudioPatch() source mix but sink is not a device");
2229 return INVALID_OPERATION;
2230 }
2231 sp<DeviceDescriptor> devDesc =
2232 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2233 if (devDesc == 0) {
2234 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2235 return BAD_VALUE;
2236 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002237
François Gaffie53615e22015-03-19 09:24:12 +01002238 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002239 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002240 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002241 NULL, // updatedSamplingRate
2242 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002243 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002244 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002245 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002246 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002247 ALOGV("createAudioPatch() profile not supported for device %08x",
2248 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002249 return INVALID_OPERATION;
2250 }
2251 devices.add(devDesc);
2252 }
2253 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002254 return INVALID_OPERATION;
2255 }
Eric Laurent874c42872014-08-08 15:13:39 -07002256
Eric Laurent6a94d692014-05-20 11:18:06 -07002257 // TODO: reconfigure output format and channels here
2258 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002259 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002260 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002261 index = mAudioPatches.indexOfKey(*handle);
2262 if (index >= 0) {
2263 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2264 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2265 }
2266 patchDesc = mAudioPatches.valueAt(index);
2267 patchDesc->mUid = uid;
2268 ALOGV("createAudioPatch() success");
2269 } else {
2270 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2271 return INVALID_OPERATION;
2272 }
2273 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2274 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2275 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002276 // only one sink supported when connecting an input device to a mix
2277 if (patch->num_sinks > 1) {
2278 return INVALID_OPERATION;
2279 }
François Gaffie53615e22015-03-19 09:24:12 +01002280 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002281 if (inputDesc == NULL) {
2282 return BAD_VALUE;
2283 }
2284 if (patchDesc != 0) {
2285 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2286 return BAD_VALUE;
2287 }
2288 }
2289 sp<DeviceDescriptor> devDesc =
2290 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2291 if (devDesc == 0) {
2292 return BAD_VALUE;
2293 }
2294
François Gaffie53615e22015-03-19 09:24:12 +01002295 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002296 devDesc->mAddress,
2297 patch->sinks[0].sample_rate,
2298 NULL, /*updatedSampleRate*/
2299 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002300 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002301 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002302 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002303 // FIXME for the parameter type,
2304 // and the NONE
2305 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002306 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002307 return INVALID_OPERATION;
2308 }
2309 // TODO: reconfigure output format and channels here
2310 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002311 devDesc->type(), inputDesc->mIoHandle);
2312 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002313 index = mAudioPatches.indexOfKey(*handle);
2314 if (index >= 0) {
2315 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2316 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2317 }
2318 patchDesc = mAudioPatches.valueAt(index);
2319 patchDesc->mUid = uid;
2320 ALOGV("createAudioPatch() success");
2321 } else {
2322 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2323 return INVALID_OPERATION;
2324 }
2325 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2326 // device to device connection
2327 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002328 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002329 return BAD_VALUE;
2330 }
2331 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002332 sp<DeviceDescriptor> srcDeviceDesc =
2333 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002334 if (srcDeviceDesc == 0) {
2335 return BAD_VALUE;
2336 }
Eric Laurent874c42872014-08-08 15:13:39 -07002337
Eric Laurent6a94d692014-05-20 11:18:06 -07002338 //update source and sink with our own data as the data passed in the patch may
2339 // be incomplete.
2340 struct audio_patch newPatch = *patch;
2341 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002342
Eric Laurent874c42872014-08-08 15:13:39 -07002343 for (size_t i = 0; i < patch->num_sinks; i++) {
2344 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2345 ALOGV("createAudioPatch() source device but one sink is not a device");
2346 return INVALID_OPERATION;
2347 }
2348
2349 sp<DeviceDescriptor> sinkDeviceDesc =
2350 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2351 if (sinkDeviceDesc == 0) {
2352 return BAD_VALUE;
2353 }
2354 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2355
Eric Laurent3bcf8592015-04-03 12:13:24 -07002356 // create a software bridge in PatchPanel if:
2357 // - source and sink devices are on differnt HW modules OR
2358 // - audio HAL version is < 3.0
2359 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2360 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2361 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002362 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002363 return INVALID_OPERATION;
2364 }
Eric Laurent874c42872014-08-08 15:13:39 -07002365 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002366 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002367 // if the sink device is reachable via an opened output stream, request to go via
2368 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002369 audio_io_handle_t output = selectOutput(outputs,
2370 AUDIO_OUTPUT_FLAG_NONE,
2371 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002372 if (output != AUDIO_IO_HANDLE_NONE) {
2373 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2374 if (outputDesc->isDuplicated()) {
2375 return INVALID_OPERATION;
2376 }
2377 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002378 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002379 newPatch.num_sources = 2;
2380 }
Eric Laurent83b88082014-06-20 18:31:16 -07002381 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002382 }
2383 // TODO: check from routing capabilities in config file and other conflicting patches
2384
2385 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2386 if (index >= 0) {
2387 afPatchHandle = patchDesc->mAfPatchHandle;
2388 }
2389
2390 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2391 &afPatchHandle,
2392 0);
2393 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2394 status, afPatchHandle);
2395 if (status == NO_ERROR) {
2396 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002397 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002398 addAudioPatch(patchDesc->mHandle, patchDesc);
2399 } else {
2400 patchDesc->mPatch = newPatch;
2401 }
2402 patchDesc->mAfPatchHandle = afPatchHandle;
2403 *handle = patchDesc->mHandle;
2404 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002405 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002406 } else {
2407 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2408 status);
2409 return INVALID_OPERATION;
2410 }
2411 } else {
2412 return BAD_VALUE;
2413 }
2414 } else {
2415 return BAD_VALUE;
2416 }
2417 return NO_ERROR;
2418}
2419
2420status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2421 uid_t uid)
2422{
2423 ALOGV("releaseAudioPatch() patch %d", handle);
2424
2425 ssize_t index = mAudioPatches.indexOfKey(handle);
2426
2427 if (index < 0) {
2428 return BAD_VALUE;
2429 }
2430 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2431 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2432 mUidCached, patchDesc->mUid, uid);
2433 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2434 return INVALID_OPERATION;
2435 }
2436
2437 struct audio_patch *patch = &patchDesc->mPatch;
2438 patchDesc->mUid = mUidCached;
2439 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002440 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002441 if (outputDesc == NULL) {
2442 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2443 return BAD_VALUE;
2444 }
2445
Eric Laurentc75307b2015-03-17 15:29:32 -07002446 setOutputDevice(outputDesc,
2447 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002448 true,
2449 0,
2450 NULL);
2451 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2452 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002453 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002454 if (inputDesc == NULL) {
2455 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2456 return BAD_VALUE;
2457 }
2458 setInputDevice(inputDesc->mIoHandle,
2459 getNewInputDevice(inputDesc->mIoHandle),
2460 true,
2461 NULL);
2462 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2463 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2464 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2465 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2466 status, patchDesc->mAfPatchHandle);
2467 removeAudioPatch(patchDesc->mHandle);
2468 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002469 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002470 } else {
2471 return BAD_VALUE;
2472 }
2473 } else {
2474 return BAD_VALUE;
2475 }
2476 return NO_ERROR;
2477}
2478
2479status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2480 struct audio_patch *patches,
2481 unsigned int *generation)
2482{
François Gaffie53615e22015-03-19 09:24:12 +01002483 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002484 return BAD_VALUE;
2485 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002486 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002487 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002488}
2489
Eric Laurente1715a42014-05-20 11:30:42 -07002490status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002491{
Eric Laurente1715a42014-05-20 11:30:42 -07002492 ALOGV("setAudioPortConfig()");
2493
2494 if (config == NULL) {
2495 return BAD_VALUE;
2496 }
2497 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2498 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002499 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2500 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002501 }
2502
Eric Laurenta121f902014-06-03 13:32:54 -07002503 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002504 if (config->type == AUDIO_PORT_TYPE_MIX) {
2505 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002506 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002507 if (outputDesc == NULL) {
2508 return BAD_VALUE;
2509 }
Eric Laurent84c70242014-06-23 08:46:27 -07002510 ALOG_ASSERT(!outputDesc->isDuplicated(),
2511 "setAudioPortConfig() called on duplicated output %d",
2512 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002513 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002514 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002515 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002516 if (inputDesc == NULL) {
2517 return BAD_VALUE;
2518 }
Eric Laurenta121f902014-06-03 13:32:54 -07002519 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002520 } else {
2521 return BAD_VALUE;
2522 }
2523 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2524 sp<DeviceDescriptor> deviceDesc;
2525 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2526 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2527 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2528 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2529 } else {
2530 return BAD_VALUE;
2531 }
2532 if (deviceDesc == NULL) {
2533 return BAD_VALUE;
2534 }
Eric Laurenta121f902014-06-03 13:32:54 -07002535 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002536 } else {
2537 return BAD_VALUE;
2538 }
2539
Eric Laurenta121f902014-06-03 13:32:54 -07002540 struct audio_port_config backupConfig;
2541 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2542 if (status == NO_ERROR) {
2543 struct audio_port_config newConfig;
2544 audioPortConfig->toAudioPortConfig(&newConfig, config);
2545 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002546 }
Eric Laurenta121f902014-06-03 13:32:54 -07002547 if (status != NO_ERROR) {
2548 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002549 }
Eric Laurente1715a42014-05-20 11:30:42 -07002550
2551 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002552}
2553
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002554void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2555{
2556 clearAudioPatches(uid);
2557 clearSessionRoutes(uid);
2558}
2559
Eric Laurent6a94d692014-05-20 11:18:06 -07002560void AudioPolicyManager::clearAudioPatches(uid_t uid)
2561{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002562 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002563 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2564 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002565 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002566 }
2567 }
2568}
2569
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002570
2571void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2572 audio_io_handle_t ouptutToSkip)
2573{
2574 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2575 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2576 for (size_t j = 0; j < mOutputs.size(); j++) {
2577 if (mOutputs.keyAt(j) == ouptutToSkip) {
2578 continue;
2579 }
2580 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2581 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2582 continue;
2583 }
2584 // If the default device for this strategy is on another output mix,
2585 // invalidate all tracks in this strategy to force re connection.
2586 // Otherwise select new device on the output mix.
2587 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2588 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2589 if (stream == AUDIO_STREAM_PATCH) {
2590 continue;
2591 }
2592 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2593 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2594 }
2595 }
2596 } else {
2597 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2598 setOutputDevice(outputDesc, newDevice, false);
2599 }
2600 }
2601}
2602
2603void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2604{
2605 // remove output routes associated with this uid
2606 SortedVector<routing_strategy> affectedStrategies;
2607 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2608 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2609 if (route->mUid == uid) {
2610 mOutputRoutes.removeItemsAt(i);
2611 if (route->mDeviceDescriptor != 0) {
2612 affectedStrategies.add(getStrategy(route->mStreamType));
2613 }
2614 }
2615 }
2616 // reroute outputs if necessary
2617 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2618 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2619 }
2620
2621 // remove input routes associated with this uid
2622 SortedVector<audio_source_t> affectedSources;
2623 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2624 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2625 if (route->mUid == uid) {
2626 mInputRoutes.removeItemsAt(i);
2627 if (route->mDeviceDescriptor != 0) {
2628 affectedSources.add(route->mSource);
2629 }
2630 }
2631 }
2632 // reroute inputs if necessary
2633 SortedVector<audio_io_handle_t> inputsToClose;
2634 for (size_t i = 0; i < mInputs.size(); i++) {
2635 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2636 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2637 inputsToClose.add(inputDesc->mIoHandle);
2638 }
2639 }
2640 for (size_t i = 0; i < inputsToClose.size(); i++) {
2641 closeInput(inputsToClose[i]);
2642 }
2643}
2644
2645
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002646status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2647 audio_io_handle_t *ioHandle,
2648 audio_devices_t *device)
2649{
2650 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2651 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002652 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002653
François Gaffiedf372692015-03-19 10:43:27 +01002654 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002655}
2656
Eric Laurent493404d2015-04-21 15:07:36 -07002657status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2658 const audio_attributes_t *attributes __unused,
2659 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002660{
2661 return INVALID_OPERATION;
2662}
2663
Eric Laurent493404d2015-04-21 15:07:36 -07002664status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002665{
2666 return INVALID_OPERATION;
2667}
2668
Eric Laurente552edb2014-03-10 17:42:56 -07002669// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002670// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002671// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002672uint32_t AudioPolicyManager::nextAudioPortGeneration()
2673{
2674 return android_atomic_inc(&mAudioPortGeneration);
2675}
2676
Eric Laurente0720872014-03-11 09:30:41 -07002677AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002678 :
2679#ifdef AUDIO_POLICY_TEST
2680 Thread(false),
2681#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002682 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002683 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002684 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002685 mAudioPortGeneration(1),
2686 mBeaconMuteRefCount(0),
2687 mBeaconPlayingRefCount(0),
Eric Laurent9459fb02015-08-12 18:36:32 -07002688 mBeaconMuted(false),
2689 mTtsOutputAvailable(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002690{
François Gaffie2110e042015-03-24 08:41:51 +01002691 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2692 if (!engineInstance) {
2693 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2694 return;
2695 }
2696 // Retrieve the Policy Manager Interface
2697 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2698 if (mEngine == NULL) {
2699 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2700 return;
2701 }
2702 mEngine->setObserver(this);
2703 status_t status = mEngine->initCheck();
2704 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2705
Eric Laurent6a94d692014-05-20 11:18:06 -07002706 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002707 mpClientInterface = clientInterface;
2708
Eric Laurent7288ab82015-05-06 18:44:02 -07002709 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002710 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2711 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2712 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2713 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2714 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2715 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002716 ALOGE("could not load audio policy configuration file, setting defaults");
2717 defaultAudioPolicyConfig();
2718 }
2719 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002720 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002721
François Gaffie2110e042015-03-24 08:41:51 +01002722 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2723 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002724
2725 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002726 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2727 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002728 for (size_t i = 0; i < mHwModules.size(); i++) {
2729 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2730 if (mHwModules[i]->mHandle == 0) {
2731 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2732 continue;
2733 }
2734 // open all output streams needed to access attached devices
2735 // except for direct output streams that are only opened when they are actually
2736 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002737 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002738 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2739 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002740 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002741
Eric Laurent3a4311c2014-03-17 12:00:47 -07002742 if (outProfile->mSupportedDevices.isEmpty()) {
2743 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2744 continue;
2745 }
Eric Laurent9459fb02015-08-12 18:36:32 -07002746 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_TTS) != 0) {
2747 mTtsOutputAvailable = true;
2748 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002749
Eric Laurentd78f1532014-09-16 16:38:20 -07002750 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2751 continue;
2752 }
Eric Laurent83b88082014-06-20 18:31:16 -07002753 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002754 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2755 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002756 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002757 // chose first device present in mSupportedDevices also part of
2758 // outputDeviceTypes
2759 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002760 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002761 if ((profileType & outputDeviceTypes) != 0) {
2762 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002763 }
Eric Laurente552edb2014-03-10 17:42:56 -07002764 }
2765 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002766 if ((profileType & outputDeviceTypes) == 0) {
2767 continue;
2768 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002769 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2770 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002771
2772 outputDesc->mDevice = profileType;
2773 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2774 config.sample_rate = outputDesc->mSamplingRate;
2775 config.channel_mask = outputDesc->mChannelMask;
2776 config.format = outputDesc->mFormat;
2777 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002778 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002779 &output,
2780 &config,
2781 &outputDesc->mDevice,
2782 String8(""),
2783 &outputDesc->mLatency,
2784 outputDesc->mFlags);
2785
2786 if (status != NO_ERROR) {
2787 ALOGW("Cannot open output stream for device %08x on hw module %s",
2788 outputDesc->mDevice,
2789 mHwModules[i]->mName);
2790 } else {
2791 outputDesc->mSamplingRate = config.sample_rate;
2792 outputDesc->mChannelMask = config.channel_mask;
2793 outputDesc->mFormat = config.format;
2794
2795 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002796 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002797 ssize_t index =
2798 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2799 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002800 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2801 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002802 }
2803 }
2804 if (mPrimaryOutput == 0 &&
2805 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002806 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002807 }
2808 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002809 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002810 outputDesc->mDevice,
2811 true);
2812 }
Eric Laurente552edb2014-03-10 17:42:56 -07002813 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002814 // open input streams needed to access attached devices to validate
2815 // mAvailableInputDevices list
2816 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2817 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002818 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002819
Eric Laurent3a4311c2014-03-17 12:00:47 -07002820 if (inProfile->mSupportedDevices.isEmpty()) {
2821 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2822 continue;
2823 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002824 // chose first device present in mSupportedDevices also part of
2825 // inputDeviceTypes
2826 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2827 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002828 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002829 if (profileType & inputDeviceTypes) {
2830 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002831 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002832 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002833 if ((profileType & inputDeviceTypes) == 0) {
2834 continue;
2835 }
2836 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2837
2838 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2839 inputDesc->mDevice = profileType;
2840
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002841 // find the address
2842 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2843 // the inputs vector must be of size 1, but we don't want to crash here
2844 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2845 : String8("");
2846 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2847 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2848
Eric Laurentd78f1532014-09-16 16:38:20 -07002849 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2850 config.sample_rate = inputDesc->mSamplingRate;
2851 config.channel_mask = inputDesc->mChannelMask;
2852 config.format = inputDesc->mFormat;
2853 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002854 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002855 &input,
2856 &config,
2857 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002858 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002859 AUDIO_SOURCE_MIC,
2860 AUDIO_INPUT_FLAG_NONE);
2861
2862 if (status == NO_ERROR) {
2863 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002864 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002865 ssize_t index =
2866 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2867 // give a valid ID to an attached device once confirmed it is reachable
Eric Laurent45aabc32015-08-06 09:11:13 -07002868 if (index >= 0) {
2869 sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index];
2870 if (!devDesc->isAttached()) {
2871 devDesc->attach(mHwModules[i]);
2872 devDesc->importAudioPort(inProfile);
2873 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002874 }
2875 }
2876 mpClientInterface->closeInput(input);
2877 } else {
2878 ALOGW("Cannot open input stream for device %08x on hw module %s",
2879 inputDesc->mDevice,
2880 mHwModules[i]->mName);
2881 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002882 }
2883 }
2884 // make sure all attached devices have been allocated a unique ID
2885 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002886 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002887 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002888 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2889 continue;
2890 }
François Gaffie2110e042015-03-24 08:41:51 +01002891 // The device is now validated and can be appended to the available devices of the engine
2892 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2893 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002894 i++;
2895 }
2896 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002897 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002898 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002899 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2900 continue;
2901 }
François Gaffie2110e042015-03-24 08:41:51 +01002902 // The device is now validated and can be appended to the available devices of the engine
2903 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2904 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002905 i++;
2906 }
2907 // make sure default device is reachable
2908 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002909 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002910 }
Eric Laurente552edb2014-03-10 17:42:56 -07002911
2912 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2913
2914 updateDevicesAndOutputs();
2915
2916#ifdef AUDIO_POLICY_TEST
2917 if (mPrimaryOutput != 0) {
2918 AudioParameter outputCmd = AudioParameter();
2919 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002920 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002921
2922 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2923 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002924 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2925 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002926 mTestLatencyMs = 0;
2927 mCurOutput = 0;
2928 mDirectOutput = false;
2929 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2930 mTestOutputs[i] = 0;
2931 }
2932
2933 const size_t SIZE = 256;
2934 char buffer[SIZE];
2935 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2936 run(buffer, ANDROID_PRIORITY_AUDIO);
2937 }
2938#endif //AUDIO_POLICY_TEST
2939}
2940
Eric Laurente0720872014-03-11 09:30:41 -07002941AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002942{
2943#ifdef AUDIO_POLICY_TEST
2944 exit();
2945#endif //AUDIO_POLICY_TEST
2946 for (size_t i = 0; i < mOutputs.size(); i++) {
2947 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002948 }
2949 for (size_t i = 0; i < mInputs.size(); i++) {
2950 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002951 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002952 mAvailableOutputDevices.clear();
2953 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002954 mOutputs.clear();
2955 mInputs.clear();
2956 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002957}
2958
Eric Laurente0720872014-03-11 09:30:41 -07002959status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002960{
Eric Laurent87ffa392015-05-22 10:32:38 -07002961 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002962}
2963
2964#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002965bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002966{
2967 ALOGV("entering threadLoop()");
2968 while (!exitPending())
2969 {
2970 String8 command;
2971 int valueInt;
2972 String8 value;
2973
2974 Mutex::Autolock _l(mLock);
2975 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2976
2977 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2978 AudioParameter param = AudioParameter(command);
2979
2980 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2981 valueInt != 0) {
2982 ALOGV("Test command %s received", command.string());
2983 String8 target;
2984 if (param.get(String8("target"), target) != NO_ERROR) {
2985 target = "Manager";
2986 }
2987 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2988 param.remove(String8("test_cmd_policy_output"));
2989 mCurOutput = valueInt;
2990 }
2991 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2992 param.remove(String8("test_cmd_policy_direct"));
2993 if (value == "false") {
2994 mDirectOutput = false;
2995 } else if (value == "true") {
2996 mDirectOutput = true;
2997 }
2998 }
2999 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3000 param.remove(String8("test_cmd_policy_input"));
3001 mTestInput = valueInt;
3002 }
3003
3004 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3005 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003006 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003007 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003008 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003009 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003010 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003011 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003012 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003013 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003014 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003015 if (target == "Manager") {
3016 mTestFormat = format;
3017 } else if (mTestOutputs[mCurOutput] != 0) {
3018 AudioParameter outputParam = AudioParameter();
3019 outputParam.addInt(String8("format"), format);
3020 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3021 }
3022 }
3023 }
3024 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3025 param.remove(String8("test_cmd_policy_channels"));
3026 int channels = 0;
3027
3028 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003029 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003030 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003031 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003032 }
3033 if (channels != 0) {
3034 if (target == "Manager") {
3035 mTestChannels = channels;
3036 } else if (mTestOutputs[mCurOutput] != 0) {
3037 AudioParameter outputParam = AudioParameter();
3038 outputParam.addInt(String8("channels"), channels);
3039 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3040 }
3041 }
3042 }
3043 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3044 param.remove(String8("test_cmd_policy_sampleRate"));
3045 if (valueInt >= 0 && valueInt <= 96000) {
3046 int samplingRate = valueInt;
3047 if (target == "Manager") {
3048 mTestSamplingRate = samplingRate;
3049 } else if (mTestOutputs[mCurOutput] != 0) {
3050 AudioParameter outputParam = AudioParameter();
3051 outputParam.addInt(String8("sampling_rate"), samplingRate);
3052 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3053 }
3054 }
3055 }
3056
3057 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3058 param.remove(String8("test_cmd_policy_reopen"));
3059
Eric Laurentc75307b2015-03-17 15:29:32 -07003060 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003061
Eric Laurentc75307b2015-03-17 15:29:32 -07003062 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003063
Eric Laurentc75307b2015-03-17 15:29:32 -07003064 removeOutput(mPrimaryOutput->mIoHandle);
3065 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3066 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003067 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003068 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3069 config.sample_rate = outputDesc->mSamplingRate;
3070 config.channel_mask = outputDesc->mChannelMask;
3071 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003072 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003073 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003074 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003075 &config,
3076 &outputDesc->mDevice,
3077 String8(""),
3078 &outputDesc->mLatency,
3079 outputDesc->mFlags);
3080 if (status != NO_ERROR) {
3081 ALOGE("Failed to reopen hardware output stream, "
3082 "samplingRate: %d, format %d, channels %d",
3083 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003084 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003085 outputDesc->mSamplingRate = config.sample_rate;
3086 outputDesc->mChannelMask = config.channel_mask;
3087 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003088 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003089 AudioParameter outputCmd = AudioParameter();
3090 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003091 mpClientInterface->setParameters(handle, outputCmd.toString());
3092 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003093 }
3094 }
3095
3096
3097 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3098 }
3099 }
3100 return false;
3101}
3102
Eric Laurente0720872014-03-11 09:30:41 -07003103void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003104{
3105 {
3106 AutoMutex _l(mLock);
3107 requestExit();
3108 mWaitWorkCV.signal();
3109 }
3110 requestExitAndWait();
3111}
3112
Eric Laurente0720872014-03-11 09:30:41 -07003113int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003114{
3115 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3116 if (output == mTestOutputs[i]) return i;
3117 }
3118 return 0;
3119}
3120#endif //AUDIO_POLICY_TEST
3121
3122// ---
3123
Eric Laurentc75307b2015-03-17 15:29:32 -07003124void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003125{
François Gaffie98cc1912015-03-18 17:52:40 +01003126 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003127 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003128 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003129}
3130
François Gaffie53615e22015-03-19 09:24:12 +01003131void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3132{
3133 mOutputs.removeItem(output);
3134}
3135
Eric Laurent1f2f2232014-06-02 12:01:23 -07003136void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003137{
François Gaffie98cc1912015-03-18 17:52:40 +01003138 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003139 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003140 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003141}
Eric Laurente552edb2014-03-10 17:42:56 -07003142
Eric Laurentc75307b2015-03-17 15:29:32 -07003143void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003144 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003145 const String8 address /*in*/,
3146 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003147 sp<DeviceDescriptor> devDesc =
3148 desc->mProfile->mSupportedDevices.getDevice(device, address);
3149 if (devDesc != 0) {
3150 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3151 desc->mIoHandle, address.string());
3152 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003153 }
3154}
3155
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003156status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003157 audio_policy_dev_state_t state,
3158 SortedVector<audio_io_handle_t>& outputs,
3159 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003160{
François Gaffie53615e22015-03-19 09:24:12 +01003161 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003162 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003163
3164 if (audio_device_is_digital(device)) {
3165 // erase all current sample rates, formats and channel masks
3166 devDesc->clearCapabilities();
3167 }
Eric Laurente552edb2014-03-10 17:42:56 -07003168
Eric Laurent3b73df72014-03-11 09:06:29 -07003169 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003170 // first list already open outputs that can be routed to this device
3171 for (size_t i = 0; i < mOutputs.size(); i++) {
3172 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003173 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003174 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003175 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3176 outputs.add(mOutputs.keyAt(i));
3177 } else {
3178 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003179 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003180 }
Eric Laurente552edb2014-03-10 17:42:56 -07003181 }
3182 }
3183 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003184 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003185 for (size_t i = 0; i < mHwModules.size(); i++)
3186 {
3187 if (mHwModules[i]->mHandle == 0) {
3188 continue;
3189 }
3190 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3191 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003192 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3193 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003194 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003195 address == profile->mSupportedDevices[0]->mAddress) {
3196 profiles.add(profile);
3197 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3198 }
Eric Laurente552edb2014-03-10 17:42:56 -07003199 }
3200 }
3201 }
3202
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003203 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3204
Eric Laurente552edb2014-03-10 17:42:56 -07003205 if (profiles.isEmpty() && outputs.isEmpty()) {
3206 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3207 return BAD_VALUE;
3208 }
3209
3210 // open outputs for matching profiles if needed. Direct outputs are also opened to
3211 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3212 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003213 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003214
3215 // nothing to do if one output is already opened for this profile
3216 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003217 for (j = 0; j < outputs.size(); j++) {
3218 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003219 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003220 // matching profile: save the sample rates, format and channel masks supported
3221 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003222 if (audio_device_is_digital(device)) {
3223 devDesc->importAudioPort(profile);
3224 }
Eric Laurente552edb2014-03-10 17:42:56 -07003225 break;
3226 }
3227 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003228 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003229 continue;
3230 }
3231
Eric Laurent83b88082014-06-20 18:31:16 -07003232 ALOGV("opening output for device %08x with params %s profile %p",
3233 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003234 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003235 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003236 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3237 config.sample_rate = desc->mSamplingRate;
3238 config.channel_mask = desc->mChannelMask;
3239 config.format = desc->mFormat;
3240 config.offload_info.sample_rate = desc->mSamplingRate;
3241 config.offload_info.channel_mask = desc->mChannelMask;
3242 config.offload_info.format = desc->mFormat;
3243 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003244 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003245 &output,
3246 &config,
3247 &desc->mDevice,
3248 address,
3249 &desc->mLatency,
3250 desc->mFlags);
3251 if (status == NO_ERROR) {
3252 desc->mSamplingRate = config.sample_rate;
3253 desc->mChannelMask = config.channel_mask;
3254 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003255
Eric Laurentd4692962014-05-05 18:13:44 -07003256 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003257 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003258 char *param = audio_device_address_to_parameter(device, address);
3259 mpClientInterface->setParameters(output, String8(param));
3260 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003261 }
3262
Eric Laurentd4692962014-05-05 18:13:44 -07003263 // Here is where we step through and resolve any "dynamic" fields
3264 String8 reply;
3265 char *value;
3266 if (profile->mSamplingRates[0] == 0) {
3267 reply = mpClientInterface->getParameters(output,
3268 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003269 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003270 reply.string());
3271 value = strpbrk((char *)reply.string(), "=");
3272 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003273 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003274 }
Eric Laurentd4692962014-05-05 18:13:44 -07003275 }
3276 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3277 reply = mpClientInterface->getParameters(output,
3278 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003279 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003280 reply.string());
3281 value = strpbrk((char *)reply.string(), "=");
3282 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003283 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003284 }
Eric Laurentd4692962014-05-05 18:13:44 -07003285 }
3286 if (profile->mChannelMasks[0] == 0) {
3287 reply = mpClientInterface->getParameters(output,
3288 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003289 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003290 reply.string());
3291 value = strpbrk((char *)reply.string(), "=");
3292 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003293 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003294 }
Eric Laurentd4692962014-05-05 18:13:44 -07003295 }
3296 if (((profile->mSamplingRates[0] == 0) &&
3297 (profile->mSamplingRates.size() < 2)) ||
3298 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3299 (profile->mFormats.size() < 2)) ||
3300 ((profile->mChannelMasks[0] == 0) &&
3301 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003302 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003303 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003304 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003305 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3306 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003307 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003308 config.sample_rate = profile->pickSamplingRate();
3309 config.channel_mask = profile->pickChannelMask();
3310 config.format = profile->pickFormat();
3311 config.offload_info.sample_rate = config.sample_rate;
3312 config.offload_info.channel_mask = config.channel_mask;
3313 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003314 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003315 &output,
3316 &config,
3317 &desc->mDevice,
3318 address,
3319 &desc->mLatency,
3320 desc->mFlags);
3321 if (status == NO_ERROR) {
3322 desc->mSamplingRate = config.sample_rate;
3323 desc->mChannelMask = config.channel_mask;
3324 desc->mFormat = config.format;
3325 } else {
3326 output = AUDIO_IO_HANDLE_NONE;
3327 }
Eric Laurentd4692962014-05-05 18:13:44 -07003328 }
3329
Eric Laurentcf2c0212014-07-25 16:20:43 -07003330 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003331 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003332 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003333 sp<AudioPolicyMix> policyMix;
3334 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003335 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3336 address.string());
3337 }
François Gaffie036e1e92015-03-19 10:16:24 +01003338 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003339 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003340
Eric Laurent87ffa392015-05-22 10:32:38 -07003341 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3342 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003343 // no duplicated output for direct outputs and
3344 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003345 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003346
Eric Laurentd4692962014-05-05 18:13:44 -07003347 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003348 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003349
Eric Laurentd4692962014-05-05 18:13:44 -07003350 //TODO: configure audio effect output stage here
3351
3352 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003353 duplicatedOutput =
3354 mpClientInterface->openDuplicateOutput(output,
3355 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003356 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003357 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003358 sp<SwAudioOutputDescriptor> dupOutputDesc =
3359 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3360 dupOutputDesc->mOutput1 = mPrimaryOutput;
3361 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003362 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3363 dupOutputDesc->mFormat = desc->mFormat;
3364 dupOutputDesc->mChannelMask = desc->mChannelMask;
3365 dupOutputDesc->mLatency = desc->mLatency;
3366 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003367 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003368 } else {
3369 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003370 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003371 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003372 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003373 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003374 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003375 }
Eric Laurente552edb2014-03-10 17:42:56 -07003376 }
3377 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003378 } else {
3379 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003380 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003381 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003382 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003383 profiles.removeAt(profile_index);
3384 profile_index--;
3385 } else {
3386 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003387 // Load digital format info only for digital devices
3388 if (audio_device_is_digital(device)) {
3389 devDesc->importAudioPort(profile);
3390 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003391
François Gaffie53615e22015-03-19 09:24:12 +01003392 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003393 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3394 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003395 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003396 NULL/*patch handle*/, address.string());
3397 }
Eric Laurente552edb2014-03-10 17:42:56 -07003398 ALOGV("checkOutputsForDevice(): adding output %d", output);
3399 }
3400 }
3401
3402 if (profiles.isEmpty()) {
3403 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3404 return BAD_VALUE;
3405 }
Eric Laurentd4692962014-05-05 18:13:44 -07003406 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003407 // check if one opened output is not needed any more after disconnecting one device
3408 for (size_t i = 0; i < mOutputs.size(); i++) {
3409 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003410 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003411 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003412 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003413 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003414 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003415 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003416 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3417 mOutputs.keyAt(i));
3418 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003419 }
Eric Laurente552edb2014-03-10 17:42:56 -07003420 }
3421 }
Eric Laurentd4692962014-05-05 18:13:44 -07003422 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003423 for (size_t i = 0; i < mHwModules.size(); i++)
3424 {
3425 if (mHwModules[i]->mHandle == 0) {
3426 continue;
3427 }
3428 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3429 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003430 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003431 if (profile->mSupportedDevices.types() & device) {
3432 ALOGV("checkOutputsForDevice(): "
3433 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003434 if (profile->mSamplingRates[0] == 0) {
3435 profile->mSamplingRates.clear();
3436 profile->mSamplingRates.add(0);
3437 }
3438 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3439 profile->mFormats.clear();
3440 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3441 }
3442 if (profile->mChannelMasks[0] == 0) {
3443 profile->mChannelMasks.clear();
3444 profile->mChannelMasks.add(0);
3445 }
3446 }
3447 }
3448 }
3449 }
3450 return NO_ERROR;
3451}
3452
Paul McLean9080a4c2015-06-18 08:24:02 -07003453status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003454 audio_policy_dev_state_t state,
3455 SortedVector<audio_io_handle_t>& inputs,
3456 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003457{
Paul McLean9080a4c2015-06-18 08:24:02 -07003458 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003459 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003460
3461 if (audio_device_is_digital(device)) {
3462 // erase all current sample rates, formats and channel masks
3463 devDesc->clearCapabilities();
3464 }
3465
Eric Laurentd4692962014-05-05 18:13:44 -07003466 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3467 // first list already open inputs that can be routed to this device
3468 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3469 desc = mInputs.valueAt(input_index);
3470 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3471 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3472 inputs.add(mInputs.keyAt(input_index));
3473 }
3474 }
3475
3476 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003477 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003478 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3479 {
3480 if (mHwModules[module_idx]->mHandle == 0) {
3481 continue;
3482 }
3483 for (size_t profile_index = 0;
3484 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3485 profile_index++)
3486 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003487 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3488
3489 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003490 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003491 address == profile->mSupportedDevices[0]->mAddress) {
3492 profiles.add(profile);
3493 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3494 profile_index, module_idx);
3495 }
Eric Laurentd4692962014-05-05 18:13:44 -07003496 }
3497 }
3498 }
3499
3500 if (profiles.isEmpty() && inputs.isEmpty()) {
3501 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3502 return BAD_VALUE;
3503 }
3504
3505 // open inputs for matching profiles if needed. Direct inputs are also opened to
3506 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3507 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3508
Eric Laurent1c333e22014-05-20 10:48:17 -07003509 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003510 // nothing to do if one input is already opened for this profile
3511 size_t input_index;
3512 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3513 desc = mInputs.valueAt(input_index);
3514 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003515 if (audio_device_is_digital(device)) {
3516 devDesc->importAudioPort(profile);
3517 }
Eric Laurentd4692962014-05-05 18:13:44 -07003518 break;
3519 }
3520 }
3521 if (input_index != mInputs.size()) {
3522 continue;
3523 }
3524
3525 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3526 desc = new AudioInputDescriptor(profile);
3527 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003528 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3529 config.sample_rate = desc->mSamplingRate;
3530 config.channel_mask = desc->mChannelMask;
3531 config.format = desc->mFormat;
3532 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003533 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003534 &input,
3535 &config,
3536 &desc->mDevice,
3537 address,
3538 AUDIO_SOURCE_MIC,
3539 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003540
Eric Laurentcf2c0212014-07-25 16:20:43 -07003541 if (status == NO_ERROR) {
3542 desc->mSamplingRate = config.sample_rate;
3543 desc->mChannelMask = config.channel_mask;
3544 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003545
Eric Laurentd4692962014-05-05 18:13:44 -07003546 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003547 char *param = audio_device_address_to_parameter(device, address);
3548 mpClientInterface->setParameters(input, String8(param));
3549 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003550 }
3551
3552 // Here is where we step through and resolve any "dynamic" fields
3553 String8 reply;
3554 char *value;
3555 if (profile->mSamplingRates[0] == 0) {
3556 reply = mpClientInterface->getParameters(input,
3557 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3558 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3559 reply.string());
3560 value = strpbrk((char *)reply.string(), "=");
3561 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003562 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003563 }
3564 }
3565 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3566 reply = mpClientInterface->getParameters(input,
3567 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3568 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3569 value = strpbrk((char *)reply.string(), "=");
3570 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003571 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003572 }
3573 }
3574 if (profile->mChannelMasks[0] == 0) {
3575 reply = mpClientInterface->getParameters(input,
3576 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3577 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3578 reply.string());
3579 value = strpbrk((char *)reply.string(), "=");
3580 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003581 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003582 }
3583 }
3584 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3585 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3586 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3587 ALOGW("checkInputsForDevice() direct input missing param");
3588 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003589 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003590 }
3591
3592 if (input != 0) {
3593 addInput(input, desc);
3594 }
3595 } // endif input != 0
3596
Eric Laurentcf2c0212014-07-25 16:20:43 -07003597 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003598 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003599 profiles.removeAt(profile_index);
3600 profile_index--;
3601 } else {
3602 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003603 if (audio_device_is_digital(device)) {
3604 devDesc->importAudioPort(profile);
3605 }
Eric Laurentd4692962014-05-05 18:13:44 -07003606 ALOGV("checkInputsForDevice(): adding input %d", input);
3607 }
3608 } // end scan profiles
3609
3610 if (profiles.isEmpty()) {
3611 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3612 return BAD_VALUE;
3613 }
3614 } else {
3615 // Disconnect
3616 // check if one opened input is not needed any more after disconnecting one device
3617 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3618 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003619 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3620 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003621 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3622 mInputs.keyAt(input_index));
3623 inputs.add(mInputs.keyAt(input_index));
3624 }
3625 }
3626 // Clear any profiles associated with the disconnected device.
3627 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3628 if (mHwModules[module_index]->mHandle == 0) {
3629 continue;
3630 }
3631 for (size_t profile_index = 0;
3632 profile_index < mHwModules[module_index]->mInputProfiles.size();
3633 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003634 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Paul McLean9080a4c2015-06-18 08:24:02 -07003635 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003636 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003637 profile_index, module_index);
3638 if (profile->mSamplingRates[0] == 0) {
3639 profile->mSamplingRates.clear();
3640 profile->mSamplingRates.add(0);
3641 }
3642 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3643 profile->mFormats.clear();
3644 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3645 }
3646 if (profile->mChannelMasks[0] == 0) {
3647 profile->mChannelMasks.clear();
3648 profile->mChannelMasks.add(0);
3649 }
3650 }
3651 }
3652 }
3653 } // end disconnect
3654
3655 return NO_ERROR;
3656}
3657
3658
Eric Laurente0720872014-03-11 09:30:41 -07003659void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003660{
3661 ALOGV("closeOutput(%d)", output);
3662
Eric Laurentc75307b2015-03-17 15:29:32 -07003663 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003664 if (outputDesc == NULL) {
3665 ALOGW("closeOutput() unknown output %d", output);
3666 return;
3667 }
François Gaffie036e1e92015-03-19 10:16:24 +01003668 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003669
Eric Laurente552edb2014-03-10 17:42:56 -07003670 // look for duplicated outputs connected to the output being removed.
3671 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003672 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003673 if (dupOutputDesc->isDuplicated() &&
3674 (dupOutputDesc->mOutput1 == outputDesc ||
3675 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003676 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003677 if (dupOutputDesc->mOutput1 == outputDesc) {
3678 outputDesc2 = dupOutputDesc->mOutput2;
3679 } else {
3680 outputDesc2 = dupOutputDesc->mOutput1;
3681 }
3682 // As all active tracks on duplicated output will be deleted,
3683 // and as they were also referenced on the other output, the reference
3684 // count for their stream type must be adjusted accordingly on
3685 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003686 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003687 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003688 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003689 }
3690 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3691 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3692
3693 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003694 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003695 }
3696 }
3697
Eric Laurent05b90f82014-08-27 15:32:29 -07003698 nextAudioPortGeneration();
3699
3700 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3701 if (index >= 0) {
3702 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3703 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3704 mAudioPatches.removeItemsAt(index);
3705 mpClientInterface->onAudioPatchListUpdate();
3706 }
3707
Eric Laurente552edb2014-03-10 17:42:56 -07003708 AudioParameter param;
3709 param.add(String8("closing"), String8("true"));
3710 mpClientInterface->setParameters(output, param.toString());
3711
3712 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003713 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003714 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003715}
3716
3717void AudioPolicyManager::closeInput(audio_io_handle_t input)
3718{
3719 ALOGV("closeInput(%d)", input);
3720
3721 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3722 if (inputDesc == NULL) {
3723 ALOGW("closeInput() unknown input %d", input);
3724 return;
3725 }
3726
Eric Laurent6a94d692014-05-20 11:18:06 -07003727 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003728
3729 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3730 if (index >= 0) {
3731 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3732 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3733 mAudioPatches.removeItemsAt(index);
3734 mpClientInterface->onAudioPatchListUpdate();
3735 }
3736
3737 mpClientInterface->closeInput(input);
3738 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003739}
3740
Eric Laurentc75307b2015-03-17 15:29:32 -07003741SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3742 audio_devices_t device,
3743 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003744{
3745 SortedVector<audio_io_handle_t> outputs;
3746
3747 ALOGVV("getOutputsForDevice() device %04x", device);
3748 for (size_t i = 0; i < openOutputs.size(); i++) {
3749 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003750 i, openOutputs.valueAt(i)->isDuplicated(),
3751 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003752 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3753 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3754 outputs.add(openOutputs.keyAt(i));
3755 }
3756 }
3757 return outputs;
3758}
3759
Eric Laurente0720872014-03-11 09:30:41 -07003760bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003761 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003762{
3763 if (outputs1.size() != outputs2.size()) {
3764 return false;
3765 }
3766 for (size_t i = 0; i < outputs1.size(); i++) {
3767 if (outputs1[i] != outputs2[i]) {
3768 return false;
3769 }
3770 }
3771 return true;
3772}
3773
Eric Laurente0720872014-03-11 09:30:41 -07003774void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003775{
3776 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3777 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3778 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3779 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3780
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003781 // also take into account external policy-related changes: add all outputs which are
3782 // associated with policies in the "before" and "after" output vectors
3783 ALOGVV("checkOutputForStrategy(): policy related outputs");
3784 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003785 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003786 if (desc != 0 && desc->mPolicyMix != NULL) {
3787 srcOutputs.add(desc->mIoHandle);
3788 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3789 }
3790 }
3791 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003792 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003793 if (desc != 0 && desc->mPolicyMix != NULL) {
3794 dstOutputs.add(desc->mIoHandle);
3795 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3796 }
3797 }
3798
Eric Laurente552edb2014-03-10 17:42:56 -07003799 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3800 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3801 strategy, srcOutputs[0], dstOutputs[0]);
3802 // mute strategy while moving tracks from one output to another
3803 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003804 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003805 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003806 setStrategyMute(strategy, true, desc);
3807 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003808 }
3809 }
3810
3811 // Move effects associated to this strategy from previous output to new output
3812 if (strategy == STRATEGY_MEDIA) {
3813 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3814 SortedVector<audio_io_handle_t> moved;
3815 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003816 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3817 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3818 effectDesc->mIo != fxOutput) {
3819 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003820 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3821 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003822 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003823 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003824 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003825 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003826 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003827 }
3828 }
3829 }
3830 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003831 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003832 if (i == AUDIO_STREAM_PATCH) {
3833 continue;
3834 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003835 if (getStrategy((audio_stream_type_t)i) == strategy) {
3836 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003837 }
3838 }
3839 }
3840}
3841
Eric Laurente0720872014-03-11 09:30:41 -07003842void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003843{
François Gaffie2110e042015-03-24 08:41:51 +01003844 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003845 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003846 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003847 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003848 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003849 checkOutputForStrategy(STRATEGY_SONIFICATION);
3850 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003851 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003852 checkOutputForStrategy(STRATEGY_MEDIA);
3853 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003854 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003855}
3856
Eric Laurente0720872014-03-11 09:30:41 -07003857void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003858{
François Gaffie53615e22015-03-19 09:24:12 +01003859 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003860 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003861 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003862 return;
3863 }
3864
Eric Laurent3a4311c2014-03-17 12:00:47 -07003865 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003866 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3867 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3868 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003869 // suspend A2DP output if:
3870 // (NOT already suspended) &&
3871 // ((SCO device is connected &&
3872 // (forced usage for communication || for record is SCO))) ||
3873 // (phone state is ringing || in call)
3874 //
3875 // restore A2DP output if:
3876 // (Already suspended) &&
3877 // ((SCO device is NOT connected ||
3878 // (forced usage NOT for communication && NOT for record is SCO))) &&
3879 // (phone state is NOT ringing && NOT in call)
3880 //
3881 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003882 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003883 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3884 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3885 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3886 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003887
3888 mpClientInterface->restoreOutput(a2dpOutput);
3889 mA2dpSuspended = false;
3890 }
3891 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003892 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003893 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3894 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3895 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3896 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003897
3898 mpClientInterface->suspendOutput(a2dpOutput);
3899 mA2dpSuspended = true;
3900 }
3901 }
3902}
3903
Eric Laurentc75307b2015-03-17 15:29:32 -07003904audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3905 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003906{
3907 audio_devices_t device = AUDIO_DEVICE_NONE;
3908
Eric Laurent6a94d692014-05-20 11:18:06 -07003909 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3910 if (index >= 0) {
3911 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3912 if (patchDesc->mUid != mUidCached) {
3913 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3914 outputDesc->device(), outputDesc->mPatchHandle);
3915 return outputDesc->device();
3916 }
3917 }
3918
Eric Laurente552edb2014-03-10 17:42:56 -07003919 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003920 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003921 // use device for strategy enforced audible
3922 // 2: we are in call or the strategy phone is active on the output:
3923 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003924 // 3: the strategy for enforced audible is active but not enforced on the output:
3925 // use the device for strategy enforced audible
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003926 // 4: the strategy accessibility is active on the output:
Eric Laurent223fd5c2014-11-11 13:43:36 -08003927 // use device for strategy accessibility
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003928 // 5: the strategy sonification is active on the output:
3929 // use device for strategy sonification
3930 // 6: the strategy "respectful" sonification is active on the output:
3931 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003932 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003933 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003934 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003935 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003936 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003937 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003938 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003939 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003940 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3941 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003942 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003943 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003944 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003945 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Jean-Michel Trivi5de234b2015-07-21 11:42:02 -07003946 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
3947 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003948 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003949 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003950 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003951 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003952 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003953 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003954 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003955 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003956 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003957 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003958 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003959 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003960 }
3961
Eric Laurent1c333e22014-05-20 10:48:17 -07003962 ALOGV("getNewOutputDevice() selected device %x", device);
3963 return device;
3964}
3965
3966audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3967{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003968 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003969
3970 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3971 if (index >= 0) {
3972 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3973 if (patchDesc->mUid != mUidCached) {
3974 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3975 inputDesc->mDevice, inputDesc->mPatchHandle);
3976 return inputDesc->mDevice;
3977 }
3978 }
3979
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003980 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003981
Eric Laurente552edb2014-03-10 17:42:56 -07003982 return device;
3983}
3984
Eric Laurente0720872014-03-11 09:30:41 -07003985uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003986 return (uint32_t)getStrategy(stream);
3987}
3988
Eric Laurente0720872014-03-11 09:30:41 -07003989audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003990 // By checking the range of stream before calling getStrategy, we avoid
3991 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3992 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003993 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003994 return AUDIO_DEVICE_NONE;
3995 }
3996 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003997 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003998 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3999 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4000 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004001 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01004002 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004003 devices = outputDesc->device();
4004 break;
4005 }
Eric Laurente552edb2014-03-10 17:42:56 -07004006 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004007
4008 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4009 and doesn't really need to.*/
4010 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4011 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4012 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4013 }
4014
Eric Laurente552edb2014-03-10 17:42:56 -07004015 return devices;
4016}
4017
François Gaffie2110e042015-03-24 08:41:51 +01004018routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4019{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004020 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004021 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004022}
4023
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004024uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4025 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004026 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4027 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4028 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004029 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4030 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4031 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004032 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004033 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004034}
4035
Eric Laurente0720872014-03-11 09:30:41 -07004036void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004037 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004038 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004039 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4040 updateDevicesAndOutputs();
4041 break;
4042 default:
4043 break;
4044 }
4045}
4046
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004047uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
Eric Laurent9459fb02015-08-12 18:36:32 -07004048
4049 // skip beacon mute management if a dedicated TTS output is available
4050 if (mTtsOutputAvailable) {
4051 return 0;
4052 }
4053
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004054 switch(event) {
4055 case STARTING_OUTPUT:
4056 mBeaconMuteRefCount++;
4057 break;
4058 case STOPPING_OUTPUT:
4059 if (mBeaconMuteRefCount > 0) {
4060 mBeaconMuteRefCount--;
4061 }
4062 break;
4063 case STARTING_BEACON:
4064 mBeaconPlayingRefCount++;
4065 break;
4066 case STOPPING_BEACON:
4067 if (mBeaconPlayingRefCount > 0) {
4068 mBeaconPlayingRefCount--;
4069 }
4070 break;
4071 }
4072
4073 if (mBeaconMuteRefCount > 0) {
4074 // any playback causes beacon to be muted
4075 return setBeaconMute(true);
4076 } else {
4077 // no other playback: unmute when beacon starts playing, mute when it stops
4078 return setBeaconMute(mBeaconPlayingRefCount == 0);
4079 }
4080}
4081
4082uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4083 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4084 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4085 // keep track of muted state to avoid repeating mute/unmute operations
4086 if (mBeaconMuted != mute) {
4087 // mute/unmute AUDIO_STREAM_TTS on all outputs
4088 ALOGV("\t muting %d", mute);
4089 uint32_t maxLatency = 0;
4090 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004091 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004092 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004093 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004094 0 /*delay*/, AUDIO_DEVICE_NONE);
4095 const uint32_t latency = desc->latency() * 2;
4096 if (latency > maxLatency) {
4097 maxLatency = latency;
4098 }
4099 }
4100 mBeaconMuted = mute;
4101 return maxLatency;
4102 }
4103 return 0;
4104}
4105
Eric Laurente0720872014-03-11 09:30:41 -07004106audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004107 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004108{
Paul McLeanaa981192015-03-21 09:55:15 -07004109 // Routing
4110 // see if we have an explicit route
4111 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4112 // (getStrategy(stream)).
4113 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4114 // and activity count is non-zero
4115 // the device = the device from the descriptor in the RouteMap, and exit.
4116 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4117 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4118 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004119 // Special case for accessibility strategy which must follow any strategy it is
4120 // currently remapped to
4121 bool strategyMatch = (strat == strategy) ||
4122 ((strategy == STRATEGY_ACCESSIBILITY) &&
4123 ((mEngine->getStrategyForUsage(
4124 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4125 (strat == STRATEGY_MEDIA)));
4126 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004127 return route->mDeviceDescriptor->type();
4128 }
4129 }
4130
Eric Laurente552edb2014-03-10 17:42:56 -07004131 if (fromCache) {
4132 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4133 strategy, mDeviceForStrategy[strategy]);
4134 return mDeviceForStrategy[strategy];
4135 }
François Gaffie2110e042015-03-24 08:41:51 +01004136 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004137}
4138
Eric Laurente0720872014-03-11 09:30:41 -07004139void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004140{
4141 for (int i = 0; i < NUM_STRATEGIES; i++) {
4142 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4143 }
4144 mPreviousOutputs = mOutputs;
4145}
4146
Eric Laurent1f2f2232014-06-02 12:01:23 -07004147uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004148 audio_devices_t prevDevice,
4149 uint32_t delayMs)
4150{
4151 // mute/unmute strategies using an incompatible device combination
4152 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4153 // if unmuting, unmute only after the specified delay
4154 if (outputDesc->isDuplicated()) {
4155 return 0;
4156 }
4157
4158 uint32_t muteWaitMs = 0;
4159 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004160 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004161
4162 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4163 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004164 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004165 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4166 bool doMute = false;
4167
4168 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4169 doMute = true;
4170 outputDesc->mStrategyMutedByDevice[i] = true;
4171 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4172 doMute = true;
4173 outputDesc->mStrategyMutedByDevice[i] = false;
4174 }
Eric Laurent99401132014-05-07 19:48:15 -07004175 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004176 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004177 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004178 // skip output if it does not share any device with current output
4179 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4180 == AUDIO_DEVICE_NONE) {
4181 continue;
4182 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004183 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4184 mute ? "muting" : "unmuting", i, curDevice);
4185 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004186 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004187 if (mute) {
4188 // FIXME: should not need to double latency if volume could be applied
4189 // immediately by the audioflinger mixer. We must account for the delay
4190 // between now and the next time the audioflinger thread for this output
4191 // will process a buffer (which corresponds to one buffer size,
4192 // usually 1/2 or 1/4 of the latency).
4193 if (muteWaitMs < desc->latency() * 2) {
4194 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004195 }
4196 }
4197 }
4198 }
4199 }
4200 }
4201
Eric Laurent99401132014-05-07 19:48:15 -07004202 // temporary mute output if device selection changes to avoid volume bursts due to
4203 // different per device volumes
4204 if (outputDesc->isActive() && (device != prevDevice)) {
4205 if (muteWaitMs < outputDesc->latency() * 2) {
4206 muteWaitMs = outputDesc->latency() * 2;
4207 }
4208 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004209 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004210 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004211 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004212 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004213 muteWaitMs *2, device);
4214 }
4215 }
4216 }
4217
Eric Laurente552edb2014-03-10 17:42:56 -07004218 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4219 if (muteWaitMs > delayMs) {
4220 muteWaitMs -= delayMs;
4221 usleep(muteWaitMs * 1000);
4222 return muteWaitMs;
4223 }
4224 return 0;
4225}
4226
Eric Laurentc75307b2015-03-17 15:29:32 -07004227uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004228 audio_devices_t device,
4229 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004230 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004231 audio_patch_handle_t *patchHandle,
4232 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004233{
Eric Laurentc75307b2015-03-17 15:29:32 -07004234 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004235 AudioParameter param;
4236 uint32_t muteWaitMs;
4237
4238 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004239 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4240 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004241 return muteWaitMs;
4242 }
4243 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4244 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004245 if ((device != AUDIO_DEVICE_NONE) &&
4246 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004247 return 0;
4248 }
4249
4250 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004251 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004252
4253 audio_devices_t prevDevice = outputDesc->mDevice;
4254
Paul McLeanaa981192015-03-21 09:55:15 -07004255 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004256
4257 if (device != AUDIO_DEVICE_NONE) {
4258 outputDesc->mDevice = device;
4259 }
4260 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4261
4262 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004263 // the requested device is AUDIO_DEVICE_NONE
4264 // OR the requested device is the same as current device
4265 // AND force is not specified
4266 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004267 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004268 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4269 !force &&
4270 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004271 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004272 return muteWaitMs;
4273 }
4274
4275 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004276
Eric Laurente552edb2014-03-10 17:42:56 -07004277 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004278 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004279 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004280 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004281 DeviceVector deviceList = (address == NULL) ?
4282 mAvailableOutputDevices.getDevicesFromType(device)
4283 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004284 if (!deviceList.isEmpty()) {
4285 struct audio_patch patch;
4286 outputDesc->toAudioPortConfig(&patch.sources[0]);
4287 patch.num_sources = 1;
4288 patch.num_sinks = 0;
4289 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4290 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004291 patch.num_sinks++;
4292 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004293 ssize_t index;
4294 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4295 index = mAudioPatches.indexOfKey(*patchHandle);
4296 } else {
4297 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4298 }
4299 sp< AudioPatch> patchDesc;
4300 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4301 if (index >= 0) {
4302 patchDesc = mAudioPatches.valueAt(index);
4303 afPatchHandle = patchDesc->mAfPatchHandle;
4304 }
4305
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004307 &afPatchHandle,
4308 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004309 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4310 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004311 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004312 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004313 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004314 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004315 addAudioPatch(patchDesc->mHandle, patchDesc);
4316 } else {
4317 patchDesc->mPatch = patch;
4318 }
4319 patchDesc->mAfPatchHandle = afPatchHandle;
4320 patchDesc->mUid = mUidCached;
4321 if (patchHandle) {
4322 *patchHandle = patchDesc->mHandle;
4323 }
4324 outputDesc->mPatchHandle = patchDesc->mHandle;
4325 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004326 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004327 }
4328 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004329
4330 // inform all input as well
4331 for (size_t i = 0; i < mInputs.size(); i++) {
4332 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004333 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004334 AudioParameter inputCmd = AudioParameter();
4335 ALOGV("%s: inform input %d of device:%d", __func__,
4336 inputDescriptor->mIoHandle, device);
4337 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4338 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4339 inputCmd.toString(),
4340 delayMs);
4341 }
4342 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004343 }
Eric Laurente552edb2014-03-10 17:42:56 -07004344
4345 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004346 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004347
4348 return muteWaitMs;
4349}
4350
Eric Laurentc75307b2015-03-17 15:29:32 -07004351status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004352 int delayMs,
4353 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004354{
Eric Laurent6a94d692014-05-20 11:18:06 -07004355 ssize_t index;
4356 if (patchHandle) {
4357 index = mAudioPatches.indexOfKey(*patchHandle);
4358 } else {
4359 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4360 }
4361 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004362 return INVALID_OPERATION;
4363 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004364 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4365 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004366 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4367 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004368 removeAudioPatch(patchDesc->mHandle);
4369 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004370 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004371 return status;
4372}
4373
4374status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4375 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004376 bool force,
4377 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004378{
4379 status_t status = NO_ERROR;
4380
Eric Laurent1f2f2232014-06-02 12:01:23 -07004381 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004382 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4383 inputDesc->mDevice = device;
4384
4385 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4386 if (!deviceList.isEmpty()) {
4387 struct audio_patch patch;
4388 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004389 // AUDIO_SOURCE_HOTWORD is for internal use only:
4390 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004391 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4392 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004393 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4394 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004395 patch.num_sinks = 1;
4396 //only one input device for now
4397 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004398 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004399 ssize_t index;
4400 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4401 index = mAudioPatches.indexOfKey(*patchHandle);
4402 } else {
4403 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4404 }
4405 sp< AudioPatch> patchDesc;
4406 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4407 if (index >= 0) {
4408 patchDesc = mAudioPatches.valueAt(index);
4409 afPatchHandle = patchDesc->mAfPatchHandle;
4410 }
4411
Eric Laurent1c333e22014-05-20 10:48:17 -07004412 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004413 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004414 0);
4415 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004416 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004417 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004418 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004419 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004420 addAudioPatch(patchDesc->mHandle, patchDesc);
4421 } else {
4422 patchDesc->mPatch = patch;
4423 }
4424 patchDesc->mAfPatchHandle = afPatchHandle;
4425 patchDesc->mUid = mUidCached;
4426 if (patchHandle) {
4427 *patchHandle = patchDesc->mHandle;
4428 }
4429 inputDesc->mPatchHandle = patchDesc->mHandle;
4430 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004431 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004432 }
4433 }
4434 }
4435 return status;
4436}
4437
Eric Laurent6a94d692014-05-20 11:18:06 -07004438status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4439 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004440{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004441 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004442 ssize_t index;
4443 if (patchHandle) {
4444 index = mAudioPatches.indexOfKey(*patchHandle);
4445 } else {
4446 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4447 }
4448 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004449 return INVALID_OPERATION;
4450 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004451 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4452 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004453 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4454 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004455 removeAudioPatch(patchDesc->mHandle);
4456 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004457 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004458 return status;
4459}
4460
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004461sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004462 String8 address,
4463 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004464 audio_format_t& format,
4465 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004466 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004467{
4468 // Choose an input profile based on the requested capture parameters: select the first available
4469 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004470 //
4471 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4472 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004473
4474 for (size_t i = 0; i < mHwModules.size(); i++)
4475 {
4476 if (mHwModules[i]->mHandle == 0) {
4477 continue;
4478 }
4479 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4480 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004481 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004482 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004483 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004484 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004485 format,
4486 &format /*updatedFormat*/,
4487 channelMask,
4488 &channelMask /*updatedChannelMask*/,
4489 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004490
Eric Laurente552edb2014-03-10 17:42:56 -07004491 return profile;
4492 }
4493 }
4494 }
4495 return NULL;
4496}
4497
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004498
4499audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004500 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004501{
François Gaffie036e1e92015-03-19 10:16:24 +01004502 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4503 audio_devices_t selectedDeviceFromMix =
4504 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004505
François Gaffie036e1e92015-03-19 10:16:24 +01004506 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4507 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004508 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004509 return getDeviceForInputSource(inputSource);
4510}
4511
4512audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4513{
Paul McLean466dc8e2015-04-17 13:15:36 -06004514 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4515 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004516 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004517 return route->mDeviceDescriptor->type();
4518 }
4519 }
4520
4521 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004522}
4523
Eric Laurente0720872014-03-11 09:30:41 -07004524float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004525 int index,
4526 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004527{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004528 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004529
4530 // if a headset is connected, apply the following rules to ring tones and notifications
4531 // to avoid sound level bursts in user's ears:
4532 // - always attenuate ring tones and notifications volume by 6dB
4533 // - if music is playing, always limit the volume to current music volume,
4534 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004535 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004536 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4537 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4538 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4539 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4540 ((stream_strategy == STRATEGY_SONIFICATION)
4541 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004542 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004543 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004544 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4545 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004546 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004547 // when the phone is ringing we must consider that music could have been paused just before
4548 // by the music application and behave as if music was active if the last music track was
4549 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004550 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004551 mLimitRingtoneVolume) {
4552 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004553 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4554 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004555 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004556 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4557 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4558 if (volumeDb > minVolDB) {
4559 volumeDb = minVolDB;
4560 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004561 }
4562 }
4563 }
4564
Eric Laurentffbc80f2015-03-18 18:30:19 -07004565 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004566}
4567
Eric Laurente0720872014-03-11 09:30:41 -07004568status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004569 int index,
4570 const sp<AudioOutputDescriptor>& outputDesc,
4571 audio_devices_t device,
4572 int delayMs,
4573 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004574{
Eric Laurente552edb2014-03-10 17:42:56 -07004575 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004576 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004577 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004578 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004579 return NO_ERROR;
4580 }
François Gaffie2110e042015-03-24 08:41:51 +01004581 audio_policy_forced_cfg_t forceUseForComm =
4582 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004583 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004584 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4585 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004586 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004587 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004588 return INVALID_OPERATION;
4589 }
4590
Eric Laurentc75307b2015-03-17 15:29:32 -07004591 if (device == AUDIO_DEVICE_NONE) {
4592 device = outputDesc->device();
4593 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004594
Eric Laurentffbc80f2015-03-18 18:30:19 -07004595 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004596 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004597 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004598 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004599
Eric Laurentffbc80f2015-03-18 18:30:19 -07004600 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004601
Eric Laurent3b73df72014-03-11 09:06:29 -07004602 if (stream == AUDIO_STREAM_VOICE_CALL ||
4603 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004604 float voiceVolume;
4605 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004606 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004607 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004608 } else {
4609 voiceVolume = 1.0;
4610 }
4611
Eric Laurentc75307b2015-03-17 15:29:32 -07004612 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004613 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4614 mLastVoiceVolume = voiceVolume;
4615 }
4616 }
4617
4618 return NO_ERROR;
4619}
4620
Eric Laurentc75307b2015-03-17 15:29:32 -07004621void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4622 audio_devices_t device,
4623 int delayMs,
4624 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004625{
Eric Laurentc75307b2015-03-17 15:29:32 -07004626 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004627
Eric Laurent3b73df72014-03-11 09:06:29 -07004628 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004629 if (stream == AUDIO_STREAM_PATCH) {
4630 continue;
4631 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004632 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004633 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4634 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004635 device,
4636 delayMs,
4637 force);
4638 }
4639}
4640
Eric Laurente0720872014-03-11 09:30:41 -07004641void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004642 bool on,
4643 const sp<AudioOutputDescriptor>& outputDesc,
4644 int delayMs,
4645 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004646{
Eric Laurent72249d52015-06-08 11:12:15 -07004647 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4648 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004649 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004650 if (stream == AUDIO_STREAM_PATCH) {
4651 continue;
4652 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004653 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004654 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004655 }
4656 }
4657}
4658
Eric Laurente0720872014-03-11 09:30:41 -07004659void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004660 bool on,
4661 const sp<AudioOutputDescriptor>& outputDesc,
4662 int delayMs,
4663 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004664{
Eric Laurentc75307b2015-03-17 15:29:32 -07004665 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004666 if (device == AUDIO_DEVICE_NONE) {
4667 device = outputDesc->device();
4668 }
4669
Eric Laurentc75307b2015-03-17 15:29:32 -07004670 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4671 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004672
4673 if (on) {
4674 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004675 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004676 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004677 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004678 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004679 }
4680 }
4681 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4682 outputDesc->mMuteCount[stream]++;
4683 } else {
4684 if (outputDesc->mMuteCount[stream] == 0) {
4685 ALOGV("setStreamMute() unmuting non muted stream!");
4686 return;
4687 }
4688 if (--outputDesc->mMuteCount[stream] == 0) {
4689 checkAndSetVolume(stream,
4690 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004691 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004692 device,
4693 delayMs);
4694 }
4695 }
4696}
4697
Eric Laurente0720872014-03-11 09:30:41 -07004698void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004699 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004700{
Eric Laurent87ffa392015-05-22 10:32:38 -07004701 if(!hasPrimaryOutput()) {
4702 return;
4703 }
4704
Eric Laurente552edb2014-03-10 17:42:56 -07004705 // if the stream pertains to sonification strategy and we are in call we must
4706 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4707 // in the device used for phone strategy and play the tone if the selected device does not
4708 // interfere with the device used for phone strategy
4709 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4710 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004711 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004712 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4713 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004714 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004715 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4716 stream, starting, outputDesc->mDevice, stateChange);
4717 if (outputDesc->mRefCount[stream]) {
4718 int muteCount = 1;
4719 if (stateChange) {
4720 muteCount = outputDesc->mRefCount[stream];
4721 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004722 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004723 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4724 for (int i = 0; i < muteCount; i++) {
4725 setStreamMute(stream, starting, mPrimaryOutput);
4726 }
4727 } else {
4728 ALOGV("handleIncallSonification() high visibility");
4729 if (outputDesc->device() &
4730 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4731 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4732 for (int i = 0; i < muteCount; i++) {
4733 setStreamMute(stream, starting, mPrimaryOutput);
4734 }
4735 }
4736 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004737 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4738 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004739 } else {
4740 mpClientInterface->stopTone();
4741 }
4742 }
4743 }
4744 }
4745}
4746
Paul McLeanaa981192015-03-21 09:55:15 -07004747
Paul McLean466dc8e2015-04-17 13:15:36 -06004748
Eric Laurente0720872014-03-11 09:30:41 -07004749void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004750{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004751 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004752 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004753 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004754 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004755 mAvailableOutputDevices.add(mDefaultOutputDevice);
4756 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004757
4758 module = new HwModule("primary");
4759
Eric Laurent322b4d22015-04-03 15:57:54 -07004760 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4761 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004762 profile->mSamplingRates.add(44100);
4763 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4764 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004765 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004766 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4767 module->mOutputProfiles.add(profile);
4768
Eric Laurent322b4d22015-04-03 15:57:54 -07004769 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4770 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004771 profile->mSamplingRates.add(8000);
4772 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4773 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004774 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004775 module->mInputProfiles.add(profile);
4776
4777 mHwModules.add(module);
4778}
4779
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004780audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4781{
4782 // flags to stream type mapping
4783 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4784 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4785 }
4786 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4787 return AUDIO_STREAM_BLUETOOTH_SCO;
4788 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004789 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4790 return AUDIO_STREAM_TTS;
4791 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004792
4793 // usage to stream type mapping
4794 switch (attr->usage) {
4795 case AUDIO_USAGE_MEDIA:
4796 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004797 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4798 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004799 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004800 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4801 return AUDIO_STREAM_ALARM;
4802 }
4803 if (isStreamActive(AUDIO_STREAM_RING)) {
4804 return AUDIO_STREAM_RING;
4805 }
4806 if (isInCall()) {
4807 return AUDIO_STREAM_VOICE_CALL;
4808 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004809 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004810 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4811 return AUDIO_STREAM_SYSTEM;
4812 case AUDIO_USAGE_VOICE_COMMUNICATION:
4813 return AUDIO_STREAM_VOICE_CALL;
4814
4815 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4816 return AUDIO_STREAM_DTMF;
4817
4818 case AUDIO_USAGE_ALARM:
4819 return AUDIO_STREAM_ALARM;
4820 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4821 return AUDIO_STREAM_RING;
4822
4823 case AUDIO_USAGE_NOTIFICATION:
4824 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4825 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4826 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4827 case AUDIO_USAGE_NOTIFICATION_EVENT:
4828 return AUDIO_STREAM_NOTIFICATION;
4829
4830 case AUDIO_USAGE_UNKNOWN:
4831 default:
4832 return AUDIO_STREAM_MUSIC;
4833 }
4834}
Eric Laurente83b55d2014-11-14 10:06:21 -08004835
François Gaffie53615e22015-03-19 09:24:12 +01004836bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4837{
Eric Laurente83b55d2014-11-14 10:06:21 -08004838 // has flags that map to a strategy?
4839 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4840 return true;
4841 }
4842
4843 // has known usage?
4844 switch (paa->usage) {
4845 case AUDIO_USAGE_UNKNOWN:
4846 case AUDIO_USAGE_MEDIA:
4847 case AUDIO_USAGE_VOICE_COMMUNICATION:
4848 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4849 case AUDIO_USAGE_ALARM:
4850 case AUDIO_USAGE_NOTIFICATION:
4851 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4852 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4853 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4854 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4855 case AUDIO_USAGE_NOTIFICATION_EVENT:
4856 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4857 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4858 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4859 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004860 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004861 break;
4862 default:
4863 return false;
4864 }
4865 return true;
4866}
4867
François Gaffiead3183e2015-03-18 16:55:35 +01004868bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4869 routing_strategy strategy, uint32_t inPastMs,
4870 nsecs_t sysTime) const
4871{
4872 if ((sysTime == 0) && (inPastMs != 0)) {
4873 sysTime = systemTime();
4874 }
4875 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4876 if (i == AUDIO_STREAM_PATCH) {
4877 continue;
4878 }
4879 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4880 (NUM_STRATEGIES == strategy)) &&
4881 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4882 return true;
4883 }
4884 }
4885 return false;
4886}
4887
François Gaffie2110e042015-03-24 08:41:51 +01004888audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4889{
4890 return mEngine->getForceUse(usage);
4891}
4892
4893bool AudioPolicyManager::isInCall()
4894{
4895 return isStateInCall(mEngine->getPhoneState());
4896}
4897
4898bool AudioPolicyManager::isStateInCall(int state)
4899{
4900 return is_state_in_call(state);
4901}
4902
Eric Laurente552edb2014-03-10 17:42:56 -07004903}; // namespace android