blob: aa2e044f5ae251ffe33a67fa174641baf087efc4 [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 Laurent1f2f2232014-06-02 12:01:23 -0700160 sp<AudioOutputDescriptor> 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 Laurentc2730ba2014-07-20 15:47:07 -0700179 audio_io_handle_t output = mOutputs.keyAt(i);
François Gaffie2110e042015-03-24 08:41:51 +0100180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700181 audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i),
182 true /*fromCache*/);
183 // do not force device change on duplicated output because if device is 0, it will
184 // also force a device 0 for the two outputs it is duplicated to which may override
185 // a valid device selection on those outputs.
186 bool force = !mOutputs.valueAt(i)->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100187 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700188 // always force when disconnecting (a non-duplicated device)
189 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
190 setOutputDevice(output, newDevice, force, 0);
191 }
Eric Laurente552edb2014-03-10 17:42:56 -0700192 }
193
Eric Laurent72aa32f2014-05-30 18:51:48 -0700194 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700195 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700196 } // end if is output device
197
Eric Laurente552edb2014-03-10 17:42:56 -0700198 // handle input devices
199 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700200 SortedVector <audio_io_handle_t> inputs;
201
Eric Laurent3a4311c2014-03-17 12:00:47 -0700202 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700203 switch (state)
204 {
205 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700206 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700207 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700208 ALOGW("setDeviceConnectionState() device already connected: %d", device);
209 return INVALID_OPERATION;
210 }
François Gaffie53615e22015-03-19 09:24:12 +0100211 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700212 if (module == NULL) {
213 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
214 device);
215 return INVALID_OPERATION;
216 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800217 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700218 return INVALID_OPERATION;
219 }
220
Eric Laurent3a4311c2014-03-17 12:00:47 -0700221 index = mAvailableInputDevices.add(devDesc);
222 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800223 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700224 } else {
225 return NO_MEMORY;
226 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800227
228 // Set connect to HALs
229 AudioParameter param = AudioParameter(devDesc->mAddress);
230 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
231 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
232
François Gaffie2110e042015-03-24 08:41:51 +0100233 // Propagate device availability to Engine
234 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700235 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700236
237 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700238 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700239 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700240 ALOGW("setDeviceConnectionState() device not connected: %d", device);
241 return INVALID_OPERATION;
242 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700243
244 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
245
246 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800247 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700248 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
249 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
250
Eric Laurenta1d525f2015-01-29 13:36:45 -0800251 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700252 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700253
François Gaffie2110e042015-03-24 08:41:51 +0100254 // Propagate device availability to Engine
255 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700256 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700257
258 default:
259 ALOGE("setDeviceConnectionState() invalid state: %x", state);
260 return BAD_VALUE;
261 }
262
Eric Laurentd4692962014-05-05 18:13:44 -0700263 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700264
François Gaffie2110e042015-03-24 08:41:51 +0100265 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700266 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
267 updateCallRouting(newDevice);
268 }
269
Eric Laurentb52c1522014-05-20 11:27:36 -0700270 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700271 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700272 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700273
274 ALOGW("setDeviceConnectionState() invalid device: %x", device);
275 return BAD_VALUE;
276}
277
Eric Laurente0720872014-03-11 09:30:41 -0700278audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100279 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700280{
François Gaffie53615e22015-03-19 09:24:12 +0100281 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
282
Eric Laurent3a4311c2014-03-17 12:00:47 -0700283 DeviceVector *deviceVector;
284
Eric Laurente552edb2014-03-10 17:42:56 -0700285 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700286 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700287 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700288 deviceVector = &mAvailableInputDevices;
289 } else {
290 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
291 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700292 }
François Gaffie53615e22015-03-19 09:24:12 +0100293 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800294}
295
Eric Laurentc2730ba2014-07-20 15:47:07 -0700296void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
297{
298 bool createTxPatch = false;
299 struct audio_patch patch;
300 patch.num_sources = 1;
301 patch.num_sinks = 1;
302 status_t status;
303 audio_patch_handle_t afPatchHandle;
304 DeviceVector deviceList;
305
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800306 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700307 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
308
309 // release existing RX patch if any
310 if (mCallRxPatch != 0) {
311 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
312 mCallRxPatch.clear();
313 }
314 // release TX patch if any
315 if (mCallTxPatch != 0) {
316 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
317 mCallTxPatch.clear();
318 }
319
320 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
321 // via setOutputDevice() on primary output.
322 // Otherwise, create two audio patches for TX and RX path.
323 if (availablePrimaryOutputDevices() & rxDevice) {
324 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
325 // If the TX device is also on the primary HW module, setOutputDevice() will take care
326 // of it due to legacy implementation. If not, create a patch.
327 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
328 == AUDIO_DEVICE_NONE) {
329 createTxPatch = true;
330 }
331 } else {
332 // create RX path audio patch
333 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
334 ALOG_ASSERT(!deviceList.isEmpty(),
335 "updateCallRouting() selected device not in output device list");
336 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
337 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
338 ALOG_ASSERT(!deviceList.isEmpty(),
339 "updateCallRouting() no telephony RX device");
340 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
341
342 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
343 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
344
345 // request to reuse existing output stream if one is already opened to reach the RX device
346 SortedVector<audio_io_handle_t> outputs =
347 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700348 audio_io_handle_t output = selectOutput(outputs,
349 AUDIO_OUTPUT_FLAG_NONE,
350 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700351 if (output != AUDIO_IO_HANDLE_NONE) {
352 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
353 ALOG_ASSERT(!outputDesc->isDuplicated(),
354 "updateCallRouting() RX device output is duplicated");
355 outputDesc->toAudioPortConfig(&patch.sources[1]);
356 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]);
398 patch.num_sources = 2;
399 }
400
401 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
402 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
403 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
404 status);
405 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100406 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700407 mCallTxPatch->mAfPatchHandle = afPatchHandle;
408 mCallTxPatch->mUid = mUidCached;
409 }
410 }
411}
412
Eric Laurente0720872014-03-11 09:30:41 -0700413void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700414{
415 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100416 // store previous phone state for management of sonification strategy below
417 int oldState = mEngine->getPhoneState();
418
419 if (mEngine->setPhoneState(state) != NO_ERROR) {
420 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700421 return;
422 }
François Gaffie2110e042015-03-24 08:41:51 +0100423 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700424 // if leaving call state, handle special case of active streams
425 // pertaining to sonification strategy see handleIncallSonification()
426 if (isInCall()) {
427 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700428 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800429 if (stream == AUDIO_STREAM_PATCH) {
430 continue;
431 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700433 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800434
435 // force reevaluating accessibility routing when call starts
436 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
438
François Gaffie2110e042015-03-24 08:41:51 +0100439 /**
440 * Switching to or from incall state or switching between telephony and VoIP lead to force
441 * routing command.
442 */
443 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
444 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700445
446 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700447 checkA2dpSuspend();
448 checkOutputForAllStrategies();
449 updateDevicesAndOutputs();
450
Eric Laurent1f2f2232014-06-02 12:01:23 -0700451 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700452
Eric Laurente552edb2014-03-10 17:42:56 -0700453 int delayMs = 0;
454 if (isStateInCall(state)) {
455 nsecs_t sysTime = systemTime();
456 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700457 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700458 // mute media and sonification strategies and delay device switch by the largest
459 // latency of any output where either strategy is active.
460 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100461 if ((isStrategyActive(desc, STRATEGY_MEDIA,
462 SONIFICATION_HEADSET_MUSIC_DELAY,
463 sysTime) ||
464 isStrategyActive(desc, STRATEGY_SONIFICATION,
465 SONIFICATION_HEADSET_MUSIC_DELAY,
466 sysTime)) &&
Eric Laurente552edb2014-03-10 17:42:56 -0700467 (delayMs < (int)desc->mLatency*2)) {
468 delayMs = desc->mLatency*2;
469 }
470 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
471 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
472 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
473 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
474 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
475 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
476 }
477 }
478
Eric Laurentc2730ba2014-07-20 15:47:07 -0700479 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
480 // the device returned is not necessarily reachable via this output
481 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
482 // force routing command to audio hardware when ending call
483 // even if no device change is needed
484 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
485 rxDevice = hwOutputDesc->device();
486 }
Eric Laurente552edb2014-03-10 17:42:56 -0700487
Eric Laurentc2730ba2014-07-20 15:47:07 -0700488 if (state == AUDIO_MODE_IN_CALL) {
489 updateCallRouting(rxDevice, delayMs);
490 } else if (oldState == AUDIO_MODE_IN_CALL) {
491 if (mCallRxPatch != 0) {
492 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
493 mCallRxPatch.clear();
494 }
495 if (mCallTxPatch != 0) {
496 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
497 mCallTxPatch.clear();
498 }
499 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
500 } else {
501 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
502 }
Eric Laurente552edb2014-03-10 17:42:56 -0700503 // if entering in call state, handle special case of active streams
504 // pertaining to sonification strategy see handleIncallSonification()
505 if (isStateInCall(state)) {
506 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700507 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800508 if (stream == AUDIO_STREAM_PATCH) {
509 continue;
510 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700512 }
513 }
514
515 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700516 if (state == AUDIO_MODE_RINGTONE &&
517 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700518 mLimitRingtoneVolume = true;
519 } else {
520 mLimitRingtoneVolume = false;
521 }
522}
523
Eric Laurente0720872014-03-11 09:30:41 -0700524void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700525 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700526{
François Gaffie2110e042015-03-24 08:41:51 +0100527 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700528
François Gaffie2110e042015-03-24 08:41:51 +0100529 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
530 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
531 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700532 }
François Gaffie2110e042015-03-24 08:41:51 +0100533 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
534 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
535 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700536
537 // check for device and output changes triggered by new force usage
538 checkA2dpSuspend();
539 checkOutputForAllStrategies();
540 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100541 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700542 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
543 updateCallRouting(newDevice);
544 }
Eric Laurente552edb2014-03-10 17:42:56 -0700545 for (size_t i = 0; i < mOutputs.size(); i++) {
546 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700547 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
François Gaffie2110e042015-03-24 08:41:51 +0100548 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700549 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
550 }
Eric Laurente552edb2014-03-10 17:42:56 -0700551 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
552 applyStreamVolumes(output, newDevice, 0, true);
553 }
554 }
555
François Gaffie53615e22015-03-19 09:24:12 +0100556 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700557 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700558 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700559 }
560
561}
562
Eric Laurente0720872014-03-11 09:30:41 -0700563void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700564{
565 ALOGV("setSystemProperty() property %s, value %s", property, value);
566}
567
568// Find a direct output profile compatible with the parameters passed, even if the input flags do
569// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800570sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700571 audio_devices_t device,
572 uint32_t samplingRate,
573 audio_format_t format,
574 audio_channel_mask_t channelMask,
575 audio_output_flags_t flags)
576{
577 for (size_t i = 0; i < mHwModules.size(); i++) {
578 if (mHwModules[i]->mHandle == 0) {
579 continue;
580 }
581 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700582 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent275e8e92014-11-30 15:14:47 -0800583 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700584 NULL /*updatedSamplingRate*/, format, channelMask,
585 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
586 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700587 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
588 return profile;
589 }
Eric Laurente552edb2014-03-10 17:42:56 -0700590 }
591 }
592 return 0;
593}
594
Eric Laurente0720872014-03-11 09:30:41 -0700595audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100596 uint32_t samplingRate,
597 audio_format_t format,
598 audio_channel_mask_t channelMask,
599 audio_output_flags_t flags,
600 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700601{
Eric Laurent3b73df72014-03-11 09:06:29 -0700602 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700603 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
604 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
605 device, stream, samplingRate, format, channelMask, flags);
606
Eric Laurente83b55d2014-11-14 10:06:21 -0800607 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
608 stream, samplingRate,format, channelMask,
609 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700610}
611
Eric Laurente83b55d2014-11-14 10:06:21 -0800612status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
613 audio_io_handle_t *output,
614 audio_session_t session,
615 audio_stream_type_t *stream,
616 uint32_t samplingRate,
617 audio_format_t format,
618 audio_channel_mask_t channelMask,
619 audio_output_flags_t flags,
620 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700621{
Eric Laurente83b55d2014-11-14 10:06:21 -0800622 audio_attributes_t attributes;
623 if (attr != NULL) {
624 if (!isValidAttributes(attr)) {
625 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
626 attr->usage, attr->content_type, attr->flags,
627 attr->tags);
628 return BAD_VALUE;
629 }
630 attributes = *attr;
631 } else {
632 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
633 ALOGE("getOutputForAttr(): invalid stream type");
634 return BAD_VALUE;
635 }
636 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700637 }
François Gaffie036e1e92015-03-19 10:16:24 +0100638 sp<AudioOutputDescriptor> desc;
639 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
640 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
641 if (!audio_is_linear_pcm(format)) {
642 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800643 }
François Gaffie036e1e92015-03-19 10:16:24 +0100644 *stream = streamTypefromAttributesInt(&attributes);
645 *output = desc->mIoHandle;
646 ALOGV("getOutputForAttr() returns output %d", *output);
647 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800648 }
649 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
650 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
651 return BAD_VALUE;
652 }
653
Eric Laurent93c3d412014-08-01 14:48:35 -0700654 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
François Gaffie53615e22015-03-19 09:24:12 +0100655 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700656
Eric Laurente83b55d2014-11-14 10:06:21 -0800657 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700658 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700659
Eric Laurente83b55d2014-11-14 10:06:21 -0800660 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700661 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
662 }
663
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700664 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700665 device, samplingRate, format, channelMask, flags);
666
Eric Laurente83b55d2014-11-14 10:06:21 -0800667 *stream = streamTypefromAttributesInt(&attributes);
668 *output = getOutputForDevice(device, session, *stream,
669 samplingRate, format, channelMask,
670 flags, offloadInfo);
671 if (*output == AUDIO_IO_HANDLE_NONE) {
672 return INVALID_OPERATION;
673 }
674 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700675}
676
677audio_io_handle_t AudioPolicyManager::getOutputForDevice(
678 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800679 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700680 audio_stream_type_t stream,
681 uint32_t samplingRate,
682 audio_format_t format,
683 audio_channel_mask_t channelMask,
684 audio_output_flags_t flags,
685 const audio_offload_info_t *offloadInfo)
686{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700687 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700688 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700689 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700690
Eric Laurente552edb2014-03-10 17:42:56 -0700691#ifdef AUDIO_POLICY_TEST
692 if (mCurOutput != 0) {
693 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
694 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
695
696 if (mTestOutputs[mCurOutput] == 0) {
697 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700698 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700699 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700700 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700701 outputDesc->mFlags =
702 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700703 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700704 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
705 config.sample_rate = mTestSamplingRate;
706 config.channel_mask = mTestChannels;
707 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700708 if (offloadInfo != NULL) {
709 config.offload_info = *offloadInfo;
710 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700711 status = mpClientInterface->openOutput(0,
712 &mTestOutputs[mCurOutput],
713 &config,
714 &outputDesc->mDevice,
715 String8(""),
716 &outputDesc->mLatency,
717 outputDesc->mFlags);
718 if (status == NO_ERROR) {
719 outputDesc->mSamplingRate = config.sample_rate;
720 outputDesc->mFormat = config.format;
721 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700722 AudioParameter outputCmd = AudioParameter();
723 outputCmd.addInt(String8("set_id"),mCurOutput);
724 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
725 addOutput(mTestOutputs[mCurOutput], outputDesc);
726 }
727 }
728 return mTestOutputs[mCurOutput];
729 }
730#endif //AUDIO_POLICY_TEST
731
732 // open a direct output if required by specified parameters
733 //force direct flag if offload flag is set: offloading implies a direct output stream
734 // and all common behaviors are driven by checking only the direct flag
735 // this should normally be set appropriately in the policy configuration file
736 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700737 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700738 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700739 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
740 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
741 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800742 // only allow deep buffering for music stream type
743 if (stream != AUDIO_STREAM_MUSIC) {
744 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
745 }
Eric Laurente552edb2014-03-10 17:42:56 -0700746
Eric Laurentb732cf52014-09-24 19:08:21 -0700747 sp<IOProfile> profile;
748
749 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700750 // and not explicitly requested
751 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
752 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700753 audio_channel_count_from_out_mask(channelMask) <= 2) {
754 goto non_direct_output;
755 }
756
Eric Laurente552edb2014-03-10 17:42:56 -0700757 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
758 // creating an offloaded track and tearing it down immediately after start when audioflinger
759 // detects there is an active non offloadable effect.
760 // FIXME: We should check the audio session here but we do not have it in this context.
761 // This may prevent offloading in rare situations where effects are left active by apps
762 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700763
Eric Laurente552edb2014-03-10 17:42:56 -0700764 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100765 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700766 profile = getProfileForDirectOutput(device,
767 samplingRate,
768 format,
769 channelMask,
770 (audio_output_flags_t)flags);
771 }
772
Eric Laurent1c333e22014-05-20 10:48:17 -0700773 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700774 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700775
776 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700777 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700778 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
779 outputDesc = desc;
780 // reuse direct output if currently open and configured with same parameters
781 if ((samplingRate == outputDesc->mSamplingRate) &&
782 (format == outputDesc->mFormat) &&
783 (channelMask == outputDesc->mChannelMask)) {
784 outputDesc->mDirectOpenCount++;
785 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
786 return mOutputs.keyAt(i);
787 }
788 }
789 }
790 // close direct output if currently open and configured with different parameters
791 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700792 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700793 }
794 outputDesc = new AudioOutputDescriptor(profile);
795 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700796 outputDesc->mLatency = 0;
797 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700798 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
799 config.sample_rate = samplingRate;
800 config.channel_mask = channelMask;
801 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700802 if (offloadInfo != NULL) {
803 config.offload_info = *offloadInfo;
804 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700805 status = mpClientInterface->openOutput(profile->mModule->mHandle,
806 &output,
807 &config,
808 &outputDesc->mDevice,
809 String8(""),
810 &outputDesc->mLatency,
811 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700812
813 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700814 if (status != NO_ERROR ||
815 (samplingRate != 0 && samplingRate != config.sample_rate) ||
816 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
817 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700818 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
819 "format %d %d, channelMask %04x %04x", output, samplingRate,
820 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
821 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700822 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700823 mpClientInterface->closeOutput(output);
824 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800825 // fall back to mixer output if possible when the direct output could not be open
826 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
827 goto non_direct_output;
828 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800829 // fall back to mixer output if possible when the direct output could not be open
830 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
831 goto non_direct_output;
832 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700833 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700834 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700835 outputDesc->mSamplingRate = config.sample_rate;
836 outputDesc->mChannelMask = config.channel_mask;
837 outputDesc->mFormat = config.format;
838 outputDesc->mRefCount[stream] = 0;
839 outputDesc->mStopTime[stream] = 0;
840 outputDesc->mDirectOpenCount = 1;
841
Eric Laurente552edb2014-03-10 17:42:56 -0700842 audio_io_handle_t srcOutput = getOutputForEffect();
843 addOutput(output, outputDesc);
844 audio_io_handle_t dstOutput = getOutputForEffect();
845 if (dstOutput == output) {
846 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
847 }
848 mPreviousOutputs = mOutputs;
849 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700850 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700851 return output;
852 }
853
Eric Laurentb732cf52014-09-24 19:08:21 -0700854non_direct_output:
855
Eric Laurente552edb2014-03-10 17:42:56 -0700856 // ignoring channel mask due to downmix capability in mixer
857
858 // open a non direct output
859
860 // for non direct outputs, only PCM is supported
861 if (audio_is_linear_pcm(format)) {
862 // get which output is suitable for the specified stream. The actual
863 // routing change will happen when startOutput() will be called
864 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
865
Eric Laurent8838a382014-09-08 16:44:28 -0700866 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
867 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
868 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700869 }
870 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
871 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
872
873 ALOGV("getOutput() returns output %d", output);
874
875 return output;
876}
877
Eric Laurente0720872014-03-11 09:30:41 -0700878audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700879 audio_output_flags_t flags,
880 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700881{
882 // select one output among several that provide a path to a particular device or set of
883 // devices (the list was previously build by getOutputsForDevice()).
884 // The priority is as follows:
885 // 1: the output with the highest number of requested policy flags
886 // 2: the primary output
887 // 3: the first output in the list
888
889 if (outputs.size() == 0) {
890 return 0;
891 }
892 if (outputs.size() == 1) {
893 return outputs[0];
894 }
895
896 int maxCommonFlags = 0;
897 audio_io_handle_t outputFlags = 0;
898 audio_io_handle_t outputPrimary = 0;
899
900 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700901 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700902 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700903 // if a valid format is specified, skip output if not compatible
904 if (format != AUDIO_FORMAT_INVALID) {
905 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
906 if (format != outputDesc->mFormat) {
907 continue;
908 }
909 } else if (!audio_is_linear_pcm(format)) {
910 continue;
911 }
912 }
913
Eric Laurent3b73df72014-03-11 09:06:29 -0700914 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700915 if (commonFlags > maxCommonFlags) {
916 outputFlags = outputs[i];
917 maxCommonFlags = commonFlags;
918 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
919 }
920 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
921 outputPrimary = outputs[i];
922 }
923 }
924 }
925
926 if (outputFlags != 0) {
927 return outputFlags;
928 }
929 if (outputPrimary != 0) {
930 return outputPrimary;
931 }
932
933 return outputs[0];
934}
935
Eric Laurente0720872014-03-11 09:30:41 -0700936status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700937 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800938 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700939{
940 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
941 ssize_t index = mOutputs.indexOfKey(output);
942 if (index < 0) {
943 ALOGW("startOutput() unknown output %d", output);
944 return BAD_VALUE;
945 }
946
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700947 // cannot start playback of STREAM_TTS if any other output is being used
948 uint32_t beaconMuteLatency = 0;
949 if (stream == AUDIO_STREAM_TTS) {
950 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +0100951 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700952 return INVALID_OPERATION;
953 } else {
954 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
955 }
956 } else {
957 // some playback other than beacon starts
958 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
959 }
960
Eric Laurent1f2f2232014-06-02 12:01:23 -0700961 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700962
963 // increment usage count for this stream on the requested output:
964 // NOTE that the usage count is the same for duplicated output and hardware output which is
965 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
966 outputDesc->changeRefCount(stream, 1);
967
968 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -0800969 // starting an output being rerouted?
970 audio_devices_t newDevice;
Eric Laurentc722f302014-12-10 11:21:49 -0800971 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -0800972 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
973 } else {
974 newDevice = getNewOutputDevice(output, false /*fromCache*/);
975 }
Eric Laurente552edb2014-03-10 17:42:56 -0700976 routing_strategy strategy = getStrategy(stream);
977 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700978 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
979 (beaconMuteLatency > 0);
980 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -0700981 bool force = false;
982 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700983 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700984 if (desc != outputDesc) {
985 // force a device change if any other output is managed by the same hw
986 // module and has a current device selection that differs from selected device.
987 // In this case, the audio HAL must receive the new device selection so that it can
988 // change the device currently selected by the other active output.
989 if (outputDesc->sharesHwModuleWith(desc) &&
990 desc->device() != newDevice) {
991 force = true;
992 }
993 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700994 // a notification so that audio focus effect can propagate, or that a mute/unmute
995 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -0700996 uint32_t latency = desc->latency();
997 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
998 waitMs = latency;
999 }
1000 }
1001 }
1002 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
1003
1004 // handle special case for sonification while in call
1005 if (isInCall()) {
1006 handleIncallSonification(stream, true, false);
1007 }
1008
1009 // apply volume rules for current stream and device if necessary
1010 checkAndSetVolume(stream,
1011 mStreams[stream].getVolumeIndex(newDevice),
1012 output,
1013 newDevice);
1014
1015 // update the outputs if starting an output with a stream that can affect notification
1016 // routing
1017 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001018
1019 // Automatically enable the remote submix input when output is started on a re routing mix
1020 // of type MIX_TYPE_RECORDERS
1021 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1022 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001023 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001024 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001025 outputDesc->mPolicyMix->mRegistrationId,
1026 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001027 }
1028
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001029 // force reevaluating accessibility routing when ringtone or alarm starts
1030 if (strategy == STRATEGY_SONIFICATION) {
1031 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1032 }
1033
Eric Laurente552edb2014-03-10 17:42:56 -07001034 if (waitMs > muteWaitMs) {
1035 usleep((waitMs - muteWaitMs) * 2 * 1000);
1036 }
1037 }
1038 return NO_ERROR;
1039}
1040
1041
Eric Laurente0720872014-03-11 09:30:41 -07001042status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001043 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001044 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001045{
1046 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1047 ssize_t index = mOutputs.indexOfKey(output);
1048 if (index < 0) {
1049 ALOGW("stopOutput() unknown output %d", output);
1050 return BAD_VALUE;
1051 }
1052
Eric Laurent1f2f2232014-06-02 12:01:23 -07001053 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001054
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001055 // always handle stream stop, check which stream type is stopping
1056 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1057
Eric Laurente552edb2014-03-10 17:42:56 -07001058 // handle special case for sonification while in call
1059 if (isInCall()) {
1060 handleIncallSonification(stream, false, false);
1061 }
1062
1063 if (outputDesc->mRefCount[stream] > 0) {
1064 // decrement usage count of this stream on the output
1065 outputDesc->changeRefCount(stream, -1);
1066 // store time at which the stream was stopped - see isStreamActive()
1067 if (outputDesc->mRefCount[stream] == 0) {
Eric Laurentc722f302014-12-10 11:21:49 -08001068 // Automatically disable the remote submix input when output is stopped on a
1069 // re routing mix of type MIX_TYPE_RECORDERS
1070 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1071 outputDesc->mPolicyMix != NULL &&
1072 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001073 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001074 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001075 outputDesc->mPolicyMix->mRegistrationId,
1076 "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001077 }
1078
Eric Laurente552edb2014-03-10 17:42:56 -07001079 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -07001080 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001081 // delay the device switch by twice the latency because stopOutput() is executed when
1082 // the track stop() command is received and at that time the audio track buffer can
1083 // still contain data that needs to be drained. The latency only covers the audio HAL
1084 // and kernel buffers. Also the latency does not always include additional delay in the
1085 // audio path (audio DSP, CODEC ...)
1086 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1087
1088 // force restoring the device selection on other active outputs if it differs from the
1089 // one being selected for this output
1090 for (size_t i = 0; i < mOutputs.size(); i++) {
1091 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001092 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001093 if (curOutput != output &&
1094 desc->isActive() &&
1095 outputDesc->sharesHwModuleWith(desc) &&
1096 (newDevice != desc->device())) {
1097 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -07001098 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001099 true,
1100 outputDesc->mLatency*2);
1101 }
1102 }
1103 // update the outputs if stopping one with a stream that can affect notification routing
1104 handleNotificationRoutingForStream(stream);
1105 }
1106 return NO_ERROR;
1107 } else {
1108 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1109 return INVALID_OPERATION;
1110 }
1111}
1112
Eric Laurente83b55d2014-11-14 10:06:21 -08001113void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001114 audio_stream_type_t stream __unused,
1115 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001116{
1117 ALOGV("releaseOutput() %d", output);
1118 ssize_t index = mOutputs.indexOfKey(output);
1119 if (index < 0) {
1120 ALOGW("releaseOutput() releasing unknown output %d", output);
1121 return;
1122 }
1123
1124#ifdef AUDIO_POLICY_TEST
1125 int testIndex = testOutputIndex(output);
1126 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001127 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001128 if (outputDesc->isActive()) {
1129 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001130 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001131 mTestOutputs[testIndex] = 0;
1132 }
1133 return;
1134 }
1135#endif //AUDIO_POLICY_TEST
1136
Eric Laurent1f2f2232014-06-02 12:01:23 -07001137 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001138 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001139 if (desc->mDirectOpenCount <= 0) {
1140 ALOGW("releaseOutput() invalid open count %d for output %d",
1141 desc->mDirectOpenCount, output);
1142 return;
1143 }
1144 if (--desc->mDirectOpenCount == 0) {
1145 closeOutput(output);
1146 // If effects where present on the output, audioflinger moved them to the primary
1147 // output by default: move them back to the appropriate output.
1148 audio_io_handle_t dstOutput = getOutputForEffect();
1149 if (dstOutput != mPrimaryOutput) {
1150 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1151 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001152 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001153 }
1154 }
1155}
1156
1157
Eric Laurentcaf7f482014-11-25 17:50:47 -08001158status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1159 audio_io_handle_t *input,
1160 audio_session_t session,
1161 uint32_t samplingRate,
1162 audio_format_t format,
1163 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001164 audio_input_flags_t flags,
1165 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001166{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001167 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1168 "session %d, flags %#x",
1169 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001170
Eric Laurentcaf7f482014-11-25 17:50:47 -08001171 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001172 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001173 audio_devices_t device;
1174 // handle legacy remote submix case where the address was not always specified
1175 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001176 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001177 audio_source_t inputSource = attr->source;
1178 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001179 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001180
Eric Laurentc447ded2015-01-06 08:47:05 -08001181 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1182 inputSource = AUDIO_SOURCE_MIC;
1183 }
1184 halInputSource = inputSource;
1185
1186 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001187 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
François Gaffie036e1e92015-03-19 10:16:24 +01001188 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, policyMix);
1189 if (ret != NO_ERROR) {
1190 return ret;
1191 }
1192 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001193 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1194 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001195 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001196 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001197 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001198 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001199 return BAD_VALUE;
1200 }
Eric Laurentc722f302014-12-10 11:21:49 -08001201 if (policyMix != NULL) {
1202 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001203 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1204 // there is an external policy, but this input is attached to a mix of recorders,
1205 // meaning it receives audio injected into the framework, so the recorder doesn't
1206 // know about it and is therefore considered "legacy"
1207 *inputType = API_INPUT_LEGACY;
1208 } else {
1209 // recording a mix of players defined by an external policy, we're rerouting for
1210 // an external policy
1211 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1212 }
Eric Laurentc722f302014-12-10 11:21:49 -08001213 } else if (audio_is_remote_submix_device(device)) {
1214 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001215 *inputType = API_INPUT_MIX_CAPTURE;
1216 } else {
1217 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001218 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001219 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001220 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001221 case AUDIO_SOURCE_VOICE_UPLINK:
1222 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1223 break;
1224 case AUDIO_SOURCE_VOICE_DOWNLINK:
1225 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1226 break;
1227 case AUDIO_SOURCE_VOICE_CALL:
1228 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1229 break;
1230 default:
1231 break;
1232 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001233 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001234 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1235 if (index >= 0) {
1236 *input = mSoundTriggerSessions.valueFor(session);
1237 isSoundTrigger = true;
1238 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1239 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1240 } else {
1241 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1242 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001243 }
1244 }
1245
Eric Laurent275e8e92014-11-30 15:14:47 -08001246 sp<IOProfile> profile = getInputProfile(device, address,
1247 samplingRate, format, channelMask,
1248 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001249 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001250 //retry without flags
1251 audio_input_flags_t log_flags = flags;
1252 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001253 profile = getInputProfile(device, address,
1254 samplingRate, format, channelMask,
1255 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001256 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001257 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1258 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001259 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001260 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001261 }
Eric Laurente552edb2014-03-10 17:42:56 -07001262 }
1263
1264 if (profile->mModule->mHandle == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001265 ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName);
1266 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001267 }
1268
1269 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1270 config.sample_rate = samplingRate;
1271 config.channel_mask = channelMask;
1272 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001273
Eric Laurentcf2c0212014-07-25 16:20:43 -07001274 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001275 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001276 &config,
1277 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001278 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001279 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001280 flags);
1281
1282 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001283 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001284 (samplingRate != config.sample_rate) ||
1285 (format != config.format) ||
1286 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001287 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001288 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001289 if (*input != AUDIO_IO_HANDLE_NONE) {
1290 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001291 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001292 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001293 }
1294
Eric Laurent1f2f2232014-06-02 12:01:23 -07001295 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001296 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001297 inputDesc->mRefCount = 0;
1298 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001299 inputDesc->mSamplingRate = samplingRate;
1300 inputDesc->mFormat = format;
1301 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001302 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001303 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001304 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001305 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001306
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001307 ALOGV("getInputForAttr() returns input type = %d", inputType);
1308
Eric Laurentcaf7f482014-11-25 17:50:47 -08001309 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001310 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001311 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001312}
1313
Eric Laurent4dc68062014-07-28 17:26:49 -07001314status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1315 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001316{
1317 ALOGV("startInput() input %d", input);
1318 ssize_t index = mInputs.indexOfKey(input);
1319 if (index < 0) {
1320 ALOGW("startInput() unknown input %d", input);
1321 return BAD_VALUE;
1322 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001323 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001324
Eric Laurentc722f302014-12-10 11:21:49 -08001325 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001326 if (index < 0) {
1327 ALOGW("startInput() unknown session %d on input %d", session, input);
1328 return BAD_VALUE;
1329 }
1330
Glenn Kasten74a8e252014-07-24 14:09:55 -07001331 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001332 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001333
1334 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001335 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001336 if (activeInput != 0 && activeInput != input) {
1337
1338 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1339 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001340 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001341 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001342 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001343 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1344 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001345 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001346 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001347 return INVALID_OPERATION;
1348 }
1349 }
1350 }
1351
Glenn Kasten74a8e252014-07-24 14:09:55 -07001352 if (inputDesc->mRefCount == 0) {
François Gaffie53615e22015-03-19 09:24:12 +01001353 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001354 SoundTrigger::setCaptureState(true);
1355 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001356 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001357
Eric Laurentc722f302014-12-10 11:21:49 -08001358 // automatically enable the remote submix output when input is started if not
1359 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001360 // For remote submix (a virtual device), we open only one input per capture request.
1361 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001362 String8 address = String8("");
1363 if (inputDesc->mPolicyMix == NULL) {
1364 address = String8("0");
1365 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1366 address = inputDesc->mPolicyMix->mRegistrationId;
1367 }
1368 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001369 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001370 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001371 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001372 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001373 }
Eric Laurente552edb2014-03-10 17:42:56 -07001374 }
1375
Eric Laurente552edb2014-03-10 17:42:56 -07001376 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1377
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001378 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001379 return NO_ERROR;
1380}
1381
Eric Laurent4dc68062014-07-28 17:26:49 -07001382status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1383 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001384{
1385 ALOGV("stopInput() input %d", input);
1386 ssize_t index = mInputs.indexOfKey(input);
1387 if (index < 0) {
1388 ALOGW("stopInput() unknown input %d", input);
1389 return BAD_VALUE;
1390 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001391 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001392
Eric Laurentc722f302014-12-10 11:21:49 -08001393 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001394 if (index < 0) {
1395 ALOGW("stopInput() unknown session %d on input %d", session, input);
1396 return BAD_VALUE;
1397 }
1398
Eric Laurente552edb2014-03-10 17:42:56 -07001399 if (inputDesc->mRefCount == 0) {
1400 ALOGW("stopInput() input %d already stopped", input);
1401 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001402 }
1403
1404 inputDesc->mRefCount--;
1405 if (inputDesc->mRefCount == 0) {
1406
Eric Laurentc722f302014-12-10 11:21:49 -08001407 // automatically disable the remote submix output when input is stopped if not
1408 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001409 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001410 String8 address = String8("");
1411 if (inputDesc->mPolicyMix == NULL) {
1412 address = String8("0");
1413 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1414 address = inputDesc->mPolicyMix->mRegistrationId;
1415 }
1416 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001417 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001418 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001419 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001420 }
Eric Laurente552edb2014-03-10 17:42:56 -07001421 }
1422
Eric Laurent1c333e22014-05-20 10:48:17 -07001423 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001424
François Gaffie53615e22015-03-19 09:24:12 +01001425 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001426 SoundTrigger::setCaptureState(false);
1427 }
Eric Laurente552edb2014-03-10 17:42:56 -07001428 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001429 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001430}
1431
Eric Laurent4dc68062014-07-28 17:26:49 -07001432void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1433 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001434{
1435 ALOGV("releaseInput() %d", input);
1436 ssize_t index = mInputs.indexOfKey(input);
1437 if (index < 0) {
1438 ALOGW("releaseInput() releasing unknown input %d", input);
1439 return;
1440 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001441 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1442 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001443
Eric Laurentc722f302014-12-10 11:21:49 -08001444 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001445 if (index < 0) {
1446 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1447 return;
1448 }
Eric Laurentc722f302014-12-10 11:21:49 -08001449 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001450 if (inputDesc->mOpenRefCount == 0) {
1451 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1452 return;
1453 }
1454 inputDesc->mOpenRefCount--;
1455 if (inputDesc->mOpenRefCount > 0) {
1456 ALOGV("releaseInput() exit > 0");
1457 return;
1458 }
1459
Eric Laurent05b90f82014-08-27 15:32:29 -07001460 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001461 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001462 ALOGV("releaseInput() exit");
1463}
1464
Eric Laurentd4692962014-05-05 18:13:44 -07001465void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001466 bool patchRemoved = false;
1467
Eric Laurentd4692962014-05-05 18:13:44 -07001468 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001469 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1470 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1471 if (patch_index >= 0) {
1472 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1473 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1474 mAudioPatches.removeItemsAt(patch_index);
1475 patchRemoved = true;
1476 }
Eric Laurentd4692962014-05-05 18:13:44 -07001477 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1478 }
1479 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001480 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001481
1482 if (patchRemoved) {
1483 mpClientInterface->onAudioPatchListUpdate();
1484 }
Eric Laurentd4692962014-05-05 18:13:44 -07001485}
1486
Eric Laurente0720872014-03-11 09:30:41 -07001487void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001488 int indexMin,
1489 int indexMax)
1490{
1491 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001492 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001493 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1494 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001495 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001496 }
Eric Laurente552edb2014-03-10 17:42:56 -07001497}
1498
Eric Laurente0720872014-03-11 09:30:41 -07001499status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001500 int index,
1501 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001502{
1503
François Gaffiedfd74092015-03-19 12:10:59 +01001504 if ((index < mStreams[stream].getVolumeIndexMin()) ||
1505 (index > mStreams[stream].getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001506 return BAD_VALUE;
1507 }
1508 if (!audio_is_output_device(device)) {
1509 return BAD_VALUE;
1510 }
1511
1512 // Force max volume if stream cannot be muted
François Gaffiedfd74092015-03-19 12:10:59 +01001513 if (!mStreams.canBeMuted(stream)) index = mStreams[stream].getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001514
1515 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1516 stream, device, index);
1517
1518 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1519 // clear all device specific values
1520 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001521 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001522 }
François Gaffiedfd74092015-03-19 12:10:59 +01001523 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001524
Eric Laurent31551f82014-10-10 18:21:56 -07001525 // update volume on all outputs whose current device is also selected by the same
1526 // strategy as the device specified by the caller
1527 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001528
1529
1530 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1531 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1532 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001533 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001534 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1535 }
1536 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1537 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001538 return NO_ERROR;
1539 }
Eric Laurente552edb2014-03-10 17:42:56 -07001540 status_t status = NO_ERROR;
1541 for (size_t i = 0; i < mOutputs.size(); i++) {
François Gaffiedfd74092015-03-19 12:10:59 +01001542 audio_devices_t curDevice = Volume::getDeviceForVolume(mOutputs.valueAt(i)->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001543 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001544 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1545 if (volStatus != NO_ERROR) {
1546 status = volStatus;
1547 }
1548 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001549 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1550 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
1551 index, mOutputs.keyAt(i), curDevice);
1552 }
Eric Laurente552edb2014-03-10 17:42:56 -07001553 }
1554 return status;
1555}
1556
Eric Laurente0720872014-03-11 09:30:41 -07001557status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001558 int *index,
1559 audio_devices_t device)
1560{
1561 if (index == NULL) {
1562 return BAD_VALUE;
1563 }
1564 if (!audio_is_output_device(device)) {
1565 return BAD_VALUE;
1566 }
1567 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1568 // the strategy the stream belongs to.
1569 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1570 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1571 }
François Gaffiedfd74092015-03-19 12:10:59 +01001572 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001573
1574 *index = mStreams[stream].getVolumeIndex(device);
1575 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1576 return NO_ERROR;
1577}
1578
Eric Laurente0720872014-03-11 09:30:41 -07001579audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001580 const SortedVector<audio_io_handle_t>& outputs)
1581{
1582 // select one output among several suitable for global effects.
1583 // The priority is as follows:
1584 // 1: An offloaded output. If the effect ends up not being offloadable,
1585 // AudioFlinger will invalidate the track and the offloaded output
1586 // will be closed causing the effect to be moved to a PCM output.
1587 // 2: A deep buffer output
1588 // 3: the first output in the list
1589
1590 if (outputs.size() == 0) {
1591 return 0;
1592 }
1593
1594 audio_io_handle_t outputOffloaded = 0;
1595 audio_io_handle_t outputDeepBuffer = 0;
1596
1597 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001598 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001599 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001600 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1601 outputOffloaded = outputs[i];
1602 }
1603 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1604 outputDeepBuffer = outputs[i];
1605 }
1606 }
1607
1608 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1609 outputOffloaded, outputDeepBuffer);
1610 if (outputOffloaded != 0) {
1611 return outputOffloaded;
1612 }
1613 if (outputDeepBuffer != 0) {
1614 return outputDeepBuffer;
1615 }
1616
1617 return outputs[0];
1618}
1619
Eric Laurente0720872014-03-11 09:30:41 -07001620audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001621{
1622 // apply simple rule where global effects are attached to the same output as MUSIC streams
1623
Eric Laurent3b73df72014-03-11 09:06:29 -07001624 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001625 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1626 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1627
1628 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1629 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1630 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1631
1632 return output;
1633}
1634
Eric Laurente0720872014-03-11 09:30:41 -07001635status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001636 audio_io_handle_t io,
1637 uint32_t strategy,
1638 int session,
1639 int id)
1640{
1641 ssize_t index = mOutputs.indexOfKey(io);
1642 if (index < 0) {
1643 index = mInputs.indexOfKey(io);
1644 if (index < 0) {
1645 ALOGW("registerEffect() unknown io %d", io);
1646 return INVALID_OPERATION;
1647 }
1648 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001649 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001650}
1651
Eric Laurente0720872014-03-11 09:30:41 -07001652bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001653{
1654 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001655 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001656 if (inputDescriptor->mRefCount == 0) {
1657 continue;
1658 }
1659 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001660 return true;
1661 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001662 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1663 // corresponds to an active capture triggered by a hardware hotword recognition
1664 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1665 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1666 // FIXME: we should not assume that the first session is the active one and keep
1667 // activity count per session. Same in startInput().
1668 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1669 if (index >= 0) {
1670 return true;
1671 }
1672 }
Eric Laurente552edb2014-03-10 17:42:56 -07001673 }
1674 return false;
1675}
1676
Eric Laurent275e8e92014-11-30 15:14:47 -08001677// Register a list of custom mixes with their attributes and format.
1678// When a mix is registered, corresponding input and output profiles are
1679// added to the remote submix hw module. The profile contains only the
1680// parameters (sampling rate, format...) specified by the mix.
1681// The corresponding input remote submix device is also connected.
1682//
1683// When a remote submix device is connected, the address is checked to select the
1684// appropriate profile and the corresponding input or output stream is opened.
1685//
1686// When capture starts, getInputForAttr() will:
1687// - 1 look for a mix matching the address passed in attribtutes tags if any
1688// - 2 if none found, getDeviceForInputSource() will:
1689// - 2.1 look for a mix matching the attributes source
1690// - 2.2 if none found, default to device selection by policy rules
1691// At this time, the corresponding output remote submix device is also connected
1692// and active playback use cases can be transferred to this mix if needed when reconnecting
1693// after AudioTracks are invalidated
1694//
1695// When playback starts, getOutputForAttr() will:
1696// - 1 look for a mix matching the address passed in attribtutes tags if any
1697// - 2 if none found, look for a mix matching the attributes usage
1698// - 3 if none found, default to device and output selection by policy rules.
1699
1700status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1701{
1702 sp<HwModule> module;
1703 for (size_t i = 0; i < mHwModules.size(); i++) {
1704 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1705 mHwModules[i]->mHandle != 0) {
1706 module = mHwModules[i];
1707 break;
1708 }
1709 }
1710
1711 if (module == 0) {
1712 return INVALID_OPERATION;
1713 }
1714
1715 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1716
1717 for (size_t i = 0; i < mixes.size(); i++) {
1718 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001719
1720 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001721 continue;
1722 }
1723 audio_config_t outputConfig = mixes[i].mFormat;
1724 audio_config_t inputConfig = mixes[i].mFormat;
1725 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1726 // stereo and let audio flinger do the channel conversion if needed.
1727 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1728 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1729 module->addOutputProfile(address, &outputConfig,
1730 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1731 module->addInputProfile(address, &inputConfig,
1732 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001733
Eric Laurentc722f302014-12-10 11:21:49 -08001734 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001735 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001736 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001737 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001738 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001739 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001740 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001741 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001742 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001743 }
1744 return NO_ERROR;
1745}
1746
1747status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1748{
1749 sp<HwModule> module;
1750 for (size_t i = 0; i < mHwModules.size(); i++) {
1751 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1752 mHwModules[i]->mHandle != 0) {
1753 module = mHwModules[i];
1754 break;
1755 }
1756 }
1757
1758 if (module == 0) {
1759 return INVALID_OPERATION;
1760 }
1761
1762 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1763
1764 for (size_t i = 0; i < mixes.size(); i++) {
1765 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001766
1767 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001768 continue;
1769 }
1770
Eric Laurentc722f302014-12-10 11:21:49 -08001771 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1772 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1773 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001774 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001775 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001776 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001777 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001778
1779 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1780 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1781 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001782 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001783 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001784 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001785 }
1786 module->removeOutputProfile(address);
1787 module->removeInputProfile(address);
1788 }
1789 return NO_ERROR;
1790}
1791
Eric Laurente552edb2014-03-10 17:42:56 -07001792
Eric Laurente0720872014-03-11 09:30:41 -07001793status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001794{
1795 const size_t SIZE = 256;
1796 char buffer[SIZE];
1797 String8 result;
1798
1799 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1800 result.append(buffer);
1801
1802 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1803 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001804 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001805 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001806 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001807 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001808 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001809 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001810 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001811 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001812 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001813 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001814 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001815 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001816 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001817 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001818 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001819 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001820 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001821
François Gaffie53615e22015-03-19 09:24:12 +01001822 mAvailableOutputDevices.dump(fd, String8("output"));
1823 mAvailableInputDevices.dump(fd, String8("input"));
1824 mHwModules.dump(fd);
1825 mOutputs.dump(fd);
1826 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001827 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001828 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001829 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001830
1831 return NO_ERROR;
1832}
1833
1834// This function checks for the parameters which can be offloaded.
1835// This can be enhanced depending on the capability of the DSP and policy
1836// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001837bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001838{
1839 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001840 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001841 offloadInfo.sample_rate, offloadInfo.channel_mask,
1842 offloadInfo.format,
1843 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1844 offloadInfo.has_video);
1845
1846 // Check if offload has been disabled
1847 char propValue[PROPERTY_VALUE_MAX];
1848 if (property_get("audio.offload.disable", propValue, "0")) {
1849 if (atoi(propValue) != 0) {
1850 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1851 return false;
1852 }
1853 }
1854
1855 // Check if stream type is music, then only allow offload as of now.
1856 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1857 {
1858 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1859 return false;
1860 }
1861
1862 //TODO: enable audio offloading with video when ready
1863 if (offloadInfo.has_video)
1864 {
1865 ALOGV("isOffloadSupported: has_video == true, returning false");
1866 return false;
1867 }
1868
1869 //If duration is less than minimum value defined in property, return false
1870 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1871 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1872 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1873 return false;
1874 }
1875 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1876 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1877 return false;
1878 }
1879
1880 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1881 // creating an offloaded track and tearing it down immediately after start when audioflinger
1882 // detects there is an active non offloadable effect.
1883 // FIXME: We should check the audio session here but we do not have it in this context.
1884 // This may prevent offloading in rare situations where effects are left active by apps
1885 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01001886 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001887 return false;
1888 }
1889
1890 // See if there is a profile to support this.
1891 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001892 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001893 offloadInfo.sample_rate,
1894 offloadInfo.format,
1895 offloadInfo.channel_mask,
1896 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001897 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1898 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001899}
1900
Eric Laurent6a94d692014-05-20 11:18:06 -07001901status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1902 audio_port_type_t type,
1903 unsigned int *num_ports,
1904 struct audio_port *ports,
1905 unsigned int *generation)
1906{
1907 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1908 generation == NULL) {
1909 return BAD_VALUE;
1910 }
1911 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1912 if (ports == NULL) {
1913 *num_ports = 0;
1914 }
1915
1916 size_t portsWritten = 0;
1917 size_t portsMax = *num_ports;
1918 *num_ports = 0;
1919 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1920 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1921 for (size_t i = 0;
1922 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
1923 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
1924 }
1925 *num_ports += mAvailableOutputDevices.size();
1926 }
1927 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1928 for (size_t i = 0;
1929 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
1930 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
1931 }
1932 *num_ports += mAvailableInputDevices.size();
1933 }
1934 }
1935 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
1936 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1937 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
1938 mInputs[i]->toAudioPort(&ports[portsWritten++]);
1939 }
1940 *num_ports += mInputs.size();
1941 }
1942 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07001943 size_t numOutputs = 0;
1944 for (size_t i = 0; i < mOutputs.size(); i++) {
1945 if (!mOutputs[i]->isDuplicated()) {
1946 numOutputs++;
1947 if (portsWritten < portsMax) {
1948 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
1949 }
1950 }
Eric Laurent6a94d692014-05-20 11:18:06 -07001951 }
Eric Laurent84c70242014-06-23 08:46:27 -07001952 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07001953 }
1954 }
1955 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07001956 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07001957 return NO_ERROR;
1958}
1959
1960status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
1961{
1962 return NO_ERROR;
1963}
1964
Eric Laurent6a94d692014-05-20 11:18:06 -07001965status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
1966 audio_patch_handle_t *handle,
1967 uid_t uid)
1968{
1969 ALOGV("createAudioPatch()");
1970
1971 if (handle == NULL || patch == NULL) {
1972 return BAD_VALUE;
1973 }
1974 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
1975
Eric Laurent874c42872014-08-08 15:13:39 -07001976 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
1977 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
1978 return BAD_VALUE;
1979 }
1980 // only one source per audio patch supported for now
1981 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07001982 return INVALID_OPERATION;
1983 }
Eric Laurent874c42872014-08-08 15:13:39 -07001984
1985 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07001986 return INVALID_OPERATION;
1987 }
Eric Laurent874c42872014-08-08 15:13:39 -07001988 for (size_t i = 0; i < patch->num_sinks; i++) {
1989 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
1990 return INVALID_OPERATION;
1991 }
1992 }
Eric Laurent6a94d692014-05-20 11:18:06 -07001993
1994 sp<AudioPatch> patchDesc;
1995 ssize_t index = mAudioPatches.indexOfKey(*handle);
1996
Eric Laurent6a94d692014-05-20 11:18:06 -07001997 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
1998 patch->sources[0].role,
1999 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002000#if LOG_NDEBUG == 0
2001 for (size_t i = 0; i < patch->num_sinks; i++) {
2002 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2003 patch->sinks[i].role,
2004 patch->sinks[i].type);
2005 }
2006#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002007
2008 if (index >= 0) {
2009 patchDesc = mAudioPatches.valueAt(index);
2010 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2011 mUidCached, patchDesc->mUid, uid);
2012 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2013 return INVALID_OPERATION;
2014 }
2015 } else {
2016 *handle = 0;
2017 }
2018
2019 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002020 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002021 if (outputDesc == NULL) {
2022 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2023 return BAD_VALUE;
2024 }
Eric Laurent84c70242014-06-23 08:46:27 -07002025 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2026 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002027 if (patchDesc != 0) {
2028 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2029 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2030 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2031 return BAD_VALUE;
2032 }
2033 }
Eric Laurent874c42872014-08-08 15:13:39 -07002034 DeviceVector devices;
2035 for (size_t i = 0; i < patch->num_sinks; i++) {
2036 // Only support mix to devices connection
2037 // TODO add support for mix to mix connection
2038 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2039 ALOGV("createAudioPatch() source mix but sink is not a device");
2040 return INVALID_OPERATION;
2041 }
2042 sp<DeviceDescriptor> devDesc =
2043 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2044 if (devDesc == 0) {
2045 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2046 return BAD_VALUE;
2047 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002048
François Gaffie53615e22015-03-19 09:24:12 +01002049 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002050 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002051 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002052 NULL, // updatedSamplingRate
2053 patch->sources[0].format,
2054 patch->sources[0].channel_mask,
2055 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2056 ALOGV("createAudioPatch() profile not supported for device %08x", devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002057 return INVALID_OPERATION;
2058 }
2059 devices.add(devDesc);
2060 }
2061 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002062 return INVALID_OPERATION;
2063 }
Eric Laurent874c42872014-08-08 15:13:39 -07002064
Eric Laurent6a94d692014-05-20 11:18:06 -07002065 // TODO: reconfigure output format and channels here
2066 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002067 devices.types(), outputDesc->mIoHandle);
2068 setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002069 index = mAudioPatches.indexOfKey(*handle);
2070 if (index >= 0) {
2071 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2072 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2073 }
2074 patchDesc = mAudioPatches.valueAt(index);
2075 patchDesc->mUid = uid;
2076 ALOGV("createAudioPatch() success");
2077 } else {
2078 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2079 return INVALID_OPERATION;
2080 }
2081 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2082 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2083 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002084 // only one sink supported when connecting an input device to a mix
2085 if (patch->num_sinks > 1) {
2086 return INVALID_OPERATION;
2087 }
François Gaffie53615e22015-03-19 09:24:12 +01002088 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002089 if (inputDesc == NULL) {
2090 return BAD_VALUE;
2091 }
2092 if (patchDesc != 0) {
2093 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2094 return BAD_VALUE;
2095 }
2096 }
2097 sp<DeviceDescriptor> devDesc =
2098 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2099 if (devDesc == 0) {
2100 return BAD_VALUE;
2101 }
2102
François Gaffie53615e22015-03-19 09:24:12 +01002103 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002104 devDesc->mAddress,
2105 patch->sinks[0].sample_rate,
2106 NULL, /*updatedSampleRate*/
2107 patch->sinks[0].format,
2108 patch->sinks[0].channel_mask,
2109 // FIXME for the parameter type,
2110 // and the NONE
2111 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002112 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002113 return INVALID_OPERATION;
2114 }
2115 // TODO: reconfigure output format and channels here
2116 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002117 devDesc->type(), inputDesc->mIoHandle);
2118 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002119 index = mAudioPatches.indexOfKey(*handle);
2120 if (index >= 0) {
2121 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2122 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2123 }
2124 patchDesc = mAudioPatches.valueAt(index);
2125 patchDesc->mUid = uid;
2126 ALOGV("createAudioPatch() success");
2127 } else {
2128 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2129 return INVALID_OPERATION;
2130 }
2131 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2132 // device to device connection
2133 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002134 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002135 return BAD_VALUE;
2136 }
2137 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002138 sp<DeviceDescriptor> srcDeviceDesc =
2139 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002140 if (srcDeviceDesc == 0) {
2141 return BAD_VALUE;
2142 }
Eric Laurent874c42872014-08-08 15:13:39 -07002143
Eric Laurent6a94d692014-05-20 11:18:06 -07002144 //update source and sink with our own data as the data passed in the patch may
2145 // be incomplete.
2146 struct audio_patch newPatch = *patch;
2147 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002148
Eric Laurent874c42872014-08-08 15:13:39 -07002149 for (size_t i = 0; i < patch->num_sinks; i++) {
2150 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2151 ALOGV("createAudioPatch() source device but one sink is not a device");
2152 return INVALID_OPERATION;
2153 }
2154
2155 sp<DeviceDescriptor> sinkDeviceDesc =
2156 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2157 if (sinkDeviceDesc == 0) {
2158 return BAD_VALUE;
2159 }
2160 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2161
2162 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
2163 // only one sink supported when connected devices across HW modules
2164 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002165 return INVALID_OPERATION;
2166 }
Eric Laurent874c42872014-08-08 15:13:39 -07002167 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002168 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002169 // if the sink device is reachable via an opened output stream, request to go via
2170 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002171 audio_io_handle_t output = selectOutput(outputs,
2172 AUDIO_OUTPUT_FLAG_NONE,
2173 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002174 if (output != AUDIO_IO_HANDLE_NONE) {
2175 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2176 if (outputDesc->isDuplicated()) {
2177 return INVALID_OPERATION;
2178 }
2179 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
2180 newPatch.num_sources = 2;
2181 }
Eric Laurent83b88082014-06-20 18:31:16 -07002182 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002183 }
2184 // TODO: check from routing capabilities in config file and other conflicting patches
2185
2186 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2187 if (index >= 0) {
2188 afPatchHandle = patchDesc->mAfPatchHandle;
2189 }
2190
2191 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2192 &afPatchHandle,
2193 0);
2194 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2195 status, afPatchHandle);
2196 if (status == NO_ERROR) {
2197 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002198 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002199 addAudioPatch(patchDesc->mHandle, patchDesc);
2200 } else {
2201 patchDesc->mPatch = newPatch;
2202 }
2203 patchDesc->mAfPatchHandle = afPatchHandle;
2204 *handle = patchDesc->mHandle;
2205 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002206 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002207 } else {
2208 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2209 status);
2210 return INVALID_OPERATION;
2211 }
2212 } else {
2213 return BAD_VALUE;
2214 }
2215 } else {
2216 return BAD_VALUE;
2217 }
2218 return NO_ERROR;
2219}
2220
2221status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2222 uid_t uid)
2223{
2224 ALOGV("releaseAudioPatch() patch %d", handle);
2225
2226 ssize_t index = mAudioPatches.indexOfKey(handle);
2227
2228 if (index < 0) {
2229 return BAD_VALUE;
2230 }
2231 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2232 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2233 mUidCached, patchDesc->mUid, uid);
2234 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2235 return INVALID_OPERATION;
2236 }
2237
2238 struct audio_patch *patch = &patchDesc->mPatch;
2239 patchDesc->mUid = mUidCached;
2240 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002241 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002242 if (outputDesc == NULL) {
2243 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2244 return BAD_VALUE;
2245 }
2246
2247 setOutputDevice(outputDesc->mIoHandle,
2248 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2249 true,
2250 0,
2251 NULL);
2252 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2253 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002254 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002255 if (inputDesc == NULL) {
2256 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2257 return BAD_VALUE;
2258 }
2259 setInputDevice(inputDesc->mIoHandle,
2260 getNewInputDevice(inputDesc->mIoHandle),
2261 true,
2262 NULL);
2263 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2264 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2265 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2266 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2267 status, patchDesc->mAfPatchHandle);
2268 removeAudioPatch(patchDesc->mHandle);
2269 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002270 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002271 } else {
2272 return BAD_VALUE;
2273 }
2274 } else {
2275 return BAD_VALUE;
2276 }
2277 return NO_ERROR;
2278}
2279
2280status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2281 struct audio_patch *patches,
2282 unsigned int *generation)
2283{
François Gaffie53615e22015-03-19 09:24:12 +01002284 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002285 return BAD_VALUE;
2286 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002287 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002288 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002289}
2290
Eric Laurente1715a42014-05-20 11:30:42 -07002291status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002292{
Eric Laurente1715a42014-05-20 11:30:42 -07002293 ALOGV("setAudioPortConfig()");
2294
2295 if (config == NULL) {
2296 return BAD_VALUE;
2297 }
2298 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2299 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002300 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2301 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002302 }
2303
Eric Laurenta121f902014-06-03 13:32:54 -07002304 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002305 if (config->type == AUDIO_PORT_TYPE_MIX) {
2306 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
François Gaffie53615e22015-03-19 09:24:12 +01002307 sp<AudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002308 if (outputDesc == NULL) {
2309 return BAD_VALUE;
2310 }
Eric Laurent84c70242014-06-23 08:46:27 -07002311 ALOG_ASSERT(!outputDesc->isDuplicated(),
2312 "setAudioPortConfig() called on duplicated output %d",
2313 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002314 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002315 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002316 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002317 if (inputDesc == NULL) {
2318 return BAD_VALUE;
2319 }
Eric Laurenta121f902014-06-03 13:32:54 -07002320 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002321 } else {
2322 return BAD_VALUE;
2323 }
2324 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2325 sp<DeviceDescriptor> deviceDesc;
2326 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2327 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2328 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2329 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2330 } else {
2331 return BAD_VALUE;
2332 }
2333 if (deviceDesc == NULL) {
2334 return BAD_VALUE;
2335 }
Eric Laurenta121f902014-06-03 13:32:54 -07002336 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002337 } else {
2338 return BAD_VALUE;
2339 }
2340
Eric Laurenta121f902014-06-03 13:32:54 -07002341 struct audio_port_config backupConfig;
2342 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2343 if (status == NO_ERROR) {
2344 struct audio_port_config newConfig;
2345 audioPortConfig->toAudioPortConfig(&newConfig, config);
2346 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002347 }
Eric Laurenta121f902014-06-03 13:32:54 -07002348 if (status != NO_ERROR) {
2349 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002350 }
Eric Laurente1715a42014-05-20 11:30:42 -07002351
2352 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002353}
2354
2355void AudioPolicyManager::clearAudioPatches(uid_t uid)
2356{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002357 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002358 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2359 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002360 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002361 }
2362 }
2363}
2364
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002365status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2366 audio_io_handle_t *ioHandle,
2367 audio_devices_t *device)
2368{
2369 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2370 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002371 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002372
François Gaffiedf372692015-03-19 10:43:27 +01002373 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002374}
2375
Eric Laurente552edb2014-03-10 17:42:56 -07002376// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002377// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002378// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002379uint32_t AudioPolicyManager::nextAudioPortGeneration()
2380{
2381 return android_atomic_inc(&mAudioPortGeneration);
2382}
2383
Eric Laurente0720872014-03-11 09:30:41 -07002384AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002385 :
2386#ifdef AUDIO_POLICY_TEST
2387 Thread(false),
2388#endif //AUDIO_POLICY_TEST
2389 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurente552edb2014-03-10 17:42:56 -07002390 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002391 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002392 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002393 mAudioPortGeneration(1),
2394 mBeaconMuteRefCount(0),
2395 mBeaconPlayingRefCount(0),
2396 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002397{
François Gaffie2110e042015-03-24 08:41:51 +01002398 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2399 if (!engineInstance) {
2400 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2401 return;
2402 }
2403 // Retrieve the Policy Manager Interface
2404 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2405 if (mEngine == NULL) {
2406 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2407 return;
2408 }
2409 mEngine->setObserver(this);
2410 status_t status = mEngine->initCheck();
2411 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2412
Eric Laurent6a94d692014-05-20 11:18:06 -07002413 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002414 mpClientInterface = clientInterface;
2415
Paul McLeane743a472015-01-28 11:07:31 -08002416 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002417 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2418 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2419 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2420 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2421 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2422 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002423 ALOGE("could not load audio policy configuration file, setting defaults");
2424 defaultAudioPolicyConfig();
2425 }
2426 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002427 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002428
François Gaffie2110e042015-03-24 08:41:51 +01002429 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2430 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002431
2432 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002433 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2434 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002435 for (size_t i = 0; i < mHwModules.size(); i++) {
2436 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2437 if (mHwModules[i]->mHandle == 0) {
2438 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2439 continue;
2440 }
2441 // open all output streams needed to access attached devices
2442 // except for direct output streams that are only opened when they are actually
2443 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002444 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002445 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2446 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002447 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002448
Eric Laurent3a4311c2014-03-17 12:00:47 -07002449 if (outProfile->mSupportedDevices.isEmpty()) {
2450 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2451 continue;
2452 }
2453
Eric Laurentd78f1532014-09-16 16:38:20 -07002454 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2455 continue;
2456 }
Eric Laurent83b88082014-06-20 18:31:16 -07002457 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002458 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2459 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002460 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002461 // chose first device present in mSupportedDevices also part of
2462 // outputDeviceTypes
2463 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002464 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002465 if ((profileType & outputDeviceTypes) != 0) {
2466 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002467 }
Eric Laurente552edb2014-03-10 17:42:56 -07002468 }
2469 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002470 if ((profileType & outputDeviceTypes) == 0) {
2471 continue;
2472 }
2473 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
2474
2475 outputDesc->mDevice = profileType;
2476 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2477 config.sample_rate = outputDesc->mSamplingRate;
2478 config.channel_mask = outputDesc->mChannelMask;
2479 config.format = outputDesc->mFormat;
2480 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2481 status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle,
2482 &output,
2483 &config,
2484 &outputDesc->mDevice,
2485 String8(""),
2486 &outputDesc->mLatency,
2487 outputDesc->mFlags);
2488
2489 if (status != NO_ERROR) {
2490 ALOGW("Cannot open output stream for device %08x on hw module %s",
2491 outputDesc->mDevice,
2492 mHwModules[i]->mName);
2493 } else {
2494 outputDesc->mSamplingRate = config.sample_rate;
2495 outputDesc->mChannelMask = config.channel_mask;
2496 outputDesc->mFormat = config.format;
2497
2498 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002499 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002500 ssize_t index =
2501 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2502 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002503 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2504 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002505 }
2506 }
2507 if (mPrimaryOutput == 0 &&
2508 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2509 mPrimaryOutput = output;
2510 }
2511 addOutput(output, outputDesc);
2512 setOutputDevice(output,
2513 outputDesc->mDevice,
2514 true);
2515 }
Eric Laurente552edb2014-03-10 17:42:56 -07002516 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002517 // open input streams needed to access attached devices to validate
2518 // mAvailableInputDevices list
2519 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2520 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002521 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002522
Eric Laurent3a4311c2014-03-17 12:00:47 -07002523 if (inProfile->mSupportedDevices.isEmpty()) {
2524 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2525 continue;
2526 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002527 // chose first device present in mSupportedDevices also part of
2528 // inputDeviceTypes
2529 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2530 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002531 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002532 if (profileType & inputDeviceTypes) {
2533 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002534 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002535 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002536 if ((profileType & inputDeviceTypes) == 0) {
2537 continue;
2538 }
2539 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2540
2541 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2542 inputDesc->mDevice = profileType;
2543
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002544 // find the address
2545 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2546 // the inputs vector must be of size 1, but we don't want to crash here
2547 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2548 : String8("");
2549 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2550 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2551
Eric Laurentd78f1532014-09-16 16:38:20 -07002552 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2553 config.sample_rate = inputDesc->mSamplingRate;
2554 config.channel_mask = inputDesc->mChannelMask;
2555 config.format = inputDesc->mFormat;
2556 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
2557 status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle,
2558 &input,
2559 &config,
2560 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002561 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002562 AUDIO_SOURCE_MIC,
2563 AUDIO_INPUT_FLAG_NONE);
2564
2565 if (status == NO_ERROR) {
2566 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002567 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002568 ssize_t index =
2569 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2570 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002571 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2572 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002573 }
2574 }
2575 mpClientInterface->closeInput(input);
2576 } else {
2577 ALOGW("Cannot open input stream for device %08x on hw module %s",
2578 inputDesc->mDevice,
2579 mHwModules[i]->mName);
2580 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002581 }
2582 }
2583 // make sure all attached devices have been allocated a unique ID
2584 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002585 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002586 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002587 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2588 continue;
2589 }
François Gaffie2110e042015-03-24 08:41:51 +01002590 // The device is now validated and can be appended to the available devices of the engine
2591 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2592 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002593 i++;
2594 }
2595 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002596 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002597 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002598 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2599 continue;
2600 }
François Gaffie2110e042015-03-24 08:41:51 +01002601 // The device is now validated and can be appended to the available devices of the engine
2602 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2603 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002604 i++;
2605 }
2606 // make sure default device is reachable
2607 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002608 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002609 }
Eric Laurente552edb2014-03-10 17:42:56 -07002610
2611 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2612
2613 updateDevicesAndOutputs();
2614
2615#ifdef AUDIO_POLICY_TEST
2616 if (mPrimaryOutput != 0) {
2617 AudioParameter outputCmd = AudioParameter();
2618 outputCmd.addInt(String8("set_id"), 0);
2619 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2620
2621 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2622 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002623 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2624 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002625 mTestLatencyMs = 0;
2626 mCurOutput = 0;
2627 mDirectOutput = false;
2628 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2629 mTestOutputs[i] = 0;
2630 }
2631
2632 const size_t SIZE = 256;
2633 char buffer[SIZE];
2634 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2635 run(buffer, ANDROID_PRIORITY_AUDIO);
2636 }
2637#endif //AUDIO_POLICY_TEST
2638}
2639
Eric Laurente0720872014-03-11 09:30:41 -07002640AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002641{
2642#ifdef AUDIO_POLICY_TEST
2643 exit();
2644#endif //AUDIO_POLICY_TEST
2645 for (size_t i = 0; i < mOutputs.size(); i++) {
2646 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002647 }
2648 for (size_t i = 0; i < mInputs.size(); i++) {
2649 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002650 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002651 mAvailableOutputDevices.clear();
2652 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002653 mOutputs.clear();
2654 mInputs.clear();
2655 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002656}
2657
Eric Laurente0720872014-03-11 09:30:41 -07002658status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002659{
2660 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2661}
2662
2663#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002664bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002665{
2666 ALOGV("entering threadLoop()");
2667 while (!exitPending())
2668 {
2669 String8 command;
2670 int valueInt;
2671 String8 value;
2672
2673 Mutex::Autolock _l(mLock);
2674 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2675
2676 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2677 AudioParameter param = AudioParameter(command);
2678
2679 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2680 valueInt != 0) {
2681 ALOGV("Test command %s received", command.string());
2682 String8 target;
2683 if (param.get(String8("target"), target) != NO_ERROR) {
2684 target = "Manager";
2685 }
2686 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2687 param.remove(String8("test_cmd_policy_output"));
2688 mCurOutput = valueInt;
2689 }
2690 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2691 param.remove(String8("test_cmd_policy_direct"));
2692 if (value == "false") {
2693 mDirectOutput = false;
2694 } else if (value == "true") {
2695 mDirectOutput = true;
2696 }
2697 }
2698 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2699 param.remove(String8("test_cmd_policy_input"));
2700 mTestInput = valueInt;
2701 }
2702
2703 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2704 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002705 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002706 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002707 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002708 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002709 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002710 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002711 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002712 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002713 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002714 if (target == "Manager") {
2715 mTestFormat = format;
2716 } else if (mTestOutputs[mCurOutput] != 0) {
2717 AudioParameter outputParam = AudioParameter();
2718 outputParam.addInt(String8("format"), format);
2719 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2720 }
2721 }
2722 }
2723 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2724 param.remove(String8("test_cmd_policy_channels"));
2725 int channels = 0;
2726
2727 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002728 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002729 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002730 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002731 }
2732 if (channels != 0) {
2733 if (target == "Manager") {
2734 mTestChannels = channels;
2735 } else if (mTestOutputs[mCurOutput] != 0) {
2736 AudioParameter outputParam = AudioParameter();
2737 outputParam.addInt(String8("channels"), channels);
2738 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2739 }
2740 }
2741 }
2742 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2743 param.remove(String8("test_cmd_policy_sampleRate"));
2744 if (valueInt >= 0 && valueInt <= 96000) {
2745 int samplingRate = valueInt;
2746 if (target == "Manager") {
2747 mTestSamplingRate = samplingRate;
2748 } else if (mTestOutputs[mCurOutput] != 0) {
2749 AudioParameter outputParam = AudioParameter();
2750 outputParam.addInt(String8("sampling_rate"), samplingRate);
2751 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2752 }
2753 }
2754 }
2755
2756 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2757 param.remove(String8("test_cmd_policy_reopen"));
2758
Eric Laurent1f2f2232014-06-02 12:01:23 -07002759 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07002760 mpClientInterface->closeOutput(mPrimaryOutput);
2761
2762 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
2763
François Gaffie53615e22015-03-19 09:24:12 +01002764 removeOutput(mPrimaryOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07002765 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07002766 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002767 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2768 config.sample_rate = outputDesc->mSamplingRate;
2769 config.channel_mask = outputDesc->mChannelMask;
2770 config.format = outputDesc->mFormat;
2771 status_t status = mpClientInterface->openOutput(moduleHandle,
2772 &mPrimaryOutput,
2773 &config,
2774 &outputDesc->mDevice,
2775 String8(""),
2776 &outputDesc->mLatency,
2777 outputDesc->mFlags);
2778 if (status != NO_ERROR) {
2779 ALOGE("Failed to reopen hardware output stream, "
2780 "samplingRate: %d, format %d, channels %d",
2781 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07002782 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002783 outputDesc->mSamplingRate = config.sample_rate;
2784 outputDesc->mChannelMask = config.channel_mask;
2785 outputDesc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07002786 AudioParameter outputCmd = AudioParameter();
2787 outputCmd.addInt(String8("set_id"), 0);
2788 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2789 addOutput(mPrimaryOutput, outputDesc);
2790 }
2791 }
2792
2793
2794 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2795 }
2796 }
2797 return false;
2798}
2799
Eric Laurente0720872014-03-11 09:30:41 -07002800void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002801{
2802 {
2803 AutoMutex _l(mLock);
2804 requestExit();
2805 mWaitWorkCV.signal();
2806 }
2807 requestExitAndWait();
2808}
2809
Eric Laurente0720872014-03-11 09:30:41 -07002810int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002811{
2812 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2813 if (output == mTestOutputs[i]) return i;
2814 }
2815 return 0;
2816}
2817#endif //AUDIO_POLICY_TEST
2818
2819// ---
2820
Eric Laurent1f2f2232014-06-02 12:01:23 -07002821void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002822{
François Gaffie98cc1912015-03-18 17:52:40 +01002823 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07002824 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002825 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002826}
2827
François Gaffie53615e22015-03-19 09:24:12 +01002828void AudioPolicyManager::removeOutput(audio_io_handle_t output)
2829{
2830 mOutputs.removeItem(output);
2831}
2832
Eric Laurent1f2f2232014-06-02 12:01:23 -07002833void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002834{
François Gaffie98cc1912015-03-18 17:52:40 +01002835 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07002836 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002837 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002838}
Eric Laurente552edb2014-03-10 17:42:56 -07002839
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002840void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08002841 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002842 const String8 address /*in*/,
2843 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08002844 sp<DeviceDescriptor> devDesc =
2845 desc->mProfile->mSupportedDevices.getDevice(device, address);
2846 if (devDesc != 0) {
2847 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
2848 desc->mIoHandle, address.string());
2849 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002850 }
2851}
2852
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002853status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01002854 audio_policy_dev_state_t state,
2855 SortedVector<audio_io_handle_t>& outputs,
2856 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002857{
François Gaffie53615e22015-03-19 09:24:12 +01002858 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002859 sp<AudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002860 // erase all current sample rates, formats and channel masks
2861 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07002862
Eric Laurent3b73df72014-03-11 09:06:29 -07002863 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002864 // first list already open outputs that can be routed to this device
2865 for (size_t i = 0; i < mOutputs.size(); i++) {
2866 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002867 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01002868 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002869 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2870 outputs.add(mOutputs.keyAt(i));
2871 } else {
2872 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08002873 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002874 }
Eric Laurente552edb2014-03-10 17:42:56 -07002875 }
2876 }
2877 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002878 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002879 for (size_t i = 0; i < mHwModules.size(); i++)
2880 {
2881 if (mHwModules[i]->mHandle == 0) {
2882 continue;
2883 }
2884 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2885 {
Eric Laurent275e8e92014-11-30 15:14:47 -08002886 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
2887 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01002888 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08002889 address == profile->mSupportedDevices[0]->mAddress) {
2890 profiles.add(profile);
2891 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
2892 }
Eric Laurente552edb2014-03-10 17:42:56 -07002893 }
2894 }
2895 }
2896
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002897 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
2898
Eric Laurente552edb2014-03-10 17:42:56 -07002899 if (profiles.isEmpty() && outputs.isEmpty()) {
2900 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2901 return BAD_VALUE;
2902 }
2903
2904 // open outputs for matching profiles if needed. Direct outputs are also opened to
2905 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2906 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002907 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002908
2909 // nothing to do if one output is already opened for this profile
2910 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002911 for (j = 0; j < outputs.size(); j++) {
2912 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07002913 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002914 // matching profile: save the sample rates, format and channel masks supported
2915 // by the profile in our device descriptor
2916 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07002917 break;
2918 }
2919 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002920 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002921 continue;
2922 }
2923
Eric Laurent83b88082014-06-20 18:31:16 -07002924 ALOGV("opening output for device %08x with params %s profile %p",
2925 device, address.string(), profile.get());
Eric Laurente552edb2014-03-10 17:42:56 -07002926 desc = new AudioOutputDescriptor(profile);
2927 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002928 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2929 config.sample_rate = desc->mSamplingRate;
2930 config.channel_mask = desc->mChannelMask;
2931 config.format = desc->mFormat;
2932 config.offload_info.sample_rate = desc->mSamplingRate;
2933 config.offload_info.channel_mask = desc->mChannelMask;
2934 config.offload_info.format = desc->mFormat;
2935 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2936 status_t status = mpClientInterface->openOutput(profile->mModule->mHandle,
2937 &output,
2938 &config,
2939 &desc->mDevice,
2940 address,
2941 &desc->mLatency,
2942 desc->mFlags);
2943 if (status == NO_ERROR) {
2944 desc->mSamplingRate = config.sample_rate;
2945 desc->mChannelMask = config.channel_mask;
2946 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07002947
Eric Laurentd4692962014-05-05 18:13:44 -07002948 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07002949 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002950 char *param = audio_device_address_to_parameter(device, address);
2951 mpClientInterface->setParameters(output, String8(param));
2952 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07002953 }
2954
Eric Laurentd4692962014-05-05 18:13:44 -07002955 // Here is where we step through and resolve any "dynamic" fields
2956 String8 reply;
2957 char *value;
2958 if (profile->mSamplingRates[0] == 0) {
2959 reply = mpClientInterface->getParameters(output,
2960 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002961 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07002962 reply.string());
2963 value = strpbrk((char *)reply.string(), "=");
2964 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002965 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002966 }
Eric Laurentd4692962014-05-05 18:13:44 -07002967 }
2968 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2969 reply = mpClientInterface->getParameters(output,
2970 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002971 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07002972 reply.string());
2973 value = strpbrk((char *)reply.string(), "=");
2974 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002975 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002976 }
Eric Laurentd4692962014-05-05 18:13:44 -07002977 }
2978 if (profile->mChannelMasks[0] == 0) {
2979 reply = mpClientInterface->getParameters(output,
2980 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002981 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07002982 reply.string());
2983 value = strpbrk((char *)reply.string(), "=");
2984 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002985 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002986 }
Eric Laurentd4692962014-05-05 18:13:44 -07002987 }
2988 if (((profile->mSamplingRates[0] == 0) &&
2989 (profile->mSamplingRates.size() < 2)) ||
2990 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
2991 (profile->mFormats.size() < 2)) ||
2992 ((profile->mChannelMasks[0] == 0) &&
2993 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002994 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07002995 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002996 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07002997 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
2998 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002999 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003000 config.sample_rate = profile->pickSamplingRate();
3001 config.channel_mask = profile->pickChannelMask();
3002 config.format = profile->pickFormat();
3003 config.offload_info.sample_rate = config.sample_rate;
3004 config.offload_info.channel_mask = config.channel_mask;
3005 config.offload_info.format = config.format;
3006 status = mpClientInterface->openOutput(profile->mModule->mHandle,
3007 &output,
3008 &config,
3009 &desc->mDevice,
3010 address,
3011 &desc->mLatency,
3012 desc->mFlags);
3013 if (status == NO_ERROR) {
3014 desc->mSamplingRate = config.sample_rate;
3015 desc->mChannelMask = config.channel_mask;
3016 desc->mFormat = config.format;
3017 } else {
3018 output = AUDIO_IO_HANDLE_NONE;
3019 }
Eric Laurentd4692962014-05-05 18:13:44 -07003020 }
3021
Eric Laurentcf2c0212014-07-25 16:20:43 -07003022 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003023 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003024 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003025 sp<AudioPolicyMix> policyMix;
3026 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003027 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3028 address.string());
3029 }
François Gaffie036e1e92015-03-19 10:16:24 +01003030 policyMix->setOutput(desc);
3031 desc->mPolicyMix = &(policyMix->getMix());
3032
Eric Laurentc722f302014-12-10 11:21:49 -08003033 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3034 // no duplicated output for direct outputs and
3035 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003036 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003037
Eric Laurentd4692962014-05-05 18:13:44 -07003038 // set initial stream volume for device
3039 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003040
Eric Laurentd4692962014-05-05 18:13:44 -07003041 //TODO: configure audio effect output stage here
3042
3043 // open a duplicating output thread for the new output and the primary output
3044 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
3045 mPrimaryOutput);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003046 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003047 // add duplicated output descriptor
Eric Laurentcf2c0212014-07-25 16:20:43 -07003048 sp<AudioOutputDescriptor> dupOutputDesc =
3049 new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07003050 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
3051 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
3052 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3053 dupOutputDesc->mFormat = desc->mFormat;
3054 dupOutputDesc->mChannelMask = desc->mChannelMask;
3055 dupOutputDesc->mLatency = desc->mLatency;
3056 addOutput(duplicatedOutput, dupOutputDesc);
3057 applyStreamVolumes(duplicatedOutput, device, 0, true);
3058 } else {
3059 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
3060 mPrimaryOutput, output);
3061 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003062 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003063 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003064 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003065 }
Eric Laurente552edb2014-03-10 17:42:56 -07003066 }
3067 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003068 } else {
3069 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003070 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003071 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003072 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003073 profiles.removeAt(profile_index);
3074 profile_index--;
3075 } else {
3076 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003077 devDesc->importAudioPort(profile);
3078
François Gaffie53615e22015-03-19 09:24:12 +01003079 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003080 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3081 device, address.string());
3082 setOutputDevice(output, device, true/*force*/, 0/*delay*/,
3083 NULL/*patch handle*/, address.string());
3084 }
Eric Laurente552edb2014-03-10 17:42:56 -07003085 ALOGV("checkOutputsForDevice(): adding output %d", output);
3086 }
3087 }
3088
3089 if (profiles.isEmpty()) {
3090 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3091 return BAD_VALUE;
3092 }
Eric Laurentd4692962014-05-05 18:13:44 -07003093 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003094 // check if one opened output is not needed any more after disconnecting one device
3095 for (size_t i = 0; i < mOutputs.size(); i++) {
3096 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003097 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003098 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003099 if (device_distinguishes_on_address(device) &&
Eric Laurent275e8e92014-11-30 15:14:47 -08003100 (desc->mProfile->mSupportedDevices.types() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003101 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurent275e8e92014-11-30 15:14:47 -08003102 } else if (!(desc->mProfile->mSupportedDevices.types()
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003103 & mAvailableOutputDevices.types())) {
3104 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3105 mOutputs.keyAt(i));
3106 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003107 }
Eric Laurente552edb2014-03-10 17:42:56 -07003108 }
3109 }
Eric Laurentd4692962014-05-05 18:13:44 -07003110 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003111 for (size_t i = 0; i < mHwModules.size(); i++)
3112 {
3113 if (mHwModules[i]->mHandle == 0) {
3114 continue;
3115 }
3116 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3117 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003118 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003119 if (profile->mSupportedDevices.types() & device) {
3120 ALOGV("checkOutputsForDevice(): "
3121 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003122 if (profile->mSamplingRates[0] == 0) {
3123 profile->mSamplingRates.clear();
3124 profile->mSamplingRates.add(0);
3125 }
3126 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3127 profile->mFormats.clear();
3128 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3129 }
3130 if (profile->mChannelMasks[0] == 0) {
3131 profile->mChannelMasks.clear();
3132 profile->mChannelMasks.add(0);
3133 }
3134 }
3135 }
3136 }
3137 }
3138 return NO_ERROR;
3139}
3140
Eric Laurentd4692962014-05-05 18:13:44 -07003141status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003142 audio_policy_dev_state_t state,
3143 SortedVector<audio_io_handle_t>& inputs,
3144 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003145{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003146 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003147 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3148 // first list already open inputs that can be routed to this device
3149 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3150 desc = mInputs.valueAt(input_index);
3151 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3152 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3153 inputs.add(mInputs.keyAt(input_index));
3154 }
3155 }
3156
3157 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003158 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003159 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3160 {
3161 if (mHwModules[module_idx]->mHandle == 0) {
3162 continue;
3163 }
3164 for (size_t profile_index = 0;
3165 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3166 profile_index++)
3167 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003168 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3169
3170 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003171 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003172 address == profile->mSupportedDevices[0]->mAddress) {
3173 profiles.add(profile);
3174 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3175 profile_index, module_idx);
3176 }
Eric Laurentd4692962014-05-05 18:13:44 -07003177 }
3178 }
3179 }
3180
3181 if (profiles.isEmpty() && inputs.isEmpty()) {
3182 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3183 return BAD_VALUE;
3184 }
3185
3186 // open inputs for matching profiles if needed. Direct inputs are also opened to
3187 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3188 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3189
Eric Laurent1c333e22014-05-20 10:48:17 -07003190 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003191 // nothing to do if one input is already opened for this profile
3192 size_t input_index;
3193 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3194 desc = mInputs.valueAt(input_index);
3195 if (desc->mProfile == profile) {
3196 break;
3197 }
3198 }
3199 if (input_index != mInputs.size()) {
3200 continue;
3201 }
3202
3203 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3204 desc = new AudioInputDescriptor(profile);
3205 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003206 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3207 config.sample_rate = desc->mSamplingRate;
3208 config.channel_mask = desc->mChannelMask;
3209 config.format = desc->mFormat;
3210 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3211 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
3212 &input,
3213 &config,
3214 &desc->mDevice,
3215 address,
3216 AUDIO_SOURCE_MIC,
3217 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003218
Eric Laurentcf2c0212014-07-25 16:20:43 -07003219 if (status == NO_ERROR) {
3220 desc->mSamplingRate = config.sample_rate;
3221 desc->mChannelMask = config.channel_mask;
3222 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003223
Eric Laurentd4692962014-05-05 18:13:44 -07003224 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003225 char *param = audio_device_address_to_parameter(device, address);
3226 mpClientInterface->setParameters(input, String8(param));
3227 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003228 }
3229
3230 // Here is where we step through and resolve any "dynamic" fields
3231 String8 reply;
3232 char *value;
3233 if (profile->mSamplingRates[0] == 0) {
3234 reply = mpClientInterface->getParameters(input,
3235 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3236 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3237 reply.string());
3238 value = strpbrk((char *)reply.string(), "=");
3239 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003240 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003241 }
3242 }
3243 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3244 reply = mpClientInterface->getParameters(input,
3245 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3246 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3247 value = strpbrk((char *)reply.string(), "=");
3248 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003249 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003250 }
3251 }
3252 if (profile->mChannelMasks[0] == 0) {
3253 reply = mpClientInterface->getParameters(input,
3254 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3255 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3256 reply.string());
3257 value = strpbrk((char *)reply.string(), "=");
3258 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003259 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003260 }
3261 }
3262 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3263 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3264 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3265 ALOGW("checkInputsForDevice() direct input missing param");
3266 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003267 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003268 }
3269
3270 if (input != 0) {
3271 addInput(input, desc);
3272 }
3273 } // endif input != 0
3274
Eric Laurentcf2c0212014-07-25 16:20:43 -07003275 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003276 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003277 profiles.removeAt(profile_index);
3278 profile_index--;
3279 } else {
3280 inputs.add(input);
3281 ALOGV("checkInputsForDevice(): adding input %d", input);
3282 }
3283 } // end scan profiles
3284
3285 if (profiles.isEmpty()) {
3286 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3287 return BAD_VALUE;
3288 }
3289 } else {
3290 // Disconnect
3291 // check if one opened input is not needed any more after disconnecting one device
3292 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3293 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003294 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3295 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003296 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3297 mInputs.keyAt(input_index));
3298 inputs.add(mInputs.keyAt(input_index));
3299 }
3300 }
3301 // Clear any profiles associated with the disconnected device.
3302 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3303 if (mHwModules[module_index]->mHandle == 0) {
3304 continue;
3305 }
3306 for (size_t profile_index = 0;
3307 profile_index < mHwModules[module_index]->mInputProfiles.size();
3308 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003309 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003310 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003311 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003312 profile_index, module_index);
3313 if (profile->mSamplingRates[0] == 0) {
3314 profile->mSamplingRates.clear();
3315 profile->mSamplingRates.add(0);
3316 }
3317 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3318 profile->mFormats.clear();
3319 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3320 }
3321 if (profile->mChannelMasks[0] == 0) {
3322 profile->mChannelMasks.clear();
3323 profile->mChannelMasks.add(0);
3324 }
3325 }
3326 }
3327 }
3328 } // end disconnect
3329
3330 return NO_ERROR;
3331}
3332
3333
Eric Laurente0720872014-03-11 09:30:41 -07003334void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003335{
3336 ALOGV("closeOutput(%d)", output);
3337
Eric Laurent1f2f2232014-06-02 12:01:23 -07003338 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003339 if (outputDesc == NULL) {
3340 ALOGW("closeOutput() unknown output %d", output);
3341 return;
3342 }
François Gaffie036e1e92015-03-19 10:16:24 +01003343 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003344
Eric Laurente552edb2014-03-10 17:42:56 -07003345 // look for duplicated outputs connected to the output being removed.
3346 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003347 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003348 if (dupOutputDesc->isDuplicated() &&
3349 (dupOutputDesc->mOutput1 == outputDesc ||
3350 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003351 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003352 if (dupOutputDesc->mOutput1 == outputDesc) {
3353 outputDesc2 = dupOutputDesc->mOutput2;
3354 } else {
3355 outputDesc2 = dupOutputDesc->mOutput1;
3356 }
3357 // As all active tracks on duplicated output will be deleted,
3358 // and as they were also referenced on the other output, the reference
3359 // count for their stream type must be adjusted accordingly on
3360 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003361 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003362 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003363 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003364 }
3365 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3366 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3367
3368 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003369 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003370 }
3371 }
3372
Eric Laurent05b90f82014-08-27 15:32:29 -07003373 nextAudioPortGeneration();
3374
3375 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3376 if (index >= 0) {
3377 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3378 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3379 mAudioPatches.removeItemsAt(index);
3380 mpClientInterface->onAudioPatchListUpdate();
3381 }
3382
Eric Laurente552edb2014-03-10 17:42:56 -07003383 AudioParameter param;
3384 param.add(String8("closing"), String8("true"));
3385 mpClientInterface->setParameters(output, param.toString());
3386
3387 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003388 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003389 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003390}
3391
3392void AudioPolicyManager::closeInput(audio_io_handle_t input)
3393{
3394 ALOGV("closeInput(%d)", input);
3395
3396 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3397 if (inputDesc == NULL) {
3398 ALOGW("closeInput() unknown input %d", input);
3399 return;
3400 }
3401
Eric Laurent6a94d692014-05-20 11:18:06 -07003402 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003403
3404 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3405 if (index >= 0) {
3406 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3407 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3408 mAudioPatches.removeItemsAt(index);
3409 mpClientInterface->onAudioPatchListUpdate();
3410 }
3411
3412 mpClientInterface->closeInput(input);
3413 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003414}
3415
Eric Laurente0720872014-03-11 09:30:41 -07003416SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003417 AudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003418{
3419 SortedVector<audio_io_handle_t> outputs;
3420
3421 ALOGVV("getOutputsForDevice() device %04x", device);
3422 for (size_t i = 0; i < openOutputs.size(); i++) {
3423 ALOGVV("output %d isDuplicated=%d device=%04x",
3424 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3425 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3426 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3427 outputs.add(openOutputs.keyAt(i));
3428 }
3429 }
3430 return outputs;
3431}
3432
Eric Laurente0720872014-03-11 09:30:41 -07003433bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003434 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003435{
3436 if (outputs1.size() != outputs2.size()) {
3437 return false;
3438 }
3439 for (size_t i = 0; i < outputs1.size(); i++) {
3440 if (outputs1[i] != outputs2[i]) {
3441 return false;
3442 }
3443 }
3444 return true;
3445}
3446
Eric Laurente0720872014-03-11 09:30:41 -07003447void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003448{
3449 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3450 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3451 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3452 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3453
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003454 // also take into account external policy-related changes: add all outputs which are
3455 // associated with policies in the "before" and "after" output vectors
3456 ALOGVV("checkOutputForStrategy(): policy related outputs");
3457 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
3458 const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
3459 if (desc != 0 && desc->mPolicyMix != NULL) {
3460 srcOutputs.add(desc->mIoHandle);
3461 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3462 }
3463 }
3464 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
3465 const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3466 if (desc != 0 && desc->mPolicyMix != NULL) {
3467 dstOutputs.add(desc->mIoHandle);
3468 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3469 }
3470 }
3471
Eric Laurente552edb2014-03-10 17:42:56 -07003472 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3473 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3474 strategy, srcOutputs[0], dstOutputs[0]);
3475 // mute strategy while moving tracks from one output to another
3476 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003477 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003478 if (isStrategyActive(desc, strategy)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003479 setStrategyMute(strategy, true, srcOutputs[i]);
3480 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3481 }
3482 }
3483
3484 // Move effects associated to this strategy from previous output to new output
3485 if (strategy == STRATEGY_MEDIA) {
3486 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3487 SortedVector<audio_io_handle_t> moved;
3488 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003489 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3490 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3491 effectDesc->mIo != fxOutput) {
3492 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003493 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3494 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003495 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003496 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003497 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003498 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003499 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003500 }
3501 }
3502 }
3503 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003504 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003505 if (i == AUDIO_STREAM_PATCH) {
3506 continue;
3507 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003508 if (getStrategy((audio_stream_type_t)i) == strategy) {
3509 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003510 }
3511 }
3512 }
3513}
3514
Eric Laurente0720872014-03-11 09:30:41 -07003515void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003516{
François Gaffie2110e042015-03-24 08:41:51 +01003517 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003518 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003519 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003520 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003521 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003522 checkOutputForStrategy(STRATEGY_SONIFICATION);
3523 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003524 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003525 checkOutputForStrategy(STRATEGY_MEDIA);
3526 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003527 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003528}
3529
Eric Laurente0720872014-03-11 09:30:41 -07003530void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003531{
François Gaffie53615e22015-03-19 09:24:12 +01003532 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003533 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003534 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003535 return;
3536 }
3537
Eric Laurent3a4311c2014-03-17 12:00:47 -07003538 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003539 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3540 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3541 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003542 // suspend A2DP output if:
3543 // (NOT already suspended) &&
3544 // ((SCO device is connected &&
3545 // (forced usage for communication || for record is SCO))) ||
3546 // (phone state is ringing || in call)
3547 //
3548 // restore A2DP output if:
3549 // (Already suspended) &&
3550 // ((SCO device is NOT connected ||
3551 // (forced usage NOT for communication && NOT for record is SCO))) &&
3552 // (phone state is NOT ringing && NOT in call)
3553 //
3554 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003555 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003556 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3557 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3558 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3559 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003560
3561 mpClientInterface->restoreOutput(a2dpOutput);
3562 mA2dpSuspended = false;
3563 }
3564 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003565 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003566 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3567 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3568 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3569 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003570
3571 mpClientInterface->suspendOutput(a2dpOutput);
3572 mA2dpSuspended = true;
3573 }
3574 }
3575}
3576
Eric Laurent1c333e22014-05-20 10:48:17 -07003577audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003578{
3579 audio_devices_t device = AUDIO_DEVICE_NONE;
3580
Eric Laurent1f2f2232014-06-02 12:01:23 -07003581 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003582
3583 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3584 if (index >= 0) {
3585 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3586 if (patchDesc->mUid != mUidCached) {
3587 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3588 outputDesc->device(), outputDesc->mPatchHandle);
3589 return outputDesc->device();
3590 }
3591 }
3592
Eric Laurente552edb2014-03-10 17:42:56 -07003593 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003594 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003595 // use device for strategy enforced audible
3596 // 2: we are in call or the strategy phone is active on the output:
3597 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003598 // 3: the strategy for enforced audible is active but not enforced on the output:
3599 // use the device for strategy enforced audible
3600 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003601 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003602 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003603 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003604 // 6: the strategy accessibility is active on the output:
3605 // use device for strategy accessibility
3606 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003607 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003608 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003609 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003610 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003611 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003612 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003613 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003614 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3615 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003616 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003617 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003618 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003619 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003620 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003621 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003622 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003623 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003624 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003625 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003626 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003627 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003628 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003629 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003630 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003631 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003632 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003633 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003634 }
3635
Eric Laurent1c333e22014-05-20 10:48:17 -07003636 ALOGV("getNewOutputDevice() selected device %x", device);
3637 return device;
3638}
3639
3640audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3641{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003642 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003643
3644 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3645 if (index >= 0) {
3646 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3647 if (patchDesc->mUid != mUidCached) {
3648 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3649 inputDesc->mDevice, inputDesc->mPatchHandle);
3650 return inputDesc->mDevice;
3651 }
3652 }
3653
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003654 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003655
3656 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003657 return device;
3658}
3659
Eric Laurente0720872014-03-11 09:30:41 -07003660uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003661 return (uint32_t)getStrategy(stream);
3662}
3663
Eric Laurente0720872014-03-11 09:30:41 -07003664audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003665 // By checking the range of stream before calling getStrategy, we avoid
3666 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3667 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003668 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003669 return AUDIO_DEVICE_NONE;
3670 }
3671 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003672 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003673 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3674 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3675 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003676 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003677 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003678 devices = outputDesc->device();
3679 break;
3680 }
Eric Laurente552edb2014-03-10 17:42:56 -07003681 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003682
3683 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3684 and doesn't really need to.*/
3685 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3686 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3687 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3688 }
3689
Eric Laurente552edb2014-03-10 17:42:56 -07003690 return devices;
3691}
3692
François Gaffie2110e042015-03-24 08:41:51 +01003693routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3694{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003695 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003696 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003697}
3698
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003699uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3700 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003701 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3702 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3703 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003704 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3705 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3706 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003707 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003708 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003709}
3710
Eric Laurente0720872014-03-11 09:30:41 -07003711void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003712 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003713 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003714 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3715 updateDevicesAndOutputs();
3716 break;
3717 default:
3718 break;
3719 }
3720}
3721
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003722uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3723 switch(event) {
3724 case STARTING_OUTPUT:
3725 mBeaconMuteRefCount++;
3726 break;
3727 case STOPPING_OUTPUT:
3728 if (mBeaconMuteRefCount > 0) {
3729 mBeaconMuteRefCount--;
3730 }
3731 break;
3732 case STARTING_BEACON:
3733 mBeaconPlayingRefCount++;
3734 break;
3735 case STOPPING_BEACON:
3736 if (mBeaconPlayingRefCount > 0) {
3737 mBeaconPlayingRefCount--;
3738 }
3739 break;
3740 }
3741
3742 if (mBeaconMuteRefCount > 0) {
3743 // any playback causes beacon to be muted
3744 return setBeaconMute(true);
3745 } else {
3746 // no other playback: unmute when beacon starts playing, mute when it stops
3747 return setBeaconMute(mBeaconPlayingRefCount == 0);
3748 }
3749}
3750
3751uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3752 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3753 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3754 // keep track of muted state to avoid repeating mute/unmute operations
3755 if (mBeaconMuted != mute) {
3756 // mute/unmute AUDIO_STREAM_TTS on all outputs
3757 ALOGV("\t muting %d", mute);
3758 uint32_t maxLatency = 0;
3759 for (size_t i = 0; i < mOutputs.size(); i++) {
3760 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
3761 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
3762 desc->mIoHandle,
3763 0 /*delay*/, AUDIO_DEVICE_NONE);
3764 const uint32_t latency = desc->latency() * 2;
3765 if (latency > maxLatency) {
3766 maxLatency = latency;
3767 }
3768 }
3769 mBeaconMuted = mute;
3770 return maxLatency;
3771 }
3772 return 0;
3773}
3774
Eric Laurente0720872014-03-11 09:30:41 -07003775audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01003776 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003777{
Eric Laurente552edb2014-03-10 17:42:56 -07003778 if (fromCache) {
3779 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3780 strategy, mDeviceForStrategy[strategy]);
3781 return mDeviceForStrategy[strategy];
3782 }
François Gaffie2110e042015-03-24 08:41:51 +01003783 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07003784}
3785
Eric Laurente0720872014-03-11 09:30:41 -07003786void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003787{
3788 for (int i = 0; i < NUM_STRATEGIES; i++) {
3789 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3790 }
3791 mPreviousOutputs = mOutputs;
3792}
3793
Eric Laurent1f2f2232014-06-02 12:01:23 -07003794uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003795 audio_devices_t prevDevice,
3796 uint32_t delayMs)
3797{
3798 // mute/unmute strategies using an incompatible device combination
3799 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3800 // if unmuting, unmute only after the specified delay
3801 if (outputDesc->isDuplicated()) {
3802 return 0;
3803 }
3804
3805 uint32_t muteWaitMs = 0;
3806 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003807 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003808
3809 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3810 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurent31551f82014-10-10 18:21:56 -07003811 curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07003812 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3813 bool doMute = false;
3814
3815 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3816 doMute = true;
3817 outputDesc->mStrategyMutedByDevice[i] = true;
3818 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3819 doMute = true;
3820 outputDesc->mStrategyMutedByDevice[i] = false;
3821 }
Eric Laurent99401132014-05-07 19:48:15 -07003822 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003823 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003824 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003825 // skip output if it does not share any device with current output
3826 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3827 == AUDIO_DEVICE_NONE) {
3828 continue;
3829 }
3830 audio_io_handle_t curOutput = mOutputs.keyAt(j);
3831 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
3832 mute ? "muting" : "unmuting", i, curDevice, curOutput);
3833 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01003834 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003835 if (mute) {
3836 // FIXME: should not need to double latency if volume could be applied
3837 // immediately by the audioflinger mixer. We must account for the delay
3838 // between now and the next time the audioflinger thread for this output
3839 // will process a buffer (which corresponds to one buffer size,
3840 // usually 1/2 or 1/4 of the latency).
3841 if (muteWaitMs < desc->latency() * 2) {
3842 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003843 }
3844 }
3845 }
3846 }
3847 }
3848 }
3849
Eric Laurent99401132014-05-07 19:48:15 -07003850 // temporary mute output if device selection changes to avoid volume bursts due to
3851 // different per device volumes
3852 if (outputDesc->isActive() && (device != prevDevice)) {
3853 if (muteWaitMs < outputDesc->latency() * 2) {
3854 muteWaitMs = outputDesc->latency() * 2;
3855 }
3856 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01003857 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003858 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07003859 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07003860 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07003861 muteWaitMs *2, device);
3862 }
3863 }
3864 }
3865
Eric Laurente552edb2014-03-10 17:42:56 -07003866 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3867 if (muteWaitMs > delayMs) {
3868 muteWaitMs -= delayMs;
3869 usleep(muteWaitMs * 1000);
3870 return muteWaitMs;
3871 }
3872 return 0;
3873}
3874
Eric Laurente0720872014-03-11 09:30:41 -07003875uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07003876 audio_devices_t device,
3877 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003878 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003879 audio_patch_handle_t *patchHandle,
3880 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07003881{
3882 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003883 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003884 AudioParameter param;
3885 uint32_t muteWaitMs;
3886
3887 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003888 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
3889 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003890 return muteWaitMs;
3891 }
3892 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
3893 // output profile
3894 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003895 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003896 return 0;
3897 }
3898
3899 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07003900 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07003901
3902 audio_devices_t prevDevice = outputDesc->mDevice;
3903
3904 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
3905
3906 if (device != AUDIO_DEVICE_NONE) {
3907 outputDesc->mDevice = device;
3908 }
3909 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
3910
3911 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07003912 // the requested device is AUDIO_DEVICE_NONE
3913 // OR the requested device is the same as current device
3914 // AND force is not specified
3915 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07003916 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentb80a2a82014-10-27 16:07:59 -07003917 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force &&
3918 outputDesc->mPatchHandle != 0) {
3919 ALOGV("setOutputDevice() setting same device %04x or null device for output %d",
3920 device, output);
Eric Laurente552edb2014-03-10 17:42:56 -07003921 return muteWaitMs;
3922 }
3923
3924 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07003925
Eric Laurente552edb2014-03-10 17:42:56 -07003926 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07003927 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003928 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07003929 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003930 DeviceVector deviceList = (address == NULL) ?
3931 mAvailableOutputDevices.getDevicesFromType(device)
3932 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07003933 if (!deviceList.isEmpty()) {
3934 struct audio_patch patch;
3935 outputDesc->toAudioPortConfig(&patch.sources[0]);
3936 patch.num_sources = 1;
3937 patch.num_sinks = 0;
3938 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
3939 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07003940 patch.num_sinks++;
3941 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003942 ssize_t index;
3943 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3944 index = mAudioPatches.indexOfKey(*patchHandle);
3945 } else {
3946 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3947 }
3948 sp< AudioPatch> patchDesc;
3949 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3950 if (index >= 0) {
3951 patchDesc = mAudioPatches.valueAt(index);
3952 afPatchHandle = patchDesc->mAfPatchHandle;
3953 }
3954
Eric Laurent1c333e22014-05-20 10:48:17 -07003955 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003956 &afPatchHandle,
3957 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07003958 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
3959 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003960 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07003961 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003962 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01003963 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07003964 addAudioPatch(patchDesc->mHandle, patchDesc);
3965 } else {
3966 patchDesc->mPatch = patch;
3967 }
3968 patchDesc->mAfPatchHandle = afPatchHandle;
3969 patchDesc->mUid = mUidCached;
3970 if (patchHandle) {
3971 *patchHandle = patchDesc->mHandle;
3972 }
3973 outputDesc->mPatchHandle = patchDesc->mHandle;
3974 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003975 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003976 }
3977 }
bryant_liuf5e7e792014-08-19 20:07:05 +08003978
3979 // inform all input as well
3980 for (size_t i = 0; i < mInputs.size(); i++) {
3981 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01003982 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08003983 AudioParameter inputCmd = AudioParameter();
3984 ALOGV("%s: inform input %d of device:%d", __func__,
3985 inputDescriptor->mIoHandle, device);
3986 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
3987 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
3988 inputCmd.toString(),
3989 delayMs);
3990 }
3991 }
Eric Laurent1c333e22014-05-20 10:48:17 -07003992 }
Eric Laurente552edb2014-03-10 17:42:56 -07003993
3994 // update stream volumes according to new device
3995 applyStreamVolumes(output, device, delayMs);
3996
3997 return muteWaitMs;
3998}
3999
Eric Laurent1c333e22014-05-20 10:48:17 -07004000status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07004001 int delayMs,
4002 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004003{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004004 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004005 ssize_t index;
4006 if (patchHandle) {
4007 index = mAudioPatches.indexOfKey(*patchHandle);
4008 } else {
4009 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4010 }
4011 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004012 return INVALID_OPERATION;
4013 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004014 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4015 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004016 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4017 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004018 removeAudioPatch(patchDesc->mHandle);
4019 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004020 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004021 return status;
4022}
4023
4024status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4025 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004026 bool force,
4027 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004028{
4029 status_t status = NO_ERROR;
4030
Eric Laurent1f2f2232014-06-02 12:01:23 -07004031 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004032 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4033 inputDesc->mDevice = device;
4034
4035 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4036 if (!deviceList.isEmpty()) {
4037 struct audio_patch patch;
4038 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004039 // AUDIO_SOURCE_HOTWORD is for internal use only:
4040 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004041 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4042 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004043 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4044 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004045 patch.num_sinks = 1;
4046 //only one input device for now
4047 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004048 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004049 ssize_t index;
4050 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4051 index = mAudioPatches.indexOfKey(*patchHandle);
4052 } else {
4053 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4054 }
4055 sp< AudioPatch> patchDesc;
4056 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4057 if (index >= 0) {
4058 patchDesc = mAudioPatches.valueAt(index);
4059 afPatchHandle = patchDesc->mAfPatchHandle;
4060 }
4061
Eric Laurent1c333e22014-05-20 10:48:17 -07004062 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004063 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004064 0);
4065 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004066 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004067 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004068 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004069 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004070 addAudioPatch(patchDesc->mHandle, patchDesc);
4071 } else {
4072 patchDesc->mPatch = patch;
4073 }
4074 patchDesc->mAfPatchHandle = afPatchHandle;
4075 patchDesc->mUid = mUidCached;
4076 if (patchHandle) {
4077 *patchHandle = patchDesc->mHandle;
4078 }
4079 inputDesc->mPatchHandle = patchDesc->mHandle;
4080 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004081 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004082 }
4083 }
4084 }
4085 return status;
4086}
4087
Eric Laurent6a94d692014-05-20 11:18:06 -07004088status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4089 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004090{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004091 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004092 ssize_t index;
4093 if (patchHandle) {
4094 index = mAudioPatches.indexOfKey(*patchHandle);
4095 } else {
4096 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4097 }
4098 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004099 return INVALID_OPERATION;
4100 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004101 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4102 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004103 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4104 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004105 removeAudioPatch(patchDesc->mHandle);
4106 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004107 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004108 return status;
4109}
4110
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004111sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004112 String8 address,
4113 uint32_t& samplingRate,
4114 audio_format_t format,
4115 audio_channel_mask_t channelMask,
4116 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004117{
4118 // Choose an input profile based on the requested capture parameters: select the first available
4119 // profile supporting all requested parameters.
4120
4121 for (size_t i = 0; i < mHwModules.size(); i++)
4122 {
4123 if (mHwModules[i]->mHandle == 0) {
4124 continue;
4125 }
4126 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4127 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004128 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004129 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004130 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004131 &samplingRate /*updatedSamplingRate*/,
4132 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004133
Eric Laurente552edb2014-03-10 17:42:56 -07004134 return profile;
4135 }
4136 }
4137 }
4138 return NULL;
4139}
4140
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004141
4142audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004143 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004144{
François Gaffie036e1e92015-03-19 10:16:24 +01004145 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4146 audio_devices_t selectedDeviceFromMix =
4147 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004148
François Gaffie036e1e92015-03-19 10:16:24 +01004149 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4150 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004151 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004152 return getDeviceForInputSource(inputSource);
4153}
4154
4155audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4156{
François Gaffie2110e042015-03-24 08:41:51 +01004157 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004158}
4159
Eric Laurente0720872014-03-11 09:30:41 -07004160float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004161 int index,
4162 audio_io_handle_t output,
4163 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004164{
4165 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004166 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004167
4168 if (device == AUDIO_DEVICE_NONE) {
4169 device = outputDesc->device();
4170 }
François Gaffie2110e042015-03-24 08:41:51 +01004171 volume = mEngine->volIndexToAmpl(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004172
4173 // if a headset is connected, apply the following rules to ring tones and notifications
4174 // to avoid sound level bursts in user's ears:
4175 // - always attenuate ring tones and notifications volume by 6dB
4176 // - if music is playing, always limit the volume to current music volume,
4177 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004178 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004179 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4180 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4181 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4182 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4183 ((stream_strategy == STRATEGY_SONIFICATION)
4184 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004185 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004186 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004187 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4188 mStreams.canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004189 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4190 // when the phone is ringing we must consider that music could have been paused just before
4191 // by the music application and behave as if music was active if the last music track was
4192 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004193 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004194 mLimitRingtoneVolume) {
4195 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004196 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
4197 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004198 output,
4199 musicDevice);
4200 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4201 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4202 if (volume > minVol) {
4203 volume = minVol;
4204 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4205 }
4206 }
4207 }
4208
4209 return volume;
4210}
4211
Eric Laurente0720872014-03-11 09:30:41 -07004212status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004213 int index,
4214 audio_io_handle_t output,
4215 audio_devices_t device,
4216 int delayMs,
4217 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004218{
4219
4220 // do not change actual stream volume if the stream is muted
4221 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
4222 ALOGVV("checkAndSetVolume() stream %d muted count %d",
4223 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
4224 return NO_ERROR;
4225 }
François Gaffie2110e042015-03-24 08:41:51 +01004226 audio_policy_forced_cfg_t forceUseForComm =
4227 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004228 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004229 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4230 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004231 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004232 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004233 return INVALID_OPERATION;
4234 }
4235
4236 float volume = computeVolume(stream, index, output, device);
Eric Laurent275e8e92014-11-30 15:14:47 -08004237 // unit gain if rerouting to external policy
4238 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
4239 ssize_t index = mOutputs.indexOfKey(output);
4240 if (index >= 0) {
4241 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurentc722f302014-12-10 11:21:49 -08004242 if (outputDesc->mPolicyMix != NULL) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004243 ALOGV("max gain when rerouting for output=%d", output);
4244 volume = 1.0f;
4245 }
4246 }
4247
4248 }
Eric Laurente552edb2014-03-10 17:42:56 -07004249 // We actually change the volume if:
4250 // - the float value returned by computeVolume() changed
4251 // - the force flag is set
4252 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
4253 force) {
4254 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
4255 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
4256 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
4257 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07004258 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
4259 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004260 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004261 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 }
4263
Eric Laurent3b73df72014-03-11 09:06:29 -07004264 if (stream == AUDIO_STREAM_VOICE_CALL ||
4265 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004266 float voiceVolume;
4267 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004268 if (stream == AUDIO_STREAM_VOICE_CALL) {
François Gaffiedfd74092015-03-19 12:10:59 +01004269 voiceVolume = (float)index/(float)mStreams[stream].getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004270 } else {
4271 voiceVolume = 1.0;
4272 }
4273
4274 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
4275 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4276 mLastVoiceVolume = voiceVolume;
4277 }
4278 }
4279
4280 return NO_ERROR;
4281}
4282
Eric Laurente0720872014-03-11 09:30:41 -07004283void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
François Gaffie53615e22015-03-19 09:24:12 +01004284 audio_devices_t device,
4285 int delayMs,
4286 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004287{
4288 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
4289
Eric Laurent3b73df72014-03-11 09:06:29 -07004290 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004291 if (stream == AUDIO_STREAM_PATCH) {
4292 continue;
4293 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004294 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004295 mStreams[stream].getVolumeIndex(device),
4296 output,
4297 device,
4298 delayMs,
4299 force);
4300 }
4301}
4302
Eric Laurente0720872014-03-11 09:30:41 -07004303void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004304 bool on,
4305 audio_io_handle_t output,
4306 int delayMs,
4307 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004308{
4309 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004310 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004311 if (stream == AUDIO_STREAM_PATCH) {
4312 continue;
4313 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004314 if (getStrategy((audio_stream_type_t)stream) == strategy) {
4315 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004316 }
4317 }
4318}
4319
Eric Laurente0720872014-03-11 09:30:41 -07004320void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01004321 bool on,
4322 audio_io_handle_t output,
4323 int delayMs,
4324 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004325{
François Gaffiedfd74092015-03-19 12:10:59 +01004326 const StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07004327 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004328 if (device == AUDIO_DEVICE_NONE) {
4329 device = outputDesc->device();
4330 }
4331
4332 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
4333 stream, on, output, outputDesc->mMuteCount[stream], device);
4334
4335 if (on) {
4336 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004337 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004338 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004339 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004340 checkAndSetVolume(stream, 0, output, device, delayMs);
4341 }
4342 }
4343 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4344 outputDesc->mMuteCount[stream]++;
4345 } else {
4346 if (outputDesc->mMuteCount[stream] == 0) {
4347 ALOGV("setStreamMute() unmuting non muted stream!");
4348 return;
4349 }
4350 if (--outputDesc->mMuteCount[stream] == 0) {
4351 checkAndSetVolume(stream,
4352 streamDesc.getVolumeIndex(device),
4353 output,
4354 device,
4355 delayMs);
4356 }
4357 }
4358}
4359
Eric Laurente0720872014-03-11 09:30:41 -07004360void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004361 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004362{
4363 // if the stream pertains to sonification strategy and we are in call we must
4364 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4365 // in the device used for phone strategy and play the tone if the selected device does not
4366 // interfere with the device used for phone strategy
4367 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4368 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004369 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004370 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4371 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004372 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004373 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4374 stream, starting, outputDesc->mDevice, stateChange);
4375 if (outputDesc->mRefCount[stream]) {
4376 int muteCount = 1;
4377 if (stateChange) {
4378 muteCount = outputDesc->mRefCount[stream];
4379 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004380 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004381 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4382 for (int i = 0; i < muteCount; i++) {
4383 setStreamMute(stream, starting, mPrimaryOutput);
4384 }
4385 } else {
4386 ALOGV("handleIncallSonification() high visibility");
4387 if (outputDesc->device() &
4388 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4389 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4390 for (int i = 0; i < muteCount; i++) {
4391 setStreamMute(stream, starting, mPrimaryOutput);
4392 }
4393 }
4394 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004395 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4396 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004397 } else {
4398 mpClientInterface->stopTone();
4399 }
4400 }
4401 }
4402 }
4403}
4404
Eric Laurente0720872014-03-11 09:30:41 -07004405void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004406{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004407 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004408 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004409 sp<DeviceDescriptor> defaultInputDevice =
4410 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004411 mAvailableOutputDevices.add(mDefaultOutputDevice);
4412 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004413
4414 module = new HwModule("primary");
4415
Eric Laurent1afeecb2014-05-14 08:52:28 -07004416 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004417 profile->mSamplingRates.add(44100);
4418 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4419 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004420 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004421 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4422 module->mOutputProfiles.add(profile);
4423
Eric Laurent1afeecb2014-05-14 08:52:28 -07004424 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004425 profile->mSamplingRates.add(8000);
4426 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4427 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004428 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004429 module->mInputProfiles.add(profile);
4430
4431 mHwModules.add(module);
4432}
4433
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004434audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4435{
4436 // flags to stream type mapping
4437 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4438 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4439 }
4440 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4441 return AUDIO_STREAM_BLUETOOTH_SCO;
4442 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004443 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4444 return AUDIO_STREAM_TTS;
4445 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004446
4447 // usage to stream type mapping
4448 switch (attr->usage) {
4449 case AUDIO_USAGE_MEDIA:
4450 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004451 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4452 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004453 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004454 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4455 return AUDIO_STREAM_ALARM;
4456 }
4457 if (isStreamActive(AUDIO_STREAM_RING)) {
4458 return AUDIO_STREAM_RING;
4459 }
4460 if (isInCall()) {
4461 return AUDIO_STREAM_VOICE_CALL;
4462 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004463 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004464 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4465 return AUDIO_STREAM_SYSTEM;
4466 case AUDIO_USAGE_VOICE_COMMUNICATION:
4467 return AUDIO_STREAM_VOICE_CALL;
4468
4469 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4470 return AUDIO_STREAM_DTMF;
4471
4472 case AUDIO_USAGE_ALARM:
4473 return AUDIO_STREAM_ALARM;
4474 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4475 return AUDIO_STREAM_RING;
4476
4477 case AUDIO_USAGE_NOTIFICATION:
4478 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4479 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4480 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4481 case AUDIO_USAGE_NOTIFICATION_EVENT:
4482 return AUDIO_STREAM_NOTIFICATION;
4483
4484 case AUDIO_USAGE_UNKNOWN:
4485 default:
4486 return AUDIO_STREAM_MUSIC;
4487 }
4488}
Eric Laurente83b55d2014-11-14 10:06:21 -08004489
François Gaffie53615e22015-03-19 09:24:12 +01004490bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4491{
Eric Laurente83b55d2014-11-14 10:06:21 -08004492 // has flags that map to a strategy?
4493 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4494 return true;
4495 }
4496
4497 // has known usage?
4498 switch (paa->usage) {
4499 case AUDIO_USAGE_UNKNOWN:
4500 case AUDIO_USAGE_MEDIA:
4501 case AUDIO_USAGE_VOICE_COMMUNICATION:
4502 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4503 case AUDIO_USAGE_ALARM:
4504 case AUDIO_USAGE_NOTIFICATION:
4505 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4506 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4507 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4508 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4509 case AUDIO_USAGE_NOTIFICATION_EVENT:
4510 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4511 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4512 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4513 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004514 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004515 break;
4516 default:
4517 return false;
4518 }
4519 return true;
4520}
4521
François Gaffiead3183e2015-03-18 16:55:35 +01004522bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4523 routing_strategy strategy, uint32_t inPastMs,
4524 nsecs_t sysTime) const
4525{
4526 if ((sysTime == 0) && (inPastMs != 0)) {
4527 sysTime = systemTime();
4528 }
4529 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4530 if (i == AUDIO_STREAM_PATCH) {
4531 continue;
4532 }
4533 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4534 (NUM_STRATEGIES == strategy)) &&
4535 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4536 return true;
4537 }
4538 }
4539 return false;
4540}
4541
François Gaffie2110e042015-03-24 08:41:51 +01004542audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4543{
4544 return mEngine->getForceUse(usage);
4545}
4546
4547bool AudioPolicyManager::isInCall()
4548{
4549 return isStateInCall(mEngine->getPhoneState());
4550}
4551
4552bool AudioPolicyManager::isStateInCall(int state)
4553{
4554 return is_state_in_call(state);
4555}
4556
Eric Laurente552edb2014-03-10 17:42:56 -07004557}; // namespace android