blob: 6befb8dad6588bf6780f4fcdd5c23d035726b602 [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 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
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()
430 if (isInCall()) {
431 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
439 // force reevaluating accessibility routing when call starts
440 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 }
517 }
518
519 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 if (state == AUDIO_MODE_RINGTONE &&
521 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700522 mLimitRingtoneVolume = true;
523 } else {
524 mLimitRingtoneVolume = false;
525 }
526}
527
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700528audio_mode_t AudioPolicyManager::getPhoneState() {
529 return mEngine->getPhoneState();
530}
531
Eric Laurente0720872014-03-11 09:30:41 -0700532void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700533 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700534{
François Gaffie2110e042015-03-24 08:41:51 +0100535 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700536
François Gaffie2110e042015-03-24 08:41:51 +0100537 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
538 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
539 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700540 }
François Gaffie2110e042015-03-24 08:41:51 +0100541 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
542 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
543 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700544
545 // check for device and output changes triggered by new force usage
546 checkA2dpSuspend();
547 checkOutputForAllStrategies();
548 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700549 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700550 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
551 updateCallRouting(newDevice);
552 }
Eric Laurente552edb2014-03-10 17:42:56 -0700553 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700554 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
555 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
556 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
557 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700558 }
Eric Laurente552edb2014-03-10 17:42:56 -0700559 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700560 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700561 }
562 }
563
François Gaffie53615e22015-03-19 09:24:12 +0100564 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700565 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700566 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700567 }
568
569}
570
Eric Laurente0720872014-03-11 09:30:41 -0700571void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700572{
573 ALOGV("setSystemProperty() property %s, value %s", property, value);
574}
575
576// Find a direct output profile compatible with the parameters passed, even if the input flags do
577// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800578sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700579 audio_devices_t device,
580 uint32_t samplingRate,
581 audio_format_t format,
582 audio_channel_mask_t channelMask,
583 audio_output_flags_t flags)
584{
Eric Laurent861a6282015-05-18 15:40:16 -0700585 // only retain flags that will drive the direct output profile selection
586 // if explicitly requested
587 static const uint32_t kRelevantFlags =
588 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
589 flags =
590 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
591
592 sp<IOProfile> profile;
593
Eric Laurente552edb2014-03-10 17:42:56 -0700594 for (size_t i = 0; i < mHwModules.size(); i++) {
595 if (mHwModules[i]->mHandle == 0) {
596 continue;
597 }
598 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700599 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
600 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700601 samplingRate, NULL /*updatedSamplingRate*/,
602 format, NULL /*updatedFormat*/,
603 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700604 flags)) {
605 continue;
606 }
607 // reject profiles not corresponding to a device currently available
608 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
609 continue;
610 }
611 // if several profiles are compatible, give priority to one with offload capability
612 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
613 continue;
614 }
615 profile = curProfile;
616 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
617 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700618 }
Eric Laurente552edb2014-03-10 17:42:56 -0700619 }
620 }
Eric Laurent861a6282015-05-18 15:40:16 -0700621 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700622}
623
Eric Laurente0720872014-03-11 09:30:41 -0700624audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100625 uint32_t samplingRate,
626 audio_format_t format,
627 audio_channel_mask_t channelMask,
628 audio_output_flags_t flags,
629 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700630{
Eric Laurent3b73df72014-03-11 09:06:29 -0700631 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700632 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
633 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
634 device, stream, samplingRate, format, channelMask, flags);
635
Eric Laurente83b55d2014-11-14 10:06:21 -0800636 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
637 stream, samplingRate,format, channelMask,
638 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700639}
640
Eric Laurente83b55d2014-11-14 10:06:21 -0800641status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
642 audio_io_handle_t *output,
643 audio_session_t session,
644 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700645 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800646 uint32_t samplingRate,
647 audio_format_t format,
648 audio_channel_mask_t channelMask,
649 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700650 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800651 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700652{
Eric Laurente83b55d2014-11-14 10:06:21 -0800653 audio_attributes_t attributes;
654 if (attr != NULL) {
655 if (!isValidAttributes(attr)) {
656 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
657 attr->usage, attr->content_type, attr->flags,
658 attr->tags);
659 return BAD_VALUE;
660 }
661 attributes = *attr;
662 } else {
663 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
664 ALOGE("getOutputForAttr(): invalid stream type");
665 return BAD_VALUE;
666 }
667 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700668 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700669 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100670 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
671 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
672 if (!audio_is_linear_pcm(format)) {
673 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800674 }
François Gaffie036e1e92015-03-19 10:16:24 +0100675 *stream = streamTypefromAttributesInt(&attributes);
676 *output = desc->mIoHandle;
677 ALOGV("getOutputForAttr() returns output %d", *output);
678 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800679 }
680 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
681 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
682 return BAD_VALUE;
683 }
684
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700685 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
686 " session %d selectedDeviceId %d",
687 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
688 session, selectedDeviceId);
689
690 *stream = streamTypefromAttributesInt(&attributes);
691
692 // Explicit routing?
693 sp<DeviceDescriptor> deviceDesc;
694 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
695 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
696 deviceDesc = mAvailableOutputDevices[i];
697 break;
698 }
699 }
700 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700701
Eric Laurente83b55d2014-11-14 10:06:21 -0800702 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700703 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700704
Eric Laurente83b55d2014-11-14 10:06:21 -0800705 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700706 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
707 }
708
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700709 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710 device, samplingRate, format, channelMask, flags);
711
Eric Laurente83b55d2014-11-14 10:06:21 -0800712 *output = getOutputForDevice(device, session, *stream,
713 samplingRate, format, channelMask,
714 flags, offloadInfo);
715 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700716 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800717 return INVALID_OPERATION;
718 }
Paul McLeanaa981192015-03-21 09:55:15 -0700719
Eric Laurente83b55d2014-11-14 10:06:21 -0800720 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700721}
722
723audio_io_handle_t AudioPolicyManager::getOutputForDevice(
724 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800725 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 audio_stream_type_t stream,
727 uint32_t samplingRate,
728 audio_format_t format,
729 audio_channel_mask_t channelMask,
730 audio_output_flags_t flags,
731 const audio_offload_info_t *offloadInfo)
732{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700733 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700734 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700735 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700736
Eric Laurente552edb2014-03-10 17:42:56 -0700737#ifdef AUDIO_POLICY_TEST
738 if (mCurOutput != 0) {
739 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
740 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
741
742 if (mTestOutputs[mCurOutput] == 0) {
743 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700744 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
745 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700746 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700747 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700748 outputDesc->mFlags =
749 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700750 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700751 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
752 config.sample_rate = mTestSamplingRate;
753 config.channel_mask = mTestChannels;
754 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700755 if (offloadInfo != NULL) {
756 config.offload_info = *offloadInfo;
757 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700758 status = mpClientInterface->openOutput(0,
759 &mTestOutputs[mCurOutput],
760 &config,
761 &outputDesc->mDevice,
762 String8(""),
763 &outputDesc->mLatency,
764 outputDesc->mFlags);
765 if (status == NO_ERROR) {
766 outputDesc->mSamplingRate = config.sample_rate;
767 outputDesc->mFormat = config.format;
768 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700769 AudioParameter outputCmd = AudioParameter();
770 outputCmd.addInt(String8("set_id"),mCurOutput);
771 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
772 addOutput(mTestOutputs[mCurOutput], outputDesc);
773 }
774 }
775 return mTestOutputs[mCurOutput];
776 }
777#endif //AUDIO_POLICY_TEST
778
779 // open a direct output if required by specified parameters
780 //force direct flag if offload flag is set: offloading implies a direct output stream
781 // and all common behaviors are driven by checking only the direct flag
782 // this should normally be set appropriately in the policy configuration file
783 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700784 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700785 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700786 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
787 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
788 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800789 // only allow deep buffering for music stream type
790 if (stream != AUDIO_STREAM_MUSIC) {
791 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700792 } else if (/* stream == AUDIO_STREAM_MUSIC && */
793 flags == AUDIO_OUTPUT_FLAG_NONE &&
794 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
795 // use DEEP_BUFFER as default output for music stream type
796 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800797 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700798 if (stream == AUDIO_STREAM_TTS) {
799 flags = AUDIO_OUTPUT_FLAG_TTS;
800 }
Eric Laurente552edb2014-03-10 17:42:56 -0700801
Eric Laurentb732cf52014-09-24 19:08:21 -0700802 sp<IOProfile> profile;
803
804 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700805 // and not explicitly requested
806 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
807 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700808 audio_channel_count_from_out_mask(channelMask) <= 2) {
809 goto non_direct_output;
810 }
811
Eric Laurente552edb2014-03-10 17:42:56 -0700812 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
813 // creating an offloaded track and tearing it down immediately after start when audioflinger
814 // detects there is an active non offloadable effect.
815 // FIXME: We should check the audio session here but we do not have it in this context.
816 // This may prevent offloading in rare situations where effects are left active by apps
817 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700818
Eric Laurente552edb2014-03-10 17:42:56 -0700819 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100820 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700821 profile = getProfileForDirectOutput(device,
822 samplingRate,
823 format,
824 channelMask,
825 (audio_output_flags_t)flags);
826 }
827
Eric Laurent1c333e22014-05-20 10:48:17 -0700828 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700829 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700830
831 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700832 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700833 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
834 outputDesc = desc;
835 // reuse direct output if currently open and configured with same parameters
836 if ((samplingRate == outputDesc->mSamplingRate) &&
837 (format == outputDesc->mFormat) &&
838 (channelMask == outputDesc->mChannelMask)) {
839 outputDesc->mDirectOpenCount++;
840 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
841 return mOutputs.keyAt(i);
842 }
843 }
844 }
845 // close direct output if currently open and configured with different parameters
846 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700847 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700848 }
Eric Laurent861a6282015-05-18 15:40:16 -0700849
850 // if the selected profile is offloaded and no offload info was specified,
851 // create a default one
852 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
853 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
854 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
855 defaultOffloadInfo.sample_rate = samplingRate;
856 defaultOffloadInfo.channel_mask = channelMask;
857 defaultOffloadInfo.format = format;
858 defaultOffloadInfo.stream_type = stream;
859 defaultOffloadInfo.bit_rate = 0;
860 defaultOffloadInfo.duration_us = -1;
861 defaultOffloadInfo.has_video = true; // conservative
862 defaultOffloadInfo.is_streaming = true; // likely
863 offloadInfo = &defaultOffloadInfo;
864 }
865
Eric Laurentc75307b2015-03-17 15:29:32 -0700866 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700867 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700868 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700869 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700870 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
871 config.sample_rate = samplingRate;
872 config.channel_mask = channelMask;
873 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700874 if (offloadInfo != NULL) {
875 config.offload_info = *offloadInfo;
876 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700877 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700878 &output,
879 &config,
880 &outputDesc->mDevice,
881 String8(""),
882 &outputDesc->mLatency,
883 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700884
885 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700886 if (status != NO_ERROR ||
887 (samplingRate != 0 && samplingRate != config.sample_rate) ||
888 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
889 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700890 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
891 "format %d %d, channelMask %04x %04x", output, samplingRate,
892 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
893 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700894 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700895 mpClientInterface->closeOutput(output);
896 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800897 // fall back to mixer output if possible when the direct output could not be open
898 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
899 goto non_direct_output;
900 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700901 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700902 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700903 outputDesc->mSamplingRate = config.sample_rate;
904 outputDesc->mChannelMask = config.channel_mask;
905 outputDesc->mFormat = config.format;
906 outputDesc->mRefCount[stream] = 0;
907 outputDesc->mStopTime[stream] = 0;
908 outputDesc->mDirectOpenCount = 1;
909
Eric Laurente552edb2014-03-10 17:42:56 -0700910 audio_io_handle_t srcOutput = getOutputForEffect();
911 addOutput(output, outputDesc);
912 audio_io_handle_t dstOutput = getOutputForEffect();
913 if (dstOutput == output) {
914 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
915 }
916 mPreviousOutputs = mOutputs;
917 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700918 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700919 return output;
920 }
921
Eric Laurentb732cf52014-09-24 19:08:21 -0700922non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700923 // ignoring channel mask due to downmix capability in mixer
924
925 // open a non direct output
926
927 // for non direct outputs, only PCM is supported
928 if (audio_is_linear_pcm(format)) {
929 // get which output is suitable for the specified stream. The actual
930 // routing change will happen when startOutput() will be called
931 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
932
Eric Laurent8838a382014-09-08 16:44:28 -0700933 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
934 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
935 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700936 }
937 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
938 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
939
Paul McLeanaa981192015-03-21 09:55:15 -0700940 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700941
942 return output;
943}
944
Eric Laurente0720872014-03-11 09:30:41 -0700945audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700946 audio_output_flags_t flags,
947 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700948{
949 // select one output among several that provide a path to a particular device or set of
950 // devices (the list was previously build by getOutputsForDevice()).
951 // The priority is as follows:
952 // 1: the output with the highest number of requested policy flags
953 // 2: the primary output
954 // 3: the first output in the list
955
956 if (outputs.size() == 0) {
957 return 0;
958 }
959 if (outputs.size() == 1) {
960 return outputs[0];
961 }
962
963 int maxCommonFlags = 0;
964 audio_io_handle_t outputFlags = 0;
965 audio_io_handle_t outputPrimary = 0;
966
967 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700968 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700969 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700970 // if a valid format is specified, skip output if not compatible
971 if (format != AUDIO_FORMAT_INVALID) {
972 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
973 if (format != outputDesc->mFormat) {
974 continue;
975 }
976 } else if (!audio_is_linear_pcm(format)) {
977 continue;
978 }
979 }
980
Eric Laurent3b73df72014-03-11 09:06:29 -0700981 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700982 if (commonFlags > maxCommonFlags) {
983 outputFlags = outputs[i];
984 maxCommonFlags = commonFlags;
985 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
986 }
987 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
988 outputPrimary = outputs[i];
989 }
990 }
991 }
992
993 if (outputFlags != 0) {
994 return outputFlags;
995 }
996 if (outputPrimary != 0) {
997 return outputPrimary;
998 }
999
1000 return outputs[0];
1001}
1002
Eric Laurente0720872014-03-11 09:30:41 -07001003status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001004 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001005 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001006{
Paul McLeanaa981192015-03-21 09:55:15 -07001007 ALOGV("startOutput() output %d, stream %d, session %d",
1008 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001009 ssize_t index = mOutputs.indexOfKey(output);
1010 if (index < 0) {
1011 ALOGW("startOutput() unknown output %d", output);
1012 return BAD_VALUE;
1013 }
1014
Eric Laurentc75307b2015-03-17 15:29:32 -07001015 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1016
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001017 // Routing?
1018 mOutputRoutes.incRouteActivity(session);
1019
Eric Laurentc75307b2015-03-17 15:29:32 -07001020 audio_devices_t newDevice;
1021 if (outputDesc->mPolicyMix != NULL) {
1022 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001023 } else if (mOutputRoutes.hasRouteChanged(session)) {
1024 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001025 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001026 } else {
1027 newDevice = AUDIO_DEVICE_NONE;
1028 }
1029
1030 uint32_t delayMs = 0;
1031
Eric Laurentc75307b2015-03-17 15:29:32 -07001032 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1033
1034 if (status != NO_ERROR) {
1035 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001036 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001037 }
1038 // Automatically enable the remote submix input when output is started on a re routing mix
1039 // of type MIX_TYPE_RECORDERS
1040 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1041 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1042 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1043 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1044 outputDesc->mPolicyMix->mRegistrationId,
1045 "remote-submix");
1046 }
1047
1048 if (delayMs != 0) {
1049 usleep(delayMs * 1000);
1050 }
1051
1052 return status;
1053}
1054
1055status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1056 audio_stream_type_t stream,
1057 audio_devices_t device,
1058 uint32_t *delayMs)
1059{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001060 // cannot start playback of STREAM_TTS if any other output is being used
1061 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001062
1063 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001064 if (stream == AUDIO_STREAM_TTS) {
1065 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001066 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001067 return INVALID_OPERATION;
1068 } else {
1069 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1070 }
1071 } else {
1072 // some playback other than beacon starts
1073 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1074 }
1075
Eric Laurente552edb2014-03-10 17:42:56 -07001076 // increment usage count for this stream on the requested output:
1077 // NOTE that the usage count is the same for duplicated output and hardware output which is
1078 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1079 outputDesc->changeRefCount(stream, 1);
1080
Eric Laurent493404d2015-04-21 15:07:36 -07001081 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001082 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001083 if (device == AUDIO_DEVICE_NONE) {
1084 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001085 }
Eric Laurente552edb2014-03-10 17:42:56 -07001086 routing_strategy strategy = getStrategy(stream);
1087 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001088 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1089 (beaconMuteLatency > 0);
1090 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001091 bool force = false;
1092 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001093 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001094 if (desc != outputDesc) {
1095 // force a device change if any other output is managed by the same hw
1096 // module and has a current device selection that differs from selected device.
1097 // In this case, the audio HAL must receive the new device selection so that it can
1098 // change the device currently selected by the other active output.
1099 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001100 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001101 force = true;
1102 }
1103 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001104 // a notification so that audio focus effect can propagate, or that a mute/unmute
1105 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001106 uint32_t latency = desc->latency();
1107 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1108 waitMs = latency;
1109 }
1110 }
1111 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001112 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001113
1114 // handle special case for sonification while in call
1115 if (isInCall()) {
1116 handleIncallSonification(stream, true, false);
1117 }
1118
1119 // apply volume rules for current stream and device if necessary
1120 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001121 mStreams.valueFor(stream).getVolumeIndex(device),
1122 outputDesc,
1123 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001124
1125 // update the outputs if starting an output with a stream that can affect notification
1126 // routing
1127 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001128
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001129 // force reevaluating accessibility routing when ringtone or alarm starts
1130 if (strategy == STRATEGY_SONIFICATION) {
1131 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1132 }
Eric Laurente552edb2014-03-10 17:42:56 -07001133 }
1134 return NO_ERROR;
1135}
1136
1137
Eric Laurente0720872014-03-11 09:30:41 -07001138status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001139 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001140 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001141{
1142 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1143 ssize_t index = mOutputs.indexOfKey(output);
1144 if (index < 0) {
1145 ALOGW("stopOutput() unknown output %d", output);
1146 return BAD_VALUE;
1147 }
1148
Eric Laurentc75307b2015-03-17 15:29:32 -07001149 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001150
Eric Laurentc75307b2015-03-17 15:29:32 -07001151 if (outputDesc->mRefCount[stream] == 1) {
1152 // Automatically disable the remote submix input when output is stopped on a
1153 // re routing mix of type MIX_TYPE_RECORDERS
1154 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1155 outputDesc->mPolicyMix != NULL &&
1156 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1157 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1158 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1159 outputDesc->mPolicyMix->mRegistrationId,
1160 "remote-submix");
1161 }
1162 }
1163
1164 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001165 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001166 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001167 int activityCount = mOutputRoutes.decRouteActivity(session);
1168 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1169
1170 if (forceDeviceUpdate) {
1171 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1172 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001173 }
1174
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001175 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001176}
1177
1178status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001179 audio_stream_type_t stream,
1180 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001181{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001182 // always handle stream stop, check which stream type is stopping
1183 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1184
Eric Laurente552edb2014-03-10 17:42:56 -07001185 // handle special case for sonification while in call
1186 if (isInCall()) {
1187 handleIncallSonification(stream, false, false);
1188 }
1189
1190 if (outputDesc->mRefCount[stream] > 0) {
1191 // decrement usage count of this stream on the output
1192 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001193
Eric Laurente552edb2014-03-10 17:42:56 -07001194 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001195 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001196 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001197 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001198 // delay the device switch by twice the latency because stopOutput() is executed when
1199 // the track stop() command is received and at that time the audio track buffer can
1200 // still contain data that needs to be drained. The latency only covers the audio HAL
1201 // and kernel buffers. Also the latency does not always include additional delay in the
1202 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001203 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001204
1205 // force restoring the device selection on other active outputs if it differs from the
1206 // one being selected for this output
1207 for (size_t i = 0; i < mOutputs.size(); i++) {
1208 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001209 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001210 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001211 desc->isActive() &&
1212 outputDesc->sharesHwModuleWith(desc) &&
1213 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 setOutputDevice(desc,
1215 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001216 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001217 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001218 }
1219 }
1220 // update the outputs if stopping one with a stream that can affect notification routing
1221 handleNotificationRoutingForStream(stream);
1222 }
1223 return NO_ERROR;
1224 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001225 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001226 return INVALID_OPERATION;
1227 }
1228}
1229
Eric Laurente83b55d2014-11-14 10:06:21 -08001230void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001231 audio_stream_type_t stream __unused,
1232 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001233{
1234 ALOGV("releaseOutput() %d", output);
1235 ssize_t index = mOutputs.indexOfKey(output);
1236 if (index < 0) {
1237 ALOGW("releaseOutput() releasing unknown output %d", output);
1238 return;
1239 }
1240
1241#ifdef AUDIO_POLICY_TEST
1242 int testIndex = testOutputIndex(output);
1243 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001244 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001245 if (outputDesc->isActive()) {
1246 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001247 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001248 mTestOutputs[testIndex] = 0;
1249 }
1250 return;
1251 }
1252#endif //AUDIO_POLICY_TEST
1253
Paul McLeanaa981192015-03-21 09:55:15 -07001254 // Routing
1255 mOutputRoutes.removeRoute(session);
1256
Eric Laurentc75307b2015-03-17 15:29:32 -07001257 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001258 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001259 if (desc->mDirectOpenCount <= 0) {
1260 ALOGW("releaseOutput() invalid open count %d for output %d",
1261 desc->mDirectOpenCount, output);
1262 return;
1263 }
1264 if (--desc->mDirectOpenCount == 0) {
1265 closeOutput(output);
1266 // If effects where present on the output, audioflinger moved them to the primary
1267 // output by default: move them back to the appropriate output.
1268 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001269 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001270 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1271 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001272 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001273 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001274 }
1275 }
1276}
1277
1278
Eric Laurentcaf7f482014-11-25 17:50:47 -08001279status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1280 audio_io_handle_t *input,
1281 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001282 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001283 uint32_t samplingRate,
1284 audio_format_t format,
1285 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001286 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001287 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001288 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001289{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001290 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1291 "session %d, flags %#x",
1292 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001293
Eric Laurentcaf7f482014-11-25 17:50:47 -08001294 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001295 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001296 audio_devices_t device;
1297 // handle legacy remote submix case where the address was not always specified
1298 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001299 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001300 audio_source_t inputSource = attr->source;
1301 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001302 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001303
Eric Laurentc447ded2015-01-06 08:47:05 -08001304 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1305 inputSource = AUDIO_SOURCE_MIC;
1306 }
1307 halInputSource = inputSource;
1308
Paul McLean466dc8e2015-04-17 13:15:36 -06001309 // Explicit routing?
1310 sp<DeviceDescriptor> deviceDesc;
1311 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1312 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1313 deviceDesc = mAvailableInputDevices[i];
1314 break;
1315 }
1316 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001317 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001318
Eric Laurentc447ded2015-01-06 08:47:05 -08001319 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001320 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001321 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001322 if (ret != NO_ERROR) {
1323 return ret;
1324 }
1325 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001326 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1327 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001328 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001329 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001330 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001331 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001332 return BAD_VALUE;
1333 }
Eric Laurentc722f302014-12-10 11:21:49 -08001334 if (policyMix != NULL) {
1335 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001336 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1337 // there is an external policy, but this input is attached to a mix of recorders,
1338 // meaning it receives audio injected into the framework, so the recorder doesn't
1339 // know about it and is therefore considered "legacy"
1340 *inputType = API_INPUT_LEGACY;
1341 } else {
1342 // recording a mix of players defined by an external policy, we're rerouting for
1343 // an external policy
1344 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1345 }
Eric Laurentc722f302014-12-10 11:21:49 -08001346 } else if (audio_is_remote_submix_device(device)) {
1347 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001348 *inputType = API_INPUT_MIX_CAPTURE;
1349 } else {
1350 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001351 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001352 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001353 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001354 case AUDIO_SOURCE_VOICE_UPLINK:
1355 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1356 break;
1357 case AUDIO_SOURCE_VOICE_DOWNLINK:
1358 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1359 break;
1360 case AUDIO_SOURCE_VOICE_CALL:
1361 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1362 break;
1363 default:
1364 break;
1365 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001366 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001367 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1368 if (index >= 0) {
1369 *input = mSoundTriggerSessions.valueFor(session);
1370 isSoundTrigger = true;
1371 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1372 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1373 } else {
1374 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1375 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001376 }
1377 }
1378
Andy Hungf129b032015-04-07 13:45:50 -07001379 // find a compatible input profile (not necessarily identical in parameters)
1380 sp<IOProfile> profile;
1381 // samplingRate and flags may be updated by getInputProfile
1382 uint32_t profileSamplingRate = samplingRate;
1383 audio_format_t profileFormat = format;
1384 audio_channel_mask_t profileChannelMask = channelMask;
1385 audio_input_flags_t profileFlags = flags;
1386 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001387 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001388 profileSamplingRate, profileFormat, profileChannelMask,
1389 profileFlags);
1390 if (profile != 0) {
1391 break; // success
1392 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1393 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1394 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001395 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1396 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001397 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001398 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001399 }
Eric Laurente552edb2014-03-10 17:42:56 -07001400 }
1401
Eric Laurent322b4d22015-04-03 15:57:54 -07001402 if (profile->getModuleHandle() == 0) {
1403 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001404 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001405 }
1406
1407 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001408 config.sample_rate = profileSamplingRate;
1409 config.channel_mask = profileChannelMask;
1410 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001411
Eric Laurent322b4d22015-04-03 15:57:54 -07001412 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001413 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001414 &config,
1415 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001416 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001417 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001418 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001419
1420 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001421 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001422 (profileSamplingRate != config.sample_rate) ||
1423 (profileFormat != config.format) ||
1424 (profileChannelMask != config.channel_mask)) {
1425 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1426 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001427 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001428 if (*input != AUDIO_IO_HANDLE_NONE) {
1429 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001430 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001431 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001432 }
1433
Eric Laurent1f2f2232014-06-02 12:01:23 -07001434 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001435 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001436 inputDesc->mRefCount = 0;
1437 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001438 inputDesc->mSamplingRate = profileSamplingRate;
1439 inputDesc->mFormat = profileFormat;
1440 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001441 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001442 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001443 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001444 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001445
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001446 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001447
Eric Laurentcaf7f482014-11-25 17:50:47 -08001448 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001449 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001450
Eric Laurentcaf7f482014-11-25 17:50:47 -08001451 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001452}
1453
Eric Laurent4dc68062014-07-28 17:26:49 -07001454status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1455 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001456{
1457 ALOGV("startInput() input %d", input);
1458 ssize_t index = mInputs.indexOfKey(input);
1459 if (index < 0) {
1460 ALOGW("startInput() unknown input %d", input);
1461 return BAD_VALUE;
1462 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001463 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001464
Eric Laurentc722f302014-12-10 11:21:49 -08001465 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001466 if (index < 0) {
1467 ALOGW("startInput() unknown session %d on input %d", session, input);
1468 return BAD_VALUE;
1469 }
1470
Glenn Kasten74a8e252014-07-24 14:09:55 -07001471 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001472 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001473
1474 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001475 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001476 if (activeInput != 0 && activeInput != input) {
1477
1478 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1479 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001480 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001481 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001482 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001483 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1484 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001485 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001486 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001487 return INVALID_OPERATION;
1488 }
1489 }
1490 }
1491
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001492 // Routing?
1493 mInputRoutes.incRouteActivity(session);
1494
Paul McLean466dc8e2015-04-17 13:15:36 -06001495 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001496 // if input maps to a dynamic policy with an activity listener, notify of state change
1497 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001498 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001499 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1500 MIX_STATE_MIXING);
1501 }
1502
François Gaffie53615e22015-03-19 09:24:12 +01001503 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001504 SoundTrigger::setCaptureState(true);
1505 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001506 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001507
Eric Laurentc722f302014-12-10 11:21:49 -08001508 // automatically enable the remote submix output when input is started if not
1509 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001510 // For remote submix (a virtual device), we open only one input per capture request.
1511 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001512 String8 address = String8("");
1513 if (inputDesc->mPolicyMix == NULL) {
1514 address = String8("0");
1515 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1516 address = inputDesc->mPolicyMix->mRegistrationId;
1517 }
1518 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001519 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001520 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001521 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001522 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001523 }
Eric Laurente552edb2014-03-10 17:42:56 -07001524 }
1525
Eric Laurente552edb2014-03-10 17:42:56 -07001526 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1527
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001528 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001529 return NO_ERROR;
1530}
1531
Eric Laurent4dc68062014-07-28 17:26:49 -07001532status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1533 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001534{
1535 ALOGV("stopInput() input %d", input);
1536 ssize_t index = mInputs.indexOfKey(input);
1537 if (index < 0) {
1538 ALOGW("stopInput() unknown input %d", input);
1539 return BAD_VALUE;
1540 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001541 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001542
Eric Laurentc722f302014-12-10 11:21:49 -08001543 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001544 if (index < 0) {
1545 ALOGW("stopInput() unknown session %d on input %d", session, input);
1546 return BAD_VALUE;
1547 }
1548
Eric Laurente552edb2014-03-10 17:42:56 -07001549 if (inputDesc->mRefCount == 0) {
1550 ALOGW("stopInput() input %d already stopped", input);
1551 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001552 }
1553
1554 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001555
1556 // Routing?
1557 mInputRoutes.decRouteActivity(session);
1558
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001559 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001560 // if input maps to a dynamic policy with an activity listener, notify of state change
1561 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001562 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001563 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1564 MIX_STATE_IDLE);
1565 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001566
Eric Laurentc722f302014-12-10 11:21:49 -08001567 // automatically disable the remote submix output when input is stopped if not
1568 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001569 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001570 String8 address = String8("");
1571 if (inputDesc->mPolicyMix == NULL) {
1572 address = String8("0");
1573 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1574 address = inputDesc->mPolicyMix->mRegistrationId;
1575 }
1576 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001577 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001578 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001579 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001580 }
Eric Laurente552edb2014-03-10 17:42:56 -07001581 }
1582
Eric Laurent1c333e22014-05-20 10:48:17 -07001583 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001584
François Gaffie53615e22015-03-19 09:24:12 +01001585 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001586 SoundTrigger::setCaptureState(false);
1587 }
Eric Laurente552edb2014-03-10 17:42:56 -07001588 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001589 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001590}
1591
Eric Laurent4dc68062014-07-28 17:26:49 -07001592void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1593 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001594{
1595 ALOGV("releaseInput() %d", input);
1596 ssize_t index = mInputs.indexOfKey(input);
1597 if (index < 0) {
1598 ALOGW("releaseInput() releasing unknown input %d", input);
1599 return;
1600 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001601
1602 // Routing
1603 mInputRoutes.removeRoute(session);
1604
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001605 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1606 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001607
Eric Laurentc722f302014-12-10 11:21:49 -08001608 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001609 if (index < 0) {
1610 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1611 return;
1612 }
Eric Laurentc722f302014-12-10 11:21:49 -08001613 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001614 if (inputDesc->mOpenRefCount == 0) {
1615 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1616 return;
1617 }
1618 inputDesc->mOpenRefCount--;
1619 if (inputDesc->mOpenRefCount > 0) {
1620 ALOGV("releaseInput() exit > 0");
1621 return;
1622 }
1623
Eric Laurent05b90f82014-08-27 15:32:29 -07001624 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001625 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001626 ALOGV("releaseInput() exit");
1627}
1628
Eric Laurentd4692962014-05-05 18:13:44 -07001629void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001630 bool patchRemoved = false;
1631
Eric Laurentd4692962014-05-05 18:13:44 -07001632 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001633 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1634 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1635 if (patch_index >= 0) {
1636 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1637 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1638 mAudioPatches.removeItemsAt(patch_index);
1639 patchRemoved = true;
1640 }
Eric Laurentd4692962014-05-05 18:13:44 -07001641 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1642 }
1643 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001644 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001645
1646 if (patchRemoved) {
1647 mpClientInterface->onAudioPatchListUpdate();
1648 }
Eric Laurentd4692962014-05-05 18:13:44 -07001649}
1650
Eric Laurente0720872014-03-11 09:30:41 -07001651void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001652 int indexMin,
1653 int indexMax)
1654{
1655 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001656 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001657 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1658 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001659 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001660 }
Eric Laurente552edb2014-03-10 17:42:56 -07001661}
1662
Eric Laurente0720872014-03-11 09:30:41 -07001663status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001664 int index,
1665 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001666{
1667
Eric Laurentc75307b2015-03-17 15:29:32 -07001668 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1669 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001670 return BAD_VALUE;
1671 }
1672 if (!audio_is_output_device(device)) {
1673 return BAD_VALUE;
1674 }
1675
1676 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001677 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001678
1679 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1680 stream, device, index);
1681
1682 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1683 // clear all device specific values
1684 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001685 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001686 }
François Gaffiedfd74092015-03-19 12:10:59 +01001687 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001688
Eric Laurent31551f82014-10-10 18:21:56 -07001689 // update volume on all outputs whose current device is also selected by the same
1690 // strategy as the device specified by the caller
1691 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001692
1693
1694 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1695 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1696 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001697 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001698 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1699 }
1700 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1701 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001702 return NO_ERROR;
1703 }
Eric Laurente552edb2014-03-10 17:42:56 -07001704 status_t status = NO_ERROR;
1705 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001706 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1707 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001708 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001709 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001710 if (volStatus != NO_ERROR) {
1711 status = volStatus;
1712 }
1713 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001714 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1715 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001716 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001717 }
Eric Laurente552edb2014-03-10 17:42:56 -07001718 }
1719 return status;
1720}
1721
Eric Laurente0720872014-03-11 09:30:41 -07001722status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001723 int *index,
1724 audio_devices_t device)
1725{
1726 if (index == NULL) {
1727 return BAD_VALUE;
1728 }
1729 if (!audio_is_output_device(device)) {
1730 return BAD_VALUE;
1731 }
1732 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1733 // the strategy the stream belongs to.
1734 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1735 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1736 }
François Gaffiedfd74092015-03-19 12:10:59 +01001737 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001738
Eric Laurentc75307b2015-03-17 15:29:32 -07001739 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001740 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1741 return NO_ERROR;
1742}
1743
Eric Laurente0720872014-03-11 09:30:41 -07001744audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001745 const SortedVector<audio_io_handle_t>& outputs)
1746{
1747 // select one output among several suitable for global effects.
1748 // The priority is as follows:
1749 // 1: An offloaded output. If the effect ends up not being offloadable,
1750 // AudioFlinger will invalidate the track and the offloaded output
1751 // will be closed causing the effect to be moved to a PCM output.
1752 // 2: A deep buffer output
1753 // 3: the first output in the list
1754
1755 if (outputs.size() == 0) {
1756 return 0;
1757 }
1758
1759 audio_io_handle_t outputOffloaded = 0;
1760 audio_io_handle_t outputDeepBuffer = 0;
1761
1762 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001763 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001764 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001765 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1766 outputOffloaded = outputs[i];
1767 }
1768 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1769 outputDeepBuffer = outputs[i];
1770 }
1771 }
1772
1773 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1774 outputOffloaded, outputDeepBuffer);
1775 if (outputOffloaded != 0) {
1776 return outputOffloaded;
1777 }
1778 if (outputDeepBuffer != 0) {
1779 return outputDeepBuffer;
1780 }
1781
1782 return outputs[0];
1783}
1784
Eric Laurente0720872014-03-11 09:30:41 -07001785audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001786{
1787 // apply simple rule where global effects are attached to the same output as MUSIC streams
1788
Eric Laurent3b73df72014-03-11 09:06:29 -07001789 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001790 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1791 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1792
1793 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1794 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1795 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1796
1797 return output;
1798}
1799
Eric Laurente0720872014-03-11 09:30:41 -07001800status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001801 audio_io_handle_t io,
1802 uint32_t strategy,
1803 int session,
1804 int id)
1805{
1806 ssize_t index = mOutputs.indexOfKey(io);
1807 if (index < 0) {
1808 index = mInputs.indexOfKey(io);
1809 if (index < 0) {
1810 ALOGW("registerEffect() unknown io %d", io);
1811 return INVALID_OPERATION;
1812 }
1813 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001814 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001815}
1816
Eric Laurentc75307b2015-03-17 15:29:32 -07001817bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1818{
1819 return mOutputs.isStreamActive(stream, inPastMs);
1820}
1821
1822bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1823{
1824 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1825}
1826
Eric Laurente0720872014-03-11 09:30:41 -07001827bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001828{
1829 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001830 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001831 if (inputDescriptor->mRefCount == 0) {
1832 continue;
1833 }
1834 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001835 return true;
1836 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001837 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1838 // corresponds to an active capture triggered by a hardware hotword recognition
1839 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1840 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1841 // FIXME: we should not assume that the first session is the active one and keep
1842 // activity count per session. Same in startInput().
1843 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1844 if (index >= 0) {
1845 return true;
1846 }
1847 }
Eric Laurente552edb2014-03-10 17:42:56 -07001848 }
1849 return false;
1850}
1851
Eric Laurent275e8e92014-11-30 15:14:47 -08001852// Register a list of custom mixes with their attributes and format.
1853// When a mix is registered, corresponding input and output profiles are
1854// added to the remote submix hw module. The profile contains only the
1855// parameters (sampling rate, format...) specified by the mix.
1856// The corresponding input remote submix device is also connected.
1857//
1858// When a remote submix device is connected, the address is checked to select the
1859// appropriate profile and the corresponding input or output stream is opened.
1860//
1861// When capture starts, getInputForAttr() will:
1862// - 1 look for a mix matching the address passed in attribtutes tags if any
1863// - 2 if none found, getDeviceForInputSource() will:
1864// - 2.1 look for a mix matching the attributes source
1865// - 2.2 if none found, default to device selection by policy rules
1866// At this time, the corresponding output remote submix device is also connected
1867// and active playback use cases can be transferred to this mix if needed when reconnecting
1868// after AudioTracks are invalidated
1869//
1870// When playback starts, getOutputForAttr() will:
1871// - 1 look for a mix matching the address passed in attribtutes tags if any
1872// - 2 if none found, look for a mix matching the attributes usage
1873// - 3 if none found, default to device and output selection by policy rules.
1874
1875status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1876{
1877 sp<HwModule> module;
1878 for (size_t i = 0; i < mHwModules.size(); i++) {
1879 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1880 mHwModules[i]->mHandle != 0) {
1881 module = mHwModules[i];
1882 break;
1883 }
1884 }
1885
1886 if (module == 0) {
1887 return INVALID_OPERATION;
1888 }
1889
1890 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1891
1892 for (size_t i = 0; i < mixes.size(); i++) {
1893 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001894
1895 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001896 continue;
1897 }
1898 audio_config_t outputConfig = mixes[i].mFormat;
1899 audio_config_t inputConfig = mixes[i].mFormat;
1900 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1901 // stereo and let audio flinger do the channel conversion if needed.
1902 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1903 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1904 module->addOutputProfile(address, &outputConfig,
1905 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1906 module->addInputProfile(address, &inputConfig,
1907 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001908
Eric Laurentc722f302014-12-10 11:21:49 -08001909 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001910 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001911 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001912 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001913 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001914 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001915 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001916 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001917 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001918 }
1919 return NO_ERROR;
1920}
1921
1922status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1923{
1924 sp<HwModule> module;
1925 for (size_t i = 0; i < mHwModules.size(); i++) {
1926 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1927 mHwModules[i]->mHandle != 0) {
1928 module = mHwModules[i];
1929 break;
1930 }
1931 }
1932
1933 if (module == 0) {
1934 return INVALID_OPERATION;
1935 }
1936
1937 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1938
1939 for (size_t i = 0; i < mixes.size(); i++) {
1940 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001941
1942 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001943 continue;
1944 }
1945
Eric Laurentc722f302014-12-10 11:21:49 -08001946 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1947 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1948 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001949 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001950 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001951 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001952 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001953
1954 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1955 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1956 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001957 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001958 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001959 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001960 }
1961 module->removeOutputProfile(address);
1962 module->removeInputProfile(address);
1963 }
1964 return NO_ERROR;
1965}
1966
Eric Laurente552edb2014-03-10 17:42:56 -07001967
Eric Laurente0720872014-03-11 09:30:41 -07001968status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001969{
1970 const size_t SIZE = 256;
1971 char buffer[SIZE];
1972 String8 result;
1973
1974 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1975 result.append(buffer);
1976
Eric Laurent87ffa392015-05-22 10:32:38 -07001977 snprintf(buffer, SIZE, " Primary Output: %d\n",
1978 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07001979 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001980 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001981 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001982 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001983 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001984 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001985 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001986 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001987 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001988 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001989 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001990 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001991 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001992 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001993 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001994 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001995 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001996 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001997
François Gaffie53615e22015-03-19 09:24:12 +01001998 mAvailableOutputDevices.dump(fd, String8("output"));
1999 mAvailableInputDevices.dump(fd, String8("input"));
2000 mHwModules.dump(fd);
2001 mOutputs.dump(fd);
2002 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002003 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002004 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002005 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002006
2007 return NO_ERROR;
2008}
2009
2010// This function checks for the parameters which can be offloaded.
2011// This can be enhanced depending on the capability of the DSP and policy
2012// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002013bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002014{
2015 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002016 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002017 offloadInfo.sample_rate, offloadInfo.channel_mask,
2018 offloadInfo.format,
2019 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2020 offloadInfo.has_video);
2021
2022 // Check if offload has been disabled
2023 char propValue[PROPERTY_VALUE_MAX];
2024 if (property_get("audio.offload.disable", propValue, "0")) {
2025 if (atoi(propValue) != 0) {
2026 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2027 return false;
2028 }
2029 }
2030
2031 // Check if stream type is music, then only allow offload as of now.
2032 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2033 {
2034 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2035 return false;
2036 }
2037
2038 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002039 const bool allowOffloadWithVideo =
2040 property_get_bool("audio.offload.video", false /* default_value */);
2041 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002042 ALOGV("isOffloadSupported: has_video == true, returning false");
2043 return false;
2044 }
2045
2046 //If duration is less than minimum value defined in property, return false
2047 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2048 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2049 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2050 return false;
2051 }
2052 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2053 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2054 return false;
2055 }
2056
2057 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2058 // creating an offloaded track and tearing it down immediately after start when audioflinger
2059 // detects there is an active non offloadable effect.
2060 // FIXME: We should check the audio session here but we do not have it in this context.
2061 // This may prevent offloading in rare situations where effects are left active by apps
2062 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002063 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002064 return false;
2065 }
2066
2067 // See if there is a profile to support this.
2068 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002069 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002070 offloadInfo.sample_rate,
2071 offloadInfo.format,
2072 offloadInfo.channel_mask,
2073 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002074 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2075 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002076}
2077
Eric Laurent6a94d692014-05-20 11:18:06 -07002078status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2079 audio_port_type_t type,
2080 unsigned int *num_ports,
2081 struct audio_port *ports,
2082 unsigned int *generation)
2083{
2084 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2085 generation == NULL) {
2086 return BAD_VALUE;
2087 }
2088 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2089 if (ports == NULL) {
2090 *num_ports = 0;
2091 }
2092
2093 size_t portsWritten = 0;
2094 size_t portsMax = *num_ports;
2095 *num_ports = 0;
2096 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2097 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2098 for (size_t i = 0;
2099 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2100 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2101 }
2102 *num_ports += mAvailableOutputDevices.size();
2103 }
2104 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2105 for (size_t i = 0;
2106 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2107 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2108 }
2109 *num_ports += mAvailableInputDevices.size();
2110 }
2111 }
2112 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2113 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2114 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2115 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2116 }
2117 *num_ports += mInputs.size();
2118 }
2119 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002120 size_t numOutputs = 0;
2121 for (size_t i = 0; i < mOutputs.size(); i++) {
2122 if (!mOutputs[i]->isDuplicated()) {
2123 numOutputs++;
2124 if (portsWritten < portsMax) {
2125 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2126 }
2127 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002128 }
Eric Laurent84c70242014-06-23 08:46:27 -07002129 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002130 }
2131 }
2132 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002133 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002134 return NO_ERROR;
2135}
2136
2137status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2138{
2139 return NO_ERROR;
2140}
2141
Eric Laurent6a94d692014-05-20 11:18:06 -07002142status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2143 audio_patch_handle_t *handle,
2144 uid_t uid)
2145{
2146 ALOGV("createAudioPatch()");
2147
2148 if (handle == NULL || patch == NULL) {
2149 return BAD_VALUE;
2150 }
2151 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2152
Eric Laurent874c42872014-08-08 15:13:39 -07002153 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2154 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2155 return BAD_VALUE;
2156 }
2157 // only one source per audio patch supported for now
2158 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002159 return INVALID_OPERATION;
2160 }
Eric Laurent874c42872014-08-08 15:13:39 -07002161
2162 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002163 return INVALID_OPERATION;
2164 }
Eric Laurent874c42872014-08-08 15:13:39 -07002165 for (size_t i = 0; i < patch->num_sinks; i++) {
2166 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2167 return INVALID_OPERATION;
2168 }
2169 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002170
2171 sp<AudioPatch> patchDesc;
2172 ssize_t index = mAudioPatches.indexOfKey(*handle);
2173
Eric Laurent6a94d692014-05-20 11:18:06 -07002174 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2175 patch->sources[0].role,
2176 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002177#if LOG_NDEBUG == 0
2178 for (size_t i = 0; i < patch->num_sinks; i++) {
2179 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2180 patch->sinks[i].role,
2181 patch->sinks[i].type);
2182 }
2183#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002184
2185 if (index >= 0) {
2186 patchDesc = mAudioPatches.valueAt(index);
2187 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2188 mUidCached, patchDesc->mUid, uid);
2189 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2190 return INVALID_OPERATION;
2191 }
2192 } else {
2193 *handle = 0;
2194 }
2195
2196 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002197 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002198 if (outputDesc == NULL) {
2199 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2200 return BAD_VALUE;
2201 }
Eric Laurent84c70242014-06-23 08:46:27 -07002202 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2203 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002204 if (patchDesc != 0) {
2205 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2206 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2207 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2208 return BAD_VALUE;
2209 }
2210 }
Eric Laurent874c42872014-08-08 15:13:39 -07002211 DeviceVector devices;
2212 for (size_t i = 0; i < patch->num_sinks; i++) {
2213 // Only support mix to devices connection
2214 // TODO add support for mix to mix connection
2215 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2216 ALOGV("createAudioPatch() source mix but sink is not a device");
2217 return INVALID_OPERATION;
2218 }
2219 sp<DeviceDescriptor> devDesc =
2220 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2221 if (devDesc == 0) {
2222 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2223 return BAD_VALUE;
2224 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002225
François Gaffie53615e22015-03-19 09:24:12 +01002226 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002227 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002228 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002229 NULL, // updatedSamplingRate
2230 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002231 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002232 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002233 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002234 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002235 ALOGV("createAudioPatch() profile not supported for device %08x",
2236 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002237 return INVALID_OPERATION;
2238 }
2239 devices.add(devDesc);
2240 }
2241 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002242 return INVALID_OPERATION;
2243 }
Eric Laurent874c42872014-08-08 15:13:39 -07002244
Eric Laurent6a94d692014-05-20 11:18:06 -07002245 // TODO: reconfigure output format and channels here
2246 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002247 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002248 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002249 index = mAudioPatches.indexOfKey(*handle);
2250 if (index >= 0) {
2251 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2252 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2253 }
2254 patchDesc = mAudioPatches.valueAt(index);
2255 patchDesc->mUid = uid;
2256 ALOGV("createAudioPatch() success");
2257 } else {
2258 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2259 return INVALID_OPERATION;
2260 }
2261 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2262 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2263 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002264 // only one sink supported when connecting an input device to a mix
2265 if (patch->num_sinks > 1) {
2266 return INVALID_OPERATION;
2267 }
François Gaffie53615e22015-03-19 09:24:12 +01002268 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002269 if (inputDesc == NULL) {
2270 return BAD_VALUE;
2271 }
2272 if (patchDesc != 0) {
2273 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2274 return BAD_VALUE;
2275 }
2276 }
2277 sp<DeviceDescriptor> devDesc =
2278 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2279 if (devDesc == 0) {
2280 return BAD_VALUE;
2281 }
2282
François Gaffie53615e22015-03-19 09:24:12 +01002283 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002284 devDesc->mAddress,
2285 patch->sinks[0].sample_rate,
2286 NULL, /*updatedSampleRate*/
2287 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002288 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002289 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002290 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002291 // FIXME for the parameter type,
2292 // and the NONE
2293 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002294 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002295 return INVALID_OPERATION;
2296 }
2297 // TODO: reconfigure output format and channels here
2298 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002299 devDesc->type(), inputDesc->mIoHandle);
2300 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002301 index = mAudioPatches.indexOfKey(*handle);
2302 if (index >= 0) {
2303 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2304 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2305 }
2306 patchDesc = mAudioPatches.valueAt(index);
2307 patchDesc->mUid = uid;
2308 ALOGV("createAudioPatch() success");
2309 } else {
2310 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2311 return INVALID_OPERATION;
2312 }
2313 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2314 // device to device connection
2315 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002316 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002317 return BAD_VALUE;
2318 }
2319 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002320 sp<DeviceDescriptor> srcDeviceDesc =
2321 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002322 if (srcDeviceDesc == 0) {
2323 return BAD_VALUE;
2324 }
Eric Laurent874c42872014-08-08 15:13:39 -07002325
Eric Laurent6a94d692014-05-20 11:18:06 -07002326 //update source and sink with our own data as the data passed in the patch may
2327 // be incomplete.
2328 struct audio_patch newPatch = *patch;
2329 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002330
Eric Laurent874c42872014-08-08 15:13:39 -07002331 for (size_t i = 0; i < patch->num_sinks; i++) {
2332 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2333 ALOGV("createAudioPatch() source device but one sink is not a device");
2334 return INVALID_OPERATION;
2335 }
2336
2337 sp<DeviceDescriptor> sinkDeviceDesc =
2338 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2339 if (sinkDeviceDesc == 0) {
2340 return BAD_VALUE;
2341 }
2342 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2343
Eric Laurent3bcf8592015-04-03 12:13:24 -07002344 // create a software bridge in PatchPanel if:
2345 // - source and sink devices are on differnt HW modules OR
2346 // - audio HAL version is < 3.0
2347 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2348 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2349 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002350 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002351 return INVALID_OPERATION;
2352 }
Eric Laurent874c42872014-08-08 15:13:39 -07002353 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002354 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002355 // if the sink device is reachable via an opened output stream, request to go via
2356 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002357 audio_io_handle_t output = selectOutput(outputs,
2358 AUDIO_OUTPUT_FLAG_NONE,
2359 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002360 if (output != AUDIO_IO_HANDLE_NONE) {
2361 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2362 if (outputDesc->isDuplicated()) {
2363 return INVALID_OPERATION;
2364 }
2365 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002366 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002367 newPatch.num_sources = 2;
2368 }
Eric Laurent83b88082014-06-20 18:31:16 -07002369 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002370 }
2371 // TODO: check from routing capabilities in config file and other conflicting patches
2372
2373 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2374 if (index >= 0) {
2375 afPatchHandle = patchDesc->mAfPatchHandle;
2376 }
2377
2378 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2379 &afPatchHandle,
2380 0);
2381 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2382 status, afPatchHandle);
2383 if (status == NO_ERROR) {
2384 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002385 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002386 addAudioPatch(patchDesc->mHandle, patchDesc);
2387 } else {
2388 patchDesc->mPatch = newPatch;
2389 }
2390 patchDesc->mAfPatchHandle = afPatchHandle;
2391 *handle = patchDesc->mHandle;
2392 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002393 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002394 } else {
2395 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2396 status);
2397 return INVALID_OPERATION;
2398 }
2399 } else {
2400 return BAD_VALUE;
2401 }
2402 } else {
2403 return BAD_VALUE;
2404 }
2405 return NO_ERROR;
2406}
2407
2408status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2409 uid_t uid)
2410{
2411 ALOGV("releaseAudioPatch() patch %d", handle);
2412
2413 ssize_t index = mAudioPatches.indexOfKey(handle);
2414
2415 if (index < 0) {
2416 return BAD_VALUE;
2417 }
2418 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2419 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2420 mUidCached, patchDesc->mUid, uid);
2421 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2422 return INVALID_OPERATION;
2423 }
2424
2425 struct audio_patch *patch = &patchDesc->mPatch;
2426 patchDesc->mUid = mUidCached;
2427 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002428 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002429 if (outputDesc == NULL) {
2430 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2431 return BAD_VALUE;
2432 }
2433
Eric Laurentc75307b2015-03-17 15:29:32 -07002434 setOutputDevice(outputDesc,
2435 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 true,
2437 0,
2438 NULL);
2439 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2440 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002441 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002442 if (inputDesc == NULL) {
2443 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2444 return BAD_VALUE;
2445 }
2446 setInputDevice(inputDesc->mIoHandle,
2447 getNewInputDevice(inputDesc->mIoHandle),
2448 true,
2449 NULL);
2450 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2451 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2452 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2453 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2454 status, patchDesc->mAfPatchHandle);
2455 removeAudioPatch(patchDesc->mHandle);
2456 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002457 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002458 } else {
2459 return BAD_VALUE;
2460 }
2461 } else {
2462 return BAD_VALUE;
2463 }
2464 return NO_ERROR;
2465}
2466
2467status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2468 struct audio_patch *patches,
2469 unsigned int *generation)
2470{
François Gaffie53615e22015-03-19 09:24:12 +01002471 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002472 return BAD_VALUE;
2473 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002474 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002475 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002476}
2477
Eric Laurente1715a42014-05-20 11:30:42 -07002478status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002479{
Eric Laurente1715a42014-05-20 11:30:42 -07002480 ALOGV("setAudioPortConfig()");
2481
2482 if (config == NULL) {
2483 return BAD_VALUE;
2484 }
2485 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2486 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002487 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2488 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002489 }
2490
Eric Laurenta121f902014-06-03 13:32:54 -07002491 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002492 if (config->type == AUDIO_PORT_TYPE_MIX) {
2493 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002494 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002495 if (outputDesc == NULL) {
2496 return BAD_VALUE;
2497 }
Eric Laurent84c70242014-06-23 08:46:27 -07002498 ALOG_ASSERT(!outputDesc->isDuplicated(),
2499 "setAudioPortConfig() called on duplicated output %d",
2500 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002501 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002502 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002503 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002504 if (inputDesc == NULL) {
2505 return BAD_VALUE;
2506 }
Eric Laurenta121f902014-06-03 13:32:54 -07002507 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002508 } else {
2509 return BAD_VALUE;
2510 }
2511 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2512 sp<DeviceDescriptor> deviceDesc;
2513 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2514 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2515 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2516 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2517 } else {
2518 return BAD_VALUE;
2519 }
2520 if (deviceDesc == NULL) {
2521 return BAD_VALUE;
2522 }
Eric Laurenta121f902014-06-03 13:32:54 -07002523 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002524 } else {
2525 return BAD_VALUE;
2526 }
2527
Eric Laurenta121f902014-06-03 13:32:54 -07002528 struct audio_port_config backupConfig;
2529 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2530 if (status == NO_ERROR) {
2531 struct audio_port_config newConfig;
2532 audioPortConfig->toAudioPortConfig(&newConfig, config);
2533 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002534 }
Eric Laurenta121f902014-06-03 13:32:54 -07002535 if (status != NO_ERROR) {
2536 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002537 }
Eric Laurente1715a42014-05-20 11:30:42 -07002538
2539 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002540}
2541
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002542void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2543{
2544 clearAudioPatches(uid);
2545 clearSessionRoutes(uid);
2546}
2547
Eric Laurent6a94d692014-05-20 11:18:06 -07002548void AudioPolicyManager::clearAudioPatches(uid_t uid)
2549{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002550 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002551 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2552 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002553 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002554 }
2555 }
2556}
2557
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002558
2559void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2560 audio_io_handle_t ouptutToSkip)
2561{
2562 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2563 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2564 for (size_t j = 0; j < mOutputs.size(); j++) {
2565 if (mOutputs.keyAt(j) == ouptutToSkip) {
2566 continue;
2567 }
2568 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2569 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2570 continue;
2571 }
2572 // If the default device for this strategy is on another output mix,
2573 // invalidate all tracks in this strategy to force re connection.
2574 // Otherwise select new device on the output mix.
2575 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2576 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2577 if (stream == AUDIO_STREAM_PATCH) {
2578 continue;
2579 }
2580 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2581 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2582 }
2583 }
2584 } else {
2585 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2586 setOutputDevice(outputDesc, newDevice, false);
2587 }
2588 }
2589}
2590
2591void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2592{
2593 // remove output routes associated with this uid
2594 SortedVector<routing_strategy> affectedStrategies;
2595 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2596 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2597 if (route->mUid == uid) {
2598 mOutputRoutes.removeItemsAt(i);
2599 if (route->mDeviceDescriptor != 0) {
2600 affectedStrategies.add(getStrategy(route->mStreamType));
2601 }
2602 }
2603 }
2604 // reroute outputs if necessary
2605 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2606 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2607 }
2608
2609 // remove input routes associated with this uid
2610 SortedVector<audio_source_t> affectedSources;
2611 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2612 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2613 if (route->mUid == uid) {
2614 mInputRoutes.removeItemsAt(i);
2615 if (route->mDeviceDescriptor != 0) {
2616 affectedSources.add(route->mSource);
2617 }
2618 }
2619 }
2620 // reroute inputs if necessary
2621 SortedVector<audio_io_handle_t> inputsToClose;
2622 for (size_t i = 0; i < mInputs.size(); i++) {
2623 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2624 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2625 inputsToClose.add(inputDesc->mIoHandle);
2626 }
2627 }
2628 for (size_t i = 0; i < inputsToClose.size(); i++) {
2629 closeInput(inputsToClose[i]);
2630 }
2631}
2632
2633
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002634status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2635 audio_io_handle_t *ioHandle,
2636 audio_devices_t *device)
2637{
2638 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2639 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002640 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002641
François Gaffiedf372692015-03-19 10:43:27 +01002642 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002643}
2644
Eric Laurent493404d2015-04-21 15:07:36 -07002645status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2646 const audio_attributes_t *attributes __unused,
2647 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002648{
2649 return INVALID_OPERATION;
2650}
2651
Eric Laurent493404d2015-04-21 15:07:36 -07002652status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002653{
2654 return INVALID_OPERATION;
2655}
2656
Eric Laurente552edb2014-03-10 17:42:56 -07002657// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002658// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002659// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002660uint32_t AudioPolicyManager::nextAudioPortGeneration()
2661{
2662 return android_atomic_inc(&mAudioPortGeneration);
2663}
2664
Eric Laurente0720872014-03-11 09:30:41 -07002665AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002666 :
2667#ifdef AUDIO_POLICY_TEST
2668 Thread(false),
2669#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002670 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002671 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002672 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002673 mAudioPortGeneration(1),
2674 mBeaconMuteRefCount(0),
2675 mBeaconPlayingRefCount(0),
2676 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002677{
François Gaffie2110e042015-03-24 08:41:51 +01002678 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2679 if (!engineInstance) {
2680 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2681 return;
2682 }
2683 // Retrieve the Policy Manager Interface
2684 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2685 if (mEngine == NULL) {
2686 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2687 return;
2688 }
2689 mEngine->setObserver(this);
2690 status_t status = mEngine->initCheck();
2691 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2692
Eric Laurent6a94d692014-05-20 11:18:06 -07002693 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002694 mpClientInterface = clientInterface;
2695
Eric Laurent7288ab82015-05-06 18:44:02 -07002696 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002697 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2698 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2699 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2700 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2701 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2702 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002703 ALOGE("could not load audio policy configuration file, setting defaults");
2704 defaultAudioPolicyConfig();
2705 }
2706 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002707 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002708
François Gaffie2110e042015-03-24 08:41:51 +01002709 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2710 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002711
2712 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002713 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2714 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002715 for (size_t i = 0; i < mHwModules.size(); i++) {
2716 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2717 if (mHwModules[i]->mHandle == 0) {
2718 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2719 continue;
2720 }
2721 // open all output streams needed to access attached devices
2722 // except for direct output streams that are only opened when they are actually
2723 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002724 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002725 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2726 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002727 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002728
Eric Laurent3a4311c2014-03-17 12:00:47 -07002729 if (outProfile->mSupportedDevices.isEmpty()) {
2730 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2731 continue;
2732 }
2733
Eric Laurentd78f1532014-09-16 16:38:20 -07002734 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2735 continue;
2736 }
Eric Laurent83b88082014-06-20 18:31:16 -07002737 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002738 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2739 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002740 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002741 // chose first device present in mSupportedDevices also part of
2742 // outputDeviceTypes
2743 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002744 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002745 if ((profileType & outputDeviceTypes) != 0) {
2746 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002747 }
Eric Laurente552edb2014-03-10 17:42:56 -07002748 }
2749 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002750 if ((profileType & outputDeviceTypes) == 0) {
2751 continue;
2752 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002753 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2754 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002755
2756 outputDesc->mDevice = profileType;
2757 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2758 config.sample_rate = outputDesc->mSamplingRate;
2759 config.channel_mask = outputDesc->mChannelMask;
2760 config.format = outputDesc->mFormat;
2761 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002762 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002763 &output,
2764 &config,
2765 &outputDesc->mDevice,
2766 String8(""),
2767 &outputDesc->mLatency,
2768 outputDesc->mFlags);
2769
2770 if (status != NO_ERROR) {
2771 ALOGW("Cannot open output stream for device %08x on hw module %s",
2772 outputDesc->mDevice,
2773 mHwModules[i]->mName);
2774 } else {
2775 outputDesc->mSamplingRate = config.sample_rate;
2776 outputDesc->mChannelMask = config.channel_mask;
2777 outputDesc->mFormat = config.format;
2778
2779 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002780 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002781 ssize_t index =
2782 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2783 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002784 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2785 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002786 }
2787 }
2788 if (mPrimaryOutput == 0 &&
2789 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002790 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002791 }
2792 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002793 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002794 outputDesc->mDevice,
2795 true);
2796 }
Eric Laurente552edb2014-03-10 17:42:56 -07002797 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002798 // open input streams needed to access attached devices to validate
2799 // mAvailableInputDevices list
2800 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2801 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002802 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002803
Eric Laurent3a4311c2014-03-17 12:00:47 -07002804 if (inProfile->mSupportedDevices.isEmpty()) {
2805 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2806 continue;
2807 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002808 // chose first device present in mSupportedDevices also part of
2809 // inputDeviceTypes
2810 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2811 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002812 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002813 if (profileType & inputDeviceTypes) {
2814 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002815 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002816 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002817 if ((profileType & inputDeviceTypes) == 0) {
2818 continue;
2819 }
2820 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2821
2822 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2823 inputDesc->mDevice = profileType;
2824
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002825 // find the address
2826 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2827 // the inputs vector must be of size 1, but we don't want to crash here
2828 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2829 : String8("");
2830 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2831 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2832
Eric Laurentd78f1532014-09-16 16:38:20 -07002833 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2834 config.sample_rate = inputDesc->mSamplingRate;
2835 config.channel_mask = inputDesc->mChannelMask;
2836 config.format = inputDesc->mFormat;
2837 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002838 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002839 &input,
2840 &config,
2841 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002842 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002843 AUDIO_SOURCE_MIC,
2844 AUDIO_INPUT_FLAG_NONE);
2845
2846 if (status == NO_ERROR) {
2847 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002848 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002849 ssize_t index =
2850 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2851 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002852 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2853 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002854 }
2855 }
2856 mpClientInterface->closeInput(input);
2857 } else {
2858 ALOGW("Cannot open input stream for device %08x on hw module %s",
2859 inputDesc->mDevice,
2860 mHwModules[i]->mName);
2861 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002862 }
2863 }
2864 // make sure all attached devices have been allocated a unique ID
2865 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002866 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002867 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002868 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2869 continue;
2870 }
François Gaffie2110e042015-03-24 08:41:51 +01002871 // The device is now validated and can be appended to the available devices of the engine
2872 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2873 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002874 i++;
2875 }
2876 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002877 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002878 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002879 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2880 continue;
2881 }
François Gaffie2110e042015-03-24 08:41:51 +01002882 // The device is now validated and can be appended to the available devices of the engine
2883 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2884 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002885 i++;
2886 }
2887 // make sure default device is reachable
2888 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002889 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002890 }
Eric Laurente552edb2014-03-10 17:42:56 -07002891
2892 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2893
2894 updateDevicesAndOutputs();
2895
2896#ifdef AUDIO_POLICY_TEST
2897 if (mPrimaryOutput != 0) {
2898 AudioParameter outputCmd = AudioParameter();
2899 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002900 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002901
2902 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2903 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002904 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2905 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002906 mTestLatencyMs = 0;
2907 mCurOutput = 0;
2908 mDirectOutput = false;
2909 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2910 mTestOutputs[i] = 0;
2911 }
2912
2913 const size_t SIZE = 256;
2914 char buffer[SIZE];
2915 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2916 run(buffer, ANDROID_PRIORITY_AUDIO);
2917 }
2918#endif //AUDIO_POLICY_TEST
2919}
2920
Eric Laurente0720872014-03-11 09:30:41 -07002921AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002922{
2923#ifdef AUDIO_POLICY_TEST
2924 exit();
2925#endif //AUDIO_POLICY_TEST
2926 for (size_t i = 0; i < mOutputs.size(); i++) {
2927 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002928 }
2929 for (size_t i = 0; i < mInputs.size(); i++) {
2930 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002931 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002932 mAvailableOutputDevices.clear();
2933 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002934 mOutputs.clear();
2935 mInputs.clear();
2936 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002937}
2938
Eric Laurente0720872014-03-11 09:30:41 -07002939status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002940{
Eric Laurent87ffa392015-05-22 10:32:38 -07002941 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002942}
2943
2944#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002945bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002946{
2947 ALOGV("entering threadLoop()");
2948 while (!exitPending())
2949 {
2950 String8 command;
2951 int valueInt;
2952 String8 value;
2953
2954 Mutex::Autolock _l(mLock);
2955 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2956
2957 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2958 AudioParameter param = AudioParameter(command);
2959
2960 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2961 valueInt != 0) {
2962 ALOGV("Test command %s received", command.string());
2963 String8 target;
2964 if (param.get(String8("target"), target) != NO_ERROR) {
2965 target = "Manager";
2966 }
2967 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2968 param.remove(String8("test_cmd_policy_output"));
2969 mCurOutput = valueInt;
2970 }
2971 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2972 param.remove(String8("test_cmd_policy_direct"));
2973 if (value == "false") {
2974 mDirectOutput = false;
2975 } else if (value == "true") {
2976 mDirectOutput = true;
2977 }
2978 }
2979 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2980 param.remove(String8("test_cmd_policy_input"));
2981 mTestInput = valueInt;
2982 }
2983
2984 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2985 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002986 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002987 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002988 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002989 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002990 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002991 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002992 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002993 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002994 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002995 if (target == "Manager") {
2996 mTestFormat = format;
2997 } else if (mTestOutputs[mCurOutput] != 0) {
2998 AudioParameter outputParam = AudioParameter();
2999 outputParam.addInt(String8("format"), format);
3000 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3001 }
3002 }
3003 }
3004 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3005 param.remove(String8("test_cmd_policy_channels"));
3006 int channels = 0;
3007
3008 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003009 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003010 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003011 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003012 }
3013 if (channels != 0) {
3014 if (target == "Manager") {
3015 mTestChannels = channels;
3016 } else if (mTestOutputs[mCurOutput] != 0) {
3017 AudioParameter outputParam = AudioParameter();
3018 outputParam.addInt(String8("channels"), channels);
3019 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3020 }
3021 }
3022 }
3023 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3024 param.remove(String8("test_cmd_policy_sampleRate"));
3025 if (valueInt >= 0 && valueInt <= 96000) {
3026 int samplingRate = valueInt;
3027 if (target == "Manager") {
3028 mTestSamplingRate = samplingRate;
3029 } else if (mTestOutputs[mCurOutput] != 0) {
3030 AudioParameter outputParam = AudioParameter();
3031 outputParam.addInt(String8("sampling_rate"), samplingRate);
3032 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3033 }
3034 }
3035 }
3036
3037 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3038 param.remove(String8("test_cmd_policy_reopen"));
3039
Eric Laurentc75307b2015-03-17 15:29:32 -07003040 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003041
Eric Laurentc75307b2015-03-17 15:29:32 -07003042 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003043
Eric Laurentc75307b2015-03-17 15:29:32 -07003044 removeOutput(mPrimaryOutput->mIoHandle);
3045 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3046 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003047 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003048 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3049 config.sample_rate = outputDesc->mSamplingRate;
3050 config.channel_mask = outputDesc->mChannelMask;
3051 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003052 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003053 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003054 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003055 &config,
3056 &outputDesc->mDevice,
3057 String8(""),
3058 &outputDesc->mLatency,
3059 outputDesc->mFlags);
3060 if (status != NO_ERROR) {
3061 ALOGE("Failed to reopen hardware output stream, "
3062 "samplingRate: %d, format %d, channels %d",
3063 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003064 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003065 outputDesc->mSamplingRate = config.sample_rate;
3066 outputDesc->mChannelMask = config.channel_mask;
3067 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003068 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003069 AudioParameter outputCmd = AudioParameter();
3070 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003071 mpClientInterface->setParameters(handle, outputCmd.toString());
3072 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003073 }
3074 }
3075
3076
3077 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3078 }
3079 }
3080 return false;
3081}
3082
Eric Laurente0720872014-03-11 09:30:41 -07003083void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003084{
3085 {
3086 AutoMutex _l(mLock);
3087 requestExit();
3088 mWaitWorkCV.signal();
3089 }
3090 requestExitAndWait();
3091}
3092
Eric Laurente0720872014-03-11 09:30:41 -07003093int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003094{
3095 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3096 if (output == mTestOutputs[i]) return i;
3097 }
3098 return 0;
3099}
3100#endif //AUDIO_POLICY_TEST
3101
3102// ---
3103
Eric Laurentc75307b2015-03-17 15:29:32 -07003104void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003105{
François Gaffie98cc1912015-03-18 17:52:40 +01003106 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003107 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003108 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003109}
3110
François Gaffie53615e22015-03-19 09:24:12 +01003111void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3112{
3113 mOutputs.removeItem(output);
3114}
3115
Eric Laurent1f2f2232014-06-02 12:01:23 -07003116void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003117{
François Gaffie98cc1912015-03-18 17:52:40 +01003118 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003119 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003120 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003121}
Eric Laurente552edb2014-03-10 17:42:56 -07003122
Eric Laurentc75307b2015-03-17 15:29:32 -07003123void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003124 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003125 const String8 address /*in*/,
3126 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003127 sp<DeviceDescriptor> devDesc =
3128 desc->mProfile->mSupportedDevices.getDevice(device, address);
3129 if (devDesc != 0) {
3130 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3131 desc->mIoHandle, address.string());
3132 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003133 }
3134}
3135
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003136status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003137 audio_policy_dev_state_t state,
3138 SortedVector<audio_io_handle_t>& outputs,
3139 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003140{
François Gaffie53615e22015-03-19 09:24:12 +01003141 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003142 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003143 // erase all current sample rates, formats and channel masks
3144 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003145
Eric Laurent3b73df72014-03-11 09:06:29 -07003146 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003147 // first list already open outputs that can be routed to this device
3148 for (size_t i = 0; i < mOutputs.size(); i++) {
3149 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003150 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003151 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003152 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3153 outputs.add(mOutputs.keyAt(i));
3154 } else {
3155 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003156 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003157 }
Eric Laurente552edb2014-03-10 17:42:56 -07003158 }
3159 }
3160 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003161 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003162 for (size_t i = 0; i < mHwModules.size(); i++)
3163 {
3164 if (mHwModules[i]->mHandle == 0) {
3165 continue;
3166 }
3167 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3168 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003169 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3170 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003171 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003172 address == profile->mSupportedDevices[0]->mAddress) {
3173 profiles.add(profile);
3174 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3175 }
Eric Laurente552edb2014-03-10 17:42:56 -07003176 }
3177 }
3178 }
3179
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003180 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3181
Eric Laurente552edb2014-03-10 17:42:56 -07003182 if (profiles.isEmpty() && outputs.isEmpty()) {
3183 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3184 return BAD_VALUE;
3185 }
3186
3187 // open outputs for matching profiles if needed. Direct outputs are also opened to
3188 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3189 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003190 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003191
3192 // nothing to do if one output is already opened for this profile
3193 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003194 for (j = 0; j < outputs.size(); j++) {
3195 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003196 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003197 // matching profile: save the sample rates, format and channel masks supported
3198 // by the profile in our device descriptor
3199 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003200 break;
3201 }
3202 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003203 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003204 continue;
3205 }
3206
Eric Laurent83b88082014-06-20 18:31:16 -07003207 ALOGV("opening output for device %08x with params %s profile %p",
3208 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003209 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003210 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003211 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3212 config.sample_rate = desc->mSamplingRate;
3213 config.channel_mask = desc->mChannelMask;
3214 config.format = desc->mFormat;
3215 config.offload_info.sample_rate = desc->mSamplingRate;
3216 config.offload_info.channel_mask = desc->mChannelMask;
3217 config.offload_info.format = desc->mFormat;
3218 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003219 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003220 &output,
3221 &config,
3222 &desc->mDevice,
3223 address,
3224 &desc->mLatency,
3225 desc->mFlags);
3226 if (status == NO_ERROR) {
3227 desc->mSamplingRate = config.sample_rate;
3228 desc->mChannelMask = config.channel_mask;
3229 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003230
Eric Laurentd4692962014-05-05 18:13:44 -07003231 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003232 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003233 char *param = audio_device_address_to_parameter(device, address);
3234 mpClientInterface->setParameters(output, String8(param));
3235 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003236 }
3237
Eric Laurentd4692962014-05-05 18:13:44 -07003238 // Here is where we step through and resolve any "dynamic" fields
3239 String8 reply;
3240 char *value;
3241 if (profile->mSamplingRates[0] == 0) {
3242 reply = mpClientInterface->getParameters(output,
3243 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003244 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003245 reply.string());
3246 value = strpbrk((char *)reply.string(), "=");
3247 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003248 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003249 }
Eric Laurentd4692962014-05-05 18:13:44 -07003250 }
3251 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3252 reply = mpClientInterface->getParameters(output,
3253 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003254 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003255 reply.string());
3256 value = strpbrk((char *)reply.string(), "=");
3257 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003258 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003259 }
Eric Laurentd4692962014-05-05 18:13:44 -07003260 }
3261 if (profile->mChannelMasks[0] == 0) {
3262 reply = mpClientInterface->getParameters(output,
3263 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003264 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003265 reply.string());
3266 value = strpbrk((char *)reply.string(), "=");
3267 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003268 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003269 }
Eric Laurentd4692962014-05-05 18:13:44 -07003270 }
3271 if (((profile->mSamplingRates[0] == 0) &&
3272 (profile->mSamplingRates.size() < 2)) ||
3273 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3274 (profile->mFormats.size() < 2)) ||
3275 ((profile->mChannelMasks[0] == 0) &&
3276 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003277 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003278 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003279 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003280 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3281 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003282 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003283 config.sample_rate = profile->pickSamplingRate();
3284 config.channel_mask = profile->pickChannelMask();
3285 config.format = profile->pickFormat();
3286 config.offload_info.sample_rate = config.sample_rate;
3287 config.offload_info.channel_mask = config.channel_mask;
3288 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003289 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003290 &output,
3291 &config,
3292 &desc->mDevice,
3293 address,
3294 &desc->mLatency,
3295 desc->mFlags);
3296 if (status == NO_ERROR) {
3297 desc->mSamplingRate = config.sample_rate;
3298 desc->mChannelMask = config.channel_mask;
3299 desc->mFormat = config.format;
3300 } else {
3301 output = AUDIO_IO_HANDLE_NONE;
3302 }
Eric Laurentd4692962014-05-05 18:13:44 -07003303 }
3304
Eric Laurentcf2c0212014-07-25 16:20:43 -07003305 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003306 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003307 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003308 sp<AudioPolicyMix> policyMix;
3309 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003310 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3311 address.string());
3312 }
François Gaffie036e1e92015-03-19 10:16:24 +01003313 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003314 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003315
Eric Laurent87ffa392015-05-22 10:32:38 -07003316 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3317 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003318 // no duplicated output for direct outputs and
3319 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003320 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003321
Eric Laurentd4692962014-05-05 18:13:44 -07003322 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003323 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003324
Eric Laurentd4692962014-05-05 18:13:44 -07003325 //TODO: configure audio effect output stage here
3326
3327 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003328 duplicatedOutput =
3329 mpClientInterface->openDuplicateOutput(output,
3330 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003331 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003332 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003333 sp<SwAudioOutputDescriptor> dupOutputDesc =
3334 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3335 dupOutputDesc->mOutput1 = mPrimaryOutput;
3336 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003337 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3338 dupOutputDesc->mFormat = desc->mFormat;
3339 dupOutputDesc->mChannelMask = desc->mChannelMask;
3340 dupOutputDesc->mLatency = desc->mLatency;
3341 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003342 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003343 } else {
3344 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003345 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003346 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003347 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003348 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003349 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003350 }
Eric Laurente552edb2014-03-10 17:42:56 -07003351 }
3352 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003353 } else {
3354 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003355 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003356 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003357 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003358 profiles.removeAt(profile_index);
3359 profile_index--;
3360 } else {
3361 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003362 devDesc->importAudioPort(profile);
3363
François Gaffie53615e22015-03-19 09:24:12 +01003364 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003365 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3366 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003367 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003368 NULL/*patch handle*/, address.string());
3369 }
Eric Laurente552edb2014-03-10 17:42:56 -07003370 ALOGV("checkOutputsForDevice(): adding output %d", output);
3371 }
3372 }
3373
3374 if (profiles.isEmpty()) {
3375 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3376 return BAD_VALUE;
3377 }
Eric Laurentd4692962014-05-05 18:13:44 -07003378 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003379 // check if one opened output is not needed any more after disconnecting one device
3380 for (size_t i = 0; i < mOutputs.size(); i++) {
3381 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003382 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003383 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003384 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003385 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003386 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003387 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003388 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3389 mOutputs.keyAt(i));
3390 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003391 }
Eric Laurente552edb2014-03-10 17:42:56 -07003392 }
3393 }
Eric Laurentd4692962014-05-05 18:13:44 -07003394 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003395 for (size_t i = 0; i < mHwModules.size(); i++)
3396 {
3397 if (mHwModules[i]->mHandle == 0) {
3398 continue;
3399 }
3400 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3401 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003402 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003403 if (profile->mSupportedDevices.types() & device) {
3404 ALOGV("checkOutputsForDevice(): "
3405 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003406 if (profile->mSamplingRates[0] == 0) {
3407 profile->mSamplingRates.clear();
3408 profile->mSamplingRates.add(0);
3409 }
3410 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3411 profile->mFormats.clear();
3412 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3413 }
3414 if (profile->mChannelMasks[0] == 0) {
3415 profile->mChannelMasks.clear();
3416 profile->mChannelMasks.add(0);
3417 }
3418 }
3419 }
3420 }
3421 }
3422 return NO_ERROR;
3423}
3424
Eric Laurentd4692962014-05-05 18:13:44 -07003425status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003426 audio_policy_dev_state_t state,
3427 SortedVector<audio_io_handle_t>& inputs,
3428 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003429{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003430 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003431 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3432 // first list already open inputs that can be routed to this device
3433 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3434 desc = mInputs.valueAt(input_index);
3435 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3436 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3437 inputs.add(mInputs.keyAt(input_index));
3438 }
3439 }
3440
3441 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003442 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003443 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3444 {
3445 if (mHwModules[module_idx]->mHandle == 0) {
3446 continue;
3447 }
3448 for (size_t profile_index = 0;
3449 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3450 profile_index++)
3451 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003452 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3453
3454 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003455 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003456 address == profile->mSupportedDevices[0]->mAddress) {
3457 profiles.add(profile);
3458 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3459 profile_index, module_idx);
3460 }
Eric Laurentd4692962014-05-05 18:13:44 -07003461 }
3462 }
3463 }
3464
3465 if (profiles.isEmpty() && inputs.isEmpty()) {
3466 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3467 return BAD_VALUE;
3468 }
3469
3470 // open inputs for matching profiles if needed. Direct inputs are also opened to
3471 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3472 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3473
Eric Laurent1c333e22014-05-20 10:48:17 -07003474 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003475 // nothing to do if one input is already opened for this profile
3476 size_t input_index;
3477 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3478 desc = mInputs.valueAt(input_index);
3479 if (desc->mProfile == profile) {
3480 break;
3481 }
3482 }
3483 if (input_index != mInputs.size()) {
3484 continue;
3485 }
3486
3487 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3488 desc = new AudioInputDescriptor(profile);
3489 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003490 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3491 config.sample_rate = desc->mSamplingRate;
3492 config.channel_mask = desc->mChannelMask;
3493 config.format = desc->mFormat;
3494 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003495 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003496 &input,
3497 &config,
3498 &desc->mDevice,
3499 address,
3500 AUDIO_SOURCE_MIC,
3501 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003502
Eric Laurentcf2c0212014-07-25 16:20:43 -07003503 if (status == NO_ERROR) {
3504 desc->mSamplingRate = config.sample_rate;
3505 desc->mChannelMask = config.channel_mask;
3506 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003507
Eric Laurentd4692962014-05-05 18:13:44 -07003508 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003509 char *param = audio_device_address_to_parameter(device, address);
3510 mpClientInterface->setParameters(input, String8(param));
3511 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003512 }
3513
3514 // Here is where we step through and resolve any "dynamic" fields
3515 String8 reply;
3516 char *value;
3517 if (profile->mSamplingRates[0] == 0) {
3518 reply = mpClientInterface->getParameters(input,
3519 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3520 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3521 reply.string());
3522 value = strpbrk((char *)reply.string(), "=");
3523 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003524 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003525 }
3526 }
3527 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3528 reply = mpClientInterface->getParameters(input,
3529 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3530 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3531 value = strpbrk((char *)reply.string(), "=");
3532 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003533 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003534 }
3535 }
3536 if (profile->mChannelMasks[0] == 0) {
3537 reply = mpClientInterface->getParameters(input,
3538 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3539 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3540 reply.string());
3541 value = strpbrk((char *)reply.string(), "=");
3542 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003543 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003544 }
3545 }
3546 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3547 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3548 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3549 ALOGW("checkInputsForDevice() direct input missing param");
3550 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003551 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003552 }
3553
3554 if (input != 0) {
3555 addInput(input, desc);
3556 }
3557 } // endif input != 0
3558
Eric Laurentcf2c0212014-07-25 16:20:43 -07003559 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003560 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003561 profiles.removeAt(profile_index);
3562 profile_index--;
3563 } else {
3564 inputs.add(input);
3565 ALOGV("checkInputsForDevice(): adding input %d", input);
3566 }
3567 } // end scan profiles
3568
3569 if (profiles.isEmpty()) {
3570 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3571 return BAD_VALUE;
3572 }
3573 } else {
3574 // Disconnect
3575 // check if one opened input is not needed any more after disconnecting one device
3576 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3577 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003578 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3579 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003580 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3581 mInputs.keyAt(input_index));
3582 inputs.add(mInputs.keyAt(input_index));
3583 }
3584 }
3585 // Clear any profiles associated with the disconnected device.
3586 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3587 if (mHwModules[module_index]->mHandle == 0) {
3588 continue;
3589 }
3590 for (size_t profile_index = 0;
3591 profile_index < mHwModules[module_index]->mInputProfiles.size();
3592 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003593 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003594 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003595 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003596 profile_index, module_index);
3597 if (profile->mSamplingRates[0] == 0) {
3598 profile->mSamplingRates.clear();
3599 profile->mSamplingRates.add(0);
3600 }
3601 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3602 profile->mFormats.clear();
3603 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3604 }
3605 if (profile->mChannelMasks[0] == 0) {
3606 profile->mChannelMasks.clear();
3607 profile->mChannelMasks.add(0);
3608 }
3609 }
3610 }
3611 }
3612 } // end disconnect
3613
3614 return NO_ERROR;
3615}
3616
3617
Eric Laurente0720872014-03-11 09:30:41 -07003618void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003619{
3620 ALOGV("closeOutput(%d)", output);
3621
Eric Laurentc75307b2015-03-17 15:29:32 -07003622 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003623 if (outputDesc == NULL) {
3624 ALOGW("closeOutput() unknown output %d", output);
3625 return;
3626 }
François Gaffie036e1e92015-03-19 10:16:24 +01003627 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003628
Eric Laurente552edb2014-03-10 17:42:56 -07003629 // look for duplicated outputs connected to the output being removed.
3630 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003631 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003632 if (dupOutputDesc->isDuplicated() &&
3633 (dupOutputDesc->mOutput1 == outputDesc ||
3634 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003635 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003636 if (dupOutputDesc->mOutput1 == outputDesc) {
3637 outputDesc2 = dupOutputDesc->mOutput2;
3638 } else {
3639 outputDesc2 = dupOutputDesc->mOutput1;
3640 }
3641 // As all active tracks on duplicated output will be deleted,
3642 // and as they were also referenced on the other output, the reference
3643 // count for their stream type must be adjusted accordingly on
3644 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003645 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003646 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003647 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003648 }
3649 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3650 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3651
3652 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003653 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003654 }
3655 }
3656
Eric Laurent05b90f82014-08-27 15:32:29 -07003657 nextAudioPortGeneration();
3658
3659 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3660 if (index >= 0) {
3661 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3662 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3663 mAudioPatches.removeItemsAt(index);
3664 mpClientInterface->onAudioPatchListUpdate();
3665 }
3666
Eric Laurente552edb2014-03-10 17:42:56 -07003667 AudioParameter param;
3668 param.add(String8("closing"), String8("true"));
3669 mpClientInterface->setParameters(output, param.toString());
3670
3671 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003672 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003673 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003674}
3675
3676void AudioPolicyManager::closeInput(audio_io_handle_t input)
3677{
3678 ALOGV("closeInput(%d)", input);
3679
3680 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3681 if (inputDesc == NULL) {
3682 ALOGW("closeInput() unknown input %d", input);
3683 return;
3684 }
3685
Eric Laurent6a94d692014-05-20 11:18:06 -07003686 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003687
3688 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3689 if (index >= 0) {
3690 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3691 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3692 mAudioPatches.removeItemsAt(index);
3693 mpClientInterface->onAudioPatchListUpdate();
3694 }
3695
3696 mpClientInterface->closeInput(input);
3697 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003698}
3699
Eric Laurentc75307b2015-03-17 15:29:32 -07003700SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3701 audio_devices_t device,
3702 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003703{
3704 SortedVector<audio_io_handle_t> outputs;
3705
3706 ALOGVV("getOutputsForDevice() device %04x", device);
3707 for (size_t i = 0; i < openOutputs.size(); i++) {
3708 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003709 i, openOutputs.valueAt(i)->isDuplicated(),
3710 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003711 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3712 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3713 outputs.add(openOutputs.keyAt(i));
3714 }
3715 }
3716 return outputs;
3717}
3718
Eric Laurente0720872014-03-11 09:30:41 -07003719bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003720 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003721{
3722 if (outputs1.size() != outputs2.size()) {
3723 return false;
3724 }
3725 for (size_t i = 0; i < outputs1.size(); i++) {
3726 if (outputs1[i] != outputs2[i]) {
3727 return false;
3728 }
3729 }
3730 return true;
3731}
3732
Eric Laurente0720872014-03-11 09:30:41 -07003733void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003734{
3735 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3736 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3737 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3738 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3739
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003740 // also take into account external policy-related changes: add all outputs which are
3741 // associated with policies in the "before" and "after" output vectors
3742 ALOGVV("checkOutputForStrategy(): policy related outputs");
3743 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003744 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003745 if (desc != 0 && desc->mPolicyMix != NULL) {
3746 srcOutputs.add(desc->mIoHandle);
3747 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3748 }
3749 }
3750 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003751 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003752 if (desc != 0 && desc->mPolicyMix != NULL) {
3753 dstOutputs.add(desc->mIoHandle);
3754 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3755 }
3756 }
3757
Eric Laurente552edb2014-03-10 17:42:56 -07003758 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3759 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3760 strategy, srcOutputs[0], dstOutputs[0]);
3761 // mute strategy while moving tracks from one output to another
3762 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003763 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003764 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003765 setStrategyMute(strategy, true, desc);
3766 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003767 }
3768 }
3769
3770 // Move effects associated to this strategy from previous output to new output
3771 if (strategy == STRATEGY_MEDIA) {
3772 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3773 SortedVector<audio_io_handle_t> moved;
3774 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003775 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3776 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3777 effectDesc->mIo != fxOutput) {
3778 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003779 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3780 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003781 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003782 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003783 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003784 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003785 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003786 }
3787 }
3788 }
3789 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003790 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003791 if (i == AUDIO_STREAM_PATCH) {
3792 continue;
3793 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003794 if (getStrategy((audio_stream_type_t)i) == strategy) {
3795 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003796 }
3797 }
3798 }
3799}
3800
Eric Laurente0720872014-03-11 09:30:41 -07003801void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003802{
François Gaffie2110e042015-03-24 08:41:51 +01003803 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003804 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003805 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003806 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003807 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003808 checkOutputForStrategy(STRATEGY_SONIFICATION);
3809 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003810 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003811 checkOutputForStrategy(STRATEGY_MEDIA);
3812 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003813 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003814}
3815
Eric Laurente0720872014-03-11 09:30:41 -07003816void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003817{
François Gaffie53615e22015-03-19 09:24:12 +01003818 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003819 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003820 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003821 return;
3822 }
3823
Eric Laurent3a4311c2014-03-17 12:00:47 -07003824 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003825 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3826 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3827 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003828 // suspend A2DP output if:
3829 // (NOT already suspended) &&
3830 // ((SCO device is connected &&
3831 // (forced usage for communication || for record is SCO))) ||
3832 // (phone state is ringing || in call)
3833 //
3834 // restore A2DP output if:
3835 // (Already suspended) &&
3836 // ((SCO device is NOT connected ||
3837 // (forced usage NOT for communication && NOT for record is SCO))) &&
3838 // (phone state is NOT ringing && NOT in call)
3839 //
3840 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003841 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003842 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3843 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3844 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3845 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003846
3847 mpClientInterface->restoreOutput(a2dpOutput);
3848 mA2dpSuspended = false;
3849 }
3850 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003851 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003852 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3853 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3854 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3855 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003856
3857 mpClientInterface->suspendOutput(a2dpOutput);
3858 mA2dpSuspended = true;
3859 }
3860 }
3861}
3862
Eric Laurentc75307b2015-03-17 15:29:32 -07003863audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3864 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003865{
3866 audio_devices_t device = AUDIO_DEVICE_NONE;
3867
Eric Laurent6a94d692014-05-20 11:18:06 -07003868 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3869 if (index >= 0) {
3870 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3871 if (patchDesc->mUid != mUidCached) {
3872 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3873 outputDesc->device(), outputDesc->mPatchHandle);
3874 return outputDesc->device();
3875 }
3876 }
3877
Eric Laurente552edb2014-03-10 17:42:56 -07003878 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003879 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003880 // use device for strategy enforced audible
3881 // 2: we are in call or the strategy phone is active on the output:
3882 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003883 // 3: the strategy for enforced audible is active but not enforced on the output:
3884 // use the device for strategy enforced audible
3885 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003886 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003887 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003888 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003889 // 6: the strategy accessibility is active on the output:
3890 // use device for strategy accessibility
3891 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003892 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003893 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003894 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003895 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003896 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003897 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003898 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003899 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3900 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003901 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003902 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003903 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003904 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003905 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003906 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003907 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003908 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003909 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003910 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003911 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003912 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003913 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003914 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003915 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003916 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003917 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003918 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003919 }
3920
Eric Laurent1c333e22014-05-20 10:48:17 -07003921 ALOGV("getNewOutputDevice() selected device %x", device);
3922 return device;
3923}
3924
3925audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3926{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003927 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003928
3929 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3930 if (index >= 0) {
3931 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3932 if (patchDesc->mUid != mUidCached) {
3933 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3934 inputDesc->mDevice, inputDesc->mPatchHandle);
3935 return inputDesc->mDevice;
3936 }
3937 }
3938
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003939 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003940
Eric Laurente552edb2014-03-10 17:42:56 -07003941 return device;
3942}
3943
Eric Laurente0720872014-03-11 09:30:41 -07003944uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003945 return (uint32_t)getStrategy(stream);
3946}
3947
Eric Laurente0720872014-03-11 09:30:41 -07003948audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003949 // By checking the range of stream before calling getStrategy, we avoid
3950 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3951 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003952 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003953 return AUDIO_DEVICE_NONE;
3954 }
3955 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003956 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003957 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3958 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3959 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003960 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003961 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003962 devices = outputDesc->device();
3963 break;
3964 }
Eric Laurente552edb2014-03-10 17:42:56 -07003965 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003966
3967 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3968 and doesn't really need to.*/
3969 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3970 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3971 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3972 }
3973
Eric Laurente552edb2014-03-10 17:42:56 -07003974 return devices;
3975}
3976
François Gaffie2110e042015-03-24 08:41:51 +01003977routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3978{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003979 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003980 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003981}
3982
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003983uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3984 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003985 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3986 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3987 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003988 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3989 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3990 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003991 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003992 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003993}
3994
Eric Laurente0720872014-03-11 09:30:41 -07003995void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003996 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003997 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003998 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3999 updateDevicesAndOutputs();
4000 break;
4001 default:
4002 break;
4003 }
4004}
4005
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004006uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4007 switch(event) {
4008 case STARTING_OUTPUT:
4009 mBeaconMuteRefCount++;
4010 break;
4011 case STOPPING_OUTPUT:
4012 if (mBeaconMuteRefCount > 0) {
4013 mBeaconMuteRefCount--;
4014 }
4015 break;
4016 case STARTING_BEACON:
4017 mBeaconPlayingRefCount++;
4018 break;
4019 case STOPPING_BEACON:
4020 if (mBeaconPlayingRefCount > 0) {
4021 mBeaconPlayingRefCount--;
4022 }
4023 break;
4024 }
4025
4026 if (mBeaconMuteRefCount > 0) {
4027 // any playback causes beacon to be muted
4028 return setBeaconMute(true);
4029 } else {
4030 // no other playback: unmute when beacon starts playing, mute when it stops
4031 return setBeaconMute(mBeaconPlayingRefCount == 0);
4032 }
4033}
4034
4035uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4036 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4037 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4038 // keep track of muted state to avoid repeating mute/unmute operations
4039 if (mBeaconMuted != mute) {
4040 // mute/unmute AUDIO_STREAM_TTS on all outputs
4041 ALOGV("\t muting %d", mute);
4042 uint32_t maxLatency = 0;
4043 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004044 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004045 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004046 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004047 0 /*delay*/, AUDIO_DEVICE_NONE);
4048 const uint32_t latency = desc->latency() * 2;
4049 if (latency > maxLatency) {
4050 maxLatency = latency;
4051 }
4052 }
4053 mBeaconMuted = mute;
4054 return maxLatency;
4055 }
4056 return 0;
4057}
4058
Eric Laurente0720872014-03-11 09:30:41 -07004059audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004060 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004061{
Paul McLeanaa981192015-03-21 09:55:15 -07004062 // Routing
4063 // see if we have an explicit route
4064 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4065 // (getStrategy(stream)).
4066 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4067 // and activity count is non-zero
4068 // the device = the device from the descriptor in the RouteMap, and exit.
4069 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4070 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4071 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004072 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004073 return route->mDeviceDescriptor->type();
4074 }
4075 }
4076
Eric Laurente552edb2014-03-10 17:42:56 -07004077 if (fromCache) {
4078 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4079 strategy, mDeviceForStrategy[strategy]);
4080 return mDeviceForStrategy[strategy];
4081 }
François Gaffie2110e042015-03-24 08:41:51 +01004082 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004083}
4084
Eric Laurente0720872014-03-11 09:30:41 -07004085void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004086{
4087 for (int i = 0; i < NUM_STRATEGIES; i++) {
4088 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4089 }
4090 mPreviousOutputs = mOutputs;
4091}
4092
Eric Laurent1f2f2232014-06-02 12:01:23 -07004093uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004094 audio_devices_t prevDevice,
4095 uint32_t delayMs)
4096{
4097 // mute/unmute strategies using an incompatible device combination
4098 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4099 // if unmuting, unmute only after the specified delay
4100 if (outputDesc->isDuplicated()) {
4101 return 0;
4102 }
4103
4104 uint32_t muteWaitMs = 0;
4105 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004106 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004107
4108 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4109 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004110 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004111 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4112 bool doMute = false;
4113
4114 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4115 doMute = true;
4116 outputDesc->mStrategyMutedByDevice[i] = true;
4117 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4118 doMute = true;
4119 outputDesc->mStrategyMutedByDevice[i] = false;
4120 }
Eric Laurent99401132014-05-07 19:48:15 -07004121 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004122 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004123 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004124 // skip output if it does not share any device with current output
4125 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4126 == AUDIO_DEVICE_NONE) {
4127 continue;
4128 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004129 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4130 mute ? "muting" : "unmuting", i, curDevice);
4131 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004132 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004133 if (mute) {
4134 // FIXME: should not need to double latency if volume could be applied
4135 // immediately by the audioflinger mixer. We must account for the delay
4136 // between now and the next time the audioflinger thread for this output
4137 // will process a buffer (which corresponds to one buffer size,
4138 // usually 1/2 or 1/4 of the latency).
4139 if (muteWaitMs < desc->latency() * 2) {
4140 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004141 }
4142 }
4143 }
4144 }
4145 }
4146 }
4147
Eric Laurent99401132014-05-07 19:48:15 -07004148 // temporary mute output if device selection changes to avoid volume bursts due to
4149 // different per device volumes
4150 if (outputDesc->isActive() && (device != prevDevice)) {
4151 if (muteWaitMs < outputDesc->latency() * 2) {
4152 muteWaitMs = outputDesc->latency() * 2;
4153 }
4154 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004155 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004156 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004157 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004158 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004159 muteWaitMs *2, device);
4160 }
4161 }
4162 }
4163
Eric Laurente552edb2014-03-10 17:42:56 -07004164 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4165 if (muteWaitMs > delayMs) {
4166 muteWaitMs -= delayMs;
4167 usleep(muteWaitMs * 1000);
4168 return muteWaitMs;
4169 }
4170 return 0;
4171}
4172
Eric Laurentc75307b2015-03-17 15:29:32 -07004173uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004174 audio_devices_t device,
4175 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004176 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004177 audio_patch_handle_t *patchHandle,
4178 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004179{
Eric Laurentc75307b2015-03-17 15:29:32 -07004180 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004181 AudioParameter param;
4182 uint32_t muteWaitMs;
4183
4184 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004185 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4186 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004187 return muteWaitMs;
4188 }
4189 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4190 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004191 if ((device != AUDIO_DEVICE_NONE) &&
4192 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004193 return 0;
4194 }
4195
4196 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004197 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004198
4199 audio_devices_t prevDevice = outputDesc->mDevice;
4200
Paul McLeanaa981192015-03-21 09:55:15 -07004201 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004202
4203 if (device != AUDIO_DEVICE_NONE) {
4204 outputDesc->mDevice = device;
4205 }
4206 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4207
4208 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004209 // the requested device is AUDIO_DEVICE_NONE
4210 // OR the requested device is the same as current device
4211 // AND force is not specified
4212 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004213 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004214 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4215 !force &&
4216 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004217 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004218 return muteWaitMs;
4219 }
4220
4221 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004222
Eric Laurente552edb2014-03-10 17:42:56 -07004223 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004224 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004225 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004226 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004227 DeviceVector deviceList = (address == NULL) ?
4228 mAvailableOutputDevices.getDevicesFromType(device)
4229 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004230 if (!deviceList.isEmpty()) {
4231 struct audio_patch patch;
4232 outputDesc->toAudioPortConfig(&patch.sources[0]);
4233 patch.num_sources = 1;
4234 patch.num_sinks = 0;
4235 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4236 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004237 patch.num_sinks++;
4238 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004239 ssize_t index;
4240 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4241 index = mAudioPatches.indexOfKey(*patchHandle);
4242 } else {
4243 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4244 }
4245 sp< AudioPatch> patchDesc;
4246 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4247 if (index >= 0) {
4248 patchDesc = mAudioPatches.valueAt(index);
4249 afPatchHandle = patchDesc->mAfPatchHandle;
4250 }
4251
Eric Laurent1c333e22014-05-20 10:48:17 -07004252 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004253 &afPatchHandle,
4254 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004255 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4256 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004257 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004258 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004259 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004260 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004261 addAudioPatch(patchDesc->mHandle, patchDesc);
4262 } else {
4263 patchDesc->mPatch = patch;
4264 }
4265 patchDesc->mAfPatchHandle = afPatchHandle;
4266 patchDesc->mUid = mUidCached;
4267 if (patchHandle) {
4268 *patchHandle = patchDesc->mHandle;
4269 }
4270 outputDesc->mPatchHandle = patchDesc->mHandle;
4271 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004272 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004273 }
4274 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004275
4276 // inform all input as well
4277 for (size_t i = 0; i < mInputs.size(); i++) {
4278 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004279 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004280 AudioParameter inputCmd = AudioParameter();
4281 ALOGV("%s: inform input %d of device:%d", __func__,
4282 inputDescriptor->mIoHandle, device);
4283 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4284 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4285 inputCmd.toString(),
4286 delayMs);
4287 }
4288 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004289 }
Eric Laurente552edb2014-03-10 17:42:56 -07004290
4291 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004292 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004293
4294 return muteWaitMs;
4295}
4296
Eric Laurentc75307b2015-03-17 15:29:32 -07004297status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004298 int delayMs,
4299 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004300{
Eric Laurent6a94d692014-05-20 11:18:06 -07004301 ssize_t index;
4302 if (patchHandle) {
4303 index = mAudioPatches.indexOfKey(*patchHandle);
4304 } else {
4305 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4306 }
4307 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004308 return INVALID_OPERATION;
4309 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004310 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4311 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004312 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4313 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004314 removeAudioPatch(patchDesc->mHandle);
4315 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004316 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004317 return status;
4318}
4319
4320status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4321 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004322 bool force,
4323 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004324{
4325 status_t status = NO_ERROR;
4326
Eric Laurent1f2f2232014-06-02 12:01:23 -07004327 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004328 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4329 inputDesc->mDevice = device;
4330
4331 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4332 if (!deviceList.isEmpty()) {
4333 struct audio_patch patch;
4334 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004335 // AUDIO_SOURCE_HOTWORD is for internal use only:
4336 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004337 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4338 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004339 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4340 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004341 patch.num_sinks = 1;
4342 //only one input device for now
4343 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004344 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004345 ssize_t index;
4346 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4347 index = mAudioPatches.indexOfKey(*patchHandle);
4348 } else {
4349 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4350 }
4351 sp< AudioPatch> patchDesc;
4352 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4353 if (index >= 0) {
4354 patchDesc = mAudioPatches.valueAt(index);
4355 afPatchHandle = patchDesc->mAfPatchHandle;
4356 }
4357
Eric Laurent1c333e22014-05-20 10:48:17 -07004358 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004359 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004360 0);
4361 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004362 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004363 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004364 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004365 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004366 addAudioPatch(patchDesc->mHandle, patchDesc);
4367 } else {
4368 patchDesc->mPatch = patch;
4369 }
4370 patchDesc->mAfPatchHandle = afPatchHandle;
4371 patchDesc->mUid = mUidCached;
4372 if (patchHandle) {
4373 *patchHandle = patchDesc->mHandle;
4374 }
4375 inputDesc->mPatchHandle = patchDesc->mHandle;
4376 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004377 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004378 }
4379 }
4380 }
4381 return status;
4382}
4383
Eric Laurent6a94d692014-05-20 11:18:06 -07004384status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4385 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004386{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004387 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004388 ssize_t index;
4389 if (patchHandle) {
4390 index = mAudioPatches.indexOfKey(*patchHandle);
4391 } else {
4392 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4393 }
4394 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004395 return INVALID_OPERATION;
4396 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004397 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4398 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004399 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4400 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004401 removeAudioPatch(patchDesc->mHandle);
4402 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004403 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004404 return status;
4405}
4406
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004407sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004408 String8 address,
4409 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004410 audio_format_t& format,
4411 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004412 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004413{
4414 // Choose an input profile based on the requested capture parameters: select the first available
4415 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004416 //
4417 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4418 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004419
4420 for (size_t i = 0; i < mHwModules.size(); i++)
4421 {
4422 if (mHwModules[i]->mHandle == 0) {
4423 continue;
4424 }
4425 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4426 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004427 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004428 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004429 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004430 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004431 format,
4432 &format /*updatedFormat*/,
4433 channelMask,
4434 &channelMask /*updatedChannelMask*/,
4435 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004436
Eric Laurente552edb2014-03-10 17:42:56 -07004437 return profile;
4438 }
4439 }
4440 }
4441 return NULL;
4442}
4443
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004444
4445audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004446 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004447{
François Gaffie036e1e92015-03-19 10:16:24 +01004448 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4449 audio_devices_t selectedDeviceFromMix =
4450 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004451
François Gaffie036e1e92015-03-19 10:16:24 +01004452 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4453 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004454 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004455 return getDeviceForInputSource(inputSource);
4456}
4457
4458audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4459{
Paul McLean466dc8e2015-04-17 13:15:36 -06004460 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4461 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004462 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004463 return route->mDeviceDescriptor->type();
4464 }
4465 }
4466
4467 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004468}
4469
Eric Laurente0720872014-03-11 09:30:41 -07004470float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004471 int index,
4472 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004473{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004474 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004475
4476 // if a headset is connected, apply the following rules to ring tones and notifications
4477 // to avoid sound level bursts in user's ears:
4478 // - always attenuate ring tones and notifications volume by 6dB
4479 // - if music is playing, always limit the volume to current music volume,
4480 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004481 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004482 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4483 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4484 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4485 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4486 ((stream_strategy == STRATEGY_SONIFICATION)
4487 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004488 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004489 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004490 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4491 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004492 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004493 // when the phone is ringing we must consider that music could have been paused just before
4494 // by the music application and behave as if music was active if the last music track was
4495 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004496 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004497 mLimitRingtoneVolume) {
4498 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004499 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4500 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004501 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004502 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4503 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4504 if (volumeDb > minVolDB) {
4505 volumeDb = minVolDB;
4506 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004507 }
4508 }
4509 }
4510
Eric Laurentffbc80f2015-03-18 18:30:19 -07004511 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004512}
4513
Eric Laurente0720872014-03-11 09:30:41 -07004514status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004515 int index,
4516 const sp<AudioOutputDescriptor>& outputDesc,
4517 audio_devices_t device,
4518 int delayMs,
4519 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004520{
Eric Laurente552edb2014-03-10 17:42:56 -07004521 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004522 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004523 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004524 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004525 return NO_ERROR;
4526 }
François Gaffie2110e042015-03-24 08:41:51 +01004527 audio_policy_forced_cfg_t forceUseForComm =
4528 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004529 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004530 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4531 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004532 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004533 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004534 return INVALID_OPERATION;
4535 }
4536
Eric Laurentc75307b2015-03-17 15:29:32 -07004537 if (device == AUDIO_DEVICE_NONE) {
4538 device = outputDesc->device();
4539 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004540
Eric Laurentffbc80f2015-03-18 18:30:19 -07004541 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004542 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004543 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004544 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004545
Eric Laurentffbc80f2015-03-18 18:30:19 -07004546 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004547
Eric Laurent3b73df72014-03-11 09:06:29 -07004548 if (stream == AUDIO_STREAM_VOICE_CALL ||
4549 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004550 float voiceVolume;
4551 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004552 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004553 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004554 } else {
4555 voiceVolume = 1.0;
4556 }
4557
Eric Laurentc75307b2015-03-17 15:29:32 -07004558 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004559 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4560 mLastVoiceVolume = voiceVolume;
4561 }
4562 }
4563
4564 return NO_ERROR;
4565}
4566
Eric Laurentc75307b2015-03-17 15:29:32 -07004567void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4568 audio_devices_t device,
4569 int delayMs,
4570 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004571{
Eric Laurentc75307b2015-03-17 15:29:32 -07004572 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004573
Eric Laurent3b73df72014-03-11 09:06:29 -07004574 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004575 if (stream == AUDIO_STREAM_PATCH) {
4576 continue;
4577 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004578 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004579 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4580 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004581 device,
4582 delayMs,
4583 force);
4584 }
4585}
4586
Eric Laurente0720872014-03-11 09:30:41 -07004587void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004588 bool on,
4589 const sp<AudioOutputDescriptor>& outputDesc,
4590 int delayMs,
4591 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004592{
Eric Laurent72249d52015-06-08 11:12:15 -07004593 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4594 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004595 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004596 if (stream == AUDIO_STREAM_PATCH) {
4597 continue;
4598 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004599 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004600 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004601 }
4602 }
4603}
4604
Eric Laurente0720872014-03-11 09:30:41 -07004605void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004606 bool on,
4607 const sp<AudioOutputDescriptor>& outputDesc,
4608 int delayMs,
4609 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004610{
Eric Laurentc75307b2015-03-17 15:29:32 -07004611 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004612 if (device == AUDIO_DEVICE_NONE) {
4613 device = outputDesc->device();
4614 }
4615
Eric Laurentc75307b2015-03-17 15:29:32 -07004616 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4617 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004618
4619 if (on) {
4620 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004621 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004622 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004623 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004624 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004625 }
4626 }
4627 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4628 outputDesc->mMuteCount[stream]++;
4629 } else {
4630 if (outputDesc->mMuteCount[stream] == 0) {
4631 ALOGV("setStreamMute() unmuting non muted stream!");
4632 return;
4633 }
4634 if (--outputDesc->mMuteCount[stream] == 0) {
4635 checkAndSetVolume(stream,
4636 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004637 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004638 device,
4639 delayMs);
4640 }
4641 }
4642}
4643
Eric Laurente0720872014-03-11 09:30:41 -07004644void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004645 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004646{
Eric Laurent87ffa392015-05-22 10:32:38 -07004647 if(!hasPrimaryOutput()) {
4648 return;
4649 }
4650
Eric Laurente552edb2014-03-10 17:42:56 -07004651 // if the stream pertains to sonification strategy and we are in call we must
4652 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4653 // in the device used for phone strategy and play the tone if the selected device does not
4654 // interfere with the device used for phone strategy
4655 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4656 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004657 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004658 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4659 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004660 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004661 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4662 stream, starting, outputDesc->mDevice, stateChange);
4663 if (outputDesc->mRefCount[stream]) {
4664 int muteCount = 1;
4665 if (stateChange) {
4666 muteCount = outputDesc->mRefCount[stream];
4667 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004668 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004669 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4670 for (int i = 0; i < muteCount; i++) {
4671 setStreamMute(stream, starting, mPrimaryOutput);
4672 }
4673 } else {
4674 ALOGV("handleIncallSonification() high visibility");
4675 if (outputDesc->device() &
4676 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4677 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4678 for (int i = 0; i < muteCount; i++) {
4679 setStreamMute(stream, starting, mPrimaryOutput);
4680 }
4681 }
4682 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004683 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4684 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004685 } else {
4686 mpClientInterface->stopTone();
4687 }
4688 }
4689 }
4690 }
4691}
4692
Paul McLeanaa981192015-03-21 09:55:15 -07004693// --- SessionRoute class implementation
4694void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004695 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4696 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004697 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4698 mRefCount, mActivityCount);
4699}
4700
4701// --- SessionRouteMap class implementation
4702bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4703{
4704 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4705}
4706
Eric Laurent493404d2015-04-21 15:07:36 -07004707bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4708{
4709 if (indexOfKey(session) >= 0) {
4710 if (valueFor(session)->mChanged) {
4711 valueFor(session)->mChanged = false;
4712 return true;
4713 }
4714 }
4715 return false;
4716}
4717
Paul McLeanaa981192015-03-21 09:55:15 -07004718void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4719{
4720 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4721 if (route != 0) {
4722 ALOG_ASSERT(route->mRefCount > 0);
4723 --route->mRefCount;
4724 if (route->mRefCount <= 0) {
4725 removeItem(session);
4726 }
4727 }
4728}
4729
4730int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4731{
4732 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4733 return route != 0 ? ++(route->mActivityCount) : -1;
4734}
4735
4736int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4737{
4738 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4739 if (route != 0 && route->mActivityCount > 0) {
4740 return --(route->mActivityCount);
4741 } else {
4742 return -1;
4743 }
4744}
4745
4746void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4747 ALOGI("%s ----", caption);
4748 for(size_t index = 0; index < size(); index++) {
4749 valueAt(index)->log(" ");
4750 }
4751}
4752
Paul McLean466dc8e2015-04-17 13:15:36 -06004753void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4754 audio_stream_type_t streamType,
4755 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004756 sp<DeviceDescriptor> descriptor,
4757 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004758{
4759 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4760 ALOGE("Adding Output Route to InputRouteMap");
4761 return;
4762 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4763 ALOGE("Adding Input Route to OutputRouteMap");
4764 return;
4765 }
4766
4767 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4768
4769 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004770 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4771 ((route->mDeviceDescriptor != 0) &&
4772 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004773 route->mChanged = true;
4774 }
4775 route->mRefCount++;
4776 route->mDeviceDescriptor = descriptor;
4777 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004778 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004779 route->mRefCount++;
4780 add(session, route);
4781 if (descriptor != 0) {
4782 route->mChanged = true;
4783 }
4784 }
4785}
4786
Eric Laurente0720872014-03-11 09:30:41 -07004787void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004788{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004789 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004790 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004791 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004792 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004793 mAvailableOutputDevices.add(mDefaultOutputDevice);
4794 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004795
4796 module = new HwModule("primary");
4797
Eric Laurent322b4d22015-04-03 15:57:54 -07004798 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4799 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004800 profile->mSamplingRates.add(44100);
4801 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4802 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004803 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004804 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4805 module->mOutputProfiles.add(profile);
4806
Eric Laurent322b4d22015-04-03 15:57:54 -07004807 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4808 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004809 profile->mSamplingRates.add(8000);
4810 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4811 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004812 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004813 module->mInputProfiles.add(profile);
4814
4815 mHwModules.add(module);
4816}
4817
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004818audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4819{
4820 // flags to stream type mapping
4821 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4822 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4823 }
4824 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4825 return AUDIO_STREAM_BLUETOOTH_SCO;
4826 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004827 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4828 return AUDIO_STREAM_TTS;
4829 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004830
4831 // usage to stream type mapping
4832 switch (attr->usage) {
4833 case AUDIO_USAGE_MEDIA:
4834 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004835 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4836 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004837 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004838 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4839 return AUDIO_STREAM_ALARM;
4840 }
4841 if (isStreamActive(AUDIO_STREAM_RING)) {
4842 return AUDIO_STREAM_RING;
4843 }
4844 if (isInCall()) {
4845 return AUDIO_STREAM_VOICE_CALL;
4846 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004847 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004848 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4849 return AUDIO_STREAM_SYSTEM;
4850 case AUDIO_USAGE_VOICE_COMMUNICATION:
4851 return AUDIO_STREAM_VOICE_CALL;
4852
4853 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4854 return AUDIO_STREAM_DTMF;
4855
4856 case AUDIO_USAGE_ALARM:
4857 return AUDIO_STREAM_ALARM;
4858 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4859 return AUDIO_STREAM_RING;
4860
4861 case AUDIO_USAGE_NOTIFICATION:
4862 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4863 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4864 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4865 case AUDIO_USAGE_NOTIFICATION_EVENT:
4866 return AUDIO_STREAM_NOTIFICATION;
4867
4868 case AUDIO_USAGE_UNKNOWN:
4869 default:
4870 return AUDIO_STREAM_MUSIC;
4871 }
4872}
Eric Laurente83b55d2014-11-14 10:06:21 -08004873
François Gaffie53615e22015-03-19 09:24:12 +01004874bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4875{
Eric Laurente83b55d2014-11-14 10:06:21 -08004876 // has flags that map to a strategy?
4877 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4878 return true;
4879 }
4880
4881 // has known usage?
4882 switch (paa->usage) {
4883 case AUDIO_USAGE_UNKNOWN:
4884 case AUDIO_USAGE_MEDIA:
4885 case AUDIO_USAGE_VOICE_COMMUNICATION:
4886 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4887 case AUDIO_USAGE_ALARM:
4888 case AUDIO_USAGE_NOTIFICATION:
4889 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4890 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4891 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4892 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4893 case AUDIO_USAGE_NOTIFICATION_EVENT:
4894 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4895 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4896 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4897 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004898 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004899 break;
4900 default:
4901 return false;
4902 }
4903 return true;
4904}
4905
François Gaffiead3183e2015-03-18 16:55:35 +01004906bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4907 routing_strategy strategy, uint32_t inPastMs,
4908 nsecs_t sysTime) const
4909{
4910 if ((sysTime == 0) && (inPastMs != 0)) {
4911 sysTime = systemTime();
4912 }
4913 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4914 if (i == AUDIO_STREAM_PATCH) {
4915 continue;
4916 }
4917 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4918 (NUM_STRATEGIES == strategy)) &&
4919 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4920 return true;
4921 }
4922 }
4923 return false;
4924}
4925
François Gaffie2110e042015-03-24 08:41:51 +01004926audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4927{
4928 return mEngine->getForceUse(usage);
4929}
4930
4931bool AudioPolicyManager::isInCall()
4932{
4933 return isStateInCall(mEngine->getPhoneState());
4934}
4935
4936bool AudioPolicyManager::isStateInCall(int state)
4937{
4938 return is_state_in_call(state);
4939}
4940
Eric Laurente552edb2014-03-10 17:42:56 -07004941}; // namespace android