blob: 6d99640ec629e3e9e050bfaf14effc289131dc82 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Paul McLean9080a4c2015-06-18 08:24:02 -0700216 if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Paul McLean9080a4c2015-06-18 08:24:02 -0700250 checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
405 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
406 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
407 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
408 status);
409 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100410 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 mCallTxPatch->mAfPatchHandle = afPatchHandle;
412 mCallTxPatch->mUid = mUidCached;
413 }
414 }
415}
416
Eric Laurente0720872014-03-11 09:30:41 -0700417void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700418{
419 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100420 // store previous phone state for management of sonification strategy below
421 int oldState = mEngine->getPhoneState();
422
423 if (mEngine->setPhoneState(state) != NO_ERROR) {
424 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700425 return;
426 }
François Gaffie2110e042015-03-24 08:41:51 +0100427 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700428 // if leaving call state, handle special case of active streams
429 // pertaining to sonification strategy see handleIncallSonification()
Eric Laurent63dea1d2015-07-02 17:10:28 -0700430 if (isStateInCall(oldState)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700431 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (stream == AUDIO_STREAM_PATCH) {
434 continue;
435 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700436 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800438
Eric Laurent63dea1d2015-07-02 17:10:28 -0700439 // force reevaluating accessibility routing when call stops
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800440 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 }
442
François Gaffie2110e042015-03-24 08:41:51 +0100443 /**
444 * Switching to or from incall state or switching between telephony and VoIP lead to force
445 * routing command.
446 */
447 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
448 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700449
450 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700451 checkA2dpSuspend();
452 checkOutputForAllStrategies();
453 updateDevicesAndOutputs();
454
Eric Laurente552edb2014-03-10 17:42:56 -0700455 int delayMs = 0;
456 if (isStateInCall(state)) {
457 nsecs_t sysTime = systemTime();
458 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700459 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // mute media and sonification strategies and delay device switch by the largest
461 // latency of any output where either strategy is active.
462 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100463 if ((isStrategyActive(desc, STRATEGY_MEDIA,
464 SONIFICATION_HEADSET_MUSIC_DELAY,
465 sysTime) ||
466 isStrategyActive(desc, STRATEGY_SONIFICATION,
467 SONIFICATION_HEADSET_MUSIC_DELAY,
468 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700469 (delayMs < (int)desc->latency()*2)) {
470 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700472 setStrategyMute(STRATEGY_MEDIA, true, desc);
473 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700474 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700475 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
476 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700477 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
478 }
479 }
480
Eric Laurent87ffa392015-05-22 10:32:38 -0700481 if (hasPrimaryOutput()) {
482 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
483 // the device returned is not necessarily reachable via this output
484 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
485 // force routing command to audio hardware when ending call
486 // even if no device change is needed
487 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
488 rxDevice = mPrimaryOutput->device();
489 }
Eric Laurente552edb2014-03-10 17:42:56 -0700490
Eric Laurent87ffa392015-05-22 10:32:38 -0700491 if (state == AUDIO_MODE_IN_CALL) {
492 updateCallRouting(rxDevice, delayMs);
493 } else if (oldState == AUDIO_MODE_IN_CALL) {
494 if (mCallRxPatch != 0) {
495 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
496 mCallRxPatch.clear();
497 }
498 if (mCallTxPatch != 0) {
499 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
500 mCallTxPatch.clear();
501 }
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 } else {
504 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700506 }
Eric Laurente552edb2014-03-10 17:42:56 -0700507 // if entering in call state, handle special case of active streams
508 // pertaining to sonification strategy see handleIncallSonification()
509 if (isStateInCall(state)) {
510 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800512 if (stream == AUDIO_STREAM_PATCH) {
513 continue;
514 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700515 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 }
Eric Laurent63dea1d2015-07-02 17:10:28 -0700517
518 // force reevaluating accessibility routing when call starts
519 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700520 }
521
522 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700523 if (state == AUDIO_MODE_RINGTONE &&
524 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700525 mLimitRingtoneVolume = true;
526 } else {
527 mLimitRingtoneVolume = false;
528 }
529}
530
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700531audio_mode_t AudioPolicyManager::getPhoneState() {
532 return mEngine->getPhoneState();
533}
534
Eric Laurente0720872014-03-11 09:30:41 -0700535void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700536 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700537{
François Gaffie2110e042015-03-24 08:41:51 +0100538 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700539
François Gaffie2110e042015-03-24 08:41:51 +0100540 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
541 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
542 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700543 }
François Gaffie2110e042015-03-24 08:41:51 +0100544 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
545 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
546 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700547
548 // check for device and output changes triggered by new force usage
549 checkA2dpSuspend();
550 checkOutputForAllStrategies();
551 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700552 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700553 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
554 updateCallRouting(newDevice);
555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
558 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
559 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
560 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700561 }
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700563 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565 }
566
François Gaffie53615e22015-03-19 09:24:12 +0100567 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700568 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700569 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700570 }
571
572}
573
Eric Laurente0720872014-03-11 09:30:41 -0700574void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700575{
576 ALOGV("setSystemProperty() property %s, value %s", property, value);
577}
578
579// Find a direct output profile compatible with the parameters passed, even if the input flags do
580// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800581sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700582 audio_devices_t device,
583 uint32_t samplingRate,
584 audio_format_t format,
585 audio_channel_mask_t channelMask,
586 audio_output_flags_t flags)
587{
Eric Laurent861a6282015-05-18 15:40:16 -0700588 // only retain flags that will drive the direct output profile selection
589 // if explicitly requested
590 static const uint32_t kRelevantFlags =
591 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
592 flags =
593 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
594
595 sp<IOProfile> profile;
596
Eric Laurente552edb2014-03-10 17:42:56 -0700597 for (size_t i = 0; i < mHwModules.size(); i++) {
598 if (mHwModules[i]->mHandle == 0) {
599 continue;
600 }
601 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700602 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
603 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700604 samplingRate, NULL /*updatedSamplingRate*/,
605 format, NULL /*updatedFormat*/,
606 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700607 flags)) {
608 continue;
609 }
610 // reject profiles not corresponding to a device currently available
611 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
612 continue;
613 }
614 // if several profiles are compatible, give priority to one with offload capability
615 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
616 continue;
617 }
618 profile = curProfile;
619 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
620 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700621 }
Eric Laurente552edb2014-03-10 17:42:56 -0700622 }
623 }
Eric Laurent861a6282015-05-18 15:40:16 -0700624 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700625}
626
Eric Laurente0720872014-03-11 09:30:41 -0700627audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100628 uint32_t samplingRate,
629 audio_format_t format,
630 audio_channel_mask_t channelMask,
631 audio_output_flags_t flags,
632 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700633{
Eric Laurent3b73df72014-03-11 09:06:29 -0700634 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700635 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
636 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
637 device, stream, samplingRate, format, channelMask, flags);
638
Eric Laurente83b55d2014-11-14 10:06:21 -0800639 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
640 stream, samplingRate,format, channelMask,
641 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700642}
643
Eric Laurente83b55d2014-11-14 10:06:21 -0800644status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
645 audio_io_handle_t *output,
646 audio_session_t session,
647 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700648 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800649 uint32_t samplingRate,
650 audio_format_t format,
651 audio_channel_mask_t channelMask,
652 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700653 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800654 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700655{
Eric Laurente83b55d2014-11-14 10:06:21 -0800656 audio_attributes_t attributes;
657 if (attr != NULL) {
658 if (!isValidAttributes(attr)) {
659 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
660 attr->usage, attr->content_type, attr->flags,
661 attr->tags);
662 return BAD_VALUE;
663 }
664 attributes = *attr;
665 } else {
666 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
667 ALOGE("getOutputForAttr(): invalid stream type");
668 return BAD_VALUE;
669 }
670 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700671 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700672 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100673 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
674 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
675 if (!audio_is_linear_pcm(format)) {
676 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800677 }
François Gaffie036e1e92015-03-19 10:16:24 +0100678 *stream = streamTypefromAttributesInt(&attributes);
679 *output = desc->mIoHandle;
680 ALOGV("getOutputForAttr() returns output %d", *output);
681 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800682 }
683 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
684 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
685 return BAD_VALUE;
686 }
687
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700688 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
689 " session %d selectedDeviceId %d",
690 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
691 session, selectedDeviceId);
692
693 *stream = streamTypefromAttributesInt(&attributes);
694
695 // Explicit routing?
696 sp<DeviceDescriptor> deviceDesc;
697 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
698 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
699 deviceDesc = mAvailableOutputDevices[i];
700 break;
701 }
702 }
703 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700704
Eric Laurente83b55d2014-11-14 10:06:21 -0800705 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700706 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700707
Eric Laurente83b55d2014-11-14 10:06:21 -0800708 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700709 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
710 }
711
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700712 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700713 device, samplingRate, format, channelMask, flags);
714
Eric Laurente83b55d2014-11-14 10:06:21 -0800715 *output = getOutputForDevice(device, session, *stream,
716 samplingRate, format, channelMask,
717 flags, offloadInfo);
718 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700719 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800720 return INVALID_OPERATION;
721 }
Paul McLeanaa981192015-03-21 09:55:15 -0700722
Eric Laurente83b55d2014-11-14 10:06:21 -0800723 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700724}
725
726audio_io_handle_t AudioPolicyManager::getOutputForDevice(
727 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800728 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700729 audio_stream_type_t stream,
730 uint32_t samplingRate,
731 audio_format_t format,
732 audio_channel_mask_t channelMask,
733 audio_output_flags_t flags,
734 const audio_offload_info_t *offloadInfo)
735{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700736 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700737 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700738 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700739
Eric Laurente552edb2014-03-10 17:42:56 -0700740#ifdef AUDIO_POLICY_TEST
741 if (mCurOutput != 0) {
742 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
743 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
744
745 if (mTestOutputs[mCurOutput] == 0) {
746 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700747 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
748 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700749 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700750 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700751 outputDesc->mFlags =
752 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700753 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700754 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
755 config.sample_rate = mTestSamplingRate;
756 config.channel_mask = mTestChannels;
757 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700758 if (offloadInfo != NULL) {
759 config.offload_info = *offloadInfo;
760 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700761 status = mpClientInterface->openOutput(0,
762 &mTestOutputs[mCurOutput],
763 &config,
764 &outputDesc->mDevice,
765 String8(""),
766 &outputDesc->mLatency,
767 outputDesc->mFlags);
768 if (status == NO_ERROR) {
769 outputDesc->mSamplingRate = config.sample_rate;
770 outputDesc->mFormat = config.format;
771 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700772 AudioParameter outputCmd = AudioParameter();
773 outputCmd.addInt(String8("set_id"),mCurOutput);
774 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
775 addOutput(mTestOutputs[mCurOutput], outputDesc);
776 }
777 }
778 return mTestOutputs[mCurOutput];
779 }
780#endif //AUDIO_POLICY_TEST
781
782 // open a direct output if required by specified parameters
783 //force direct flag if offload flag is set: offloading implies a direct output stream
784 // and all common behaviors are driven by checking only the direct flag
785 // this should normally be set appropriately in the policy configuration file
786 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700787 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700788 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700789 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
790 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
791 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800792 // only allow deep buffering for music stream type
793 if (stream != AUDIO_STREAM_MUSIC) {
794 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
Ravi Kumar Alamanda439e4ed2015-04-03 12:13:21 -0700795 } else if (/* stream == AUDIO_STREAM_MUSIC && */
796 flags == AUDIO_OUTPUT_FLAG_NONE &&
797 property_get_bool("audio.deep_buffer.media", false /* default_value */)) {
798 // use DEEP_BUFFER as default output for music stream type
799 flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
Eric Laurente83b55d2014-11-14 10:06:21 -0800800 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700801 if (stream == AUDIO_STREAM_TTS) {
802 flags = AUDIO_OUTPUT_FLAG_TTS;
803 }
Eric Laurente552edb2014-03-10 17:42:56 -0700804
Eric Laurentb732cf52014-09-24 19:08:21 -0700805 sp<IOProfile> profile;
806
807 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700808 // and not explicitly requested
809 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
810 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700811 audio_channel_count_from_out_mask(channelMask) <= 2) {
812 goto non_direct_output;
813 }
814
Eric Laurente552edb2014-03-10 17:42:56 -0700815 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
816 // creating an offloaded track and tearing it down immediately after start when audioflinger
817 // detects there is an active non offloadable effect.
818 // FIXME: We should check the audio session here but we do not have it in this context.
819 // This may prevent offloading in rare situations where effects are left active by apps
820 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700821
Eric Laurente552edb2014-03-10 17:42:56 -0700822 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100823 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700824 profile = getProfileForDirectOutput(device,
825 samplingRate,
826 format,
827 channelMask,
828 (audio_output_flags_t)flags);
829 }
830
Eric Laurent1c333e22014-05-20 10:48:17 -0700831 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700832 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700833
834 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700835 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700836 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
837 outputDesc = desc;
838 // reuse direct output if currently open and configured with same parameters
839 if ((samplingRate == outputDesc->mSamplingRate) &&
840 (format == outputDesc->mFormat) &&
841 (channelMask == outputDesc->mChannelMask)) {
842 outputDesc->mDirectOpenCount++;
843 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
844 return mOutputs.keyAt(i);
845 }
846 }
847 }
848 // close direct output if currently open and configured with different parameters
849 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700850 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700851 }
Eric Laurent861a6282015-05-18 15:40:16 -0700852
853 // if the selected profile is offloaded and no offload info was specified,
854 // create a default one
855 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
856 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
857 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
858 defaultOffloadInfo.sample_rate = samplingRate;
859 defaultOffloadInfo.channel_mask = channelMask;
860 defaultOffloadInfo.format = format;
861 defaultOffloadInfo.stream_type = stream;
862 defaultOffloadInfo.bit_rate = 0;
863 defaultOffloadInfo.duration_us = -1;
864 defaultOffloadInfo.has_video = true; // conservative
865 defaultOffloadInfo.is_streaming = true; // likely
866 offloadInfo = &defaultOffloadInfo;
867 }
868
Eric Laurentc75307b2015-03-17 15:29:32 -0700869 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700870 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700871 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700872 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700873 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
874 config.sample_rate = samplingRate;
875 config.channel_mask = channelMask;
876 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700877 if (offloadInfo != NULL) {
878 config.offload_info = *offloadInfo;
879 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700880 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700881 &output,
882 &config,
883 &outputDesc->mDevice,
884 String8(""),
885 &outputDesc->mLatency,
886 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700887
888 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700889 if (status != NO_ERROR ||
890 (samplingRate != 0 && samplingRate != config.sample_rate) ||
891 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
892 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700893 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
894 "format %d %d, channelMask %04x %04x", output, samplingRate,
895 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
896 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700897 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700898 mpClientInterface->closeOutput(output);
899 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800900 // fall back to mixer output if possible when the direct output could not be open
901 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
902 goto non_direct_output;
903 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700904 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700905 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700906 outputDesc->mSamplingRate = config.sample_rate;
907 outputDesc->mChannelMask = config.channel_mask;
908 outputDesc->mFormat = config.format;
909 outputDesc->mRefCount[stream] = 0;
910 outputDesc->mStopTime[stream] = 0;
911 outputDesc->mDirectOpenCount = 1;
912
Eric Laurente552edb2014-03-10 17:42:56 -0700913 audio_io_handle_t srcOutput = getOutputForEffect();
914 addOutput(output, outputDesc);
915 audio_io_handle_t dstOutput = getOutputForEffect();
916 if (dstOutput == output) {
917 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
918 }
919 mPreviousOutputs = mOutputs;
920 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700921 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700922 return output;
923 }
924
Eric Laurentb732cf52014-09-24 19:08:21 -0700925non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700926 // ignoring channel mask due to downmix capability in mixer
927
928 // open a non direct output
929
930 // for non direct outputs, only PCM is supported
931 if (audio_is_linear_pcm(format)) {
932 // get which output is suitable for the specified stream. The actual
933 // routing change will happen when startOutput() will be called
934 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
935
Eric Laurent8838a382014-09-08 16:44:28 -0700936 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
937 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
938 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700939 }
940 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
941 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
942
Paul McLeanaa981192015-03-21 09:55:15 -0700943 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700944
945 return output;
946}
947
Eric Laurente0720872014-03-11 09:30:41 -0700948audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700949 audio_output_flags_t flags,
950 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700951{
952 // select one output among several that provide a path to a particular device or set of
953 // devices (the list was previously build by getOutputsForDevice()).
954 // The priority is as follows:
955 // 1: the output with the highest number of requested policy flags
956 // 2: the primary output
957 // 3: the first output in the list
958
959 if (outputs.size() == 0) {
960 return 0;
961 }
962 if (outputs.size() == 1) {
963 return outputs[0];
964 }
965
966 int maxCommonFlags = 0;
967 audio_io_handle_t outputFlags = 0;
968 audio_io_handle_t outputPrimary = 0;
969
970 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700971 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700972 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700973 // if a valid format is specified, skip output if not compatible
974 if (format != AUDIO_FORMAT_INVALID) {
975 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
976 if (format != outputDesc->mFormat) {
977 continue;
978 }
979 } else if (!audio_is_linear_pcm(format)) {
980 continue;
981 }
982 }
983
Eric Laurent3b73df72014-03-11 09:06:29 -0700984 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700985 if (commonFlags > maxCommonFlags) {
986 outputFlags = outputs[i];
987 maxCommonFlags = commonFlags;
988 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
989 }
990 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
991 outputPrimary = outputs[i];
992 }
993 }
994 }
995
996 if (outputFlags != 0) {
997 return outputFlags;
998 }
999 if (outputPrimary != 0) {
1000 return outputPrimary;
1001 }
1002
1003 return outputs[0];
1004}
1005
Eric Laurente0720872014-03-11 09:30:41 -07001006status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001007 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001008 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001009{
Paul McLeanaa981192015-03-21 09:55:15 -07001010 ALOGV("startOutput() output %d, stream %d, session %d",
1011 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001012 ssize_t index = mOutputs.indexOfKey(output);
1013 if (index < 0) {
1014 ALOGW("startOutput() unknown output %d", output);
1015 return BAD_VALUE;
1016 }
1017
Eric Laurentc75307b2015-03-17 15:29:32 -07001018 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1019
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001020 // Routing?
1021 mOutputRoutes.incRouteActivity(session);
1022
Eric Laurentc75307b2015-03-17 15:29:32 -07001023 audio_devices_t newDevice;
1024 if (outputDesc->mPolicyMix != NULL) {
1025 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001026 } else if (mOutputRoutes.hasRouteChanged(session)) {
1027 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001028 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001029 } else {
1030 newDevice = AUDIO_DEVICE_NONE;
1031 }
1032
1033 uint32_t delayMs = 0;
1034
Eric Laurentc75307b2015-03-17 15:29:32 -07001035 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1036
1037 if (status != NO_ERROR) {
1038 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001039 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001040 }
1041 // Automatically enable the remote submix input when output is started on a re routing mix
1042 // of type MIX_TYPE_RECORDERS
1043 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1044 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1045 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1046 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1047 outputDesc->mPolicyMix->mRegistrationId,
1048 "remote-submix");
1049 }
1050
1051 if (delayMs != 0) {
1052 usleep(delayMs * 1000);
1053 }
1054
1055 return status;
1056}
1057
1058status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1059 audio_stream_type_t stream,
1060 audio_devices_t device,
1061 uint32_t *delayMs)
1062{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001063 // cannot start playback of STREAM_TTS if any other output is being used
1064 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001065
1066 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001067 if (stream == AUDIO_STREAM_TTS) {
1068 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001069 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001070 return INVALID_OPERATION;
1071 } else {
1072 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1073 }
1074 } else {
1075 // some playback other than beacon starts
1076 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1077 }
1078
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001079 // check active before incrementing usage count
1080 bool force = !outputDesc->isActive();
1081
Eric Laurente552edb2014-03-10 17:42:56 -07001082 // increment usage count for this stream on the requested output:
1083 // NOTE that the usage count is the same for duplicated output and hardware output which is
1084 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1085 outputDesc->changeRefCount(stream, 1);
1086
Eric Laurent493404d2015-04-21 15:07:36 -07001087 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001088 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001089 if (device == AUDIO_DEVICE_NONE) {
1090 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001091 }
Eric Laurente552edb2014-03-10 17:42:56 -07001092 routing_strategy strategy = getStrategy(stream);
1093 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001094 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1095 (beaconMuteLatency > 0);
1096 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001097 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001098 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001099 if (desc != outputDesc) {
1100 // force a device change if any other output is managed by the same hw
1101 // module and has a current device selection that differs from selected device.
1102 // In this case, the audio HAL must receive the new device selection so that it can
1103 // change the device currently selected by the other active output.
1104 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001105 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001106 force = true;
1107 }
1108 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001109 // a notification so that audio focus effect can propagate, or that a mute/unmute
1110 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001111 uint32_t latency = desc->latency();
1112 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1113 waitMs = latency;
1114 }
1115 }
1116 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001117 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001118
1119 // handle special case for sonification while in call
1120 if (isInCall()) {
1121 handleIncallSonification(stream, true, false);
1122 }
1123
1124 // apply volume rules for current stream and device if necessary
1125 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001126 mStreams.valueFor(stream).getVolumeIndex(device),
1127 outputDesc,
1128 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001129
1130 // update the outputs if starting an output with a stream that can affect notification
1131 // routing
1132 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001133
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001134 // force reevaluating accessibility routing when ringtone or alarm starts
1135 if (strategy == STRATEGY_SONIFICATION) {
1136 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1137 }
Eric Laurente552edb2014-03-10 17:42:56 -07001138 }
1139 return NO_ERROR;
1140}
1141
1142
Eric Laurente0720872014-03-11 09:30:41 -07001143status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001144 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001145 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001146{
1147 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1148 ssize_t index = mOutputs.indexOfKey(output);
1149 if (index < 0) {
1150 ALOGW("stopOutput() unknown output %d", output);
1151 return BAD_VALUE;
1152 }
1153
Eric Laurentc75307b2015-03-17 15:29:32 -07001154 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001155
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 if (outputDesc->mRefCount[stream] == 1) {
1157 // Automatically disable the remote submix input when output is stopped on a
1158 // re routing mix of type MIX_TYPE_RECORDERS
1159 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1160 outputDesc->mPolicyMix != NULL &&
1161 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1162 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1163 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1164 outputDesc->mPolicyMix->mRegistrationId,
1165 "remote-submix");
1166 }
1167 }
1168
1169 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001170 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001171 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001172 int activityCount = mOutputRoutes.decRouteActivity(session);
1173 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1174
1175 if (forceDeviceUpdate) {
1176 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1177 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001178 }
1179
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001180 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001181}
1182
1183status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001184 audio_stream_type_t stream,
1185 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001186{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001187 // always handle stream stop, check which stream type is stopping
1188 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1189
Eric Laurente552edb2014-03-10 17:42:56 -07001190 // handle special case for sonification while in call
1191 if (isInCall()) {
1192 handleIncallSonification(stream, false, false);
1193 }
1194
1195 if (outputDesc->mRefCount[stream] > 0) {
1196 // decrement usage count of this stream on the output
1197 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001198
Eric Laurente552edb2014-03-10 17:42:56 -07001199 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001200 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001201 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001202 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001203 // delay the device switch by twice the latency because stopOutput() is executed when
1204 // the track stop() command is received and at that time the audio track buffer can
1205 // still contain data that needs to be drained. The latency only covers the audio HAL
1206 // and kernel buffers. Also the latency does not always include additional delay in the
1207 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001209
1210 // force restoring the device selection on other active outputs if it differs from the
1211 // one being selected for this output
1212 for (size_t i = 0; i < mOutputs.size(); i++) {
1213 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001214 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001215 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001216 desc->isActive() &&
1217 outputDesc->sharesHwModuleWith(desc) &&
1218 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001219 setOutputDevice(desc,
1220 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001221 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001222 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001223 }
1224 }
1225 // update the outputs if stopping one with a stream that can affect notification routing
1226 handleNotificationRoutingForStream(stream);
1227 }
1228 return NO_ERROR;
1229 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001230 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001231 return INVALID_OPERATION;
1232 }
1233}
1234
Eric Laurente83b55d2014-11-14 10:06:21 -08001235void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001236 audio_stream_type_t stream __unused,
1237 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001238{
1239 ALOGV("releaseOutput() %d", output);
1240 ssize_t index = mOutputs.indexOfKey(output);
1241 if (index < 0) {
1242 ALOGW("releaseOutput() releasing unknown output %d", output);
1243 return;
1244 }
1245
1246#ifdef AUDIO_POLICY_TEST
1247 int testIndex = testOutputIndex(output);
1248 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001249 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001250 if (outputDesc->isActive()) {
1251 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001252 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001253 mTestOutputs[testIndex] = 0;
1254 }
1255 return;
1256 }
1257#endif //AUDIO_POLICY_TEST
1258
Paul McLeanaa981192015-03-21 09:55:15 -07001259 // Routing
1260 mOutputRoutes.removeRoute(session);
1261
Eric Laurentc75307b2015-03-17 15:29:32 -07001262 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001263 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001264 if (desc->mDirectOpenCount <= 0) {
1265 ALOGW("releaseOutput() invalid open count %d for output %d",
1266 desc->mDirectOpenCount, output);
1267 return;
1268 }
1269 if (--desc->mDirectOpenCount == 0) {
1270 closeOutput(output);
1271 // If effects where present on the output, audioflinger moved them to the primary
1272 // output by default: move them back to the appropriate output.
1273 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001274 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001275 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1276 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001277 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001278 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001279 }
1280 }
1281}
1282
1283
Eric Laurentcaf7f482014-11-25 17:50:47 -08001284status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1285 audio_io_handle_t *input,
1286 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001287 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001288 uint32_t samplingRate,
1289 audio_format_t format,
1290 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001291 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001292 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001293 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001294{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001295 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1296 "session %d, flags %#x",
1297 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001298
Eric Laurentcaf7f482014-11-25 17:50:47 -08001299 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001300 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001301 audio_devices_t device;
1302 // handle legacy remote submix case where the address was not always specified
1303 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001304 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001305 audio_source_t inputSource = attr->source;
1306 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001307 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001308
Eric Laurentc447ded2015-01-06 08:47:05 -08001309 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1310 inputSource = AUDIO_SOURCE_MIC;
1311 }
1312 halInputSource = inputSource;
1313
Paul McLean466dc8e2015-04-17 13:15:36 -06001314 // Explicit routing?
1315 sp<DeviceDescriptor> deviceDesc;
1316 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1317 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1318 deviceDesc = mAvailableInputDevices[i];
1319 break;
1320 }
1321 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001322 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001323
Eric Laurentc447ded2015-01-06 08:47:05 -08001324 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001325 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001326 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001327 if (ret != NO_ERROR) {
1328 return ret;
1329 }
1330 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001331 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1332 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001333 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001334 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001335 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001336 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001337 return BAD_VALUE;
1338 }
Eric Laurentc722f302014-12-10 11:21:49 -08001339 if (policyMix != NULL) {
1340 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001341 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1342 // there is an external policy, but this input is attached to a mix of recorders,
1343 // meaning it receives audio injected into the framework, so the recorder doesn't
1344 // know about it and is therefore considered "legacy"
1345 *inputType = API_INPUT_LEGACY;
1346 } else {
1347 // recording a mix of players defined by an external policy, we're rerouting for
1348 // an external policy
1349 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1350 }
Eric Laurentc722f302014-12-10 11:21:49 -08001351 } else if (audio_is_remote_submix_device(device)) {
1352 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001353 *inputType = API_INPUT_MIX_CAPTURE;
1354 } else {
1355 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001356 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001357 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001358 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001359 case AUDIO_SOURCE_VOICE_UPLINK:
1360 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1361 break;
1362 case AUDIO_SOURCE_VOICE_DOWNLINK:
1363 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1364 break;
1365 case AUDIO_SOURCE_VOICE_CALL:
1366 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1367 break;
1368 default:
1369 break;
1370 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001371 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001372 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1373 if (index >= 0) {
1374 *input = mSoundTriggerSessions.valueFor(session);
1375 isSoundTrigger = true;
1376 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1377 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1378 } else {
1379 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1380 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001381 }
1382 }
1383
Andy Hungf129b032015-04-07 13:45:50 -07001384 // find a compatible input profile (not necessarily identical in parameters)
1385 sp<IOProfile> profile;
1386 // samplingRate and flags may be updated by getInputProfile
1387 uint32_t profileSamplingRate = samplingRate;
1388 audio_format_t profileFormat = format;
1389 audio_channel_mask_t profileChannelMask = channelMask;
1390 audio_input_flags_t profileFlags = flags;
1391 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001392 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001393 profileSamplingRate, profileFormat, profileChannelMask,
1394 profileFlags);
1395 if (profile != 0) {
1396 break; // success
1397 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1398 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1399 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001400 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1401 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001402 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001403 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001404 }
Eric Laurente552edb2014-03-10 17:42:56 -07001405 }
1406
Eric Laurent322b4d22015-04-03 15:57:54 -07001407 if (profile->getModuleHandle() == 0) {
1408 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001409 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001410 }
1411
1412 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001413 config.sample_rate = profileSamplingRate;
1414 config.channel_mask = profileChannelMask;
1415 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001416
Eric Laurent322b4d22015-04-03 15:57:54 -07001417 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001418 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001419 &config,
1420 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001421 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001422 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001423 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001424
1425 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001426 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001427 (profileSamplingRate != config.sample_rate) ||
1428 (profileFormat != config.format) ||
1429 (profileChannelMask != config.channel_mask)) {
1430 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1431 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001432 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001433 if (*input != AUDIO_IO_HANDLE_NONE) {
1434 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001435 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001436 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001437 }
1438
Eric Laurent1f2f2232014-06-02 12:01:23 -07001439 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001440 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001441 inputDesc->mRefCount = 0;
1442 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001443 inputDesc->mSamplingRate = profileSamplingRate;
1444 inputDesc->mFormat = profileFormat;
1445 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001446 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001447 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001448 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001449 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001450
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001451 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001452
Eric Laurentcaf7f482014-11-25 17:50:47 -08001453 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001454 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001455
Eric Laurentcaf7f482014-11-25 17:50:47 -08001456 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001457}
1458
Eric Laurent4dc68062014-07-28 17:26:49 -07001459status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1460 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001461{
1462 ALOGV("startInput() input %d", input);
1463 ssize_t index = mInputs.indexOfKey(input);
1464 if (index < 0) {
1465 ALOGW("startInput() unknown input %d", input);
1466 return BAD_VALUE;
1467 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001468 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001469
Eric Laurentc722f302014-12-10 11:21:49 -08001470 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001471 if (index < 0) {
1472 ALOGW("startInput() unknown session %d on input %d", session, input);
1473 return BAD_VALUE;
1474 }
1475
Glenn Kasten74a8e252014-07-24 14:09:55 -07001476 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001477 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001478
1479 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001480 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001481 if (activeInput != 0 && activeInput != input) {
1482
1483 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1484 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001485 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001486 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001487 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001488 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1489 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001490 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001491 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001492 return INVALID_OPERATION;
1493 }
1494 }
1495 }
1496
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001497 // Routing?
1498 mInputRoutes.incRouteActivity(session);
1499
Paul McLean466dc8e2015-04-17 13:15:36 -06001500 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001501 // if input maps to a dynamic policy with an activity listener, notify of state change
1502 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001503 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001504 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1505 MIX_STATE_MIXING);
1506 }
1507
François Gaffie53615e22015-03-19 09:24:12 +01001508 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001509 SoundTrigger::setCaptureState(true);
1510 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001511 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001512
Eric Laurentc722f302014-12-10 11:21:49 -08001513 // automatically enable the remote submix output when input is started if not
1514 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001515 // For remote submix (a virtual device), we open only one input per capture request.
1516 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001517 String8 address = String8("");
1518 if (inputDesc->mPolicyMix == NULL) {
1519 address = String8("0");
1520 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1521 address = inputDesc->mPolicyMix->mRegistrationId;
1522 }
1523 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001524 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001525 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001526 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001527 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001528 }
Eric Laurente552edb2014-03-10 17:42:56 -07001529 }
1530
Eric Laurente552edb2014-03-10 17:42:56 -07001531 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1532
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001533 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001534 return NO_ERROR;
1535}
1536
Eric Laurent4dc68062014-07-28 17:26:49 -07001537status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1538 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001539{
1540 ALOGV("stopInput() input %d", input);
1541 ssize_t index = mInputs.indexOfKey(input);
1542 if (index < 0) {
1543 ALOGW("stopInput() unknown input %d", input);
1544 return BAD_VALUE;
1545 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001546 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001547
Eric Laurentc722f302014-12-10 11:21:49 -08001548 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001549 if (index < 0) {
1550 ALOGW("stopInput() unknown session %d on input %d", session, input);
1551 return BAD_VALUE;
1552 }
1553
Eric Laurente552edb2014-03-10 17:42:56 -07001554 if (inputDesc->mRefCount == 0) {
1555 ALOGW("stopInput() input %d already stopped", input);
1556 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001557 }
1558
1559 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001560
1561 // Routing?
1562 mInputRoutes.decRouteActivity(session);
1563
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001564 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001565 // if input maps to a dynamic policy with an activity listener, notify of state change
1566 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001567 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001568 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1569 MIX_STATE_IDLE);
1570 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001571
Eric Laurentc722f302014-12-10 11:21:49 -08001572 // automatically disable the remote submix output when input is stopped if not
1573 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001574 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001575 String8 address = String8("");
1576 if (inputDesc->mPolicyMix == NULL) {
1577 address = String8("0");
1578 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1579 address = inputDesc->mPolicyMix->mRegistrationId;
1580 }
1581 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001582 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001583 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001584 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001585 }
Eric Laurente552edb2014-03-10 17:42:56 -07001586 }
1587
Eric Laurent1c333e22014-05-20 10:48:17 -07001588 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001589
François Gaffie53615e22015-03-19 09:24:12 +01001590 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001591 SoundTrigger::setCaptureState(false);
1592 }
Eric Laurente552edb2014-03-10 17:42:56 -07001593 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001594 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001595}
1596
Eric Laurent4dc68062014-07-28 17:26:49 -07001597void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1598 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001599{
1600 ALOGV("releaseInput() %d", input);
1601 ssize_t index = mInputs.indexOfKey(input);
1602 if (index < 0) {
1603 ALOGW("releaseInput() releasing unknown input %d", input);
1604 return;
1605 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001606
1607 // Routing
1608 mInputRoutes.removeRoute(session);
1609
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001610 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1611 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001612
Eric Laurentc722f302014-12-10 11:21:49 -08001613 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001614 if (index < 0) {
1615 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1616 return;
1617 }
Eric Laurentc722f302014-12-10 11:21:49 -08001618 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001619 if (inputDesc->mOpenRefCount == 0) {
1620 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1621 return;
1622 }
1623 inputDesc->mOpenRefCount--;
1624 if (inputDesc->mOpenRefCount > 0) {
1625 ALOGV("releaseInput() exit > 0");
1626 return;
1627 }
1628
Eric Laurent05b90f82014-08-27 15:32:29 -07001629 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001630 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001631 ALOGV("releaseInput() exit");
1632}
1633
Eric Laurentd4692962014-05-05 18:13:44 -07001634void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001635 bool patchRemoved = false;
1636
Eric Laurentd4692962014-05-05 18:13:44 -07001637 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001638 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1639 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1640 if (patch_index >= 0) {
1641 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1642 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1643 mAudioPatches.removeItemsAt(patch_index);
1644 patchRemoved = true;
1645 }
Eric Laurentd4692962014-05-05 18:13:44 -07001646 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1647 }
1648 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001649 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001650
1651 if (patchRemoved) {
1652 mpClientInterface->onAudioPatchListUpdate();
1653 }
Eric Laurentd4692962014-05-05 18:13:44 -07001654}
1655
Eric Laurente0720872014-03-11 09:30:41 -07001656void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001657 int indexMin,
1658 int indexMax)
1659{
1660 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001661 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001662 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1663 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001664 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001665 }
Eric Laurente552edb2014-03-10 17:42:56 -07001666}
1667
Eric Laurente0720872014-03-11 09:30:41 -07001668status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001669 int index,
1670 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001671{
1672
Eric Laurentc75307b2015-03-17 15:29:32 -07001673 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1674 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001675 return BAD_VALUE;
1676 }
1677 if (!audio_is_output_device(device)) {
1678 return BAD_VALUE;
1679 }
1680
1681 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001682 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001683
1684 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1685 stream, device, index);
1686
1687 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1688 // clear all device specific values
1689 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001690 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001691 }
François Gaffiedfd74092015-03-19 12:10:59 +01001692 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001693
Eric Laurent31551f82014-10-10 18:21:56 -07001694 // update volume on all outputs whose current device is also selected by the same
1695 // strategy as the device specified by the caller
1696 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001697
1698
1699 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1700 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1701 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001702 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001703 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1704 }
1705 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1706 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001707 return NO_ERROR;
1708 }
Eric Laurente552edb2014-03-10 17:42:56 -07001709 status_t status = NO_ERROR;
1710 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001711 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1712 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001713 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001714 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001715 if (volStatus != NO_ERROR) {
1716 status = volStatus;
1717 }
1718 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001719 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1720 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001721 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001722 }
Eric Laurente552edb2014-03-10 17:42:56 -07001723 }
1724 return status;
1725}
1726
Eric Laurente0720872014-03-11 09:30:41 -07001727status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001728 int *index,
1729 audio_devices_t device)
1730{
1731 if (index == NULL) {
1732 return BAD_VALUE;
1733 }
1734 if (!audio_is_output_device(device)) {
1735 return BAD_VALUE;
1736 }
1737 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1738 // the strategy the stream belongs to.
1739 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1740 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1741 }
François Gaffiedfd74092015-03-19 12:10:59 +01001742 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001743
Eric Laurentc75307b2015-03-17 15:29:32 -07001744 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001745 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1746 return NO_ERROR;
1747}
1748
Eric Laurente0720872014-03-11 09:30:41 -07001749audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001750 const SortedVector<audio_io_handle_t>& outputs)
1751{
1752 // select one output among several suitable for global effects.
1753 // The priority is as follows:
1754 // 1: An offloaded output. If the effect ends up not being offloadable,
1755 // AudioFlinger will invalidate the track and the offloaded output
1756 // will be closed causing the effect to be moved to a PCM output.
1757 // 2: A deep buffer output
1758 // 3: the first output in the list
1759
1760 if (outputs.size() == 0) {
1761 return 0;
1762 }
1763
1764 audio_io_handle_t outputOffloaded = 0;
1765 audio_io_handle_t outputDeepBuffer = 0;
1766
1767 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001768 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001769 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001770 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1771 outputOffloaded = outputs[i];
1772 }
1773 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1774 outputDeepBuffer = outputs[i];
1775 }
1776 }
1777
1778 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1779 outputOffloaded, outputDeepBuffer);
1780 if (outputOffloaded != 0) {
1781 return outputOffloaded;
1782 }
1783 if (outputDeepBuffer != 0) {
1784 return outputDeepBuffer;
1785 }
1786
1787 return outputs[0];
1788}
1789
Eric Laurente0720872014-03-11 09:30:41 -07001790audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001791{
1792 // apply simple rule where global effects are attached to the same output as MUSIC streams
1793
Eric Laurent3b73df72014-03-11 09:06:29 -07001794 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001795 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1796 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1797
1798 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1799 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1800 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1801
1802 return output;
1803}
1804
Eric Laurente0720872014-03-11 09:30:41 -07001805status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001806 audio_io_handle_t io,
1807 uint32_t strategy,
1808 int session,
1809 int id)
1810{
1811 ssize_t index = mOutputs.indexOfKey(io);
1812 if (index < 0) {
1813 index = mInputs.indexOfKey(io);
1814 if (index < 0) {
1815 ALOGW("registerEffect() unknown io %d", io);
1816 return INVALID_OPERATION;
1817 }
1818 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001819 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001820}
1821
Eric Laurentc75307b2015-03-17 15:29:32 -07001822bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1823{
1824 return mOutputs.isStreamActive(stream, inPastMs);
1825}
1826
1827bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1828{
1829 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1830}
1831
Eric Laurente0720872014-03-11 09:30:41 -07001832bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001833{
1834 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001835 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001836 if (inputDescriptor->mRefCount == 0) {
1837 continue;
1838 }
1839 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001840 return true;
1841 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001842 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1843 // corresponds to an active capture triggered by a hardware hotword recognition
1844 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1845 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1846 // FIXME: we should not assume that the first session is the active one and keep
1847 // activity count per session. Same in startInput().
1848 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1849 if (index >= 0) {
1850 return true;
1851 }
1852 }
Eric Laurente552edb2014-03-10 17:42:56 -07001853 }
1854 return false;
1855}
1856
Eric Laurent275e8e92014-11-30 15:14:47 -08001857// Register a list of custom mixes with their attributes and format.
1858// When a mix is registered, corresponding input and output profiles are
1859// added to the remote submix hw module. The profile contains only the
1860// parameters (sampling rate, format...) specified by the mix.
1861// The corresponding input remote submix device is also connected.
1862//
1863// When a remote submix device is connected, the address is checked to select the
1864// appropriate profile and the corresponding input or output stream is opened.
1865//
1866// When capture starts, getInputForAttr() will:
1867// - 1 look for a mix matching the address passed in attribtutes tags if any
1868// - 2 if none found, getDeviceForInputSource() will:
1869// - 2.1 look for a mix matching the attributes source
1870// - 2.2 if none found, default to device selection by policy rules
1871// At this time, the corresponding output remote submix device is also connected
1872// and active playback use cases can be transferred to this mix if needed when reconnecting
1873// after AudioTracks are invalidated
1874//
1875// When playback starts, getOutputForAttr() will:
1876// - 1 look for a mix matching the address passed in attribtutes tags if any
1877// - 2 if none found, look for a mix matching the attributes usage
1878// - 3 if none found, default to device and output selection by policy rules.
1879
1880status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1881{
1882 sp<HwModule> module;
1883 for (size_t i = 0; i < mHwModules.size(); i++) {
1884 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1885 mHwModules[i]->mHandle != 0) {
1886 module = mHwModules[i];
1887 break;
1888 }
1889 }
1890
1891 if (module == 0) {
1892 return INVALID_OPERATION;
1893 }
1894
1895 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1896
1897 for (size_t i = 0; i < mixes.size(); i++) {
1898 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001899
1900 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001901 continue;
1902 }
1903 audio_config_t outputConfig = mixes[i].mFormat;
1904 audio_config_t inputConfig = mixes[i].mFormat;
1905 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1906 // stereo and let audio flinger do the channel conversion if needed.
1907 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1908 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1909 module->addOutputProfile(address, &outputConfig,
1910 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1911 module->addInputProfile(address, &inputConfig,
1912 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001913
Eric Laurentc722f302014-12-10 11:21:49 -08001914 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001915 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001916 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001917 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001918 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001919 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001920 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001921 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001922 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001923 }
1924 return NO_ERROR;
1925}
1926
1927status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1928{
1929 sp<HwModule> module;
1930 for (size_t i = 0; i < mHwModules.size(); i++) {
1931 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1932 mHwModules[i]->mHandle != 0) {
1933 module = mHwModules[i];
1934 break;
1935 }
1936 }
1937
1938 if (module == 0) {
1939 return INVALID_OPERATION;
1940 }
1941
1942 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1943
1944 for (size_t i = 0; i < mixes.size(); i++) {
1945 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001946
1947 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001948 continue;
1949 }
1950
Eric Laurentc722f302014-12-10 11:21:49 -08001951 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1952 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1953 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001954 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001955 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001956 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001957 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001958
1959 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1960 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1961 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001962 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001963 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001964 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001965 }
1966 module->removeOutputProfile(address);
1967 module->removeInputProfile(address);
1968 }
1969 return NO_ERROR;
1970}
1971
Eric Laurente552edb2014-03-10 17:42:56 -07001972
Eric Laurente0720872014-03-11 09:30:41 -07001973status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001974{
1975 const size_t SIZE = 256;
1976 char buffer[SIZE];
1977 String8 result;
1978
1979 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1980 result.append(buffer);
1981
Eric Laurent87ffa392015-05-22 10:32:38 -07001982 snprintf(buffer, SIZE, " Primary Output: %d\n",
1983 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07001984 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001985 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001986 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001987 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001988 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001989 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001990 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001991 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001992 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001993 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001994 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001995 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001996 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001997 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001998 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001999 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002000 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01002001 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07002002
François Gaffie53615e22015-03-19 09:24:12 +01002003 mAvailableOutputDevices.dump(fd, String8("output"));
2004 mAvailableInputDevices.dump(fd, String8("input"));
2005 mHwModules.dump(fd);
2006 mOutputs.dump(fd);
2007 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01002008 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01002009 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002010 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002011
2012 return NO_ERROR;
2013}
2014
2015// This function checks for the parameters which can be offloaded.
2016// This can be enhanced depending on the capability of the DSP and policy
2017// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002018bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002019{
2020 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002021 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002022 offloadInfo.sample_rate, offloadInfo.channel_mask,
2023 offloadInfo.format,
2024 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2025 offloadInfo.has_video);
2026
2027 // Check if offload has been disabled
2028 char propValue[PROPERTY_VALUE_MAX];
2029 if (property_get("audio.offload.disable", propValue, "0")) {
2030 if (atoi(propValue) != 0) {
2031 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2032 return false;
2033 }
2034 }
2035
2036 // Check if stream type is music, then only allow offload as of now.
2037 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2038 {
2039 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2040 return false;
2041 }
2042
2043 //TODO: enable audio offloading with video when ready
Andy Hung08945c42015-05-31 21:36:46 -07002044 const bool allowOffloadWithVideo =
2045 property_get_bool("audio.offload.video", false /* default_value */);
2046 if (offloadInfo.has_video && !allowOffloadWithVideo) {
Eric Laurente552edb2014-03-10 17:42:56 -07002047 ALOGV("isOffloadSupported: has_video == true, returning false");
2048 return false;
2049 }
2050
2051 //If duration is less than minimum value defined in property, return false
2052 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2053 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2054 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2055 return false;
2056 }
2057 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2058 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2059 return false;
2060 }
2061
2062 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2063 // creating an offloaded track and tearing it down immediately after start when audioflinger
2064 // detects there is an active non offloadable effect.
2065 // FIXME: We should check the audio session here but we do not have it in this context.
2066 // This may prevent offloading in rare situations where effects are left active by apps
2067 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002068 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002069 return false;
2070 }
2071
2072 // See if there is a profile to support this.
2073 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002074 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002075 offloadInfo.sample_rate,
2076 offloadInfo.format,
2077 offloadInfo.channel_mask,
2078 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002079 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2080 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002081}
2082
Eric Laurent6a94d692014-05-20 11:18:06 -07002083status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2084 audio_port_type_t type,
2085 unsigned int *num_ports,
2086 struct audio_port *ports,
2087 unsigned int *generation)
2088{
2089 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2090 generation == NULL) {
2091 return BAD_VALUE;
2092 }
2093 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2094 if (ports == NULL) {
2095 *num_ports = 0;
2096 }
2097
2098 size_t portsWritten = 0;
2099 size_t portsMax = *num_ports;
2100 *num_ports = 0;
2101 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2102 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2103 for (size_t i = 0;
2104 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2105 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2106 }
2107 *num_ports += mAvailableOutputDevices.size();
2108 }
2109 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2110 for (size_t i = 0;
2111 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2112 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2113 }
2114 *num_ports += mAvailableInputDevices.size();
2115 }
2116 }
2117 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2118 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2119 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2120 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2121 }
2122 *num_ports += mInputs.size();
2123 }
2124 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002125 size_t numOutputs = 0;
2126 for (size_t i = 0; i < mOutputs.size(); i++) {
2127 if (!mOutputs[i]->isDuplicated()) {
2128 numOutputs++;
2129 if (portsWritten < portsMax) {
2130 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2131 }
2132 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002133 }
Eric Laurent84c70242014-06-23 08:46:27 -07002134 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002135 }
2136 }
2137 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002138 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002139 return NO_ERROR;
2140}
2141
2142status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2143{
2144 return NO_ERROR;
2145}
2146
Eric Laurent6a94d692014-05-20 11:18:06 -07002147status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2148 audio_patch_handle_t *handle,
2149 uid_t uid)
2150{
2151 ALOGV("createAudioPatch()");
2152
2153 if (handle == NULL || patch == NULL) {
2154 return BAD_VALUE;
2155 }
2156 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2157
Eric Laurent874c42872014-08-08 15:13:39 -07002158 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2159 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2160 return BAD_VALUE;
2161 }
2162 // only one source per audio patch supported for now
2163 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002164 return INVALID_OPERATION;
2165 }
Eric Laurent874c42872014-08-08 15:13:39 -07002166
2167 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002168 return INVALID_OPERATION;
2169 }
Eric Laurent874c42872014-08-08 15:13:39 -07002170 for (size_t i = 0; i < patch->num_sinks; i++) {
2171 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2172 return INVALID_OPERATION;
2173 }
2174 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002175
2176 sp<AudioPatch> patchDesc;
2177 ssize_t index = mAudioPatches.indexOfKey(*handle);
2178
Eric Laurent6a94d692014-05-20 11:18:06 -07002179 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2180 patch->sources[0].role,
2181 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002182#if LOG_NDEBUG == 0
2183 for (size_t i = 0; i < patch->num_sinks; i++) {
2184 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2185 patch->sinks[i].role,
2186 patch->sinks[i].type);
2187 }
2188#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002189
2190 if (index >= 0) {
2191 patchDesc = mAudioPatches.valueAt(index);
2192 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2193 mUidCached, patchDesc->mUid, uid);
2194 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2195 return INVALID_OPERATION;
2196 }
2197 } else {
2198 *handle = 0;
2199 }
2200
2201 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002202 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002203 if (outputDesc == NULL) {
2204 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2205 return BAD_VALUE;
2206 }
Eric Laurent84c70242014-06-23 08:46:27 -07002207 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2208 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002209 if (patchDesc != 0) {
2210 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2211 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2212 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2213 return BAD_VALUE;
2214 }
2215 }
Eric Laurent874c42872014-08-08 15:13:39 -07002216 DeviceVector devices;
2217 for (size_t i = 0; i < patch->num_sinks; i++) {
2218 // Only support mix to devices connection
2219 // TODO add support for mix to mix connection
2220 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2221 ALOGV("createAudioPatch() source mix but sink is not a device");
2222 return INVALID_OPERATION;
2223 }
2224 sp<DeviceDescriptor> devDesc =
2225 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2226 if (devDesc == 0) {
2227 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2228 return BAD_VALUE;
2229 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002230
François Gaffie53615e22015-03-19 09:24:12 +01002231 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002232 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002233 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002234 NULL, // updatedSamplingRate
2235 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002236 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002237 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002238 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002239 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002240 ALOGV("createAudioPatch() profile not supported for device %08x",
2241 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002242 return INVALID_OPERATION;
2243 }
2244 devices.add(devDesc);
2245 }
2246 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002247 return INVALID_OPERATION;
2248 }
Eric Laurent874c42872014-08-08 15:13:39 -07002249
Eric Laurent6a94d692014-05-20 11:18:06 -07002250 // TODO: reconfigure output format and channels here
2251 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002252 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002253 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002254 index = mAudioPatches.indexOfKey(*handle);
2255 if (index >= 0) {
2256 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2257 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2258 }
2259 patchDesc = mAudioPatches.valueAt(index);
2260 patchDesc->mUid = uid;
2261 ALOGV("createAudioPatch() success");
2262 } else {
2263 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2264 return INVALID_OPERATION;
2265 }
2266 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2267 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2268 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002269 // only one sink supported when connecting an input device to a mix
2270 if (patch->num_sinks > 1) {
2271 return INVALID_OPERATION;
2272 }
François Gaffie53615e22015-03-19 09:24:12 +01002273 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002274 if (inputDesc == NULL) {
2275 return BAD_VALUE;
2276 }
2277 if (patchDesc != 0) {
2278 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2279 return BAD_VALUE;
2280 }
2281 }
2282 sp<DeviceDescriptor> devDesc =
2283 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2284 if (devDesc == 0) {
2285 return BAD_VALUE;
2286 }
2287
François Gaffie53615e22015-03-19 09:24:12 +01002288 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002289 devDesc->mAddress,
2290 patch->sinks[0].sample_rate,
2291 NULL, /*updatedSampleRate*/
2292 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002293 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002294 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002295 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002296 // FIXME for the parameter type,
2297 // and the NONE
2298 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002299 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002300 return INVALID_OPERATION;
2301 }
2302 // TODO: reconfigure output format and channels here
2303 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002304 devDesc->type(), inputDesc->mIoHandle);
2305 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002306 index = mAudioPatches.indexOfKey(*handle);
2307 if (index >= 0) {
2308 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2309 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2310 }
2311 patchDesc = mAudioPatches.valueAt(index);
2312 patchDesc->mUid = uid;
2313 ALOGV("createAudioPatch() success");
2314 } else {
2315 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2316 return INVALID_OPERATION;
2317 }
2318 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2319 // device to device connection
2320 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002321 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002322 return BAD_VALUE;
2323 }
2324 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002325 sp<DeviceDescriptor> srcDeviceDesc =
2326 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002327 if (srcDeviceDesc == 0) {
2328 return BAD_VALUE;
2329 }
Eric Laurent874c42872014-08-08 15:13:39 -07002330
Eric Laurent6a94d692014-05-20 11:18:06 -07002331 //update source and sink with our own data as the data passed in the patch may
2332 // be incomplete.
2333 struct audio_patch newPatch = *patch;
2334 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002335
Eric Laurent874c42872014-08-08 15:13:39 -07002336 for (size_t i = 0; i < patch->num_sinks; i++) {
2337 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2338 ALOGV("createAudioPatch() source device but one sink is not a device");
2339 return INVALID_OPERATION;
2340 }
2341
2342 sp<DeviceDescriptor> sinkDeviceDesc =
2343 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2344 if (sinkDeviceDesc == 0) {
2345 return BAD_VALUE;
2346 }
2347 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2348
Eric Laurent3bcf8592015-04-03 12:13:24 -07002349 // create a software bridge in PatchPanel if:
2350 // - source and sink devices are on differnt HW modules OR
2351 // - audio HAL version is < 3.0
2352 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2353 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2354 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002355 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002356 return INVALID_OPERATION;
2357 }
Eric Laurent874c42872014-08-08 15:13:39 -07002358 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002359 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002360 // if the sink device is reachable via an opened output stream, request to go via
2361 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002362 audio_io_handle_t output = selectOutput(outputs,
2363 AUDIO_OUTPUT_FLAG_NONE,
2364 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002365 if (output != AUDIO_IO_HANDLE_NONE) {
2366 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2367 if (outputDesc->isDuplicated()) {
2368 return INVALID_OPERATION;
2369 }
2370 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002371 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002372 newPatch.num_sources = 2;
2373 }
Eric Laurent83b88082014-06-20 18:31:16 -07002374 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002375 }
2376 // TODO: check from routing capabilities in config file and other conflicting patches
2377
2378 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2379 if (index >= 0) {
2380 afPatchHandle = patchDesc->mAfPatchHandle;
2381 }
2382
2383 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2384 &afPatchHandle,
2385 0);
2386 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2387 status, afPatchHandle);
2388 if (status == NO_ERROR) {
2389 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002390 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002391 addAudioPatch(patchDesc->mHandle, patchDesc);
2392 } else {
2393 patchDesc->mPatch = newPatch;
2394 }
2395 patchDesc->mAfPatchHandle = afPatchHandle;
2396 *handle = patchDesc->mHandle;
2397 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002398 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002399 } else {
2400 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2401 status);
2402 return INVALID_OPERATION;
2403 }
2404 } else {
2405 return BAD_VALUE;
2406 }
2407 } else {
2408 return BAD_VALUE;
2409 }
2410 return NO_ERROR;
2411}
2412
2413status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2414 uid_t uid)
2415{
2416 ALOGV("releaseAudioPatch() patch %d", handle);
2417
2418 ssize_t index = mAudioPatches.indexOfKey(handle);
2419
2420 if (index < 0) {
2421 return BAD_VALUE;
2422 }
2423 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2424 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2425 mUidCached, patchDesc->mUid, uid);
2426 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2427 return INVALID_OPERATION;
2428 }
2429
2430 struct audio_patch *patch = &patchDesc->mPatch;
2431 patchDesc->mUid = mUidCached;
2432 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002433 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 if (outputDesc == NULL) {
2435 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2436 return BAD_VALUE;
2437 }
2438
Eric Laurentc75307b2015-03-17 15:29:32 -07002439 setOutputDevice(outputDesc,
2440 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002441 true,
2442 0,
2443 NULL);
2444 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2445 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002446 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002447 if (inputDesc == NULL) {
2448 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2449 return BAD_VALUE;
2450 }
2451 setInputDevice(inputDesc->mIoHandle,
2452 getNewInputDevice(inputDesc->mIoHandle),
2453 true,
2454 NULL);
2455 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2456 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2457 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2458 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2459 status, patchDesc->mAfPatchHandle);
2460 removeAudioPatch(patchDesc->mHandle);
2461 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002462 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002463 } else {
2464 return BAD_VALUE;
2465 }
2466 } else {
2467 return BAD_VALUE;
2468 }
2469 return NO_ERROR;
2470}
2471
2472status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2473 struct audio_patch *patches,
2474 unsigned int *generation)
2475{
François Gaffie53615e22015-03-19 09:24:12 +01002476 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002477 return BAD_VALUE;
2478 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002479 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002480 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002481}
2482
Eric Laurente1715a42014-05-20 11:30:42 -07002483status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002484{
Eric Laurente1715a42014-05-20 11:30:42 -07002485 ALOGV("setAudioPortConfig()");
2486
2487 if (config == NULL) {
2488 return BAD_VALUE;
2489 }
2490 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2491 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002492 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2493 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002494 }
2495
Eric Laurenta121f902014-06-03 13:32:54 -07002496 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002497 if (config->type == AUDIO_PORT_TYPE_MIX) {
2498 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002499 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002500 if (outputDesc == NULL) {
2501 return BAD_VALUE;
2502 }
Eric Laurent84c70242014-06-23 08:46:27 -07002503 ALOG_ASSERT(!outputDesc->isDuplicated(),
2504 "setAudioPortConfig() called on duplicated output %d",
2505 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002506 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002507 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002508 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002509 if (inputDesc == NULL) {
2510 return BAD_VALUE;
2511 }
Eric Laurenta121f902014-06-03 13:32:54 -07002512 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002513 } else {
2514 return BAD_VALUE;
2515 }
2516 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2517 sp<DeviceDescriptor> deviceDesc;
2518 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2519 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2520 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2521 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2522 } else {
2523 return BAD_VALUE;
2524 }
2525 if (deviceDesc == NULL) {
2526 return BAD_VALUE;
2527 }
Eric Laurenta121f902014-06-03 13:32:54 -07002528 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002529 } else {
2530 return BAD_VALUE;
2531 }
2532
Eric Laurenta121f902014-06-03 13:32:54 -07002533 struct audio_port_config backupConfig;
2534 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2535 if (status == NO_ERROR) {
2536 struct audio_port_config newConfig;
2537 audioPortConfig->toAudioPortConfig(&newConfig, config);
2538 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002539 }
Eric Laurenta121f902014-06-03 13:32:54 -07002540 if (status != NO_ERROR) {
2541 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002542 }
Eric Laurente1715a42014-05-20 11:30:42 -07002543
2544 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002545}
2546
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002547void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2548{
2549 clearAudioPatches(uid);
2550 clearSessionRoutes(uid);
2551}
2552
Eric Laurent6a94d692014-05-20 11:18:06 -07002553void AudioPolicyManager::clearAudioPatches(uid_t uid)
2554{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002555 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002556 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2557 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002558 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002559 }
2560 }
2561}
2562
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002563
2564void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2565 audio_io_handle_t ouptutToSkip)
2566{
2567 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2568 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2569 for (size_t j = 0; j < mOutputs.size(); j++) {
2570 if (mOutputs.keyAt(j) == ouptutToSkip) {
2571 continue;
2572 }
2573 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2574 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2575 continue;
2576 }
2577 // If the default device for this strategy is on another output mix,
2578 // invalidate all tracks in this strategy to force re connection.
2579 // Otherwise select new device on the output mix.
2580 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2581 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2582 if (stream == AUDIO_STREAM_PATCH) {
2583 continue;
2584 }
2585 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2586 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2587 }
2588 }
2589 } else {
2590 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2591 setOutputDevice(outputDesc, newDevice, false);
2592 }
2593 }
2594}
2595
2596void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2597{
2598 // remove output routes associated with this uid
2599 SortedVector<routing_strategy> affectedStrategies;
2600 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2601 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2602 if (route->mUid == uid) {
2603 mOutputRoutes.removeItemsAt(i);
2604 if (route->mDeviceDescriptor != 0) {
2605 affectedStrategies.add(getStrategy(route->mStreamType));
2606 }
2607 }
2608 }
2609 // reroute outputs if necessary
2610 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2611 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2612 }
2613
2614 // remove input routes associated with this uid
2615 SortedVector<audio_source_t> affectedSources;
2616 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2617 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2618 if (route->mUid == uid) {
2619 mInputRoutes.removeItemsAt(i);
2620 if (route->mDeviceDescriptor != 0) {
2621 affectedSources.add(route->mSource);
2622 }
2623 }
2624 }
2625 // reroute inputs if necessary
2626 SortedVector<audio_io_handle_t> inputsToClose;
2627 for (size_t i = 0; i < mInputs.size(); i++) {
2628 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2629 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2630 inputsToClose.add(inputDesc->mIoHandle);
2631 }
2632 }
2633 for (size_t i = 0; i < inputsToClose.size(); i++) {
2634 closeInput(inputsToClose[i]);
2635 }
2636}
2637
2638
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002639status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2640 audio_io_handle_t *ioHandle,
2641 audio_devices_t *device)
2642{
2643 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2644 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002645 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002646
François Gaffiedf372692015-03-19 10:43:27 +01002647 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002648}
2649
Eric Laurent493404d2015-04-21 15:07:36 -07002650status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2651 const audio_attributes_t *attributes __unused,
2652 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002653{
2654 return INVALID_OPERATION;
2655}
2656
Eric Laurent493404d2015-04-21 15:07:36 -07002657status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002658{
2659 return INVALID_OPERATION;
2660}
2661
Eric Laurente552edb2014-03-10 17:42:56 -07002662// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002663// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002664// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002665uint32_t AudioPolicyManager::nextAudioPortGeneration()
2666{
2667 return android_atomic_inc(&mAudioPortGeneration);
2668}
2669
Eric Laurente0720872014-03-11 09:30:41 -07002670AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002671 :
2672#ifdef AUDIO_POLICY_TEST
2673 Thread(false),
2674#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002675 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002676 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002677 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002678 mAudioPortGeneration(1),
2679 mBeaconMuteRefCount(0),
2680 mBeaconPlayingRefCount(0),
2681 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002682{
François Gaffie2110e042015-03-24 08:41:51 +01002683 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2684 if (!engineInstance) {
2685 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2686 return;
2687 }
2688 // Retrieve the Policy Manager Interface
2689 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2690 if (mEngine == NULL) {
2691 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2692 return;
2693 }
2694 mEngine->setObserver(this);
2695 status_t status = mEngine->initCheck();
2696 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2697
Eric Laurent6a94d692014-05-20 11:18:06 -07002698 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002699 mpClientInterface = clientInterface;
2700
Eric Laurent7288ab82015-05-06 18:44:02 -07002701 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002702 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2703 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2704 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2705 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2706 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2707 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002708 ALOGE("could not load audio policy configuration file, setting defaults");
2709 defaultAudioPolicyConfig();
2710 }
2711 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002712 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002713
François Gaffie2110e042015-03-24 08:41:51 +01002714 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2715 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002716
2717 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002718 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2719 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002720 for (size_t i = 0; i < mHwModules.size(); i++) {
2721 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2722 if (mHwModules[i]->mHandle == 0) {
2723 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2724 continue;
2725 }
2726 // open all output streams needed to access attached devices
2727 // except for direct output streams that are only opened when they are actually
2728 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002729 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002730 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2731 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002732 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002733
Eric Laurent3a4311c2014-03-17 12:00:47 -07002734 if (outProfile->mSupportedDevices.isEmpty()) {
2735 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2736 continue;
2737 }
2738
Eric Laurentd78f1532014-09-16 16:38:20 -07002739 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2740 continue;
2741 }
Eric Laurent83b88082014-06-20 18:31:16 -07002742 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002743 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2744 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002745 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002746 // chose first device present in mSupportedDevices also part of
2747 // outputDeviceTypes
2748 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002749 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002750 if ((profileType & outputDeviceTypes) != 0) {
2751 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002752 }
Eric Laurente552edb2014-03-10 17:42:56 -07002753 }
2754 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002755 if ((profileType & outputDeviceTypes) == 0) {
2756 continue;
2757 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002758 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2759 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002760
2761 outputDesc->mDevice = profileType;
2762 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2763 config.sample_rate = outputDesc->mSamplingRate;
2764 config.channel_mask = outputDesc->mChannelMask;
2765 config.format = outputDesc->mFormat;
2766 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002767 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002768 &output,
2769 &config,
2770 &outputDesc->mDevice,
2771 String8(""),
2772 &outputDesc->mLatency,
2773 outputDesc->mFlags);
2774
2775 if (status != NO_ERROR) {
2776 ALOGW("Cannot open output stream for device %08x on hw module %s",
2777 outputDesc->mDevice,
2778 mHwModules[i]->mName);
2779 } else {
2780 outputDesc->mSamplingRate = config.sample_rate;
2781 outputDesc->mChannelMask = config.channel_mask;
2782 outputDesc->mFormat = config.format;
2783
2784 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002785 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002786 ssize_t index =
2787 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2788 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002789 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2790 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002791 }
2792 }
2793 if (mPrimaryOutput == 0 &&
2794 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002795 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002796 }
2797 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002798 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002799 outputDesc->mDevice,
2800 true);
2801 }
Eric Laurente552edb2014-03-10 17:42:56 -07002802 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002803 // open input streams needed to access attached devices to validate
2804 // mAvailableInputDevices list
2805 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2806 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002807 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002808
Eric Laurent3a4311c2014-03-17 12:00:47 -07002809 if (inProfile->mSupportedDevices.isEmpty()) {
2810 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2811 continue;
2812 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002813 // chose first device present in mSupportedDevices also part of
2814 // inputDeviceTypes
2815 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2816 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002817 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002818 if (profileType & inputDeviceTypes) {
2819 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002820 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002821 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002822 if ((profileType & inputDeviceTypes) == 0) {
2823 continue;
2824 }
2825 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2826
2827 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2828 inputDesc->mDevice = profileType;
2829
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002830 // find the address
2831 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2832 // the inputs vector must be of size 1, but we don't want to crash here
2833 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2834 : String8("");
2835 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2836 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2837
Eric Laurentd78f1532014-09-16 16:38:20 -07002838 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2839 config.sample_rate = inputDesc->mSamplingRate;
2840 config.channel_mask = inputDesc->mChannelMask;
2841 config.format = inputDesc->mFormat;
2842 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002843 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002844 &input,
2845 &config,
2846 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002847 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002848 AUDIO_SOURCE_MIC,
2849 AUDIO_INPUT_FLAG_NONE);
2850
2851 if (status == NO_ERROR) {
2852 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002853 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002854 ssize_t index =
2855 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2856 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002857 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2858 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002859 }
2860 }
2861 mpClientInterface->closeInput(input);
2862 } else {
2863 ALOGW("Cannot open input stream for device %08x on hw module %s",
2864 inputDesc->mDevice,
2865 mHwModules[i]->mName);
2866 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002867 }
2868 }
2869 // make sure all attached devices have been allocated a unique ID
2870 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002871 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002872 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002873 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2874 continue;
2875 }
François Gaffie2110e042015-03-24 08:41:51 +01002876 // The device is now validated and can be appended to the available devices of the engine
2877 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2878 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002879 i++;
2880 }
2881 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002882 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002883 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002884 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2885 continue;
2886 }
François Gaffie2110e042015-03-24 08:41:51 +01002887 // The device is now validated and can be appended to the available devices of the engine
2888 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2889 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002890 i++;
2891 }
2892 // make sure default device is reachable
2893 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002894 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002895 }
Eric Laurente552edb2014-03-10 17:42:56 -07002896
2897 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2898
2899 updateDevicesAndOutputs();
2900
2901#ifdef AUDIO_POLICY_TEST
2902 if (mPrimaryOutput != 0) {
2903 AudioParameter outputCmd = AudioParameter();
2904 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002905 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002906
2907 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2908 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002909 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2910 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002911 mTestLatencyMs = 0;
2912 mCurOutput = 0;
2913 mDirectOutput = false;
2914 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2915 mTestOutputs[i] = 0;
2916 }
2917
2918 const size_t SIZE = 256;
2919 char buffer[SIZE];
2920 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2921 run(buffer, ANDROID_PRIORITY_AUDIO);
2922 }
2923#endif //AUDIO_POLICY_TEST
2924}
2925
Eric Laurente0720872014-03-11 09:30:41 -07002926AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002927{
2928#ifdef AUDIO_POLICY_TEST
2929 exit();
2930#endif //AUDIO_POLICY_TEST
2931 for (size_t i = 0; i < mOutputs.size(); i++) {
2932 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002933 }
2934 for (size_t i = 0; i < mInputs.size(); i++) {
2935 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002936 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002937 mAvailableOutputDevices.clear();
2938 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002939 mOutputs.clear();
2940 mInputs.clear();
2941 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002942}
2943
Eric Laurente0720872014-03-11 09:30:41 -07002944status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002945{
Eric Laurent87ffa392015-05-22 10:32:38 -07002946 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002947}
2948
2949#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002950bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002951{
2952 ALOGV("entering threadLoop()");
2953 while (!exitPending())
2954 {
2955 String8 command;
2956 int valueInt;
2957 String8 value;
2958
2959 Mutex::Autolock _l(mLock);
2960 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2961
2962 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2963 AudioParameter param = AudioParameter(command);
2964
2965 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2966 valueInt != 0) {
2967 ALOGV("Test command %s received", command.string());
2968 String8 target;
2969 if (param.get(String8("target"), target) != NO_ERROR) {
2970 target = "Manager";
2971 }
2972 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2973 param.remove(String8("test_cmd_policy_output"));
2974 mCurOutput = valueInt;
2975 }
2976 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2977 param.remove(String8("test_cmd_policy_direct"));
2978 if (value == "false") {
2979 mDirectOutput = false;
2980 } else if (value == "true") {
2981 mDirectOutput = true;
2982 }
2983 }
2984 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2985 param.remove(String8("test_cmd_policy_input"));
2986 mTestInput = valueInt;
2987 }
2988
2989 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2990 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002991 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002992 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002993 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002994 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002995 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002996 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002997 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002998 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002999 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003000 if (target == "Manager") {
3001 mTestFormat = format;
3002 } else if (mTestOutputs[mCurOutput] != 0) {
3003 AudioParameter outputParam = AudioParameter();
3004 outputParam.addInt(String8("format"), format);
3005 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3006 }
3007 }
3008 }
3009 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3010 param.remove(String8("test_cmd_policy_channels"));
3011 int channels = 0;
3012
3013 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003014 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003015 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003016 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003017 }
3018 if (channels != 0) {
3019 if (target == "Manager") {
3020 mTestChannels = channels;
3021 } else if (mTestOutputs[mCurOutput] != 0) {
3022 AudioParameter outputParam = AudioParameter();
3023 outputParam.addInt(String8("channels"), channels);
3024 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3025 }
3026 }
3027 }
3028 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3029 param.remove(String8("test_cmd_policy_sampleRate"));
3030 if (valueInt >= 0 && valueInt <= 96000) {
3031 int samplingRate = valueInt;
3032 if (target == "Manager") {
3033 mTestSamplingRate = samplingRate;
3034 } else if (mTestOutputs[mCurOutput] != 0) {
3035 AudioParameter outputParam = AudioParameter();
3036 outputParam.addInt(String8("sampling_rate"), samplingRate);
3037 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3038 }
3039 }
3040 }
3041
3042 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3043 param.remove(String8("test_cmd_policy_reopen"));
3044
Eric Laurentc75307b2015-03-17 15:29:32 -07003045 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003046
Eric Laurentc75307b2015-03-17 15:29:32 -07003047 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003048
Eric Laurentc75307b2015-03-17 15:29:32 -07003049 removeOutput(mPrimaryOutput->mIoHandle);
3050 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3051 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003052 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003053 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3054 config.sample_rate = outputDesc->mSamplingRate;
3055 config.channel_mask = outputDesc->mChannelMask;
3056 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003057 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003058 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003059 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003060 &config,
3061 &outputDesc->mDevice,
3062 String8(""),
3063 &outputDesc->mLatency,
3064 outputDesc->mFlags);
3065 if (status != NO_ERROR) {
3066 ALOGE("Failed to reopen hardware output stream, "
3067 "samplingRate: %d, format %d, channels %d",
3068 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003069 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003070 outputDesc->mSamplingRate = config.sample_rate;
3071 outputDesc->mChannelMask = config.channel_mask;
3072 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003073 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003074 AudioParameter outputCmd = AudioParameter();
3075 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003076 mpClientInterface->setParameters(handle, outputCmd.toString());
3077 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003078 }
3079 }
3080
3081
3082 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3083 }
3084 }
3085 return false;
3086}
3087
Eric Laurente0720872014-03-11 09:30:41 -07003088void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003089{
3090 {
3091 AutoMutex _l(mLock);
3092 requestExit();
3093 mWaitWorkCV.signal();
3094 }
3095 requestExitAndWait();
3096}
3097
Eric Laurente0720872014-03-11 09:30:41 -07003098int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003099{
3100 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3101 if (output == mTestOutputs[i]) return i;
3102 }
3103 return 0;
3104}
3105#endif //AUDIO_POLICY_TEST
3106
3107// ---
3108
Eric Laurentc75307b2015-03-17 15:29:32 -07003109void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003110{
François Gaffie98cc1912015-03-18 17:52:40 +01003111 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003112 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003113 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003114}
3115
François Gaffie53615e22015-03-19 09:24:12 +01003116void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3117{
3118 mOutputs.removeItem(output);
3119}
3120
Eric Laurent1f2f2232014-06-02 12:01:23 -07003121void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003122{
François Gaffie98cc1912015-03-18 17:52:40 +01003123 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003124 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003125 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003126}
Eric Laurente552edb2014-03-10 17:42:56 -07003127
Eric Laurentc75307b2015-03-17 15:29:32 -07003128void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003129 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003130 const String8 address /*in*/,
3131 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003132 sp<DeviceDescriptor> devDesc =
3133 desc->mProfile->mSupportedDevices.getDevice(device, address);
3134 if (devDesc != 0) {
3135 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3136 desc->mIoHandle, address.string());
3137 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003138 }
3139}
3140
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003141status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003142 audio_policy_dev_state_t state,
3143 SortedVector<audio_io_handle_t>& outputs,
3144 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003145{
François Gaffie53615e22015-03-19 09:24:12 +01003146 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003147 sp<SwAudioOutputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003148
3149 if (audio_device_is_digital(device)) {
3150 // erase all current sample rates, formats and channel masks
3151 devDesc->clearCapabilities();
3152 }
Eric Laurente552edb2014-03-10 17:42:56 -07003153
Eric Laurent3b73df72014-03-11 09:06:29 -07003154 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003155 // first list already open outputs that can be routed to this device
3156 for (size_t i = 0; i < mOutputs.size(); i++) {
3157 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003158 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003159 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003160 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3161 outputs.add(mOutputs.keyAt(i));
3162 } else {
3163 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003164 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003165 }
Eric Laurente552edb2014-03-10 17:42:56 -07003166 }
3167 }
3168 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003169 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003170 for (size_t i = 0; i < mHwModules.size(); i++)
3171 {
3172 if (mHwModules[i]->mHandle == 0) {
3173 continue;
3174 }
3175 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3176 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003177 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3178 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003179 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003180 address == profile->mSupportedDevices[0]->mAddress) {
3181 profiles.add(profile);
3182 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3183 }
Eric Laurente552edb2014-03-10 17:42:56 -07003184 }
3185 }
3186 }
3187
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003188 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3189
Eric Laurente552edb2014-03-10 17:42:56 -07003190 if (profiles.isEmpty() && outputs.isEmpty()) {
3191 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3192 return BAD_VALUE;
3193 }
3194
3195 // open outputs for matching profiles if needed. Direct outputs are also opened to
3196 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3197 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003198 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003199
3200 // nothing to do if one output is already opened for this profile
3201 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003202 for (j = 0; j < outputs.size(); j++) {
3203 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003204 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003205 // matching profile: save the sample rates, format and channel masks supported
3206 // by the profile in our device descriptor
Paul McLean9080a4c2015-06-18 08:24:02 -07003207 if (audio_device_is_digital(device)) {
3208 devDesc->importAudioPort(profile);
3209 }
Eric Laurente552edb2014-03-10 17:42:56 -07003210 break;
3211 }
3212 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003213 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003214 continue;
3215 }
3216
Eric Laurent83b88082014-06-20 18:31:16 -07003217 ALOGV("opening output for device %08x with params %s profile %p",
3218 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003219 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003220 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003221 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3222 config.sample_rate = desc->mSamplingRate;
3223 config.channel_mask = desc->mChannelMask;
3224 config.format = desc->mFormat;
3225 config.offload_info.sample_rate = desc->mSamplingRate;
3226 config.offload_info.channel_mask = desc->mChannelMask;
3227 config.offload_info.format = desc->mFormat;
3228 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003229 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003230 &output,
3231 &config,
3232 &desc->mDevice,
3233 address,
3234 &desc->mLatency,
3235 desc->mFlags);
3236 if (status == NO_ERROR) {
3237 desc->mSamplingRate = config.sample_rate;
3238 desc->mChannelMask = config.channel_mask;
3239 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003240
Eric Laurentd4692962014-05-05 18:13:44 -07003241 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003242 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003243 char *param = audio_device_address_to_parameter(device, address);
3244 mpClientInterface->setParameters(output, String8(param));
3245 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003246 }
3247
Eric Laurentd4692962014-05-05 18:13:44 -07003248 // Here is where we step through and resolve any "dynamic" fields
3249 String8 reply;
3250 char *value;
3251 if (profile->mSamplingRates[0] == 0) {
3252 reply = mpClientInterface->getParameters(output,
3253 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003254 ALOGV("checkOutputsForDevice() supported sampling rates %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->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003259 }
Eric Laurentd4692962014-05-05 18:13:44 -07003260 }
3261 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3262 reply = mpClientInterface->getParameters(output,
3263 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003264 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003265 reply.string());
3266 value = strpbrk((char *)reply.string(), "=");
3267 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003268 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003269 }
Eric Laurentd4692962014-05-05 18:13:44 -07003270 }
3271 if (profile->mChannelMasks[0] == 0) {
3272 reply = mpClientInterface->getParameters(output,
3273 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003274 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003275 reply.string());
3276 value = strpbrk((char *)reply.string(), "=");
3277 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003278 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003279 }
Eric Laurentd4692962014-05-05 18:13:44 -07003280 }
3281 if (((profile->mSamplingRates[0] == 0) &&
3282 (profile->mSamplingRates.size() < 2)) ||
3283 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3284 (profile->mFormats.size() < 2)) ||
3285 ((profile->mChannelMasks[0] == 0) &&
3286 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003287 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003288 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003289 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003290 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3291 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003292 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003293 config.sample_rate = profile->pickSamplingRate();
3294 config.channel_mask = profile->pickChannelMask();
3295 config.format = profile->pickFormat();
3296 config.offload_info.sample_rate = config.sample_rate;
3297 config.offload_info.channel_mask = config.channel_mask;
3298 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003299 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003300 &output,
3301 &config,
3302 &desc->mDevice,
3303 address,
3304 &desc->mLatency,
3305 desc->mFlags);
3306 if (status == NO_ERROR) {
3307 desc->mSamplingRate = config.sample_rate;
3308 desc->mChannelMask = config.channel_mask;
3309 desc->mFormat = config.format;
3310 } else {
3311 output = AUDIO_IO_HANDLE_NONE;
3312 }
Eric Laurentd4692962014-05-05 18:13:44 -07003313 }
3314
Eric Laurentcf2c0212014-07-25 16:20:43 -07003315 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003316 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003317 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003318 sp<AudioPolicyMix> policyMix;
3319 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003320 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3321 address.string());
3322 }
François Gaffie036e1e92015-03-19 10:16:24 +01003323 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003324 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003325
Eric Laurent87ffa392015-05-22 10:32:38 -07003326 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3327 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003328 // no duplicated output for direct outputs and
3329 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003330 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003331
Eric Laurentd4692962014-05-05 18:13:44 -07003332 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003333 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003334
Eric Laurentd4692962014-05-05 18:13:44 -07003335 //TODO: configure audio effect output stage here
3336
3337 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003338 duplicatedOutput =
3339 mpClientInterface->openDuplicateOutput(output,
3340 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003341 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003342 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003343 sp<SwAudioOutputDescriptor> dupOutputDesc =
3344 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3345 dupOutputDesc->mOutput1 = mPrimaryOutput;
3346 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003347 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3348 dupOutputDesc->mFormat = desc->mFormat;
3349 dupOutputDesc->mChannelMask = desc->mChannelMask;
3350 dupOutputDesc->mLatency = desc->mLatency;
3351 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003352 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003353 } else {
3354 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003355 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003356 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003357 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003358 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003359 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003360 }
Eric Laurente552edb2014-03-10 17:42:56 -07003361 }
3362 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003363 } else {
3364 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003365 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003366 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003367 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003368 profiles.removeAt(profile_index);
3369 profile_index--;
3370 } else {
3371 outputs.add(output);
Paul McLean9080a4c2015-06-18 08:24:02 -07003372 // Load digital format info only for digital devices
3373 if (audio_device_is_digital(device)) {
3374 devDesc->importAudioPort(profile);
3375 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003376
François Gaffie53615e22015-03-19 09:24:12 +01003377 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003378 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3379 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003380 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003381 NULL/*patch handle*/, address.string());
3382 }
Eric Laurente552edb2014-03-10 17:42:56 -07003383 ALOGV("checkOutputsForDevice(): adding output %d", output);
3384 }
3385 }
3386
3387 if (profiles.isEmpty()) {
3388 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3389 return BAD_VALUE;
3390 }
Eric Laurentd4692962014-05-05 18:13:44 -07003391 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003392 // check if one opened output is not needed any more after disconnecting one device
3393 for (size_t i = 0; i < mOutputs.size(); i++) {
3394 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003395 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003396 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003397 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003398 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003399 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003400 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003401 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3402 mOutputs.keyAt(i));
3403 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003404 }
Eric Laurente552edb2014-03-10 17:42:56 -07003405 }
3406 }
Eric Laurentd4692962014-05-05 18:13:44 -07003407 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003408 for (size_t i = 0; i < mHwModules.size(); i++)
3409 {
3410 if (mHwModules[i]->mHandle == 0) {
3411 continue;
3412 }
3413 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3414 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003415 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003416 if (profile->mSupportedDevices.types() & device) {
3417 ALOGV("checkOutputsForDevice(): "
3418 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003419 if (profile->mSamplingRates[0] == 0) {
3420 profile->mSamplingRates.clear();
3421 profile->mSamplingRates.add(0);
3422 }
3423 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3424 profile->mFormats.clear();
3425 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3426 }
3427 if (profile->mChannelMasks[0] == 0) {
3428 profile->mChannelMasks.clear();
3429 profile->mChannelMasks.add(0);
3430 }
3431 }
3432 }
3433 }
3434 }
3435 return NO_ERROR;
3436}
3437
Paul McLean9080a4c2015-06-18 08:24:02 -07003438status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003439 audio_policy_dev_state_t state,
3440 SortedVector<audio_io_handle_t>& inputs,
3441 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003442{
Paul McLean9080a4c2015-06-18 08:24:02 -07003443 audio_devices_t device = devDesc->type();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003444 sp<AudioInputDescriptor> desc;
Eric Laurentcc750d32015-06-25 11:48:20 -07003445
3446 if (audio_device_is_digital(device)) {
3447 // erase all current sample rates, formats and channel masks
3448 devDesc->clearCapabilities();
3449 }
3450
Eric Laurentd4692962014-05-05 18:13:44 -07003451 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3452 // first list already open inputs that can be routed to this device
3453 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3454 desc = mInputs.valueAt(input_index);
3455 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3456 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3457 inputs.add(mInputs.keyAt(input_index));
3458 }
3459 }
3460
3461 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003462 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003463 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3464 {
3465 if (mHwModules[module_idx]->mHandle == 0) {
3466 continue;
3467 }
3468 for (size_t profile_index = 0;
3469 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3470 profile_index++)
3471 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003472 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3473
3474 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003475 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003476 address == profile->mSupportedDevices[0]->mAddress) {
3477 profiles.add(profile);
3478 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3479 profile_index, module_idx);
3480 }
Eric Laurentd4692962014-05-05 18:13:44 -07003481 }
3482 }
3483 }
3484
3485 if (profiles.isEmpty() && inputs.isEmpty()) {
3486 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3487 return BAD_VALUE;
3488 }
3489
3490 // open inputs for matching profiles if needed. Direct inputs are also opened to
3491 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3492 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3493
Eric Laurent1c333e22014-05-20 10:48:17 -07003494 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003495 // nothing to do if one input is already opened for this profile
3496 size_t input_index;
3497 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3498 desc = mInputs.valueAt(input_index);
3499 if (desc->mProfile == profile) {
Paul McLean9080a4c2015-06-18 08:24:02 -07003500 if (audio_device_is_digital(device)) {
3501 devDesc->importAudioPort(profile);
3502 }
Eric Laurentd4692962014-05-05 18:13:44 -07003503 break;
3504 }
3505 }
3506 if (input_index != mInputs.size()) {
3507 continue;
3508 }
3509
3510 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3511 desc = new AudioInputDescriptor(profile);
3512 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003513 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3514 config.sample_rate = desc->mSamplingRate;
3515 config.channel_mask = desc->mChannelMask;
3516 config.format = desc->mFormat;
3517 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003518 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003519 &input,
3520 &config,
3521 &desc->mDevice,
3522 address,
3523 AUDIO_SOURCE_MIC,
3524 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003525
Eric Laurentcf2c0212014-07-25 16:20:43 -07003526 if (status == NO_ERROR) {
3527 desc->mSamplingRate = config.sample_rate;
3528 desc->mChannelMask = config.channel_mask;
3529 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003530
Eric Laurentd4692962014-05-05 18:13:44 -07003531 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003532 char *param = audio_device_address_to_parameter(device, address);
3533 mpClientInterface->setParameters(input, String8(param));
3534 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003535 }
3536
3537 // Here is where we step through and resolve any "dynamic" fields
3538 String8 reply;
3539 char *value;
3540 if (profile->mSamplingRates[0] == 0) {
3541 reply = mpClientInterface->getParameters(input,
3542 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3543 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3544 reply.string());
3545 value = strpbrk((char *)reply.string(), "=");
3546 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003547 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003548 }
3549 }
3550 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3551 reply = mpClientInterface->getParameters(input,
3552 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3553 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3554 value = strpbrk((char *)reply.string(), "=");
3555 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003556 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003557 }
3558 }
3559 if (profile->mChannelMasks[0] == 0) {
3560 reply = mpClientInterface->getParameters(input,
3561 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3562 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3563 reply.string());
3564 value = strpbrk((char *)reply.string(), "=");
3565 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003566 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003567 }
3568 }
3569 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3570 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3571 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3572 ALOGW("checkInputsForDevice() direct input missing param");
3573 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003574 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003575 }
3576
3577 if (input != 0) {
3578 addInput(input, desc);
3579 }
3580 } // endif input != 0
3581
Eric Laurentcf2c0212014-07-25 16:20:43 -07003582 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003583 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003584 profiles.removeAt(profile_index);
3585 profile_index--;
3586 } else {
3587 inputs.add(input);
Paul McLean9080a4c2015-06-18 08:24:02 -07003588 if (audio_device_is_digital(device)) {
3589 devDesc->importAudioPort(profile);
3590 }
Eric Laurentd4692962014-05-05 18:13:44 -07003591 ALOGV("checkInputsForDevice(): adding input %d", input);
3592 }
3593 } // end scan profiles
3594
3595 if (profiles.isEmpty()) {
3596 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3597 return BAD_VALUE;
3598 }
3599 } else {
3600 // Disconnect
3601 // check if one opened input is not needed any more after disconnecting one device
3602 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3603 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003604 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3605 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003606 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3607 mInputs.keyAt(input_index));
3608 inputs.add(mInputs.keyAt(input_index));
3609 }
3610 }
3611 // Clear any profiles associated with the disconnected device.
3612 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3613 if (mHwModules[module_index]->mHandle == 0) {
3614 continue;
3615 }
3616 for (size_t profile_index = 0;
3617 profile_index < mHwModules[module_index]->mInputProfiles.size();
3618 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003619 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Paul McLean9080a4c2015-06-18 08:24:02 -07003620 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003621 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003622 profile_index, module_index);
3623 if (profile->mSamplingRates[0] == 0) {
3624 profile->mSamplingRates.clear();
3625 profile->mSamplingRates.add(0);
3626 }
3627 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3628 profile->mFormats.clear();
3629 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3630 }
3631 if (profile->mChannelMasks[0] == 0) {
3632 profile->mChannelMasks.clear();
3633 profile->mChannelMasks.add(0);
3634 }
3635 }
3636 }
3637 }
3638 } // end disconnect
3639
3640 return NO_ERROR;
3641}
3642
3643
Eric Laurente0720872014-03-11 09:30:41 -07003644void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003645{
3646 ALOGV("closeOutput(%d)", output);
3647
Eric Laurentc75307b2015-03-17 15:29:32 -07003648 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003649 if (outputDesc == NULL) {
3650 ALOGW("closeOutput() unknown output %d", output);
3651 return;
3652 }
François Gaffie036e1e92015-03-19 10:16:24 +01003653 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003654
Eric Laurente552edb2014-03-10 17:42:56 -07003655 // look for duplicated outputs connected to the output being removed.
3656 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003657 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003658 if (dupOutputDesc->isDuplicated() &&
3659 (dupOutputDesc->mOutput1 == outputDesc ||
3660 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003661 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003662 if (dupOutputDesc->mOutput1 == outputDesc) {
3663 outputDesc2 = dupOutputDesc->mOutput2;
3664 } else {
3665 outputDesc2 = dupOutputDesc->mOutput1;
3666 }
3667 // As all active tracks on duplicated output will be deleted,
3668 // and as they were also referenced on the other output, the reference
3669 // count for their stream type must be adjusted accordingly on
3670 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003671 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003672 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003673 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003674 }
3675 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3676 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3677
3678 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003679 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003680 }
3681 }
3682
Eric Laurent05b90f82014-08-27 15:32:29 -07003683 nextAudioPortGeneration();
3684
3685 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3686 if (index >= 0) {
3687 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3688 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3689 mAudioPatches.removeItemsAt(index);
3690 mpClientInterface->onAudioPatchListUpdate();
3691 }
3692
Eric Laurente552edb2014-03-10 17:42:56 -07003693 AudioParameter param;
3694 param.add(String8("closing"), String8("true"));
3695 mpClientInterface->setParameters(output, param.toString());
3696
3697 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003698 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003699 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003700}
3701
3702void AudioPolicyManager::closeInput(audio_io_handle_t input)
3703{
3704 ALOGV("closeInput(%d)", input);
3705
3706 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3707 if (inputDesc == NULL) {
3708 ALOGW("closeInput() unknown input %d", input);
3709 return;
3710 }
3711
Eric Laurent6a94d692014-05-20 11:18:06 -07003712 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003713
3714 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3715 if (index >= 0) {
3716 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3717 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3718 mAudioPatches.removeItemsAt(index);
3719 mpClientInterface->onAudioPatchListUpdate();
3720 }
3721
3722 mpClientInterface->closeInput(input);
3723 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003724}
3725
Eric Laurentc75307b2015-03-17 15:29:32 -07003726SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3727 audio_devices_t device,
3728 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003729{
3730 SortedVector<audio_io_handle_t> outputs;
3731
3732 ALOGVV("getOutputsForDevice() device %04x", device);
3733 for (size_t i = 0; i < openOutputs.size(); i++) {
3734 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003735 i, openOutputs.valueAt(i)->isDuplicated(),
3736 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003737 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3738 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3739 outputs.add(openOutputs.keyAt(i));
3740 }
3741 }
3742 return outputs;
3743}
3744
Eric Laurente0720872014-03-11 09:30:41 -07003745bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003746 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003747{
3748 if (outputs1.size() != outputs2.size()) {
3749 return false;
3750 }
3751 for (size_t i = 0; i < outputs1.size(); i++) {
3752 if (outputs1[i] != outputs2[i]) {
3753 return false;
3754 }
3755 }
3756 return true;
3757}
3758
Eric Laurente0720872014-03-11 09:30:41 -07003759void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003760{
3761 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3762 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3763 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3764 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3765
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003766 // also take into account external policy-related changes: add all outputs which are
3767 // associated with policies in the "before" and "after" output vectors
3768 ALOGVV("checkOutputForStrategy(): policy related outputs");
3769 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003770 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003771 if (desc != 0 && desc->mPolicyMix != NULL) {
3772 srcOutputs.add(desc->mIoHandle);
3773 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3774 }
3775 }
3776 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003777 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003778 if (desc != 0 && desc->mPolicyMix != NULL) {
3779 dstOutputs.add(desc->mIoHandle);
3780 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3781 }
3782 }
3783
Eric Laurente552edb2014-03-10 17:42:56 -07003784 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3785 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3786 strategy, srcOutputs[0], dstOutputs[0]);
3787 // mute strategy while moving tracks from one output to another
3788 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003789 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003790 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003791 setStrategyMute(strategy, true, desc);
3792 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003793 }
3794 }
3795
3796 // Move effects associated to this strategy from previous output to new output
3797 if (strategy == STRATEGY_MEDIA) {
3798 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3799 SortedVector<audio_io_handle_t> moved;
3800 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003801 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3802 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3803 effectDesc->mIo != fxOutput) {
3804 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003805 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3806 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003807 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003808 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003809 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003810 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003811 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003812 }
3813 }
3814 }
3815 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003816 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003817 if (i == AUDIO_STREAM_PATCH) {
3818 continue;
3819 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003820 if (getStrategy((audio_stream_type_t)i) == strategy) {
3821 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003822 }
3823 }
3824 }
3825}
3826
Eric Laurente0720872014-03-11 09:30:41 -07003827void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003828{
François Gaffie2110e042015-03-24 08:41:51 +01003829 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003830 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003831 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003832 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003833 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003834 checkOutputForStrategy(STRATEGY_SONIFICATION);
3835 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003836 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003837 checkOutputForStrategy(STRATEGY_MEDIA);
3838 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003839 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003840}
3841
Eric Laurente0720872014-03-11 09:30:41 -07003842void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003843{
François Gaffie53615e22015-03-19 09:24:12 +01003844 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003845 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003846 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003847 return;
3848 }
3849
Eric Laurent3a4311c2014-03-17 12:00:47 -07003850 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003851 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3852 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3853 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003854 // suspend A2DP output if:
3855 // (NOT already suspended) &&
3856 // ((SCO device is connected &&
3857 // (forced usage for communication || for record is SCO))) ||
3858 // (phone state is ringing || in call)
3859 //
3860 // restore A2DP output if:
3861 // (Already suspended) &&
3862 // ((SCO device is NOT connected ||
3863 // (forced usage NOT for communication && NOT for record is SCO))) &&
3864 // (phone state is NOT ringing && NOT in call)
3865 //
3866 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003867 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003868 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3869 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3870 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3871 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003872
3873 mpClientInterface->restoreOutput(a2dpOutput);
3874 mA2dpSuspended = false;
3875 }
3876 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003877 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003878 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3879 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3880 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3881 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003882
3883 mpClientInterface->suspendOutput(a2dpOutput);
3884 mA2dpSuspended = true;
3885 }
3886 }
3887}
3888
Eric Laurentc75307b2015-03-17 15:29:32 -07003889audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3890 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003891{
3892 audio_devices_t device = AUDIO_DEVICE_NONE;
3893
Eric Laurent6a94d692014-05-20 11:18:06 -07003894 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3895 if (index >= 0) {
3896 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3897 if (patchDesc->mUid != mUidCached) {
3898 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3899 outputDesc->device(), outputDesc->mPatchHandle);
3900 return outputDesc->device();
3901 }
3902 }
3903
Eric Laurente552edb2014-03-10 17:42:56 -07003904 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003905 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003906 // use device for strategy enforced audible
3907 // 2: we are in call or the strategy phone is active on the output:
3908 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003909 // 3: the strategy for enforced audible is active but not enforced on the output:
3910 // use the device for strategy enforced audible
3911 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003912 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003913 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003914 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003915 // 6: the strategy accessibility is active on the output:
3916 // use device for strategy accessibility
3917 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003918 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003919 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003920 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003921 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003922 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003923 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003924 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003925 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3926 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003927 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003928 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003929 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003930 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003931 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003932 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003933 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003934 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003935 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003936 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003937 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003938 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003939 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003940 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003941 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003942 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003943 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003944 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003945 }
3946
Eric Laurent1c333e22014-05-20 10:48:17 -07003947 ALOGV("getNewOutputDevice() selected device %x", device);
3948 return device;
3949}
3950
3951audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3952{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003953 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003954
3955 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3956 if (index >= 0) {
3957 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3958 if (patchDesc->mUid != mUidCached) {
3959 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3960 inputDesc->mDevice, inputDesc->mPatchHandle);
3961 return inputDesc->mDevice;
3962 }
3963 }
3964
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003965 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003966
Eric Laurente552edb2014-03-10 17:42:56 -07003967 return device;
3968}
3969
Eric Laurente0720872014-03-11 09:30:41 -07003970uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003971 return (uint32_t)getStrategy(stream);
3972}
3973
Eric Laurente0720872014-03-11 09:30:41 -07003974audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003975 // By checking the range of stream before calling getStrategy, we avoid
3976 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3977 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003978 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003979 return AUDIO_DEVICE_NONE;
3980 }
3981 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003982 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003983 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3984 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3985 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003986 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003987 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003988 devices = outputDesc->device();
3989 break;
3990 }
Eric Laurente552edb2014-03-10 17:42:56 -07003991 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003992
3993 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3994 and doesn't really need to.*/
3995 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3996 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3997 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3998 }
3999
Eric Laurente552edb2014-03-10 17:42:56 -07004000 return devices;
4001}
4002
François Gaffie2110e042015-03-24 08:41:51 +01004003routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
4004{
Eric Laurent223fd5c2014-11-11 13:43:36 -08004005 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01004006 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004007}
4008
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004009uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4010 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004011 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4012 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4013 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004014 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4015 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4016 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004017 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01004018 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004019}
4020
Eric Laurente0720872014-03-11 09:30:41 -07004021void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004022 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004023 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004024 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4025 updateDevicesAndOutputs();
4026 break;
4027 default:
4028 break;
4029 }
4030}
4031
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004032uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4033 switch(event) {
4034 case STARTING_OUTPUT:
4035 mBeaconMuteRefCount++;
4036 break;
4037 case STOPPING_OUTPUT:
4038 if (mBeaconMuteRefCount > 0) {
4039 mBeaconMuteRefCount--;
4040 }
4041 break;
4042 case STARTING_BEACON:
4043 mBeaconPlayingRefCount++;
4044 break;
4045 case STOPPING_BEACON:
4046 if (mBeaconPlayingRefCount > 0) {
4047 mBeaconPlayingRefCount--;
4048 }
4049 break;
4050 }
4051
4052 if (mBeaconMuteRefCount > 0) {
4053 // any playback causes beacon to be muted
4054 return setBeaconMute(true);
4055 } else {
4056 // no other playback: unmute when beacon starts playing, mute when it stops
4057 return setBeaconMute(mBeaconPlayingRefCount == 0);
4058 }
4059}
4060
4061uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4062 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4063 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4064 // keep track of muted state to avoid repeating mute/unmute operations
4065 if (mBeaconMuted != mute) {
4066 // mute/unmute AUDIO_STREAM_TTS on all outputs
4067 ALOGV("\t muting %d", mute);
4068 uint32_t maxLatency = 0;
4069 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004070 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004071 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004072 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004073 0 /*delay*/, AUDIO_DEVICE_NONE);
4074 const uint32_t latency = desc->latency() * 2;
4075 if (latency > maxLatency) {
4076 maxLatency = latency;
4077 }
4078 }
4079 mBeaconMuted = mute;
4080 return maxLatency;
4081 }
4082 return 0;
4083}
4084
Eric Laurente0720872014-03-11 09:30:41 -07004085audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004086 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004087{
Paul McLeanaa981192015-03-21 09:55:15 -07004088 // Routing
4089 // see if we have an explicit route
4090 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4091 // (getStrategy(stream)).
4092 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4093 // and activity count is non-zero
4094 // the device = the device from the descriptor in the RouteMap, and exit.
4095 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4096 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4097 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent093a20c2015-06-11 12:33:29 -07004098 // Special case for accessibility strategy which must follow any strategy it is
4099 // currently remapped to
4100 bool strategyMatch = (strat == strategy) ||
4101 ((strategy == STRATEGY_ACCESSIBILITY) &&
4102 ((mEngine->getStrategyForUsage(
4103 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) ||
4104 (strat == STRATEGY_MEDIA)));
4105 if (strategyMatch && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004106 return route->mDeviceDescriptor->type();
4107 }
4108 }
4109
Eric Laurente552edb2014-03-10 17:42:56 -07004110 if (fromCache) {
4111 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4112 strategy, mDeviceForStrategy[strategy]);
4113 return mDeviceForStrategy[strategy];
4114 }
François Gaffie2110e042015-03-24 08:41:51 +01004115 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004116}
4117
Eric Laurente0720872014-03-11 09:30:41 -07004118void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004119{
4120 for (int i = 0; i < NUM_STRATEGIES; i++) {
4121 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4122 }
4123 mPreviousOutputs = mOutputs;
4124}
4125
Eric Laurent1f2f2232014-06-02 12:01:23 -07004126uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004127 audio_devices_t prevDevice,
4128 uint32_t delayMs)
4129{
4130 // mute/unmute strategies using an incompatible device combination
4131 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4132 // if unmuting, unmute only after the specified delay
4133 if (outputDesc->isDuplicated()) {
4134 return 0;
4135 }
4136
4137 uint32_t muteWaitMs = 0;
4138 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004139 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004140
4141 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4142 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004143 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004144 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4145 bool doMute = false;
4146
4147 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4148 doMute = true;
4149 outputDesc->mStrategyMutedByDevice[i] = true;
4150 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4151 doMute = true;
4152 outputDesc->mStrategyMutedByDevice[i] = false;
4153 }
Eric Laurent99401132014-05-07 19:48:15 -07004154 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004155 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004156 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004157 // skip output if it does not share any device with current output
4158 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4159 == AUDIO_DEVICE_NONE) {
4160 continue;
4161 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004162 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4163 mute ? "muting" : "unmuting", i, curDevice);
4164 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004165 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004166 if (mute) {
4167 // FIXME: should not need to double latency if volume could be applied
4168 // immediately by the audioflinger mixer. We must account for the delay
4169 // between now and the next time the audioflinger thread for this output
4170 // will process a buffer (which corresponds to one buffer size,
4171 // usually 1/2 or 1/4 of the latency).
4172 if (muteWaitMs < desc->latency() * 2) {
4173 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004174 }
4175 }
4176 }
4177 }
4178 }
4179 }
4180
Eric Laurent99401132014-05-07 19:48:15 -07004181 // temporary mute output if device selection changes to avoid volume bursts due to
4182 // different per device volumes
4183 if (outputDesc->isActive() && (device != prevDevice)) {
4184 if (muteWaitMs < outputDesc->latency() * 2) {
4185 muteWaitMs = outputDesc->latency() * 2;
4186 }
4187 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004188 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004189 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004190 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004191 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004192 muteWaitMs *2, device);
4193 }
4194 }
4195 }
4196
Eric Laurente552edb2014-03-10 17:42:56 -07004197 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4198 if (muteWaitMs > delayMs) {
4199 muteWaitMs -= delayMs;
4200 usleep(muteWaitMs * 1000);
4201 return muteWaitMs;
4202 }
4203 return 0;
4204}
4205
Eric Laurentc75307b2015-03-17 15:29:32 -07004206uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004207 audio_devices_t device,
4208 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004209 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004210 audio_patch_handle_t *patchHandle,
4211 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004212{
Eric Laurentc75307b2015-03-17 15:29:32 -07004213 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004214 AudioParameter param;
4215 uint32_t muteWaitMs;
4216
4217 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004218 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4219 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004220 return muteWaitMs;
4221 }
4222 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4223 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004224 if ((device != AUDIO_DEVICE_NONE) &&
4225 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004226 return 0;
4227 }
4228
4229 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004230 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004231
4232 audio_devices_t prevDevice = outputDesc->mDevice;
4233
Paul McLeanaa981192015-03-21 09:55:15 -07004234 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004235
4236 if (device != AUDIO_DEVICE_NONE) {
4237 outputDesc->mDevice = device;
4238 }
4239 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4240
4241 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004242 // the requested device is AUDIO_DEVICE_NONE
4243 // OR the requested device is the same as current device
4244 // AND force is not specified
4245 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004246 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004247 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4248 !force &&
4249 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004250 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004251 return muteWaitMs;
4252 }
4253
4254 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004255
Eric Laurente552edb2014-03-10 17:42:56 -07004256 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004257 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004258 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004259 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004260 DeviceVector deviceList = (address == NULL) ?
4261 mAvailableOutputDevices.getDevicesFromType(device)
4262 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004263 if (!deviceList.isEmpty()) {
4264 struct audio_patch patch;
4265 outputDesc->toAudioPortConfig(&patch.sources[0]);
4266 patch.num_sources = 1;
4267 patch.num_sinks = 0;
4268 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4269 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004270 patch.num_sinks++;
4271 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004272 ssize_t index;
4273 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4274 index = mAudioPatches.indexOfKey(*patchHandle);
4275 } else {
4276 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4277 }
4278 sp< AudioPatch> patchDesc;
4279 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4280 if (index >= 0) {
4281 patchDesc = mAudioPatches.valueAt(index);
4282 afPatchHandle = patchDesc->mAfPatchHandle;
4283 }
4284
Eric Laurent1c333e22014-05-20 10:48:17 -07004285 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004286 &afPatchHandle,
4287 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004288 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4289 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004290 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004291 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004292 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004293 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004294 addAudioPatch(patchDesc->mHandle, patchDesc);
4295 } else {
4296 patchDesc->mPatch = patch;
4297 }
4298 patchDesc->mAfPatchHandle = afPatchHandle;
4299 patchDesc->mUid = mUidCached;
4300 if (patchHandle) {
4301 *patchHandle = patchDesc->mHandle;
4302 }
4303 outputDesc->mPatchHandle = patchDesc->mHandle;
4304 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004305 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 }
4307 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004308
4309 // inform all input as well
4310 for (size_t i = 0; i < mInputs.size(); i++) {
4311 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004312 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004313 AudioParameter inputCmd = AudioParameter();
4314 ALOGV("%s: inform input %d of device:%d", __func__,
4315 inputDescriptor->mIoHandle, device);
4316 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4317 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4318 inputCmd.toString(),
4319 delayMs);
4320 }
4321 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004322 }
Eric Laurente552edb2014-03-10 17:42:56 -07004323
4324 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004325 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004326
4327 return muteWaitMs;
4328}
4329
Eric Laurentc75307b2015-03-17 15:29:32 -07004330status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004331 int delayMs,
4332 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004333{
Eric Laurent6a94d692014-05-20 11:18:06 -07004334 ssize_t index;
4335 if (patchHandle) {
4336 index = mAudioPatches.indexOfKey(*patchHandle);
4337 } else {
4338 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4339 }
4340 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004341 return INVALID_OPERATION;
4342 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004343 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4344 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004345 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4346 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004347 removeAudioPatch(patchDesc->mHandle);
4348 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004349 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004350 return status;
4351}
4352
4353status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4354 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004355 bool force,
4356 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004357{
4358 status_t status = NO_ERROR;
4359
Eric Laurent1f2f2232014-06-02 12:01:23 -07004360 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004361 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4362 inputDesc->mDevice = device;
4363
4364 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4365 if (!deviceList.isEmpty()) {
4366 struct audio_patch patch;
4367 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004368 // AUDIO_SOURCE_HOTWORD is for internal use only:
4369 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004370 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4371 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004372 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4373 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004374 patch.num_sinks = 1;
4375 //only one input device for now
4376 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004377 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004378 ssize_t index;
4379 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4380 index = mAudioPatches.indexOfKey(*patchHandle);
4381 } else {
4382 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4383 }
4384 sp< AudioPatch> patchDesc;
4385 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4386 if (index >= 0) {
4387 patchDesc = mAudioPatches.valueAt(index);
4388 afPatchHandle = patchDesc->mAfPatchHandle;
4389 }
4390
Eric Laurent1c333e22014-05-20 10:48:17 -07004391 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004392 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004393 0);
4394 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004395 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004396 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004397 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004398 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004399 addAudioPatch(patchDesc->mHandle, patchDesc);
4400 } else {
4401 patchDesc->mPatch = patch;
4402 }
4403 patchDesc->mAfPatchHandle = afPatchHandle;
4404 patchDesc->mUid = mUidCached;
4405 if (patchHandle) {
4406 *patchHandle = patchDesc->mHandle;
4407 }
4408 inputDesc->mPatchHandle = patchDesc->mHandle;
4409 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004410 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004411 }
4412 }
4413 }
4414 return status;
4415}
4416
Eric Laurent6a94d692014-05-20 11:18:06 -07004417status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4418 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004419{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004420 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004421 ssize_t index;
4422 if (patchHandle) {
4423 index = mAudioPatches.indexOfKey(*patchHandle);
4424 } else {
4425 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4426 }
4427 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004428 return INVALID_OPERATION;
4429 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004430 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4431 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004432 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4433 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004434 removeAudioPatch(patchDesc->mHandle);
4435 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004436 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004437 return status;
4438}
4439
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004440sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004441 String8 address,
4442 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004443 audio_format_t& format,
4444 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004445 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004446{
4447 // Choose an input profile based on the requested capture parameters: select the first available
4448 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004449 //
4450 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4451 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004452
4453 for (size_t i = 0; i < mHwModules.size(); i++)
4454 {
4455 if (mHwModules[i]->mHandle == 0) {
4456 continue;
4457 }
4458 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4459 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004460 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004461 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004462 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004463 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004464 format,
4465 &format /*updatedFormat*/,
4466 channelMask,
4467 &channelMask /*updatedChannelMask*/,
4468 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004469
Eric Laurente552edb2014-03-10 17:42:56 -07004470 return profile;
4471 }
4472 }
4473 }
4474 return NULL;
4475}
4476
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004477
4478audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004479 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004480{
François Gaffie036e1e92015-03-19 10:16:24 +01004481 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4482 audio_devices_t selectedDeviceFromMix =
4483 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004484
François Gaffie036e1e92015-03-19 10:16:24 +01004485 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4486 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004487 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004488 return getDeviceForInputSource(inputSource);
4489}
4490
4491audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4492{
Paul McLean466dc8e2015-04-17 13:15:36 -06004493 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4494 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004495 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004496 return route->mDeviceDescriptor->type();
4497 }
4498 }
4499
4500 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004501}
4502
Eric Laurente0720872014-03-11 09:30:41 -07004503float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004504 int index,
4505 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004506{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004507 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004508
4509 // if a headset is connected, apply the following rules to ring tones and notifications
4510 // to avoid sound level bursts in user's ears:
4511 // - always attenuate ring tones and notifications volume by 6dB
4512 // - if music is playing, always limit the volume to current music volume,
4513 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004514 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004515 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4516 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4517 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4518 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4519 ((stream_strategy == STRATEGY_SONIFICATION)
4520 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004521 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004522 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004523 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4524 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004525 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004526 // when the phone is ringing we must consider that music could have been paused just before
4527 // by the music application and behave as if music was active if the last music track was
4528 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004529 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004530 mLimitRingtoneVolume) {
4531 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004532 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4533 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004534 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004535 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4536 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4537 if (volumeDb > minVolDB) {
4538 volumeDb = minVolDB;
4539 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004540 }
4541 }
4542 }
4543
Eric Laurentffbc80f2015-03-18 18:30:19 -07004544 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004545}
4546
Eric Laurente0720872014-03-11 09:30:41 -07004547status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004548 int index,
4549 const sp<AudioOutputDescriptor>& outputDesc,
4550 audio_devices_t device,
4551 int delayMs,
4552 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004553{
Eric Laurente552edb2014-03-10 17:42:56 -07004554 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004555 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004556 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004557 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004558 return NO_ERROR;
4559 }
François Gaffie2110e042015-03-24 08:41:51 +01004560 audio_policy_forced_cfg_t forceUseForComm =
4561 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004562 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004563 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4564 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004565 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004566 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004567 return INVALID_OPERATION;
4568 }
4569
Eric Laurentc75307b2015-03-17 15:29:32 -07004570 if (device == AUDIO_DEVICE_NONE) {
4571 device = outputDesc->device();
4572 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004573
Eric Laurentffbc80f2015-03-18 18:30:19 -07004574 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004575 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004576 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004577 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004578
Eric Laurentffbc80f2015-03-18 18:30:19 -07004579 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004580
Eric Laurent3b73df72014-03-11 09:06:29 -07004581 if (stream == AUDIO_STREAM_VOICE_CALL ||
4582 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004583 float voiceVolume;
4584 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004585 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004586 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004587 } else {
4588 voiceVolume = 1.0;
4589 }
4590
Eric Laurentc75307b2015-03-17 15:29:32 -07004591 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004592 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4593 mLastVoiceVolume = voiceVolume;
4594 }
4595 }
4596
4597 return NO_ERROR;
4598}
4599
Eric Laurentc75307b2015-03-17 15:29:32 -07004600void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4601 audio_devices_t device,
4602 int delayMs,
4603 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004604{
Eric Laurentc75307b2015-03-17 15:29:32 -07004605 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004606
Eric Laurent3b73df72014-03-11 09:06:29 -07004607 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004608 if (stream == AUDIO_STREAM_PATCH) {
4609 continue;
4610 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004611 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004612 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4613 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004614 device,
4615 delayMs,
4616 force);
4617 }
4618}
4619
Eric Laurente0720872014-03-11 09:30:41 -07004620void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004621 bool on,
4622 const sp<AudioOutputDescriptor>& outputDesc,
4623 int delayMs,
4624 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004625{
Eric Laurent72249d52015-06-08 11:12:15 -07004626 ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d",
4627 strategy, on, outputDesc->getId());
Eric Laurent3b73df72014-03-11 09:06:29 -07004628 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004629 if (stream == AUDIO_STREAM_PATCH) {
4630 continue;
4631 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004632 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004633 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004634 }
4635 }
4636}
4637
Eric Laurente0720872014-03-11 09:30:41 -07004638void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004639 bool on,
4640 const sp<AudioOutputDescriptor>& outputDesc,
4641 int delayMs,
4642 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004643{
Eric Laurentc75307b2015-03-17 15:29:32 -07004644 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004645 if (device == AUDIO_DEVICE_NONE) {
4646 device = outputDesc->device();
4647 }
4648
Eric Laurentc75307b2015-03-17 15:29:32 -07004649 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4650 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004651
4652 if (on) {
4653 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004654 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004655 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004656 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004657 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004658 }
4659 }
4660 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4661 outputDesc->mMuteCount[stream]++;
4662 } else {
4663 if (outputDesc->mMuteCount[stream] == 0) {
4664 ALOGV("setStreamMute() unmuting non muted stream!");
4665 return;
4666 }
4667 if (--outputDesc->mMuteCount[stream] == 0) {
4668 checkAndSetVolume(stream,
4669 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004670 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004671 device,
4672 delayMs);
4673 }
4674 }
4675}
4676
Eric Laurente0720872014-03-11 09:30:41 -07004677void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004678 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004679{
Eric Laurent87ffa392015-05-22 10:32:38 -07004680 if(!hasPrimaryOutput()) {
4681 return;
4682 }
4683
Eric Laurente552edb2014-03-10 17:42:56 -07004684 // if the stream pertains to sonification strategy and we are in call we must
4685 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4686 // in the device used for phone strategy and play the tone if the selected device does not
4687 // interfere with the device used for phone strategy
4688 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4689 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004690 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004691 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4692 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004693 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004694 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4695 stream, starting, outputDesc->mDevice, stateChange);
4696 if (outputDesc->mRefCount[stream]) {
4697 int muteCount = 1;
4698 if (stateChange) {
4699 muteCount = outputDesc->mRefCount[stream];
4700 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004701 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004702 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4703 for (int i = 0; i < muteCount; i++) {
4704 setStreamMute(stream, starting, mPrimaryOutput);
4705 }
4706 } else {
4707 ALOGV("handleIncallSonification() high visibility");
4708 if (outputDesc->device() &
4709 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4710 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4711 for (int i = 0; i < muteCount; i++) {
4712 setStreamMute(stream, starting, mPrimaryOutput);
4713 }
4714 }
4715 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004716 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4717 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004718 } else {
4719 mpClientInterface->stopTone();
4720 }
4721 }
4722 }
4723 }
4724}
4725
Paul McLeanaa981192015-03-21 09:55:15 -07004726
Paul McLean466dc8e2015-04-17 13:15:36 -06004727
Eric Laurente0720872014-03-11 09:30:41 -07004728void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004729{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004730 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004731 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004732 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004733 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004734 mAvailableOutputDevices.add(mDefaultOutputDevice);
4735 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004736
4737 module = new HwModule("primary");
4738
Eric Laurent322b4d22015-04-03 15:57:54 -07004739 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4740 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004741 profile->mSamplingRates.add(44100);
4742 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4743 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004744 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004745 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4746 module->mOutputProfiles.add(profile);
4747
Eric Laurent322b4d22015-04-03 15:57:54 -07004748 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4749 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004750 profile->mSamplingRates.add(8000);
4751 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4752 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004753 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004754 module->mInputProfiles.add(profile);
4755
4756 mHwModules.add(module);
4757}
4758
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004759audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4760{
4761 // flags to stream type mapping
4762 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4763 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4764 }
4765 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4766 return AUDIO_STREAM_BLUETOOTH_SCO;
4767 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004768 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4769 return AUDIO_STREAM_TTS;
4770 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004771
4772 // usage to stream type mapping
4773 switch (attr->usage) {
4774 case AUDIO_USAGE_MEDIA:
4775 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004776 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4777 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004778 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004779 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4780 return AUDIO_STREAM_ALARM;
4781 }
4782 if (isStreamActive(AUDIO_STREAM_RING)) {
4783 return AUDIO_STREAM_RING;
4784 }
4785 if (isInCall()) {
4786 return AUDIO_STREAM_VOICE_CALL;
4787 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004788 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004789 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4790 return AUDIO_STREAM_SYSTEM;
4791 case AUDIO_USAGE_VOICE_COMMUNICATION:
4792 return AUDIO_STREAM_VOICE_CALL;
4793
4794 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4795 return AUDIO_STREAM_DTMF;
4796
4797 case AUDIO_USAGE_ALARM:
4798 return AUDIO_STREAM_ALARM;
4799 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4800 return AUDIO_STREAM_RING;
4801
4802 case AUDIO_USAGE_NOTIFICATION:
4803 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4804 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4805 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4806 case AUDIO_USAGE_NOTIFICATION_EVENT:
4807 return AUDIO_STREAM_NOTIFICATION;
4808
4809 case AUDIO_USAGE_UNKNOWN:
4810 default:
4811 return AUDIO_STREAM_MUSIC;
4812 }
4813}
Eric Laurente83b55d2014-11-14 10:06:21 -08004814
François Gaffie53615e22015-03-19 09:24:12 +01004815bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4816{
Eric Laurente83b55d2014-11-14 10:06:21 -08004817 // has flags that map to a strategy?
4818 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4819 return true;
4820 }
4821
4822 // has known usage?
4823 switch (paa->usage) {
4824 case AUDIO_USAGE_UNKNOWN:
4825 case AUDIO_USAGE_MEDIA:
4826 case AUDIO_USAGE_VOICE_COMMUNICATION:
4827 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4828 case AUDIO_USAGE_ALARM:
4829 case AUDIO_USAGE_NOTIFICATION:
4830 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4831 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4832 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4833 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4834 case AUDIO_USAGE_NOTIFICATION_EVENT:
4835 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4836 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4837 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4838 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004839 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004840 break;
4841 default:
4842 return false;
4843 }
4844 return true;
4845}
4846
François Gaffiead3183e2015-03-18 16:55:35 +01004847bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4848 routing_strategy strategy, uint32_t inPastMs,
4849 nsecs_t sysTime) const
4850{
4851 if ((sysTime == 0) && (inPastMs != 0)) {
4852 sysTime = systemTime();
4853 }
4854 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4855 if (i == AUDIO_STREAM_PATCH) {
4856 continue;
4857 }
4858 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4859 (NUM_STRATEGIES == strategy)) &&
4860 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4861 return true;
4862 }
4863 }
4864 return false;
4865}
4866
François Gaffie2110e042015-03-24 08:41:51 +01004867audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4868{
4869 return mEngine->getForceUse(usage);
4870}
4871
4872bool AudioPolicyManager::isInCall()
4873{
4874 return isStateInCall(mEngine->getPhoneState());
4875}
4876
4877bool AudioPolicyManager::isStateInCall(int state)
4878{
4879 return is_state_in_call(state);
4880}
4881
Eric Laurente552edb2014-03-10 17:42:56 -07004882}; // namespace android