blob: 665cce953c48f1a8eb67fa638e25b03827e6203b [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
405 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
406 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
407 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
408 status);
409 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100410 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 mCallTxPatch->mAfPatchHandle = afPatchHandle;
412 mCallTxPatch->mUid = mUidCached;
413 }
414 }
415}
416
Eric Laurente0720872014-03-11 09:30:41 -0700417void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700418{
419 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100420 // store previous phone state for management of sonification strategy below
421 int oldState = mEngine->getPhoneState();
422
423 if (mEngine->setPhoneState(state) != NO_ERROR) {
424 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700425 return;
426 }
François Gaffie2110e042015-03-24 08:41:51 +0100427 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700428 // if leaving call state, handle special case of active streams
429 // pertaining to sonification strategy see handleIncallSonification()
430 if (isInCall()) {
431 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (stream == AUDIO_STREAM_PATCH) {
434 continue;
435 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700436 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800438
439 // force reevaluating accessibility routing when call starts
440 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 }
442
François Gaffie2110e042015-03-24 08:41:51 +0100443 /**
444 * Switching to or from incall state or switching between telephony and VoIP lead to force
445 * routing command.
446 */
447 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
448 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700449
450 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700451 checkA2dpSuspend();
452 checkOutputForAllStrategies();
453 updateDevicesAndOutputs();
454
Eric Laurente552edb2014-03-10 17:42:56 -0700455 int delayMs = 0;
456 if (isStateInCall(state)) {
457 nsecs_t sysTime = systemTime();
458 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700459 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // mute media and sonification strategies and delay device switch by the largest
461 // latency of any output where either strategy is active.
462 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100463 if ((isStrategyActive(desc, STRATEGY_MEDIA,
464 SONIFICATION_HEADSET_MUSIC_DELAY,
465 sysTime) ||
466 isStrategyActive(desc, STRATEGY_SONIFICATION,
467 SONIFICATION_HEADSET_MUSIC_DELAY,
468 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700469 (delayMs < (int)desc->latency()*2)) {
470 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700472 setStrategyMute(STRATEGY_MEDIA, true, desc);
473 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700474 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700475 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
476 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700477 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
478 }
479 }
480
Eric Laurent87ffa392015-05-22 10:32:38 -0700481 if (hasPrimaryOutput()) {
482 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
483 // the device returned is not necessarily reachable via this output
484 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
485 // force routing command to audio hardware when ending call
486 // even if no device change is needed
487 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
488 rxDevice = mPrimaryOutput->device();
489 }
Eric Laurente552edb2014-03-10 17:42:56 -0700490
Eric Laurent87ffa392015-05-22 10:32:38 -0700491 if (state == AUDIO_MODE_IN_CALL) {
492 updateCallRouting(rxDevice, delayMs);
493 } else if (oldState == AUDIO_MODE_IN_CALL) {
494 if (mCallRxPatch != 0) {
495 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
496 mCallRxPatch.clear();
497 }
498 if (mCallTxPatch != 0) {
499 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
500 mCallTxPatch.clear();
501 }
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 } else {
504 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700506 }
Eric Laurente552edb2014-03-10 17:42:56 -0700507 // if entering in call state, handle special case of active streams
508 // pertaining to sonification strategy see handleIncallSonification()
509 if (isStateInCall(state)) {
510 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800512 if (stream == AUDIO_STREAM_PATCH) {
513 continue;
514 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700515 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 }
517 }
518
519 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 if (state == AUDIO_MODE_RINGTONE &&
521 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700522 mLimitRingtoneVolume = true;
523 } else {
524 mLimitRingtoneVolume = false;
525 }
526}
527
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700528audio_mode_t AudioPolicyManager::getPhoneState() {
529 return mEngine->getPhoneState();
530}
531
Eric Laurente0720872014-03-11 09:30:41 -0700532void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700533 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700534{
François Gaffie2110e042015-03-24 08:41:51 +0100535 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700536
François Gaffie2110e042015-03-24 08:41:51 +0100537 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
538 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
539 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700540 }
François Gaffie2110e042015-03-24 08:41:51 +0100541 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
542 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
543 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700544
545 // check for device and output changes triggered by new force usage
546 checkA2dpSuspend();
547 checkOutputForAllStrategies();
548 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700549 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700550 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
551 updateCallRouting(newDevice);
552 }
Eric Laurente552edb2014-03-10 17:42:56 -0700553 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700554 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
555 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
556 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
557 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700558 }
Eric Laurente552edb2014-03-10 17:42:56 -0700559 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700560 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700561 }
562 }
563
François Gaffie53615e22015-03-19 09:24:12 +0100564 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700565 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700566 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700567 }
568
569}
570
Eric Laurente0720872014-03-11 09:30:41 -0700571void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700572{
573 ALOGV("setSystemProperty() property %s, value %s", property, value);
574}
575
576// Find a direct output profile compatible with the parameters passed, even if the input flags do
577// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800578sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700579 audio_devices_t device,
580 uint32_t samplingRate,
581 audio_format_t format,
582 audio_channel_mask_t channelMask,
583 audio_output_flags_t flags)
584{
Eric Laurent861a6282015-05-18 15:40:16 -0700585 // only retain flags that will drive the direct output profile selection
586 // if explicitly requested
587 static const uint32_t kRelevantFlags =
588 (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
589 flags =
590 (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT);
591
592 sp<IOProfile> profile;
593
Eric Laurente552edb2014-03-10 17:42:56 -0700594 for (size_t i = 0; i < mHwModules.size(); i++) {
595 if (mHwModules[i]->mHandle == 0) {
596 continue;
597 }
598 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent861a6282015-05-18 15:40:16 -0700599 sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j];
600 if (!curProfile->isCompatibleProfile(device, String8(""),
Andy Hungf129b032015-04-07 13:45:50 -0700601 samplingRate, NULL /*updatedSamplingRate*/,
602 format, NULL /*updatedFormat*/,
603 channelMask, NULL /*updatedChannelMask*/,
Eric Laurent861a6282015-05-18 15:40:16 -0700604 flags)) {
605 continue;
606 }
607 // reject profiles not corresponding to a device currently available
608 if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) {
609 continue;
610 }
611 // if several profiles are compatible, give priority to one with offload capability
612 if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) {
613 continue;
614 }
615 profile = curProfile;
616 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
617 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700618 }
Eric Laurente552edb2014-03-10 17:42:56 -0700619 }
620 }
Eric Laurent861a6282015-05-18 15:40:16 -0700621 return profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700622}
623
Eric Laurente0720872014-03-11 09:30:41 -0700624audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100625 uint32_t samplingRate,
626 audio_format_t format,
627 audio_channel_mask_t channelMask,
628 audio_output_flags_t flags,
629 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700630{
Eric Laurent3b73df72014-03-11 09:06:29 -0700631 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700632 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
633 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
634 device, stream, samplingRate, format, channelMask, flags);
635
Eric Laurente83b55d2014-11-14 10:06:21 -0800636 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
637 stream, samplingRate,format, channelMask,
638 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700639}
640
Eric Laurente83b55d2014-11-14 10:06:21 -0800641status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
642 audio_io_handle_t *output,
643 audio_session_t session,
644 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700645 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800646 uint32_t samplingRate,
647 audio_format_t format,
648 audio_channel_mask_t channelMask,
649 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700650 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800651 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700652{
Eric Laurente83b55d2014-11-14 10:06:21 -0800653 audio_attributes_t attributes;
654 if (attr != NULL) {
655 if (!isValidAttributes(attr)) {
656 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
657 attr->usage, attr->content_type, attr->flags,
658 attr->tags);
659 return BAD_VALUE;
660 }
661 attributes = *attr;
662 } else {
663 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
664 ALOGE("getOutputForAttr(): invalid stream type");
665 return BAD_VALUE;
666 }
667 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700668 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700669 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100670 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
671 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
672 if (!audio_is_linear_pcm(format)) {
673 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800674 }
François Gaffie036e1e92015-03-19 10:16:24 +0100675 *stream = streamTypefromAttributesInt(&attributes);
676 *output = desc->mIoHandle;
677 ALOGV("getOutputForAttr() returns output %d", *output);
678 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800679 }
680 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
681 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
682 return BAD_VALUE;
683 }
684
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700685 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
686 " session %d selectedDeviceId %d",
687 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
688 session, selectedDeviceId);
689
690 *stream = streamTypefromAttributesInt(&attributes);
691
692 // Explicit routing?
693 sp<DeviceDescriptor> deviceDesc;
694 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
695 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
696 deviceDesc = mAvailableOutputDevices[i];
697 break;
698 }
699 }
700 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700701
Eric Laurente83b55d2014-11-14 10:06:21 -0800702 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700703 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700704
Eric Laurente83b55d2014-11-14 10:06:21 -0800705 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700706 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
707 }
708
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700709 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700710 device, samplingRate, format, channelMask, flags);
711
Eric Laurente83b55d2014-11-14 10:06:21 -0800712 *output = getOutputForDevice(device, session, *stream,
713 samplingRate, format, channelMask,
714 flags, offloadInfo);
715 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700716 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800717 return INVALID_OPERATION;
718 }
Paul McLeanaa981192015-03-21 09:55:15 -0700719
Eric Laurente83b55d2014-11-14 10:06:21 -0800720 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700721}
722
723audio_io_handle_t AudioPolicyManager::getOutputForDevice(
724 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800725 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700726 audio_stream_type_t stream,
727 uint32_t samplingRate,
728 audio_format_t format,
729 audio_channel_mask_t channelMask,
730 audio_output_flags_t flags,
731 const audio_offload_info_t *offloadInfo)
732{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700733 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700734 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700735 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700736
Eric Laurente552edb2014-03-10 17:42:56 -0700737#ifdef AUDIO_POLICY_TEST
738 if (mCurOutput != 0) {
739 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
740 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
741
742 if (mTestOutputs[mCurOutput] == 0) {
743 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700744 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
745 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700746 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700747 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700748 outputDesc->mFlags =
749 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700750 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700751 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
752 config.sample_rate = mTestSamplingRate;
753 config.channel_mask = mTestChannels;
754 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700755 if (offloadInfo != NULL) {
756 config.offload_info = *offloadInfo;
757 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700758 status = mpClientInterface->openOutput(0,
759 &mTestOutputs[mCurOutput],
760 &config,
761 &outputDesc->mDevice,
762 String8(""),
763 &outputDesc->mLatency,
764 outputDesc->mFlags);
765 if (status == NO_ERROR) {
766 outputDesc->mSamplingRate = config.sample_rate;
767 outputDesc->mFormat = config.format;
768 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700769 AudioParameter outputCmd = AudioParameter();
770 outputCmd.addInt(String8("set_id"),mCurOutput);
771 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
772 addOutput(mTestOutputs[mCurOutput], outputDesc);
773 }
774 }
775 return mTestOutputs[mCurOutput];
776 }
777#endif //AUDIO_POLICY_TEST
778
779 // open a direct output if required by specified parameters
780 //force direct flag if offload flag is set: offloading implies a direct output stream
781 // and all common behaviors are driven by checking only the direct flag
782 // this should normally be set appropriately in the policy configuration file
783 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700784 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700785 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700786 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
787 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
788 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800789 // only allow deep buffering for music stream type
790 if (stream != AUDIO_STREAM_MUSIC) {
791 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
792 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700793 if (stream == AUDIO_STREAM_TTS) {
794 flags = AUDIO_OUTPUT_FLAG_TTS;
795 }
Eric Laurente552edb2014-03-10 17:42:56 -0700796
Eric Laurentb732cf52014-09-24 19:08:21 -0700797 sp<IOProfile> profile;
798
799 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700800 // and not explicitly requested
801 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
802 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700803 audio_channel_count_from_out_mask(channelMask) <= 2) {
804 goto non_direct_output;
805 }
806
Eric Laurente552edb2014-03-10 17:42:56 -0700807 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
808 // creating an offloaded track and tearing it down immediately after start when audioflinger
809 // detects there is an active non offloadable effect.
810 // FIXME: We should check the audio session here but we do not have it in this context.
811 // This may prevent offloading in rare situations where effects are left active by apps
812 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700813
Eric Laurente552edb2014-03-10 17:42:56 -0700814 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100815 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700816 profile = getProfileForDirectOutput(device,
817 samplingRate,
818 format,
819 channelMask,
820 (audio_output_flags_t)flags);
821 }
822
Eric Laurent1c333e22014-05-20 10:48:17 -0700823 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700824 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700825
826 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700827 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700828 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
829 outputDesc = desc;
830 // reuse direct output if currently open and configured with same parameters
831 if ((samplingRate == outputDesc->mSamplingRate) &&
832 (format == outputDesc->mFormat) &&
833 (channelMask == outputDesc->mChannelMask)) {
834 outputDesc->mDirectOpenCount++;
835 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
836 return mOutputs.keyAt(i);
837 }
838 }
839 }
840 // close direct output if currently open and configured with different parameters
841 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700842 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700843 }
Eric Laurent861a6282015-05-18 15:40:16 -0700844
845 // if the selected profile is offloaded and no offload info was specified,
846 // create a default one
847 audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER;
848 if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) {
849 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
850 defaultOffloadInfo.sample_rate = samplingRate;
851 defaultOffloadInfo.channel_mask = channelMask;
852 defaultOffloadInfo.format = format;
853 defaultOffloadInfo.stream_type = stream;
854 defaultOffloadInfo.bit_rate = 0;
855 defaultOffloadInfo.duration_us = -1;
856 defaultOffloadInfo.has_video = true; // conservative
857 defaultOffloadInfo.is_streaming = true; // likely
858 offloadInfo = &defaultOffloadInfo;
859 }
860
Eric Laurentc75307b2015-03-17 15:29:32 -0700861 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700862 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700863 outputDesc->mLatency = 0;
Eric Laurent861a6282015-05-18 15:40:16 -0700864 outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700865 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
866 config.sample_rate = samplingRate;
867 config.channel_mask = channelMask;
868 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700869 if (offloadInfo != NULL) {
870 config.offload_info = *offloadInfo;
871 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700872 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700873 &output,
874 &config,
875 &outputDesc->mDevice,
876 String8(""),
877 &outputDesc->mLatency,
878 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700879
880 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700881 if (status != NO_ERROR ||
882 (samplingRate != 0 && samplingRate != config.sample_rate) ||
883 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
884 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700885 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
886 "format %d %d, channelMask %04x %04x", output, samplingRate,
887 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
888 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700889 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700890 mpClientInterface->closeOutput(output);
891 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800892 // fall back to mixer output if possible when the direct output could not be open
893 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
894 goto non_direct_output;
895 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700896 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700897 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700898 outputDesc->mSamplingRate = config.sample_rate;
899 outputDesc->mChannelMask = config.channel_mask;
900 outputDesc->mFormat = config.format;
901 outputDesc->mRefCount[stream] = 0;
902 outputDesc->mStopTime[stream] = 0;
903 outputDesc->mDirectOpenCount = 1;
904
Eric Laurente552edb2014-03-10 17:42:56 -0700905 audio_io_handle_t srcOutput = getOutputForEffect();
906 addOutput(output, outputDesc);
907 audio_io_handle_t dstOutput = getOutputForEffect();
908 if (dstOutput == output) {
909 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
910 }
911 mPreviousOutputs = mOutputs;
912 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700913 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700914 return output;
915 }
916
Eric Laurentb732cf52014-09-24 19:08:21 -0700917non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700918 // ignoring channel mask due to downmix capability in mixer
919
920 // open a non direct output
921
922 // for non direct outputs, only PCM is supported
923 if (audio_is_linear_pcm(format)) {
924 // get which output is suitable for the specified stream. The actual
925 // routing change will happen when startOutput() will be called
926 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
927
Eric Laurent8838a382014-09-08 16:44:28 -0700928 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
929 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
930 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700931 }
932 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
933 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
934
Paul McLeanaa981192015-03-21 09:55:15 -0700935 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700936
937 return output;
938}
939
Eric Laurente0720872014-03-11 09:30:41 -0700940audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700941 audio_output_flags_t flags,
942 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700943{
944 // select one output among several that provide a path to a particular device or set of
945 // devices (the list was previously build by getOutputsForDevice()).
946 // The priority is as follows:
947 // 1: the output with the highest number of requested policy flags
948 // 2: the primary output
949 // 3: the first output in the list
950
951 if (outputs.size() == 0) {
952 return 0;
953 }
954 if (outputs.size() == 1) {
955 return outputs[0];
956 }
957
958 int maxCommonFlags = 0;
959 audio_io_handle_t outputFlags = 0;
960 audio_io_handle_t outputPrimary = 0;
961
962 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700963 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700964 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700965 // if a valid format is specified, skip output if not compatible
966 if (format != AUDIO_FORMAT_INVALID) {
967 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
968 if (format != outputDesc->mFormat) {
969 continue;
970 }
971 } else if (!audio_is_linear_pcm(format)) {
972 continue;
973 }
974 }
975
Eric Laurent3b73df72014-03-11 09:06:29 -0700976 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700977 if (commonFlags > maxCommonFlags) {
978 outputFlags = outputs[i];
979 maxCommonFlags = commonFlags;
980 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
981 }
982 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
983 outputPrimary = outputs[i];
984 }
985 }
986 }
987
988 if (outputFlags != 0) {
989 return outputFlags;
990 }
991 if (outputPrimary != 0) {
992 return outputPrimary;
993 }
994
995 return outputs[0];
996}
997
Eric Laurente0720872014-03-11 09:30:41 -0700998status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700999 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001000 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001001{
Paul McLeanaa981192015-03-21 09:55:15 -07001002 ALOGV("startOutput() output %d, stream %d, session %d",
1003 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -07001004 ssize_t index = mOutputs.indexOfKey(output);
1005 if (index < 0) {
1006 ALOGW("startOutput() unknown output %d", output);
1007 return BAD_VALUE;
1008 }
1009
Eric Laurentc75307b2015-03-17 15:29:32 -07001010 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
1011
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001012 // Routing?
1013 mOutputRoutes.incRouteActivity(session);
1014
Eric Laurentc75307b2015-03-17 15:29:32 -07001015 audio_devices_t newDevice;
1016 if (outputDesc->mPolicyMix != NULL) {
1017 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -07001018 } else if (mOutputRoutes.hasRouteChanged(session)) {
1019 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001020 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -07001021 } else {
1022 newDevice = AUDIO_DEVICE_NONE;
1023 }
1024
1025 uint32_t delayMs = 0;
1026
Eric Laurentc75307b2015-03-17 15:29:32 -07001027 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
1028
1029 if (status != NO_ERROR) {
1030 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001031 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001032 }
1033 // Automatically enable the remote submix input when output is started on a re routing mix
1034 // of type MIX_TYPE_RECORDERS
1035 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1036 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1037 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1038 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1039 outputDesc->mPolicyMix->mRegistrationId,
1040 "remote-submix");
1041 }
1042
1043 if (delayMs != 0) {
1044 usleep(delayMs * 1000);
1045 }
1046
1047 return status;
1048}
1049
1050status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1051 audio_stream_type_t stream,
1052 audio_devices_t device,
1053 uint32_t *delayMs)
1054{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001055 // cannot start playback of STREAM_TTS if any other output is being used
1056 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001057
1058 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001059 if (stream == AUDIO_STREAM_TTS) {
1060 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001061 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001062 return INVALID_OPERATION;
1063 } else {
1064 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1065 }
1066 } else {
1067 // some playback other than beacon starts
1068 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1069 }
1070
Eric Laurente552edb2014-03-10 17:42:56 -07001071 // increment usage count for this stream on the requested output:
1072 // NOTE that the usage count is the same for duplicated output and hardware output which is
1073 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1074 outputDesc->changeRefCount(stream, 1);
1075
Eric Laurent493404d2015-04-21 15:07:36 -07001076 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001077 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001078 if (device == AUDIO_DEVICE_NONE) {
1079 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001080 }
Eric Laurente552edb2014-03-10 17:42:56 -07001081 routing_strategy strategy = getStrategy(stream);
1082 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001083 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1084 (beaconMuteLatency > 0);
1085 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001086 bool force = false;
1087 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001088 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001089 if (desc != outputDesc) {
1090 // force a device change if any other output is managed by the same hw
1091 // module and has a current device selection that differs from selected device.
1092 // In this case, the audio HAL must receive the new device selection so that it can
1093 // change the device currently selected by the other active output.
1094 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001095 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001096 force = true;
1097 }
1098 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001099 // a notification so that audio focus effect can propagate, or that a mute/unmute
1100 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001101 uint32_t latency = desc->latency();
1102 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1103 waitMs = latency;
1104 }
1105 }
1106 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001107 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001108
1109 // handle special case for sonification while in call
1110 if (isInCall()) {
1111 handleIncallSonification(stream, true, false);
1112 }
1113
1114 // apply volume rules for current stream and device if necessary
1115 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001116 mStreams.valueFor(stream).getVolumeIndex(device),
1117 outputDesc,
1118 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001119
1120 // update the outputs if starting an output with a stream that can affect notification
1121 // routing
1122 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001123
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001124 // force reevaluating accessibility routing when ringtone or alarm starts
1125 if (strategy == STRATEGY_SONIFICATION) {
1126 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1127 }
Eric Laurente552edb2014-03-10 17:42:56 -07001128 }
1129 return NO_ERROR;
1130}
1131
1132
Eric Laurente0720872014-03-11 09:30:41 -07001133status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001134 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001135 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001136{
1137 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1138 ssize_t index = mOutputs.indexOfKey(output);
1139 if (index < 0) {
1140 ALOGW("stopOutput() unknown output %d", output);
1141 return BAD_VALUE;
1142 }
1143
Eric Laurentc75307b2015-03-17 15:29:32 -07001144 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001145
Eric Laurentc75307b2015-03-17 15:29:32 -07001146 if (outputDesc->mRefCount[stream] == 1) {
1147 // Automatically disable the remote submix input when output is stopped on a
1148 // re routing mix of type MIX_TYPE_RECORDERS
1149 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1150 outputDesc->mPolicyMix != NULL &&
1151 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1152 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1153 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1154 outputDesc->mPolicyMix->mRegistrationId,
1155 "remote-submix");
1156 }
1157 }
1158
1159 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001160 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001161 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001162 int activityCount = mOutputRoutes.decRouteActivity(session);
1163 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1164
1165 if (forceDeviceUpdate) {
1166 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1167 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001168 }
1169
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001170 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001171}
1172
1173status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001174 audio_stream_type_t stream,
1175 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001176{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001177 // always handle stream stop, check which stream type is stopping
1178 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1179
Eric Laurente552edb2014-03-10 17:42:56 -07001180 // handle special case for sonification while in call
1181 if (isInCall()) {
1182 handleIncallSonification(stream, false, false);
1183 }
1184
1185 if (outputDesc->mRefCount[stream] > 0) {
1186 // decrement usage count of this stream on the output
1187 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001188
Eric Laurente552edb2014-03-10 17:42:56 -07001189 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001190 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001191 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001192 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001193 // delay the device switch by twice the latency because stopOutput() is executed when
1194 // the track stop() command is received and at that time the audio track buffer can
1195 // still contain data that needs to be drained. The latency only covers the audio HAL
1196 // and kernel buffers. Also the latency does not always include additional delay in the
1197 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001198 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001199
1200 // force restoring the device selection on other active outputs if it differs from the
1201 // one being selected for this output
1202 for (size_t i = 0; i < mOutputs.size(); i++) {
1203 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001204 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001205 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001206 desc->isActive() &&
1207 outputDesc->sharesHwModuleWith(desc) &&
1208 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001209 setOutputDevice(desc,
1210 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001211 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001213 }
1214 }
1215 // update the outputs if stopping one with a stream that can affect notification routing
1216 handleNotificationRoutingForStream(stream);
1217 }
1218 return NO_ERROR;
1219 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001220 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001221 return INVALID_OPERATION;
1222 }
1223}
1224
Eric Laurente83b55d2014-11-14 10:06:21 -08001225void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001226 audio_stream_type_t stream __unused,
1227 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001228{
1229 ALOGV("releaseOutput() %d", output);
1230 ssize_t index = mOutputs.indexOfKey(output);
1231 if (index < 0) {
1232 ALOGW("releaseOutput() releasing unknown output %d", output);
1233 return;
1234 }
1235
1236#ifdef AUDIO_POLICY_TEST
1237 int testIndex = testOutputIndex(output);
1238 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001239 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001240 if (outputDesc->isActive()) {
1241 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001242 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001243 mTestOutputs[testIndex] = 0;
1244 }
1245 return;
1246 }
1247#endif //AUDIO_POLICY_TEST
1248
Paul McLeanaa981192015-03-21 09:55:15 -07001249 // Routing
1250 mOutputRoutes.removeRoute(session);
1251
Eric Laurentc75307b2015-03-17 15:29:32 -07001252 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001253 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001254 if (desc->mDirectOpenCount <= 0) {
1255 ALOGW("releaseOutput() invalid open count %d for output %d",
1256 desc->mDirectOpenCount, output);
1257 return;
1258 }
1259 if (--desc->mDirectOpenCount == 0) {
1260 closeOutput(output);
1261 // If effects where present on the output, audioflinger moved them to the primary
1262 // output by default: move them back to the appropriate output.
1263 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001264 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001265 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1266 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001267 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001268 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001269 }
1270 }
1271}
1272
1273
Eric Laurentcaf7f482014-11-25 17:50:47 -08001274status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1275 audio_io_handle_t *input,
1276 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001277 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001278 uint32_t samplingRate,
1279 audio_format_t format,
1280 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001281 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001282 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001283 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001284{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001285 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1286 "session %d, flags %#x",
1287 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001288
Eric Laurentcaf7f482014-11-25 17:50:47 -08001289 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001290 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001291 audio_devices_t device;
1292 // handle legacy remote submix case where the address was not always specified
1293 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001294 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001295 audio_source_t inputSource = attr->source;
1296 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001297 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001298
Eric Laurentc447ded2015-01-06 08:47:05 -08001299 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1300 inputSource = AUDIO_SOURCE_MIC;
1301 }
1302 halInputSource = inputSource;
1303
Paul McLean466dc8e2015-04-17 13:15:36 -06001304 // Explicit routing?
1305 sp<DeviceDescriptor> deviceDesc;
1306 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1307 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1308 deviceDesc = mAvailableInputDevices[i];
1309 break;
1310 }
1311 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001312 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001313
Eric Laurentc447ded2015-01-06 08:47:05 -08001314 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001315 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001316 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001317 if (ret != NO_ERROR) {
1318 return ret;
1319 }
1320 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001321 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1322 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001323 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001324 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001325 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001326 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001327 return BAD_VALUE;
1328 }
Eric Laurentc722f302014-12-10 11:21:49 -08001329 if (policyMix != NULL) {
1330 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001331 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1332 // there is an external policy, but this input is attached to a mix of recorders,
1333 // meaning it receives audio injected into the framework, so the recorder doesn't
1334 // know about it and is therefore considered "legacy"
1335 *inputType = API_INPUT_LEGACY;
1336 } else {
1337 // recording a mix of players defined by an external policy, we're rerouting for
1338 // an external policy
1339 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1340 }
Eric Laurentc722f302014-12-10 11:21:49 -08001341 } else if (audio_is_remote_submix_device(device)) {
1342 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001343 *inputType = API_INPUT_MIX_CAPTURE;
1344 } else {
1345 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001346 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001347 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001348 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001349 case AUDIO_SOURCE_VOICE_UPLINK:
1350 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1351 break;
1352 case AUDIO_SOURCE_VOICE_DOWNLINK:
1353 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1354 break;
1355 case AUDIO_SOURCE_VOICE_CALL:
1356 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1357 break;
1358 default:
1359 break;
1360 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001361 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001362 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1363 if (index >= 0) {
1364 *input = mSoundTriggerSessions.valueFor(session);
1365 isSoundTrigger = true;
1366 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1367 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1368 } else {
1369 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1370 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001371 }
1372 }
1373
Andy Hungf129b032015-04-07 13:45:50 -07001374 // find a compatible input profile (not necessarily identical in parameters)
1375 sp<IOProfile> profile;
1376 // samplingRate and flags may be updated by getInputProfile
1377 uint32_t profileSamplingRate = samplingRate;
1378 audio_format_t profileFormat = format;
1379 audio_channel_mask_t profileChannelMask = channelMask;
1380 audio_input_flags_t profileFlags = flags;
1381 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001382 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001383 profileSamplingRate, profileFormat, profileChannelMask,
1384 profileFlags);
1385 if (profile != 0) {
1386 break; // success
1387 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1388 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1389 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001390 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1391 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001392 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001393 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001394 }
Eric Laurente552edb2014-03-10 17:42:56 -07001395 }
1396
Eric Laurent322b4d22015-04-03 15:57:54 -07001397 if (profile->getModuleHandle() == 0) {
1398 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001399 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001400 }
1401
1402 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001403 config.sample_rate = profileSamplingRate;
1404 config.channel_mask = profileChannelMask;
1405 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001406
Eric Laurent322b4d22015-04-03 15:57:54 -07001407 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001409 &config,
1410 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001411 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001412 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001413 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001414
1415 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001416 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001417 (profileSamplingRate != config.sample_rate) ||
1418 (profileFormat != config.format) ||
1419 (profileChannelMask != config.channel_mask)) {
1420 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1421 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001422 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001423 if (*input != AUDIO_IO_HANDLE_NONE) {
1424 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001425 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001426 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001427 }
1428
Eric Laurent1f2f2232014-06-02 12:01:23 -07001429 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001430 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001431 inputDesc->mRefCount = 0;
1432 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001433 inputDesc->mSamplingRate = profileSamplingRate;
1434 inputDesc->mFormat = profileFormat;
1435 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001436 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001437 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001438 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001439 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001440
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001441 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001442
Eric Laurentcaf7f482014-11-25 17:50:47 -08001443 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001444 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001445
Eric Laurentcaf7f482014-11-25 17:50:47 -08001446 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001447}
1448
Eric Laurent4dc68062014-07-28 17:26:49 -07001449status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1450 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001451{
1452 ALOGV("startInput() input %d", input);
1453 ssize_t index = mInputs.indexOfKey(input);
1454 if (index < 0) {
1455 ALOGW("startInput() unknown input %d", input);
1456 return BAD_VALUE;
1457 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001458 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001459
Eric Laurentc722f302014-12-10 11:21:49 -08001460 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001461 if (index < 0) {
1462 ALOGW("startInput() unknown session %d on input %d", session, input);
1463 return BAD_VALUE;
1464 }
1465
Glenn Kasten74a8e252014-07-24 14:09:55 -07001466 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001467 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001468
1469 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001470 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001471 if (activeInput != 0 && activeInput != input) {
1472
1473 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1474 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001475 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001476 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001477 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001478 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1479 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001480 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001481 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001482 return INVALID_OPERATION;
1483 }
1484 }
1485 }
1486
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001487 // Routing?
1488 mInputRoutes.incRouteActivity(session);
1489
Paul McLean466dc8e2015-04-17 13:15:36 -06001490 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001491 // if input maps to a dynamic policy with an activity listener, notify of state change
1492 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001493 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001494 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1495 MIX_STATE_MIXING);
1496 }
1497
François Gaffie53615e22015-03-19 09:24:12 +01001498 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001499 SoundTrigger::setCaptureState(true);
1500 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001501 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001502
Eric Laurentc722f302014-12-10 11:21:49 -08001503 // automatically enable the remote submix output when input is started if not
1504 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001505 // For remote submix (a virtual device), we open only one input per capture request.
1506 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001507 String8 address = String8("");
1508 if (inputDesc->mPolicyMix == NULL) {
1509 address = String8("0");
1510 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1511 address = inputDesc->mPolicyMix->mRegistrationId;
1512 }
1513 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001514 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001515 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001516 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001517 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001518 }
Eric Laurente552edb2014-03-10 17:42:56 -07001519 }
1520
Eric Laurente552edb2014-03-10 17:42:56 -07001521 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1522
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001523 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001524 return NO_ERROR;
1525}
1526
Eric Laurent4dc68062014-07-28 17:26:49 -07001527status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1528 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001529{
1530 ALOGV("stopInput() input %d", input);
1531 ssize_t index = mInputs.indexOfKey(input);
1532 if (index < 0) {
1533 ALOGW("stopInput() unknown input %d", input);
1534 return BAD_VALUE;
1535 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001536 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001537
Eric Laurentc722f302014-12-10 11:21:49 -08001538 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001539 if (index < 0) {
1540 ALOGW("stopInput() unknown session %d on input %d", session, input);
1541 return BAD_VALUE;
1542 }
1543
Eric Laurente552edb2014-03-10 17:42:56 -07001544 if (inputDesc->mRefCount == 0) {
1545 ALOGW("stopInput() input %d already stopped", input);
1546 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001547 }
1548
1549 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001550
1551 // Routing?
1552 mInputRoutes.decRouteActivity(session);
1553
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001554 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001555 // if input maps to a dynamic policy with an activity listener, notify of state change
1556 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001557 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001558 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1559 MIX_STATE_IDLE);
1560 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001561
Eric Laurentc722f302014-12-10 11:21:49 -08001562 // automatically disable the remote submix output when input is stopped if not
1563 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001564 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001565 String8 address = String8("");
1566 if (inputDesc->mPolicyMix == NULL) {
1567 address = String8("0");
1568 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1569 address = inputDesc->mPolicyMix->mRegistrationId;
1570 }
1571 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001572 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001573 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001574 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001575 }
Eric Laurente552edb2014-03-10 17:42:56 -07001576 }
1577
Eric Laurent1c333e22014-05-20 10:48:17 -07001578 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001579
François Gaffie53615e22015-03-19 09:24:12 +01001580 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001581 SoundTrigger::setCaptureState(false);
1582 }
Eric Laurente552edb2014-03-10 17:42:56 -07001583 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001584 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001585}
1586
Eric Laurent4dc68062014-07-28 17:26:49 -07001587void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1588 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001589{
1590 ALOGV("releaseInput() %d", input);
1591 ssize_t index = mInputs.indexOfKey(input);
1592 if (index < 0) {
1593 ALOGW("releaseInput() releasing unknown input %d", input);
1594 return;
1595 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001596
1597 // Routing
1598 mInputRoutes.removeRoute(session);
1599
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001600 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1601 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001602
Eric Laurentc722f302014-12-10 11:21:49 -08001603 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001604 if (index < 0) {
1605 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1606 return;
1607 }
Eric Laurentc722f302014-12-10 11:21:49 -08001608 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001609 if (inputDesc->mOpenRefCount == 0) {
1610 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1611 return;
1612 }
1613 inputDesc->mOpenRefCount--;
1614 if (inputDesc->mOpenRefCount > 0) {
1615 ALOGV("releaseInput() exit > 0");
1616 return;
1617 }
1618
Eric Laurent05b90f82014-08-27 15:32:29 -07001619 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001620 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001621 ALOGV("releaseInput() exit");
1622}
1623
Eric Laurentd4692962014-05-05 18:13:44 -07001624void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001625 bool patchRemoved = false;
1626
Eric Laurentd4692962014-05-05 18:13:44 -07001627 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001628 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1629 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1630 if (patch_index >= 0) {
1631 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1632 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1633 mAudioPatches.removeItemsAt(patch_index);
1634 patchRemoved = true;
1635 }
Eric Laurentd4692962014-05-05 18:13:44 -07001636 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1637 }
1638 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001639 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001640
1641 if (patchRemoved) {
1642 mpClientInterface->onAudioPatchListUpdate();
1643 }
Eric Laurentd4692962014-05-05 18:13:44 -07001644}
1645
Eric Laurente0720872014-03-11 09:30:41 -07001646void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001647 int indexMin,
1648 int indexMax)
1649{
1650 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001651 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001652 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1653 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001654 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001655 }
Eric Laurente552edb2014-03-10 17:42:56 -07001656}
1657
Eric Laurente0720872014-03-11 09:30:41 -07001658status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001659 int index,
1660 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001661{
1662
Eric Laurentc75307b2015-03-17 15:29:32 -07001663 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1664 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001665 return BAD_VALUE;
1666 }
1667 if (!audio_is_output_device(device)) {
1668 return BAD_VALUE;
1669 }
1670
1671 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001672 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001673
1674 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1675 stream, device, index);
1676
1677 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1678 // clear all device specific values
1679 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001680 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001681 }
François Gaffiedfd74092015-03-19 12:10:59 +01001682 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001683
Eric Laurent31551f82014-10-10 18:21:56 -07001684 // update volume on all outputs whose current device is also selected by the same
1685 // strategy as the device specified by the caller
1686 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001687
1688
1689 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1690 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1691 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001692 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001693 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1694 }
1695 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1696 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001697 return NO_ERROR;
1698 }
Eric Laurente552edb2014-03-10 17:42:56 -07001699 status_t status = NO_ERROR;
1700 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001701 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1702 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001703 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001704 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001705 if (volStatus != NO_ERROR) {
1706 status = volStatus;
1707 }
1708 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001709 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1710 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001711 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001712 }
Eric Laurente552edb2014-03-10 17:42:56 -07001713 }
1714 return status;
1715}
1716
Eric Laurente0720872014-03-11 09:30:41 -07001717status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001718 int *index,
1719 audio_devices_t device)
1720{
1721 if (index == NULL) {
1722 return BAD_VALUE;
1723 }
1724 if (!audio_is_output_device(device)) {
1725 return BAD_VALUE;
1726 }
1727 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1728 // the strategy the stream belongs to.
1729 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1730 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1731 }
François Gaffiedfd74092015-03-19 12:10:59 +01001732 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001733
Eric Laurentc75307b2015-03-17 15:29:32 -07001734 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001735 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1736 return NO_ERROR;
1737}
1738
Eric Laurente0720872014-03-11 09:30:41 -07001739audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001740 const SortedVector<audio_io_handle_t>& outputs)
1741{
1742 // select one output among several suitable for global effects.
1743 // The priority is as follows:
1744 // 1: An offloaded output. If the effect ends up not being offloadable,
1745 // AudioFlinger will invalidate the track and the offloaded output
1746 // will be closed causing the effect to be moved to a PCM output.
1747 // 2: A deep buffer output
1748 // 3: the first output in the list
1749
1750 if (outputs.size() == 0) {
1751 return 0;
1752 }
1753
1754 audio_io_handle_t outputOffloaded = 0;
1755 audio_io_handle_t outputDeepBuffer = 0;
1756
1757 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001758 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001759 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001760 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1761 outputOffloaded = outputs[i];
1762 }
1763 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1764 outputDeepBuffer = outputs[i];
1765 }
1766 }
1767
1768 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1769 outputOffloaded, outputDeepBuffer);
1770 if (outputOffloaded != 0) {
1771 return outputOffloaded;
1772 }
1773 if (outputDeepBuffer != 0) {
1774 return outputDeepBuffer;
1775 }
1776
1777 return outputs[0];
1778}
1779
Eric Laurente0720872014-03-11 09:30:41 -07001780audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001781{
1782 // apply simple rule where global effects are attached to the same output as MUSIC streams
1783
Eric Laurent3b73df72014-03-11 09:06:29 -07001784 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001785 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1786 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1787
1788 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1789 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1790 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1791
1792 return output;
1793}
1794
Eric Laurente0720872014-03-11 09:30:41 -07001795status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001796 audio_io_handle_t io,
1797 uint32_t strategy,
1798 int session,
1799 int id)
1800{
1801 ssize_t index = mOutputs.indexOfKey(io);
1802 if (index < 0) {
1803 index = mInputs.indexOfKey(io);
1804 if (index < 0) {
1805 ALOGW("registerEffect() unknown io %d", io);
1806 return INVALID_OPERATION;
1807 }
1808 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001809 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001810}
1811
Eric Laurentc75307b2015-03-17 15:29:32 -07001812bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1813{
1814 return mOutputs.isStreamActive(stream, inPastMs);
1815}
1816
1817bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1818{
1819 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1820}
1821
Eric Laurente0720872014-03-11 09:30:41 -07001822bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001823{
1824 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001825 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001826 if (inputDescriptor->mRefCount == 0) {
1827 continue;
1828 }
1829 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001830 return true;
1831 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001832 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1833 // corresponds to an active capture triggered by a hardware hotword recognition
1834 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1835 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1836 // FIXME: we should not assume that the first session is the active one and keep
1837 // activity count per session. Same in startInput().
1838 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1839 if (index >= 0) {
1840 return true;
1841 }
1842 }
Eric Laurente552edb2014-03-10 17:42:56 -07001843 }
1844 return false;
1845}
1846
Eric Laurent275e8e92014-11-30 15:14:47 -08001847// Register a list of custom mixes with their attributes and format.
1848// When a mix is registered, corresponding input and output profiles are
1849// added to the remote submix hw module. The profile contains only the
1850// parameters (sampling rate, format...) specified by the mix.
1851// The corresponding input remote submix device is also connected.
1852//
1853// When a remote submix device is connected, the address is checked to select the
1854// appropriate profile and the corresponding input or output stream is opened.
1855//
1856// When capture starts, getInputForAttr() will:
1857// - 1 look for a mix matching the address passed in attribtutes tags if any
1858// - 2 if none found, getDeviceForInputSource() will:
1859// - 2.1 look for a mix matching the attributes source
1860// - 2.2 if none found, default to device selection by policy rules
1861// At this time, the corresponding output remote submix device is also connected
1862// and active playback use cases can be transferred to this mix if needed when reconnecting
1863// after AudioTracks are invalidated
1864//
1865// When playback starts, getOutputForAttr() will:
1866// - 1 look for a mix matching the address passed in attribtutes tags if any
1867// - 2 if none found, look for a mix matching the attributes usage
1868// - 3 if none found, default to device and output selection by policy rules.
1869
1870status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1871{
1872 sp<HwModule> module;
1873 for (size_t i = 0; i < mHwModules.size(); i++) {
1874 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1875 mHwModules[i]->mHandle != 0) {
1876 module = mHwModules[i];
1877 break;
1878 }
1879 }
1880
1881 if (module == 0) {
1882 return INVALID_OPERATION;
1883 }
1884
1885 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1886
1887 for (size_t i = 0; i < mixes.size(); i++) {
1888 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001889
1890 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001891 continue;
1892 }
1893 audio_config_t outputConfig = mixes[i].mFormat;
1894 audio_config_t inputConfig = mixes[i].mFormat;
1895 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1896 // stereo and let audio flinger do the channel conversion if needed.
1897 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1898 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1899 module->addOutputProfile(address, &outputConfig,
1900 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1901 module->addInputProfile(address, &inputConfig,
1902 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001903
Eric Laurentc722f302014-12-10 11:21:49 -08001904 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001905 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001906 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001907 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001908 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001909 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001910 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001911 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001912 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001913 }
1914 return NO_ERROR;
1915}
1916
1917status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1918{
1919 sp<HwModule> module;
1920 for (size_t i = 0; i < mHwModules.size(); i++) {
1921 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1922 mHwModules[i]->mHandle != 0) {
1923 module = mHwModules[i];
1924 break;
1925 }
1926 }
1927
1928 if (module == 0) {
1929 return INVALID_OPERATION;
1930 }
1931
1932 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1933
1934 for (size_t i = 0; i < mixes.size(); i++) {
1935 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001936
1937 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001938 continue;
1939 }
1940
Eric Laurentc722f302014-12-10 11:21:49 -08001941 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1942 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1943 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001944 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001945 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001946 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001947 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001948
1949 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1950 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1951 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001952 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001953 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001954 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001955 }
1956 module->removeOutputProfile(address);
1957 module->removeInputProfile(address);
1958 }
1959 return NO_ERROR;
1960}
1961
Eric Laurente552edb2014-03-10 17:42:56 -07001962
Eric Laurente0720872014-03-11 09:30:41 -07001963status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001964{
1965 const size_t SIZE = 256;
1966 char buffer[SIZE];
1967 String8 result;
1968
1969 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1970 result.append(buffer);
1971
Eric Laurent87ffa392015-05-22 10:32:38 -07001972 snprintf(buffer, SIZE, " Primary Output: %d\n",
1973 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07001974 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001975 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001976 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001977 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001978 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001979 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001980 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001981 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001982 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001983 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001984 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001985 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001986 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001987 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001988 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001989 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001990 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001991 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001992
François Gaffie53615e22015-03-19 09:24:12 +01001993 mAvailableOutputDevices.dump(fd, String8("output"));
1994 mAvailableInputDevices.dump(fd, String8("input"));
1995 mHwModules.dump(fd);
1996 mOutputs.dump(fd);
1997 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001998 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001999 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01002000 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07002001
2002 return NO_ERROR;
2003}
2004
2005// This function checks for the parameters which can be offloaded.
2006// This can be enhanced depending on the capability of the DSP and policy
2007// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002008bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002009{
2010 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002011 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002012 offloadInfo.sample_rate, offloadInfo.channel_mask,
2013 offloadInfo.format,
2014 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2015 offloadInfo.has_video);
2016
2017 // Check if offload has been disabled
2018 char propValue[PROPERTY_VALUE_MAX];
2019 if (property_get("audio.offload.disable", propValue, "0")) {
2020 if (atoi(propValue) != 0) {
2021 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2022 return false;
2023 }
2024 }
2025
2026 // Check if stream type is music, then only allow offload as of now.
2027 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2028 {
2029 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2030 return false;
2031 }
2032
2033 //TODO: enable audio offloading with video when ready
2034 if (offloadInfo.has_video)
2035 {
2036 ALOGV("isOffloadSupported: has_video == true, returning false");
2037 return false;
2038 }
2039
2040 //If duration is less than minimum value defined in property, return false
2041 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2042 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2043 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2044 return false;
2045 }
2046 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2047 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2048 return false;
2049 }
2050
2051 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2052 // creating an offloaded track and tearing it down immediately after start when audioflinger
2053 // detects there is an active non offloadable effect.
2054 // FIXME: We should check the audio session here but we do not have it in this context.
2055 // This may prevent offloading in rare situations where effects are left active by apps
2056 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002057 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002058 return false;
2059 }
2060
2061 // See if there is a profile to support this.
2062 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002063 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002064 offloadInfo.sample_rate,
2065 offloadInfo.format,
2066 offloadInfo.channel_mask,
2067 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002068 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2069 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002070}
2071
Eric Laurent6a94d692014-05-20 11:18:06 -07002072status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2073 audio_port_type_t type,
2074 unsigned int *num_ports,
2075 struct audio_port *ports,
2076 unsigned int *generation)
2077{
2078 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2079 generation == NULL) {
2080 return BAD_VALUE;
2081 }
2082 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2083 if (ports == NULL) {
2084 *num_ports = 0;
2085 }
2086
2087 size_t portsWritten = 0;
2088 size_t portsMax = *num_ports;
2089 *num_ports = 0;
2090 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2091 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2092 for (size_t i = 0;
2093 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2094 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2095 }
2096 *num_ports += mAvailableOutputDevices.size();
2097 }
2098 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2099 for (size_t i = 0;
2100 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2101 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2102 }
2103 *num_ports += mAvailableInputDevices.size();
2104 }
2105 }
2106 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2107 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2108 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2109 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2110 }
2111 *num_ports += mInputs.size();
2112 }
2113 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002114 size_t numOutputs = 0;
2115 for (size_t i = 0; i < mOutputs.size(); i++) {
2116 if (!mOutputs[i]->isDuplicated()) {
2117 numOutputs++;
2118 if (portsWritten < portsMax) {
2119 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2120 }
2121 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002122 }
Eric Laurent84c70242014-06-23 08:46:27 -07002123 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002124 }
2125 }
2126 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002127 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002128 return NO_ERROR;
2129}
2130
2131status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2132{
2133 return NO_ERROR;
2134}
2135
Eric Laurent6a94d692014-05-20 11:18:06 -07002136status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2137 audio_patch_handle_t *handle,
2138 uid_t uid)
2139{
2140 ALOGV("createAudioPatch()");
2141
2142 if (handle == NULL || patch == NULL) {
2143 return BAD_VALUE;
2144 }
2145 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2146
Eric Laurent874c42872014-08-08 15:13:39 -07002147 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2148 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2149 return BAD_VALUE;
2150 }
2151 // only one source per audio patch supported for now
2152 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002153 return INVALID_OPERATION;
2154 }
Eric Laurent874c42872014-08-08 15:13:39 -07002155
2156 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002157 return INVALID_OPERATION;
2158 }
Eric Laurent874c42872014-08-08 15:13:39 -07002159 for (size_t i = 0; i < patch->num_sinks; i++) {
2160 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2161 return INVALID_OPERATION;
2162 }
2163 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002164
2165 sp<AudioPatch> patchDesc;
2166 ssize_t index = mAudioPatches.indexOfKey(*handle);
2167
Eric Laurent6a94d692014-05-20 11:18:06 -07002168 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2169 patch->sources[0].role,
2170 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002171#if LOG_NDEBUG == 0
2172 for (size_t i = 0; i < patch->num_sinks; i++) {
2173 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2174 patch->sinks[i].role,
2175 patch->sinks[i].type);
2176 }
2177#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002178
2179 if (index >= 0) {
2180 patchDesc = mAudioPatches.valueAt(index);
2181 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2182 mUidCached, patchDesc->mUid, uid);
2183 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2184 return INVALID_OPERATION;
2185 }
2186 } else {
2187 *handle = 0;
2188 }
2189
2190 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002191 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002192 if (outputDesc == NULL) {
2193 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2194 return BAD_VALUE;
2195 }
Eric Laurent84c70242014-06-23 08:46:27 -07002196 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2197 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002198 if (patchDesc != 0) {
2199 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2200 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2201 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2202 return BAD_VALUE;
2203 }
2204 }
Eric Laurent874c42872014-08-08 15:13:39 -07002205 DeviceVector devices;
2206 for (size_t i = 0; i < patch->num_sinks; i++) {
2207 // Only support mix to devices connection
2208 // TODO add support for mix to mix connection
2209 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2210 ALOGV("createAudioPatch() source mix but sink is not a device");
2211 return INVALID_OPERATION;
2212 }
2213 sp<DeviceDescriptor> devDesc =
2214 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2215 if (devDesc == 0) {
2216 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2217 return BAD_VALUE;
2218 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002219
François Gaffie53615e22015-03-19 09:24:12 +01002220 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002221 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002222 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002223 NULL, // updatedSamplingRate
2224 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002225 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002226 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002227 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002228 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002229 ALOGV("createAudioPatch() profile not supported for device %08x",
2230 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002231 return INVALID_OPERATION;
2232 }
2233 devices.add(devDesc);
2234 }
2235 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002236 return INVALID_OPERATION;
2237 }
Eric Laurent874c42872014-08-08 15:13:39 -07002238
Eric Laurent6a94d692014-05-20 11:18:06 -07002239 // TODO: reconfigure output format and channels here
2240 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002241 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002242 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002243 index = mAudioPatches.indexOfKey(*handle);
2244 if (index >= 0) {
2245 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2246 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2247 }
2248 patchDesc = mAudioPatches.valueAt(index);
2249 patchDesc->mUid = uid;
2250 ALOGV("createAudioPatch() success");
2251 } else {
2252 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2253 return INVALID_OPERATION;
2254 }
2255 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2256 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2257 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002258 // only one sink supported when connecting an input device to a mix
2259 if (patch->num_sinks > 1) {
2260 return INVALID_OPERATION;
2261 }
François Gaffie53615e22015-03-19 09:24:12 +01002262 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002263 if (inputDesc == NULL) {
2264 return BAD_VALUE;
2265 }
2266 if (patchDesc != 0) {
2267 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2268 return BAD_VALUE;
2269 }
2270 }
2271 sp<DeviceDescriptor> devDesc =
2272 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2273 if (devDesc == 0) {
2274 return BAD_VALUE;
2275 }
2276
François Gaffie53615e22015-03-19 09:24:12 +01002277 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002278 devDesc->mAddress,
2279 patch->sinks[0].sample_rate,
2280 NULL, /*updatedSampleRate*/
2281 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002282 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002283 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002284 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002285 // FIXME for the parameter type,
2286 // and the NONE
2287 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002288 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002289 return INVALID_OPERATION;
2290 }
2291 // TODO: reconfigure output format and channels here
2292 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002293 devDesc->type(), inputDesc->mIoHandle);
2294 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002295 index = mAudioPatches.indexOfKey(*handle);
2296 if (index >= 0) {
2297 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2298 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2299 }
2300 patchDesc = mAudioPatches.valueAt(index);
2301 patchDesc->mUid = uid;
2302 ALOGV("createAudioPatch() success");
2303 } else {
2304 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2305 return INVALID_OPERATION;
2306 }
2307 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2308 // device to device connection
2309 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002310 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002311 return BAD_VALUE;
2312 }
2313 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002314 sp<DeviceDescriptor> srcDeviceDesc =
2315 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002316 if (srcDeviceDesc == 0) {
2317 return BAD_VALUE;
2318 }
Eric Laurent874c42872014-08-08 15:13:39 -07002319
Eric Laurent6a94d692014-05-20 11:18:06 -07002320 //update source and sink with our own data as the data passed in the patch may
2321 // be incomplete.
2322 struct audio_patch newPatch = *patch;
2323 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002324
Eric Laurent874c42872014-08-08 15:13:39 -07002325 for (size_t i = 0; i < patch->num_sinks; i++) {
2326 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2327 ALOGV("createAudioPatch() source device but one sink is not a device");
2328 return INVALID_OPERATION;
2329 }
2330
2331 sp<DeviceDescriptor> sinkDeviceDesc =
2332 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2333 if (sinkDeviceDesc == 0) {
2334 return BAD_VALUE;
2335 }
2336 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2337
Eric Laurent3bcf8592015-04-03 12:13:24 -07002338 // create a software bridge in PatchPanel if:
2339 // - source and sink devices are on differnt HW modules OR
2340 // - audio HAL version is < 3.0
2341 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2342 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2343 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002344 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002345 return INVALID_OPERATION;
2346 }
Eric Laurent874c42872014-08-08 15:13:39 -07002347 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002348 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002349 // if the sink device is reachable via an opened output stream, request to go via
2350 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002351 audio_io_handle_t output = selectOutput(outputs,
2352 AUDIO_OUTPUT_FLAG_NONE,
2353 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002354 if (output != AUDIO_IO_HANDLE_NONE) {
2355 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2356 if (outputDesc->isDuplicated()) {
2357 return INVALID_OPERATION;
2358 }
2359 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002360 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002361 newPatch.num_sources = 2;
2362 }
Eric Laurent83b88082014-06-20 18:31:16 -07002363 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002364 }
2365 // TODO: check from routing capabilities in config file and other conflicting patches
2366
2367 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2368 if (index >= 0) {
2369 afPatchHandle = patchDesc->mAfPatchHandle;
2370 }
2371
2372 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2373 &afPatchHandle,
2374 0);
2375 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2376 status, afPatchHandle);
2377 if (status == NO_ERROR) {
2378 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002379 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002380 addAudioPatch(patchDesc->mHandle, patchDesc);
2381 } else {
2382 patchDesc->mPatch = newPatch;
2383 }
2384 patchDesc->mAfPatchHandle = afPatchHandle;
2385 *handle = patchDesc->mHandle;
2386 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002387 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002388 } else {
2389 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2390 status);
2391 return INVALID_OPERATION;
2392 }
2393 } else {
2394 return BAD_VALUE;
2395 }
2396 } else {
2397 return BAD_VALUE;
2398 }
2399 return NO_ERROR;
2400}
2401
2402status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2403 uid_t uid)
2404{
2405 ALOGV("releaseAudioPatch() patch %d", handle);
2406
2407 ssize_t index = mAudioPatches.indexOfKey(handle);
2408
2409 if (index < 0) {
2410 return BAD_VALUE;
2411 }
2412 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2413 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2414 mUidCached, patchDesc->mUid, uid);
2415 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2416 return INVALID_OPERATION;
2417 }
2418
2419 struct audio_patch *patch = &patchDesc->mPatch;
2420 patchDesc->mUid = mUidCached;
2421 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002422 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002423 if (outputDesc == NULL) {
2424 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2425 return BAD_VALUE;
2426 }
2427
Eric Laurentc75307b2015-03-17 15:29:32 -07002428 setOutputDevice(outputDesc,
2429 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 true,
2431 0,
2432 NULL);
2433 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2434 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002435 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 if (inputDesc == NULL) {
2437 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2438 return BAD_VALUE;
2439 }
2440 setInputDevice(inputDesc->mIoHandle,
2441 getNewInputDevice(inputDesc->mIoHandle),
2442 true,
2443 NULL);
2444 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2445 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2446 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2447 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2448 status, patchDesc->mAfPatchHandle);
2449 removeAudioPatch(patchDesc->mHandle);
2450 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002451 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002452 } else {
2453 return BAD_VALUE;
2454 }
2455 } else {
2456 return BAD_VALUE;
2457 }
2458 return NO_ERROR;
2459}
2460
2461status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2462 struct audio_patch *patches,
2463 unsigned int *generation)
2464{
François Gaffie53615e22015-03-19 09:24:12 +01002465 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002466 return BAD_VALUE;
2467 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002468 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002469 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002470}
2471
Eric Laurente1715a42014-05-20 11:30:42 -07002472status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002473{
Eric Laurente1715a42014-05-20 11:30:42 -07002474 ALOGV("setAudioPortConfig()");
2475
2476 if (config == NULL) {
2477 return BAD_VALUE;
2478 }
2479 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2480 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002481 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2482 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002483 }
2484
Eric Laurenta121f902014-06-03 13:32:54 -07002485 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002486 if (config->type == AUDIO_PORT_TYPE_MIX) {
2487 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002488 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002489 if (outputDesc == NULL) {
2490 return BAD_VALUE;
2491 }
Eric Laurent84c70242014-06-23 08:46:27 -07002492 ALOG_ASSERT(!outputDesc->isDuplicated(),
2493 "setAudioPortConfig() called on duplicated output %d",
2494 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002495 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002496 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002497 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002498 if (inputDesc == NULL) {
2499 return BAD_VALUE;
2500 }
Eric Laurenta121f902014-06-03 13:32:54 -07002501 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002502 } else {
2503 return BAD_VALUE;
2504 }
2505 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2506 sp<DeviceDescriptor> deviceDesc;
2507 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2508 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2509 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2510 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2511 } else {
2512 return BAD_VALUE;
2513 }
2514 if (deviceDesc == NULL) {
2515 return BAD_VALUE;
2516 }
Eric Laurenta121f902014-06-03 13:32:54 -07002517 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002518 } else {
2519 return BAD_VALUE;
2520 }
2521
Eric Laurenta121f902014-06-03 13:32:54 -07002522 struct audio_port_config backupConfig;
2523 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2524 if (status == NO_ERROR) {
2525 struct audio_port_config newConfig;
2526 audioPortConfig->toAudioPortConfig(&newConfig, config);
2527 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002528 }
Eric Laurenta121f902014-06-03 13:32:54 -07002529 if (status != NO_ERROR) {
2530 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002531 }
Eric Laurente1715a42014-05-20 11:30:42 -07002532
2533 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002534}
2535
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002536void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2537{
2538 clearAudioPatches(uid);
2539 clearSessionRoutes(uid);
2540}
2541
Eric Laurent6a94d692014-05-20 11:18:06 -07002542void AudioPolicyManager::clearAudioPatches(uid_t uid)
2543{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002544 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002545 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2546 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002547 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002548 }
2549 }
2550}
2551
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002552
2553void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2554 audio_io_handle_t ouptutToSkip)
2555{
2556 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2557 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2558 for (size_t j = 0; j < mOutputs.size(); j++) {
2559 if (mOutputs.keyAt(j) == ouptutToSkip) {
2560 continue;
2561 }
2562 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2563 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2564 continue;
2565 }
2566 // If the default device for this strategy is on another output mix,
2567 // invalidate all tracks in this strategy to force re connection.
2568 // Otherwise select new device on the output mix.
2569 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2570 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2571 if (stream == AUDIO_STREAM_PATCH) {
2572 continue;
2573 }
2574 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2575 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2576 }
2577 }
2578 } else {
2579 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2580 setOutputDevice(outputDesc, newDevice, false);
2581 }
2582 }
2583}
2584
2585void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2586{
2587 // remove output routes associated with this uid
2588 SortedVector<routing_strategy> affectedStrategies;
2589 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2590 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2591 if (route->mUid == uid) {
2592 mOutputRoutes.removeItemsAt(i);
2593 if (route->mDeviceDescriptor != 0) {
2594 affectedStrategies.add(getStrategy(route->mStreamType));
2595 }
2596 }
2597 }
2598 // reroute outputs if necessary
2599 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2600 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2601 }
2602
2603 // remove input routes associated with this uid
2604 SortedVector<audio_source_t> affectedSources;
2605 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2606 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2607 if (route->mUid == uid) {
2608 mInputRoutes.removeItemsAt(i);
2609 if (route->mDeviceDescriptor != 0) {
2610 affectedSources.add(route->mSource);
2611 }
2612 }
2613 }
2614 // reroute inputs if necessary
2615 SortedVector<audio_io_handle_t> inputsToClose;
2616 for (size_t i = 0; i < mInputs.size(); i++) {
2617 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2618 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2619 inputsToClose.add(inputDesc->mIoHandle);
2620 }
2621 }
2622 for (size_t i = 0; i < inputsToClose.size(); i++) {
2623 closeInput(inputsToClose[i]);
2624 }
2625}
2626
2627
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002628status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2629 audio_io_handle_t *ioHandle,
2630 audio_devices_t *device)
2631{
2632 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2633 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002634 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002635
François Gaffiedf372692015-03-19 10:43:27 +01002636 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002637}
2638
Eric Laurent493404d2015-04-21 15:07:36 -07002639status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2640 const audio_attributes_t *attributes __unused,
2641 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002642{
2643 return INVALID_OPERATION;
2644}
2645
Eric Laurent493404d2015-04-21 15:07:36 -07002646status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002647{
2648 return INVALID_OPERATION;
2649}
2650
Eric Laurente552edb2014-03-10 17:42:56 -07002651// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002652// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002653// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002654uint32_t AudioPolicyManager::nextAudioPortGeneration()
2655{
2656 return android_atomic_inc(&mAudioPortGeneration);
2657}
2658
Eric Laurente0720872014-03-11 09:30:41 -07002659AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002660 :
2661#ifdef AUDIO_POLICY_TEST
2662 Thread(false),
2663#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002664 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002665 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002666 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002667 mAudioPortGeneration(1),
2668 mBeaconMuteRefCount(0),
2669 mBeaconPlayingRefCount(0),
2670 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002671{
François Gaffie2110e042015-03-24 08:41:51 +01002672 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2673 if (!engineInstance) {
2674 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2675 return;
2676 }
2677 // Retrieve the Policy Manager Interface
2678 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2679 if (mEngine == NULL) {
2680 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2681 return;
2682 }
2683 mEngine->setObserver(this);
2684 status_t status = mEngine->initCheck();
2685 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2686
Eric Laurent6a94d692014-05-20 11:18:06 -07002687 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002688 mpClientInterface = clientInterface;
2689
Eric Laurent7288ab82015-05-06 18:44:02 -07002690 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002691 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2692 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2693 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2694 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2695 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2696 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002697 ALOGE("could not load audio policy configuration file, setting defaults");
2698 defaultAudioPolicyConfig();
2699 }
2700 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002701 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002702
François Gaffie2110e042015-03-24 08:41:51 +01002703 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2704 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002705
2706 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002707 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2708 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002709 for (size_t i = 0; i < mHwModules.size(); i++) {
2710 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2711 if (mHwModules[i]->mHandle == 0) {
2712 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2713 continue;
2714 }
2715 // open all output streams needed to access attached devices
2716 // except for direct output streams that are only opened when they are actually
2717 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002718 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002719 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2720 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002721 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002722
Eric Laurent3a4311c2014-03-17 12:00:47 -07002723 if (outProfile->mSupportedDevices.isEmpty()) {
2724 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2725 continue;
2726 }
2727
Eric Laurentd78f1532014-09-16 16:38:20 -07002728 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2729 continue;
2730 }
Eric Laurent83b88082014-06-20 18:31:16 -07002731 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002732 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2733 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002734 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002735 // chose first device present in mSupportedDevices also part of
2736 // outputDeviceTypes
2737 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002738 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002739 if ((profileType & outputDeviceTypes) != 0) {
2740 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002741 }
Eric Laurente552edb2014-03-10 17:42:56 -07002742 }
2743 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002744 if ((profileType & outputDeviceTypes) == 0) {
2745 continue;
2746 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002747 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2748 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002749
2750 outputDesc->mDevice = profileType;
2751 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2752 config.sample_rate = outputDesc->mSamplingRate;
2753 config.channel_mask = outputDesc->mChannelMask;
2754 config.format = outputDesc->mFormat;
2755 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002756 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002757 &output,
2758 &config,
2759 &outputDesc->mDevice,
2760 String8(""),
2761 &outputDesc->mLatency,
2762 outputDesc->mFlags);
2763
2764 if (status != NO_ERROR) {
2765 ALOGW("Cannot open output stream for device %08x on hw module %s",
2766 outputDesc->mDevice,
2767 mHwModules[i]->mName);
2768 } else {
2769 outputDesc->mSamplingRate = config.sample_rate;
2770 outputDesc->mChannelMask = config.channel_mask;
2771 outputDesc->mFormat = config.format;
2772
2773 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002774 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002775 ssize_t index =
2776 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2777 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002778 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2779 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002780 }
2781 }
2782 if (mPrimaryOutput == 0 &&
2783 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002784 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002785 }
2786 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002787 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002788 outputDesc->mDevice,
2789 true);
2790 }
Eric Laurente552edb2014-03-10 17:42:56 -07002791 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002792 // open input streams needed to access attached devices to validate
2793 // mAvailableInputDevices list
2794 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2795 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002796 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002797
Eric Laurent3a4311c2014-03-17 12:00:47 -07002798 if (inProfile->mSupportedDevices.isEmpty()) {
2799 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2800 continue;
2801 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002802 // chose first device present in mSupportedDevices also part of
2803 // inputDeviceTypes
2804 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2805 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002806 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002807 if (profileType & inputDeviceTypes) {
2808 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002809 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002810 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002811 if ((profileType & inputDeviceTypes) == 0) {
2812 continue;
2813 }
2814 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2815
2816 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2817 inputDesc->mDevice = profileType;
2818
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002819 // find the address
2820 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2821 // the inputs vector must be of size 1, but we don't want to crash here
2822 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2823 : String8("");
2824 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2825 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2826
Eric Laurentd78f1532014-09-16 16:38:20 -07002827 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2828 config.sample_rate = inputDesc->mSamplingRate;
2829 config.channel_mask = inputDesc->mChannelMask;
2830 config.format = inputDesc->mFormat;
2831 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002832 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002833 &input,
2834 &config,
2835 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002836 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002837 AUDIO_SOURCE_MIC,
2838 AUDIO_INPUT_FLAG_NONE);
2839
2840 if (status == NO_ERROR) {
2841 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002842 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002843 ssize_t index =
2844 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2845 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002846 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2847 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002848 }
2849 }
2850 mpClientInterface->closeInput(input);
2851 } else {
2852 ALOGW("Cannot open input stream for device %08x on hw module %s",
2853 inputDesc->mDevice,
2854 mHwModules[i]->mName);
2855 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002856 }
2857 }
2858 // make sure all attached devices have been allocated a unique ID
2859 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002860 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002861 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002862 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2863 continue;
2864 }
François Gaffie2110e042015-03-24 08:41:51 +01002865 // The device is now validated and can be appended to the available devices of the engine
2866 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2867 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002868 i++;
2869 }
2870 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002871 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002872 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002873 mAvailableInputDevices.remove(mAvailableInputDevices[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(mAvailableInputDevices[i],
2878 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002879 i++;
2880 }
2881 // make sure default device is reachable
2882 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002883 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002884 }
Eric Laurente552edb2014-03-10 17:42:56 -07002885
2886 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2887
2888 updateDevicesAndOutputs();
2889
2890#ifdef AUDIO_POLICY_TEST
2891 if (mPrimaryOutput != 0) {
2892 AudioParameter outputCmd = AudioParameter();
2893 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002894 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002895
2896 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2897 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002898 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2899 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002900 mTestLatencyMs = 0;
2901 mCurOutput = 0;
2902 mDirectOutput = false;
2903 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2904 mTestOutputs[i] = 0;
2905 }
2906
2907 const size_t SIZE = 256;
2908 char buffer[SIZE];
2909 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2910 run(buffer, ANDROID_PRIORITY_AUDIO);
2911 }
2912#endif //AUDIO_POLICY_TEST
2913}
2914
Eric Laurente0720872014-03-11 09:30:41 -07002915AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002916{
2917#ifdef AUDIO_POLICY_TEST
2918 exit();
2919#endif //AUDIO_POLICY_TEST
2920 for (size_t i = 0; i < mOutputs.size(); i++) {
2921 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002922 }
2923 for (size_t i = 0; i < mInputs.size(); i++) {
2924 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002925 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002926 mAvailableOutputDevices.clear();
2927 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002928 mOutputs.clear();
2929 mInputs.clear();
2930 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002931}
2932
Eric Laurente0720872014-03-11 09:30:41 -07002933status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002934{
Eric Laurent87ffa392015-05-22 10:32:38 -07002935 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002936}
2937
2938#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002939bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002940{
2941 ALOGV("entering threadLoop()");
2942 while (!exitPending())
2943 {
2944 String8 command;
2945 int valueInt;
2946 String8 value;
2947
2948 Mutex::Autolock _l(mLock);
2949 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2950
2951 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2952 AudioParameter param = AudioParameter(command);
2953
2954 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2955 valueInt != 0) {
2956 ALOGV("Test command %s received", command.string());
2957 String8 target;
2958 if (param.get(String8("target"), target) != NO_ERROR) {
2959 target = "Manager";
2960 }
2961 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2962 param.remove(String8("test_cmd_policy_output"));
2963 mCurOutput = valueInt;
2964 }
2965 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2966 param.remove(String8("test_cmd_policy_direct"));
2967 if (value == "false") {
2968 mDirectOutput = false;
2969 } else if (value == "true") {
2970 mDirectOutput = true;
2971 }
2972 }
2973 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2974 param.remove(String8("test_cmd_policy_input"));
2975 mTestInput = valueInt;
2976 }
2977
2978 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2979 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002980 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002981 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002982 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002983 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002984 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002985 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002986 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002987 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002988 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002989 if (target == "Manager") {
2990 mTestFormat = format;
2991 } else if (mTestOutputs[mCurOutput] != 0) {
2992 AudioParameter outputParam = AudioParameter();
2993 outputParam.addInt(String8("format"), format);
2994 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2995 }
2996 }
2997 }
2998 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2999 param.remove(String8("test_cmd_policy_channels"));
3000 int channels = 0;
3001
3002 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003003 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003004 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003005 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003006 }
3007 if (channels != 0) {
3008 if (target == "Manager") {
3009 mTestChannels = channels;
3010 } else if (mTestOutputs[mCurOutput] != 0) {
3011 AudioParameter outputParam = AudioParameter();
3012 outputParam.addInt(String8("channels"), channels);
3013 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3014 }
3015 }
3016 }
3017 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3018 param.remove(String8("test_cmd_policy_sampleRate"));
3019 if (valueInt >= 0 && valueInt <= 96000) {
3020 int samplingRate = valueInt;
3021 if (target == "Manager") {
3022 mTestSamplingRate = samplingRate;
3023 } else if (mTestOutputs[mCurOutput] != 0) {
3024 AudioParameter outputParam = AudioParameter();
3025 outputParam.addInt(String8("sampling_rate"), samplingRate);
3026 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3027 }
3028 }
3029 }
3030
3031 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3032 param.remove(String8("test_cmd_policy_reopen"));
3033
Eric Laurentc75307b2015-03-17 15:29:32 -07003034 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003035
Eric Laurentc75307b2015-03-17 15:29:32 -07003036 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003037
Eric Laurentc75307b2015-03-17 15:29:32 -07003038 removeOutput(mPrimaryOutput->mIoHandle);
3039 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3040 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003041 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003042 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3043 config.sample_rate = outputDesc->mSamplingRate;
3044 config.channel_mask = outputDesc->mChannelMask;
3045 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003046 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003047 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003048 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003049 &config,
3050 &outputDesc->mDevice,
3051 String8(""),
3052 &outputDesc->mLatency,
3053 outputDesc->mFlags);
3054 if (status != NO_ERROR) {
3055 ALOGE("Failed to reopen hardware output stream, "
3056 "samplingRate: %d, format %d, channels %d",
3057 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003058 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003059 outputDesc->mSamplingRate = config.sample_rate;
3060 outputDesc->mChannelMask = config.channel_mask;
3061 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003062 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003063 AudioParameter outputCmd = AudioParameter();
3064 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003065 mpClientInterface->setParameters(handle, outputCmd.toString());
3066 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003067 }
3068 }
3069
3070
3071 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3072 }
3073 }
3074 return false;
3075}
3076
Eric Laurente0720872014-03-11 09:30:41 -07003077void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003078{
3079 {
3080 AutoMutex _l(mLock);
3081 requestExit();
3082 mWaitWorkCV.signal();
3083 }
3084 requestExitAndWait();
3085}
3086
Eric Laurente0720872014-03-11 09:30:41 -07003087int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003088{
3089 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3090 if (output == mTestOutputs[i]) return i;
3091 }
3092 return 0;
3093}
3094#endif //AUDIO_POLICY_TEST
3095
3096// ---
3097
Eric Laurentc75307b2015-03-17 15:29:32 -07003098void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003099{
François Gaffie98cc1912015-03-18 17:52:40 +01003100 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003101 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003102 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003103}
3104
François Gaffie53615e22015-03-19 09:24:12 +01003105void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3106{
3107 mOutputs.removeItem(output);
3108}
3109
Eric Laurent1f2f2232014-06-02 12:01:23 -07003110void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003111{
François Gaffie98cc1912015-03-18 17:52:40 +01003112 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003113 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003114 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003115}
Eric Laurente552edb2014-03-10 17:42:56 -07003116
Eric Laurentc75307b2015-03-17 15:29:32 -07003117void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003118 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003119 const String8 address /*in*/,
3120 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003121 sp<DeviceDescriptor> devDesc =
3122 desc->mProfile->mSupportedDevices.getDevice(device, address);
3123 if (devDesc != 0) {
3124 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3125 desc->mIoHandle, address.string());
3126 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003127 }
3128}
3129
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003130status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003131 audio_policy_dev_state_t state,
3132 SortedVector<audio_io_handle_t>& outputs,
3133 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003134{
François Gaffie53615e22015-03-19 09:24:12 +01003135 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003136 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003137 // erase all current sample rates, formats and channel masks
3138 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003139
Eric Laurent3b73df72014-03-11 09:06:29 -07003140 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003141 // first list already open outputs that can be routed to this device
3142 for (size_t i = 0; i < mOutputs.size(); i++) {
3143 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003144 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003145 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003146 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3147 outputs.add(mOutputs.keyAt(i));
3148 } else {
3149 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003150 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003151 }
Eric Laurente552edb2014-03-10 17:42:56 -07003152 }
3153 }
3154 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003155 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003156 for (size_t i = 0; i < mHwModules.size(); i++)
3157 {
3158 if (mHwModules[i]->mHandle == 0) {
3159 continue;
3160 }
3161 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3162 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003163 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3164 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003165 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003166 address == profile->mSupportedDevices[0]->mAddress) {
3167 profiles.add(profile);
3168 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3169 }
Eric Laurente552edb2014-03-10 17:42:56 -07003170 }
3171 }
3172 }
3173
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003174 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3175
Eric Laurente552edb2014-03-10 17:42:56 -07003176 if (profiles.isEmpty() && outputs.isEmpty()) {
3177 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3178 return BAD_VALUE;
3179 }
3180
3181 // open outputs for matching profiles if needed. Direct outputs are also opened to
3182 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3183 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003184 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003185
3186 // nothing to do if one output is already opened for this profile
3187 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003188 for (j = 0; j < outputs.size(); j++) {
3189 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003190 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003191 // matching profile: save the sample rates, format and channel masks supported
3192 // by the profile in our device descriptor
3193 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003194 break;
3195 }
3196 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003197 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003198 continue;
3199 }
3200
Eric Laurent83b88082014-06-20 18:31:16 -07003201 ALOGV("opening output for device %08x with params %s profile %p",
3202 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003203 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003204 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003205 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3206 config.sample_rate = desc->mSamplingRate;
3207 config.channel_mask = desc->mChannelMask;
3208 config.format = desc->mFormat;
3209 config.offload_info.sample_rate = desc->mSamplingRate;
3210 config.offload_info.channel_mask = desc->mChannelMask;
3211 config.offload_info.format = desc->mFormat;
3212 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003213 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003214 &output,
3215 &config,
3216 &desc->mDevice,
3217 address,
3218 &desc->mLatency,
3219 desc->mFlags);
3220 if (status == NO_ERROR) {
3221 desc->mSamplingRate = config.sample_rate;
3222 desc->mChannelMask = config.channel_mask;
3223 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003224
Eric Laurentd4692962014-05-05 18:13:44 -07003225 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003226 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003227 char *param = audio_device_address_to_parameter(device, address);
3228 mpClientInterface->setParameters(output, String8(param));
3229 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003230 }
3231
Eric Laurentd4692962014-05-05 18:13:44 -07003232 // Here is where we step through and resolve any "dynamic" fields
3233 String8 reply;
3234 char *value;
3235 if (profile->mSamplingRates[0] == 0) {
3236 reply = mpClientInterface->getParameters(output,
3237 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003238 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003239 reply.string());
3240 value = strpbrk((char *)reply.string(), "=");
3241 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003242 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003243 }
Eric Laurentd4692962014-05-05 18:13:44 -07003244 }
3245 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3246 reply = mpClientInterface->getParameters(output,
3247 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003248 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003249 reply.string());
3250 value = strpbrk((char *)reply.string(), "=");
3251 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003252 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003253 }
Eric Laurentd4692962014-05-05 18:13:44 -07003254 }
3255 if (profile->mChannelMasks[0] == 0) {
3256 reply = mpClientInterface->getParameters(output,
3257 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003258 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003259 reply.string());
3260 value = strpbrk((char *)reply.string(), "=");
3261 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003262 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003263 }
Eric Laurentd4692962014-05-05 18:13:44 -07003264 }
3265 if (((profile->mSamplingRates[0] == 0) &&
3266 (profile->mSamplingRates.size() < 2)) ||
3267 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3268 (profile->mFormats.size() < 2)) ||
3269 ((profile->mChannelMasks[0] == 0) &&
3270 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003271 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003272 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003273 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003274 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3275 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003276 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003277 config.sample_rate = profile->pickSamplingRate();
3278 config.channel_mask = profile->pickChannelMask();
3279 config.format = profile->pickFormat();
3280 config.offload_info.sample_rate = config.sample_rate;
3281 config.offload_info.channel_mask = config.channel_mask;
3282 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003283 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003284 &output,
3285 &config,
3286 &desc->mDevice,
3287 address,
3288 &desc->mLatency,
3289 desc->mFlags);
3290 if (status == NO_ERROR) {
3291 desc->mSamplingRate = config.sample_rate;
3292 desc->mChannelMask = config.channel_mask;
3293 desc->mFormat = config.format;
3294 } else {
3295 output = AUDIO_IO_HANDLE_NONE;
3296 }
Eric Laurentd4692962014-05-05 18:13:44 -07003297 }
3298
Eric Laurentcf2c0212014-07-25 16:20:43 -07003299 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003300 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003301 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003302 sp<AudioPolicyMix> policyMix;
3303 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003304 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3305 address.string());
3306 }
François Gaffie036e1e92015-03-19 10:16:24 +01003307 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003308 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003309
Eric Laurent87ffa392015-05-22 10:32:38 -07003310 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3311 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003312 // no duplicated output for direct outputs and
3313 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003314 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003315
Eric Laurentd4692962014-05-05 18:13:44 -07003316 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003317 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003318
Eric Laurentd4692962014-05-05 18:13:44 -07003319 //TODO: configure audio effect output stage here
3320
3321 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003322 duplicatedOutput =
3323 mpClientInterface->openDuplicateOutput(output,
3324 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003325 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003326 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003327 sp<SwAudioOutputDescriptor> dupOutputDesc =
3328 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3329 dupOutputDesc->mOutput1 = mPrimaryOutput;
3330 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003331 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3332 dupOutputDesc->mFormat = desc->mFormat;
3333 dupOutputDesc->mChannelMask = desc->mChannelMask;
3334 dupOutputDesc->mLatency = desc->mLatency;
3335 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003336 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003337 } else {
3338 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003339 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003340 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003341 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003342 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003343 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003344 }
Eric Laurente552edb2014-03-10 17:42:56 -07003345 }
3346 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003347 } else {
3348 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003349 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003350 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003351 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003352 profiles.removeAt(profile_index);
3353 profile_index--;
3354 } else {
3355 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003356 devDesc->importAudioPort(profile);
3357
François Gaffie53615e22015-03-19 09:24:12 +01003358 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003359 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3360 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003361 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003362 NULL/*patch handle*/, address.string());
3363 }
Eric Laurente552edb2014-03-10 17:42:56 -07003364 ALOGV("checkOutputsForDevice(): adding output %d", output);
3365 }
3366 }
3367
3368 if (profiles.isEmpty()) {
3369 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3370 return BAD_VALUE;
3371 }
Eric Laurentd4692962014-05-05 18:13:44 -07003372 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003373 // check if one opened output is not needed any more after disconnecting one device
3374 for (size_t i = 0; i < mOutputs.size(); i++) {
3375 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003376 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003377 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003378 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003379 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003380 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003381 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003382 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3383 mOutputs.keyAt(i));
3384 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003385 }
Eric Laurente552edb2014-03-10 17:42:56 -07003386 }
3387 }
Eric Laurentd4692962014-05-05 18:13:44 -07003388 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003389 for (size_t i = 0; i < mHwModules.size(); i++)
3390 {
3391 if (mHwModules[i]->mHandle == 0) {
3392 continue;
3393 }
3394 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3395 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003396 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003397 if (profile->mSupportedDevices.types() & device) {
3398 ALOGV("checkOutputsForDevice(): "
3399 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003400 if (profile->mSamplingRates[0] == 0) {
3401 profile->mSamplingRates.clear();
3402 profile->mSamplingRates.add(0);
3403 }
3404 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3405 profile->mFormats.clear();
3406 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3407 }
3408 if (profile->mChannelMasks[0] == 0) {
3409 profile->mChannelMasks.clear();
3410 profile->mChannelMasks.add(0);
3411 }
3412 }
3413 }
3414 }
3415 }
3416 return NO_ERROR;
3417}
3418
Eric Laurentd4692962014-05-05 18:13:44 -07003419status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003420 audio_policy_dev_state_t state,
3421 SortedVector<audio_io_handle_t>& inputs,
3422 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003423{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003424 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003425 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3426 // first list already open inputs that can be routed to this device
3427 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3428 desc = mInputs.valueAt(input_index);
3429 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3430 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3431 inputs.add(mInputs.keyAt(input_index));
3432 }
3433 }
3434
3435 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003436 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003437 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3438 {
3439 if (mHwModules[module_idx]->mHandle == 0) {
3440 continue;
3441 }
3442 for (size_t profile_index = 0;
3443 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3444 profile_index++)
3445 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003446 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3447
3448 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003449 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003450 address == profile->mSupportedDevices[0]->mAddress) {
3451 profiles.add(profile);
3452 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3453 profile_index, module_idx);
3454 }
Eric Laurentd4692962014-05-05 18:13:44 -07003455 }
3456 }
3457 }
3458
3459 if (profiles.isEmpty() && inputs.isEmpty()) {
3460 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3461 return BAD_VALUE;
3462 }
3463
3464 // open inputs for matching profiles if needed. Direct inputs are also opened to
3465 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3466 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3467
Eric Laurent1c333e22014-05-20 10:48:17 -07003468 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003469 // nothing to do if one input is already opened for this profile
3470 size_t input_index;
3471 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3472 desc = mInputs.valueAt(input_index);
3473 if (desc->mProfile == profile) {
3474 break;
3475 }
3476 }
3477 if (input_index != mInputs.size()) {
3478 continue;
3479 }
3480
3481 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3482 desc = new AudioInputDescriptor(profile);
3483 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003484 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3485 config.sample_rate = desc->mSamplingRate;
3486 config.channel_mask = desc->mChannelMask;
3487 config.format = desc->mFormat;
3488 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003489 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003490 &input,
3491 &config,
3492 &desc->mDevice,
3493 address,
3494 AUDIO_SOURCE_MIC,
3495 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003496
Eric Laurentcf2c0212014-07-25 16:20:43 -07003497 if (status == NO_ERROR) {
3498 desc->mSamplingRate = config.sample_rate;
3499 desc->mChannelMask = config.channel_mask;
3500 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003501
Eric Laurentd4692962014-05-05 18:13:44 -07003502 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003503 char *param = audio_device_address_to_parameter(device, address);
3504 mpClientInterface->setParameters(input, String8(param));
3505 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003506 }
3507
3508 // Here is where we step through and resolve any "dynamic" fields
3509 String8 reply;
3510 char *value;
3511 if (profile->mSamplingRates[0] == 0) {
3512 reply = mpClientInterface->getParameters(input,
3513 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3514 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3515 reply.string());
3516 value = strpbrk((char *)reply.string(), "=");
3517 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003518 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003519 }
3520 }
3521 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3522 reply = mpClientInterface->getParameters(input,
3523 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3524 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3525 value = strpbrk((char *)reply.string(), "=");
3526 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003527 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003528 }
3529 }
3530 if (profile->mChannelMasks[0] == 0) {
3531 reply = mpClientInterface->getParameters(input,
3532 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3533 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3534 reply.string());
3535 value = strpbrk((char *)reply.string(), "=");
3536 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003537 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003538 }
3539 }
3540 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3541 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3542 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3543 ALOGW("checkInputsForDevice() direct input missing param");
3544 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003545 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003546 }
3547
3548 if (input != 0) {
3549 addInput(input, desc);
3550 }
3551 } // endif input != 0
3552
Eric Laurentcf2c0212014-07-25 16:20:43 -07003553 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003554 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003555 profiles.removeAt(profile_index);
3556 profile_index--;
3557 } else {
3558 inputs.add(input);
3559 ALOGV("checkInputsForDevice(): adding input %d", input);
3560 }
3561 } // end scan profiles
3562
3563 if (profiles.isEmpty()) {
3564 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3565 return BAD_VALUE;
3566 }
3567 } else {
3568 // Disconnect
3569 // check if one opened input is not needed any more after disconnecting one device
3570 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3571 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003572 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3573 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003574 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3575 mInputs.keyAt(input_index));
3576 inputs.add(mInputs.keyAt(input_index));
3577 }
3578 }
3579 // Clear any profiles associated with the disconnected device.
3580 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3581 if (mHwModules[module_index]->mHandle == 0) {
3582 continue;
3583 }
3584 for (size_t profile_index = 0;
3585 profile_index < mHwModules[module_index]->mInputProfiles.size();
3586 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003587 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003588 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003589 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003590 profile_index, module_index);
3591 if (profile->mSamplingRates[0] == 0) {
3592 profile->mSamplingRates.clear();
3593 profile->mSamplingRates.add(0);
3594 }
3595 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3596 profile->mFormats.clear();
3597 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3598 }
3599 if (profile->mChannelMasks[0] == 0) {
3600 profile->mChannelMasks.clear();
3601 profile->mChannelMasks.add(0);
3602 }
3603 }
3604 }
3605 }
3606 } // end disconnect
3607
3608 return NO_ERROR;
3609}
3610
3611
Eric Laurente0720872014-03-11 09:30:41 -07003612void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003613{
3614 ALOGV("closeOutput(%d)", output);
3615
Eric Laurentc75307b2015-03-17 15:29:32 -07003616 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003617 if (outputDesc == NULL) {
3618 ALOGW("closeOutput() unknown output %d", output);
3619 return;
3620 }
François Gaffie036e1e92015-03-19 10:16:24 +01003621 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003622
Eric Laurente552edb2014-03-10 17:42:56 -07003623 // look for duplicated outputs connected to the output being removed.
3624 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003625 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003626 if (dupOutputDesc->isDuplicated() &&
3627 (dupOutputDesc->mOutput1 == outputDesc ||
3628 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003629 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003630 if (dupOutputDesc->mOutput1 == outputDesc) {
3631 outputDesc2 = dupOutputDesc->mOutput2;
3632 } else {
3633 outputDesc2 = dupOutputDesc->mOutput1;
3634 }
3635 // As all active tracks on duplicated output will be deleted,
3636 // and as they were also referenced on the other output, the reference
3637 // count for their stream type must be adjusted accordingly on
3638 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003639 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003640 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003641 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003642 }
3643 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3644 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3645
3646 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003647 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003648 }
3649 }
3650
Eric Laurent05b90f82014-08-27 15:32:29 -07003651 nextAudioPortGeneration();
3652
3653 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3654 if (index >= 0) {
3655 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3656 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3657 mAudioPatches.removeItemsAt(index);
3658 mpClientInterface->onAudioPatchListUpdate();
3659 }
3660
Eric Laurente552edb2014-03-10 17:42:56 -07003661 AudioParameter param;
3662 param.add(String8("closing"), String8("true"));
3663 mpClientInterface->setParameters(output, param.toString());
3664
3665 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003666 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003667 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003668}
3669
3670void AudioPolicyManager::closeInput(audio_io_handle_t input)
3671{
3672 ALOGV("closeInput(%d)", input);
3673
3674 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3675 if (inputDesc == NULL) {
3676 ALOGW("closeInput() unknown input %d", input);
3677 return;
3678 }
3679
Eric Laurent6a94d692014-05-20 11:18:06 -07003680 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003681
3682 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3683 if (index >= 0) {
3684 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3685 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3686 mAudioPatches.removeItemsAt(index);
3687 mpClientInterface->onAudioPatchListUpdate();
3688 }
3689
3690 mpClientInterface->closeInput(input);
3691 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003692}
3693
Eric Laurentc75307b2015-03-17 15:29:32 -07003694SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3695 audio_devices_t device,
3696 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003697{
3698 SortedVector<audio_io_handle_t> outputs;
3699
3700 ALOGVV("getOutputsForDevice() device %04x", device);
3701 for (size_t i = 0; i < openOutputs.size(); i++) {
3702 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003703 i, openOutputs.valueAt(i)->isDuplicated(),
3704 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003705 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3706 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3707 outputs.add(openOutputs.keyAt(i));
3708 }
3709 }
3710 return outputs;
3711}
3712
Eric Laurente0720872014-03-11 09:30:41 -07003713bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003714 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003715{
3716 if (outputs1.size() != outputs2.size()) {
3717 return false;
3718 }
3719 for (size_t i = 0; i < outputs1.size(); i++) {
3720 if (outputs1[i] != outputs2[i]) {
3721 return false;
3722 }
3723 }
3724 return true;
3725}
3726
Eric Laurente0720872014-03-11 09:30:41 -07003727void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003728{
3729 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3730 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3731 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3732 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3733
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003734 // also take into account external policy-related changes: add all outputs which are
3735 // associated with policies in the "before" and "after" output vectors
3736 ALOGVV("checkOutputForStrategy(): policy related outputs");
3737 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003738 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003739 if (desc != 0 && desc->mPolicyMix != NULL) {
3740 srcOutputs.add(desc->mIoHandle);
3741 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3742 }
3743 }
3744 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003745 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003746 if (desc != 0 && desc->mPolicyMix != NULL) {
3747 dstOutputs.add(desc->mIoHandle);
3748 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3749 }
3750 }
3751
Eric Laurente552edb2014-03-10 17:42:56 -07003752 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3753 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3754 strategy, srcOutputs[0], dstOutputs[0]);
3755 // mute strategy while moving tracks from one output to another
3756 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003757 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003758 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003759 setStrategyMute(strategy, true, desc);
3760 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003761 }
3762 }
3763
3764 // Move effects associated to this strategy from previous output to new output
3765 if (strategy == STRATEGY_MEDIA) {
3766 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3767 SortedVector<audio_io_handle_t> moved;
3768 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003769 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3770 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3771 effectDesc->mIo != fxOutput) {
3772 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003773 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3774 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003775 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003776 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003777 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003778 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003779 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003780 }
3781 }
3782 }
3783 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003784 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003785 if (i == AUDIO_STREAM_PATCH) {
3786 continue;
3787 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003788 if (getStrategy((audio_stream_type_t)i) == strategy) {
3789 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003790 }
3791 }
3792 }
3793}
3794
Eric Laurente0720872014-03-11 09:30:41 -07003795void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003796{
François Gaffie2110e042015-03-24 08:41:51 +01003797 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003798 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003799 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003800 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003801 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003802 checkOutputForStrategy(STRATEGY_SONIFICATION);
3803 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003804 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003805 checkOutputForStrategy(STRATEGY_MEDIA);
3806 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003807 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003808}
3809
Eric Laurente0720872014-03-11 09:30:41 -07003810void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003811{
François Gaffie53615e22015-03-19 09:24:12 +01003812 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003813 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003814 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003815 return;
3816 }
3817
Eric Laurent3a4311c2014-03-17 12:00:47 -07003818 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003819 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3820 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3821 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003822 // suspend A2DP output if:
3823 // (NOT already suspended) &&
3824 // ((SCO device is connected &&
3825 // (forced usage for communication || for record is SCO))) ||
3826 // (phone state is ringing || in call)
3827 //
3828 // restore A2DP output if:
3829 // (Already suspended) &&
3830 // ((SCO device is NOT connected ||
3831 // (forced usage NOT for communication && NOT for record is SCO))) &&
3832 // (phone state is NOT ringing && NOT in call)
3833 //
3834 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003835 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003836 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3837 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3838 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3839 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003840
3841 mpClientInterface->restoreOutput(a2dpOutput);
3842 mA2dpSuspended = false;
3843 }
3844 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003845 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003846 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3847 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3848 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3849 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003850
3851 mpClientInterface->suspendOutput(a2dpOutput);
3852 mA2dpSuspended = true;
3853 }
3854 }
3855}
3856
Eric Laurentc75307b2015-03-17 15:29:32 -07003857audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3858 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003859{
3860 audio_devices_t device = AUDIO_DEVICE_NONE;
3861
Eric Laurent6a94d692014-05-20 11:18:06 -07003862 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3863 if (index >= 0) {
3864 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3865 if (patchDesc->mUid != mUidCached) {
3866 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3867 outputDesc->device(), outputDesc->mPatchHandle);
3868 return outputDesc->device();
3869 }
3870 }
3871
Eric Laurente552edb2014-03-10 17:42:56 -07003872 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003873 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003874 // use device for strategy enforced audible
3875 // 2: we are in call or the strategy phone is active on the output:
3876 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003877 // 3: the strategy for enforced audible is active but not enforced on the output:
3878 // use the device for strategy enforced audible
3879 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003880 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003881 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003882 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003883 // 6: the strategy accessibility is active on the output:
3884 // use device for strategy accessibility
3885 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003886 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003887 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003888 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003889 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003890 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003891 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003892 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003893 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3894 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003895 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003896 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003897 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003898 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003899 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003900 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003901 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003902 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003903 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003904 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003905 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003906 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003907 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003908 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003909 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003910 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003911 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003912 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003913 }
3914
Eric Laurent1c333e22014-05-20 10:48:17 -07003915 ALOGV("getNewOutputDevice() selected device %x", device);
3916 return device;
3917}
3918
3919audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3920{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003921 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003922
3923 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3924 if (index >= 0) {
3925 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3926 if (patchDesc->mUid != mUidCached) {
3927 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3928 inputDesc->mDevice, inputDesc->mPatchHandle);
3929 return inputDesc->mDevice;
3930 }
3931 }
3932
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003933 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003934
Eric Laurente552edb2014-03-10 17:42:56 -07003935 return device;
3936}
3937
Eric Laurente0720872014-03-11 09:30:41 -07003938uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003939 return (uint32_t)getStrategy(stream);
3940}
3941
Eric Laurente0720872014-03-11 09:30:41 -07003942audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003943 // By checking the range of stream before calling getStrategy, we avoid
3944 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3945 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003946 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003947 return AUDIO_DEVICE_NONE;
3948 }
3949 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003950 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003951 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3952 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3953 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003954 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003955 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003956 devices = outputDesc->device();
3957 break;
3958 }
Eric Laurente552edb2014-03-10 17:42:56 -07003959 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003960
3961 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3962 and doesn't really need to.*/
3963 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3964 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3965 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3966 }
3967
Eric Laurente552edb2014-03-10 17:42:56 -07003968 return devices;
3969}
3970
François Gaffie2110e042015-03-24 08:41:51 +01003971routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3972{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003973 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003974 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003975}
3976
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003977uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3978 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003979 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3980 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3981 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003982 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3983 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3984 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003985 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003986 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003987}
3988
Eric Laurente0720872014-03-11 09:30:41 -07003989void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003990 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003991 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003992 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3993 updateDevicesAndOutputs();
3994 break;
3995 default:
3996 break;
3997 }
3998}
3999
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004000uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4001 switch(event) {
4002 case STARTING_OUTPUT:
4003 mBeaconMuteRefCount++;
4004 break;
4005 case STOPPING_OUTPUT:
4006 if (mBeaconMuteRefCount > 0) {
4007 mBeaconMuteRefCount--;
4008 }
4009 break;
4010 case STARTING_BEACON:
4011 mBeaconPlayingRefCount++;
4012 break;
4013 case STOPPING_BEACON:
4014 if (mBeaconPlayingRefCount > 0) {
4015 mBeaconPlayingRefCount--;
4016 }
4017 break;
4018 }
4019
4020 if (mBeaconMuteRefCount > 0) {
4021 // any playback causes beacon to be muted
4022 return setBeaconMute(true);
4023 } else {
4024 // no other playback: unmute when beacon starts playing, mute when it stops
4025 return setBeaconMute(mBeaconPlayingRefCount == 0);
4026 }
4027}
4028
4029uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4030 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4031 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4032 // keep track of muted state to avoid repeating mute/unmute operations
4033 if (mBeaconMuted != mute) {
4034 // mute/unmute AUDIO_STREAM_TTS on all outputs
4035 ALOGV("\t muting %d", mute);
4036 uint32_t maxLatency = 0;
4037 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004038 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004039 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004040 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004041 0 /*delay*/, AUDIO_DEVICE_NONE);
4042 const uint32_t latency = desc->latency() * 2;
4043 if (latency > maxLatency) {
4044 maxLatency = latency;
4045 }
4046 }
4047 mBeaconMuted = mute;
4048 return maxLatency;
4049 }
4050 return 0;
4051}
4052
Eric Laurente0720872014-03-11 09:30:41 -07004053audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004054 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004055{
Paul McLeanaa981192015-03-21 09:55:15 -07004056 // Routing
4057 // see if we have an explicit route
4058 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4059 // (getStrategy(stream)).
4060 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4061 // and activity count is non-zero
4062 // the device = the device from the descriptor in the RouteMap, and exit.
4063 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4064 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4065 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004066 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004067 return route->mDeviceDescriptor->type();
4068 }
4069 }
4070
Eric Laurente552edb2014-03-10 17:42:56 -07004071 if (fromCache) {
4072 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4073 strategy, mDeviceForStrategy[strategy]);
4074 return mDeviceForStrategy[strategy];
4075 }
François Gaffie2110e042015-03-24 08:41:51 +01004076 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004077}
4078
Eric Laurente0720872014-03-11 09:30:41 -07004079void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004080{
4081 for (int i = 0; i < NUM_STRATEGIES; i++) {
4082 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4083 }
4084 mPreviousOutputs = mOutputs;
4085}
4086
Eric Laurent1f2f2232014-06-02 12:01:23 -07004087uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004088 audio_devices_t prevDevice,
4089 uint32_t delayMs)
4090{
4091 // mute/unmute strategies using an incompatible device combination
4092 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4093 // if unmuting, unmute only after the specified delay
4094 if (outputDesc->isDuplicated()) {
4095 return 0;
4096 }
4097
4098 uint32_t muteWaitMs = 0;
4099 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004100 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004101
4102 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4103 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004104 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004105 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4106 bool doMute = false;
4107
4108 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4109 doMute = true;
4110 outputDesc->mStrategyMutedByDevice[i] = true;
4111 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4112 doMute = true;
4113 outputDesc->mStrategyMutedByDevice[i] = false;
4114 }
Eric Laurent99401132014-05-07 19:48:15 -07004115 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004116 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004117 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004118 // skip output if it does not share any device with current output
4119 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4120 == AUDIO_DEVICE_NONE) {
4121 continue;
4122 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004123 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4124 mute ? "muting" : "unmuting", i, curDevice);
4125 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004126 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004127 if (mute) {
4128 // FIXME: should not need to double latency if volume could be applied
4129 // immediately by the audioflinger mixer. We must account for the delay
4130 // between now and the next time the audioflinger thread for this output
4131 // will process a buffer (which corresponds to one buffer size,
4132 // usually 1/2 or 1/4 of the latency).
4133 if (muteWaitMs < desc->latency() * 2) {
4134 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004135 }
4136 }
4137 }
4138 }
4139 }
4140 }
4141
Eric Laurent99401132014-05-07 19:48:15 -07004142 // temporary mute output if device selection changes to avoid volume bursts due to
4143 // different per device volumes
4144 if (outputDesc->isActive() && (device != prevDevice)) {
4145 if (muteWaitMs < outputDesc->latency() * 2) {
4146 muteWaitMs = outputDesc->latency() * 2;
4147 }
4148 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004149 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004150 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004151 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004152 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004153 muteWaitMs *2, device);
4154 }
4155 }
4156 }
4157
Eric Laurente552edb2014-03-10 17:42:56 -07004158 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4159 if (muteWaitMs > delayMs) {
4160 muteWaitMs -= delayMs;
4161 usleep(muteWaitMs * 1000);
4162 return muteWaitMs;
4163 }
4164 return 0;
4165}
4166
Eric Laurentc75307b2015-03-17 15:29:32 -07004167uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004168 audio_devices_t device,
4169 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004170 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004171 audio_patch_handle_t *patchHandle,
4172 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004173{
Eric Laurentc75307b2015-03-17 15:29:32 -07004174 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004175 AudioParameter param;
4176 uint32_t muteWaitMs;
4177
4178 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004179 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4180 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004181 return muteWaitMs;
4182 }
4183 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4184 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004185 if ((device != AUDIO_DEVICE_NONE) &&
4186 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004187 return 0;
4188 }
4189
4190 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004191 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004192
4193 audio_devices_t prevDevice = outputDesc->mDevice;
4194
Paul McLeanaa981192015-03-21 09:55:15 -07004195 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004196
4197 if (device != AUDIO_DEVICE_NONE) {
4198 outputDesc->mDevice = device;
4199 }
4200 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4201
4202 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004203 // the requested device is AUDIO_DEVICE_NONE
4204 // OR the requested device is the same as current device
4205 // AND force is not specified
4206 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004207 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004208 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4209 !force &&
4210 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004211 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004212 return muteWaitMs;
4213 }
4214
4215 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004216
Eric Laurente552edb2014-03-10 17:42:56 -07004217 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004218 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004219 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004220 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004221 DeviceVector deviceList = (address == NULL) ?
4222 mAvailableOutputDevices.getDevicesFromType(device)
4223 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004224 if (!deviceList.isEmpty()) {
4225 struct audio_patch patch;
4226 outputDesc->toAudioPortConfig(&patch.sources[0]);
4227 patch.num_sources = 1;
4228 patch.num_sinks = 0;
4229 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4230 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004231 patch.num_sinks++;
4232 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004233 ssize_t index;
4234 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4235 index = mAudioPatches.indexOfKey(*patchHandle);
4236 } else {
4237 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4238 }
4239 sp< AudioPatch> patchDesc;
4240 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4241 if (index >= 0) {
4242 patchDesc = mAudioPatches.valueAt(index);
4243 afPatchHandle = patchDesc->mAfPatchHandle;
4244 }
4245
Eric Laurent1c333e22014-05-20 10:48:17 -07004246 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004247 &afPatchHandle,
4248 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004249 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4250 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004251 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004252 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004253 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004254 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004255 addAudioPatch(patchDesc->mHandle, patchDesc);
4256 } else {
4257 patchDesc->mPatch = patch;
4258 }
4259 patchDesc->mAfPatchHandle = afPatchHandle;
4260 patchDesc->mUid = mUidCached;
4261 if (patchHandle) {
4262 *patchHandle = patchDesc->mHandle;
4263 }
4264 outputDesc->mPatchHandle = patchDesc->mHandle;
4265 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004266 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004267 }
4268 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004269
4270 // inform all input as well
4271 for (size_t i = 0; i < mInputs.size(); i++) {
4272 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004273 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004274 AudioParameter inputCmd = AudioParameter();
4275 ALOGV("%s: inform input %d of device:%d", __func__,
4276 inputDescriptor->mIoHandle, device);
4277 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4278 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4279 inputCmd.toString(),
4280 delayMs);
4281 }
4282 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004283 }
Eric Laurente552edb2014-03-10 17:42:56 -07004284
4285 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004286 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004287
4288 return muteWaitMs;
4289}
4290
Eric Laurentc75307b2015-03-17 15:29:32 -07004291status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004292 int delayMs,
4293 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004294{
Eric Laurent6a94d692014-05-20 11:18:06 -07004295 ssize_t index;
4296 if (patchHandle) {
4297 index = mAudioPatches.indexOfKey(*patchHandle);
4298 } else {
4299 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4300 }
4301 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004302 return INVALID_OPERATION;
4303 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004304 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4305 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4307 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004308 removeAudioPatch(patchDesc->mHandle);
4309 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004310 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004311 return status;
4312}
4313
4314status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4315 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004316 bool force,
4317 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004318{
4319 status_t status = NO_ERROR;
4320
Eric Laurent1f2f2232014-06-02 12:01:23 -07004321 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004322 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4323 inputDesc->mDevice = device;
4324
4325 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4326 if (!deviceList.isEmpty()) {
4327 struct audio_patch patch;
4328 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004329 // AUDIO_SOURCE_HOTWORD is for internal use only:
4330 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004331 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4332 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004333 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4334 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004335 patch.num_sinks = 1;
4336 //only one input device for now
4337 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004338 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004339 ssize_t index;
4340 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4341 index = mAudioPatches.indexOfKey(*patchHandle);
4342 } else {
4343 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4344 }
4345 sp< AudioPatch> patchDesc;
4346 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4347 if (index >= 0) {
4348 patchDesc = mAudioPatches.valueAt(index);
4349 afPatchHandle = patchDesc->mAfPatchHandle;
4350 }
4351
Eric Laurent1c333e22014-05-20 10:48:17 -07004352 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004353 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004354 0);
4355 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004356 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004357 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004358 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004359 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004360 addAudioPatch(patchDesc->mHandle, patchDesc);
4361 } else {
4362 patchDesc->mPatch = patch;
4363 }
4364 patchDesc->mAfPatchHandle = afPatchHandle;
4365 patchDesc->mUid = mUidCached;
4366 if (patchHandle) {
4367 *patchHandle = patchDesc->mHandle;
4368 }
4369 inputDesc->mPatchHandle = patchDesc->mHandle;
4370 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004371 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004372 }
4373 }
4374 }
4375 return status;
4376}
4377
Eric Laurent6a94d692014-05-20 11:18:06 -07004378status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4379 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004380{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004381 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004382 ssize_t index;
4383 if (patchHandle) {
4384 index = mAudioPatches.indexOfKey(*patchHandle);
4385 } else {
4386 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4387 }
4388 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004389 return INVALID_OPERATION;
4390 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004391 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4392 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004393 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4394 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004395 removeAudioPatch(patchDesc->mHandle);
4396 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004397 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004398 return status;
4399}
4400
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004401sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004402 String8 address,
4403 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004404 audio_format_t& format,
4405 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004406 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004407{
4408 // Choose an input profile based on the requested capture parameters: select the first available
4409 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004410 //
4411 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4412 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004413
4414 for (size_t i = 0; i < mHwModules.size(); i++)
4415 {
4416 if (mHwModules[i]->mHandle == 0) {
4417 continue;
4418 }
4419 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4420 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004421 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004422 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004423 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004424 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004425 format,
4426 &format /*updatedFormat*/,
4427 channelMask,
4428 &channelMask /*updatedChannelMask*/,
4429 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004430
Eric Laurente552edb2014-03-10 17:42:56 -07004431 return profile;
4432 }
4433 }
4434 }
4435 return NULL;
4436}
4437
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004438
4439audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004440 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004441{
François Gaffie036e1e92015-03-19 10:16:24 +01004442 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4443 audio_devices_t selectedDeviceFromMix =
4444 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004445
François Gaffie036e1e92015-03-19 10:16:24 +01004446 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4447 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004448 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004449 return getDeviceForInputSource(inputSource);
4450}
4451
4452audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4453{
Paul McLean466dc8e2015-04-17 13:15:36 -06004454 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4455 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004456 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004457 return route->mDeviceDescriptor->type();
4458 }
4459 }
4460
4461 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004462}
4463
Eric Laurente0720872014-03-11 09:30:41 -07004464float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004465 int index,
4466 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004467{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004468 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004469
4470 // if a headset is connected, apply the following rules to ring tones and notifications
4471 // to avoid sound level bursts in user's ears:
4472 // - always attenuate ring tones and notifications volume by 6dB
4473 // - if music is playing, always limit the volume to current music volume,
4474 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004475 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004476 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4477 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4478 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4479 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4480 ((stream_strategy == STRATEGY_SONIFICATION)
4481 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004482 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004483 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004484 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4485 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004486 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004487 // when the phone is ringing we must consider that music could have been paused just before
4488 // by the music application and behave as if music was active if the last music track was
4489 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004490 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004491 mLimitRingtoneVolume) {
4492 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004493 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4494 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004495 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004496 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4497 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4498 if (volumeDb > minVolDB) {
4499 volumeDb = minVolDB;
4500 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004501 }
4502 }
4503 }
4504
Eric Laurentffbc80f2015-03-18 18:30:19 -07004505 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004506}
4507
Eric Laurente0720872014-03-11 09:30:41 -07004508status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004509 int index,
4510 const sp<AudioOutputDescriptor>& outputDesc,
4511 audio_devices_t device,
4512 int delayMs,
4513 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004514{
Eric Laurente552edb2014-03-10 17:42:56 -07004515 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004516 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004517 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004518 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004519 return NO_ERROR;
4520 }
François Gaffie2110e042015-03-24 08:41:51 +01004521 audio_policy_forced_cfg_t forceUseForComm =
4522 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004523 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004524 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4525 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004526 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004527 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004528 return INVALID_OPERATION;
4529 }
4530
Eric Laurentc75307b2015-03-17 15:29:32 -07004531 if (device == AUDIO_DEVICE_NONE) {
4532 device = outputDesc->device();
4533 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004534
Eric Laurentffbc80f2015-03-18 18:30:19 -07004535 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004536 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004537 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004538 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004539
Eric Laurentffbc80f2015-03-18 18:30:19 -07004540 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004541
Eric Laurent3b73df72014-03-11 09:06:29 -07004542 if (stream == AUDIO_STREAM_VOICE_CALL ||
4543 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004544 float voiceVolume;
4545 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004546 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004547 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004548 } else {
4549 voiceVolume = 1.0;
4550 }
4551
Eric Laurentc75307b2015-03-17 15:29:32 -07004552 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004553 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4554 mLastVoiceVolume = voiceVolume;
4555 }
4556 }
4557
4558 return NO_ERROR;
4559}
4560
Eric Laurentc75307b2015-03-17 15:29:32 -07004561void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4562 audio_devices_t device,
4563 int delayMs,
4564 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004565{
Eric Laurentc75307b2015-03-17 15:29:32 -07004566 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004567
Eric Laurent3b73df72014-03-11 09:06:29 -07004568 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004569 if (stream == AUDIO_STREAM_PATCH) {
4570 continue;
4571 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004572 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004573 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4574 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004575 device,
4576 delayMs,
4577 force);
4578 }
4579}
4580
Eric Laurente0720872014-03-11 09:30:41 -07004581void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004582 bool on,
4583 const sp<AudioOutputDescriptor>& outputDesc,
4584 int delayMs,
4585 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004586{
4587 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004588 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004589 if (stream == AUDIO_STREAM_PATCH) {
4590 continue;
4591 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004592 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004593 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004594 }
4595 }
4596}
4597
Eric Laurente0720872014-03-11 09:30:41 -07004598void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004599 bool on,
4600 const sp<AudioOutputDescriptor>& outputDesc,
4601 int delayMs,
4602 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004603{
Eric Laurentc75307b2015-03-17 15:29:32 -07004604 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004605 if (device == AUDIO_DEVICE_NONE) {
4606 device = outputDesc->device();
4607 }
4608
Eric Laurentc75307b2015-03-17 15:29:32 -07004609 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4610 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004611
4612 if (on) {
4613 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004614 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004615 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004616 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004617 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004618 }
4619 }
4620 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4621 outputDesc->mMuteCount[stream]++;
4622 } else {
4623 if (outputDesc->mMuteCount[stream] == 0) {
4624 ALOGV("setStreamMute() unmuting non muted stream!");
4625 return;
4626 }
4627 if (--outputDesc->mMuteCount[stream] == 0) {
4628 checkAndSetVolume(stream,
4629 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004630 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004631 device,
4632 delayMs);
4633 }
4634 }
4635}
4636
Eric Laurente0720872014-03-11 09:30:41 -07004637void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004638 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004639{
Eric Laurent87ffa392015-05-22 10:32:38 -07004640 if(!hasPrimaryOutput()) {
4641 return;
4642 }
4643
Eric Laurente552edb2014-03-10 17:42:56 -07004644 // if the stream pertains to sonification strategy and we are in call we must
4645 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4646 // in the device used for phone strategy and play the tone if the selected device does not
4647 // interfere with the device used for phone strategy
4648 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4649 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004650 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004651 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4652 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004653 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004654 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4655 stream, starting, outputDesc->mDevice, stateChange);
4656 if (outputDesc->mRefCount[stream]) {
4657 int muteCount = 1;
4658 if (stateChange) {
4659 muteCount = outputDesc->mRefCount[stream];
4660 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004661 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004662 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4663 for (int i = 0; i < muteCount; i++) {
4664 setStreamMute(stream, starting, mPrimaryOutput);
4665 }
4666 } else {
4667 ALOGV("handleIncallSonification() high visibility");
4668 if (outputDesc->device() &
4669 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4670 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4671 for (int i = 0; i < muteCount; i++) {
4672 setStreamMute(stream, starting, mPrimaryOutput);
4673 }
4674 }
4675 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004676 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4677 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004678 } else {
4679 mpClientInterface->stopTone();
4680 }
4681 }
4682 }
4683 }
4684}
4685
Paul McLeanaa981192015-03-21 09:55:15 -07004686// --- SessionRoute class implementation
4687void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004688 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4689 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004690 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4691 mRefCount, mActivityCount);
4692}
4693
4694// --- SessionRouteMap class implementation
4695bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4696{
4697 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4698}
4699
Eric Laurent493404d2015-04-21 15:07:36 -07004700bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4701{
4702 if (indexOfKey(session) >= 0) {
4703 if (valueFor(session)->mChanged) {
4704 valueFor(session)->mChanged = false;
4705 return true;
4706 }
4707 }
4708 return false;
4709}
4710
Paul McLeanaa981192015-03-21 09:55:15 -07004711void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4712{
4713 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4714 if (route != 0) {
4715 ALOG_ASSERT(route->mRefCount > 0);
4716 --route->mRefCount;
4717 if (route->mRefCount <= 0) {
4718 removeItem(session);
4719 }
4720 }
4721}
4722
4723int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4724{
4725 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4726 return route != 0 ? ++(route->mActivityCount) : -1;
4727}
4728
4729int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4730{
4731 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4732 if (route != 0 && route->mActivityCount > 0) {
4733 return --(route->mActivityCount);
4734 } else {
4735 return -1;
4736 }
4737}
4738
4739void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4740 ALOGI("%s ----", caption);
4741 for(size_t index = 0; index < size(); index++) {
4742 valueAt(index)->log(" ");
4743 }
4744}
4745
Paul McLean466dc8e2015-04-17 13:15:36 -06004746void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4747 audio_stream_type_t streamType,
4748 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004749 sp<DeviceDescriptor> descriptor,
4750 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004751{
4752 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4753 ALOGE("Adding Output Route to InputRouteMap");
4754 return;
4755 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4756 ALOGE("Adding Input Route to OutputRouteMap");
4757 return;
4758 }
4759
4760 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4761
4762 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004763 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4764 ((route->mDeviceDescriptor != 0) &&
4765 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004766 route->mChanged = true;
4767 }
4768 route->mRefCount++;
4769 route->mDeviceDescriptor = descriptor;
4770 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004771 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004772 route->mRefCount++;
4773 add(session, route);
4774 if (descriptor != 0) {
4775 route->mChanged = true;
4776 }
4777 }
4778}
4779
Eric Laurente0720872014-03-11 09:30:41 -07004780void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004781{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004782 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004783 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004784 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004785 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004786 mAvailableOutputDevices.add(mDefaultOutputDevice);
4787 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004788
4789 module = new HwModule("primary");
4790
Eric Laurent322b4d22015-04-03 15:57:54 -07004791 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4792 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004793 profile->mSamplingRates.add(44100);
4794 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4795 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004796 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004797 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4798 module->mOutputProfiles.add(profile);
4799
Eric Laurent322b4d22015-04-03 15:57:54 -07004800 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4801 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004802 profile->mSamplingRates.add(8000);
4803 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4804 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004805 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004806 module->mInputProfiles.add(profile);
4807
4808 mHwModules.add(module);
4809}
4810
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004811audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4812{
4813 // flags to stream type mapping
4814 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4815 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4816 }
4817 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4818 return AUDIO_STREAM_BLUETOOTH_SCO;
4819 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004820 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4821 return AUDIO_STREAM_TTS;
4822 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004823
4824 // usage to stream type mapping
4825 switch (attr->usage) {
4826 case AUDIO_USAGE_MEDIA:
4827 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004828 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4829 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004830 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004831 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4832 return AUDIO_STREAM_ALARM;
4833 }
4834 if (isStreamActive(AUDIO_STREAM_RING)) {
4835 return AUDIO_STREAM_RING;
4836 }
4837 if (isInCall()) {
4838 return AUDIO_STREAM_VOICE_CALL;
4839 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004840 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004841 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4842 return AUDIO_STREAM_SYSTEM;
4843 case AUDIO_USAGE_VOICE_COMMUNICATION:
4844 return AUDIO_STREAM_VOICE_CALL;
4845
4846 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4847 return AUDIO_STREAM_DTMF;
4848
4849 case AUDIO_USAGE_ALARM:
4850 return AUDIO_STREAM_ALARM;
4851 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4852 return AUDIO_STREAM_RING;
4853
4854 case AUDIO_USAGE_NOTIFICATION:
4855 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4856 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4857 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4858 case AUDIO_USAGE_NOTIFICATION_EVENT:
4859 return AUDIO_STREAM_NOTIFICATION;
4860
4861 case AUDIO_USAGE_UNKNOWN:
4862 default:
4863 return AUDIO_STREAM_MUSIC;
4864 }
4865}
Eric Laurente83b55d2014-11-14 10:06:21 -08004866
François Gaffie53615e22015-03-19 09:24:12 +01004867bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4868{
Eric Laurente83b55d2014-11-14 10:06:21 -08004869 // has flags that map to a strategy?
4870 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4871 return true;
4872 }
4873
4874 // has known usage?
4875 switch (paa->usage) {
4876 case AUDIO_USAGE_UNKNOWN:
4877 case AUDIO_USAGE_MEDIA:
4878 case AUDIO_USAGE_VOICE_COMMUNICATION:
4879 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4880 case AUDIO_USAGE_ALARM:
4881 case AUDIO_USAGE_NOTIFICATION:
4882 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4883 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4884 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4885 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4886 case AUDIO_USAGE_NOTIFICATION_EVENT:
4887 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4888 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4889 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4890 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004891 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004892 break;
4893 default:
4894 return false;
4895 }
4896 return true;
4897}
4898
François Gaffiead3183e2015-03-18 16:55:35 +01004899bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4900 routing_strategy strategy, uint32_t inPastMs,
4901 nsecs_t sysTime) const
4902{
4903 if ((sysTime == 0) && (inPastMs != 0)) {
4904 sysTime = systemTime();
4905 }
4906 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4907 if (i == AUDIO_STREAM_PATCH) {
4908 continue;
4909 }
4910 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4911 (NUM_STRATEGIES == strategy)) &&
4912 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4913 return true;
4914 }
4915 }
4916 return false;
4917}
4918
François Gaffie2110e042015-03-24 08:41:51 +01004919audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4920{
4921 return mEngine->getForceUse(usage);
4922}
4923
4924bool AudioPolicyManager::isInCall()
4925{
4926 return isStateInCall(mEngine->getPhoneState());
4927}
4928
4929bool AudioPolicyManager::isStateInCall(int state)
4930{
4931 return is_state_in_call(state);
4932}
4933
Eric Laurente552edb2014-03-10 17:42:56 -07004934}; // namespace android