blob: 127e22f70ce86a836f147fcd7f0ef154a5044ea1 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
François Gaffie2110e042015-03-24 08:41:51 +0100264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800305 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700306 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
307
308 // release existing RX patch if any
309 if (mCallRxPatch != 0) {
310 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
311 mCallRxPatch.clear();
312 }
313 // release TX patch if any
314 if (mCallTxPatch != 0) {
315 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
316 mCallTxPatch.clear();
317 }
318
319 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
320 // via setOutputDevice() on primary output.
321 // Otherwise, create two audio patches for TX and RX path.
322 if (availablePrimaryOutputDevices() & rxDevice) {
323 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
324 // If the TX device is also on the primary HW module, setOutputDevice() will take care
325 // of it due to legacy implementation. If not, create a patch.
326 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
327 == AUDIO_DEVICE_NONE) {
328 createTxPatch = true;
329 }
330 } else {
331 // create RX path audio patch
332 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
333 ALOG_ASSERT(!deviceList.isEmpty(),
334 "updateCallRouting() selected device not in output device list");
335 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
336 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
337 ALOG_ASSERT(!deviceList.isEmpty(),
338 "updateCallRouting() no telephony RX device");
339 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
340
341 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
342 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
343
344 // request to reuse existing output stream if one is already opened to reach the RX device
345 SortedVector<audio_io_handle_t> outputs =
346 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700347 audio_io_handle_t output = selectOutput(outputs,
348 AUDIO_OUTPUT_FLAG_NONE,
349 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700350 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 ALOG_ASSERT(!outputDesc->isDuplicated(),
353 "updateCallRouting() RX device output is duplicated");
354 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700355 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700356 patch.num_sources = 2;
357 }
358
359 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
360 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
361 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
362 status);
363 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100364 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700365 mCallRxPatch->mAfPatchHandle = afPatchHandle;
366 mCallRxPatch->mUid = mUidCached;
367 }
368 createTxPatch = true;
369 }
370 if (createTxPatch) {
371
372 struct audio_patch patch;
373 patch.num_sources = 1;
374 patch.num_sinks = 1;
375 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
376 ALOG_ASSERT(!deviceList.isEmpty(),
377 "updateCallRouting() selected device not in input device list");
378 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
379 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
381 ALOG_ASSERT(!deviceList.isEmpty(),
382 "updateCallRouting() no telephony TX device");
383 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
384 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
385
386 SortedVector<audio_io_handle_t> outputs =
387 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700388 audio_io_handle_t output = selectOutput(outputs,
389 AUDIO_OUTPUT_FLAG_NONE,
390 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700391 // request to reuse existing output stream if one is already opened to reach the TX
392 // path output device
393 if (output != AUDIO_IO_HANDLE_NONE) {
394 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
395 ALOG_ASSERT(!outputDesc->isDuplicated(),
396 "updateCallRouting() RX device output is duplicated");
397 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700398 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 patch.num_sources = 2;
400 }
401
402 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
403 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
404 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
405 status);
406 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100407 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 mCallTxPatch->mAfPatchHandle = afPatchHandle;
409 mCallTxPatch->mUid = mUidCached;
410 }
411 }
412}
413
Eric Laurente0720872014-03-11 09:30:41 -0700414void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700415{
416 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100417 // store previous phone state for management of sonification strategy below
418 int oldState = mEngine->getPhoneState();
419
420 if (mEngine->setPhoneState(state) != NO_ERROR) {
421 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700422 return;
423 }
François Gaffie2110e042015-03-24 08:41:51 +0100424 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700425 // if leaving call state, handle special case of active streams
426 // pertaining to sonification strategy see handleIncallSonification()
427 if (isInCall()) {
428 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (stream == AUDIO_STREAM_PATCH) {
431 continue;
432 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700433 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800435
436 // force reevaluating accessibility routing when call starts
437 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 /**
441 * Switching to or from incall state or switching between telephony and VoIP lead to force
442 * routing command.
443 */
444 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
445 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700446
447 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700448 checkA2dpSuspend();
449 checkOutputForAllStrategies();
450 updateDevicesAndOutputs();
451
Eric Laurentc75307b2015-03-17 15:29:32 -0700452 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -0700453
Eric Laurente552edb2014-03-10 17:42:56 -0700454 int delayMs = 0;
455 if (isStateInCall(state)) {
456 nsecs_t sysTime = systemTime();
457 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700458 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700459 // mute media and sonification strategies and delay device switch by the largest
460 // latency of any output where either strategy is active.
461 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100462 if ((isStrategyActive(desc, STRATEGY_MEDIA,
463 SONIFICATION_HEADSET_MUSIC_DELAY,
464 sysTime) ||
465 isStrategyActive(desc, STRATEGY_SONIFICATION,
466 SONIFICATION_HEADSET_MUSIC_DELAY,
467 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700468 (delayMs < (int)desc->latency()*2)) {
469 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700471 setStrategyMute(STRATEGY_MEDIA, true, desc);
472 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700473 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700474 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
475 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700476 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
477 }
478 }
479
Eric Laurentc2730ba2014-07-20 15:47:07 -0700480 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
481 // the device returned is not necessarily reachable via this output
482 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
483 // force routing command to audio hardware when ending call
484 // even if no device change is needed
485 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
486 rxDevice = hwOutputDesc->device();
487 }
Eric Laurente552edb2014-03-10 17:42:56 -0700488
Eric Laurentc2730ba2014-07-20 15:47:07 -0700489 if (state == AUDIO_MODE_IN_CALL) {
490 updateCallRouting(rxDevice, delayMs);
491 } else if (oldState == AUDIO_MODE_IN_CALL) {
492 if (mCallRxPatch != 0) {
493 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
494 mCallRxPatch.clear();
495 }
496 if (mCallTxPatch != 0) {
497 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
498 mCallTxPatch.clear();
499 }
500 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
501 } else {
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504 // if entering in call state, handle special case of active streams
505 // pertaining to sonification strategy see handleIncallSonification()
506 if (isStateInCall(state)) {
507 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700512 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700513 }
514 }
515
516 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700517 if (state == AUDIO_MODE_RINGTONE &&
518 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 mLimitRingtoneVolume = true;
520 } else {
521 mLimitRingtoneVolume = false;
522 }
523}
524
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700525audio_mode_t AudioPolicyManager::getPhoneState() {
526 return mEngine->getPhoneState();
527}
528
Eric Laurente0720872014-03-11 09:30:41 -0700529void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
François Gaffie2110e042015-03-24 08:41:51 +0100532 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700533
François Gaffie2110e042015-03-24 08:41:51 +0100534 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
535 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
536 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700537 }
François Gaffie2110e042015-03-24 08:41:51 +0100538 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
539 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
540 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700541
542 // check for device and output changes triggered by new force usage
543 checkA2dpSuspend();
544 checkOutputForAllStrategies();
545 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100546 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700547 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
548 updateCallRouting(newDevice);
549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700551 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
552 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
553 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
554 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700558 }
559 }
560
François Gaffie53615e22015-03-19 09:24:12 +0100561 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700563 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565
566}
567
Eric Laurente0720872014-03-11 09:30:41 -0700568void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
570 ALOGV("setSystemProperty() property %s, value %s", property, value);
571}
572
573// Find a direct output profile compatible with the parameters passed, even if the input flags do
574// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700576 audio_devices_t device,
577 uint32_t samplingRate,
578 audio_format_t format,
579 audio_channel_mask_t channelMask,
580 audio_output_flags_t flags)
581{
582 for (size_t i = 0; i < mHwModules.size(); i++) {
583 if (mHwModules[i]->mHandle == 0) {
584 continue;
585 }
586 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700587 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Andy Hungf129b032015-04-07 13:45:50 -0700588 bool found = profile->isCompatibleProfile(device, String8(""),
589 samplingRate, NULL /*updatedSamplingRate*/,
590 format, NULL /*updatedFormat*/,
591 channelMask, NULL /*updatedChannelMask*/,
Glenn Kastencbd48022014-07-24 13:46:44 -0700592 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
593 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700594 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
595 return profile;
596 }
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
598 }
599 return 0;
600}
601
Eric Laurente0720872014-03-11 09:30:41 -0700602audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags,
607 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700608{
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
611 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
612 device, stream, samplingRate, format, channelMask, flags);
613
Eric Laurente83b55d2014-11-14 10:06:21 -0800614 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
615 stream, samplingRate,format, channelMask,
616 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700617}
618
Eric Laurente83b55d2014-11-14 10:06:21 -0800619status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
620 audio_io_handle_t *output,
621 audio_session_t session,
622 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700623 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800624 uint32_t samplingRate,
625 audio_format_t format,
626 audio_channel_mask_t channelMask,
627 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700628 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800629 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700630{
Eric Laurente83b55d2014-11-14 10:06:21 -0800631 audio_attributes_t attributes;
632 if (attr != NULL) {
633 if (!isValidAttributes(attr)) {
634 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
635 attr->usage, attr->content_type, attr->flags,
636 attr->tags);
637 return BAD_VALUE;
638 }
639 attributes = *attr;
640 } else {
641 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
642 ALOGE("getOutputForAttr(): invalid stream type");
643 return BAD_VALUE;
644 }
645 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700646 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700647 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100648 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
649 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
650 if (!audio_is_linear_pcm(format)) {
651 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800652 }
François Gaffie036e1e92015-03-19 10:16:24 +0100653 *stream = streamTypefromAttributesInt(&attributes);
654 *output = desc->mIoHandle;
655 ALOGV("getOutputForAttr() returns output %d", *output);
656 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800657 }
658 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
659 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
660 return BAD_VALUE;
661 }
662
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700663 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
664 " session %d selectedDeviceId %d",
665 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
666 session, selectedDeviceId);
667
668 *stream = streamTypefromAttributesInt(&attributes);
669
670 // Explicit routing?
671 sp<DeviceDescriptor> deviceDesc;
672 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
673 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
674 deviceDesc = mAvailableOutputDevices[i];
675 break;
676 }
677 }
678 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700679
Eric Laurente83b55d2014-11-14 10:06:21 -0800680 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700681 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700682
Eric Laurente83b55d2014-11-14 10:06:21 -0800683 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700684 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
685 }
686
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700687 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700688 device, samplingRate, format, channelMask, flags);
689
Eric Laurente83b55d2014-11-14 10:06:21 -0800690 *output = getOutputForDevice(device, session, *stream,
691 samplingRate, format, channelMask,
692 flags, offloadInfo);
693 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700694 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800695 return INVALID_OPERATION;
696 }
Paul McLeanaa981192015-03-21 09:55:15 -0700697
Eric Laurente83b55d2014-11-14 10:06:21 -0800698 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700699}
700
701audio_io_handle_t AudioPolicyManager::getOutputForDevice(
702 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800703 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700704 audio_stream_type_t stream,
705 uint32_t samplingRate,
706 audio_format_t format,
707 audio_channel_mask_t channelMask,
708 audio_output_flags_t flags,
709 const audio_offload_info_t *offloadInfo)
710{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700711 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700712 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700713 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700714
Eric Laurente552edb2014-03-10 17:42:56 -0700715#ifdef AUDIO_POLICY_TEST
716 if (mCurOutput != 0) {
717 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
718 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
719
720 if (mTestOutputs[mCurOutput] == 0) {
721 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700722 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
723 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700724 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700725 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700726 outputDesc->mFlags =
727 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700728 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700729 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
730 config.sample_rate = mTestSamplingRate;
731 config.channel_mask = mTestChannels;
732 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700733 if (offloadInfo != NULL) {
734 config.offload_info = *offloadInfo;
735 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700736 status = mpClientInterface->openOutput(0,
737 &mTestOutputs[mCurOutput],
738 &config,
739 &outputDesc->mDevice,
740 String8(""),
741 &outputDesc->mLatency,
742 outputDesc->mFlags);
743 if (status == NO_ERROR) {
744 outputDesc->mSamplingRate = config.sample_rate;
745 outputDesc->mFormat = config.format;
746 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700747 AudioParameter outputCmd = AudioParameter();
748 outputCmd.addInt(String8("set_id"),mCurOutput);
749 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
750 addOutput(mTestOutputs[mCurOutput], outputDesc);
751 }
752 }
753 return mTestOutputs[mCurOutput];
754 }
755#endif //AUDIO_POLICY_TEST
756
757 // open a direct output if required by specified parameters
758 //force direct flag if offload flag is set: offloading implies a direct output stream
759 // and all common behaviors are driven by checking only the direct flag
760 // this should normally be set appropriately in the policy configuration file
761 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700762 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700763 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700764 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
765 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
766 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800767 // only allow deep buffering for music stream type
768 if (stream != AUDIO_STREAM_MUSIC) {
769 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
770 }
Eric Laurente552edb2014-03-10 17:42:56 -0700771
Eric Laurentb732cf52014-09-24 19:08:21 -0700772 sp<IOProfile> profile;
773
774 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700775 // and not explicitly requested
776 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
777 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700778 audio_channel_count_from_out_mask(channelMask) <= 2) {
779 goto non_direct_output;
780 }
781
Eric Laurente552edb2014-03-10 17:42:56 -0700782 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
783 // creating an offloaded track and tearing it down immediately after start when audioflinger
784 // detects there is an active non offloadable effect.
785 // FIXME: We should check the audio session here but we do not have it in this context.
786 // This may prevent offloading in rare situations where effects are left active by apps
787 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700788
Eric Laurente552edb2014-03-10 17:42:56 -0700789 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100790 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700791 profile = getProfileForDirectOutput(device,
792 samplingRate,
793 format,
794 channelMask,
795 (audio_output_flags_t)flags);
796 }
797
Eric Laurent1c333e22014-05-20 10:48:17 -0700798 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700799 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700800
801 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700802 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700803 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
804 outputDesc = desc;
805 // reuse direct output if currently open and configured with same parameters
806 if ((samplingRate == outputDesc->mSamplingRate) &&
807 (format == outputDesc->mFormat) &&
808 (channelMask == outputDesc->mChannelMask)) {
809 outputDesc->mDirectOpenCount++;
810 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
811 return mOutputs.keyAt(i);
812 }
813 }
814 }
815 // close direct output if currently open and configured with different parameters
816 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700817 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700818 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700819 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700820 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700821 outputDesc->mLatency = 0;
822 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700823 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
824 config.sample_rate = samplingRate;
825 config.channel_mask = channelMask;
826 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700827 if (offloadInfo != NULL) {
828 config.offload_info = *offloadInfo;
829 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700830 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700831 &output,
832 &config,
833 &outputDesc->mDevice,
834 String8(""),
835 &outputDesc->mLatency,
836 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700837
838 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700839 if (status != NO_ERROR ||
840 (samplingRate != 0 && samplingRate != config.sample_rate) ||
841 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
842 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700843 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
844 "format %d %d, channelMask %04x %04x", output, samplingRate,
845 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
846 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700847 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700848 mpClientInterface->closeOutput(output);
849 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800850 // fall back to mixer output if possible when the direct output could not be open
851 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
852 goto non_direct_output;
853 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800854 // fall back to mixer output if possible when the direct output could not be open
855 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
856 goto non_direct_output;
857 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700858 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700859 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700860 outputDesc->mSamplingRate = config.sample_rate;
861 outputDesc->mChannelMask = config.channel_mask;
862 outputDesc->mFormat = config.format;
863 outputDesc->mRefCount[stream] = 0;
864 outputDesc->mStopTime[stream] = 0;
865 outputDesc->mDirectOpenCount = 1;
866
Eric Laurente552edb2014-03-10 17:42:56 -0700867 audio_io_handle_t srcOutput = getOutputForEffect();
868 addOutput(output, outputDesc);
869 audio_io_handle_t dstOutput = getOutputForEffect();
870 if (dstOutput == output) {
871 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
872 }
873 mPreviousOutputs = mOutputs;
874 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700875 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700876 return output;
877 }
878
Eric Laurentb732cf52014-09-24 19:08:21 -0700879non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700880 // ignoring channel mask due to downmix capability in mixer
881
882 // open a non direct output
883
884 // for non direct outputs, only PCM is supported
885 if (audio_is_linear_pcm(format)) {
886 // get which output is suitable for the specified stream. The actual
887 // routing change will happen when startOutput() will be called
888 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
889
Eric Laurent8838a382014-09-08 16:44:28 -0700890 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
891 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
892 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700893 }
894 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
895 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
896
Paul McLeanaa981192015-03-21 09:55:15 -0700897 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700898
899 return output;
900}
901
Eric Laurente0720872014-03-11 09:30:41 -0700902audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700903 audio_output_flags_t flags,
904 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700905{
906 // select one output among several that provide a path to a particular device or set of
907 // devices (the list was previously build by getOutputsForDevice()).
908 // The priority is as follows:
909 // 1: the output with the highest number of requested policy flags
910 // 2: the primary output
911 // 3: the first output in the list
912
913 if (outputs.size() == 0) {
914 return 0;
915 }
916 if (outputs.size() == 1) {
917 return outputs[0];
918 }
919
920 int maxCommonFlags = 0;
921 audio_io_handle_t outputFlags = 0;
922 audio_io_handle_t outputPrimary = 0;
923
924 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700925 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700926 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700927 // if a valid format is specified, skip output if not compatible
928 if (format != AUDIO_FORMAT_INVALID) {
929 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
930 if (format != outputDesc->mFormat) {
931 continue;
932 }
933 } else if (!audio_is_linear_pcm(format)) {
934 continue;
935 }
936 }
937
Eric Laurent3b73df72014-03-11 09:06:29 -0700938 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700939 if (commonFlags > maxCommonFlags) {
940 outputFlags = outputs[i];
941 maxCommonFlags = commonFlags;
942 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
943 }
944 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
945 outputPrimary = outputs[i];
946 }
947 }
948 }
949
950 if (outputFlags != 0) {
951 return outputFlags;
952 }
953 if (outputPrimary != 0) {
954 return outputPrimary;
955 }
956
957 return outputs[0];
958}
959
Eric Laurente0720872014-03-11 09:30:41 -0700960status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700961 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800962 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700963{
Paul McLeanaa981192015-03-21 09:55:15 -0700964 ALOGV("startOutput() output %d, stream %d, session %d",
965 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700966 ssize_t index = mOutputs.indexOfKey(output);
967 if (index < 0) {
968 ALOGW("startOutput() unknown output %d", output);
969 return BAD_VALUE;
970 }
971
Eric Laurentc75307b2015-03-17 15:29:32 -0700972 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
973
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700974 // Routing?
975 mOutputRoutes.incRouteActivity(session);
976
Eric Laurentc75307b2015-03-17 15:29:32 -0700977 audio_devices_t newDevice;
978 if (outputDesc->mPolicyMix != NULL) {
979 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -0700980 } else if (mOutputRoutes.hasRouteChanged(session)) {
981 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700982 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -0700983 } else {
984 newDevice = AUDIO_DEVICE_NONE;
985 }
986
987 uint32_t delayMs = 0;
988
Eric Laurentc75307b2015-03-17 15:29:32 -0700989 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
990
991 if (status != NO_ERROR) {
992 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700993 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -0700994 }
995 // Automatically enable the remote submix input when output is started on a re routing mix
996 // of type MIX_TYPE_RECORDERS
997 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
998 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
999 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1000 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1001 outputDesc->mPolicyMix->mRegistrationId,
1002 "remote-submix");
1003 }
1004
1005 if (delayMs != 0) {
1006 usleep(delayMs * 1000);
1007 }
1008
1009 return status;
1010}
1011
1012status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1013 audio_stream_type_t stream,
1014 audio_devices_t device,
1015 uint32_t *delayMs)
1016{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001017 // cannot start playback of STREAM_TTS if any other output is being used
1018 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001019
1020 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001021 if (stream == AUDIO_STREAM_TTS) {
1022 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001023 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001024 return INVALID_OPERATION;
1025 } else {
1026 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1027 }
1028 } else {
1029 // some playback other than beacon starts
1030 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1031 }
1032
Eric Laurente552edb2014-03-10 17:42:56 -07001033 // increment usage count for this stream on the requested output:
1034 // NOTE that the usage count is the same for duplicated output and hardware output which is
1035 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1036 outputDesc->changeRefCount(stream, 1);
1037
Eric Laurent493404d2015-04-21 15:07:36 -07001038 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001039 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001040 if (device == AUDIO_DEVICE_NONE) {
1041 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001042 }
Eric Laurente552edb2014-03-10 17:42:56 -07001043 routing_strategy strategy = getStrategy(stream);
1044 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001045 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1046 (beaconMuteLatency > 0);
1047 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001048 bool force = false;
1049 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001050 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001051 if (desc != outputDesc) {
1052 // force a device change if any other output is managed by the same hw
1053 // module and has a current device selection that differs from selected device.
1054 // In this case, the audio HAL must receive the new device selection so that it can
1055 // change the device currently selected by the other active output.
1056 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001057 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001058 force = true;
1059 }
1060 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001061 // a notification so that audio focus effect can propagate, or that a mute/unmute
1062 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001063 uint32_t latency = desc->latency();
1064 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1065 waitMs = latency;
1066 }
1067 }
1068 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001069 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001070
1071 // handle special case for sonification while in call
1072 if (isInCall()) {
1073 handleIncallSonification(stream, true, false);
1074 }
1075
1076 // apply volume rules for current stream and device if necessary
1077 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001078 mStreams.valueFor(stream).getVolumeIndex(device),
1079 outputDesc,
1080 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001081
1082 // update the outputs if starting an output with a stream that can affect notification
1083 // routing
1084 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001085
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001086 // force reevaluating accessibility routing when ringtone or alarm starts
1087 if (strategy == STRATEGY_SONIFICATION) {
1088 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1089 }
Eric Laurente552edb2014-03-10 17:42:56 -07001090 }
1091 return NO_ERROR;
1092}
1093
1094
Eric Laurente0720872014-03-11 09:30:41 -07001095status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001096 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001097 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001098{
1099 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1100 ssize_t index = mOutputs.indexOfKey(output);
1101 if (index < 0) {
1102 ALOGW("stopOutput() unknown output %d", output);
1103 return BAD_VALUE;
1104 }
1105
Eric Laurentc75307b2015-03-17 15:29:32 -07001106 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001107
Eric Laurentc75307b2015-03-17 15:29:32 -07001108 if (outputDesc->mRefCount[stream] == 1) {
1109 // Automatically disable the remote submix input when output is stopped on a
1110 // re routing mix of type MIX_TYPE_RECORDERS
1111 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1112 outputDesc->mPolicyMix != NULL &&
1113 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1114 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1115 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1116 outputDesc->mPolicyMix->mRegistrationId,
1117 "remote-submix");
1118 }
1119 }
1120
1121 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001122 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001123 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001124 int activityCount = mOutputRoutes.decRouteActivity(session);
1125 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1126
1127 if (forceDeviceUpdate) {
1128 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1129 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001130 }
1131
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001132 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001133}
1134
1135status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001136 audio_stream_type_t stream,
1137 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001138{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001139 // always handle stream stop, check which stream type is stopping
1140 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1141
Eric Laurente552edb2014-03-10 17:42:56 -07001142 // handle special case for sonification while in call
1143 if (isInCall()) {
1144 handleIncallSonification(stream, false, false);
1145 }
1146
1147 if (outputDesc->mRefCount[stream] > 0) {
1148 // decrement usage count of this stream on the output
1149 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001150
Eric Laurente552edb2014-03-10 17:42:56 -07001151 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001152 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001153 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001154 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001155 // delay the device switch by twice the latency because stopOutput() is executed when
1156 // the track stop() command is received and at that time the audio track buffer can
1157 // still contain data that needs to be drained. The latency only covers the audio HAL
1158 // and kernel buffers. Also the latency does not always include additional delay in the
1159 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001161
1162 // force restoring the device selection on other active outputs if it differs from the
1163 // one being selected for this output
1164 for (size_t i = 0; i < mOutputs.size(); i++) {
1165 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001166 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001167 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001168 desc->isActive() &&
1169 outputDesc->sharesHwModuleWith(desc) &&
1170 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001171 setOutputDevice(desc,
1172 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001173 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001174 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001175 }
1176 }
1177 // update the outputs if stopping one with a stream that can affect notification routing
1178 handleNotificationRoutingForStream(stream);
1179 }
1180 return NO_ERROR;
1181 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001182 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001183 return INVALID_OPERATION;
1184 }
1185}
1186
Eric Laurente83b55d2014-11-14 10:06:21 -08001187void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001188 audio_stream_type_t stream __unused,
1189 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001190{
1191 ALOGV("releaseOutput() %d", output);
1192 ssize_t index = mOutputs.indexOfKey(output);
1193 if (index < 0) {
1194 ALOGW("releaseOutput() releasing unknown output %d", output);
1195 return;
1196 }
1197
1198#ifdef AUDIO_POLICY_TEST
1199 int testIndex = testOutputIndex(output);
1200 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001201 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001202 if (outputDesc->isActive()) {
1203 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001204 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001205 mTestOutputs[testIndex] = 0;
1206 }
1207 return;
1208 }
1209#endif //AUDIO_POLICY_TEST
1210
Paul McLeanaa981192015-03-21 09:55:15 -07001211 // Routing
1212 mOutputRoutes.removeRoute(session);
1213
Eric Laurentc75307b2015-03-17 15:29:32 -07001214 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001215 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001216 if (desc->mDirectOpenCount <= 0) {
1217 ALOGW("releaseOutput() invalid open count %d for output %d",
1218 desc->mDirectOpenCount, output);
1219 return;
1220 }
1221 if (--desc->mDirectOpenCount == 0) {
1222 closeOutput(output);
1223 // If effects where present on the output, audioflinger moved them to the primary
1224 // output by default: move them back to the appropriate output.
1225 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001226 if (dstOutput != mPrimaryOutput->mIoHandle) {
1227 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1228 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001229 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001230 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001231 }
1232 }
1233}
1234
1235
Eric Laurentcaf7f482014-11-25 17:50:47 -08001236status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1237 audio_io_handle_t *input,
1238 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001239 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001240 uint32_t samplingRate,
1241 audio_format_t format,
1242 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001243 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001244 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001245 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001246{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001247 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1248 "session %d, flags %#x",
1249 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001250
Eric Laurentcaf7f482014-11-25 17:50:47 -08001251 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001252 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001253 audio_devices_t device;
1254 // handle legacy remote submix case where the address was not always specified
1255 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001256 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001257 audio_source_t inputSource = attr->source;
1258 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001259 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001260
Eric Laurentc447ded2015-01-06 08:47:05 -08001261 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1262 inputSource = AUDIO_SOURCE_MIC;
1263 }
1264 halInputSource = inputSource;
1265
Paul McLean466dc8e2015-04-17 13:15:36 -06001266 // Explicit routing?
1267 sp<DeviceDescriptor> deviceDesc;
1268 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1269 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1270 deviceDesc = mAvailableInputDevices[i];
1271 break;
1272 }
1273 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001274 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001275
Eric Laurentc447ded2015-01-06 08:47:05 -08001276 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001277 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001278 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001279 if (ret != NO_ERROR) {
1280 return ret;
1281 }
1282 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001283 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1284 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001285 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001286 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001287 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001288 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001289 return BAD_VALUE;
1290 }
Eric Laurentc722f302014-12-10 11:21:49 -08001291 if (policyMix != NULL) {
1292 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001293 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1294 // there is an external policy, but this input is attached to a mix of recorders,
1295 // meaning it receives audio injected into the framework, so the recorder doesn't
1296 // know about it and is therefore considered "legacy"
1297 *inputType = API_INPUT_LEGACY;
1298 } else {
1299 // recording a mix of players defined by an external policy, we're rerouting for
1300 // an external policy
1301 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1302 }
Eric Laurentc722f302014-12-10 11:21:49 -08001303 } else if (audio_is_remote_submix_device(device)) {
1304 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001305 *inputType = API_INPUT_MIX_CAPTURE;
1306 } else {
1307 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001308 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001309 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001310 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001311 case AUDIO_SOURCE_VOICE_UPLINK:
1312 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1313 break;
1314 case AUDIO_SOURCE_VOICE_DOWNLINK:
1315 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1316 break;
1317 case AUDIO_SOURCE_VOICE_CALL:
1318 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1319 break;
1320 default:
1321 break;
1322 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001323 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001324 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1325 if (index >= 0) {
1326 *input = mSoundTriggerSessions.valueFor(session);
1327 isSoundTrigger = true;
1328 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1329 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1330 } else {
1331 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1332 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001333 }
1334 }
1335
Andy Hungf129b032015-04-07 13:45:50 -07001336 // find a compatible input profile (not necessarily identical in parameters)
1337 sp<IOProfile> profile;
1338 // samplingRate and flags may be updated by getInputProfile
1339 uint32_t profileSamplingRate = samplingRate;
1340 audio_format_t profileFormat = format;
1341 audio_channel_mask_t profileChannelMask = channelMask;
1342 audio_input_flags_t profileFlags = flags;
1343 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001344 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001345 profileSamplingRate, profileFormat, profileChannelMask,
1346 profileFlags);
1347 if (profile != 0) {
1348 break; // success
1349 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1350 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1351 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001352 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1353 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001354 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001356 }
Eric Laurente552edb2014-03-10 17:42:56 -07001357 }
1358
Eric Laurent322b4d22015-04-03 15:57:54 -07001359 if (profile->getModuleHandle() == 0) {
1360 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001361 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001362 }
1363
1364 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001365 config.sample_rate = profileSamplingRate;
1366 config.channel_mask = profileChannelMask;
1367 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001368
Eric Laurent322b4d22015-04-03 15:57:54 -07001369 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001370 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001371 &config,
1372 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001373 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001374 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001375 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001376
1377 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001378 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001379 (profileSamplingRate != config.sample_rate) ||
1380 (profileFormat != config.format) ||
1381 (profileChannelMask != config.channel_mask)) {
1382 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1383 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001384 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001385 if (*input != AUDIO_IO_HANDLE_NONE) {
1386 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001387 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001388 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001389 }
1390
Eric Laurent1f2f2232014-06-02 12:01:23 -07001391 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001392 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001393 inputDesc->mRefCount = 0;
1394 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001395 inputDesc->mSamplingRate = profileSamplingRate;
1396 inputDesc->mFormat = profileFormat;
1397 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001398 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001399 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001400 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001401 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001402
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001403 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001404
Eric Laurentcaf7f482014-11-25 17:50:47 -08001405 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001406 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001407
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001409}
1410
Eric Laurent4dc68062014-07-28 17:26:49 -07001411status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1412 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001413{
1414 ALOGV("startInput() input %d", input);
1415 ssize_t index = mInputs.indexOfKey(input);
1416 if (index < 0) {
1417 ALOGW("startInput() unknown input %d", input);
1418 return BAD_VALUE;
1419 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001420 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001421
Eric Laurentc722f302014-12-10 11:21:49 -08001422 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001423 if (index < 0) {
1424 ALOGW("startInput() unknown session %d on input %d", session, input);
1425 return BAD_VALUE;
1426 }
1427
Glenn Kasten74a8e252014-07-24 14:09:55 -07001428 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001429 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001430
1431 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001432 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001433 if (activeInput != 0 && activeInput != input) {
1434
1435 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1436 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001437 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001438 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001439 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001440 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1441 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001442 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001443 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001444 return INVALID_OPERATION;
1445 }
1446 }
1447 }
1448
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001449 // Routing?
1450 mInputRoutes.incRouteActivity(session);
1451
Paul McLean466dc8e2015-04-17 13:15:36 -06001452 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001453 // if input maps to a dynamic policy with an activity listener, notify of state change
1454 if ((inputDesc->mPolicyMix != NULL)
1455 && ((inputDesc->mPolicyMix->mFlags & MIX_FLAG_NOTIFY_ACTIVITY) != 0)) {
1456 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1457 MIX_STATE_MIXING);
1458 }
1459
François Gaffie53615e22015-03-19 09:24:12 +01001460 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001461 SoundTrigger::setCaptureState(true);
1462 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001463 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001464
Eric Laurentc722f302014-12-10 11:21:49 -08001465 // automatically enable the remote submix output when input is started if not
1466 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001467 // For remote submix (a virtual device), we open only one input per capture request.
1468 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001469 String8 address = String8("");
1470 if (inputDesc->mPolicyMix == NULL) {
1471 address = String8("0");
1472 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1473 address = inputDesc->mPolicyMix->mRegistrationId;
1474 }
1475 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001476 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001477 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001478 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001479 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001480 }
Eric Laurente552edb2014-03-10 17:42:56 -07001481 }
1482
Eric Laurente552edb2014-03-10 17:42:56 -07001483 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1484
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001485 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001486 return NO_ERROR;
1487}
1488
Eric Laurent4dc68062014-07-28 17:26:49 -07001489status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1490 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001491{
1492 ALOGV("stopInput() input %d", input);
1493 ssize_t index = mInputs.indexOfKey(input);
1494 if (index < 0) {
1495 ALOGW("stopInput() unknown input %d", input);
1496 return BAD_VALUE;
1497 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001498 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001499
Eric Laurentc722f302014-12-10 11:21:49 -08001500 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001501 if (index < 0) {
1502 ALOGW("stopInput() unknown session %d on input %d", session, input);
1503 return BAD_VALUE;
1504 }
1505
Eric Laurente552edb2014-03-10 17:42:56 -07001506 if (inputDesc->mRefCount == 0) {
1507 ALOGW("stopInput() input %d already stopped", input);
1508 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001509 }
1510
1511 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001512
1513 // Routing?
1514 mInputRoutes.decRouteActivity(session);
1515
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001516 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001517 // if input maps to a dynamic policy with an activity listener, notify of state change
1518 if ((inputDesc->mPolicyMix != NULL)
1519 && ((inputDesc->mPolicyMix->mFlags & MIX_FLAG_NOTIFY_ACTIVITY) != 0)) {
1520 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1521 MIX_STATE_IDLE);
1522 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001523
Eric Laurentc722f302014-12-10 11:21:49 -08001524 // automatically disable the remote submix output when input is stopped if not
1525 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001526 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001527 String8 address = String8("");
1528 if (inputDesc->mPolicyMix == NULL) {
1529 address = String8("0");
1530 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1531 address = inputDesc->mPolicyMix->mRegistrationId;
1532 }
1533 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001534 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001535 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001536 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001537 }
Eric Laurente552edb2014-03-10 17:42:56 -07001538 }
1539
Eric Laurent1c333e22014-05-20 10:48:17 -07001540 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001541
François Gaffie53615e22015-03-19 09:24:12 +01001542 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001543 SoundTrigger::setCaptureState(false);
1544 }
Eric Laurente552edb2014-03-10 17:42:56 -07001545 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001546 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001547}
1548
Eric Laurent4dc68062014-07-28 17:26:49 -07001549void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1550 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001551{
1552 ALOGV("releaseInput() %d", input);
1553 ssize_t index = mInputs.indexOfKey(input);
1554 if (index < 0) {
1555 ALOGW("releaseInput() releasing unknown input %d", input);
1556 return;
1557 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001558
1559 // Routing
1560 mInputRoutes.removeRoute(session);
1561
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001562 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1563 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001564
Eric Laurentc722f302014-12-10 11:21:49 -08001565 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001566 if (index < 0) {
1567 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1568 return;
1569 }
Eric Laurentc722f302014-12-10 11:21:49 -08001570 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001571 if (inputDesc->mOpenRefCount == 0) {
1572 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1573 return;
1574 }
1575 inputDesc->mOpenRefCount--;
1576 if (inputDesc->mOpenRefCount > 0) {
1577 ALOGV("releaseInput() exit > 0");
1578 return;
1579 }
1580
Eric Laurent05b90f82014-08-27 15:32:29 -07001581 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001582 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001583 ALOGV("releaseInput() exit");
1584}
1585
Eric Laurentd4692962014-05-05 18:13:44 -07001586void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001587 bool patchRemoved = false;
1588
Eric Laurentd4692962014-05-05 18:13:44 -07001589 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001590 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1591 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1592 if (patch_index >= 0) {
1593 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1594 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1595 mAudioPatches.removeItemsAt(patch_index);
1596 patchRemoved = true;
1597 }
Eric Laurentd4692962014-05-05 18:13:44 -07001598 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1599 }
1600 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001601 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001602
1603 if (patchRemoved) {
1604 mpClientInterface->onAudioPatchListUpdate();
1605 }
Eric Laurentd4692962014-05-05 18:13:44 -07001606}
1607
Eric Laurente0720872014-03-11 09:30:41 -07001608void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001609 int indexMin,
1610 int indexMax)
1611{
1612 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001613 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001614 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1615 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001616 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001617 }
Eric Laurente552edb2014-03-10 17:42:56 -07001618}
1619
Eric Laurente0720872014-03-11 09:30:41 -07001620status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001621 int index,
1622 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001623{
1624
Eric Laurentc75307b2015-03-17 15:29:32 -07001625 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1626 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001627 return BAD_VALUE;
1628 }
1629 if (!audio_is_output_device(device)) {
1630 return BAD_VALUE;
1631 }
1632
1633 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001634 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001635
1636 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1637 stream, device, index);
1638
1639 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1640 // clear all device specific values
1641 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001642 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001643 }
François Gaffiedfd74092015-03-19 12:10:59 +01001644 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001645
Eric Laurent31551f82014-10-10 18:21:56 -07001646 // update volume on all outputs whose current device is also selected by the same
1647 // strategy as the device specified by the caller
1648 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001649
1650
1651 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1652 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1653 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001654 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001655 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1656 }
1657 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1658 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001659 return NO_ERROR;
1660 }
Eric Laurente552edb2014-03-10 17:42:56 -07001661 status_t status = NO_ERROR;
1662 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001663 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1664 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001665 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001666 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001667 if (volStatus != NO_ERROR) {
1668 status = volStatus;
1669 }
1670 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001671 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1672 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001673 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001674 }
Eric Laurente552edb2014-03-10 17:42:56 -07001675 }
1676 return status;
1677}
1678
Eric Laurente0720872014-03-11 09:30:41 -07001679status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001680 int *index,
1681 audio_devices_t device)
1682{
1683 if (index == NULL) {
1684 return BAD_VALUE;
1685 }
1686 if (!audio_is_output_device(device)) {
1687 return BAD_VALUE;
1688 }
1689 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1690 // the strategy the stream belongs to.
1691 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1692 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1693 }
François Gaffiedfd74092015-03-19 12:10:59 +01001694 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001695
Eric Laurentc75307b2015-03-17 15:29:32 -07001696 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001697 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1698 return NO_ERROR;
1699}
1700
Eric Laurente0720872014-03-11 09:30:41 -07001701audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001702 const SortedVector<audio_io_handle_t>& outputs)
1703{
1704 // select one output among several suitable for global effects.
1705 // The priority is as follows:
1706 // 1: An offloaded output. If the effect ends up not being offloadable,
1707 // AudioFlinger will invalidate the track and the offloaded output
1708 // will be closed causing the effect to be moved to a PCM output.
1709 // 2: A deep buffer output
1710 // 3: the first output in the list
1711
1712 if (outputs.size() == 0) {
1713 return 0;
1714 }
1715
1716 audio_io_handle_t outputOffloaded = 0;
1717 audio_io_handle_t outputDeepBuffer = 0;
1718
1719 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001720 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001721 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001722 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1723 outputOffloaded = outputs[i];
1724 }
1725 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1726 outputDeepBuffer = outputs[i];
1727 }
1728 }
1729
1730 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1731 outputOffloaded, outputDeepBuffer);
1732 if (outputOffloaded != 0) {
1733 return outputOffloaded;
1734 }
1735 if (outputDeepBuffer != 0) {
1736 return outputDeepBuffer;
1737 }
1738
1739 return outputs[0];
1740}
1741
Eric Laurente0720872014-03-11 09:30:41 -07001742audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001743{
1744 // apply simple rule where global effects are attached to the same output as MUSIC streams
1745
Eric Laurent3b73df72014-03-11 09:06:29 -07001746 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001747 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1748 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1749
1750 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1751 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1752 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1753
1754 return output;
1755}
1756
Eric Laurente0720872014-03-11 09:30:41 -07001757status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001758 audio_io_handle_t io,
1759 uint32_t strategy,
1760 int session,
1761 int id)
1762{
1763 ssize_t index = mOutputs.indexOfKey(io);
1764 if (index < 0) {
1765 index = mInputs.indexOfKey(io);
1766 if (index < 0) {
1767 ALOGW("registerEffect() unknown io %d", io);
1768 return INVALID_OPERATION;
1769 }
1770 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001771 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001772}
1773
Eric Laurentc75307b2015-03-17 15:29:32 -07001774bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1775{
1776 return mOutputs.isStreamActive(stream, inPastMs);
1777}
1778
1779bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1780{
1781 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1782}
1783
Eric Laurente0720872014-03-11 09:30:41 -07001784bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001785{
1786 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001787 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001788 if (inputDescriptor->mRefCount == 0) {
1789 continue;
1790 }
1791 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001792 return true;
1793 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001794 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1795 // corresponds to an active capture triggered by a hardware hotword recognition
1796 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1797 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1798 // FIXME: we should not assume that the first session is the active one and keep
1799 // activity count per session. Same in startInput().
1800 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1801 if (index >= 0) {
1802 return true;
1803 }
1804 }
Eric Laurente552edb2014-03-10 17:42:56 -07001805 }
1806 return false;
1807}
1808
Eric Laurent275e8e92014-11-30 15:14:47 -08001809// Register a list of custom mixes with their attributes and format.
1810// When a mix is registered, corresponding input and output profiles are
1811// added to the remote submix hw module. The profile contains only the
1812// parameters (sampling rate, format...) specified by the mix.
1813// The corresponding input remote submix device is also connected.
1814//
1815// When a remote submix device is connected, the address is checked to select the
1816// appropriate profile and the corresponding input or output stream is opened.
1817//
1818// When capture starts, getInputForAttr() will:
1819// - 1 look for a mix matching the address passed in attribtutes tags if any
1820// - 2 if none found, getDeviceForInputSource() will:
1821// - 2.1 look for a mix matching the attributes source
1822// - 2.2 if none found, default to device selection by policy rules
1823// At this time, the corresponding output remote submix device is also connected
1824// and active playback use cases can be transferred to this mix if needed when reconnecting
1825// after AudioTracks are invalidated
1826//
1827// When playback starts, getOutputForAttr() will:
1828// - 1 look for a mix matching the address passed in attribtutes tags if any
1829// - 2 if none found, look for a mix matching the attributes usage
1830// - 3 if none found, default to device and output selection by policy rules.
1831
1832status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1833{
1834 sp<HwModule> module;
1835 for (size_t i = 0; i < mHwModules.size(); i++) {
1836 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1837 mHwModules[i]->mHandle != 0) {
1838 module = mHwModules[i];
1839 break;
1840 }
1841 }
1842
1843 if (module == 0) {
1844 return INVALID_OPERATION;
1845 }
1846
1847 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1848
1849 for (size_t i = 0; i < mixes.size(); i++) {
1850 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001851
1852 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001853 continue;
1854 }
1855 audio_config_t outputConfig = mixes[i].mFormat;
1856 audio_config_t inputConfig = mixes[i].mFormat;
1857 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1858 // stereo and let audio flinger do the channel conversion if needed.
1859 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1860 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1861 module->addOutputProfile(address, &outputConfig,
1862 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1863 module->addInputProfile(address, &inputConfig,
1864 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001865
Eric Laurentc722f302014-12-10 11:21:49 -08001866 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001867 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001868 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001869 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001870 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001871 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001872 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001873 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001874 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001875 }
1876 return NO_ERROR;
1877}
1878
1879status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1880{
1881 sp<HwModule> module;
1882 for (size_t i = 0; i < mHwModules.size(); i++) {
1883 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1884 mHwModules[i]->mHandle != 0) {
1885 module = mHwModules[i];
1886 break;
1887 }
1888 }
1889
1890 if (module == 0) {
1891 return INVALID_OPERATION;
1892 }
1893
1894 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1895
1896 for (size_t i = 0; i < mixes.size(); i++) {
1897 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001898
1899 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001900 continue;
1901 }
1902
Eric Laurentc722f302014-12-10 11:21:49 -08001903 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1904 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1905 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001906 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001907 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001908 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001909 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001910
1911 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1912 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1913 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001914 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001915 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001916 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001917 }
1918 module->removeOutputProfile(address);
1919 module->removeInputProfile(address);
1920 }
1921 return NO_ERROR;
1922}
1923
Eric Laurente552edb2014-03-10 17:42:56 -07001924
Eric Laurente0720872014-03-11 09:30:41 -07001925status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001926{
1927 const size_t SIZE = 256;
1928 char buffer[SIZE];
1929 String8 result;
1930
1931 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1932 result.append(buffer);
1933
Eric Laurentc75307b2015-03-17 15:29:32 -07001934 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001935 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001936 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001937 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001938 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001939 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001940 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001941 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001942 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001943 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001944 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001945 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001946 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001947 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001948 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001949 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001950 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001951 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001952 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001953
François Gaffie53615e22015-03-19 09:24:12 +01001954 mAvailableOutputDevices.dump(fd, String8("output"));
1955 mAvailableInputDevices.dump(fd, String8("input"));
1956 mHwModules.dump(fd);
1957 mOutputs.dump(fd);
1958 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001959 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001960 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001961 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001962
1963 return NO_ERROR;
1964}
1965
1966// This function checks for the parameters which can be offloaded.
1967// This can be enhanced depending on the capability of the DSP and policy
1968// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001969bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001970{
1971 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001972 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001973 offloadInfo.sample_rate, offloadInfo.channel_mask,
1974 offloadInfo.format,
1975 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1976 offloadInfo.has_video);
1977
1978 // Check if offload has been disabled
1979 char propValue[PROPERTY_VALUE_MAX];
1980 if (property_get("audio.offload.disable", propValue, "0")) {
1981 if (atoi(propValue) != 0) {
1982 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1983 return false;
1984 }
1985 }
1986
1987 // Check if stream type is music, then only allow offload as of now.
1988 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1989 {
1990 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1991 return false;
1992 }
1993
1994 //TODO: enable audio offloading with video when ready
1995 if (offloadInfo.has_video)
1996 {
1997 ALOGV("isOffloadSupported: has_video == true, returning false");
1998 return false;
1999 }
2000
2001 //If duration is less than minimum value defined in property, return false
2002 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2003 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2004 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2005 return false;
2006 }
2007 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2008 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2009 return false;
2010 }
2011
2012 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2013 // creating an offloaded track and tearing it down immediately after start when audioflinger
2014 // detects there is an active non offloadable effect.
2015 // FIXME: We should check the audio session here but we do not have it in this context.
2016 // This may prevent offloading in rare situations where effects are left active by apps
2017 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002018 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002019 return false;
2020 }
2021
2022 // See if there is a profile to support this.
2023 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002024 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002025 offloadInfo.sample_rate,
2026 offloadInfo.format,
2027 offloadInfo.channel_mask,
2028 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002029 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2030 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002031}
2032
Eric Laurent6a94d692014-05-20 11:18:06 -07002033status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2034 audio_port_type_t type,
2035 unsigned int *num_ports,
2036 struct audio_port *ports,
2037 unsigned int *generation)
2038{
2039 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2040 generation == NULL) {
2041 return BAD_VALUE;
2042 }
2043 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2044 if (ports == NULL) {
2045 *num_ports = 0;
2046 }
2047
2048 size_t portsWritten = 0;
2049 size_t portsMax = *num_ports;
2050 *num_ports = 0;
2051 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2052 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2053 for (size_t i = 0;
2054 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2055 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2056 }
2057 *num_ports += mAvailableOutputDevices.size();
2058 }
2059 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2060 for (size_t i = 0;
2061 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2062 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2063 }
2064 *num_ports += mAvailableInputDevices.size();
2065 }
2066 }
2067 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2068 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2069 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2070 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2071 }
2072 *num_ports += mInputs.size();
2073 }
2074 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002075 size_t numOutputs = 0;
2076 for (size_t i = 0; i < mOutputs.size(); i++) {
2077 if (!mOutputs[i]->isDuplicated()) {
2078 numOutputs++;
2079 if (portsWritten < portsMax) {
2080 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2081 }
2082 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002083 }
Eric Laurent84c70242014-06-23 08:46:27 -07002084 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002085 }
2086 }
2087 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002088 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002089 return NO_ERROR;
2090}
2091
2092status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2093{
2094 return NO_ERROR;
2095}
2096
Eric Laurent6a94d692014-05-20 11:18:06 -07002097status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2098 audio_patch_handle_t *handle,
2099 uid_t uid)
2100{
2101 ALOGV("createAudioPatch()");
2102
2103 if (handle == NULL || patch == NULL) {
2104 return BAD_VALUE;
2105 }
2106 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2107
Eric Laurent874c42872014-08-08 15:13:39 -07002108 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2109 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2110 return BAD_VALUE;
2111 }
2112 // only one source per audio patch supported for now
2113 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002114 return INVALID_OPERATION;
2115 }
Eric Laurent874c42872014-08-08 15:13:39 -07002116
2117 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002118 return INVALID_OPERATION;
2119 }
Eric Laurent874c42872014-08-08 15:13:39 -07002120 for (size_t i = 0; i < patch->num_sinks; i++) {
2121 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2122 return INVALID_OPERATION;
2123 }
2124 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002125
2126 sp<AudioPatch> patchDesc;
2127 ssize_t index = mAudioPatches.indexOfKey(*handle);
2128
Eric Laurent6a94d692014-05-20 11:18:06 -07002129 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2130 patch->sources[0].role,
2131 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002132#if LOG_NDEBUG == 0
2133 for (size_t i = 0; i < patch->num_sinks; i++) {
2134 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2135 patch->sinks[i].role,
2136 patch->sinks[i].type);
2137 }
2138#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002139
2140 if (index >= 0) {
2141 patchDesc = mAudioPatches.valueAt(index);
2142 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2143 mUidCached, patchDesc->mUid, uid);
2144 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2145 return INVALID_OPERATION;
2146 }
2147 } else {
2148 *handle = 0;
2149 }
2150
2151 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002152 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002153 if (outputDesc == NULL) {
2154 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2155 return BAD_VALUE;
2156 }
Eric Laurent84c70242014-06-23 08:46:27 -07002157 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2158 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002159 if (patchDesc != 0) {
2160 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2161 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2162 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2163 return BAD_VALUE;
2164 }
2165 }
Eric Laurent874c42872014-08-08 15:13:39 -07002166 DeviceVector devices;
2167 for (size_t i = 0; i < patch->num_sinks; i++) {
2168 // Only support mix to devices connection
2169 // TODO add support for mix to mix connection
2170 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2171 ALOGV("createAudioPatch() source mix but sink is not a device");
2172 return INVALID_OPERATION;
2173 }
2174 sp<DeviceDescriptor> devDesc =
2175 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2176 if (devDesc == 0) {
2177 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2178 return BAD_VALUE;
2179 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002180
François Gaffie53615e22015-03-19 09:24:12 +01002181 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002182 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002183 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002184 NULL, // updatedSamplingRate
2185 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002186 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002187 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002188 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002189 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002190 ALOGV("createAudioPatch() profile not supported for device %08x",
2191 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002192 return INVALID_OPERATION;
2193 }
2194 devices.add(devDesc);
2195 }
2196 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002197 return INVALID_OPERATION;
2198 }
Eric Laurent874c42872014-08-08 15:13:39 -07002199
Eric Laurent6a94d692014-05-20 11:18:06 -07002200 // TODO: reconfigure output format and channels here
2201 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002202 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002203 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002204 index = mAudioPatches.indexOfKey(*handle);
2205 if (index >= 0) {
2206 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2207 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2208 }
2209 patchDesc = mAudioPatches.valueAt(index);
2210 patchDesc->mUid = uid;
2211 ALOGV("createAudioPatch() success");
2212 } else {
2213 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2214 return INVALID_OPERATION;
2215 }
2216 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2217 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2218 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002219 // only one sink supported when connecting an input device to a mix
2220 if (patch->num_sinks > 1) {
2221 return INVALID_OPERATION;
2222 }
François Gaffie53615e22015-03-19 09:24:12 +01002223 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002224 if (inputDesc == NULL) {
2225 return BAD_VALUE;
2226 }
2227 if (patchDesc != 0) {
2228 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2229 return BAD_VALUE;
2230 }
2231 }
2232 sp<DeviceDescriptor> devDesc =
2233 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2234 if (devDesc == 0) {
2235 return BAD_VALUE;
2236 }
2237
François Gaffie53615e22015-03-19 09:24:12 +01002238 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002239 devDesc->mAddress,
2240 patch->sinks[0].sample_rate,
2241 NULL, /*updatedSampleRate*/
2242 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002243 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002244 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002245 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002246 // FIXME for the parameter type,
2247 // and the NONE
2248 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002249 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002250 return INVALID_OPERATION;
2251 }
2252 // TODO: reconfigure output format and channels here
2253 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002254 devDesc->type(), inputDesc->mIoHandle);
2255 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002256 index = mAudioPatches.indexOfKey(*handle);
2257 if (index >= 0) {
2258 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2259 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2260 }
2261 patchDesc = mAudioPatches.valueAt(index);
2262 patchDesc->mUid = uid;
2263 ALOGV("createAudioPatch() success");
2264 } else {
2265 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2266 return INVALID_OPERATION;
2267 }
2268 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2269 // device to device connection
2270 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002271 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002272 return BAD_VALUE;
2273 }
2274 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002275 sp<DeviceDescriptor> srcDeviceDesc =
2276 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002277 if (srcDeviceDesc == 0) {
2278 return BAD_VALUE;
2279 }
Eric Laurent874c42872014-08-08 15:13:39 -07002280
Eric Laurent6a94d692014-05-20 11:18:06 -07002281 //update source and sink with our own data as the data passed in the patch may
2282 // be incomplete.
2283 struct audio_patch newPatch = *patch;
2284 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002285
Eric Laurent874c42872014-08-08 15:13:39 -07002286 for (size_t i = 0; i < patch->num_sinks; i++) {
2287 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2288 ALOGV("createAudioPatch() source device but one sink is not a device");
2289 return INVALID_OPERATION;
2290 }
2291
2292 sp<DeviceDescriptor> sinkDeviceDesc =
2293 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2294 if (sinkDeviceDesc == 0) {
2295 return BAD_VALUE;
2296 }
2297 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2298
Eric Laurent3bcf8592015-04-03 12:13:24 -07002299 // create a software bridge in PatchPanel if:
2300 // - source and sink devices are on differnt HW modules OR
2301 // - audio HAL version is < 3.0
2302 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2303 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2304 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002305 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002306 return INVALID_OPERATION;
2307 }
Eric Laurent874c42872014-08-08 15:13:39 -07002308 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002309 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002310 // if the sink device is reachable via an opened output stream, request to go via
2311 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002312 audio_io_handle_t output = selectOutput(outputs,
2313 AUDIO_OUTPUT_FLAG_NONE,
2314 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002315 if (output != AUDIO_IO_HANDLE_NONE) {
2316 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2317 if (outputDesc->isDuplicated()) {
2318 return INVALID_OPERATION;
2319 }
2320 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002321 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002322 newPatch.num_sources = 2;
2323 }
Eric Laurent83b88082014-06-20 18:31:16 -07002324 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002325 }
2326 // TODO: check from routing capabilities in config file and other conflicting patches
2327
2328 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2329 if (index >= 0) {
2330 afPatchHandle = patchDesc->mAfPatchHandle;
2331 }
2332
2333 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2334 &afPatchHandle,
2335 0);
2336 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2337 status, afPatchHandle);
2338 if (status == NO_ERROR) {
2339 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002340 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002341 addAudioPatch(patchDesc->mHandle, patchDesc);
2342 } else {
2343 patchDesc->mPatch = newPatch;
2344 }
2345 patchDesc->mAfPatchHandle = afPatchHandle;
2346 *handle = patchDesc->mHandle;
2347 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002348 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002349 } else {
2350 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2351 status);
2352 return INVALID_OPERATION;
2353 }
2354 } else {
2355 return BAD_VALUE;
2356 }
2357 } else {
2358 return BAD_VALUE;
2359 }
2360 return NO_ERROR;
2361}
2362
2363status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2364 uid_t uid)
2365{
2366 ALOGV("releaseAudioPatch() patch %d", handle);
2367
2368 ssize_t index = mAudioPatches.indexOfKey(handle);
2369
2370 if (index < 0) {
2371 return BAD_VALUE;
2372 }
2373 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2374 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2375 mUidCached, patchDesc->mUid, uid);
2376 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2377 return INVALID_OPERATION;
2378 }
2379
2380 struct audio_patch *patch = &patchDesc->mPatch;
2381 patchDesc->mUid = mUidCached;
2382 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002383 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002384 if (outputDesc == NULL) {
2385 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2386 return BAD_VALUE;
2387 }
2388
Eric Laurentc75307b2015-03-17 15:29:32 -07002389 setOutputDevice(outputDesc,
2390 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002391 true,
2392 0,
2393 NULL);
2394 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2395 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002396 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002397 if (inputDesc == NULL) {
2398 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2399 return BAD_VALUE;
2400 }
2401 setInputDevice(inputDesc->mIoHandle,
2402 getNewInputDevice(inputDesc->mIoHandle),
2403 true,
2404 NULL);
2405 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2406 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2407 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2408 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2409 status, patchDesc->mAfPatchHandle);
2410 removeAudioPatch(patchDesc->mHandle);
2411 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002412 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002413 } else {
2414 return BAD_VALUE;
2415 }
2416 } else {
2417 return BAD_VALUE;
2418 }
2419 return NO_ERROR;
2420}
2421
2422status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2423 struct audio_patch *patches,
2424 unsigned int *generation)
2425{
François Gaffie53615e22015-03-19 09:24:12 +01002426 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002427 return BAD_VALUE;
2428 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002429 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002430 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002431}
2432
Eric Laurente1715a42014-05-20 11:30:42 -07002433status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002434{
Eric Laurente1715a42014-05-20 11:30:42 -07002435 ALOGV("setAudioPortConfig()");
2436
2437 if (config == NULL) {
2438 return BAD_VALUE;
2439 }
2440 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2441 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002442 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2443 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002444 }
2445
Eric Laurenta121f902014-06-03 13:32:54 -07002446 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002447 if (config->type == AUDIO_PORT_TYPE_MIX) {
2448 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002449 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002450 if (outputDesc == NULL) {
2451 return BAD_VALUE;
2452 }
Eric Laurent84c70242014-06-23 08:46:27 -07002453 ALOG_ASSERT(!outputDesc->isDuplicated(),
2454 "setAudioPortConfig() called on duplicated output %d",
2455 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002456 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002457 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002458 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002459 if (inputDesc == NULL) {
2460 return BAD_VALUE;
2461 }
Eric Laurenta121f902014-06-03 13:32:54 -07002462 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002463 } else {
2464 return BAD_VALUE;
2465 }
2466 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2467 sp<DeviceDescriptor> deviceDesc;
2468 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2469 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2470 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2471 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2472 } else {
2473 return BAD_VALUE;
2474 }
2475 if (deviceDesc == NULL) {
2476 return BAD_VALUE;
2477 }
Eric Laurenta121f902014-06-03 13:32:54 -07002478 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002479 } else {
2480 return BAD_VALUE;
2481 }
2482
Eric Laurenta121f902014-06-03 13:32:54 -07002483 struct audio_port_config backupConfig;
2484 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2485 if (status == NO_ERROR) {
2486 struct audio_port_config newConfig;
2487 audioPortConfig->toAudioPortConfig(&newConfig, config);
2488 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002489 }
Eric Laurenta121f902014-06-03 13:32:54 -07002490 if (status != NO_ERROR) {
2491 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002492 }
Eric Laurente1715a42014-05-20 11:30:42 -07002493
2494 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002495}
2496
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002497void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2498{
2499 clearAudioPatches(uid);
2500 clearSessionRoutes(uid);
2501}
2502
Eric Laurent6a94d692014-05-20 11:18:06 -07002503void AudioPolicyManager::clearAudioPatches(uid_t uid)
2504{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002505 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002506 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2507 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002508 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002509 }
2510 }
2511}
2512
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002513
2514void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2515 audio_io_handle_t ouptutToSkip)
2516{
2517 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2518 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2519 for (size_t j = 0; j < mOutputs.size(); j++) {
2520 if (mOutputs.keyAt(j) == ouptutToSkip) {
2521 continue;
2522 }
2523 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2524 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2525 continue;
2526 }
2527 // If the default device for this strategy is on another output mix,
2528 // invalidate all tracks in this strategy to force re connection.
2529 // Otherwise select new device on the output mix.
2530 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2531 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2532 if (stream == AUDIO_STREAM_PATCH) {
2533 continue;
2534 }
2535 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2536 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2537 }
2538 }
2539 } else {
2540 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2541 setOutputDevice(outputDesc, newDevice, false);
2542 }
2543 }
2544}
2545
2546void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2547{
2548 // remove output routes associated with this uid
2549 SortedVector<routing_strategy> affectedStrategies;
2550 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2551 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2552 if (route->mUid == uid) {
2553 mOutputRoutes.removeItemsAt(i);
2554 if (route->mDeviceDescriptor != 0) {
2555 affectedStrategies.add(getStrategy(route->mStreamType));
2556 }
2557 }
2558 }
2559 // reroute outputs if necessary
2560 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2561 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2562 }
2563
2564 // remove input routes associated with this uid
2565 SortedVector<audio_source_t> affectedSources;
2566 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2567 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2568 if (route->mUid == uid) {
2569 mInputRoutes.removeItemsAt(i);
2570 if (route->mDeviceDescriptor != 0) {
2571 affectedSources.add(route->mSource);
2572 }
2573 }
2574 }
2575 // reroute inputs if necessary
2576 SortedVector<audio_io_handle_t> inputsToClose;
2577 for (size_t i = 0; i < mInputs.size(); i++) {
2578 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2579 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2580 inputsToClose.add(inputDesc->mIoHandle);
2581 }
2582 }
2583 for (size_t i = 0; i < inputsToClose.size(); i++) {
2584 closeInput(inputsToClose[i]);
2585 }
2586}
2587
2588
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002589status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2590 audio_io_handle_t *ioHandle,
2591 audio_devices_t *device)
2592{
2593 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2594 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002595 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002596
François Gaffiedf372692015-03-19 10:43:27 +01002597 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002598}
2599
Eric Laurent493404d2015-04-21 15:07:36 -07002600status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2601 const audio_attributes_t *attributes __unused,
2602 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002603{
2604 return INVALID_OPERATION;
2605}
2606
Eric Laurent493404d2015-04-21 15:07:36 -07002607status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002608{
2609 return INVALID_OPERATION;
2610}
2611
Eric Laurente552edb2014-03-10 17:42:56 -07002612// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002613// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002614// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002615uint32_t AudioPolicyManager::nextAudioPortGeneration()
2616{
2617 return android_atomic_inc(&mAudioPortGeneration);
2618}
2619
Eric Laurente0720872014-03-11 09:30:41 -07002620AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002621 :
2622#ifdef AUDIO_POLICY_TEST
2623 Thread(false),
2624#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002625 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002626 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002627 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002628 mAudioPortGeneration(1),
2629 mBeaconMuteRefCount(0),
2630 mBeaconPlayingRefCount(0),
2631 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002632{
François Gaffie2110e042015-03-24 08:41:51 +01002633 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2634 if (!engineInstance) {
2635 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2636 return;
2637 }
2638 // Retrieve the Policy Manager Interface
2639 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2640 if (mEngine == NULL) {
2641 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2642 return;
2643 }
2644 mEngine->setObserver(this);
2645 status_t status = mEngine->initCheck();
2646 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2647
Eric Laurent6a94d692014-05-20 11:18:06 -07002648 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002649 mpClientInterface = clientInterface;
2650
Paul McLeane743a472015-01-28 11:07:31 -08002651 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002652 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2653 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2654 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2655 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2656 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2657 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002658 ALOGE("could not load audio policy configuration file, setting defaults");
2659 defaultAudioPolicyConfig();
2660 }
2661 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002662 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002663
François Gaffie2110e042015-03-24 08:41:51 +01002664 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2665 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002666
2667 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002668 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2669 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002670 for (size_t i = 0; i < mHwModules.size(); i++) {
2671 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2672 if (mHwModules[i]->mHandle == 0) {
2673 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2674 continue;
2675 }
2676 // open all output streams needed to access attached devices
2677 // except for direct output streams that are only opened when they are actually
2678 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002679 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002680 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2681 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002682 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002683
Eric Laurent3a4311c2014-03-17 12:00:47 -07002684 if (outProfile->mSupportedDevices.isEmpty()) {
2685 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2686 continue;
2687 }
2688
Eric Laurentd78f1532014-09-16 16:38:20 -07002689 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2690 continue;
2691 }
Eric Laurent83b88082014-06-20 18:31:16 -07002692 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002693 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2694 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002695 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002696 // chose first device present in mSupportedDevices also part of
2697 // outputDeviceTypes
2698 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002699 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002700 if ((profileType & outputDeviceTypes) != 0) {
2701 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002702 }
Eric Laurente552edb2014-03-10 17:42:56 -07002703 }
2704 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002705 if ((profileType & outputDeviceTypes) == 0) {
2706 continue;
2707 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002708 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2709 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002710
2711 outputDesc->mDevice = profileType;
2712 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2713 config.sample_rate = outputDesc->mSamplingRate;
2714 config.channel_mask = outputDesc->mChannelMask;
2715 config.format = outputDesc->mFormat;
2716 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002717 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002718 &output,
2719 &config,
2720 &outputDesc->mDevice,
2721 String8(""),
2722 &outputDesc->mLatency,
2723 outputDesc->mFlags);
2724
2725 if (status != NO_ERROR) {
2726 ALOGW("Cannot open output stream for device %08x on hw module %s",
2727 outputDesc->mDevice,
2728 mHwModules[i]->mName);
2729 } else {
2730 outputDesc->mSamplingRate = config.sample_rate;
2731 outputDesc->mChannelMask = config.channel_mask;
2732 outputDesc->mFormat = config.format;
2733
2734 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002735 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002736 ssize_t index =
2737 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2738 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002739 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2740 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002741 }
2742 }
2743 if (mPrimaryOutput == 0 &&
2744 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002745 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002746 }
2747 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002748 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002749 outputDesc->mDevice,
2750 true);
2751 }
Eric Laurente552edb2014-03-10 17:42:56 -07002752 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002753 // open input streams needed to access attached devices to validate
2754 // mAvailableInputDevices list
2755 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2756 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002757 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002758
Eric Laurent3a4311c2014-03-17 12:00:47 -07002759 if (inProfile->mSupportedDevices.isEmpty()) {
2760 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2761 continue;
2762 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002763 // chose first device present in mSupportedDevices also part of
2764 // inputDeviceTypes
2765 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2766 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002767 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002768 if (profileType & inputDeviceTypes) {
2769 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002770 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002771 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002772 if ((profileType & inputDeviceTypes) == 0) {
2773 continue;
2774 }
2775 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2776
2777 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2778 inputDesc->mDevice = profileType;
2779
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002780 // find the address
2781 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2782 // the inputs vector must be of size 1, but we don't want to crash here
2783 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2784 : String8("");
2785 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2786 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2787
Eric Laurentd78f1532014-09-16 16:38:20 -07002788 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2789 config.sample_rate = inputDesc->mSamplingRate;
2790 config.channel_mask = inputDesc->mChannelMask;
2791 config.format = inputDesc->mFormat;
2792 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002793 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002794 &input,
2795 &config,
2796 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002797 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002798 AUDIO_SOURCE_MIC,
2799 AUDIO_INPUT_FLAG_NONE);
2800
2801 if (status == NO_ERROR) {
2802 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002803 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002804 ssize_t index =
2805 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2806 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002807 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2808 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002809 }
2810 }
2811 mpClientInterface->closeInput(input);
2812 } else {
2813 ALOGW("Cannot open input stream for device %08x on hw module %s",
2814 inputDesc->mDevice,
2815 mHwModules[i]->mName);
2816 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002817 }
2818 }
2819 // make sure all attached devices have been allocated a unique ID
2820 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002821 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002822 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002823 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2824 continue;
2825 }
François Gaffie2110e042015-03-24 08:41:51 +01002826 // The device is now validated and can be appended to the available devices of the engine
2827 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2828 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002829 i++;
2830 }
2831 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002832 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002833 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002834 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2835 continue;
2836 }
François Gaffie2110e042015-03-24 08:41:51 +01002837 // The device is now validated and can be appended to the available devices of the engine
2838 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2839 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002840 i++;
2841 }
2842 // make sure default device is reachable
2843 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002844 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002845 }
Eric Laurente552edb2014-03-10 17:42:56 -07002846
2847 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2848
2849 updateDevicesAndOutputs();
2850
2851#ifdef AUDIO_POLICY_TEST
2852 if (mPrimaryOutput != 0) {
2853 AudioParameter outputCmd = AudioParameter();
2854 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002855 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002856
2857 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2858 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002859 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2860 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002861 mTestLatencyMs = 0;
2862 mCurOutput = 0;
2863 mDirectOutput = false;
2864 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2865 mTestOutputs[i] = 0;
2866 }
2867
2868 const size_t SIZE = 256;
2869 char buffer[SIZE];
2870 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2871 run(buffer, ANDROID_PRIORITY_AUDIO);
2872 }
2873#endif //AUDIO_POLICY_TEST
2874}
2875
Eric Laurente0720872014-03-11 09:30:41 -07002876AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002877{
2878#ifdef AUDIO_POLICY_TEST
2879 exit();
2880#endif //AUDIO_POLICY_TEST
2881 for (size_t i = 0; i < mOutputs.size(); i++) {
2882 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002883 }
2884 for (size_t i = 0; i < mInputs.size(); i++) {
2885 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002886 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002887 mAvailableOutputDevices.clear();
2888 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002889 mOutputs.clear();
2890 mInputs.clear();
2891 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002892}
2893
Eric Laurente0720872014-03-11 09:30:41 -07002894status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002895{
2896 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2897}
2898
2899#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002900bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002901{
2902 ALOGV("entering threadLoop()");
2903 while (!exitPending())
2904 {
2905 String8 command;
2906 int valueInt;
2907 String8 value;
2908
2909 Mutex::Autolock _l(mLock);
2910 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2911
2912 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2913 AudioParameter param = AudioParameter(command);
2914
2915 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2916 valueInt != 0) {
2917 ALOGV("Test command %s received", command.string());
2918 String8 target;
2919 if (param.get(String8("target"), target) != NO_ERROR) {
2920 target = "Manager";
2921 }
2922 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2923 param.remove(String8("test_cmd_policy_output"));
2924 mCurOutput = valueInt;
2925 }
2926 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2927 param.remove(String8("test_cmd_policy_direct"));
2928 if (value == "false") {
2929 mDirectOutput = false;
2930 } else if (value == "true") {
2931 mDirectOutput = true;
2932 }
2933 }
2934 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2935 param.remove(String8("test_cmd_policy_input"));
2936 mTestInput = valueInt;
2937 }
2938
2939 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2940 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002941 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002942 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002943 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002944 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002945 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002946 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002947 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002948 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002949 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002950 if (target == "Manager") {
2951 mTestFormat = format;
2952 } else if (mTestOutputs[mCurOutput] != 0) {
2953 AudioParameter outputParam = AudioParameter();
2954 outputParam.addInt(String8("format"), format);
2955 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2956 }
2957 }
2958 }
2959 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2960 param.remove(String8("test_cmd_policy_channels"));
2961 int channels = 0;
2962
2963 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002964 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002965 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002966 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002967 }
2968 if (channels != 0) {
2969 if (target == "Manager") {
2970 mTestChannels = channels;
2971 } else if (mTestOutputs[mCurOutput] != 0) {
2972 AudioParameter outputParam = AudioParameter();
2973 outputParam.addInt(String8("channels"), channels);
2974 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2975 }
2976 }
2977 }
2978 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2979 param.remove(String8("test_cmd_policy_sampleRate"));
2980 if (valueInt >= 0 && valueInt <= 96000) {
2981 int samplingRate = valueInt;
2982 if (target == "Manager") {
2983 mTestSamplingRate = samplingRate;
2984 } else if (mTestOutputs[mCurOutput] != 0) {
2985 AudioParameter outputParam = AudioParameter();
2986 outputParam.addInt(String8("sampling_rate"), samplingRate);
2987 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2988 }
2989 }
2990 }
2991
2992 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2993 param.remove(String8("test_cmd_policy_reopen"));
2994
Eric Laurentc75307b2015-03-17 15:29:32 -07002995 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07002996
Eric Laurentc75307b2015-03-17 15:29:32 -07002997 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07002998
Eric Laurentc75307b2015-03-17 15:29:32 -07002999 removeOutput(mPrimaryOutput->mIoHandle);
3000 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3001 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003002 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003003 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3004 config.sample_rate = outputDesc->mSamplingRate;
3005 config.channel_mask = outputDesc->mChannelMask;
3006 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003007 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003008 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003009 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003010 &config,
3011 &outputDesc->mDevice,
3012 String8(""),
3013 &outputDesc->mLatency,
3014 outputDesc->mFlags);
3015 if (status != NO_ERROR) {
3016 ALOGE("Failed to reopen hardware output stream, "
3017 "samplingRate: %d, format %d, channels %d",
3018 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003019 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003020 outputDesc->mSamplingRate = config.sample_rate;
3021 outputDesc->mChannelMask = config.channel_mask;
3022 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003023 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003024 AudioParameter outputCmd = AudioParameter();
3025 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003026 mpClientInterface->setParameters(handle, outputCmd.toString());
3027 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003028 }
3029 }
3030
3031
3032 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3033 }
3034 }
3035 return false;
3036}
3037
Eric Laurente0720872014-03-11 09:30:41 -07003038void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003039{
3040 {
3041 AutoMutex _l(mLock);
3042 requestExit();
3043 mWaitWorkCV.signal();
3044 }
3045 requestExitAndWait();
3046}
3047
Eric Laurente0720872014-03-11 09:30:41 -07003048int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003049{
3050 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3051 if (output == mTestOutputs[i]) return i;
3052 }
3053 return 0;
3054}
3055#endif //AUDIO_POLICY_TEST
3056
3057// ---
3058
Eric Laurentc75307b2015-03-17 15:29:32 -07003059void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003060{
François Gaffie98cc1912015-03-18 17:52:40 +01003061 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003062 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003063 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003064}
3065
François Gaffie53615e22015-03-19 09:24:12 +01003066void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3067{
3068 mOutputs.removeItem(output);
3069}
3070
Eric Laurent1f2f2232014-06-02 12:01:23 -07003071void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003072{
François Gaffie98cc1912015-03-18 17:52:40 +01003073 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003074 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003075 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003076}
Eric Laurente552edb2014-03-10 17:42:56 -07003077
Eric Laurentc75307b2015-03-17 15:29:32 -07003078void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003079 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003080 const String8 address /*in*/,
3081 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003082 sp<DeviceDescriptor> devDesc =
3083 desc->mProfile->mSupportedDevices.getDevice(device, address);
3084 if (devDesc != 0) {
3085 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3086 desc->mIoHandle, address.string());
3087 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003088 }
3089}
3090
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003091status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003092 audio_policy_dev_state_t state,
3093 SortedVector<audio_io_handle_t>& outputs,
3094 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003095{
François Gaffie53615e22015-03-19 09:24:12 +01003096 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003097 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003098 // erase all current sample rates, formats and channel masks
3099 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003100
Eric Laurent3b73df72014-03-11 09:06:29 -07003101 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003102 // first list already open outputs that can be routed to this device
3103 for (size_t i = 0; i < mOutputs.size(); i++) {
3104 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003105 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003106 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003107 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3108 outputs.add(mOutputs.keyAt(i));
3109 } else {
3110 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003111 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003112 }
Eric Laurente552edb2014-03-10 17:42:56 -07003113 }
3114 }
3115 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003116 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003117 for (size_t i = 0; i < mHwModules.size(); i++)
3118 {
3119 if (mHwModules[i]->mHandle == 0) {
3120 continue;
3121 }
3122 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3123 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003124 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3125 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003126 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003127 address == profile->mSupportedDevices[0]->mAddress) {
3128 profiles.add(profile);
3129 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3130 }
Eric Laurente552edb2014-03-10 17:42:56 -07003131 }
3132 }
3133 }
3134
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003135 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3136
Eric Laurente552edb2014-03-10 17:42:56 -07003137 if (profiles.isEmpty() && outputs.isEmpty()) {
3138 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3139 return BAD_VALUE;
3140 }
3141
3142 // open outputs for matching profiles if needed. Direct outputs are also opened to
3143 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3144 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003145 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003146
3147 // nothing to do if one output is already opened for this profile
3148 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003149 for (j = 0; j < outputs.size(); j++) {
3150 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003151 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003152 // matching profile: save the sample rates, format and channel masks supported
3153 // by the profile in our device descriptor
3154 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003155 break;
3156 }
3157 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003158 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003159 continue;
3160 }
3161
Eric Laurent83b88082014-06-20 18:31:16 -07003162 ALOGV("opening output for device %08x with params %s profile %p",
3163 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003164 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003165 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003166 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3167 config.sample_rate = desc->mSamplingRate;
3168 config.channel_mask = desc->mChannelMask;
3169 config.format = desc->mFormat;
3170 config.offload_info.sample_rate = desc->mSamplingRate;
3171 config.offload_info.channel_mask = desc->mChannelMask;
3172 config.offload_info.format = desc->mFormat;
3173 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003174 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003175 &output,
3176 &config,
3177 &desc->mDevice,
3178 address,
3179 &desc->mLatency,
3180 desc->mFlags);
3181 if (status == NO_ERROR) {
3182 desc->mSamplingRate = config.sample_rate;
3183 desc->mChannelMask = config.channel_mask;
3184 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003185
Eric Laurentd4692962014-05-05 18:13:44 -07003186 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003187 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003188 char *param = audio_device_address_to_parameter(device, address);
3189 mpClientInterface->setParameters(output, String8(param));
3190 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003191 }
3192
Eric Laurentd4692962014-05-05 18:13:44 -07003193 // Here is where we step through and resolve any "dynamic" fields
3194 String8 reply;
3195 char *value;
3196 if (profile->mSamplingRates[0] == 0) {
3197 reply = mpClientInterface->getParameters(output,
3198 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003199 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003200 reply.string());
3201 value = strpbrk((char *)reply.string(), "=");
3202 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003203 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003204 }
Eric Laurentd4692962014-05-05 18:13:44 -07003205 }
3206 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3207 reply = mpClientInterface->getParameters(output,
3208 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003209 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003210 reply.string());
3211 value = strpbrk((char *)reply.string(), "=");
3212 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003213 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003214 }
Eric Laurentd4692962014-05-05 18:13:44 -07003215 }
3216 if (profile->mChannelMasks[0] == 0) {
3217 reply = mpClientInterface->getParameters(output,
3218 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003219 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003220 reply.string());
3221 value = strpbrk((char *)reply.string(), "=");
3222 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003223 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003224 }
Eric Laurentd4692962014-05-05 18:13:44 -07003225 }
3226 if (((profile->mSamplingRates[0] == 0) &&
3227 (profile->mSamplingRates.size() < 2)) ||
3228 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3229 (profile->mFormats.size() < 2)) ||
3230 ((profile->mChannelMasks[0] == 0) &&
3231 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003232 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003233 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003234 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003235 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3236 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003237 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003238 config.sample_rate = profile->pickSamplingRate();
3239 config.channel_mask = profile->pickChannelMask();
3240 config.format = profile->pickFormat();
3241 config.offload_info.sample_rate = config.sample_rate;
3242 config.offload_info.channel_mask = config.channel_mask;
3243 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003244 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003245 &output,
3246 &config,
3247 &desc->mDevice,
3248 address,
3249 &desc->mLatency,
3250 desc->mFlags);
3251 if (status == NO_ERROR) {
3252 desc->mSamplingRate = config.sample_rate;
3253 desc->mChannelMask = config.channel_mask;
3254 desc->mFormat = config.format;
3255 } else {
3256 output = AUDIO_IO_HANDLE_NONE;
3257 }
Eric Laurentd4692962014-05-05 18:13:44 -07003258 }
3259
Eric Laurentcf2c0212014-07-25 16:20:43 -07003260 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003261 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003262 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003263 sp<AudioPolicyMix> policyMix;
3264 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003265 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3266 address.string());
3267 }
François Gaffie036e1e92015-03-19 10:16:24 +01003268 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003269 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003270
Eric Laurentc722f302014-12-10 11:21:49 -08003271 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3272 // no duplicated output for direct outputs and
3273 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003274 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003275
Eric Laurentd4692962014-05-05 18:13:44 -07003276 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003277 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003278
Eric Laurentd4692962014-05-05 18:13:44 -07003279 //TODO: configure audio effect output stage here
3280
3281 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003282 duplicatedOutput =
3283 mpClientInterface->openDuplicateOutput(output,
3284 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003285 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003286 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003287 sp<SwAudioOutputDescriptor> dupOutputDesc =
3288 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3289 dupOutputDesc->mOutput1 = mPrimaryOutput;
3290 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003291 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3292 dupOutputDesc->mFormat = desc->mFormat;
3293 dupOutputDesc->mChannelMask = desc->mChannelMask;
3294 dupOutputDesc->mLatency = desc->mLatency;
3295 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003296 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003297 } else {
3298 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003299 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003300 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003301 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003302 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003303 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003304 }
Eric Laurente552edb2014-03-10 17:42:56 -07003305 }
3306 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003307 } else {
3308 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003309 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003310 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003311 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003312 profiles.removeAt(profile_index);
3313 profile_index--;
3314 } else {
3315 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003316 devDesc->importAudioPort(profile);
3317
François Gaffie53615e22015-03-19 09:24:12 +01003318 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003319 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3320 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003321 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003322 NULL/*patch handle*/, address.string());
3323 }
Eric Laurente552edb2014-03-10 17:42:56 -07003324 ALOGV("checkOutputsForDevice(): adding output %d", output);
3325 }
3326 }
3327
3328 if (profiles.isEmpty()) {
3329 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3330 return BAD_VALUE;
3331 }
Eric Laurentd4692962014-05-05 18:13:44 -07003332 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003333 // check if one opened output is not needed any more after disconnecting one device
3334 for (size_t i = 0; i < mOutputs.size(); i++) {
3335 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003336 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003337 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003338 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003339 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003340 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003341 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003342 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3343 mOutputs.keyAt(i));
3344 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003345 }
Eric Laurente552edb2014-03-10 17:42:56 -07003346 }
3347 }
Eric Laurentd4692962014-05-05 18:13:44 -07003348 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003349 for (size_t i = 0; i < mHwModules.size(); i++)
3350 {
3351 if (mHwModules[i]->mHandle == 0) {
3352 continue;
3353 }
3354 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3355 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003356 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003357 if (profile->mSupportedDevices.types() & device) {
3358 ALOGV("checkOutputsForDevice(): "
3359 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003360 if (profile->mSamplingRates[0] == 0) {
3361 profile->mSamplingRates.clear();
3362 profile->mSamplingRates.add(0);
3363 }
3364 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3365 profile->mFormats.clear();
3366 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3367 }
3368 if (profile->mChannelMasks[0] == 0) {
3369 profile->mChannelMasks.clear();
3370 profile->mChannelMasks.add(0);
3371 }
3372 }
3373 }
3374 }
3375 }
3376 return NO_ERROR;
3377}
3378
Eric Laurentd4692962014-05-05 18:13:44 -07003379status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003380 audio_policy_dev_state_t state,
3381 SortedVector<audio_io_handle_t>& inputs,
3382 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003383{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003384 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003385 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3386 // first list already open inputs that can be routed to this device
3387 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3388 desc = mInputs.valueAt(input_index);
3389 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3390 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3391 inputs.add(mInputs.keyAt(input_index));
3392 }
3393 }
3394
3395 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003396 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003397 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3398 {
3399 if (mHwModules[module_idx]->mHandle == 0) {
3400 continue;
3401 }
3402 for (size_t profile_index = 0;
3403 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3404 profile_index++)
3405 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003406 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3407
3408 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003409 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003410 address == profile->mSupportedDevices[0]->mAddress) {
3411 profiles.add(profile);
3412 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3413 profile_index, module_idx);
3414 }
Eric Laurentd4692962014-05-05 18:13:44 -07003415 }
3416 }
3417 }
3418
3419 if (profiles.isEmpty() && inputs.isEmpty()) {
3420 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3421 return BAD_VALUE;
3422 }
3423
3424 // open inputs for matching profiles if needed. Direct inputs are also opened to
3425 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3426 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3427
Eric Laurent1c333e22014-05-20 10:48:17 -07003428 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003429 // nothing to do if one input is already opened for this profile
3430 size_t input_index;
3431 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3432 desc = mInputs.valueAt(input_index);
3433 if (desc->mProfile == profile) {
3434 break;
3435 }
3436 }
3437 if (input_index != mInputs.size()) {
3438 continue;
3439 }
3440
3441 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3442 desc = new AudioInputDescriptor(profile);
3443 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003444 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3445 config.sample_rate = desc->mSamplingRate;
3446 config.channel_mask = desc->mChannelMask;
3447 config.format = desc->mFormat;
3448 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003449 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003450 &input,
3451 &config,
3452 &desc->mDevice,
3453 address,
3454 AUDIO_SOURCE_MIC,
3455 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003456
Eric Laurentcf2c0212014-07-25 16:20:43 -07003457 if (status == NO_ERROR) {
3458 desc->mSamplingRate = config.sample_rate;
3459 desc->mChannelMask = config.channel_mask;
3460 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003461
Eric Laurentd4692962014-05-05 18:13:44 -07003462 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003463 char *param = audio_device_address_to_parameter(device, address);
3464 mpClientInterface->setParameters(input, String8(param));
3465 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003466 }
3467
3468 // Here is where we step through and resolve any "dynamic" fields
3469 String8 reply;
3470 char *value;
3471 if (profile->mSamplingRates[0] == 0) {
3472 reply = mpClientInterface->getParameters(input,
3473 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3474 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3475 reply.string());
3476 value = strpbrk((char *)reply.string(), "=");
3477 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003478 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003479 }
3480 }
3481 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3482 reply = mpClientInterface->getParameters(input,
3483 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3484 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3485 value = strpbrk((char *)reply.string(), "=");
3486 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003487 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003488 }
3489 }
3490 if (profile->mChannelMasks[0] == 0) {
3491 reply = mpClientInterface->getParameters(input,
3492 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3493 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3494 reply.string());
3495 value = strpbrk((char *)reply.string(), "=");
3496 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003497 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003498 }
3499 }
3500 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3501 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3502 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3503 ALOGW("checkInputsForDevice() direct input missing param");
3504 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003505 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003506 }
3507
3508 if (input != 0) {
3509 addInput(input, desc);
3510 }
3511 } // endif input != 0
3512
Eric Laurentcf2c0212014-07-25 16:20:43 -07003513 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003514 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003515 profiles.removeAt(profile_index);
3516 profile_index--;
3517 } else {
3518 inputs.add(input);
3519 ALOGV("checkInputsForDevice(): adding input %d", input);
3520 }
3521 } // end scan profiles
3522
3523 if (profiles.isEmpty()) {
3524 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3525 return BAD_VALUE;
3526 }
3527 } else {
3528 // Disconnect
3529 // check if one opened input is not needed any more after disconnecting one device
3530 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3531 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003532 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3533 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003534 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3535 mInputs.keyAt(input_index));
3536 inputs.add(mInputs.keyAt(input_index));
3537 }
3538 }
3539 // Clear any profiles associated with the disconnected device.
3540 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3541 if (mHwModules[module_index]->mHandle == 0) {
3542 continue;
3543 }
3544 for (size_t profile_index = 0;
3545 profile_index < mHwModules[module_index]->mInputProfiles.size();
3546 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003547 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003548 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003549 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003550 profile_index, module_index);
3551 if (profile->mSamplingRates[0] == 0) {
3552 profile->mSamplingRates.clear();
3553 profile->mSamplingRates.add(0);
3554 }
3555 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3556 profile->mFormats.clear();
3557 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3558 }
3559 if (profile->mChannelMasks[0] == 0) {
3560 profile->mChannelMasks.clear();
3561 profile->mChannelMasks.add(0);
3562 }
3563 }
3564 }
3565 }
3566 } // end disconnect
3567
3568 return NO_ERROR;
3569}
3570
3571
Eric Laurente0720872014-03-11 09:30:41 -07003572void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003573{
3574 ALOGV("closeOutput(%d)", output);
3575
Eric Laurentc75307b2015-03-17 15:29:32 -07003576 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003577 if (outputDesc == NULL) {
3578 ALOGW("closeOutput() unknown output %d", output);
3579 return;
3580 }
François Gaffie036e1e92015-03-19 10:16:24 +01003581 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003582
Eric Laurente552edb2014-03-10 17:42:56 -07003583 // look for duplicated outputs connected to the output being removed.
3584 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003585 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003586 if (dupOutputDesc->isDuplicated() &&
3587 (dupOutputDesc->mOutput1 == outputDesc ||
3588 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003589 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003590 if (dupOutputDesc->mOutput1 == outputDesc) {
3591 outputDesc2 = dupOutputDesc->mOutput2;
3592 } else {
3593 outputDesc2 = dupOutputDesc->mOutput1;
3594 }
3595 // As all active tracks on duplicated output will be deleted,
3596 // and as they were also referenced on the other output, the reference
3597 // count for their stream type must be adjusted accordingly on
3598 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003599 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003600 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003601 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003602 }
3603 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3604 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3605
3606 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003607 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003608 }
3609 }
3610
Eric Laurent05b90f82014-08-27 15:32:29 -07003611 nextAudioPortGeneration();
3612
3613 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3614 if (index >= 0) {
3615 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3616 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3617 mAudioPatches.removeItemsAt(index);
3618 mpClientInterface->onAudioPatchListUpdate();
3619 }
3620
Eric Laurente552edb2014-03-10 17:42:56 -07003621 AudioParameter param;
3622 param.add(String8("closing"), String8("true"));
3623 mpClientInterface->setParameters(output, param.toString());
3624
3625 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003626 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003627 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003628}
3629
3630void AudioPolicyManager::closeInput(audio_io_handle_t input)
3631{
3632 ALOGV("closeInput(%d)", input);
3633
3634 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3635 if (inputDesc == NULL) {
3636 ALOGW("closeInput() unknown input %d", input);
3637 return;
3638 }
3639
Eric Laurent6a94d692014-05-20 11:18:06 -07003640 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003641
3642 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3643 if (index >= 0) {
3644 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3645 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3646 mAudioPatches.removeItemsAt(index);
3647 mpClientInterface->onAudioPatchListUpdate();
3648 }
3649
3650 mpClientInterface->closeInput(input);
3651 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003652}
3653
Eric Laurentc75307b2015-03-17 15:29:32 -07003654SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3655 audio_devices_t device,
3656 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003657{
3658 SortedVector<audio_io_handle_t> outputs;
3659
3660 ALOGVV("getOutputsForDevice() device %04x", device);
3661 for (size_t i = 0; i < openOutputs.size(); i++) {
3662 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003663 i, openOutputs.valueAt(i)->isDuplicated(),
3664 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003665 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3666 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3667 outputs.add(openOutputs.keyAt(i));
3668 }
3669 }
3670 return outputs;
3671}
3672
Eric Laurente0720872014-03-11 09:30:41 -07003673bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003674 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003675{
3676 if (outputs1.size() != outputs2.size()) {
3677 return false;
3678 }
3679 for (size_t i = 0; i < outputs1.size(); i++) {
3680 if (outputs1[i] != outputs2[i]) {
3681 return false;
3682 }
3683 }
3684 return true;
3685}
3686
Eric Laurente0720872014-03-11 09:30:41 -07003687void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003688{
3689 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3690 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3691 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3692 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3693
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003694 // also take into account external policy-related changes: add all outputs which are
3695 // associated with policies in the "before" and "after" output vectors
3696 ALOGVV("checkOutputForStrategy(): policy related outputs");
3697 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003698 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003699 if (desc != 0 && desc->mPolicyMix != NULL) {
3700 srcOutputs.add(desc->mIoHandle);
3701 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3702 }
3703 }
3704 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003705 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003706 if (desc != 0 && desc->mPolicyMix != NULL) {
3707 dstOutputs.add(desc->mIoHandle);
3708 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3709 }
3710 }
3711
Eric Laurente552edb2014-03-10 17:42:56 -07003712 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3713 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3714 strategy, srcOutputs[0], dstOutputs[0]);
3715 // mute strategy while moving tracks from one output to another
3716 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003717 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003718 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003719 setStrategyMute(strategy, true, desc);
3720 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003721 }
3722 }
3723
3724 // Move effects associated to this strategy from previous output to new output
3725 if (strategy == STRATEGY_MEDIA) {
3726 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3727 SortedVector<audio_io_handle_t> moved;
3728 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003729 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3730 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3731 effectDesc->mIo != fxOutput) {
3732 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003733 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3734 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003735 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003736 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003737 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003738 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003739 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003740 }
3741 }
3742 }
3743 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003744 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003745 if (i == AUDIO_STREAM_PATCH) {
3746 continue;
3747 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003748 if (getStrategy((audio_stream_type_t)i) == strategy) {
3749 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003750 }
3751 }
3752 }
3753}
3754
Eric Laurente0720872014-03-11 09:30:41 -07003755void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003756{
François Gaffie2110e042015-03-24 08:41:51 +01003757 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003758 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003759 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003760 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003761 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003762 checkOutputForStrategy(STRATEGY_SONIFICATION);
3763 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003764 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003765 checkOutputForStrategy(STRATEGY_MEDIA);
3766 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003767 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003768}
3769
Eric Laurente0720872014-03-11 09:30:41 -07003770void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003771{
François Gaffie53615e22015-03-19 09:24:12 +01003772 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003773 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003774 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003775 return;
3776 }
3777
Eric Laurent3a4311c2014-03-17 12:00:47 -07003778 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003779 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3780 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3781 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003782 // suspend A2DP output if:
3783 // (NOT already suspended) &&
3784 // ((SCO device is connected &&
3785 // (forced usage for communication || for record is SCO))) ||
3786 // (phone state is ringing || in call)
3787 //
3788 // restore A2DP output if:
3789 // (Already suspended) &&
3790 // ((SCO device is NOT connected ||
3791 // (forced usage NOT for communication && NOT for record is SCO))) &&
3792 // (phone state is NOT ringing && NOT in call)
3793 //
3794 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003795 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003796 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3797 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3798 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3799 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003800
3801 mpClientInterface->restoreOutput(a2dpOutput);
3802 mA2dpSuspended = false;
3803 }
3804 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003805 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003806 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3807 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3808 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3809 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003810
3811 mpClientInterface->suspendOutput(a2dpOutput);
3812 mA2dpSuspended = true;
3813 }
3814 }
3815}
3816
Eric Laurentc75307b2015-03-17 15:29:32 -07003817audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3818 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003819{
3820 audio_devices_t device = AUDIO_DEVICE_NONE;
3821
Eric Laurent6a94d692014-05-20 11:18:06 -07003822 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3823 if (index >= 0) {
3824 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3825 if (patchDesc->mUid != mUidCached) {
3826 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3827 outputDesc->device(), outputDesc->mPatchHandle);
3828 return outputDesc->device();
3829 }
3830 }
3831
Eric Laurente552edb2014-03-10 17:42:56 -07003832 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003833 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003834 // use device for strategy enforced audible
3835 // 2: we are in call or the strategy phone is active on the output:
3836 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003837 // 3: the strategy for enforced audible is active but not enforced on the output:
3838 // use the device for strategy enforced audible
3839 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003840 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003841 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003842 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003843 // 6: the strategy accessibility is active on the output:
3844 // use device for strategy accessibility
3845 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003846 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003847 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003848 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003849 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003850 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003851 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003852 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003853 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3854 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003855 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003856 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003857 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003858 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003859 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003860 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003861 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003862 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003863 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003864 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003865 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003866 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003867 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003868 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003869 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003870 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003871 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003872 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003873 }
3874
Eric Laurent1c333e22014-05-20 10:48:17 -07003875 ALOGV("getNewOutputDevice() selected device %x", device);
3876 return device;
3877}
3878
3879audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3880{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003881 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003882
3883 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3884 if (index >= 0) {
3885 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3886 if (patchDesc->mUid != mUidCached) {
3887 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3888 inputDesc->mDevice, inputDesc->mPatchHandle);
3889 return inputDesc->mDevice;
3890 }
3891 }
3892
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003893 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003894
Eric Laurente552edb2014-03-10 17:42:56 -07003895 return device;
3896}
3897
Eric Laurente0720872014-03-11 09:30:41 -07003898uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003899 return (uint32_t)getStrategy(stream);
3900}
3901
Eric Laurente0720872014-03-11 09:30:41 -07003902audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003903 // By checking the range of stream before calling getStrategy, we avoid
3904 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3905 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003906 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003907 return AUDIO_DEVICE_NONE;
3908 }
3909 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003910 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003911 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3912 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3913 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003914 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003915 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003916 devices = outputDesc->device();
3917 break;
3918 }
Eric Laurente552edb2014-03-10 17:42:56 -07003919 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003920
3921 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3922 and doesn't really need to.*/
3923 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3924 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3925 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3926 }
3927
Eric Laurente552edb2014-03-10 17:42:56 -07003928 return devices;
3929}
3930
François Gaffie2110e042015-03-24 08:41:51 +01003931routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3932{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003933 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003934 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003935}
3936
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003937uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3938 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003939 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3940 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3941 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003942 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3943 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3944 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003945 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003946 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003947}
3948
Eric Laurente0720872014-03-11 09:30:41 -07003949void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003950 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003951 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003952 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3953 updateDevicesAndOutputs();
3954 break;
3955 default:
3956 break;
3957 }
3958}
3959
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003960uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3961 switch(event) {
3962 case STARTING_OUTPUT:
3963 mBeaconMuteRefCount++;
3964 break;
3965 case STOPPING_OUTPUT:
3966 if (mBeaconMuteRefCount > 0) {
3967 mBeaconMuteRefCount--;
3968 }
3969 break;
3970 case STARTING_BEACON:
3971 mBeaconPlayingRefCount++;
3972 break;
3973 case STOPPING_BEACON:
3974 if (mBeaconPlayingRefCount > 0) {
3975 mBeaconPlayingRefCount--;
3976 }
3977 break;
3978 }
3979
3980 if (mBeaconMuteRefCount > 0) {
3981 // any playback causes beacon to be muted
3982 return setBeaconMute(true);
3983 } else {
3984 // no other playback: unmute when beacon starts playing, mute when it stops
3985 return setBeaconMute(mBeaconPlayingRefCount == 0);
3986 }
3987}
3988
3989uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3990 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3991 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3992 // keep track of muted state to avoid repeating mute/unmute operations
3993 if (mBeaconMuted != mute) {
3994 // mute/unmute AUDIO_STREAM_TTS on all outputs
3995 ALOGV("\t muting %d", mute);
3996 uint32_t maxLatency = 0;
3997 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003998 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003999 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004000 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004001 0 /*delay*/, AUDIO_DEVICE_NONE);
4002 const uint32_t latency = desc->latency() * 2;
4003 if (latency > maxLatency) {
4004 maxLatency = latency;
4005 }
4006 }
4007 mBeaconMuted = mute;
4008 return maxLatency;
4009 }
4010 return 0;
4011}
4012
Eric Laurente0720872014-03-11 09:30:41 -07004013audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004014 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004015{
Paul McLeanaa981192015-03-21 09:55:15 -07004016 // Routing
4017 // see if we have an explicit route
4018 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4019 // (getStrategy(stream)).
4020 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4021 // and activity count is non-zero
4022 // the device = the device from the descriptor in the RouteMap, and exit.
4023 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4024 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4025 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004026 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004027 return route->mDeviceDescriptor->type();
4028 }
4029 }
4030
Eric Laurente552edb2014-03-10 17:42:56 -07004031 if (fromCache) {
4032 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4033 strategy, mDeviceForStrategy[strategy]);
4034 return mDeviceForStrategy[strategy];
4035 }
François Gaffie2110e042015-03-24 08:41:51 +01004036 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004037}
4038
Eric Laurente0720872014-03-11 09:30:41 -07004039void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004040{
4041 for (int i = 0; i < NUM_STRATEGIES; i++) {
4042 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4043 }
4044 mPreviousOutputs = mOutputs;
4045}
4046
Eric Laurent1f2f2232014-06-02 12:01:23 -07004047uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004048 audio_devices_t prevDevice,
4049 uint32_t delayMs)
4050{
4051 // mute/unmute strategies using an incompatible device combination
4052 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4053 // if unmuting, unmute only after the specified delay
4054 if (outputDesc->isDuplicated()) {
4055 return 0;
4056 }
4057
4058 uint32_t muteWaitMs = 0;
4059 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004060 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004061
4062 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4063 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004064 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004065 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4066 bool doMute = false;
4067
4068 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4069 doMute = true;
4070 outputDesc->mStrategyMutedByDevice[i] = true;
4071 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4072 doMute = true;
4073 outputDesc->mStrategyMutedByDevice[i] = false;
4074 }
Eric Laurent99401132014-05-07 19:48:15 -07004075 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004076 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004077 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004078 // skip output if it does not share any device with current output
4079 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4080 == AUDIO_DEVICE_NONE) {
4081 continue;
4082 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004083 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4084 mute ? "muting" : "unmuting", i, curDevice);
4085 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004086 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004087 if (mute) {
4088 // FIXME: should not need to double latency if volume could be applied
4089 // immediately by the audioflinger mixer. We must account for the delay
4090 // between now and the next time the audioflinger thread for this output
4091 // will process a buffer (which corresponds to one buffer size,
4092 // usually 1/2 or 1/4 of the latency).
4093 if (muteWaitMs < desc->latency() * 2) {
4094 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004095 }
4096 }
4097 }
4098 }
4099 }
4100 }
4101
Eric Laurent99401132014-05-07 19:48:15 -07004102 // temporary mute output if device selection changes to avoid volume bursts due to
4103 // different per device volumes
4104 if (outputDesc->isActive() && (device != prevDevice)) {
4105 if (muteWaitMs < outputDesc->latency() * 2) {
4106 muteWaitMs = outputDesc->latency() * 2;
4107 }
4108 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004109 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004110 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004111 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004112 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004113 muteWaitMs *2, device);
4114 }
4115 }
4116 }
4117
Eric Laurente552edb2014-03-10 17:42:56 -07004118 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4119 if (muteWaitMs > delayMs) {
4120 muteWaitMs -= delayMs;
4121 usleep(muteWaitMs * 1000);
4122 return muteWaitMs;
4123 }
4124 return 0;
4125}
4126
Eric Laurentc75307b2015-03-17 15:29:32 -07004127uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004128 audio_devices_t device,
4129 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004130 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004131 audio_patch_handle_t *patchHandle,
4132 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004133{
Eric Laurentc75307b2015-03-17 15:29:32 -07004134 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004135 AudioParameter param;
4136 uint32_t muteWaitMs;
4137
4138 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004139 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4140 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004141 return muteWaitMs;
4142 }
4143 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4144 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004145 if ((device != AUDIO_DEVICE_NONE) &&
4146 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004147 return 0;
4148 }
4149
4150 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004151 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004152
4153 audio_devices_t prevDevice = outputDesc->mDevice;
4154
Paul McLeanaa981192015-03-21 09:55:15 -07004155 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004156
4157 if (device != AUDIO_DEVICE_NONE) {
4158 outputDesc->mDevice = device;
4159 }
4160 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4161
4162 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004163 // the requested device is AUDIO_DEVICE_NONE
4164 // OR the requested device is the same as current device
4165 // AND force is not specified
4166 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004167 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004168 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4169 !force &&
4170 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004171 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004172 return muteWaitMs;
4173 }
4174
4175 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004176
Eric Laurente552edb2014-03-10 17:42:56 -07004177 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004178 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004179 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004180 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004181 DeviceVector deviceList = (address == NULL) ?
4182 mAvailableOutputDevices.getDevicesFromType(device)
4183 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004184 if (!deviceList.isEmpty()) {
4185 struct audio_patch patch;
4186 outputDesc->toAudioPortConfig(&patch.sources[0]);
4187 patch.num_sources = 1;
4188 patch.num_sinks = 0;
4189 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4190 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004191 patch.num_sinks++;
4192 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004193 ssize_t index;
4194 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4195 index = mAudioPatches.indexOfKey(*patchHandle);
4196 } else {
4197 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4198 }
4199 sp< AudioPatch> patchDesc;
4200 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4201 if (index >= 0) {
4202 patchDesc = mAudioPatches.valueAt(index);
4203 afPatchHandle = patchDesc->mAfPatchHandle;
4204 }
4205
Eric Laurent1c333e22014-05-20 10:48:17 -07004206 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004207 &afPatchHandle,
4208 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004209 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4210 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004211 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004212 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004213 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004214 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004215 addAudioPatch(patchDesc->mHandle, patchDesc);
4216 } else {
4217 patchDesc->mPatch = patch;
4218 }
4219 patchDesc->mAfPatchHandle = afPatchHandle;
4220 patchDesc->mUid = mUidCached;
4221 if (patchHandle) {
4222 *patchHandle = patchDesc->mHandle;
4223 }
4224 outputDesc->mPatchHandle = patchDesc->mHandle;
4225 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004226 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004227 }
4228 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004229
4230 // inform all input as well
4231 for (size_t i = 0; i < mInputs.size(); i++) {
4232 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004233 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004234 AudioParameter inputCmd = AudioParameter();
4235 ALOGV("%s: inform input %d of device:%d", __func__,
4236 inputDescriptor->mIoHandle, device);
4237 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4238 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4239 inputCmd.toString(),
4240 delayMs);
4241 }
4242 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004243 }
Eric Laurente552edb2014-03-10 17:42:56 -07004244
4245 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004246 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004247
4248 return muteWaitMs;
4249}
4250
Eric Laurentc75307b2015-03-17 15:29:32 -07004251status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004252 int delayMs,
4253 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004254{
Eric Laurent6a94d692014-05-20 11:18:06 -07004255 ssize_t index;
4256 if (patchHandle) {
4257 index = mAudioPatches.indexOfKey(*patchHandle);
4258 } else {
4259 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4260 }
4261 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004262 return INVALID_OPERATION;
4263 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004264 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4265 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004266 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4267 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004268 removeAudioPatch(patchDesc->mHandle);
4269 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004270 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004271 return status;
4272}
4273
4274status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4275 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004276 bool force,
4277 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004278{
4279 status_t status = NO_ERROR;
4280
Eric Laurent1f2f2232014-06-02 12:01:23 -07004281 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004282 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4283 inputDesc->mDevice = device;
4284
4285 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4286 if (!deviceList.isEmpty()) {
4287 struct audio_patch patch;
4288 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004289 // AUDIO_SOURCE_HOTWORD is for internal use only:
4290 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004291 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4292 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004293 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4294 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004295 patch.num_sinks = 1;
4296 //only one input device for now
4297 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004298 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004299 ssize_t index;
4300 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4301 index = mAudioPatches.indexOfKey(*patchHandle);
4302 } else {
4303 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4304 }
4305 sp< AudioPatch> patchDesc;
4306 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4307 if (index >= 0) {
4308 patchDesc = mAudioPatches.valueAt(index);
4309 afPatchHandle = patchDesc->mAfPatchHandle;
4310 }
4311
Eric Laurent1c333e22014-05-20 10:48:17 -07004312 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004313 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004314 0);
4315 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004316 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004317 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004318 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004319 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004320 addAudioPatch(patchDesc->mHandle, patchDesc);
4321 } else {
4322 patchDesc->mPatch = patch;
4323 }
4324 patchDesc->mAfPatchHandle = afPatchHandle;
4325 patchDesc->mUid = mUidCached;
4326 if (patchHandle) {
4327 *patchHandle = patchDesc->mHandle;
4328 }
4329 inputDesc->mPatchHandle = patchDesc->mHandle;
4330 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004331 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004332 }
4333 }
4334 }
4335 return status;
4336}
4337
Eric Laurent6a94d692014-05-20 11:18:06 -07004338status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4339 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004340{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004341 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004342 ssize_t index;
4343 if (patchHandle) {
4344 index = mAudioPatches.indexOfKey(*patchHandle);
4345 } else {
4346 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4347 }
4348 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004349 return INVALID_OPERATION;
4350 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004351 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4352 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004353 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4354 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004355 removeAudioPatch(patchDesc->mHandle);
4356 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004357 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004358 return status;
4359}
4360
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004361sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004362 String8 address,
4363 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004364 audio_format_t& format,
4365 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004366 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004367{
4368 // Choose an input profile based on the requested capture parameters: select the first available
4369 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004370 //
4371 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4372 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004373
4374 for (size_t i = 0; i < mHwModules.size(); i++)
4375 {
4376 if (mHwModules[i]->mHandle == 0) {
4377 continue;
4378 }
4379 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4380 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004381 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004382 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004383 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004384 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004385 format,
4386 &format /*updatedFormat*/,
4387 channelMask,
4388 &channelMask /*updatedChannelMask*/,
4389 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004390
Eric Laurente552edb2014-03-10 17:42:56 -07004391 return profile;
4392 }
4393 }
4394 }
4395 return NULL;
4396}
4397
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004398
4399audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004400 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004401{
François Gaffie036e1e92015-03-19 10:16:24 +01004402 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4403 audio_devices_t selectedDeviceFromMix =
4404 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004405
François Gaffie036e1e92015-03-19 10:16:24 +01004406 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4407 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004408 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004409 return getDeviceForInputSource(inputSource);
4410}
4411
4412audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4413{
Paul McLean466dc8e2015-04-17 13:15:36 -06004414 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4415 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004416 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004417 return route->mDeviceDescriptor->type();
4418 }
4419 }
4420
4421 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004422}
4423
Eric Laurente0720872014-03-11 09:30:41 -07004424float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004425 int index,
4426 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004427{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004428 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004429
4430 // if a headset is connected, apply the following rules to ring tones and notifications
4431 // to avoid sound level bursts in user's ears:
4432 // - always attenuate ring tones and notifications volume by 6dB
4433 // - if music is playing, always limit the volume to current music volume,
4434 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004435 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004436 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4437 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4438 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4439 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4440 ((stream_strategy == STRATEGY_SONIFICATION)
4441 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004442 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004443 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004444 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4445 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004446 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004447 // when the phone is ringing we must consider that music could have been paused just before
4448 // by the music application and behave as if music was active if the last music track was
4449 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004450 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004451 mLimitRingtoneVolume) {
4452 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004453 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4454 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004455 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004456 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4457 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4458 if (volumeDb > minVolDB) {
4459 volumeDb = minVolDB;
4460 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004461 }
4462 }
4463 }
4464
Eric Laurentffbc80f2015-03-18 18:30:19 -07004465 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004466}
4467
Eric Laurente0720872014-03-11 09:30:41 -07004468status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004469 int index,
4470 const sp<AudioOutputDescriptor>& outputDesc,
4471 audio_devices_t device,
4472 int delayMs,
4473 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004474{
Eric Laurente552edb2014-03-10 17:42:56 -07004475 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004476 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004477 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004478 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004479 return NO_ERROR;
4480 }
François Gaffie2110e042015-03-24 08:41:51 +01004481 audio_policy_forced_cfg_t forceUseForComm =
4482 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004483 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004484 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4485 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004486 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004487 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004488 return INVALID_OPERATION;
4489 }
4490
Eric Laurentc75307b2015-03-17 15:29:32 -07004491 if (device == AUDIO_DEVICE_NONE) {
4492 device = outputDesc->device();
4493 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004494
Eric Laurentffbc80f2015-03-18 18:30:19 -07004495 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004496 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004497 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004498 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004499
Eric Laurentffbc80f2015-03-18 18:30:19 -07004500 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004501
Eric Laurent3b73df72014-03-11 09:06:29 -07004502 if (stream == AUDIO_STREAM_VOICE_CALL ||
4503 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004504 float voiceVolume;
4505 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004506 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004507 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004508 } else {
4509 voiceVolume = 1.0;
4510 }
4511
Eric Laurentc75307b2015-03-17 15:29:32 -07004512 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004513 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4514 mLastVoiceVolume = voiceVolume;
4515 }
4516 }
4517
4518 return NO_ERROR;
4519}
4520
Eric Laurentc75307b2015-03-17 15:29:32 -07004521void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4522 audio_devices_t device,
4523 int delayMs,
4524 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004525{
Eric Laurentc75307b2015-03-17 15:29:32 -07004526 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004527
Eric Laurent3b73df72014-03-11 09:06:29 -07004528 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004529 if (stream == AUDIO_STREAM_PATCH) {
4530 continue;
4531 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004532 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004533 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4534 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004535 device,
4536 delayMs,
4537 force);
4538 }
4539}
4540
Eric Laurente0720872014-03-11 09:30:41 -07004541void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004542 bool on,
4543 const sp<AudioOutputDescriptor>& outputDesc,
4544 int delayMs,
4545 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004546{
4547 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004548 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004549 if (stream == AUDIO_STREAM_PATCH) {
4550 continue;
4551 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004552 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004553 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004554 }
4555 }
4556}
4557
Eric Laurente0720872014-03-11 09:30:41 -07004558void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004559 bool on,
4560 const sp<AudioOutputDescriptor>& outputDesc,
4561 int delayMs,
4562 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004563{
Eric Laurentc75307b2015-03-17 15:29:32 -07004564 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004565 if (device == AUDIO_DEVICE_NONE) {
4566 device = outputDesc->device();
4567 }
4568
Eric Laurentc75307b2015-03-17 15:29:32 -07004569 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4570 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004571
4572 if (on) {
4573 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004574 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004575 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004576 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004577 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004578 }
4579 }
4580 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4581 outputDesc->mMuteCount[stream]++;
4582 } else {
4583 if (outputDesc->mMuteCount[stream] == 0) {
4584 ALOGV("setStreamMute() unmuting non muted stream!");
4585 return;
4586 }
4587 if (--outputDesc->mMuteCount[stream] == 0) {
4588 checkAndSetVolume(stream,
4589 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004590 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004591 device,
4592 delayMs);
4593 }
4594 }
4595}
4596
Eric Laurente0720872014-03-11 09:30:41 -07004597void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004598 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004599{
4600 // if the stream pertains to sonification strategy and we are in call we must
4601 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4602 // in the device used for phone strategy and play the tone if the selected device does not
4603 // interfere with the device used for phone strategy
4604 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4605 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004606 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004607 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4608 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004609 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004610 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4611 stream, starting, outputDesc->mDevice, stateChange);
4612 if (outputDesc->mRefCount[stream]) {
4613 int muteCount = 1;
4614 if (stateChange) {
4615 muteCount = outputDesc->mRefCount[stream];
4616 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004617 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004618 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4619 for (int i = 0; i < muteCount; i++) {
4620 setStreamMute(stream, starting, mPrimaryOutput);
4621 }
4622 } else {
4623 ALOGV("handleIncallSonification() high visibility");
4624 if (outputDesc->device() &
4625 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4626 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4627 for (int i = 0; i < muteCount; i++) {
4628 setStreamMute(stream, starting, mPrimaryOutput);
4629 }
4630 }
4631 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004632 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4633 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004634 } else {
4635 mpClientInterface->stopTone();
4636 }
4637 }
4638 }
4639 }
4640}
4641
Paul McLeanaa981192015-03-21 09:55:15 -07004642// --- SessionRoute class implementation
4643void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004644 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4645 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004646 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4647 mRefCount, mActivityCount);
4648}
4649
4650// --- SessionRouteMap class implementation
4651bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4652{
4653 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4654}
4655
Eric Laurent493404d2015-04-21 15:07:36 -07004656bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4657{
4658 if (indexOfKey(session) >= 0) {
4659 if (valueFor(session)->mChanged) {
4660 valueFor(session)->mChanged = false;
4661 return true;
4662 }
4663 }
4664 return false;
4665}
4666
Paul McLeanaa981192015-03-21 09:55:15 -07004667void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4668{
4669 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4670 if (route != 0) {
4671 ALOG_ASSERT(route->mRefCount > 0);
4672 --route->mRefCount;
4673 if (route->mRefCount <= 0) {
4674 removeItem(session);
4675 }
4676 }
4677}
4678
4679int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4680{
4681 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4682 return route != 0 ? ++(route->mActivityCount) : -1;
4683}
4684
4685int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4686{
4687 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4688 if (route != 0 && route->mActivityCount > 0) {
4689 return --(route->mActivityCount);
4690 } else {
4691 return -1;
4692 }
4693}
4694
4695void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4696 ALOGI("%s ----", caption);
4697 for(size_t index = 0; index < size(); index++) {
4698 valueAt(index)->log(" ");
4699 }
4700}
4701
Paul McLean466dc8e2015-04-17 13:15:36 -06004702void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4703 audio_stream_type_t streamType,
4704 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004705 sp<DeviceDescriptor> descriptor,
4706 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004707{
4708 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4709 ALOGE("Adding Output Route to InputRouteMap");
4710 return;
4711 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4712 ALOGE("Adding Input Route to OutputRouteMap");
4713 return;
4714 }
4715
4716 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4717
4718 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004719 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4720 ((route->mDeviceDescriptor != 0) &&
4721 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004722 route->mChanged = true;
4723 }
4724 route->mRefCount++;
4725 route->mDeviceDescriptor = descriptor;
4726 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004727 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004728 route->mRefCount++;
4729 add(session, route);
4730 if (descriptor != 0) {
4731 route->mChanged = true;
4732 }
4733 }
4734}
4735
Eric Laurente0720872014-03-11 09:30:41 -07004736void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004737{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004738 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004739 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004740 sp<DeviceDescriptor> defaultInputDevice =
4741 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004742 mAvailableOutputDevices.add(mDefaultOutputDevice);
4743 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004744
4745 module = new HwModule("primary");
4746
Eric Laurent322b4d22015-04-03 15:57:54 -07004747 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4748 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004749 profile->mSamplingRates.add(44100);
4750 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4751 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004752 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004753 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4754 module->mOutputProfiles.add(profile);
4755
Eric Laurent322b4d22015-04-03 15:57:54 -07004756 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4757 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004758 profile->mSamplingRates.add(8000);
4759 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4760 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004761 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004762 module->mInputProfiles.add(profile);
4763
4764 mHwModules.add(module);
4765}
4766
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004767audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4768{
4769 // flags to stream type mapping
4770 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4771 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4772 }
4773 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4774 return AUDIO_STREAM_BLUETOOTH_SCO;
4775 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004776 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4777 return AUDIO_STREAM_TTS;
4778 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004779
4780 // usage to stream type mapping
4781 switch (attr->usage) {
4782 case AUDIO_USAGE_MEDIA:
4783 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004784 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4785 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004786 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004787 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4788 return AUDIO_STREAM_ALARM;
4789 }
4790 if (isStreamActive(AUDIO_STREAM_RING)) {
4791 return AUDIO_STREAM_RING;
4792 }
4793 if (isInCall()) {
4794 return AUDIO_STREAM_VOICE_CALL;
4795 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004796 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004797 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4798 return AUDIO_STREAM_SYSTEM;
4799 case AUDIO_USAGE_VOICE_COMMUNICATION:
4800 return AUDIO_STREAM_VOICE_CALL;
4801
4802 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4803 return AUDIO_STREAM_DTMF;
4804
4805 case AUDIO_USAGE_ALARM:
4806 return AUDIO_STREAM_ALARM;
4807 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4808 return AUDIO_STREAM_RING;
4809
4810 case AUDIO_USAGE_NOTIFICATION:
4811 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4812 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4813 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4814 case AUDIO_USAGE_NOTIFICATION_EVENT:
4815 return AUDIO_STREAM_NOTIFICATION;
4816
4817 case AUDIO_USAGE_UNKNOWN:
4818 default:
4819 return AUDIO_STREAM_MUSIC;
4820 }
4821}
Eric Laurente83b55d2014-11-14 10:06:21 -08004822
François Gaffie53615e22015-03-19 09:24:12 +01004823bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4824{
Eric Laurente83b55d2014-11-14 10:06:21 -08004825 // has flags that map to a strategy?
4826 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4827 return true;
4828 }
4829
4830 // has known usage?
4831 switch (paa->usage) {
4832 case AUDIO_USAGE_UNKNOWN:
4833 case AUDIO_USAGE_MEDIA:
4834 case AUDIO_USAGE_VOICE_COMMUNICATION:
4835 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4836 case AUDIO_USAGE_ALARM:
4837 case AUDIO_USAGE_NOTIFICATION:
4838 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4839 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4840 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4841 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4842 case AUDIO_USAGE_NOTIFICATION_EVENT:
4843 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4844 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4845 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4846 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004847 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004848 break;
4849 default:
4850 return false;
4851 }
4852 return true;
4853}
4854
François Gaffiead3183e2015-03-18 16:55:35 +01004855bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4856 routing_strategy strategy, uint32_t inPastMs,
4857 nsecs_t sysTime) const
4858{
4859 if ((sysTime == 0) && (inPastMs != 0)) {
4860 sysTime = systemTime();
4861 }
4862 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4863 if (i == AUDIO_STREAM_PATCH) {
4864 continue;
4865 }
4866 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4867 (NUM_STRATEGIES == strategy)) &&
4868 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4869 return true;
4870 }
4871 }
4872 return false;
4873}
4874
François Gaffie2110e042015-03-24 08:41:51 +01004875audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4876{
4877 return mEngine->getForceUse(usage);
4878}
4879
4880bool AudioPolicyManager::isInCall()
4881{
4882 return isStateInCall(mEngine->getPhoneState());
4883}
4884
4885bool AudioPolicyManager::isStateInCall(int state)
4886{
4887 return is_state_in_call(state);
4888}
4889
Eric Laurente552edb2014-03-10 17:42:56 -07004890}; // namespace android