blob: 17060e90af676e6c293f844830f89773eec9e1b9 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
François Gaffie2110e042015-03-24 08:41:51 +0100264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800305 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700306 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
307
308 // release existing RX patch if any
309 if (mCallRxPatch != 0) {
310 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
311 mCallRxPatch.clear();
312 }
313 // release TX patch if any
314 if (mCallTxPatch != 0) {
315 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
316 mCallTxPatch.clear();
317 }
318
319 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
320 // via setOutputDevice() on primary output.
321 // Otherwise, create two audio patches for TX and RX path.
322 if (availablePrimaryOutputDevices() & rxDevice) {
323 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
324 // If the TX device is also on the primary HW module, setOutputDevice() will take care
325 // of it due to legacy implementation. If not, create a patch.
326 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
327 == AUDIO_DEVICE_NONE) {
328 createTxPatch = true;
329 }
330 } else {
331 // create RX path audio patch
332 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
333 ALOG_ASSERT(!deviceList.isEmpty(),
334 "updateCallRouting() selected device not in output device list");
335 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
336 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
337 ALOG_ASSERT(!deviceList.isEmpty(),
338 "updateCallRouting() no telephony RX device");
339 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
340
341 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
342 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
343
344 // request to reuse existing output stream if one is already opened to reach the RX device
345 SortedVector<audio_io_handle_t> outputs =
346 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700347 audio_io_handle_t output = selectOutput(outputs,
348 AUDIO_OUTPUT_FLAG_NONE,
349 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700350 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 ALOG_ASSERT(!outputDesc->isDuplicated(),
353 "updateCallRouting() RX device output is duplicated");
354 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700355 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700356 patch.num_sources = 2;
357 }
358
359 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
360 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
361 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
362 status);
363 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100364 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700365 mCallRxPatch->mAfPatchHandle = afPatchHandle;
366 mCallRxPatch->mUid = mUidCached;
367 }
368 createTxPatch = true;
369 }
370 if (createTxPatch) {
371
372 struct audio_patch patch;
373 patch.num_sources = 1;
374 patch.num_sinks = 1;
375 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
376 ALOG_ASSERT(!deviceList.isEmpty(),
377 "updateCallRouting() selected device not in input device list");
378 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
379 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
381 ALOG_ASSERT(!deviceList.isEmpty(),
382 "updateCallRouting() no telephony TX device");
383 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
384 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
385
386 SortedVector<audio_io_handle_t> outputs =
387 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700388 audio_io_handle_t output = selectOutput(outputs,
389 AUDIO_OUTPUT_FLAG_NONE,
390 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700391 // request to reuse existing output stream if one is already opened to reach the TX
392 // path output device
393 if (output != AUDIO_IO_HANDLE_NONE) {
394 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
395 ALOG_ASSERT(!outputDesc->isDuplicated(),
396 "updateCallRouting() RX device output is duplicated");
397 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700398 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 patch.num_sources = 2;
400 }
401
402 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
403 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
404 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
405 status);
406 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100407 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 mCallTxPatch->mAfPatchHandle = afPatchHandle;
409 mCallTxPatch->mUid = mUidCached;
410 }
411 }
412}
413
Eric Laurente0720872014-03-11 09:30:41 -0700414void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700415{
416 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100417 // store previous phone state for management of sonification strategy below
418 int oldState = mEngine->getPhoneState();
419
420 if (mEngine->setPhoneState(state) != NO_ERROR) {
421 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700422 return;
423 }
François Gaffie2110e042015-03-24 08:41:51 +0100424 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700425 // if leaving call state, handle special case of active streams
426 // pertaining to sonification strategy see handleIncallSonification()
427 if (isInCall()) {
428 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (stream == AUDIO_STREAM_PATCH) {
431 continue;
432 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700433 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800435
436 // force reevaluating accessibility routing when call starts
437 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 /**
441 * Switching to or from incall state or switching between telephony and VoIP lead to force
442 * routing command.
443 */
444 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
445 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700446
447 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700448 checkA2dpSuspend();
449 checkOutputForAllStrategies();
450 updateDevicesAndOutputs();
451
Eric Laurentc75307b2015-03-17 15:29:32 -0700452 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -0700453
Eric Laurente552edb2014-03-10 17:42:56 -0700454 int delayMs = 0;
455 if (isStateInCall(state)) {
456 nsecs_t sysTime = systemTime();
457 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700458 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700459 // mute media and sonification strategies and delay device switch by the largest
460 // latency of any output where either strategy is active.
461 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100462 if ((isStrategyActive(desc, STRATEGY_MEDIA,
463 SONIFICATION_HEADSET_MUSIC_DELAY,
464 sysTime) ||
465 isStrategyActive(desc, STRATEGY_SONIFICATION,
466 SONIFICATION_HEADSET_MUSIC_DELAY,
467 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700468 (delayMs < (int)desc->latency()*2)) {
469 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700471 setStrategyMute(STRATEGY_MEDIA, true, desc);
472 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700473 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700474 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
475 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700476 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
477 }
478 }
479
Eric Laurentc2730ba2014-07-20 15:47:07 -0700480 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
481 // the device returned is not necessarily reachable via this output
482 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
483 // force routing command to audio hardware when ending call
484 // even if no device change is needed
485 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
486 rxDevice = hwOutputDesc->device();
487 }
Eric Laurente552edb2014-03-10 17:42:56 -0700488
Eric Laurentc2730ba2014-07-20 15:47:07 -0700489 if (state == AUDIO_MODE_IN_CALL) {
490 updateCallRouting(rxDevice, delayMs);
491 } else if (oldState == AUDIO_MODE_IN_CALL) {
492 if (mCallRxPatch != 0) {
493 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
494 mCallRxPatch.clear();
495 }
496 if (mCallTxPatch != 0) {
497 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
498 mCallTxPatch.clear();
499 }
500 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
501 } else {
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504 // if entering in call state, handle special case of active streams
505 // pertaining to sonification strategy see handleIncallSonification()
506 if (isStateInCall(state)) {
507 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700512 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700513 }
514 }
515
516 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700517 if (state == AUDIO_MODE_RINGTONE &&
518 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 mLimitRingtoneVolume = true;
520 } else {
521 mLimitRingtoneVolume = false;
522 }
523}
524
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700525audio_mode_t AudioPolicyManager::getPhoneState() {
526 return mEngine->getPhoneState();
527}
528
Eric Laurente0720872014-03-11 09:30:41 -0700529void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
François Gaffie2110e042015-03-24 08:41:51 +0100532 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700533
François Gaffie2110e042015-03-24 08:41:51 +0100534 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
535 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
536 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700537 }
François Gaffie2110e042015-03-24 08:41:51 +0100538 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
539 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
540 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700541
542 // check for device and output changes triggered by new force usage
543 checkA2dpSuspend();
544 checkOutputForAllStrategies();
545 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100546 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700547 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
548 updateCallRouting(newDevice);
549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700551 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
552 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
553 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
554 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700558 }
559 }
560
François Gaffie53615e22015-03-19 09:24:12 +0100561 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700563 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565
566}
567
Eric Laurente0720872014-03-11 09:30:41 -0700568void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
570 ALOGV("setSystemProperty() property %s, value %s", property, value);
571}
572
573// Find a direct output profile compatible with the parameters passed, even if the input flags do
574// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700576 audio_devices_t device,
577 uint32_t samplingRate,
578 audio_format_t format,
579 audio_channel_mask_t channelMask,
580 audio_output_flags_t flags)
581{
Eric Laurent861a6282015-05-18 15:40:16 -0700582 // only retain flags that will drive the direct output profile selection
583 // if explicitly requested
584 static const uint32_t kRelevantFlags =
585 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
586 flags =
587 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
588
589 sp<IOProfile> profile;
590
Eric Laurente552edb2014-03-10 17:42:56 -0700591 for (size_t i = 0; i < mHwModules.size(); i++) {
592 if (mHwModules[i]->mHandle == 0) {
593 continue;
594 }
595 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700596 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
597 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700598 samplingRate, NULL /*updatedSamplingRate*/,
599 format, NULL /*updatedFormat*/,
600 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700601 flags)) {
602 continue;
603 }
604 // reject profiles not corresponding to a device currently available
605 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
606 continue;
607 }
608 // if several profiles are compatible, give priority to one with offload capability
609 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
610 continue;
611 }
612 profile = curProfile;
613 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
614 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700615 }
Eric Laurente552edb2014-03-10 17:42:56 -0700616 }
617 }
Eric Laurent861a6282015-05-18 15:40:16 -0700618 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700619}
620
Eric Laurente0720872014-03-11 09:30:41 -0700621audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100622 uint32_t samplingRate,
623 audio_format_t format,
624 audio_channel_mask_t channelMask,
625 audio_output_flags_t flags,
626 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700627{
Eric Laurent3b73df72014-03-11 09:06:29 -0700628 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700629 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
630 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
631 device, stream, samplingRate, format, channelMask, flags);
632
Eric Laurente83b55d2014-11-14 10:06:21 -0800633 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
634 stream, samplingRate,format, channelMask,
635 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700636}
637
Eric Laurente83b55d2014-11-14 10:06:21 -0800638status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
639 audio_io_handle_t *output,
640 audio_session_t session,
641 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700642 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800643 uint32_t samplingRate,
644 audio_format_t format,
645 audio_channel_mask_t channelMask,
646 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700647 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800648 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700649{
Eric Laurente83b55d2014-11-14 10:06:21 -0800650 audio_attributes_t attributes;
651 if (attr != NULL) {
652 if (!isValidAttributes(attr)) {
653 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
654 attr->usage, attr->content_type, attr->flags,
655 attr->tags);
656 return BAD_VALUE;
657 }
658 attributes = *attr;
659 } else {
660 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
661 ALOGE("getOutputForAttr(): invalid stream type");
662 return BAD_VALUE;
663 }
664 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700665 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700666 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100667 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
668 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
669 if (!audio_is_linear_pcm(format)) {
670 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800671 }
François Gaffie036e1e92015-03-19 10:16:24 +0100672 *stream = streamTypefromAttributesInt(&attributes);
673 *output = desc->mIoHandle;
674 ALOGV("getOutputForAttr() returns output %d", *output);
675 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800676 }
677 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
678 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
679 return BAD_VALUE;
680 }
681
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700682 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
683 " session %d selectedDeviceId %d",
684 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
685 session, selectedDeviceId);
686
687 *stream = streamTypefromAttributesInt(&attributes);
688
689 // Explicit routing?
690 sp<DeviceDescriptor> deviceDesc;
691 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
692 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
693 deviceDesc = mAvailableOutputDevices[i];
694 break;
695 }
696 }
697 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700698
Eric Laurente83b55d2014-11-14 10:06:21 -0800699 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700700 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700701
Eric Laurente83b55d2014-11-14 10:06:21 -0800702 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700703 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
704 }
705
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700706 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707 device, samplingRate, format, channelMask, flags);
708
Eric Laurente83b55d2014-11-14 10:06:21 -0800709 *output = getOutputForDevice(device, session, *stream,
710 samplingRate, format, channelMask,
711 flags, offloadInfo);
712 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700713 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800714 return INVALID_OPERATION;
715 }
Paul McLeanaa981192015-03-21 09:55:15 -0700716
Eric Laurente83b55d2014-11-14 10:06:21 -0800717 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700718}
719
720audio_io_handle_t AudioPolicyManager::getOutputForDevice(
721 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800722 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700723 audio_stream_type_t stream,
724 uint32_t samplingRate,
725 audio_format_t format,
726 audio_channel_mask_t channelMask,
727 audio_output_flags_t flags,
728 const audio_offload_info_t *offloadInfo)
729{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700730 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700731 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700732 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700733
Eric Laurente552edb2014-03-10 17:42:56 -0700734#ifdef AUDIO_POLICY_TEST
735 if (mCurOutput != 0) {
736 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
737 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
738
739 if (mTestOutputs[mCurOutput] == 0) {
740 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700741 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
742 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700743 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700744 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700745 outputDesc->mFlags =
746 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700747 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700748 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
749 config.sample_rate = mTestSamplingRate;
750 config.channel_mask = mTestChannels;
751 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700752 if (offloadInfo != NULL) {
753 config.offload_info = *offloadInfo;
754 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700755 status = mpClientInterface->openOutput(0,
756 &mTestOutputs[mCurOutput],
757 &config,
758 &outputDesc->mDevice,
759 String8(""),
760 &outputDesc->mLatency,
761 outputDesc->mFlags);
762 if (status == NO_ERROR) {
763 outputDesc->mSamplingRate = config.sample_rate;
764 outputDesc->mFormat = config.format;
765 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700766 AudioParameter outputCmd = AudioParameter();
767 outputCmd.addInt(String8("set_id"),mCurOutput);
768 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
769 addOutput(mTestOutputs[mCurOutput], outputDesc);
770 }
771 }
772 return mTestOutputs[mCurOutput];
773 }
774#endif //AUDIO_POLICY_TEST
775
776 // open a direct output if required by specified parameters
777 //force direct flag if offload flag is set: offloading implies a direct output stream
778 // and all common behaviors are driven by checking only the direct flag
779 // this should normally be set appropriately in the policy configuration file
780 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700781 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700782 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700783 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
784 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
785 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800786 // only allow deep buffering for music stream type
787 if (stream != AUDIO_STREAM_MUSIC) {
788 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
789 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700790 if (stream == AUDIO_STREAM_TTS) {
791 flags = AUDIO_OUTPUT_FLAG_TTS;
792 }
Eric Laurente552edb2014-03-10 17:42:56 -0700793
Eric Laurentb732cf52014-09-24 19:08:21 -0700794 sp<IOProfile> profile;
795
796 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700797 // and not explicitly requested
798 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
799 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700800 audio_channel_count_from_out_mask(channelMask) <= 2) {
801 goto non_direct_output;
802 }
803
Eric Laurente552edb2014-03-10 17:42:56 -0700804 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
805 // creating an offloaded track and tearing it down immediately after start when audioflinger
806 // detects there is an active non offloadable effect.
807 // FIXME: We should check the audio session here but we do not have it in this context.
808 // This may prevent offloading in rare situations where effects are left active by apps
809 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700810
Eric Laurente552edb2014-03-10 17:42:56 -0700811 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100812 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700813 profile = getProfileForDirectOutput(device,
814 samplingRate,
815 format,
816 channelMask,
817 (audio_output_flags_t)flags);
818 }
819
Eric Laurent1c333e22014-05-20 10:48:17 -0700820 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700821 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700822
823 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700824 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700825 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
826 outputDesc = desc;
827 // reuse direct output if currently open and configured with same parameters
828 if ((samplingRate == outputDesc->mSamplingRate) &&
829 (format == outputDesc->mFormat) &&
830 (channelMask == outputDesc->mChannelMask)) {
831 outputDesc->mDirectOpenCount++;
832 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
833 return mOutputs.keyAt(i);
834 }
835 }
836 }
837 // close direct output if currently open and configured with different parameters
838 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700839 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700840 }
Eric Laurent861a6282015-05-18 15:40:16 -0700841
842 // if the selected profile is offloaded and no offload info was specified,
843 // create a default one
844 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
845 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
846 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
847 defaultOffloadInfo.sample_rate = samplingRate;
848 defaultOffloadInfo.channel_mask = channelMask;
849 defaultOffloadInfo.format = format;
850 defaultOffloadInfo.stream_type = stream;
851 defaultOffloadInfo.bit_rate = 0;
852 defaultOffloadInfo.duration_us = -1;
853 defaultOffloadInfo.has_video = true; // conservative
854 defaultOffloadInfo.is_streaming = true; // likely
855 offloadInfo = &defaultOffloadInfo;
856 }
857
Eric Laurentc75307b2015-03-17 15:29:32 -0700858 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700859 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700860 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700861 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700862 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
863 config.sample_rate = samplingRate;
864 config.channel_mask = channelMask;
865 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700866 if (offloadInfo != NULL) {
867 config.offload_info = *offloadInfo;
868 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700869 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700870 &output,
871 &config,
872 &outputDesc->mDevice,
873 String8(""),
874 &outputDesc->mLatency,
875 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700876
877 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700878 if (status != NO_ERROR ||
879 (samplingRate != 0 && samplingRate != config.sample_rate) ||
880 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
881 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700882 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
883 "format %d %d, channelMask %04x %04x", output, samplingRate,
884 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
885 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700886 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700887 mpClientInterface->closeOutput(output);
888 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800889 // fall back to mixer output if possible when the direct output could not be open
890 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
891 goto non_direct_output;
892 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700893 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700894 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700895 outputDesc->mSamplingRate = config.sample_rate;
896 outputDesc->mChannelMask = config.channel_mask;
897 outputDesc->mFormat = config.format;
898 outputDesc->mRefCount[stream] = 0;
899 outputDesc->mStopTime[stream] = 0;
900 outputDesc->mDirectOpenCount = 1;
901
Eric Laurente552edb2014-03-10 17:42:56 -0700902 audio_io_handle_t srcOutput = getOutputForEffect();
903 addOutput(output, outputDesc);
904 audio_io_handle_t dstOutput = getOutputForEffect();
905 if (dstOutput == output) {
906 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
907 }
908 mPreviousOutputs = mOutputs;
909 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700910 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700911 return output;
912 }
913
Eric Laurentb732cf52014-09-24 19:08:21 -0700914non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700915 // ignoring channel mask due to downmix capability in mixer
916
917 // open a non direct output
918
919 // for non direct outputs, only PCM is supported
920 if (audio_is_linear_pcm(format)) {
921 // get which output is suitable for the specified stream. The actual
922 // routing change will happen when startOutput() will be called
923 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
924
Eric Laurent8838a382014-09-08 16:44:28 -0700925 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
926 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
927 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700928 }
929 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
930 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
931
Paul McLeanaa981192015-03-21 09:55:15 -0700932 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700933
934 return output;
935}
936
Eric Laurente0720872014-03-11 09:30:41 -0700937audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700938 audio_output_flags_t flags,
939 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700940{
941 // select one output among several that provide a path to a particular device or set of
942 // devices (the list was previously build by getOutputsForDevice()).
943 // The priority is as follows:
944 // 1: the output with the highest number of requested policy flags
945 // 2: the primary output
946 // 3: the first output in the list
947
948 if (outputs.size() == 0) {
949 return 0;
950 }
951 if (outputs.size() == 1) {
952 return outputs[0];
953 }
954
955 int maxCommonFlags = 0;
956 audio_io_handle_t outputFlags = 0;
957 audio_io_handle_t outputPrimary = 0;
958
959 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700960 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700961 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700962 // if a valid format is specified, skip output if not compatible
963 if (format != AUDIO_FORMAT_INVALID) {
964 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
965 if (format != outputDesc->mFormat) {
966 continue;
967 }
968 } else if (!audio_is_linear_pcm(format)) {
969 continue;
970 }
971 }
972
Eric Laurent3b73df72014-03-11 09:06:29 -0700973 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700974 if (commonFlags > maxCommonFlags) {
975 outputFlags = outputs[i];
976 maxCommonFlags = commonFlags;
977 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
978 }
979 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
980 outputPrimary = outputs[i];
981 }
982 }
983 }
984
985 if (outputFlags != 0) {
986 return outputFlags;
987 }
988 if (outputPrimary != 0) {
989 return outputPrimary;
990 }
991
992 return outputs[0];
993}
994
Eric Laurente0720872014-03-11 09:30:41 -0700995status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700996 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800997 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700998{
Paul McLeanaa981192015-03-21 09:55:15 -0700999 ALOGV("startOutput() output %d, stream %d, session %d",
1000 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001001 ssize_t index = mOutputs.indexOfKey(output);
1002 if (index < 0) {
1003 ALOGW("startOutput() unknown output %d", output);
1004 return BAD_VALUE;
1005 }
1006
Eric Laurentc75307b2015-03-17 15:29:32 -07001007 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1008
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001009 // Routing?
1010 mOutputRoutes.incRouteActivity(session);
1011
Eric Laurentc75307b2015-03-17 15:29:32 -07001012 audio_devices_t newDevice;
1013 if (outputDesc->mPolicyMix != NULL) {
1014 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001015 } else if (mOutputRoutes.hasRouteChanged(session)) {
1016 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001017 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001018 } else {
1019 newDevice = AUDIO_DEVICE_NONE;
1020 }
1021
1022 uint32_t delayMs = 0;
1023
Eric Laurentc75307b2015-03-17 15:29:32 -07001024 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1025
1026 if (status != NO_ERROR) {
1027 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001028 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001029 }
1030 // Automatically enable the remote submix input when output is started on a re routing mix
1031 // of type MIX_TYPE_RECORDERS
1032 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1033 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1034 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1035 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1036 outputDesc->mPolicyMix->mRegistrationId,
1037 "remote-submix");
1038 }
1039
1040 if (delayMs != 0) {
1041 usleep(delayMs * 1000);
1042 }
1043
1044 return status;
1045}
1046
1047status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1048 audio_stream_type_t stream,
1049 audio_devices_t device,
1050 uint32_t *delayMs)
1051{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001052 // cannot start playback of STREAM_TTS if any other output is being used
1053 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001054
1055 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001056 if (stream == AUDIO_STREAM_TTS) {
1057 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001058 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001059 return INVALID_OPERATION;
1060 } else {
1061 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1062 }
1063 } else {
1064 // some playback other than beacon starts
1065 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1066 }
1067
Eric Laurente552edb2014-03-10 17:42:56 -07001068 // increment usage count for this stream on the requested output:
1069 // NOTE that the usage count is the same for duplicated output and hardware output which is
1070 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1071 outputDesc->changeRefCount(stream, 1);
1072
Eric Laurent493404d2015-04-21 15:07:36 -07001073 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001074 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001075 if (device == AUDIO_DEVICE_NONE) {
1076 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001077 }
Eric Laurente552edb2014-03-10 17:42:56 -07001078 routing_strategy strategy = getStrategy(stream);
1079 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001080 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1081 (beaconMuteLatency > 0);
1082 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001083 bool force = false;
1084 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001085 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001086 if (desc != outputDesc) {
1087 // force a device change if any other output is managed by the same hw
1088 // module and has a current device selection that differs from selected device.
1089 // In this case, the audio HAL must receive the new device selection so that it can
1090 // change the device currently selected by the other active output.
1091 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001092 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001093 force = true;
1094 }
1095 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001096 // a notification so that audio focus effect can propagate, or that a mute/unmute
1097 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001098 uint32_t latency = desc->latency();
1099 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1100 waitMs = latency;
1101 }
1102 }
1103 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001104 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001105
1106 // handle special case for sonification while in call
1107 if (isInCall()) {
1108 handleIncallSonification(stream, true, false);
1109 }
1110
1111 // apply volume rules for current stream and device if necessary
1112 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001113 mStreams.valueFor(stream).getVolumeIndex(device),
1114 outputDesc,
1115 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001116
1117 // update the outputs if starting an output with a stream that can affect notification
1118 // routing
1119 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001120
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001121 // force reevaluating accessibility routing when ringtone or alarm starts
1122 if (strategy == STRATEGY_SONIFICATION) {
1123 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1124 }
Eric Laurente552edb2014-03-10 17:42:56 -07001125 }
1126 return NO_ERROR;
1127}
1128
1129
Eric Laurente0720872014-03-11 09:30:41 -07001130status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001131 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001132 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001133{
1134 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1135 ssize_t index = mOutputs.indexOfKey(output);
1136 if (index < 0) {
1137 ALOGW("stopOutput() unknown output %d", output);
1138 return BAD_VALUE;
1139 }
1140
Eric Laurentc75307b2015-03-17 15:29:32 -07001141 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001142
Eric Laurentc75307b2015-03-17 15:29:32 -07001143 if (outputDesc->mRefCount[stream] == 1) {
1144 // Automatically disable the remote submix input when output is stopped on a
1145 // re routing mix of type MIX_TYPE_RECORDERS
1146 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1147 outputDesc->mPolicyMix != NULL &&
1148 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1149 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1150 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1151 outputDesc->mPolicyMix->mRegistrationId,
1152 "remote-submix");
1153 }
1154 }
1155
1156 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001157 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001158 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001159 int activityCount = mOutputRoutes.decRouteActivity(session);
1160 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1161
1162 if (forceDeviceUpdate) {
1163 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1164 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001165 }
1166
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001167 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001168}
1169
1170status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001171 audio_stream_type_t stream,
1172 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001173{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001174 // always handle stream stop, check which stream type is stopping
1175 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1176
Eric Laurente552edb2014-03-10 17:42:56 -07001177 // handle special case for sonification while in call
1178 if (isInCall()) {
1179 handleIncallSonification(stream, false, false);
1180 }
1181
1182 if (outputDesc->mRefCount[stream] > 0) {
1183 // decrement usage count of this stream on the output
1184 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001185
Eric Laurente552edb2014-03-10 17:42:56 -07001186 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001187 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001188 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001189 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001190 // delay the device switch by twice the latency because stopOutput() is executed when
1191 // the track stop() command is received and at that time the audio track buffer can
1192 // still contain data that needs to be drained. The latency only covers the audio HAL
1193 // and kernel buffers. Also the latency does not always include additional delay in the
1194 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001195 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001196
1197 // force restoring the device selection on other active outputs if it differs from the
1198 // one being selected for this output
1199 for (size_t i = 0; i < mOutputs.size(); i++) {
1200 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001201 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001202 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001203 desc->isActive() &&
1204 outputDesc->sharesHwModuleWith(desc) &&
1205 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001206 setOutputDevice(desc,
1207 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001208 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001209 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001210 }
1211 }
1212 // update the outputs if stopping one with a stream that can affect notification routing
1213 handleNotificationRoutingForStream(stream);
1214 }
1215 return NO_ERROR;
1216 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001217 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001218 return INVALID_OPERATION;
1219 }
1220}
1221
Eric Laurente83b55d2014-11-14 10:06:21 -08001222void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001223 audio_stream_type_t stream __unused,
1224 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001225{
1226 ALOGV("releaseOutput() %d", output);
1227 ssize_t index = mOutputs.indexOfKey(output);
1228 if (index < 0) {
1229 ALOGW("releaseOutput() releasing unknown output %d", output);
1230 return;
1231 }
1232
1233#ifdef AUDIO_POLICY_TEST
1234 int testIndex = testOutputIndex(output);
1235 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001236 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001237 if (outputDesc->isActive()) {
1238 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001239 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001240 mTestOutputs[testIndex] = 0;
1241 }
1242 return;
1243 }
1244#endif //AUDIO_POLICY_TEST
1245
Paul McLeanaa981192015-03-21 09:55:15 -07001246 // Routing
1247 mOutputRoutes.removeRoute(session);
1248
Eric Laurentc75307b2015-03-17 15:29:32 -07001249 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001250 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001251 if (desc->mDirectOpenCount <= 0) {
1252 ALOGW("releaseOutput() invalid open count %d for output %d",
1253 desc->mDirectOpenCount, output);
1254 return;
1255 }
1256 if (--desc->mDirectOpenCount == 0) {
1257 closeOutput(output);
1258 // If effects where present on the output, audioflinger moved them to the primary
1259 // output by default: move them back to the appropriate output.
1260 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001261 if (dstOutput != mPrimaryOutput->mIoHandle) {
1262 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1263 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001264 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001265 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001266 }
1267 }
1268}
1269
1270
Eric Laurentcaf7f482014-11-25 17:50:47 -08001271status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1272 audio_io_handle_t *input,
1273 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001274 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001275 uint32_t samplingRate,
1276 audio_format_t format,
1277 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001278 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001279 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001280 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001281{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001282 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1283 "session %d, flags %#x",
1284 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001285
Eric Laurentcaf7f482014-11-25 17:50:47 -08001286 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001287 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001288 audio_devices_t device;
1289 // handle legacy remote submix case where the address was not always specified
1290 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001291 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001292 audio_source_t inputSource = attr->source;
1293 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001294 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001295
Eric Laurentc447ded2015-01-06 08:47:05 -08001296 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1297 inputSource = AUDIO_SOURCE_MIC;
1298 }
1299 halInputSource = inputSource;
1300
Paul McLean466dc8e2015-04-17 13:15:36 -06001301 // Explicit routing?
1302 sp<DeviceDescriptor> deviceDesc;
1303 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1304 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1305 deviceDesc = mAvailableInputDevices[i];
1306 break;
1307 }
1308 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001309 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001310
Eric Laurentc447ded2015-01-06 08:47:05 -08001311 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001312 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001313 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001314 if (ret != NO_ERROR) {
1315 return ret;
1316 }
1317 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001318 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1319 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001320 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001321 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001322 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001323 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001324 return BAD_VALUE;
1325 }
Eric Laurentc722f302014-12-10 11:21:49 -08001326 if (policyMix != NULL) {
1327 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001328 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1329 // there is an external policy, but this input is attached to a mix of recorders,
1330 // meaning it receives audio injected into the framework, so the recorder doesn't
1331 // know about it and is therefore considered "legacy"
1332 *inputType = API_INPUT_LEGACY;
1333 } else {
1334 // recording a mix of players defined by an external policy, we're rerouting for
1335 // an external policy
1336 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1337 }
Eric Laurentc722f302014-12-10 11:21:49 -08001338 } else if (audio_is_remote_submix_device(device)) {
1339 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001340 *inputType = API_INPUT_MIX_CAPTURE;
1341 } else {
1342 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001343 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001344 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001345 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001346 case AUDIO_SOURCE_VOICE_UPLINK:
1347 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1348 break;
1349 case AUDIO_SOURCE_VOICE_DOWNLINK:
1350 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1351 break;
1352 case AUDIO_SOURCE_VOICE_CALL:
1353 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1354 break;
1355 default:
1356 break;
1357 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001358 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001359 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1360 if (index >= 0) {
1361 *input = mSoundTriggerSessions.valueFor(session);
1362 isSoundTrigger = true;
1363 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1364 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1365 } else {
1366 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1367 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001368 }
1369 }
1370
Andy Hungf129b032015-04-07 13:45:50 -07001371 // find a compatible input profile (not necessarily identical in parameters)
1372 sp<IOProfile> profile;
1373 // samplingRate and flags may be updated by getInputProfile
1374 uint32_t profileSamplingRate = samplingRate;
1375 audio_format_t profileFormat = format;
1376 audio_channel_mask_t profileChannelMask = channelMask;
1377 audio_input_flags_t profileFlags = flags;
1378 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001379 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001380 profileSamplingRate, profileFormat, profileChannelMask,
1381 profileFlags);
1382 if (profile != 0) {
1383 break; // success
1384 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1385 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1386 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001387 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1388 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001389 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001390 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001391 }
Eric Laurente552edb2014-03-10 17:42:56 -07001392 }
1393
Eric Laurent322b4d22015-04-03 15:57:54 -07001394 if (profile->getModuleHandle() == 0) {
1395 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001396 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001397 }
1398
1399 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001400 config.sample_rate = profileSamplingRate;
1401 config.channel_mask = profileChannelMask;
1402 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001403
Eric Laurent322b4d22015-04-03 15:57:54 -07001404 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001405 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001406 &config,
1407 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001408 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001409 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001410 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001411
1412 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001413 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001414 (profileSamplingRate != config.sample_rate) ||
1415 (profileFormat != config.format) ||
1416 (profileChannelMask != config.channel_mask)) {
1417 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1418 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001419 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001420 if (*input != AUDIO_IO_HANDLE_NONE) {
1421 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001422 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001423 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001424 }
1425
Eric Laurent1f2f2232014-06-02 12:01:23 -07001426 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001427 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001428 inputDesc->mRefCount = 0;
1429 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001430 inputDesc->mSamplingRate = profileSamplingRate;
1431 inputDesc->mFormat = profileFormat;
1432 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001433 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001434 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001435 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001436 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001437
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001438 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001439
Eric Laurentcaf7f482014-11-25 17:50:47 -08001440 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001441 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001442
Eric Laurentcaf7f482014-11-25 17:50:47 -08001443 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001444}
1445
Eric Laurent4dc68062014-07-28 17:26:49 -07001446status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1447 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001448{
1449 ALOGV("startInput() input %d", input);
1450 ssize_t index = mInputs.indexOfKey(input);
1451 if (index < 0) {
1452 ALOGW("startInput() unknown input %d", input);
1453 return BAD_VALUE;
1454 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001455 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001456
Eric Laurentc722f302014-12-10 11:21:49 -08001457 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001458 if (index < 0) {
1459 ALOGW("startInput() unknown session %d on input %d", session, input);
1460 return BAD_VALUE;
1461 }
1462
Glenn Kasten74a8e252014-07-24 14:09:55 -07001463 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001464 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001465
1466 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001467 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001468 if (activeInput != 0 && activeInput != input) {
1469
1470 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1471 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001472 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001473 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001474 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001475 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1476 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001477 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001478 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001479 return INVALID_OPERATION;
1480 }
1481 }
1482 }
1483
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001484 // Routing?
1485 mInputRoutes.incRouteActivity(session);
1486
Paul McLean466dc8e2015-04-17 13:15:36 -06001487 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001488 // if input maps to a dynamic policy with an activity listener, notify of state change
1489 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001490 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001491 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1492 MIX_STATE_MIXING);
1493 }
1494
François Gaffie53615e22015-03-19 09:24:12 +01001495 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001496 SoundTrigger::setCaptureState(true);
1497 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001498 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001499
Eric Laurentc722f302014-12-10 11:21:49 -08001500 // automatically enable the remote submix output when input is started if not
1501 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001502 // For remote submix (a virtual device), we open only one input per capture request.
1503 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001504 String8 address = String8("");
1505 if (inputDesc->mPolicyMix == NULL) {
1506 address = String8("0");
1507 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1508 address = inputDesc->mPolicyMix->mRegistrationId;
1509 }
1510 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001511 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001512 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001513 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001514 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001515 }
Eric Laurente552edb2014-03-10 17:42:56 -07001516 }
1517
Eric Laurente552edb2014-03-10 17:42:56 -07001518 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1519
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001520 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001521 return NO_ERROR;
1522}
1523
Eric Laurent4dc68062014-07-28 17:26:49 -07001524status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1525 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001526{
1527 ALOGV("stopInput() input %d", input);
1528 ssize_t index = mInputs.indexOfKey(input);
1529 if (index < 0) {
1530 ALOGW("stopInput() unknown input %d", input);
1531 return BAD_VALUE;
1532 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001533 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001534
Eric Laurentc722f302014-12-10 11:21:49 -08001535 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001536 if (index < 0) {
1537 ALOGW("stopInput() unknown session %d on input %d", session, input);
1538 return BAD_VALUE;
1539 }
1540
Eric Laurente552edb2014-03-10 17:42:56 -07001541 if (inputDesc->mRefCount == 0) {
1542 ALOGW("stopInput() input %d already stopped", input);
1543 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001544 }
1545
1546 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001547
1548 // Routing?
1549 mInputRoutes.decRouteActivity(session);
1550
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001551 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001552 // if input maps to a dynamic policy with an activity listener, notify of state change
1553 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001554 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001555 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1556 MIX_STATE_IDLE);
1557 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001558
Eric Laurentc722f302014-12-10 11:21:49 -08001559 // automatically disable the remote submix output when input is stopped if not
1560 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001561 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001562 String8 address = String8("");
1563 if (inputDesc->mPolicyMix == NULL) {
1564 address = String8("0");
1565 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1566 address = inputDesc->mPolicyMix->mRegistrationId;
1567 }
1568 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001569 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001570 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001571 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001572 }
Eric Laurente552edb2014-03-10 17:42:56 -07001573 }
1574
Eric Laurent1c333e22014-05-20 10:48:17 -07001575 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001576
François Gaffie53615e22015-03-19 09:24:12 +01001577 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001578 SoundTrigger::setCaptureState(false);
1579 }
Eric Laurente552edb2014-03-10 17:42:56 -07001580 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001581 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001582}
1583
Eric Laurent4dc68062014-07-28 17:26:49 -07001584void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1585 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001586{
1587 ALOGV("releaseInput() %d", input);
1588 ssize_t index = mInputs.indexOfKey(input);
1589 if (index < 0) {
1590 ALOGW("releaseInput() releasing unknown input %d", input);
1591 return;
1592 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001593
1594 // Routing
1595 mInputRoutes.removeRoute(session);
1596
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001597 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1598 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001599
Eric Laurentc722f302014-12-10 11:21:49 -08001600 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001601 if (index < 0) {
1602 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1603 return;
1604 }
Eric Laurentc722f302014-12-10 11:21:49 -08001605 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001606 if (inputDesc->mOpenRefCount == 0) {
1607 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1608 return;
1609 }
1610 inputDesc->mOpenRefCount--;
1611 if (inputDesc->mOpenRefCount > 0) {
1612 ALOGV("releaseInput() exit > 0");
1613 return;
1614 }
1615
Eric Laurent05b90f82014-08-27 15:32:29 -07001616 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001617 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001618 ALOGV("releaseInput() exit");
1619}
1620
Eric Laurentd4692962014-05-05 18:13:44 -07001621void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001622 bool patchRemoved = false;
1623
Eric Laurentd4692962014-05-05 18:13:44 -07001624 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001625 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1626 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1627 if (patch_index >= 0) {
1628 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1629 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1630 mAudioPatches.removeItemsAt(patch_index);
1631 patchRemoved = true;
1632 }
Eric Laurentd4692962014-05-05 18:13:44 -07001633 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1634 }
1635 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001636 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001637
1638 if (patchRemoved) {
1639 mpClientInterface->onAudioPatchListUpdate();
1640 }
Eric Laurentd4692962014-05-05 18:13:44 -07001641}
1642
Eric Laurente0720872014-03-11 09:30:41 -07001643void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001644 int indexMin,
1645 int indexMax)
1646{
1647 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001648 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001649 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1650 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001651 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001652 }
Eric Laurente552edb2014-03-10 17:42:56 -07001653}
1654
Eric Laurente0720872014-03-11 09:30:41 -07001655status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001656 int index,
1657 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001658{
1659
Eric Laurentc75307b2015-03-17 15:29:32 -07001660 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1661 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001662 return BAD_VALUE;
1663 }
1664 if (!audio_is_output_device(device)) {
1665 return BAD_VALUE;
1666 }
1667
1668 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001669 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001670
1671 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1672 stream, device, index);
1673
1674 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1675 // clear all device specific values
1676 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001677 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001678 }
François Gaffiedfd74092015-03-19 12:10:59 +01001679 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001680
Eric Laurent31551f82014-10-10 18:21:56 -07001681 // update volume on all outputs whose current device is also selected by the same
1682 // strategy as the device specified by the caller
1683 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001684
1685
1686 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1687 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1688 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001689 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001690 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1691 }
1692 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1693 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001694 return NO_ERROR;
1695 }
Eric Laurente552edb2014-03-10 17:42:56 -07001696 status_t status = NO_ERROR;
1697 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001698 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1699 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001700 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001701 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001702 if (volStatus != NO_ERROR) {
1703 status = volStatus;
1704 }
1705 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001706 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1707 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001708 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001709 }
Eric Laurente552edb2014-03-10 17:42:56 -07001710 }
1711 return status;
1712}
1713
Eric Laurente0720872014-03-11 09:30:41 -07001714status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001715 int *index,
1716 audio_devices_t device)
1717{
1718 if (index == NULL) {
1719 return BAD_VALUE;
1720 }
1721 if (!audio_is_output_device(device)) {
1722 return BAD_VALUE;
1723 }
1724 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1725 // the strategy the stream belongs to.
1726 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1727 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1728 }
François Gaffiedfd74092015-03-19 12:10:59 +01001729 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001730
Eric Laurentc75307b2015-03-17 15:29:32 -07001731 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001732 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1733 return NO_ERROR;
1734}
1735
Eric Laurente0720872014-03-11 09:30:41 -07001736audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001737 const SortedVector<audio_io_handle_t>& outputs)
1738{
1739 // select one output among several suitable for global effects.
1740 // The priority is as follows:
1741 // 1: An offloaded output. If the effect ends up not being offloadable,
1742 // AudioFlinger will invalidate the track and the offloaded output
1743 // will be closed causing the effect to be moved to a PCM output.
1744 // 2: A deep buffer output
1745 // 3: the first output in the list
1746
1747 if (outputs.size() == 0) {
1748 return 0;
1749 }
1750
1751 audio_io_handle_t outputOffloaded = 0;
1752 audio_io_handle_t outputDeepBuffer = 0;
1753
1754 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001755 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001756 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001757 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1758 outputOffloaded = outputs[i];
1759 }
1760 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1761 outputDeepBuffer = outputs[i];
1762 }
1763 }
1764
1765 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1766 outputOffloaded, outputDeepBuffer);
1767 if (outputOffloaded != 0) {
1768 return outputOffloaded;
1769 }
1770 if (outputDeepBuffer != 0) {
1771 return outputDeepBuffer;
1772 }
1773
1774 return outputs[0];
1775}
1776
Eric Laurente0720872014-03-11 09:30:41 -07001777audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001778{
1779 // apply simple rule where global effects are attached to the same output as MUSIC streams
1780
Eric Laurent3b73df72014-03-11 09:06:29 -07001781 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001782 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1783 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1784
1785 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1786 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1787 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1788
1789 return output;
1790}
1791
Eric Laurente0720872014-03-11 09:30:41 -07001792status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001793 audio_io_handle_t io,
1794 uint32_t strategy,
1795 int session,
1796 int id)
1797{
1798 ssize_t index = mOutputs.indexOfKey(io);
1799 if (index < 0) {
1800 index = mInputs.indexOfKey(io);
1801 if (index < 0) {
1802 ALOGW("registerEffect() unknown io %d", io);
1803 return INVALID_OPERATION;
1804 }
1805 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001806 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001807}
1808
Eric Laurentc75307b2015-03-17 15:29:32 -07001809bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1810{
1811 return mOutputs.isStreamActive(stream, inPastMs);
1812}
1813
1814bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1815{
1816 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1817}
1818
Eric Laurente0720872014-03-11 09:30:41 -07001819bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001820{
1821 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001822 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001823 if (inputDescriptor->mRefCount == 0) {
1824 continue;
1825 }
1826 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001827 return true;
1828 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001829 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1830 // corresponds to an active capture triggered by a hardware hotword recognition
1831 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1832 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1833 // FIXME: we should not assume that the first session is the active one and keep
1834 // activity count per session. Same in startInput().
1835 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1836 if (index >= 0) {
1837 return true;
1838 }
1839 }
Eric Laurente552edb2014-03-10 17:42:56 -07001840 }
1841 return false;
1842}
1843
Eric Laurent275e8e92014-11-30 15:14:47 -08001844// Register a list of custom mixes with their attributes and format.
1845// When a mix is registered, corresponding input and output profiles are
1846// added to the remote submix hw module. The profile contains only the
1847// parameters (sampling rate, format...) specified by the mix.
1848// The corresponding input remote submix device is also connected.
1849//
1850// When a remote submix device is connected, the address is checked to select the
1851// appropriate profile and the corresponding input or output stream is opened.
1852//
1853// When capture starts, getInputForAttr() will:
1854// - 1 look for a mix matching the address passed in attribtutes tags if any
1855// - 2 if none found, getDeviceForInputSource() will:
1856// - 2.1 look for a mix matching the attributes source
1857// - 2.2 if none found, default to device selection by policy rules
1858// At this time, the corresponding output remote submix device is also connected
1859// and active playback use cases can be transferred to this mix if needed when reconnecting
1860// after AudioTracks are invalidated
1861//
1862// When playback starts, getOutputForAttr() will:
1863// - 1 look for a mix matching the address passed in attribtutes tags if any
1864// - 2 if none found, look for a mix matching the attributes usage
1865// - 3 if none found, default to device and output selection by policy rules.
1866
1867status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1868{
1869 sp<HwModule> module;
1870 for (size_t i = 0; i < mHwModules.size(); i++) {
1871 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1872 mHwModules[i]->mHandle != 0) {
1873 module = mHwModules[i];
1874 break;
1875 }
1876 }
1877
1878 if (module == 0) {
1879 return INVALID_OPERATION;
1880 }
1881
1882 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1883
1884 for (size_t i = 0; i < mixes.size(); i++) {
1885 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001886
1887 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001888 continue;
1889 }
1890 audio_config_t outputConfig = mixes[i].mFormat;
1891 audio_config_t inputConfig = mixes[i].mFormat;
1892 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1893 // stereo and let audio flinger do the channel conversion if needed.
1894 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1895 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1896 module->addOutputProfile(address, &outputConfig,
1897 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1898 module->addInputProfile(address, &inputConfig,
1899 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001900
Eric Laurentc722f302014-12-10 11:21:49 -08001901 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001902 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001903 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001904 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001905 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001906 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001907 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001908 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001909 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001910 }
1911 return NO_ERROR;
1912}
1913
1914status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1915{
1916 sp<HwModule> module;
1917 for (size_t i = 0; i < mHwModules.size(); i++) {
1918 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1919 mHwModules[i]->mHandle != 0) {
1920 module = mHwModules[i];
1921 break;
1922 }
1923 }
1924
1925 if (module == 0) {
1926 return INVALID_OPERATION;
1927 }
1928
1929 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1930
1931 for (size_t i = 0; i < mixes.size(); i++) {
1932 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001933
1934 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001935 continue;
1936 }
1937
Eric Laurentc722f302014-12-10 11:21:49 -08001938 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1939 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1940 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001941 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001942 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001943 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001944 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001945
1946 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1947 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1948 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001949 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001950 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001951 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001952 }
1953 module->removeOutputProfile(address);
1954 module->removeInputProfile(address);
1955 }
1956 return NO_ERROR;
1957}
1958
Eric Laurente552edb2014-03-10 17:42:56 -07001959
Eric Laurente0720872014-03-11 09:30:41 -07001960status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001961{
1962 const size_t SIZE = 256;
1963 char buffer[SIZE];
1964 String8 result;
1965
1966 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1967 result.append(buffer);
1968
Eric Laurentc75307b2015-03-17 15:29:32 -07001969 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001970 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001971 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001972 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001973 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001974 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001975 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001976 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001977 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001978 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001979 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001980 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001981 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001982 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001983 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001984 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001985 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001986 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001987 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001988
François Gaffie53615e22015-03-19 09:24:12 +01001989 mAvailableOutputDevices.dump(fd, String8("output"));
1990 mAvailableInputDevices.dump(fd, String8("input"));
1991 mHwModules.dump(fd);
1992 mOutputs.dump(fd);
1993 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001994 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001995 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001996 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001997
1998 return NO_ERROR;
1999}
2000
2001// This function checks for the parameters which can be offloaded.
2002// This can be enhanced depending on the capability of the DSP and policy
2003// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002004bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002005{
2006 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002007 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002008 offloadInfo.sample_rate, offloadInfo.channel_mask,
2009 offloadInfo.format,
2010 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2011 offloadInfo.has_video);
2012
2013 // Check if offload has been disabled
2014 char propValue[PROPERTY_VALUE_MAX];
2015 if (property_get("audio.offload.disable", propValue, "0")) {
2016 if (atoi(propValue) != 0) {
2017 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2018 return false;
2019 }
2020 }
2021
2022 // Check if stream type is music, then only allow offload as of now.
2023 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2024 {
2025 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2026 return false;
2027 }
2028
2029 //TODO: enable audio offloading with video when ready
2030 if (offloadInfo.has_video)
2031 {
2032 ALOGV("isOffloadSupported: has_video == true, returning false");
2033 return false;
2034 }
2035
2036 //If duration is less than minimum value defined in property, return false
2037 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2038 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2039 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2040 return false;
2041 }
2042 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2043 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2044 return false;
2045 }
2046
2047 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2048 // creating an offloaded track and tearing it down immediately after start when audioflinger
2049 // detects there is an active non offloadable effect.
2050 // FIXME: We should check the audio session here but we do not have it in this context.
2051 // This may prevent offloading in rare situations where effects are left active by apps
2052 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002053 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002054 return false;
2055 }
2056
2057 // See if there is a profile to support this.
2058 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002059 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002060 offloadInfo.sample_rate,
2061 offloadInfo.format,
2062 offloadInfo.channel_mask,
2063 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002064 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2065 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002066}
2067
Eric Laurent6a94d692014-05-20 11:18:06 -07002068status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2069 audio_port_type_t type,
2070 unsigned int *num_ports,
2071 struct audio_port *ports,
2072 unsigned int *generation)
2073{
2074 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2075 generation == NULL) {
2076 return BAD_VALUE;
2077 }
2078 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2079 if (ports == NULL) {
2080 *num_ports = 0;
2081 }
2082
2083 size_t portsWritten = 0;
2084 size_t portsMax = *num_ports;
2085 *num_ports = 0;
2086 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2087 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2088 for (size_t i = 0;
2089 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2090 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2091 }
2092 *num_ports += mAvailableOutputDevices.size();
2093 }
2094 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2095 for (size_t i = 0;
2096 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2097 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2098 }
2099 *num_ports += mAvailableInputDevices.size();
2100 }
2101 }
2102 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2103 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2104 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2105 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2106 }
2107 *num_ports += mInputs.size();
2108 }
2109 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002110 size_t numOutputs = 0;
2111 for (size_t i = 0; i < mOutputs.size(); i++) {
2112 if (!mOutputs[i]->isDuplicated()) {
2113 numOutputs++;
2114 if (portsWritten < portsMax) {
2115 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2116 }
2117 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002118 }
Eric Laurent84c70242014-06-23 08:46:27 -07002119 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002120 }
2121 }
2122 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002123 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002124 return NO_ERROR;
2125}
2126
2127status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2128{
2129 return NO_ERROR;
2130}
2131
Eric Laurent6a94d692014-05-20 11:18:06 -07002132status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2133 audio_patch_handle_t *handle,
2134 uid_t uid)
2135{
2136 ALOGV("createAudioPatch()");
2137
2138 if (handle == NULL || patch == NULL) {
2139 return BAD_VALUE;
2140 }
2141 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2142
Eric Laurent874c42872014-08-08 15:13:39 -07002143 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2144 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2145 return BAD_VALUE;
2146 }
2147 // only one source per audio patch supported for now
2148 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002149 return INVALID_OPERATION;
2150 }
Eric Laurent874c42872014-08-08 15:13:39 -07002151
2152 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002153 return INVALID_OPERATION;
2154 }
Eric Laurent874c42872014-08-08 15:13:39 -07002155 for (size_t i = 0; i < patch->num_sinks; i++) {
2156 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2157 return INVALID_OPERATION;
2158 }
2159 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002160
2161 sp<AudioPatch> patchDesc;
2162 ssize_t index = mAudioPatches.indexOfKey(*handle);
2163
Eric Laurent6a94d692014-05-20 11:18:06 -07002164 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2165 patch->sources[0].role,
2166 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002167#if LOG_NDEBUG == 0
2168 for (size_t i = 0; i < patch->num_sinks; i++) {
2169 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2170 patch->sinks[i].role,
2171 patch->sinks[i].type);
2172 }
2173#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002174
2175 if (index >= 0) {
2176 patchDesc = mAudioPatches.valueAt(index);
2177 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2178 mUidCached, patchDesc->mUid, uid);
2179 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2180 return INVALID_OPERATION;
2181 }
2182 } else {
2183 *handle = 0;
2184 }
2185
2186 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002187 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002188 if (outputDesc == NULL) {
2189 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2190 return BAD_VALUE;
2191 }
Eric Laurent84c70242014-06-23 08:46:27 -07002192 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2193 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002194 if (patchDesc != 0) {
2195 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2196 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2197 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2198 return BAD_VALUE;
2199 }
2200 }
Eric Laurent874c42872014-08-08 15:13:39 -07002201 DeviceVector devices;
2202 for (size_t i = 0; i < patch->num_sinks; i++) {
2203 // Only support mix to devices connection
2204 // TODO add support for mix to mix connection
2205 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2206 ALOGV("createAudioPatch() source mix but sink is not a device");
2207 return INVALID_OPERATION;
2208 }
2209 sp<DeviceDescriptor> devDesc =
2210 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2211 if (devDesc == 0) {
2212 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2213 return BAD_VALUE;
2214 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002215
François Gaffie53615e22015-03-19 09:24:12 +01002216 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002217 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002218 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002219 NULL, // updatedSamplingRate
2220 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002221 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002222 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002223 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002224 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002225 ALOGV("createAudioPatch() profile not supported for device %08x",
2226 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002227 return INVALID_OPERATION;
2228 }
2229 devices.add(devDesc);
2230 }
2231 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002232 return INVALID_OPERATION;
2233 }
Eric Laurent874c42872014-08-08 15:13:39 -07002234
Eric Laurent6a94d692014-05-20 11:18:06 -07002235 // TODO: reconfigure output format and channels here
2236 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002237 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002238 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002239 index = mAudioPatches.indexOfKey(*handle);
2240 if (index >= 0) {
2241 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2242 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2243 }
2244 patchDesc = mAudioPatches.valueAt(index);
2245 patchDesc->mUid = uid;
2246 ALOGV("createAudioPatch() success");
2247 } else {
2248 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2249 return INVALID_OPERATION;
2250 }
2251 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2252 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2253 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002254 // only one sink supported when connecting an input device to a mix
2255 if (patch->num_sinks > 1) {
2256 return INVALID_OPERATION;
2257 }
François Gaffie53615e22015-03-19 09:24:12 +01002258 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002259 if (inputDesc == NULL) {
2260 return BAD_VALUE;
2261 }
2262 if (patchDesc != 0) {
2263 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2264 return BAD_VALUE;
2265 }
2266 }
2267 sp<DeviceDescriptor> devDesc =
2268 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2269 if (devDesc == 0) {
2270 return BAD_VALUE;
2271 }
2272
François Gaffie53615e22015-03-19 09:24:12 +01002273 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002274 devDesc->mAddress,
2275 patch->sinks[0].sample_rate,
2276 NULL, /*updatedSampleRate*/
2277 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002278 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002279 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002280 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002281 // FIXME for the parameter type,
2282 // and the NONE
2283 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002284 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002285 return INVALID_OPERATION;
2286 }
2287 // TODO: reconfigure output format and channels here
2288 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002289 devDesc->type(), inputDesc->mIoHandle);
2290 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002291 index = mAudioPatches.indexOfKey(*handle);
2292 if (index >= 0) {
2293 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2294 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2295 }
2296 patchDesc = mAudioPatches.valueAt(index);
2297 patchDesc->mUid = uid;
2298 ALOGV("createAudioPatch() success");
2299 } else {
2300 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2301 return INVALID_OPERATION;
2302 }
2303 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2304 // device to device connection
2305 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002306 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002307 return BAD_VALUE;
2308 }
2309 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002310 sp<DeviceDescriptor> srcDeviceDesc =
2311 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002312 if (srcDeviceDesc == 0) {
2313 return BAD_VALUE;
2314 }
Eric Laurent874c42872014-08-08 15:13:39 -07002315
Eric Laurent6a94d692014-05-20 11:18:06 -07002316 //update source and sink with our own data as the data passed in the patch may
2317 // be incomplete.
2318 struct audio_patch newPatch = *patch;
2319 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002320
Eric Laurent874c42872014-08-08 15:13:39 -07002321 for (size_t i = 0; i < patch->num_sinks; i++) {
2322 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2323 ALOGV("createAudioPatch() source device but one sink is not a device");
2324 return INVALID_OPERATION;
2325 }
2326
2327 sp<DeviceDescriptor> sinkDeviceDesc =
2328 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2329 if (sinkDeviceDesc == 0) {
2330 return BAD_VALUE;
2331 }
2332 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2333
Eric Laurent3bcf8592015-04-03 12:13:24 -07002334 // create a software bridge in PatchPanel if:
2335 // - source and sink devices are on differnt HW modules OR
2336 // - audio HAL version is < 3.0
2337 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2338 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2339 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002340 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002341 return INVALID_OPERATION;
2342 }
Eric Laurent874c42872014-08-08 15:13:39 -07002343 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002344 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002345 // if the sink device is reachable via an opened output stream, request to go via
2346 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002347 audio_io_handle_t output = selectOutput(outputs,
2348 AUDIO_OUTPUT_FLAG_NONE,
2349 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002350 if (output != AUDIO_IO_HANDLE_NONE) {
2351 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2352 if (outputDesc->isDuplicated()) {
2353 return INVALID_OPERATION;
2354 }
2355 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002356 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002357 newPatch.num_sources = 2;
2358 }
Eric Laurent83b88082014-06-20 18:31:16 -07002359 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002360 }
2361 // TODO: check from routing capabilities in config file and other conflicting patches
2362
2363 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2364 if (index >= 0) {
2365 afPatchHandle = patchDesc->mAfPatchHandle;
2366 }
2367
2368 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2369 &afPatchHandle,
2370 0);
2371 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2372 status, afPatchHandle);
2373 if (status == NO_ERROR) {
2374 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002375 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002376 addAudioPatch(patchDesc->mHandle, patchDesc);
2377 } else {
2378 patchDesc->mPatch = newPatch;
2379 }
2380 patchDesc->mAfPatchHandle = afPatchHandle;
2381 *handle = patchDesc->mHandle;
2382 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002383 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002384 } else {
2385 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2386 status);
2387 return INVALID_OPERATION;
2388 }
2389 } else {
2390 return BAD_VALUE;
2391 }
2392 } else {
2393 return BAD_VALUE;
2394 }
2395 return NO_ERROR;
2396}
2397
2398status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2399 uid_t uid)
2400{
2401 ALOGV("releaseAudioPatch() patch %d", handle);
2402
2403 ssize_t index = mAudioPatches.indexOfKey(handle);
2404
2405 if (index < 0) {
2406 return BAD_VALUE;
2407 }
2408 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2409 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2410 mUidCached, patchDesc->mUid, uid);
2411 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2412 return INVALID_OPERATION;
2413 }
2414
2415 struct audio_patch *patch = &patchDesc->mPatch;
2416 patchDesc->mUid = mUidCached;
2417 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002418 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002419 if (outputDesc == NULL) {
2420 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2421 return BAD_VALUE;
2422 }
2423
Eric Laurentc75307b2015-03-17 15:29:32 -07002424 setOutputDevice(outputDesc,
2425 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002426 true,
2427 0,
2428 NULL);
2429 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2430 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002431 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 if (inputDesc == NULL) {
2433 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2434 return BAD_VALUE;
2435 }
2436 setInputDevice(inputDesc->mIoHandle,
2437 getNewInputDevice(inputDesc->mIoHandle),
2438 true,
2439 NULL);
2440 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2441 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2442 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2443 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2444 status, patchDesc->mAfPatchHandle);
2445 removeAudioPatch(patchDesc->mHandle);
2446 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002447 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002448 } else {
2449 return BAD_VALUE;
2450 }
2451 } else {
2452 return BAD_VALUE;
2453 }
2454 return NO_ERROR;
2455}
2456
2457status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2458 struct audio_patch *patches,
2459 unsigned int *generation)
2460{
François Gaffie53615e22015-03-19 09:24:12 +01002461 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002462 return BAD_VALUE;
2463 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002464 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002465 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002466}
2467
Eric Laurente1715a42014-05-20 11:30:42 -07002468status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002469{
Eric Laurente1715a42014-05-20 11:30:42 -07002470 ALOGV("setAudioPortConfig()");
2471
2472 if (config == NULL) {
2473 return BAD_VALUE;
2474 }
2475 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2476 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002477 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2478 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002479 }
2480
Eric Laurenta121f902014-06-03 13:32:54 -07002481 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002482 if (config->type == AUDIO_PORT_TYPE_MIX) {
2483 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002484 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002485 if (outputDesc == NULL) {
2486 return BAD_VALUE;
2487 }
Eric Laurent84c70242014-06-23 08:46:27 -07002488 ALOG_ASSERT(!outputDesc->isDuplicated(),
2489 "setAudioPortConfig() called on duplicated output %d",
2490 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002491 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002492 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002493 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002494 if (inputDesc == NULL) {
2495 return BAD_VALUE;
2496 }
Eric Laurenta121f902014-06-03 13:32:54 -07002497 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002498 } else {
2499 return BAD_VALUE;
2500 }
2501 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2502 sp<DeviceDescriptor> deviceDesc;
2503 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2504 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2505 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2506 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2507 } else {
2508 return BAD_VALUE;
2509 }
2510 if (deviceDesc == NULL) {
2511 return BAD_VALUE;
2512 }
Eric Laurenta121f902014-06-03 13:32:54 -07002513 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002514 } else {
2515 return BAD_VALUE;
2516 }
2517
Eric Laurenta121f902014-06-03 13:32:54 -07002518 struct audio_port_config backupConfig;
2519 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2520 if (status == NO_ERROR) {
2521 struct audio_port_config newConfig;
2522 audioPortConfig->toAudioPortConfig(&newConfig, config);
2523 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002524 }
Eric Laurenta121f902014-06-03 13:32:54 -07002525 if (status != NO_ERROR) {
2526 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002527 }
Eric Laurente1715a42014-05-20 11:30:42 -07002528
2529 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002530}
2531
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002532void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2533{
2534 clearAudioPatches(uid);
2535 clearSessionRoutes(uid);
2536}
2537
Eric Laurent6a94d692014-05-20 11:18:06 -07002538void AudioPolicyManager::clearAudioPatches(uid_t uid)
2539{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002540 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002541 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2542 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002543 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002544 }
2545 }
2546}
2547
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002548
2549void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2550 audio_io_handle_t ouptutToSkip)
2551{
2552 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2553 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2554 for (size_t j = 0; j < mOutputs.size(); j++) {
2555 if (mOutputs.keyAt(j) == ouptutToSkip) {
2556 continue;
2557 }
2558 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2559 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2560 continue;
2561 }
2562 // If the default device for this strategy is on another output mix,
2563 // invalidate all tracks in this strategy to force re connection.
2564 // Otherwise select new device on the output mix.
2565 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2566 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2567 if (stream == AUDIO_STREAM_PATCH) {
2568 continue;
2569 }
2570 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2571 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2572 }
2573 }
2574 } else {
2575 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2576 setOutputDevice(outputDesc, newDevice, false);
2577 }
2578 }
2579}
2580
2581void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2582{
2583 // remove output routes associated with this uid
2584 SortedVector<routing_strategy> affectedStrategies;
2585 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2586 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2587 if (route->mUid == uid) {
2588 mOutputRoutes.removeItemsAt(i);
2589 if (route->mDeviceDescriptor != 0) {
2590 affectedStrategies.add(getStrategy(route->mStreamType));
2591 }
2592 }
2593 }
2594 // reroute outputs if necessary
2595 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2596 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2597 }
2598
2599 // remove input routes associated with this uid
2600 SortedVector<audio_source_t> affectedSources;
2601 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2602 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2603 if (route->mUid == uid) {
2604 mInputRoutes.removeItemsAt(i);
2605 if (route->mDeviceDescriptor != 0) {
2606 affectedSources.add(route->mSource);
2607 }
2608 }
2609 }
2610 // reroute inputs if necessary
2611 SortedVector<audio_io_handle_t> inputsToClose;
2612 for (size_t i = 0; i < mInputs.size(); i++) {
2613 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2614 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2615 inputsToClose.add(inputDesc->mIoHandle);
2616 }
2617 }
2618 for (size_t i = 0; i < inputsToClose.size(); i++) {
2619 closeInput(inputsToClose[i]);
2620 }
2621}
2622
2623
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002624status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2625 audio_io_handle_t *ioHandle,
2626 audio_devices_t *device)
2627{
2628 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2629 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002630 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002631
François Gaffiedf372692015-03-19 10:43:27 +01002632 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002633}
2634
Eric Laurent493404d2015-04-21 15:07:36 -07002635status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2636 const audio_attributes_t *attributes __unused,
2637 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002638{
2639 return INVALID_OPERATION;
2640}
2641
Eric Laurent493404d2015-04-21 15:07:36 -07002642status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002643{
2644 return INVALID_OPERATION;
2645}
2646
Eric Laurente552edb2014-03-10 17:42:56 -07002647// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002648// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002649// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002650uint32_t AudioPolicyManager::nextAudioPortGeneration()
2651{
2652 return android_atomic_inc(&mAudioPortGeneration);
2653}
2654
Eric Laurente0720872014-03-11 09:30:41 -07002655AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002656 :
2657#ifdef AUDIO_POLICY_TEST
2658 Thread(false),
2659#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002660 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002661 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002662 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002663 mAudioPortGeneration(1),
2664 mBeaconMuteRefCount(0),
2665 mBeaconPlayingRefCount(0),
2666 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002667{
François Gaffie2110e042015-03-24 08:41:51 +01002668 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2669 if (!engineInstance) {
2670 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2671 return;
2672 }
2673 // Retrieve the Policy Manager Interface
2674 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2675 if (mEngine == NULL) {
2676 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2677 return;
2678 }
2679 mEngine->setObserver(this);
2680 status_t status = mEngine->initCheck();
2681 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2682
Eric Laurent6a94d692014-05-20 11:18:06 -07002683 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002684 mpClientInterface = clientInterface;
2685
Eric Laurent7288ab82015-05-06 18:44:02 -07002686 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002687 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2688 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2689 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2690 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2691 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2692 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002693 ALOGE("could not load audio policy configuration file, setting defaults");
2694 defaultAudioPolicyConfig();
2695 }
2696 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002697 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002698
François Gaffie2110e042015-03-24 08:41:51 +01002699 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2700 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002701
2702 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002703 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2704 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002705 for (size_t i = 0; i < mHwModules.size(); i++) {
2706 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2707 if (mHwModules[i]->mHandle == 0) {
2708 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2709 continue;
2710 }
2711 // open all output streams needed to access attached devices
2712 // except for direct output streams that are only opened when they are actually
2713 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002714 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002715 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2716 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002717 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002718
Eric Laurent3a4311c2014-03-17 12:00:47 -07002719 if (outProfile->mSupportedDevices.isEmpty()) {
2720 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2721 continue;
2722 }
2723
Eric Laurentd78f1532014-09-16 16:38:20 -07002724 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2725 continue;
2726 }
Eric Laurent83b88082014-06-20 18:31:16 -07002727 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002728 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2729 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002730 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002731 // chose first device present in mSupportedDevices also part of
2732 // outputDeviceTypes
2733 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002734 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002735 if ((profileType & outputDeviceTypes) != 0) {
2736 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002737 }
Eric Laurente552edb2014-03-10 17:42:56 -07002738 }
2739 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002740 if ((profileType & outputDeviceTypes) == 0) {
2741 continue;
2742 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002743 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2744 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002745
2746 outputDesc->mDevice = profileType;
2747 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2748 config.sample_rate = outputDesc->mSamplingRate;
2749 config.channel_mask = outputDesc->mChannelMask;
2750 config.format = outputDesc->mFormat;
2751 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002752 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002753 &output,
2754 &config,
2755 &outputDesc->mDevice,
2756 String8(""),
2757 &outputDesc->mLatency,
2758 outputDesc->mFlags);
2759
2760 if (status != NO_ERROR) {
2761 ALOGW("Cannot open output stream for device %08x on hw module %s",
2762 outputDesc->mDevice,
2763 mHwModules[i]->mName);
2764 } else {
2765 outputDesc->mSamplingRate = config.sample_rate;
2766 outputDesc->mChannelMask = config.channel_mask;
2767 outputDesc->mFormat = config.format;
2768
2769 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002770 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002771 ssize_t index =
2772 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2773 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002774 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2775 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002776 }
2777 }
2778 if (mPrimaryOutput == 0 &&
2779 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002780 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002781 }
2782 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002783 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002784 outputDesc->mDevice,
2785 true);
2786 }
Eric Laurente552edb2014-03-10 17:42:56 -07002787 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002788 // open input streams needed to access attached devices to validate
2789 // mAvailableInputDevices list
2790 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2791 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002792 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002793
Eric Laurent3a4311c2014-03-17 12:00:47 -07002794 if (inProfile->mSupportedDevices.isEmpty()) {
2795 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2796 continue;
2797 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002798 // chose first device present in mSupportedDevices also part of
2799 // inputDeviceTypes
2800 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2801 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002802 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002803 if (profileType & inputDeviceTypes) {
2804 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002805 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002806 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002807 if ((profileType & inputDeviceTypes) == 0) {
2808 continue;
2809 }
2810 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2811
2812 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2813 inputDesc->mDevice = profileType;
2814
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002815 // find the address
2816 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2817 // the inputs vector must be of size 1, but we don't want to crash here
2818 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2819 : String8("");
2820 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2821 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2822
Eric Laurentd78f1532014-09-16 16:38:20 -07002823 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2824 config.sample_rate = inputDesc->mSamplingRate;
2825 config.channel_mask = inputDesc->mChannelMask;
2826 config.format = inputDesc->mFormat;
2827 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002828 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002829 &input,
2830 &config,
2831 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002832 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002833 AUDIO_SOURCE_MIC,
2834 AUDIO_INPUT_FLAG_NONE);
2835
2836 if (status == NO_ERROR) {
2837 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002838 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002839 ssize_t index =
2840 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2841 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002842 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2843 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002844 }
2845 }
2846 mpClientInterface->closeInput(input);
2847 } else {
2848 ALOGW("Cannot open input stream for device %08x on hw module %s",
2849 inputDesc->mDevice,
2850 mHwModules[i]->mName);
2851 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002852 }
2853 }
2854 // make sure all attached devices have been allocated a unique ID
2855 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002856 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002857 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002858 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2859 continue;
2860 }
François Gaffie2110e042015-03-24 08:41:51 +01002861 // The device is now validated and can be appended to the available devices of the engine
2862 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2863 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002864 i++;
2865 }
2866 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002867 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002868 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002869 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2870 continue;
2871 }
François Gaffie2110e042015-03-24 08:41:51 +01002872 // The device is now validated and can be appended to the available devices of the engine
2873 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2874 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002875 i++;
2876 }
2877 // make sure default device is reachable
2878 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002879 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002880 }
Eric Laurente552edb2014-03-10 17:42:56 -07002881
2882 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2883
2884 updateDevicesAndOutputs();
2885
2886#ifdef AUDIO_POLICY_TEST
2887 if (mPrimaryOutput != 0) {
2888 AudioParameter outputCmd = AudioParameter();
2889 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002890 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002891
2892 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2893 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002894 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2895 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002896 mTestLatencyMs = 0;
2897 mCurOutput = 0;
2898 mDirectOutput = false;
2899 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2900 mTestOutputs[i] = 0;
2901 }
2902
2903 const size_t SIZE = 256;
2904 char buffer[SIZE];
2905 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2906 run(buffer, ANDROID_PRIORITY_AUDIO);
2907 }
2908#endif //AUDIO_POLICY_TEST
2909}
2910
Eric Laurente0720872014-03-11 09:30:41 -07002911AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002912{
2913#ifdef AUDIO_POLICY_TEST
2914 exit();
2915#endif //AUDIO_POLICY_TEST
2916 for (size_t i = 0; i < mOutputs.size(); i++) {
2917 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002918 }
2919 for (size_t i = 0; i < mInputs.size(); i++) {
2920 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002921 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002922 mAvailableOutputDevices.clear();
2923 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002924 mOutputs.clear();
2925 mInputs.clear();
2926 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002927}
2928
Eric Laurente0720872014-03-11 09:30:41 -07002929status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002930{
2931 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2932}
2933
2934#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002935bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002936{
2937 ALOGV("entering threadLoop()");
2938 while (!exitPending())
2939 {
2940 String8 command;
2941 int valueInt;
2942 String8 value;
2943
2944 Mutex::Autolock _l(mLock);
2945 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2946
2947 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2948 AudioParameter param = AudioParameter(command);
2949
2950 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2951 valueInt != 0) {
2952 ALOGV("Test command %s received", command.string());
2953 String8 target;
2954 if (param.get(String8("target"), target) != NO_ERROR) {
2955 target = "Manager";
2956 }
2957 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2958 param.remove(String8("test_cmd_policy_output"));
2959 mCurOutput = valueInt;
2960 }
2961 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2962 param.remove(String8("test_cmd_policy_direct"));
2963 if (value == "false") {
2964 mDirectOutput = false;
2965 } else if (value == "true") {
2966 mDirectOutput = true;
2967 }
2968 }
2969 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2970 param.remove(String8("test_cmd_policy_input"));
2971 mTestInput = valueInt;
2972 }
2973
2974 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2975 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002976 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002977 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002978 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002979 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002980 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002981 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002982 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002983 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002984 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002985 if (target == "Manager") {
2986 mTestFormat = format;
2987 } else if (mTestOutputs[mCurOutput] != 0) {
2988 AudioParameter outputParam = AudioParameter();
2989 outputParam.addInt(String8("format"), format);
2990 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2991 }
2992 }
2993 }
2994 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2995 param.remove(String8("test_cmd_policy_channels"));
2996 int channels = 0;
2997
2998 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002999 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003000 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003001 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003002 }
3003 if (channels != 0) {
3004 if (target == "Manager") {
3005 mTestChannels = channels;
3006 } else if (mTestOutputs[mCurOutput] != 0) {
3007 AudioParameter outputParam = AudioParameter();
3008 outputParam.addInt(String8("channels"), channels);
3009 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3010 }
3011 }
3012 }
3013 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3014 param.remove(String8("test_cmd_policy_sampleRate"));
3015 if (valueInt >= 0 && valueInt <= 96000) {
3016 int samplingRate = valueInt;
3017 if (target == "Manager") {
3018 mTestSamplingRate = samplingRate;
3019 } else if (mTestOutputs[mCurOutput] != 0) {
3020 AudioParameter outputParam = AudioParameter();
3021 outputParam.addInt(String8("sampling_rate"), samplingRate);
3022 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3023 }
3024 }
3025 }
3026
3027 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3028 param.remove(String8("test_cmd_policy_reopen"));
3029
Eric Laurentc75307b2015-03-17 15:29:32 -07003030 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003031
Eric Laurentc75307b2015-03-17 15:29:32 -07003032 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003033
Eric Laurentc75307b2015-03-17 15:29:32 -07003034 removeOutput(mPrimaryOutput->mIoHandle);
3035 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3036 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003037 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003038 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3039 config.sample_rate = outputDesc->mSamplingRate;
3040 config.channel_mask = outputDesc->mChannelMask;
3041 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003042 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003043 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003044 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003045 &config,
3046 &outputDesc->mDevice,
3047 String8(""),
3048 &outputDesc->mLatency,
3049 outputDesc->mFlags);
3050 if (status != NO_ERROR) {
3051 ALOGE("Failed to reopen hardware output stream, "
3052 "samplingRate: %d, format %d, channels %d",
3053 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003054 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003055 outputDesc->mSamplingRate = config.sample_rate;
3056 outputDesc->mChannelMask = config.channel_mask;
3057 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003058 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003059 AudioParameter outputCmd = AudioParameter();
3060 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003061 mpClientInterface->setParameters(handle, outputCmd.toString());
3062 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003063 }
3064 }
3065
3066
3067 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3068 }
3069 }
3070 return false;
3071}
3072
Eric Laurente0720872014-03-11 09:30:41 -07003073void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003074{
3075 {
3076 AutoMutex _l(mLock);
3077 requestExit();
3078 mWaitWorkCV.signal();
3079 }
3080 requestExitAndWait();
3081}
3082
Eric Laurente0720872014-03-11 09:30:41 -07003083int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003084{
3085 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3086 if (output == mTestOutputs[i]) return i;
3087 }
3088 return 0;
3089}
3090#endif //AUDIO_POLICY_TEST
3091
3092// ---
3093
Eric Laurentc75307b2015-03-17 15:29:32 -07003094void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003095{
François Gaffie98cc1912015-03-18 17:52:40 +01003096 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003097 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003098 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003099}
3100
François Gaffie53615e22015-03-19 09:24:12 +01003101void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3102{
3103 mOutputs.removeItem(output);
3104}
3105
Eric Laurent1f2f2232014-06-02 12:01:23 -07003106void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003107{
François Gaffie98cc1912015-03-18 17:52:40 +01003108 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003109 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003110 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003111}
Eric Laurente552edb2014-03-10 17:42:56 -07003112
Eric Laurentc75307b2015-03-17 15:29:32 -07003113void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003114 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003115 const String8 address /*in*/,
3116 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003117 sp<DeviceDescriptor> devDesc =
3118 desc->mProfile->mSupportedDevices.getDevice(device, address);
3119 if (devDesc != 0) {
3120 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3121 desc->mIoHandle, address.string());
3122 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003123 }
3124}
3125
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003126status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003127 audio_policy_dev_state_t state,
3128 SortedVector<audio_io_handle_t>& outputs,
3129 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003130{
François Gaffie53615e22015-03-19 09:24:12 +01003131 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003132 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003133 // erase all current sample rates, formats and channel masks
3134 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003135
Eric Laurent3b73df72014-03-11 09:06:29 -07003136 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003137 // first list already open outputs that can be routed to this device
3138 for (size_t i = 0; i < mOutputs.size(); i++) {
3139 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003140 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003141 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003142 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3143 outputs.add(mOutputs.keyAt(i));
3144 } else {
3145 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003146 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003147 }
Eric Laurente552edb2014-03-10 17:42:56 -07003148 }
3149 }
3150 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003151 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003152 for (size_t i = 0; i < mHwModules.size(); i++)
3153 {
3154 if (mHwModules[i]->mHandle == 0) {
3155 continue;
3156 }
3157 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3158 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003159 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3160 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003161 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003162 address == profile->mSupportedDevices[0]->mAddress) {
3163 profiles.add(profile);
3164 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3165 }
Eric Laurente552edb2014-03-10 17:42:56 -07003166 }
3167 }
3168 }
3169
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003170 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3171
Eric Laurente552edb2014-03-10 17:42:56 -07003172 if (profiles.isEmpty() && outputs.isEmpty()) {
3173 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3174 return BAD_VALUE;
3175 }
3176
3177 // open outputs for matching profiles if needed. Direct outputs are also opened to
3178 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3179 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003180 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003181
3182 // nothing to do if one output is already opened for this profile
3183 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003184 for (j = 0; j < outputs.size(); j++) {
3185 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003186 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003187 // matching profile: save the sample rates, format and channel masks supported
3188 // by the profile in our device descriptor
3189 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003190 break;
3191 }
3192 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003193 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003194 continue;
3195 }
3196
Eric Laurent83b88082014-06-20 18:31:16 -07003197 ALOGV("opening output for device %08x with params %s profile %p",
3198 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003199 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003200 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003201 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3202 config.sample_rate = desc->mSamplingRate;
3203 config.channel_mask = desc->mChannelMask;
3204 config.format = desc->mFormat;
3205 config.offload_info.sample_rate = desc->mSamplingRate;
3206 config.offload_info.channel_mask = desc->mChannelMask;
3207 config.offload_info.format = desc->mFormat;
3208 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003209 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003210 &output,
3211 &config,
3212 &desc->mDevice,
3213 address,
3214 &desc->mLatency,
3215 desc->mFlags);
3216 if (status == NO_ERROR) {
3217 desc->mSamplingRate = config.sample_rate;
3218 desc->mChannelMask = config.channel_mask;
3219 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003220
Eric Laurentd4692962014-05-05 18:13:44 -07003221 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003222 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003223 char *param = audio_device_address_to_parameter(device, address);
3224 mpClientInterface->setParameters(output, String8(param));
3225 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003226 }
3227
Eric Laurentd4692962014-05-05 18:13:44 -07003228 // Here is where we step through and resolve any "dynamic" fields
3229 String8 reply;
3230 char *value;
3231 if (profile->mSamplingRates[0] == 0) {
3232 reply = mpClientInterface->getParameters(output,
3233 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003234 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003235 reply.string());
3236 value = strpbrk((char *)reply.string(), "=");
3237 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003238 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003239 }
Eric Laurentd4692962014-05-05 18:13:44 -07003240 }
3241 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3242 reply = mpClientInterface->getParameters(output,
3243 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003244 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003245 reply.string());
3246 value = strpbrk((char *)reply.string(), "=");
3247 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003248 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003249 }
Eric Laurentd4692962014-05-05 18:13:44 -07003250 }
3251 if (profile->mChannelMasks[0] == 0) {
3252 reply = mpClientInterface->getParameters(output,
3253 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003254 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003255 reply.string());
3256 value = strpbrk((char *)reply.string(), "=");
3257 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003258 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003259 }
Eric Laurentd4692962014-05-05 18:13:44 -07003260 }
3261 if (((profile->mSamplingRates[0] == 0) &&
3262 (profile->mSamplingRates.size() < 2)) ||
3263 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3264 (profile->mFormats.size() < 2)) ||
3265 ((profile->mChannelMasks[0] == 0) &&
3266 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003267 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003268 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003269 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003270 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3271 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003272 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003273 config.sample_rate = profile->pickSamplingRate();
3274 config.channel_mask = profile->pickChannelMask();
3275 config.format = profile->pickFormat();
3276 config.offload_info.sample_rate = config.sample_rate;
3277 config.offload_info.channel_mask = config.channel_mask;
3278 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003279 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003280 &output,
3281 &config,
3282 &desc->mDevice,
3283 address,
3284 &desc->mLatency,
3285 desc->mFlags);
3286 if (status == NO_ERROR) {
3287 desc->mSamplingRate = config.sample_rate;
3288 desc->mChannelMask = config.channel_mask;
3289 desc->mFormat = config.format;
3290 } else {
3291 output = AUDIO_IO_HANDLE_NONE;
3292 }
Eric Laurentd4692962014-05-05 18:13:44 -07003293 }
3294
Eric Laurentcf2c0212014-07-25 16:20:43 -07003295 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003296 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003297 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003298 sp<AudioPolicyMix> policyMix;
3299 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003300 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3301 address.string());
3302 }
François Gaffie036e1e92015-03-19 10:16:24 +01003303 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003304 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003305
Eric Laurentc722f302014-12-10 11:21:49 -08003306 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3307 // no duplicated output for direct outputs and
3308 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003309 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003310
Eric Laurentd4692962014-05-05 18:13:44 -07003311 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003312 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003313
Eric Laurentd4692962014-05-05 18:13:44 -07003314 //TODO: configure audio effect output stage here
3315
3316 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003317 duplicatedOutput =
3318 mpClientInterface->openDuplicateOutput(output,
3319 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003320 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003321 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003322 sp<SwAudioOutputDescriptor> dupOutputDesc =
3323 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3324 dupOutputDesc->mOutput1 = mPrimaryOutput;
3325 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003326 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3327 dupOutputDesc->mFormat = desc->mFormat;
3328 dupOutputDesc->mChannelMask = desc->mChannelMask;
3329 dupOutputDesc->mLatency = desc->mLatency;
3330 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003331 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003332 } else {
3333 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003334 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003335 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003336 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003337 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003338 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003339 }
Eric Laurente552edb2014-03-10 17:42:56 -07003340 }
3341 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003342 } else {
3343 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003344 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003345 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003346 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003347 profiles.removeAt(profile_index);
3348 profile_index--;
3349 } else {
3350 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003351 devDesc->importAudioPort(profile);
3352
François Gaffie53615e22015-03-19 09:24:12 +01003353 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003354 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3355 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003356 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003357 NULL/*patch handle*/, address.string());
3358 }
Eric Laurente552edb2014-03-10 17:42:56 -07003359 ALOGV("checkOutputsForDevice(): adding output %d", output);
3360 }
3361 }
3362
3363 if (profiles.isEmpty()) {
3364 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3365 return BAD_VALUE;
3366 }
Eric Laurentd4692962014-05-05 18:13:44 -07003367 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003368 // check if one opened output is not needed any more after disconnecting one device
3369 for (size_t i = 0; i < mOutputs.size(); i++) {
3370 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003371 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003372 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003373 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003374 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003375 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003376 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003377 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3378 mOutputs.keyAt(i));
3379 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003380 }
Eric Laurente552edb2014-03-10 17:42:56 -07003381 }
3382 }
Eric Laurentd4692962014-05-05 18:13:44 -07003383 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003384 for (size_t i = 0; i < mHwModules.size(); i++)
3385 {
3386 if (mHwModules[i]->mHandle == 0) {
3387 continue;
3388 }
3389 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3390 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003391 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003392 if (profile->mSupportedDevices.types() & device) {
3393 ALOGV("checkOutputsForDevice(): "
3394 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003395 if (profile->mSamplingRates[0] == 0) {
3396 profile->mSamplingRates.clear();
3397 profile->mSamplingRates.add(0);
3398 }
3399 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3400 profile->mFormats.clear();
3401 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3402 }
3403 if (profile->mChannelMasks[0] == 0) {
3404 profile->mChannelMasks.clear();
3405 profile->mChannelMasks.add(0);
3406 }
3407 }
3408 }
3409 }
3410 }
3411 return NO_ERROR;
3412}
3413
Eric Laurentd4692962014-05-05 18:13:44 -07003414status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003415 audio_policy_dev_state_t state,
3416 SortedVector<audio_io_handle_t>& inputs,
3417 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003418{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003419 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003420 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3421 // first list already open inputs that can be routed to this device
3422 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3423 desc = mInputs.valueAt(input_index);
3424 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3425 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3426 inputs.add(mInputs.keyAt(input_index));
3427 }
3428 }
3429
3430 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003431 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003432 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3433 {
3434 if (mHwModules[module_idx]->mHandle == 0) {
3435 continue;
3436 }
3437 for (size_t profile_index = 0;
3438 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3439 profile_index++)
3440 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003441 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3442
3443 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003444 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003445 address == profile->mSupportedDevices[0]->mAddress) {
3446 profiles.add(profile);
3447 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3448 profile_index, module_idx);
3449 }
Eric Laurentd4692962014-05-05 18:13:44 -07003450 }
3451 }
3452 }
3453
3454 if (profiles.isEmpty() && inputs.isEmpty()) {
3455 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3456 return BAD_VALUE;
3457 }
3458
3459 // open inputs for matching profiles if needed. Direct inputs are also opened to
3460 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3461 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3462
Eric Laurent1c333e22014-05-20 10:48:17 -07003463 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003464 // nothing to do if one input is already opened for this profile
3465 size_t input_index;
3466 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3467 desc = mInputs.valueAt(input_index);
3468 if (desc->mProfile == profile) {
3469 break;
3470 }
3471 }
3472 if (input_index != mInputs.size()) {
3473 continue;
3474 }
3475
3476 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3477 desc = new AudioInputDescriptor(profile);
3478 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003479 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3480 config.sample_rate = desc->mSamplingRate;
3481 config.channel_mask = desc->mChannelMask;
3482 config.format = desc->mFormat;
3483 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003484 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003485 &input,
3486 &config,
3487 &desc->mDevice,
3488 address,
3489 AUDIO_SOURCE_MIC,
3490 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003491
Eric Laurentcf2c0212014-07-25 16:20:43 -07003492 if (status == NO_ERROR) {
3493 desc->mSamplingRate = config.sample_rate;
3494 desc->mChannelMask = config.channel_mask;
3495 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003496
Eric Laurentd4692962014-05-05 18:13:44 -07003497 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003498 char *param = audio_device_address_to_parameter(device, address);
3499 mpClientInterface->setParameters(input, String8(param));
3500 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003501 }
3502
3503 // Here is where we step through and resolve any "dynamic" fields
3504 String8 reply;
3505 char *value;
3506 if (profile->mSamplingRates[0] == 0) {
3507 reply = mpClientInterface->getParameters(input,
3508 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3509 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3510 reply.string());
3511 value = strpbrk((char *)reply.string(), "=");
3512 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003513 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003514 }
3515 }
3516 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3517 reply = mpClientInterface->getParameters(input,
3518 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3519 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3520 value = strpbrk((char *)reply.string(), "=");
3521 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003522 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003523 }
3524 }
3525 if (profile->mChannelMasks[0] == 0) {
3526 reply = mpClientInterface->getParameters(input,
3527 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3528 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3529 reply.string());
3530 value = strpbrk((char *)reply.string(), "=");
3531 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003532 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003533 }
3534 }
3535 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3536 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3537 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3538 ALOGW("checkInputsForDevice() direct input missing param");
3539 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003540 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003541 }
3542
3543 if (input != 0) {
3544 addInput(input, desc);
3545 }
3546 } // endif input != 0
3547
Eric Laurentcf2c0212014-07-25 16:20:43 -07003548 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003549 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003550 profiles.removeAt(profile_index);
3551 profile_index--;
3552 } else {
3553 inputs.add(input);
3554 ALOGV("checkInputsForDevice(): adding input %d", input);
3555 }
3556 } // end scan profiles
3557
3558 if (profiles.isEmpty()) {
3559 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3560 return BAD_VALUE;
3561 }
3562 } else {
3563 // Disconnect
3564 // check if one opened input is not needed any more after disconnecting one device
3565 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3566 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003567 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3568 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003569 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3570 mInputs.keyAt(input_index));
3571 inputs.add(mInputs.keyAt(input_index));
3572 }
3573 }
3574 // Clear any profiles associated with the disconnected device.
3575 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3576 if (mHwModules[module_index]->mHandle == 0) {
3577 continue;
3578 }
3579 for (size_t profile_index = 0;
3580 profile_index < mHwModules[module_index]->mInputProfiles.size();
3581 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003582 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003583 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003584 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003585 profile_index, module_index);
3586 if (profile->mSamplingRates[0] == 0) {
3587 profile->mSamplingRates.clear();
3588 profile->mSamplingRates.add(0);
3589 }
3590 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3591 profile->mFormats.clear();
3592 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3593 }
3594 if (profile->mChannelMasks[0] == 0) {
3595 profile->mChannelMasks.clear();
3596 profile->mChannelMasks.add(0);
3597 }
3598 }
3599 }
3600 }
3601 } // end disconnect
3602
3603 return NO_ERROR;
3604}
3605
3606
Eric Laurente0720872014-03-11 09:30:41 -07003607void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003608{
3609 ALOGV("closeOutput(%d)", output);
3610
Eric Laurentc75307b2015-03-17 15:29:32 -07003611 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003612 if (outputDesc == NULL) {
3613 ALOGW("closeOutput() unknown output %d", output);
3614 return;
3615 }
François Gaffie036e1e92015-03-19 10:16:24 +01003616 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003617
Eric Laurente552edb2014-03-10 17:42:56 -07003618 // look for duplicated outputs connected to the output being removed.
3619 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003620 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003621 if (dupOutputDesc->isDuplicated() &&
3622 (dupOutputDesc->mOutput1 == outputDesc ||
3623 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003624 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003625 if (dupOutputDesc->mOutput1 == outputDesc) {
3626 outputDesc2 = dupOutputDesc->mOutput2;
3627 } else {
3628 outputDesc2 = dupOutputDesc->mOutput1;
3629 }
3630 // As all active tracks on duplicated output will be deleted,
3631 // and as they were also referenced on the other output, the reference
3632 // count for their stream type must be adjusted accordingly on
3633 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003634 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003635 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003636 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003637 }
3638 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3639 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3640
3641 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003642 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003643 }
3644 }
3645
Eric Laurent05b90f82014-08-27 15:32:29 -07003646 nextAudioPortGeneration();
3647
3648 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3649 if (index >= 0) {
3650 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3651 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3652 mAudioPatches.removeItemsAt(index);
3653 mpClientInterface->onAudioPatchListUpdate();
3654 }
3655
Eric Laurente552edb2014-03-10 17:42:56 -07003656 AudioParameter param;
3657 param.add(String8("closing"), String8("true"));
3658 mpClientInterface->setParameters(output, param.toString());
3659
3660 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003661 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003662 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003663}
3664
3665void AudioPolicyManager::closeInput(audio_io_handle_t input)
3666{
3667 ALOGV("closeInput(%d)", input);
3668
3669 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3670 if (inputDesc == NULL) {
3671 ALOGW("closeInput() unknown input %d", input);
3672 return;
3673 }
3674
Eric Laurent6a94d692014-05-20 11:18:06 -07003675 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003676
3677 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3678 if (index >= 0) {
3679 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3680 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3681 mAudioPatches.removeItemsAt(index);
3682 mpClientInterface->onAudioPatchListUpdate();
3683 }
3684
3685 mpClientInterface->closeInput(input);
3686 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003687}
3688
Eric Laurentc75307b2015-03-17 15:29:32 -07003689SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3690 audio_devices_t device,
3691 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003692{
3693 SortedVector<audio_io_handle_t> outputs;
3694
3695 ALOGVV("getOutputsForDevice() device %04x", device);
3696 for (size_t i = 0; i < openOutputs.size(); i++) {
3697 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003698 i, openOutputs.valueAt(i)->isDuplicated(),
3699 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003700 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3701 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3702 outputs.add(openOutputs.keyAt(i));
3703 }
3704 }
3705 return outputs;
3706}
3707
Eric Laurente0720872014-03-11 09:30:41 -07003708bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003709 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003710{
3711 if (outputs1.size() != outputs2.size()) {
3712 return false;
3713 }
3714 for (size_t i = 0; i < outputs1.size(); i++) {
3715 if (outputs1[i] != outputs2[i]) {
3716 return false;
3717 }
3718 }
3719 return true;
3720}
3721
Eric Laurente0720872014-03-11 09:30:41 -07003722void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003723{
3724 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3725 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3726 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3727 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3728
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003729 // also take into account external policy-related changes: add all outputs which are
3730 // associated with policies in the "before" and "after" output vectors
3731 ALOGVV("checkOutputForStrategy(): policy related outputs");
3732 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003733 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003734 if (desc != 0 && desc->mPolicyMix != NULL) {
3735 srcOutputs.add(desc->mIoHandle);
3736 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3737 }
3738 }
3739 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003740 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003741 if (desc != 0 && desc->mPolicyMix != NULL) {
3742 dstOutputs.add(desc->mIoHandle);
3743 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3744 }
3745 }
3746
Eric Laurente552edb2014-03-10 17:42:56 -07003747 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3748 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3749 strategy, srcOutputs[0], dstOutputs[0]);
3750 // mute strategy while moving tracks from one output to another
3751 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003752 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003753 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003754 setStrategyMute(strategy, true, desc);
3755 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003756 }
3757 }
3758
3759 // Move effects associated to this strategy from previous output to new output
3760 if (strategy == STRATEGY_MEDIA) {
3761 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3762 SortedVector<audio_io_handle_t> moved;
3763 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003764 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3765 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3766 effectDesc->mIo != fxOutput) {
3767 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003768 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3769 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003770 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003771 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003772 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003773 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003774 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003775 }
3776 }
3777 }
3778 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003779 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003780 if (i == AUDIO_STREAM_PATCH) {
3781 continue;
3782 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003783 if (getStrategy((audio_stream_type_t)i) == strategy) {
3784 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003785 }
3786 }
3787 }
3788}
3789
Eric Laurente0720872014-03-11 09:30:41 -07003790void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003791{
François Gaffie2110e042015-03-24 08:41:51 +01003792 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003793 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003794 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003795 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003796 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003797 checkOutputForStrategy(STRATEGY_SONIFICATION);
3798 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003799 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003800 checkOutputForStrategy(STRATEGY_MEDIA);
3801 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003802 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003803}
3804
Eric Laurente0720872014-03-11 09:30:41 -07003805void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003806{
François Gaffie53615e22015-03-19 09:24:12 +01003807 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003808 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003809 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003810 return;
3811 }
3812
Eric Laurent3a4311c2014-03-17 12:00:47 -07003813 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003814 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3815 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3816 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003817 // suspend A2DP output if:
3818 // (NOT already suspended) &&
3819 // ((SCO device is connected &&
3820 // (forced usage for communication || for record is SCO))) ||
3821 // (phone state is ringing || in call)
3822 //
3823 // restore A2DP output if:
3824 // (Already suspended) &&
3825 // ((SCO device is NOT connected ||
3826 // (forced usage NOT for communication && NOT for record is SCO))) &&
3827 // (phone state is NOT ringing && NOT in call)
3828 //
3829 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003830 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003831 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3832 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3833 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3834 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003835
3836 mpClientInterface->restoreOutput(a2dpOutput);
3837 mA2dpSuspended = false;
3838 }
3839 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003840 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003841 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3842 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3843 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3844 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003845
3846 mpClientInterface->suspendOutput(a2dpOutput);
3847 mA2dpSuspended = true;
3848 }
3849 }
3850}
3851
Eric Laurentc75307b2015-03-17 15:29:32 -07003852audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3853 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003854{
3855 audio_devices_t device = AUDIO_DEVICE_NONE;
3856
Eric Laurent6a94d692014-05-20 11:18:06 -07003857 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3858 if (index >= 0) {
3859 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3860 if (patchDesc->mUid != mUidCached) {
3861 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3862 outputDesc->device(), outputDesc->mPatchHandle);
3863 return outputDesc->device();
3864 }
3865 }
3866
Eric Laurente552edb2014-03-10 17:42:56 -07003867 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003868 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003869 // use device for strategy enforced audible
3870 // 2: we are in call or the strategy phone is active on the output:
3871 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003872 // 3: the strategy for enforced audible is active but not enforced on the output:
3873 // use the device for strategy enforced audible
3874 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003875 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003876 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003877 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003878 // 6: the strategy accessibility is active on the output:
3879 // use device for strategy accessibility
3880 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003881 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003882 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003883 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003884 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003885 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003886 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003887 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003888 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3889 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003890 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003891 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003892 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003893 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003894 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003895 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003896 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003897 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003898 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003899 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003900 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003901 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003902 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003903 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003904 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003905 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003906 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003907 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003908 }
3909
Eric Laurent1c333e22014-05-20 10:48:17 -07003910 ALOGV("getNewOutputDevice() selected device %x", device);
3911 return device;
3912}
3913
3914audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3915{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003916 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003917
3918 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3919 if (index >= 0) {
3920 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3921 if (patchDesc->mUid != mUidCached) {
3922 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3923 inputDesc->mDevice, inputDesc->mPatchHandle);
3924 return inputDesc->mDevice;
3925 }
3926 }
3927
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003928 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003929
Eric Laurente552edb2014-03-10 17:42:56 -07003930 return device;
3931}
3932
Eric Laurente0720872014-03-11 09:30:41 -07003933uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003934 return (uint32_t)getStrategy(stream);
3935}
3936
Eric Laurente0720872014-03-11 09:30:41 -07003937audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003938 // By checking the range of stream before calling getStrategy, we avoid
3939 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3940 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003941 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003942 return AUDIO_DEVICE_NONE;
3943 }
3944 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003945 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003946 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3947 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3948 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003949 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003950 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003951 devices = outputDesc->device();
3952 break;
3953 }
Eric Laurente552edb2014-03-10 17:42:56 -07003954 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003955
3956 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3957 and doesn't really need to.*/
3958 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3959 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3960 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3961 }
3962
Eric Laurente552edb2014-03-10 17:42:56 -07003963 return devices;
3964}
3965
François Gaffie2110e042015-03-24 08:41:51 +01003966routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3967{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003968 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003969 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003970}
3971
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003972uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3973 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003974 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3975 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3976 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003977 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3978 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3979 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003980 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003981 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003982}
3983
Eric Laurente0720872014-03-11 09:30:41 -07003984void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003985 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003986 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003987 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3988 updateDevicesAndOutputs();
3989 break;
3990 default:
3991 break;
3992 }
3993}
3994
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003995uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3996 switch(event) {
3997 case STARTING_OUTPUT:
3998 mBeaconMuteRefCount++;
3999 break;
4000 case STOPPING_OUTPUT:
4001 if (mBeaconMuteRefCount > 0) {
4002 mBeaconMuteRefCount--;
4003 }
4004 break;
4005 case STARTING_BEACON:
4006 mBeaconPlayingRefCount++;
4007 break;
4008 case STOPPING_BEACON:
4009 if (mBeaconPlayingRefCount > 0) {
4010 mBeaconPlayingRefCount--;
4011 }
4012 break;
4013 }
4014
4015 if (mBeaconMuteRefCount > 0) {
4016 // any playback causes beacon to be muted
4017 return setBeaconMute(true);
4018 } else {
4019 // no other playback: unmute when beacon starts playing, mute when it stops
4020 return setBeaconMute(mBeaconPlayingRefCount == 0);
4021 }
4022}
4023
4024uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4025 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4026 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4027 // keep track of muted state to avoid repeating mute/unmute operations
4028 if (mBeaconMuted != mute) {
4029 // mute/unmute AUDIO_STREAM_TTS on all outputs
4030 ALOGV("\t muting %d", mute);
4031 uint32_t maxLatency = 0;
4032 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004033 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004034 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004035 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004036 0 /*delay*/, AUDIO_DEVICE_NONE);
4037 const uint32_t latency = desc->latency() * 2;
4038 if (latency > maxLatency) {
4039 maxLatency = latency;
4040 }
4041 }
4042 mBeaconMuted = mute;
4043 return maxLatency;
4044 }
4045 return 0;
4046}
4047
Eric Laurente0720872014-03-11 09:30:41 -07004048audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004049 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004050{
Paul McLeanaa981192015-03-21 09:55:15 -07004051 // Routing
4052 // see if we have an explicit route
4053 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4054 // (getStrategy(stream)).
4055 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4056 // and activity count is non-zero
4057 // the device = the device from the descriptor in the RouteMap, and exit.
4058 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4059 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4060 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004061 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004062 return route->mDeviceDescriptor->type();
4063 }
4064 }
4065
Eric Laurente552edb2014-03-10 17:42:56 -07004066 if (fromCache) {
4067 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4068 strategy, mDeviceForStrategy[strategy]);
4069 return mDeviceForStrategy[strategy];
4070 }
François Gaffie2110e042015-03-24 08:41:51 +01004071 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004072}
4073
Eric Laurente0720872014-03-11 09:30:41 -07004074void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004075{
4076 for (int i = 0; i < NUM_STRATEGIES; i++) {
4077 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4078 }
4079 mPreviousOutputs = mOutputs;
4080}
4081
Eric Laurent1f2f2232014-06-02 12:01:23 -07004082uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004083 audio_devices_t prevDevice,
4084 uint32_t delayMs)
4085{
4086 // mute/unmute strategies using an incompatible device combination
4087 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4088 // if unmuting, unmute only after the specified delay
4089 if (outputDesc->isDuplicated()) {
4090 return 0;
4091 }
4092
4093 uint32_t muteWaitMs = 0;
4094 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004095 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004096
4097 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4098 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004099 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004100 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4101 bool doMute = false;
4102
4103 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4104 doMute = true;
4105 outputDesc->mStrategyMutedByDevice[i] = true;
4106 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4107 doMute = true;
4108 outputDesc->mStrategyMutedByDevice[i] = false;
4109 }
Eric Laurent99401132014-05-07 19:48:15 -07004110 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004111 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004112 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004113 // skip output if it does not share any device with current output
4114 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4115 == AUDIO_DEVICE_NONE) {
4116 continue;
4117 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004118 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4119 mute ? "muting" : "unmuting", i, curDevice);
4120 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004121 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004122 if (mute) {
4123 // FIXME: should not need to double latency if volume could be applied
4124 // immediately by the audioflinger mixer. We must account for the delay
4125 // between now and the next time the audioflinger thread for this output
4126 // will process a buffer (which corresponds to one buffer size,
4127 // usually 1/2 or 1/4 of the latency).
4128 if (muteWaitMs < desc->latency() * 2) {
4129 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004130 }
4131 }
4132 }
4133 }
4134 }
4135 }
4136
Eric Laurent99401132014-05-07 19:48:15 -07004137 // temporary mute output if device selection changes to avoid volume bursts due to
4138 // different per device volumes
4139 if (outputDesc->isActive() && (device != prevDevice)) {
4140 if (muteWaitMs < outputDesc->latency() * 2) {
4141 muteWaitMs = outputDesc->latency() * 2;
4142 }
4143 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004144 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004145 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004146 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004147 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004148 muteWaitMs *2, device);
4149 }
4150 }
4151 }
4152
Eric Laurente552edb2014-03-10 17:42:56 -07004153 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4154 if (muteWaitMs > delayMs) {
4155 muteWaitMs -= delayMs;
4156 usleep(muteWaitMs * 1000);
4157 return muteWaitMs;
4158 }
4159 return 0;
4160}
4161
Eric Laurentc75307b2015-03-17 15:29:32 -07004162uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004163 audio_devices_t device,
4164 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004165 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004166 audio_patch_handle_t *patchHandle,
4167 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004168{
Eric Laurentc75307b2015-03-17 15:29:32 -07004169 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004170 AudioParameter param;
4171 uint32_t muteWaitMs;
4172
4173 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004174 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4175 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004176 return muteWaitMs;
4177 }
4178 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4179 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004180 if ((device != AUDIO_DEVICE_NONE) &&
4181 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004182 return 0;
4183 }
4184
4185 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004186 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004187
4188 audio_devices_t prevDevice = outputDesc->mDevice;
4189
Paul McLeanaa981192015-03-21 09:55:15 -07004190 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004191
4192 if (device != AUDIO_DEVICE_NONE) {
4193 outputDesc->mDevice = device;
4194 }
4195 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4196
4197 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004198 // the requested device is AUDIO_DEVICE_NONE
4199 // OR the requested device is the same as current device
4200 // AND force is not specified
4201 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004202 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004203 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4204 !force &&
4205 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004206 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004207 return muteWaitMs;
4208 }
4209
4210 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004211
Eric Laurente552edb2014-03-10 17:42:56 -07004212 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004213 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004214 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004215 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004216 DeviceVector deviceList = (address == NULL) ?
4217 mAvailableOutputDevices.getDevicesFromType(device)
4218 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004219 if (!deviceList.isEmpty()) {
4220 struct audio_patch patch;
4221 outputDesc->toAudioPortConfig(&patch.sources[0]);
4222 patch.num_sources = 1;
4223 patch.num_sinks = 0;
4224 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4225 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004226 patch.num_sinks++;
4227 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004228 ssize_t index;
4229 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4230 index = mAudioPatches.indexOfKey(*patchHandle);
4231 } else {
4232 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4233 }
4234 sp< AudioPatch> patchDesc;
4235 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4236 if (index >= 0) {
4237 patchDesc = mAudioPatches.valueAt(index);
4238 afPatchHandle = patchDesc->mAfPatchHandle;
4239 }
4240
Eric Laurent1c333e22014-05-20 10:48:17 -07004241 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004242 &afPatchHandle,
4243 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004244 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4245 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004246 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004247 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004248 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004249 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004250 addAudioPatch(patchDesc->mHandle, patchDesc);
4251 } else {
4252 patchDesc->mPatch = patch;
4253 }
4254 patchDesc->mAfPatchHandle = afPatchHandle;
4255 patchDesc->mUid = mUidCached;
4256 if (patchHandle) {
4257 *patchHandle = patchDesc->mHandle;
4258 }
4259 outputDesc->mPatchHandle = patchDesc->mHandle;
4260 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004261 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004262 }
4263 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004264
4265 // inform all input as well
4266 for (size_t i = 0; i < mInputs.size(); i++) {
4267 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004268 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004269 AudioParameter inputCmd = AudioParameter();
4270 ALOGV("%s: inform input %d of device:%d", __func__,
4271 inputDescriptor->mIoHandle, device);
4272 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4273 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4274 inputCmd.toString(),
4275 delayMs);
4276 }
4277 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004278 }
Eric Laurente552edb2014-03-10 17:42:56 -07004279
4280 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004281 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004282
4283 return muteWaitMs;
4284}
4285
Eric Laurentc75307b2015-03-17 15:29:32 -07004286status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004287 int delayMs,
4288 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004289{
Eric Laurent6a94d692014-05-20 11:18:06 -07004290 ssize_t index;
4291 if (patchHandle) {
4292 index = mAudioPatches.indexOfKey(*patchHandle);
4293 } else {
4294 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4295 }
4296 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004297 return INVALID_OPERATION;
4298 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004299 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4300 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004301 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4302 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004303 removeAudioPatch(patchDesc->mHandle);
4304 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004305 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 return status;
4307}
4308
4309status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4310 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004311 bool force,
4312 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004313{
4314 status_t status = NO_ERROR;
4315
Eric Laurent1f2f2232014-06-02 12:01:23 -07004316 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004317 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4318 inputDesc->mDevice = device;
4319
4320 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4321 if (!deviceList.isEmpty()) {
4322 struct audio_patch patch;
4323 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004324 // AUDIO_SOURCE_HOTWORD is for internal use only:
4325 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004326 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4327 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004328 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4329 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004330 patch.num_sinks = 1;
4331 //only one input device for now
4332 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004333 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004334 ssize_t index;
4335 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4336 index = mAudioPatches.indexOfKey(*patchHandle);
4337 } else {
4338 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4339 }
4340 sp< AudioPatch> patchDesc;
4341 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4342 if (index >= 0) {
4343 patchDesc = mAudioPatches.valueAt(index);
4344 afPatchHandle = patchDesc->mAfPatchHandle;
4345 }
4346
Eric Laurent1c333e22014-05-20 10:48:17 -07004347 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004348 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004349 0);
4350 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004351 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004352 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004353 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004354 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004355 addAudioPatch(patchDesc->mHandle, patchDesc);
4356 } else {
4357 patchDesc->mPatch = patch;
4358 }
4359 patchDesc->mAfPatchHandle = afPatchHandle;
4360 patchDesc->mUid = mUidCached;
4361 if (patchHandle) {
4362 *patchHandle = patchDesc->mHandle;
4363 }
4364 inputDesc->mPatchHandle = patchDesc->mHandle;
4365 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004366 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004367 }
4368 }
4369 }
4370 return status;
4371}
4372
Eric Laurent6a94d692014-05-20 11:18:06 -07004373status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4374 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004375{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004376 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004377 ssize_t index;
4378 if (patchHandle) {
4379 index = mAudioPatches.indexOfKey(*patchHandle);
4380 } else {
4381 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4382 }
4383 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004384 return INVALID_OPERATION;
4385 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004386 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4387 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004388 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4389 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004390 removeAudioPatch(patchDesc->mHandle);
4391 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004392 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004393 return status;
4394}
4395
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004396sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004397 String8 address,
4398 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004399 audio_format_t& format,
4400 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004401 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004402{
4403 // Choose an input profile based on the requested capture parameters: select the first available
4404 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004405 //
4406 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4407 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004408
4409 for (size_t i = 0; i < mHwModules.size(); i++)
4410 {
4411 if (mHwModules[i]->mHandle == 0) {
4412 continue;
4413 }
4414 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4415 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004416 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004417 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004418 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004419 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004420 format,
4421 &format /*updatedFormat*/,
4422 channelMask,
4423 &channelMask /*updatedChannelMask*/,
4424 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004425
Eric Laurente552edb2014-03-10 17:42:56 -07004426 return profile;
4427 }
4428 }
4429 }
4430 return NULL;
4431}
4432
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004433
4434audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004435 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004436{
François Gaffie036e1e92015-03-19 10:16:24 +01004437 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4438 audio_devices_t selectedDeviceFromMix =
4439 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004440
François Gaffie036e1e92015-03-19 10:16:24 +01004441 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4442 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004443 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004444 return getDeviceForInputSource(inputSource);
4445}
4446
4447audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4448{
Paul McLean466dc8e2015-04-17 13:15:36 -06004449 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4450 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004451 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004452 return route->mDeviceDescriptor->type();
4453 }
4454 }
4455
4456 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004457}
4458
Eric Laurente0720872014-03-11 09:30:41 -07004459float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004460 int index,
4461 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004462{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004463 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004464
4465 // if a headset is connected, apply the following rules to ring tones and notifications
4466 // to avoid sound level bursts in user's ears:
4467 // - always attenuate ring tones and notifications volume by 6dB
4468 // - if music is playing, always limit the volume to current music volume,
4469 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004470 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004471 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4472 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4473 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4474 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4475 ((stream_strategy == STRATEGY_SONIFICATION)
4476 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004477 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004478 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004479 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4480 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004481 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004482 // when the phone is ringing we must consider that music could have been paused just before
4483 // by the music application and behave as if music was active if the last music track was
4484 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004485 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004486 mLimitRingtoneVolume) {
4487 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004488 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4489 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004490 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004491 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4492 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4493 if (volumeDb > minVolDB) {
4494 volumeDb = minVolDB;
4495 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004496 }
4497 }
4498 }
4499
Eric Laurentffbc80f2015-03-18 18:30:19 -07004500 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004501}
4502
Eric Laurente0720872014-03-11 09:30:41 -07004503status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004504 int index,
4505 const sp<AudioOutputDescriptor>& outputDesc,
4506 audio_devices_t device,
4507 int delayMs,
4508 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004509{
Eric Laurente552edb2014-03-10 17:42:56 -07004510 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004511 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004512 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004513 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004514 return NO_ERROR;
4515 }
François Gaffie2110e042015-03-24 08:41:51 +01004516 audio_policy_forced_cfg_t forceUseForComm =
4517 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004518 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004519 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4520 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004521 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004522 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004523 return INVALID_OPERATION;
4524 }
4525
Eric Laurentc75307b2015-03-17 15:29:32 -07004526 if (device == AUDIO_DEVICE_NONE) {
4527 device = outputDesc->device();
4528 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004529
Eric Laurentffbc80f2015-03-18 18:30:19 -07004530 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004531 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004532 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004533 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004534
Eric Laurentffbc80f2015-03-18 18:30:19 -07004535 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004536
Eric Laurent3b73df72014-03-11 09:06:29 -07004537 if (stream == AUDIO_STREAM_VOICE_CALL ||
4538 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004539 float voiceVolume;
4540 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004541 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004542 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004543 } else {
4544 voiceVolume = 1.0;
4545 }
4546
Eric Laurentc75307b2015-03-17 15:29:32 -07004547 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004548 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4549 mLastVoiceVolume = voiceVolume;
4550 }
4551 }
4552
4553 return NO_ERROR;
4554}
4555
Eric Laurentc75307b2015-03-17 15:29:32 -07004556void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4557 audio_devices_t device,
4558 int delayMs,
4559 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004560{
Eric Laurentc75307b2015-03-17 15:29:32 -07004561 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004562
Eric Laurent3b73df72014-03-11 09:06:29 -07004563 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004564 if (stream == AUDIO_STREAM_PATCH) {
4565 continue;
4566 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004567 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004568 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4569 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004570 device,
4571 delayMs,
4572 force);
4573 }
4574}
4575
Eric Laurente0720872014-03-11 09:30:41 -07004576void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004577 bool on,
4578 const sp<AudioOutputDescriptor>& outputDesc,
4579 int delayMs,
4580 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004581{
4582 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004583 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004584 if (stream == AUDIO_STREAM_PATCH) {
4585 continue;
4586 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004587 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004588 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004589 }
4590 }
4591}
4592
Eric Laurente0720872014-03-11 09:30:41 -07004593void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004594 bool on,
4595 const sp<AudioOutputDescriptor>& outputDesc,
4596 int delayMs,
4597 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004598{
Eric Laurentc75307b2015-03-17 15:29:32 -07004599 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004600 if (device == AUDIO_DEVICE_NONE) {
4601 device = outputDesc->device();
4602 }
4603
Eric Laurentc75307b2015-03-17 15:29:32 -07004604 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4605 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004606
4607 if (on) {
4608 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004609 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004610 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004611 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004612 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004613 }
4614 }
4615 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4616 outputDesc->mMuteCount[stream]++;
4617 } else {
4618 if (outputDesc->mMuteCount[stream] == 0) {
4619 ALOGV("setStreamMute() unmuting non muted stream!");
4620 return;
4621 }
4622 if (--outputDesc->mMuteCount[stream] == 0) {
4623 checkAndSetVolume(stream,
4624 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004625 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004626 device,
4627 delayMs);
4628 }
4629 }
4630}
4631
Eric Laurente0720872014-03-11 09:30:41 -07004632void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004633 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004634{
4635 // if the stream pertains to sonification strategy and we are in call we must
4636 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4637 // in the device used for phone strategy and play the tone if the selected device does not
4638 // interfere with the device used for phone strategy
4639 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4640 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004641 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004642 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4643 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004644 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004645 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4646 stream, starting, outputDesc->mDevice, stateChange);
4647 if (outputDesc->mRefCount[stream]) {
4648 int muteCount = 1;
4649 if (stateChange) {
4650 muteCount = outputDesc->mRefCount[stream];
4651 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004652 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004653 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4654 for (int i = 0; i < muteCount; i++) {
4655 setStreamMute(stream, starting, mPrimaryOutput);
4656 }
4657 } else {
4658 ALOGV("handleIncallSonification() high visibility");
4659 if (outputDesc->device() &
4660 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4661 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4662 for (int i = 0; i < muteCount; i++) {
4663 setStreamMute(stream, starting, mPrimaryOutput);
4664 }
4665 }
4666 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004667 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4668 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004669 } else {
4670 mpClientInterface->stopTone();
4671 }
4672 }
4673 }
4674 }
4675}
4676
Paul McLeanaa981192015-03-21 09:55:15 -07004677// --- SessionRoute class implementation
4678void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004679 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4680 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004681 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4682 mRefCount, mActivityCount);
4683}
4684
4685// --- SessionRouteMap class implementation
4686bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4687{
4688 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4689}
4690
Eric Laurent493404d2015-04-21 15:07:36 -07004691bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4692{
4693 if (indexOfKey(session) >= 0) {
4694 if (valueFor(session)->mChanged) {
4695 valueFor(session)->mChanged = false;
4696 return true;
4697 }
4698 }
4699 return false;
4700}
4701
Paul McLeanaa981192015-03-21 09:55:15 -07004702void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4703{
4704 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4705 if (route != 0) {
4706 ALOG_ASSERT(route->mRefCount > 0);
4707 --route->mRefCount;
4708 if (route->mRefCount <= 0) {
4709 removeItem(session);
4710 }
4711 }
4712}
4713
4714int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4715{
4716 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4717 return route != 0 ? ++(route->mActivityCount) : -1;
4718}
4719
4720int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4721{
4722 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4723 if (route != 0 && route->mActivityCount > 0) {
4724 return --(route->mActivityCount);
4725 } else {
4726 return -1;
4727 }
4728}
4729
4730void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4731 ALOGI("%s ----", caption);
4732 for(size_t index = 0; index < size(); index++) {
4733 valueAt(index)->log(" ");
4734 }
4735}
4736
Paul McLean466dc8e2015-04-17 13:15:36 -06004737void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4738 audio_stream_type_t streamType,
4739 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004740 sp<DeviceDescriptor> descriptor,
4741 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004742{
4743 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4744 ALOGE("Adding Output Route to InputRouteMap");
4745 return;
4746 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4747 ALOGE("Adding Input Route to OutputRouteMap");
4748 return;
4749 }
4750
4751 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4752
4753 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004754 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4755 ((route->mDeviceDescriptor != 0) &&
4756 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004757 route->mChanged = true;
4758 }
4759 route->mRefCount++;
4760 route->mDeviceDescriptor = descriptor;
4761 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004762 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004763 route->mRefCount++;
4764 add(session, route);
4765 if (descriptor != 0) {
4766 route->mChanged = true;
4767 }
4768 }
4769}
4770
Eric Laurente0720872014-03-11 09:30:41 -07004771void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004772{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004773 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004774 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004775 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004776 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004777 mAvailableOutputDevices.add(mDefaultOutputDevice);
4778 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004779
4780 module = new HwModule("primary");
4781
Eric Laurent322b4d22015-04-03 15:57:54 -07004782 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4783 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004784 profile->mSamplingRates.add(44100);
4785 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4786 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004787 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004788 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4789 module->mOutputProfiles.add(profile);
4790
Eric Laurent322b4d22015-04-03 15:57:54 -07004791 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4792 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004793 profile->mSamplingRates.add(8000);
4794 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4795 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004796 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004797 module->mInputProfiles.add(profile);
4798
4799 mHwModules.add(module);
4800}
4801
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004802audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4803{
4804 // flags to stream type mapping
4805 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4806 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4807 }
4808 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4809 return AUDIO_STREAM_BLUETOOTH_SCO;
4810 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004811 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4812 return AUDIO_STREAM_TTS;
4813 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004814
4815 // usage to stream type mapping
4816 switch (attr->usage) {
4817 case AUDIO_USAGE_MEDIA:
4818 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004819 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4820 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004821 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004822 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4823 return AUDIO_STREAM_ALARM;
4824 }
4825 if (isStreamActive(AUDIO_STREAM_RING)) {
4826 return AUDIO_STREAM_RING;
4827 }
4828 if (isInCall()) {
4829 return AUDIO_STREAM_VOICE_CALL;
4830 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004831 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004832 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4833 return AUDIO_STREAM_SYSTEM;
4834 case AUDIO_USAGE_VOICE_COMMUNICATION:
4835 return AUDIO_STREAM_VOICE_CALL;
4836
4837 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4838 return AUDIO_STREAM_DTMF;
4839
4840 case AUDIO_USAGE_ALARM:
4841 return AUDIO_STREAM_ALARM;
4842 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4843 return AUDIO_STREAM_RING;
4844
4845 case AUDIO_USAGE_NOTIFICATION:
4846 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4847 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4848 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4849 case AUDIO_USAGE_NOTIFICATION_EVENT:
4850 return AUDIO_STREAM_NOTIFICATION;
4851
4852 case AUDIO_USAGE_UNKNOWN:
4853 default:
4854 return AUDIO_STREAM_MUSIC;
4855 }
4856}
Eric Laurente83b55d2014-11-14 10:06:21 -08004857
François Gaffie53615e22015-03-19 09:24:12 +01004858bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4859{
Eric Laurente83b55d2014-11-14 10:06:21 -08004860 // has flags that map to a strategy?
4861 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4862 return true;
4863 }
4864
4865 // has known usage?
4866 switch (paa->usage) {
4867 case AUDIO_USAGE_UNKNOWN:
4868 case AUDIO_USAGE_MEDIA:
4869 case AUDIO_USAGE_VOICE_COMMUNICATION:
4870 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4871 case AUDIO_USAGE_ALARM:
4872 case AUDIO_USAGE_NOTIFICATION:
4873 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4874 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4875 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4876 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4877 case AUDIO_USAGE_NOTIFICATION_EVENT:
4878 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4879 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4880 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4881 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004882 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004883 break;
4884 default:
4885 return false;
4886 }
4887 return true;
4888}
4889
François Gaffiead3183e2015-03-18 16:55:35 +01004890bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4891 routing_strategy strategy, uint32_t inPastMs,
4892 nsecs_t sysTime) const
4893{
4894 if ((sysTime == 0) && (inPastMs != 0)) {
4895 sysTime = systemTime();
4896 }
4897 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4898 if (i == AUDIO_STREAM_PATCH) {
4899 continue;
4900 }
4901 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4902 (NUM_STRATEGIES == strategy)) &&
4903 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4904 return true;
4905 }
4906 }
4907 return false;
4908}
4909
François Gaffie2110e042015-03-24 08:41:51 +01004910audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4911{
4912 return mEngine->getForceUse(usage);
4913}
4914
4915bool AudioPolicyManager::isInCall()
4916{
4917 return isStateInCall(mEngine->getPhoneState());
4918}
4919
4920bool AudioPolicyManager::isStateInCall(int state)
4921{
4922 return is_state_in_call(state);
4923}
4924
Eric Laurente552edb2014-03-10 17:42:56 -07004925}; // namespace android