blob: d1ee4007fb774c760ec2c52c5d78cfb943cb5133 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
François Gaffie2110e042015-03-24 08:41:51 +0100264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800305 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700306 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
307
308 // release existing RX patch if any
309 if (mCallRxPatch != 0) {
310 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
311 mCallRxPatch.clear();
312 }
313 // release TX patch if any
314 if (mCallTxPatch != 0) {
315 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
316 mCallTxPatch.clear();
317 }
318
319 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
320 // via setOutputDevice() on primary output.
321 // Otherwise, create two audio patches for TX and RX path.
322 if (availablePrimaryOutputDevices() & rxDevice) {
323 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
324 // If the TX device is also on the primary HW module, setOutputDevice() will take care
325 // of it due to legacy implementation. If not, create a patch.
326 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
327 == AUDIO_DEVICE_NONE) {
328 createTxPatch = true;
329 }
330 } else {
331 // create RX path audio patch
332 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
333 ALOG_ASSERT(!deviceList.isEmpty(),
334 "updateCallRouting() selected device not in output device list");
335 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
336 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
337 ALOG_ASSERT(!deviceList.isEmpty(),
338 "updateCallRouting() no telephony RX device");
339 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
340
341 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
342 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
343
344 // request to reuse existing output stream if one is already opened to reach the RX device
345 SortedVector<audio_io_handle_t> outputs =
346 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700347 audio_io_handle_t output = selectOutput(outputs,
348 AUDIO_OUTPUT_FLAG_NONE,
349 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700350 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 ALOG_ASSERT(!outputDesc->isDuplicated(),
353 "updateCallRouting() RX device output is duplicated");
354 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700355 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700356 patch.num_sources = 2;
357 }
358
359 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
360 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
361 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
362 status);
363 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100364 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700365 mCallRxPatch->mAfPatchHandle = afPatchHandle;
366 mCallRxPatch->mUid = mUidCached;
367 }
368 createTxPatch = true;
369 }
370 if (createTxPatch) {
371
372 struct audio_patch patch;
373 patch.num_sources = 1;
374 patch.num_sinks = 1;
375 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
376 ALOG_ASSERT(!deviceList.isEmpty(),
377 "updateCallRouting() selected device not in input device list");
378 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
379 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
381 ALOG_ASSERT(!deviceList.isEmpty(),
382 "updateCallRouting() no telephony TX device");
383 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
384 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
385
386 SortedVector<audio_io_handle_t> outputs =
387 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700388 audio_io_handle_t output = selectOutput(outputs,
389 AUDIO_OUTPUT_FLAG_NONE,
390 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700391 // request to reuse existing output stream if one is already opened to reach the TX
392 // path output device
393 if (output != AUDIO_IO_HANDLE_NONE) {
394 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
395 ALOG_ASSERT(!outputDesc->isDuplicated(),
396 "updateCallRouting() RX device output is duplicated");
397 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700398 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 patch.num_sources = 2;
400 }
401
402 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
403 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
404 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
405 status);
406 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100407 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 mCallTxPatch->mAfPatchHandle = afPatchHandle;
409 mCallTxPatch->mUid = mUidCached;
410 }
411 }
412}
413
Eric Laurente0720872014-03-11 09:30:41 -0700414void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700415{
416 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100417 // store previous phone state for management of sonification strategy below
418 int oldState = mEngine->getPhoneState();
419
420 if (mEngine->setPhoneState(state) != NO_ERROR) {
421 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700422 return;
423 }
François Gaffie2110e042015-03-24 08:41:51 +0100424 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700425 // if leaving call state, handle special case of active streams
426 // pertaining to sonification strategy see handleIncallSonification()
427 if (isInCall()) {
428 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (stream == AUDIO_STREAM_PATCH) {
431 continue;
432 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700433 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800435
436 // force reevaluating accessibility routing when call starts
437 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 /**
441 * Switching to or from incall state or switching between telephony and VoIP lead to force
442 * routing command.
443 */
444 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
445 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700446
447 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700448 checkA2dpSuspend();
449 checkOutputForAllStrategies();
450 updateDevicesAndOutputs();
451
Eric Laurentc75307b2015-03-17 15:29:32 -0700452 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -0700453
Eric Laurente552edb2014-03-10 17:42:56 -0700454 int delayMs = 0;
455 if (isStateInCall(state)) {
456 nsecs_t sysTime = systemTime();
457 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700458 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700459 // mute media and sonification strategies and delay device switch by the largest
460 // latency of any output where either strategy is active.
461 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100462 if ((isStrategyActive(desc, STRATEGY_MEDIA,
463 SONIFICATION_HEADSET_MUSIC_DELAY,
464 sysTime) ||
465 isStrategyActive(desc, STRATEGY_SONIFICATION,
466 SONIFICATION_HEADSET_MUSIC_DELAY,
467 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700468 (delayMs < (int)desc->latency()*2)) {
469 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700471 setStrategyMute(STRATEGY_MEDIA, true, desc);
472 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700473 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700474 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
475 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700476 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
477 }
478 }
479
Eric Laurentc2730ba2014-07-20 15:47:07 -0700480 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
481 // the device returned is not necessarily reachable via this output
482 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
483 // force routing command to audio hardware when ending call
484 // even if no device change is needed
485 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
486 rxDevice = hwOutputDesc->device();
487 }
Eric Laurente552edb2014-03-10 17:42:56 -0700488
Eric Laurentc2730ba2014-07-20 15:47:07 -0700489 if (state == AUDIO_MODE_IN_CALL) {
490 updateCallRouting(rxDevice, delayMs);
491 } else if (oldState == AUDIO_MODE_IN_CALL) {
492 if (mCallRxPatch != 0) {
493 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
494 mCallRxPatch.clear();
495 }
496 if (mCallTxPatch != 0) {
497 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
498 mCallTxPatch.clear();
499 }
500 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
501 } else {
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504 // if entering in call state, handle special case of active streams
505 // pertaining to sonification strategy see handleIncallSonification()
506 if (isStateInCall(state)) {
507 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700512 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700513 }
514 }
515
516 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700517 if (state == AUDIO_MODE_RINGTONE &&
518 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 mLimitRingtoneVolume = true;
520 } else {
521 mLimitRingtoneVolume = false;
522 }
523}
524
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700525audio_mode_t AudioPolicyManager::getPhoneState() {
526 return mEngine->getPhoneState();
527}
528
Eric Laurente0720872014-03-11 09:30:41 -0700529void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
François Gaffie2110e042015-03-24 08:41:51 +0100532 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700533
François Gaffie2110e042015-03-24 08:41:51 +0100534 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
535 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
536 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700537 }
François Gaffie2110e042015-03-24 08:41:51 +0100538 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
539 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
540 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700541
542 // check for device and output changes triggered by new force usage
543 checkA2dpSuspend();
544 checkOutputForAllStrategies();
545 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100546 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700547 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
548 updateCallRouting(newDevice);
549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700551 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
552 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
553 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
554 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700558 }
559 }
560
François Gaffie53615e22015-03-19 09:24:12 +0100561 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700563 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565
566}
567
Eric Laurente0720872014-03-11 09:30:41 -0700568void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
570 ALOGV("setSystemProperty() property %s, value %s", property, value);
571}
572
573// Find a direct output profile compatible with the parameters passed, even if the input flags do
574// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700576 audio_devices_t device,
577 uint32_t samplingRate,
578 audio_format_t format,
579 audio_channel_mask_t channelMask,
580 audio_output_flags_t flags)
581{
582 for (size_t i = 0; i < mHwModules.size(); i++) {
583 if (mHwModules[i]->mHandle == 0) {
584 continue;
585 }
586 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700587 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Andy Hungf129b032015-04-07 13:45:50 -0700588 bool found = profile->isCompatibleProfile(device, String8(""),
589 samplingRate, NULL /*updatedSamplingRate*/,
590 format, NULL /*updatedFormat*/,
591 channelMask, NULL /*updatedChannelMask*/,
Glenn Kastencbd48022014-07-24 13:46:44 -0700592 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
593 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700594 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
595 return profile;
596 }
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
598 }
599 return 0;
600}
601
Eric Laurente0720872014-03-11 09:30:41 -0700602audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags,
607 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700608{
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
611 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
612 device, stream, samplingRate, format, channelMask, flags);
613
Eric Laurente83b55d2014-11-14 10:06:21 -0800614 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
615 stream, samplingRate,format, channelMask,
616 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700617}
618
Eric Laurente83b55d2014-11-14 10:06:21 -0800619status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
620 audio_io_handle_t *output,
621 audio_session_t session,
622 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700623 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800624 uint32_t samplingRate,
625 audio_format_t format,
626 audio_channel_mask_t channelMask,
627 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700628 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800629 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700630{
Eric Laurente83b55d2014-11-14 10:06:21 -0800631 audio_attributes_t attributes;
632 if (attr != NULL) {
633 if (!isValidAttributes(attr)) {
634 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
635 attr->usage, attr->content_type, attr->flags,
636 attr->tags);
637 return BAD_VALUE;
638 }
639 attributes = *attr;
640 } else {
641 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
642 ALOGE("getOutputForAttr(): invalid stream type");
643 return BAD_VALUE;
644 }
645 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700646 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700647 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100648 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
649 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
650 if (!audio_is_linear_pcm(format)) {
651 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800652 }
François Gaffie036e1e92015-03-19 10:16:24 +0100653 *stream = streamTypefromAttributesInt(&attributes);
654 *output = desc->mIoHandle;
655 ALOGV("getOutputForAttr() returns output %d", *output);
656 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800657 }
658 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
659 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
660 return BAD_VALUE;
661 }
662
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700663 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
664 " session %d selectedDeviceId %d",
665 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
666 session, selectedDeviceId);
667
668 *stream = streamTypefromAttributesInt(&attributes);
669
670 // Explicit routing?
671 sp<DeviceDescriptor> deviceDesc;
672 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
673 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
674 deviceDesc = mAvailableOutputDevices[i];
675 break;
676 }
677 }
678 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700679
Eric Laurente83b55d2014-11-14 10:06:21 -0800680 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700681 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700682
Eric Laurente83b55d2014-11-14 10:06:21 -0800683 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700684 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
685 }
686
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700687 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700688 device, samplingRate, format, channelMask, flags);
689
Eric Laurente83b55d2014-11-14 10:06:21 -0800690 *output = getOutputForDevice(device, session, *stream,
691 samplingRate, format, channelMask,
692 flags, offloadInfo);
693 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700694 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800695 return INVALID_OPERATION;
696 }
Paul McLeanaa981192015-03-21 09:55:15 -0700697
Eric Laurente83b55d2014-11-14 10:06:21 -0800698 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700699}
700
701audio_io_handle_t AudioPolicyManager::getOutputForDevice(
702 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800703 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700704 audio_stream_type_t stream,
705 uint32_t samplingRate,
706 audio_format_t format,
707 audio_channel_mask_t channelMask,
708 audio_output_flags_t flags,
709 const audio_offload_info_t *offloadInfo)
710{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700711 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700712 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700713 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700714
Eric Laurente552edb2014-03-10 17:42:56 -0700715#ifdef AUDIO_POLICY_TEST
716 if (mCurOutput != 0) {
717 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
718 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
719
720 if (mTestOutputs[mCurOutput] == 0) {
721 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700722 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
723 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700724 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700725 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700726 outputDesc->mFlags =
727 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700728 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700729 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
730 config.sample_rate = mTestSamplingRate;
731 config.channel_mask = mTestChannels;
732 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700733 if (offloadInfo != NULL) {
734 config.offload_info = *offloadInfo;
735 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700736 status = mpClientInterface->openOutput(0,
737 &mTestOutputs[mCurOutput],
738 &config,
739 &outputDesc->mDevice,
740 String8(""),
741 &outputDesc->mLatency,
742 outputDesc->mFlags);
743 if (status == NO_ERROR) {
744 outputDesc->mSamplingRate = config.sample_rate;
745 outputDesc->mFormat = config.format;
746 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700747 AudioParameter outputCmd = AudioParameter();
748 outputCmd.addInt(String8("set_id"),mCurOutput);
749 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
750 addOutput(mTestOutputs[mCurOutput], outputDesc);
751 }
752 }
753 return mTestOutputs[mCurOutput];
754 }
755#endif //AUDIO_POLICY_TEST
756
757 // open a direct output if required by specified parameters
758 //force direct flag if offload flag is set: offloading implies a direct output stream
759 // and all common behaviors are driven by checking only the direct flag
760 // this should normally be set appropriately in the policy configuration file
761 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700762 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700763 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700764 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
765 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
766 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800767 // only allow deep buffering for music stream type
768 if (stream != AUDIO_STREAM_MUSIC) {
769 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
770 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700771 if (stream == AUDIO_STREAM_TTS) {
772 flags = AUDIO_OUTPUT_FLAG_TTS;
773 }
Eric Laurente552edb2014-03-10 17:42:56 -0700774
Eric Laurentb732cf52014-09-24 19:08:21 -0700775 sp<IOProfile> profile;
776
777 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700778 // and not explicitly requested
779 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
780 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700781 audio_channel_count_from_out_mask(channelMask) <= 2) {
782 goto non_direct_output;
783 }
784
Eric Laurente552edb2014-03-10 17:42:56 -0700785 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
786 // creating an offloaded track and tearing it down immediately after start when audioflinger
787 // detects there is an active non offloadable effect.
788 // FIXME: We should check the audio session here but we do not have it in this context.
789 // This may prevent offloading in rare situations where effects are left active by apps
790 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700791
Eric Laurente552edb2014-03-10 17:42:56 -0700792 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100793 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700794 profile = getProfileForDirectOutput(device,
795 samplingRate,
796 format,
797 channelMask,
798 (audio_output_flags_t)flags);
799 }
800
Eric Laurent1c333e22014-05-20 10:48:17 -0700801 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700802 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700803
804 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700805 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700806 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
807 outputDesc = desc;
808 // reuse direct output if currently open and configured with same parameters
809 if ((samplingRate == outputDesc->mSamplingRate) &&
810 (format == outputDesc->mFormat) &&
811 (channelMask == outputDesc->mChannelMask)) {
812 outputDesc->mDirectOpenCount++;
813 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
814 return mOutputs.keyAt(i);
815 }
816 }
817 }
818 // close direct output if currently open and configured with different parameters
819 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700820 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700821 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700822 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700823 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700824 outputDesc->mLatency = 0;
825 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700826 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
827 config.sample_rate = samplingRate;
828 config.channel_mask = channelMask;
829 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700830 if (offloadInfo != NULL) {
831 config.offload_info = *offloadInfo;
832 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700833 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700834 &output,
835 &config,
836 &outputDesc->mDevice,
837 String8(""),
838 &outputDesc->mLatency,
839 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700840
841 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700842 if (status != NO_ERROR ||
843 (samplingRate != 0 && samplingRate != config.sample_rate) ||
844 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
845 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700846 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
847 "format %d %d, channelMask %04x %04x", output, samplingRate,
848 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
849 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700850 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700851 mpClientInterface->closeOutput(output);
852 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800853 // fall back to mixer output if possible when the direct output could not be open
854 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
855 goto non_direct_output;
856 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800857 // fall back to mixer output if possible when the direct output could not be open
858 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
859 goto non_direct_output;
860 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700861 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700862 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700863 outputDesc->mSamplingRate = config.sample_rate;
864 outputDesc->mChannelMask = config.channel_mask;
865 outputDesc->mFormat = config.format;
866 outputDesc->mRefCount[stream] = 0;
867 outputDesc->mStopTime[stream] = 0;
868 outputDesc->mDirectOpenCount = 1;
869
Eric Laurente552edb2014-03-10 17:42:56 -0700870 audio_io_handle_t srcOutput = getOutputForEffect();
871 addOutput(output, outputDesc);
872 audio_io_handle_t dstOutput = getOutputForEffect();
873 if (dstOutput == output) {
874 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
875 }
876 mPreviousOutputs = mOutputs;
877 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700878 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700879 return output;
880 }
881
Eric Laurentb732cf52014-09-24 19:08:21 -0700882non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700883 // ignoring channel mask due to downmix capability in mixer
884
885 // open a non direct output
886
887 // for non direct outputs, only PCM is supported
888 if (audio_is_linear_pcm(format)) {
889 // get which output is suitable for the specified stream. The actual
890 // routing change will happen when startOutput() will be called
891 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
892
Eric Laurent8838a382014-09-08 16:44:28 -0700893 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
894 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
895 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700896 }
897 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
898 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
899
Paul McLeanaa981192015-03-21 09:55:15 -0700900 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700901
902 return output;
903}
904
Eric Laurente0720872014-03-11 09:30:41 -0700905audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700906 audio_output_flags_t flags,
907 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700908{
909 // select one output among several that provide a path to a particular device or set of
910 // devices (the list was previously build by getOutputsForDevice()).
911 // The priority is as follows:
912 // 1: the output with the highest number of requested policy flags
913 // 2: the primary output
914 // 3: the first output in the list
915
916 if (outputs.size() == 0) {
917 return 0;
918 }
919 if (outputs.size() == 1) {
920 return outputs[0];
921 }
922
923 int maxCommonFlags = 0;
924 audio_io_handle_t outputFlags = 0;
925 audio_io_handle_t outputPrimary = 0;
926
927 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700928 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700929 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700930 // if a valid format is specified, skip output if not compatible
931 if (format != AUDIO_FORMAT_INVALID) {
932 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
933 if (format != outputDesc->mFormat) {
934 continue;
935 }
936 } else if (!audio_is_linear_pcm(format)) {
937 continue;
938 }
939 }
940
Eric Laurent3b73df72014-03-11 09:06:29 -0700941 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700942 if (commonFlags > maxCommonFlags) {
943 outputFlags = outputs[i];
944 maxCommonFlags = commonFlags;
945 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
946 }
947 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
948 outputPrimary = outputs[i];
949 }
950 }
951 }
952
953 if (outputFlags != 0) {
954 return outputFlags;
955 }
956 if (outputPrimary != 0) {
957 return outputPrimary;
958 }
959
960 return outputs[0];
961}
962
Eric Laurente0720872014-03-11 09:30:41 -0700963status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700964 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800965 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700966{
Paul McLeanaa981192015-03-21 09:55:15 -0700967 ALOGV("startOutput() output %d, stream %d, session %d",
968 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700969 ssize_t index = mOutputs.indexOfKey(output);
970 if (index < 0) {
971 ALOGW("startOutput() unknown output %d", output);
972 return BAD_VALUE;
973 }
974
Eric Laurentc75307b2015-03-17 15:29:32 -0700975 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
976
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700977 // Routing?
978 mOutputRoutes.incRouteActivity(session);
979
Eric Laurentc75307b2015-03-17 15:29:32 -0700980 audio_devices_t newDevice;
981 if (outputDesc->mPolicyMix != NULL) {
982 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -0700983 } else if (mOutputRoutes.hasRouteChanged(session)) {
984 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700985 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -0700986 } else {
987 newDevice = AUDIO_DEVICE_NONE;
988 }
989
990 uint32_t delayMs = 0;
991
Eric Laurentc75307b2015-03-17 15:29:32 -0700992 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
993
994 if (status != NO_ERROR) {
995 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700996 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -0700997 }
998 // Automatically enable the remote submix input when output is started on a re routing mix
999 // of type MIX_TYPE_RECORDERS
1000 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1001 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1002 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1003 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1004 outputDesc->mPolicyMix->mRegistrationId,
1005 "remote-submix");
1006 }
1007
1008 if (delayMs != 0) {
1009 usleep(delayMs * 1000);
1010 }
1011
1012 return status;
1013}
1014
1015status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1016 audio_stream_type_t stream,
1017 audio_devices_t device,
1018 uint32_t *delayMs)
1019{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001020 // cannot start playback of STREAM_TTS if any other output is being used
1021 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001022
1023 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001024 if (stream == AUDIO_STREAM_TTS) {
1025 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001026 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001027 return INVALID_OPERATION;
1028 } else {
1029 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1030 }
1031 } else {
1032 // some playback other than beacon starts
1033 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1034 }
1035
Eric Laurente552edb2014-03-10 17:42:56 -07001036 // increment usage count for this stream on the requested output:
1037 // NOTE that the usage count is the same for duplicated output and hardware output which is
1038 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1039 outputDesc->changeRefCount(stream, 1);
1040
Eric Laurent493404d2015-04-21 15:07:36 -07001041 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001042 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001043 if (device == AUDIO_DEVICE_NONE) {
1044 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001045 }
Eric Laurente552edb2014-03-10 17:42:56 -07001046 routing_strategy strategy = getStrategy(stream);
1047 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001048 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1049 (beaconMuteLatency > 0);
1050 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001051 bool force = false;
1052 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001053 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001054 if (desc != outputDesc) {
1055 // force a device change if any other output is managed by the same hw
1056 // module and has a current device selection that differs from selected device.
1057 // In this case, the audio HAL must receive the new device selection so that it can
1058 // change the device currently selected by the other active output.
1059 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001060 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001061 force = true;
1062 }
1063 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001064 // a notification so that audio focus effect can propagate, or that a mute/unmute
1065 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001066 uint32_t latency = desc->latency();
1067 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1068 waitMs = latency;
1069 }
1070 }
1071 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001072 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001073
1074 // handle special case for sonification while in call
1075 if (isInCall()) {
1076 handleIncallSonification(stream, true, false);
1077 }
1078
1079 // apply volume rules for current stream and device if necessary
1080 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001081 mStreams.valueFor(stream).getVolumeIndex(device),
1082 outputDesc,
1083 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001084
1085 // update the outputs if starting an output with a stream that can affect notification
1086 // routing
1087 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001088
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001089 // force reevaluating accessibility routing when ringtone or alarm starts
1090 if (strategy == STRATEGY_SONIFICATION) {
1091 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1092 }
Eric Laurente552edb2014-03-10 17:42:56 -07001093 }
1094 return NO_ERROR;
1095}
1096
1097
Eric Laurente0720872014-03-11 09:30:41 -07001098status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001099 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001100 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001101{
1102 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1103 ssize_t index = mOutputs.indexOfKey(output);
1104 if (index < 0) {
1105 ALOGW("stopOutput() unknown output %d", output);
1106 return BAD_VALUE;
1107 }
1108
Eric Laurentc75307b2015-03-17 15:29:32 -07001109 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001110
Eric Laurentc75307b2015-03-17 15:29:32 -07001111 if (outputDesc->mRefCount[stream] == 1) {
1112 // Automatically disable the remote submix input when output is stopped on a
1113 // re routing mix of type MIX_TYPE_RECORDERS
1114 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1115 outputDesc->mPolicyMix != NULL &&
1116 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1117 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1118 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1119 outputDesc->mPolicyMix->mRegistrationId,
1120 "remote-submix");
1121 }
1122 }
1123
1124 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001125 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001126 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001127 int activityCount = mOutputRoutes.decRouteActivity(session);
1128 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1129
1130 if (forceDeviceUpdate) {
1131 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1132 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001133 }
1134
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001135 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001136}
1137
1138status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001139 audio_stream_type_t stream,
1140 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001141{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001142 // always handle stream stop, check which stream type is stopping
1143 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1144
Eric Laurente552edb2014-03-10 17:42:56 -07001145 // handle special case for sonification while in call
1146 if (isInCall()) {
1147 handleIncallSonification(stream, false, false);
1148 }
1149
1150 if (outputDesc->mRefCount[stream] > 0) {
1151 // decrement usage count of this stream on the output
1152 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001153
Eric Laurente552edb2014-03-10 17:42:56 -07001154 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001155 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001156 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001157 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001158 // delay the device switch by twice the latency because stopOutput() is executed when
1159 // the track stop() command is received and at that time the audio track buffer can
1160 // still contain data that needs to be drained. The latency only covers the audio HAL
1161 // and kernel buffers. Also the latency does not always include additional delay in the
1162 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001163 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001164
1165 // force restoring the device selection on other active outputs if it differs from the
1166 // one being selected for this output
1167 for (size_t i = 0; i < mOutputs.size(); i++) {
1168 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001169 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001170 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001171 desc->isActive() &&
1172 outputDesc->sharesHwModuleWith(desc) &&
1173 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001174 setOutputDevice(desc,
1175 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001176 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001177 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001178 }
1179 }
1180 // update the outputs if stopping one with a stream that can affect notification routing
1181 handleNotificationRoutingForStream(stream);
1182 }
1183 return NO_ERROR;
1184 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001185 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001186 return INVALID_OPERATION;
1187 }
1188}
1189
Eric Laurente83b55d2014-11-14 10:06:21 -08001190void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001191 audio_stream_type_t stream __unused,
1192 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001193{
1194 ALOGV("releaseOutput() %d", output);
1195 ssize_t index = mOutputs.indexOfKey(output);
1196 if (index < 0) {
1197 ALOGW("releaseOutput() releasing unknown output %d", output);
1198 return;
1199 }
1200
1201#ifdef AUDIO_POLICY_TEST
1202 int testIndex = testOutputIndex(output);
1203 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001204 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001205 if (outputDesc->isActive()) {
1206 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001207 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001208 mTestOutputs[testIndex] = 0;
1209 }
1210 return;
1211 }
1212#endif //AUDIO_POLICY_TEST
1213
Paul McLeanaa981192015-03-21 09:55:15 -07001214 // Routing
1215 mOutputRoutes.removeRoute(session);
1216
Eric Laurentc75307b2015-03-17 15:29:32 -07001217 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001218 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001219 if (desc->mDirectOpenCount <= 0) {
1220 ALOGW("releaseOutput() invalid open count %d for output %d",
1221 desc->mDirectOpenCount, output);
1222 return;
1223 }
1224 if (--desc->mDirectOpenCount == 0) {
1225 closeOutput(output);
1226 // If effects where present on the output, audioflinger moved them to the primary
1227 // output by default: move them back to the appropriate output.
1228 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001229 if (dstOutput != mPrimaryOutput->mIoHandle) {
1230 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1231 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001232 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001233 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001234 }
1235 }
1236}
1237
1238
Eric Laurentcaf7f482014-11-25 17:50:47 -08001239status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1240 audio_io_handle_t *input,
1241 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001242 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001243 uint32_t samplingRate,
1244 audio_format_t format,
1245 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001246 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001247 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001248 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001249{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001250 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1251 "session %d, flags %#x",
1252 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001253
Eric Laurentcaf7f482014-11-25 17:50:47 -08001254 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001255 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001256 audio_devices_t device;
1257 // handle legacy remote submix case where the address was not always specified
1258 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001259 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001260 audio_source_t inputSource = attr->source;
1261 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001262 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001263
Eric Laurentc447ded2015-01-06 08:47:05 -08001264 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1265 inputSource = AUDIO_SOURCE_MIC;
1266 }
1267 halInputSource = inputSource;
1268
Paul McLean466dc8e2015-04-17 13:15:36 -06001269 // Explicit routing?
1270 sp<DeviceDescriptor> deviceDesc;
1271 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1272 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1273 deviceDesc = mAvailableInputDevices[i];
1274 break;
1275 }
1276 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001277 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001278
Eric Laurentc447ded2015-01-06 08:47:05 -08001279 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001280 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001281 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001282 if (ret != NO_ERROR) {
1283 return ret;
1284 }
1285 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001286 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1287 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001288 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001289 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001290 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001291 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001292 return BAD_VALUE;
1293 }
Eric Laurentc722f302014-12-10 11:21:49 -08001294 if (policyMix != NULL) {
1295 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001296 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1297 // there is an external policy, but this input is attached to a mix of recorders,
1298 // meaning it receives audio injected into the framework, so the recorder doesn't
1299 // know about it and is therefore considered "legacy"
1300 *inputType = API_INPUT_LEGACY;
1301 } else {
1302 // recording a mix of players defined by an external policy, we're rerouting for
1303 // an external policy
1304 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1305 }
Eric Laurentc722f302014-12-10 11:21:49 -08001306 } else if (audio_is_remote_submix_device(device)) {
1307 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001308 *inputType = API_INPUT_MIX_CAPTURE;
1309 } else {
1310 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001311 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001312 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001313 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001314 case AUDIO_SOURCE_VOICE_UPLINK:
1315 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1316 break;
1317 case AUDIO_SOURCE_VOICE_DOWNLINK:
1318 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1319 break;
1320 case AUDIO_SOURCE_VOICE_CALL:
1321 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1322 break;
1323 default:
1324 break;
1325 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001326 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001327 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1328 if (index >= 0) {
1329 *input = mSoundTriggerSessions.valueFor(session);
1330 isSoundTrigger = true;
1331 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1332 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1333 } else {
1334 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1335 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001336 }
1337 }
1338
Andy Hungf129b032015-04-07 13:45:50 -07001339 // find a compatible input profile (not necessarily identical in parameters)
1340 sp<IOProfile> profile;
1341 // samplingRate and flags may be updated by getInputProfile
1342 uint32_t profileSamplingRate = samplingRate;
1343 audio_format_t profileFormat = format;
1344 audio_channel_mask_t profileChannelMask = channelMask;
1345 audio_input_flags_t profileFlags = flags;
1346 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001347 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001348 profileSamplingRate, profileFormat, profileChannelMask,
1349 profileFlags);
1350 if (profile != 0) {
1351 break; // success
1352 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1353 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1354 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1356 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001357 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001358 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001359 }
Eric Laurente552edb2014-03-10 17:42:56 -07001360 }
1361
Eric Laurent322b4d22015-04-03 15:57:54 -07001362 if (profile->getModuleHandle() == 0) {
1363 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001364 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001365 }
1366
1367 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001368 config.sample_rate = profileSamplingRate;
1369 config.channel_mask = profileChannelMask;
1370 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001371
Eric Laurent322b4d22015-04-03 15:57:54 -07001372 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001373 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001374 &config,
1375 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001376 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001377 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001378 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001379
1380 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001381 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001382 (profileSamplingRate != config.sample_rate) ||
1383 (profileFormat != config.format) ||
1384 (profileChannelMask != config.channel_mask)) {
1385 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1386 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001387 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001388 if (*input != AUDIO_IO_HANDLE_NONE) {
1389 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001390 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001391 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001392 }
1393
Eric Laurent1f2f2232014-06-02 12:01:23 -07001394 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001395 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001396 inputDesc->mRefCount = 0;
1397 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001398 inputDesc->mSamplingRate = profileSamplingRate;
1399 inputDesc->mFormat = profileFormat;
1400 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001401 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001402 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001403 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001404 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001405
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001406 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001407
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001409 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001410
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001412}
1413
Eric Laurent4dc68062014-07-28 17:26:49 -07001414status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1415 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001416{
1417 ALOGV("startInput() input %d", input);
1418 ssize_t index = mInputs.indexOfKey(input);
1419 if (index < 0) {
1420 ALOGW("startInput() unknown input %d", input);
1421 return BAD_VALUE;
1422 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001423 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001424
Eric Laurentc722f302014-12-10 11:21:49 -08001425 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001426 if (index < 0) {
1427 ALOGW("startInput() unknown session %d on input %d", session, input);
1428 return BAD_VALUE;
1429 }
1430
Glenn Kasten74a8e252014-07-24 14:09:55 -07001431 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001432 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001433
1434 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001435 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001436 if (activeInput != 0 && activeInput != input) {
1437
1438 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1439 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001440 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001441 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001442 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001443 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1444 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001445 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001446 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001447 return INVALID_OPERATION;
1448 }
1449 }
1450 }
1451
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001452 // Routing?
1453 mInputRoutes.incRouteActivity(session);
1454
Paul McLean466dc8e2015-04-17 13:15:36 -06001455 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001456 // if input maps to a dynamic policy with an activity listener, notify of state change
1457 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001458 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001459 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1460 MIX_STATE_MIXING);
1461 }
1462
François Gaffie53615e22015-03-19 09:24:12 +01001463 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001464 SoundTrigger::setCaptureState(true);
1465 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001466 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001467
Eric Laurentc722f302014-12-10 11:21:49 -08001468 // automatically enable the remote submix output when input is started if not
1469 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001470 // For remote submix (a virtual device), we open only one input per capture request.
1471 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001472 String8 address = String8("");
1473 if (inputDesc->mPolicyMix == NULL) {
1474 address = String8("0");
1475 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1476 address = inputDesc->mPolicyMix->mRegistrationId;
1477 }
1478 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001479 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001480 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001481 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001482 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001483 }
Eric Laurente552edb2014-03-10 17:42:56 -07001484 }
1485
Eric Laurente552edb2014-03-10 17:42:56 -07001486 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1487
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001488 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001489 return NO_ERROR;
1490}
1491
Eric Laurent4dc68062014-07-28 17:26:49 -07001492status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1493 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001494{
1495 ALOGV("stopInput() input %d", input);
1496 ssize_t index = mInputs.indexOfKey(input);
1497 if (index < 0) {
1498 ALOGW("stopInput() unknown input %d", input);
1499 return BAD_VALUE;
1500 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001501 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001502
Eric Laurentc722f302014-12-10 11:21:49 -08001503 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001504 if (index < 0) {
1505 ALOGW("stopInput() unknown session %d on input %d", session, input);
1506 return BAD_VALUE;
1507 }
1508
Eric Laurente552edb2014-03-10 17:42:56 -07001509 if (inputDesc->mRefCount == 0) {
1510 ALOGW("stopInput() input %d already stopped", input);
1511 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001512 }
1513
1514 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001515
1516 // Routing?
1517 mInputRoutes.decRouteActivity(session);
1518
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001519 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001520 // if input maps to a dynamic policy with an activity listener, notify of state change
1521 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001522 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001523 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1524 MIX_STATE_IDLE);
1525 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001526
Eric Laurentc722f302014-12-10 11:21:49 -08001527 // automatically disable the remote submix output when input is stopped if not
1528 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001529 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001530 String8 address = String8("");
1531 if (inputDesc->mPolicyMix == NULL) {
1532 address = String8("0");
1533 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1534 address = inputDesc->mPolicyMix->mRegistrationId;
1535 }
1536 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001537 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001538 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001539 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001540 }
Eric Laurente552edb2014-03-10 17:42:56 -07001541 }
1542
Eric Laurent1c333e22014-05-20 10:48:17 -07001543 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001544
François Gaffie53615e22015-03-19 09:24:12 +01001545 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001546 SoundTrigger::setCaptureState(false);
1547 }
Eric Laurente552edb2014-03-10 17:42:56 -07001548 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001549 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001550}
1551
Eric Laurent4dc68062014-07-28 17:26:49 -07001552void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1553 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001554{
1555 ALOGV("releaseInput() %d", input);
1556 ssize_t index = mInputs.indexOfKey(input);
1557 if (index < 0) {
1558 ALOGW("releaseInput() releasing unknown input %d", input);
1559 return;
1560 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001561
1562 // Routing
1563 mInputRoutes.removeRoute(session);
1564
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001565 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1566 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001567
Eric Laurentc722f302014-12-10 11:21:49 -08001568 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001569 if (index < 0) {
1570 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1571 return;
1572 }
Eric Laurentc722f302014-12-10 11:21:49 -08001573 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001574 if (inputDesc->mOpenRefCount == 0) {
1575 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1576 return;
1577 }
1578 inputDesc->mOpenRefCount--;
1579 if (inputDesc->mOpenRefCount > 0) {
1580 ALOGV("releaseInput() exit > 0");
1581 return;
1582 }
1583
Eric Laurent05b90f82014-08-27 15:32:29 -07001584 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001585 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001586 ALOGV("releaseInput() exit");
1587}
1588
Eric Laurentd4692962014-05-05 18:13:44 -07001589void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001590 bool patchRemoved = false;
1591
Eric Laurentd4692962014-05-05 18:13:44 -07001592 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001593 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1594 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1595 if (patch_index >= 0) {
1596 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1597 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1598 mAudioPatches.removeItemsAt(patch_index);
1599 patchRemoved = true;
1600 }
Eric Laurentd4692962014-05-05 18:13:44 -07001601 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1602 }
1603 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001604 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001605
1606 if (patchRemoved) {
1607 mpClientInterface->onAudioPatchListUpdate();
1608 }
Eric Laurentd4692962014-05-05 18:13:44 -07001609}
1610
Eric Laurente0720872014-03-11 09:30:41 -07001611void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001612 int indexMin,
1613 int indexMax)
1614{
1615 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001616 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001617 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1618 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001619 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001620 }
Eric Laurente552edb2014-03-10 17:42:56 -07001621}
1622
Eric Laurente0720872014-03-11 09:30:41 -07001623status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001624 int index,
1625 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001626{
1627
Eric Laurentc75307b2015-03-17 15:29:32 -07001628 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1629 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001630 return BAD_VALUE;
1631 }
1632 if (!audio_is_output_device(device)) {
1633 return BAD_VALUE;
1634 }
1635
1636 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001637 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001638
1639 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1640 stream, device, index);
1641
1642 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1643 // clear all device specific values
1644 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001645 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001646 }
François Gaffiedfd74092015-03-19 12:10:59 +01001647 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001648
Eric Laurent31551f82014-10-10 18:21:56 -07001649 // update volume on all outputs whose current device is also selected by the same
1650 // strategy as the device specified by the caller
1651 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001652
1653
1654 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1655 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1656 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001657 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001658 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1659 }
1660 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1661 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001662 return NO_ERROR;
1663 }
Eric Laurente552edb2014-03-10 17:42:56 -07001664 status_t status = NO_ERROR;
1665 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001666 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1667 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001668 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001669 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001670 if (volStatus != NO_ERROR) {
1671 status = volStatus;
1672 }
1673 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001674 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1675 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001676 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001677 }
Eric Laurente552edb2014-03-10 17:42:56 -07001678 }
1679 return status;
1680}
1681
Eric Laurente0720872014-03-11 09:30:41 -07001682status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001683 int *index,
1684 audio_devices_t device)
1685{
1686 if (index == NULL) {
1687 return BAD_VALUE;
1688 }
1689 if (!audio_is_output_device(device)) {
1690 return BAD_VALUE;
1691 }
1692 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1693 // the strategy the stream belongs to.
1694 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1695 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1696 }
François Gaffiedfd74092015-03-19 12:10:59 +01001697 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001698
Eric Laurentc75307b2015-03-17 15:29:32 -07001699 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001700 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1701 return NO_ERROR;
1702}
1703
Eric Laurente0720872014-03-11 09:30:41 -07001704audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001705 const SortedVector<audio_io_handle_t>& outputs)
1706{
1707 // select one output among several suitable for global effects.
1708 // The priority is as follows:
1709 // 1: An offloaded output. If the effect ends up not being offloadable,
1710 // AudioFlinger will invalidate the track and the offloaded output
1711 // will be closed causing the effect to be moved to a PCM output.
1712 // 2: A deep buffer output
1713 // 3: the first output in the list
1714
1715 if (outputs.size() == 0) {
1716 return 0;
1717 }
1718
1719 audio_io_handle_t outputOffloaded = 0;
1720 audio_io_handle_t outputDeepBuffer = 0;
1721
1722 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001723 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001724 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001725 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1726 outputOffloaded = outputs[i];
1727 }
1728 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1729 outputDeepBuffer = outputs[i];
1730 }
1731 }
1732
1733 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1734 outputOffloaded, outputDeepBuffer);
1735 if (outputOffloaded != 0) {
1736 return outputOffloaded;
1737 }
1738 if (outputDeepBuffer != 0) {
1739 return outputDeepBuffer;
1740 }
1741
1742 return outputs[0];
1743}
1744
Eric Laurente0720872014-03-11 09:30:41 -07001745audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001746{
1747 // apply simple rule where global effects are attached to the same output as MUSIC streams
1748
Eric Laurent3b73df72014-03-11 09:06:29 -07001749 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001750 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1751 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1752
1753 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1754 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1755 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1756
1757 return output;
1758}
1759
Eric Laurente0720872014-03-11 09:30:41 -07001760status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001761 audio_io_handle_t io,
1762 uint32_t strategy,
1763 int session,
1764 int id)
1765{
1766 ssize_t index = mOutputs.indexOfKey(io);
1767 if (index < 0) {
1768 index = mInputs.indexOfKey(io);
1769 if (index < 0) {
1770 ALOGW("registerEffect() unknown io %d", io);
1771 return INVALID_OPERATION;
1772 }
1773 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001774 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001775}
1776
Eric Laurentc75307b2015-03-17 15:29:32 -07001777bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1778{
1779 return mOutputs.isStreamActive(stream, inPastMs);
1780}
1781
1782bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1783{
1784 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1785}
1786
Eric Laurente0720872014-03-11 09:30:41 -07001787bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001788{
1789 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001790 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001791 if (inputDescriptor->mRefCount == 0) {
1792 continue;
1793 }
1794 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001795 return true;
1796 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001797 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1798 // corresponds to an active capture triggered by a hardware hotword recognition
1799 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1800 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1801 // FIXME: we should not assume that the first session is the active one and keep
1802 // activity count per session. Same in startInput().
1803 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1804 if (index >= 0) {
1805 return true;
1806 }
1807 }
Eric Laurente552edb2014-03-10 17:42:56 -07001808 }
1809 return false;
1810}
1811
Eric Laurent275e8e92014-11-30 15:14:47 -08001812// Register a list of custom mixes with their attributes and format.
1813// When a mix is registered, corresponding input and output profiles are
1814// added to the remote submix hw module. The profile contains only the
1815// parameters (sampling rate, format...) specified by the mix.
1816// The corresponding input remote submix device is also connected.
1817//
1818// When a remote submix device is connected, the address is checked to select the
1819// appropriate profile and the corresponding input or output stream is opened.
1820//
1821// When capture starts, getInputForAttr() will:
1822// - 1 look for a mix matching the address passed in attribtutes tags if any
1823// - 2 if none found, getDeviceForInputSource() will:
1824// - 2.1 look for a mix matching the attributes source
1825// - 2.2 if none found, default to device selection by policy rules
1826// At this time, the corresponding output remote submix device is also connected
1827// and active playback use cases can be transferred to this mix if needed when reconnecting
1828// after AudioTracks are invalidated
1829//
1830// When playback starts, getOutputForAttr() will:
1831// - 1 look for a mix matching the address passed in attribtutes tags if any
1832// - 2 if none found, look for a mix matching the attributes usage
1833// - 3 if none found, default to device and output selection by policy rules.
1834
1835status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1836{
1837 sp<HwModule> module;
1838 for (size_t i = 0; i < mHwModules.size(); i++) {
1839 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1840 mHwModules[i]->mHandle != 0) {
1841 module = mHwModules[i];
1842 break;
1843 }
1844 }
1845
1846 if (module == 0) {
1847 return INVALID_OPERATION;
1848 }
1849
1850 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1851
1852 for (size_t i = 0; i < mixes.size(); i++) {
1853 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001854
1855 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001856 continue;
1857 }
1858 audio_config_t outputConfig = mixes[i].mFormat;
1859 audio_config_t inputConfig = mixes[i].mFormat;
1860 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1861 // stereo and let audio flinger do the channel conversion if needed.
1862 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1863 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1864 module->addOutputProfile(address, &outputConfig,
1865 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1866 module->addInputProfile(address, &inputConfig,
1867 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001868
Eric Laurentc722f302014-12-10 11:21:49 -08001869 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001870 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001871 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001872 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001873 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001874 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001875 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001876 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001877 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001878 }
1879 return NO_ERROR;
1880}
1881
1882status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1883{
1884 sp<HwModule> module;
1885 for (size_t i = 0; i < mHwModules.size(); i++) {
1886 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1887 mHwModules[i]->mHandle != 0) {
1888 module = mHwModules[i];
1889 break;
1890 }
1891 }
1892
1893 if (module == 0) {
1894 return INVALID_OPERATION;
1895 }
1896
1897 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1898
1899 for (size_t i = 0; i < mixes.size(); i++) {
1900 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001901
1902 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001903 continue;
1904 }
1905
Eric Laurentc722f302014-12-10 11:21:49 -08001906 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1907 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1908 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001909 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001910 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
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 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1915 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1916 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001917 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001918 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001919 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001920 }
1921 module->removeOutputProfile(address);
1922 module->removeInputProfile(address);
1923 }
1924 return NO_ERROR;
1925}
1926
Eric Laurente552edb2014-03-10 17:42:56 -07001927
Eric Laurente0720872014-03-11 09:30:41 -07001928status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001929{
1930 const size_t SIZE = 256;
1931 char buffer[SIZE];
1932 String8 result;
1933
1934 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1935 result.append(buffer);
1936
Eric Laurentc75307b2015-03-17 15:29:32 -07001937 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001938 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001939 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001940 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001941 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001942 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001943 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001944 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001945 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001946 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001947 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001948 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001949 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001950 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001951 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001952 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001953 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001954 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001955 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001956
François Gaffie53615e22015-03-19 09:24:12 +01001957 mAvailableOutputDevices.dump(fd, String8("output"));
1958 mAvailableInputDevices.dump(fd, String8("input"));
1959 mHwModules.dump(fd);
1960 mOutputs.dump(fd);
1961 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001962 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001963 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001964 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001965
1966 return NO_ERROR;
1967}
1968
1969// This function checks for the parameters which can be offloaded.
1970// This can be enhanced depending on the capability of the DSP and policy
1971// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001972bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001973{
1974 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001975 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001976 offloadInfo.sample_rate, offloadInfo.channel_mask,
1977 offloadInfo.format,
1978 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1979 offloadInfo.has_video);
1980
1981 // Check if offload has been disabled
1982 char propValue[PROPERTY_VALUE_MAX];
1983 if (property_get("audio.offload.disable", propValue, "0")) {
1984 if (atoi(propValue) != 0) {
1985 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1986 return false;
1987 }
1988 }
1989
1990 // Check if stream type is music, then only allow offload as of now.
1991 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1992 {
1993 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1994 return false;
1995 }
1996
1997 //TODO: enable audio offloading with video when ready
1998 if (offloadInfo.has_video)
1999 {
2000 ALOGV("isOffloadSupported: has_video == true, returning false");
2001 return false;
2002 }
2003
2004 //If duration is less than minimum value defined in property, return false
2005 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2006 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2007 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2008 return false;
2009 }
2010 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2011 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2012 return false;
2013 }
2014
2015 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2016 // creating an offloaded track and tearing it down immediately after start when audioflinger
2017 // detects there is an active non offloadable effect.
2018 // FIXME: We should check the audio session here but we do not have it in this context.
2019 // This may prevent offloading in rare situations where effects are left active by apps
2020 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002021 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002022 return false;
2023 }
2024
2025 // See if there is a profile to support this.
2026 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002027 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002028 offloadInfo.sample_rate,
2029 offloadInfo.format,
2030 offloadInfo.channel_mask,
2031 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002032 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2033 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002034}
2035
Eric Laurent6a94d692014-05-20 11:18:06 -07002036status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2037 audio_port_type_t type,
2038 unsigned int *num_ports,
2039 struct audio_port *ports,
2040 unsigned int *generation)
2041{
2042 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2043 generation == NULL) {
2044 return BAD_VALUE;
2045 }
2046 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2047 if (ports == NULL) {
2048 *num_ports = 0;
2049 }
2050
2051 size_t portsWritten = 0;
2052 size_t portsMax = *num_ports;
2053 *num_ports = 0;
2054 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2055 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2056 for (size_t i = 0;
2057 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2058 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2059 }
2060 *num_ports += mAvailableOutputDevices.size();
2061 }
2062 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2063 for (size_t i = 0;
2064 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2065 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2066 }
2067 *num_ports += mAvailableInputDevices.size();
2068 }
2069 }
2070 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2071 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2072 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2073 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2074 }
2075 *num_ports += mInputs.size();
2076 }
2077 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002078 size_t numOutputs = 0;
2079 for (size_t i = 0; i < mOutputs.size(); i++) {
2080 if (!mOutputs[i]->isDuplicated()) {
2081 numOutputs++;
2082 if (portsWritten < portsMax) {
2083 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2084 }
2085 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002086 }
Eric Laurent84c70242014-06-23 08:46:27 -07002087 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002088 }
2089 }
2090 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002091 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002092 return NO_ERROR;
2093}
2094
2095status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2096{
2097 return NO_ERROR;
2098}
2099
Eric Laurent6a94d692014-05-20 11:18:06 -07002100status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2101 audio_patch_handle_t *handle,
2102 uid_t uid)
2103{
2104 ALOGV("createAudioPatch()");
2105
2106 if (handle == NULL || patch == NULL) {
2107 return BAD_VALUE;
2108 }
2109 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2110
Eric Laurent874c42872014-08-08 15:13:39 -07002111 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2112 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2113 return BAD_VALUE;
2114 }
2115 // only one source per audio patch supported for now
2116 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002117 return INVALID_OPERATION;
2118 }
Eric Laurent874c42872014-08-08 15:13:39 -07002119
2120 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002121 return INVALID_OPERATION;
2122 }
Eric Laurent874c42872014-08-08 15:13:39 -07002123 for (size_t i = 0; i < patch->num_sinks; i++) {
2124 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2125 return INVALID_OPERATION;
2126 }
2127 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002128
2129 sp<AudioPatch> patchDesc;
2130 ssize_t index = mAudioPatches.indexOfKey(*handle);
2131
Eric Laurent6a94d692014-05-20 11:18:06 -07002132 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2133 patch->sources[0].role,
2134 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002135#if LOG_NDEBUG == 0
2136 for (size_t i = 0; i < patch->num_sinks; i++) {
2137 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2138 patch->sinks[i].role,
2139 patch->sinks[i].type);
2140 }
2141#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002142
2143 if (index >= 0) {
2144 patchDesc = mAudioPatches.valueAt(index);
2145 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2146 mUidCached, patchDesc->mUid, uid);
2147 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2148 return INVALID_OPERATION;
2149 }
2150 } else {
2151 *handle = 0;
2152 }
2153
2154 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002155 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002156 if (outputDesc == NULL) {
2157 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2158 return BAD_VALUE;
2159 }
Eric Laurent84c70242014-06-23 08:46:27 -07002160 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2161 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002162 if (patchDesc != 0) {
2163 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2164 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2165 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2166 return BAD_VALUE;
2167 }
2168 }
Eric Laurent874c42872014-08-08 15:13:39 -07002169 DeviceVector devices;
2170 for (size_t i = 0; i < patch->num_sinks; i++) {
2171 // Only support mix to devices connection
2172 // TODO add support for mix to mix connection
2173 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2174 ALOGV("createAudioPatch() source mix but sink is not a device");
2175 return INVALID_OPERATION;
2176 }
2177 sp<DeviceDescriptor> devDesc =
2178 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2179 if (devDesc == 0) {
2180 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2181 return BAD_VALUE;
2182 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002183
François Gaffie53615e22015-03-19 09:24:12 +01002184 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002185 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002186 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002187 NULL, // updatedSamplingRate
2188 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002189 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002190 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002191 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002192 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002193 ALOGV("createAudioPatch() profile not supported for device %08x",
2194 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002195 return INVALID_OPERATION;
2196 }
2197 devices.add(devDesc);
2198 }
2199 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002200 return INVALID_OPERATION;
2201 }
Eric Laurent874c42872014-08-08 15:13:39 -07002202
Eric Laurent6a94d692014-05-20 11:18:06 -07002203 // TODO: reconfigure output format and channels here
2204 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002205 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002206 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002207 index = mAudioPatches.indexOfKey(*handle);
2208 if (index >= 0) {
2209 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2210 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2211 }
2212 patchDesc = mAudioPatches.valueAt(index);
2213 patchDesc->mUid = uid;
2214 ALOGV("createAudioPatch() success");
2215 } else {
2216 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2217 return INVALID_OPERATION;
2218 }
2219 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2220 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2221 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002222 // only one sink supported when connecting an input device to a mix
2223 if (patch->num_sinks > 1) {
2224 return INVALID_OPERATION;
2225 }
François Gaffie53615e22015-03-19 09:24:12 +01002226 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002227 if (inputDesc == NULL) {
2228 return BAD_VALUE;
2229 }
2230 if (patchDesc != 0) {
2231 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2232 return BAD_VALUE;
2233 }
2234 }
2235 sp<DeviceDescriptor> devDesc =
2236 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2237 if (devDesc == 0) {
2238 return BAD_VALUE;
2239 }
2240
François Gaffie53615e22015-03-19 09:24:12 +01002241 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002242 devDesc->mAddress,
2243 patch->sinks[0].sample_rate,
2244 NULL, /*updatedSampleRate*/
2245 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002246 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002247 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002248 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002249 // FIXME for the parameter type,
2250 // and the NONE
2251 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002252 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002253 return INVALID_OPERATION;
2254 }
2255 // TODO: reconfigure output format and channels here
2256 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002257 devDesc->type(), inputDesc->mIoHandle);
2258 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002259 index = mAudioPatches.indexOfKey(*handle);
2260 if (index >= 0) {
2261 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2262 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2263 }
2264 patchDesc = mAudioPatches.valueAt(index);
2265 patchDesc->mUid = uid;
2266 ALOGV("createAudioPatch() success");
2267 } else {
2268 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2269 return INVALID_OPERATION;
2270 }
2271 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2272 // device to device connection
2273 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002274 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002275 return BAD_VALUE;
2276 }
2277 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002278 sp<DeviceDescriptor> srcDeviceDesc =
2279 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002280 if (srcDeviceDesc == 0) {
2281 return BAD_VALUE;
2282 }
Eric Laurent874c42872014-08-08 15:13:39 -07002283
Eric Laurent6a94d692014-05-20 11:18:06 -07002284 //update source and sink with our own data as the data passed in the patch may
2285 // be incomplete.
2286 struct audio_patch newPatch = *patch;
2287 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002288
Eric Laurent874c42872014-08-08 15:13:39 -07002289 for (size_t i = 0; i < patch->num_sinks; i++) {
2290 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2291 ALOGV("createAudioPatch() source device but one sink is not a device");
2292 return INVALID_OPERATION;
2293 }
2294
2295 sp<DeviceDescriptor> sinkDeviceDesc =
2296 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2297 if (sinkDeviceDesc == 0) {
2298 return BAD_VALUE;
2299 }
2300 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2301
Eric Laurent3bcf8592015-04-03 12:13:24 -07002302 // create a software bridge in PatchPanel if:
2303 // - source and sink devices are on differnt HW modules OR
2304 // - audio HAL version is < 3.0
2305 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2306 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2307 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002308 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002309 return INVALID_OPERATION;
2310 }
Eric Laurent874c42872014-08-08 15:13:39 -07002311 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002312 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002313 // if the sink device is reachable via an opened output stream, request to go via
2314 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002315 audio_io_handle_t output = selectOutput(outputs,
2316 AUDIO_OUTPUT_FLAG_NONE,
2317 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002318 if (output != AUDIO_IO_HANDLE_NONE) {
2319 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2320 if (outputDesc->isDuplicated()) {
2321 return INVALID_OPERATION;
2322 }
2323 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002324 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002325 newPatch.num_sources = 2;
2326 }
Eric Laurent83b88082014-06-20 18:31:16 -07002327 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002328 }
2329 // TODO: check from routing capabilities in config file and other conflicting patches
2330
2331 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2332 if (index >= 0) {
2333 afPatchHandle = patchDesc->mAfPatchHandle;
2334 }
2335
2336 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2337 &afPatchHandle,
2338 0);
2339 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2340 status, afPatchHandle);
2341 if (status == NO_ERROR) {
2342 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002343 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002344 addAudioPatch(patchDesc->mHandle, patchDesc);
2345 } else {
2346 patchDesc->mPatch = newPatch;
2347 }
2348 patchDesc->mAfPatchHandle = afPatchHandle;
2349 *handle = patchDesc->mHandle;
2350 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002351 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002352 } else {
2353 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2354 status);
2355 return INVALID_OPERATION;
2356 }
2357 } else {
2358 return BAD_VALUE;
2359 }
2360 } else {
2361 return BAD_VALUE;
2362 }
2363 return NO_ERROR;
2364}
2365
2366status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2367 uid_t uid)
2368{
2369 ALOGV("releaseAudioPatch() patch %d", handle);
2370
2371 ssize_t index = mAudioPatches.indexOfKey(handle);
2372
2373 if (index < 0) {
2374 return BAD_VALUE;
2375 }
2376 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2377 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2378 mUidCached, patchDesc->mUid, uid);
2379 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2380 return INVALID_OPERATION;
2381 }
2382
2383 struct audio_patch *patch = &patchDesc->mPatch;
2384 patchDesc->mUid = mUidCached;
2385 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002386 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002387 if (outputDesc == NULL) {
2388 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2389 return BAD_VALUE;
2390 }
2391
Eric Laurentc75307b2015-03-17 15:29:32 -07002392 setOutputDevice(outputDesc,
2393 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002394 true,
2395 0,
2396 NULL);
2397 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2398 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002399 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002400 if (inputDesc == NULL) {
2401 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2402 return BAD_VALUE;
2403 }
2404 setInputDevice(inputDesc->mIoHandle,
2405 getNewInputDevice(inputDesc->mIoHandle),
2406 true,
2407 NULL);
2408 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2409 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2410 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2411 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2412 status, patchDesc->mAfPatchHandle);
2413 removeAudioPatch(patchDesc->mHandle);
2414 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002415 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002416 } else {
2417 return BAD_VALUE;
2418 }
2419 } else {
2420 return BAD_VALUE;
2421 }
2422 return NO_ERROR;
2423}
2424
2425status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2426 struct audio_patch *patches,
2427 unsigned int *generation)
2428{
François Gaffie53615e22015-03-19 09:24:12 +01002429 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002430 return BAD_VALUE;
2431 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002433 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002434}
2435
Eric Laurente1715a42014-05-20 11:30:42 -07002436status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002437{
Eric Laurente1715a42014-05-20 11:30:42 -07002438 ALOGV("setAudioPortConfig()");
2439
2440 if (config == NULL) {
2441 return BAD_VALUE;
2442 }
2443 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2444 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002445 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2446 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002447 }
2448
Eric Laurenta121f902014-06-03 13:32:54 -07002449 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002450 if (config->type == AUDIO_PORT_TYPE_MIX) {
2451 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002452 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002453 if (outputDesc == NULL) {
2454 return BAD_VALUE;
2455 }
Eric Laurent84c70242014-06-23 08:46:27 -07002456 ALOG_ASSERT(!outputDesc->isDuplicated(),
2457 "setAudioPortConfig() called on duplicated output %d",
2458 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002459 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002460 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002461 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002462 if (inputDesc == NULL) {
2463 return BAD_VALUE;
2464 }
Eric Laurenta121f902014-06-03 13:32:54 -07002465 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002466 } else {
2467 return BAD_VALUE;
2468 }
2469 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2470 sp<DeviceDescriptor> deviceDesc;
2471 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2472 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2473 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2474 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2475 } else {
2476 return BAD_VALUE;
2477 }
2478 if (deviceDesc == NULL) {
2479 return BAD_VALUE;
2480 }
Eric Laurenta121f902014-06-03 13:32:54 -07002481 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002482 } else {
2483 return BAD_VALUE;
2484 }
2485
Eric Laurenta121f902014-06-03 13:32:54 -07002486 struct audio_port_config backupConfig;
2487 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2488 if (status == NO_ERROR) {
2489 struct audio_port_config newConfig;
2490 audioPortConfig->toAudioPortConfig(&newConfig, config);
2491 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002492 }
Eric Laurenta121f902014-06-03 13:32:54 -07002493 if (status != NO_ERROR) {
2494 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002495 }
Eric Laurente1715a42014-05-20 11:30:42 -07002496
2497 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002498}
2499
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002500void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2501{
2502 clearAudioPatches(uid);
2503 clearSessionRoutes(uid);
2504}
2505
Eric Laurent6a94d692014-05-20 11:18:06 -07002506void AudioPolicyManager::clearAudioPatches(uid_t uid)
2507{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002508 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002509 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2510 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002511 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002512 }
2513 }
2514}
2515
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002516
2517void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2518 audio_io_handle_t ouptutToSkip)
2519{
2520 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2521 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2522 for (size_t j = 0; j < mOutputs.size(); j++) {
2523 if (mOutputs.keyAt(j) == ouptutToSkip) {
2524 continue;
2525 }
2526 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2527 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2528 continue;
2529 }
2530 // If the default device for this strategy is on another output mix,
2531 // invalidate all tracks in this strategy to force re connection.
2532 // Otherwise select new device on the output mix.
2533 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2534 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2535 if (stream == AUDIO_STREAM_PATCH) {
2536 continue;
2537 }
2538 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2539 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2540 }
2541 }
2542 } else {
2543 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2544 setOutputDevice(outputDesc, newDevice, false);
2545 }
2546 }
2547}
2548
2549void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2550{
2551 // remove output routes associated with this uid
2552 SortedVector<routing_strategy> affectedStrategies;
2553 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2554 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2555 if (route->mUid == uid) {
2556 mOutputRoutes.removeItemsAt(i);
2557 if (route->mDeviceDescriptor != 0) {
2558 affectedStrategies.add(getStrategy(route->mStreamType));
2559 }
2560 }
2561 }
2562 // reroute outputs if necessary
2563 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2564 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2565 }
2566
2567 // remove input routes associated with this uid
2568 SortedVector<audio_source_t> affectedSources;
2569 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2570 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2571 if (route->mUid == uid) {
2572 mInputRoutes.removeItemsAt(i);
2573 if (route->mDeviceDescriptor != 0) {
2574 affectedSources.add(route->mSource);
2575 }
2576 }
2577 }
2578 // reroute inputs if necessary
2579 SortedVector<audio_io_handle_t> inputsToClose;
2580 for (size_t i = 0; i < mInputs.size(); i++) {
2581 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2582 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2583 inputsToClose.add(inputDesc->mIoHandle);
2584 }
2585 }
2586 for (size_t i = 0; i < inputsToClose.size(); i++) {
2587 closeInput(inputsToClose[i]);
2588 }
2589}
2590
2591
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002592status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2593 audio_io_handle_t *ioHandle,
2594 audio_devices_t *device)
2595{
2596 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2597 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002598 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002599
François Gaffiedf372692015-03-19 10:43:27 +01002600 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002601}
2602
Eric Laurent493404d2015-04-21 15:07:36 -07002603status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2604 const audio_attributes_t *attributes __unused,
2605 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002606{
2607 return INVALID_OPERATION;
2608}
2609
Eric Laurent493404d2015-04-21 15:07:36 -07002610status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002611{
2612 return INVALID_OPERATION;
2613}
2614
Eric Laurente552edb2014-03-10 17:42:56 -07002615// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002616// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002617// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002618uint32_t AudioPolicyManager::nextAudioPortGeneration()
2619{
2620 return android_atomic_inc(&mAudioPortGeneration);
2621}
2622
Eric Laurente0720872014-03-11 09:30:41 -07002623AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002624 :
2625#ifdef AUDIO_POLICY_TEST
2626 Thread(false),
2627#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002628 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002629 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002630 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002631 mAudioPortGeneration(1),
2632 mBeaconMuteRefCount(0),
2633 mBeaconPlayingRefCount(0),
2634 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002635{
François Gaffie2110e042015-03-24 08:41:51 +01002636 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2637 if (!engineInstance) {
2638 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2639 return;
2640 }
2641 // Retrieve the Policy Manager Interface
2642 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2643 if (mEngine == NULL) {
2644 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2645 return;
2646 }
2647 mEngine->setObserver(this);
2648 status_t status = mEngine->initCheck();
2649 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2650
Eric Laurent6a94d692014-05-20 11:18:06 -07002651 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002652 mpClientInterface = clientInterface;
2653
Eric Laurent7288ab82015-05-06 18:44:02 -07002654 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002655 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2656 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2657 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2658 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2659 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2660 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002661 ALOGE("could not load audio policy configuration file, setting defaults");
2662 defaultAudioPolicyConfig();
2663 }
2664 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002665 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002666
François Gaffie2110e042015-03-24 08:41:51 +01002667 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2668 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002669
2670 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002671 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2672 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002673 for (size_t i = 0; i < mHwModules.size(); i++) {
2674 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2675 if (mHwModules[i]->mHandle == 0) {
2676 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2677 continue;
2678 }
2679 // open all output streams needed to access attached devices
2680 // except for direct output streams that are only opened when they are actually
2681 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002682 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002683 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2684 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002685 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002686
Eric Laurent3a4311c2014-03-17 12:00:47 -07002687 if (outProfile->mSupportedDevices.isEmpty()) {
2688 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2689 continue;
2690 }
2691
Eric Laurentd78f1532014-09-16 16:38:20 -07002692 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2693 continue;
2694 }
Eric Laurent83b88082014-06-20 18:31:16 -07002695 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002696 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2697 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002698 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002699 // chose first device present in mSupportedDevices also part of
2700 // outputDeviceTypes
2701 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002702 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002703 if ((profileType & outputDeviceTypes) != 0) {
2704 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002705 }
Eric Laurente552edb2014-03-10 17:42:56 -07002706 }
2707 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002708 if ((profileType & outputDeviceTypes) == 0) {
2709 continue;
2710 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002711 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2712 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002713
2714 outputDesc->mDevice = profileType;
2715 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2716 config.sample_rate = outputDesc->mSamplingRate;
2717 config.channel_mask = outputDesc->mChannelMask;
2718 config.format = outputDesc->mFormat;
2719 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002720 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002721 &output,
2722 &config,
2723 &outputDesc->mDevice,
2724 String8(""),
2725 &outputDesc->mLatency,
2726 outputDesc->mFlags);
2727
2728 if (status != NO_ERROR) {
2729 ALOGW("Cannot open output stream for device %08x on hw module %s",
2730 outputDesc->mDevice,
2731 mHwModules[i]->mName);
2732 } else {
2733 outputDesc->mSamplingRate = config.sample_rate;
2734 outputDesc->mChannelMask = config.channel_mask;
2735 outputDesc->mFormat = config.format;
2736
2737 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002738 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002739 ssize_t index =
2740 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2741 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002742 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2743 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002744 }
2745 }
2746 if (mPrimaryOutput == 0 &&
2747 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002748 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002749 }
2750 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002751 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002752 outputDesc->mDevice,
2753 true);
2754 }
Eric Laurente552edb2014-03-10 17:42:56 -07002755 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002756 // open input streams needed to access attached devices to validate
2757 // mAvailableInputDevices list
2758 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2759 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002760 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002761
Eric Laurent3a4311c2014-03-17 12:00:47 -07002762 if (inProfile->mSupportedDevices.isEmpty()) {
2763 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2764 continue;
2765 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002766 // chose first device present in mSupportedDevices also part of
2767 // inputDeviceTypes
2768 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2769 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002770 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002771 if (profileType & inputDeviceTypes) {
2772 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002773 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002774 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002775 if ((profileType & inputDeviceTypes) == 0) {
2776 continue;
2777 }
2778 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2779
2780 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2781 inputDesc->mDevice = profileType;
2782
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002783 // find the address
2784 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2785 // the inputs vector must be of size 1, but we don't want to crash here
2786 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2787 : String8("");
2788 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2789 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2790
Eric Laurentd78f1532014-09-16 16:38:20 -07002791 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2792 config.sample_rate = inputDesc->mSamplingRate;
2793 config.channel_mask = inputDesc->mChannelMask;
2794 config.format = inputDesc->mFormat;
2795 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002796 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002797 &input,
2798 &config,
2799 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002800 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002801 AUDIO_SOURCE_MIC,
2802 AUDIO_INPUT_FLAG_NONE);
2803
2804 if (status == NO_ERROR) {
2805 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002806 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002807 ssize_t index =
2808 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2809 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002810 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2811 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002812 }
2813 }
2814 mpClientInterface->closeInput(input);
2815 } else {
2816 ALOGW("Cannot open input stream for device %08x on hw module %s",
2817 inputDesc->mDevice,
2818 mHwModules[i]->mName);
2819 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002820 }
2821 }
2822 // make sure all attached devices have been allocated a unique ID
2823 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002824 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002825 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002826 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2827 continue;
2828 }
François Gaffie2110e042015-03-24 08:41:51 +01002829 // The device is now validated and can be appended to the available devices of the engine
2830 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2831 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002832 i++;
2833 }
2834 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002835 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002836 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002837 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2838 continue;
2839 }
François Gaffie2110e042015-03-24 08:41:51 +01002840 // The device is now validated and can be appended to the available devices of the engine
2841 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2842 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002843 i++;
2844 }
2845 // make sure default device is reachable
2846 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002847 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002848 }
Eric Laurente552edb2014-03-10 17:42:56 -07002849
2850 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2851
2852 updateDevicesAndOutputs();
2853
2854#ifdef AUDIO_POLICY_TEST
2855 if (mPrimaryOutput != 0) {
2856 AudioParameter outputCmd = AudioParameter();
2857 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002858 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002859
2860 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2861 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002862 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2863 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002864 mTestLatencyMs = 0;
2865 mCurOutput = 0;
2866 mDirectOutput = false;
2867 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2868 mTestOutputs[i] = 0;
2869 }
2870
2871 const size_t SIZE = 256;
2872 char buffer[SIZE];
2873 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2874 run(buffer, ANDROID_PRIORITY_AUDIO);
2875 }
2876#endif //AUDIO_POLICY_TEST
2877}
2878
Eric Laurente0720872014-03-11 09:30:41 -07002879AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002880{
2881#ifdef AUDIO_POLICY_TEST
2882 exit();
2883#endif //AUDIO_POLICY_TEST
2884 for (size_t i = 0; i < mOutputs.size(); i++) {
2885 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002886 }
2887 for (size_t i = 0; i < mInputs.size(); i++) {
2888 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002889 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002890 mAvailableOutputDevices.clear();
2891 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002892 mOutputs.clear();
2893 mInputs.clear();
2894 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002895}
2896
Eric Laurente0720872014-03-11 09:30:41 -07002897status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002898{
2899 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2900}
2901
2902#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002903bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002904{
2905 ALOGV("entering threadLoop()");
2906 while (!exitPending())
2907 {
2908 String8 command;
2909 int valueInt;
2910 String8 value;
2911
2912 Mutex::Autolock _l(mLock);
2913 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2914
2915 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2916 AudioParameter param = AudioParameter(command);
2917
2918 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2919 valueInt != 0) {
2920 ALOGV("Test command %s received", command.string());
2921 String8 target;
2922 if (param.get(String8("target"), target) != NO_ERROR) {
2923 target = "Manager";
2924 }
2925 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2926 param.remove(String8("test_cmd_policy_output"));
2927 mCurOutput = valueInt;
2928 }
2929 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2930 param.remove(String8("test_cmd_policy_direct"));
2931 if (value == "false") {
2932 mDirectOutput = false;
2933 } else if (value == "true") {
2934 mDirectOutput = true;
2935 }
2936 }
2937 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2938 param.remove(String8("test_cmd_policy_input"));
2939 mTestInput = valueInt;
2940 }
2941
2942 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2943 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002944 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002945 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002946 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002947 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002948 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002949 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002950 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002951 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002952 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002953 if (target == "Manager") {
2954 mTestFormat = format;
2955 } else if (mTestOutputs[mCurOutput] != 0) {
2956 AudioParameter outputParam = AudioParameter();
2957 outputParam.addInt(String8("format"), format);
2958 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2959 }
2960 }
2961 }
2962 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2963 param.remove(String8("test_cmd_policy_channels"));
2964 int channels = 0;
2965
2966 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002967 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002968 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002969 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002970 }
2971 if (channels != 0) {
2972 if (target == "Manager") {
2973 mTestChannels = channels;
2974 } else if (mTestOutputs[mCurOutput] != 0) {
2975 AudioParameter outputParam = AudioParameter();
2976 outputParam.addInt(String8("channels"), channels);
2977 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2978 }
2979 }
2980 }
2981 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2982 param.remove(String8("test_cmd_policy_sampleRate"));
2983 if (valueInt >= 0 && valueInt <= 96000) {
2984 int samplingRate = valueInt;
2985 if (target == "Manager") {
2986 mTestSamplingRate = samplingRate;
2987 } else if (mTestOutputs[mCurOutput] != 0) {
2988 AudioParameter outputParam = AudioParameter();
2989 outputParam.addInt(String8("sampling_rate"), samplingRate);
2990 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2991 }
2992 }
2993 }
2994
2995 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2996 param.remove(String8("test_cmd_policy_reopen"));
2997
Eric Laurentc75307b2015-03-17 15:29:32 -07002998 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07002999
Eric Laurentc75307b2015-03-17 15:29:32 -07003000 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003001
Eric Laurentc75307b2015-03-17 15:29:32 -07003002 removeOutput(mPrimaryOutput->mIoHandle);
3003 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3004 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003005 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003006 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3007 config.sample_rate = outputDesc->mSamplingRate;
3008 config.channel_mask = outputDesc->mChannelMask;
3009 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003010 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003011 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003012 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003013 &config,
3014 &outputDesc->mDevice,
3015 String8(""),
3016 &outputDesc->mLatency,
3017 outputDesc->mFlags);
3018 if (status != NO_ERROR) {
3019 ALOGE("Failed to reopen hardware output stream, "
3020 "samplingRate: %d, format %d, channels %d",
3021 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003022 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003023 outputDesc->mSamplingRate = config.sample_rate;
3024 outputDesc->mChannelMask = config.channel_mask;
3025 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003026 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003027 AudioParameter outputCmd = AudioParameter();
3028 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003029 mpClientInterface->setParameters(handle, outputCmd.toString());
3030 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003031 }
3032 }
3033
3034
3035 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3036 }
3037 }
3038 return false;
3039}
3040
Eric Laurente0720872014-03-11 09:30:41 -07003041void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003042{
3043 {
3044 AutoMutex _l(mLock);
3045 requestExit();
3046 mWaitWorkCV.signal();
3047 }
3048 requestExitAndWait();
3049}
3050
Eric Laurente0720872014-03-11 09:30:41 -07003051int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003052{
3053 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3054 if (output == mTestOutputs[i]) return i;
3055 }
3056 return 0;
3057}
3058#endif //AUDIO_POLICY_TEST
3059
3060// ---
3061
Eric Laurentc75307b2015-03-17 15:29:32 -07003062void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003063{
François Gaffie98cc1912015-03-18 17:52:40 +01003064 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003065 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003066 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003067}
3068
François Gaffie53615e22015-03-19 09:24:12 +01003069void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3070{
3071 mOutputs.removeItem(output);
3072}
3073
Eric Laurent1f2f2232014-06-02 12:01:23 -07003074void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003075{
François Gaffie98cc1912015-03-18 17:52:40 +01003076 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003077 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003078 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003079}
Eric Laurente552edb2014-03-10 17:42:56 -07003080
Eric Laurentc75307b2015-03-17 15:29:32 -07003081void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003082 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003083 const String8 address /*in*/,
3084 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003085 sp<DeviceDescriptor> devDesc =
3086 desc->mProfile->mSupportedDevices.getDevice(device, address);
3087 if (devDesc != 0) {
3088 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3089 desc->mIoHandle, address.string());
3090 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003091 }
3092}
3093
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003094status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003095 audio_policy_dev_state_t state,
3096 SortedVector<audio_io_handle_t>& outputs,
3097 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003098{
François Gaffie53615e22015-03-19 09:24:12 +01003099 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003100 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003101 // erase all current sample rates, formats and channel masks
3102 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003103
Eric Laurent3b73df72014-03-11 09:06:29 -07003104 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003105 // first list already open outputs that can be routed to this device
3106 for (size_t i = 0; i < mOutputs.size(); i++) {
3107 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003108 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003109 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003110 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3111 outputs.add(mOutputs.keyAt(i));
3112 } else {
3113 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003114 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003115 }
Eric Laurente552edb2014-03-10 17:42:56 -07003116 }
3117 }
3118 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003119 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003120 for (size_t i = 0; i < mHwModules.size(); i++)
3121 {
3122 if (mHwModules[i]->mHandle == 0) {
3123 continue;
3124 }
3125 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3126 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003127 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3128 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003129 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003130 address == profile->mSupportedDevices[0]->mAddress) {
3131 profiles.add(profile);
3132 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3133 }
Eric Laurente552edb2014-03-10 17:42:56 -07003134 }
3135 }
3136 }
3137
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003138 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3139
Eric Laurente552edb2014-03-10 17:42:56 -07003140 if (profiles.isEmpty() && outputs.isEmpty()) {
3141 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3142 return BAD_VALUE;
3143 }
3144
3145 // open outputs for matching profiles if needed. Direct outputs are also opened to
3146 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3147 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003148 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003149
3150 // nothing to do if one output is already opened for this profile
3151 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003152 for (j = 0; j < outputs.size(); j++) {
3153 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003154 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003155 // matching profile: save the sample rates, format and channel masks supported
3156 // by the profile in our device descriptor
3157 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003158 break;
3159 }
3160 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003161 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003162 continue;
3163 }
3164
Eric Laurent83b88082014-06-20 18:31:16 -07003165 ALOGV("opening output for device %08x with params %s profile %p",
3166 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003167 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003168 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003169 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3170 config.sample_rate = desc->mSamplingRate;
3171 config.channel_mask = desc->mChannelMask;
3172 config.format = desc->mFormat;
3173 config.offload_info.sample_rate = desc->mSamplingRate;
3174 config.offload_info.channel_mask = desc->mChannelMask;
3175 config.offload_info.format = desc->mFormat;
3176 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003177 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003178 &output,
3179 &config,
3180 &desc->mDevice,
3181 address,
3182 &desc->mLatency,
3183 desc->mFlags);
3184 if (status == NO_ERROR) {
3185 desc->mSamplingRate = config.sample_rate;
3186 desc->mChannelMask = config.channel_mask;
3187 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003188
Eric Laurentd4692962014-05-05 18:13:44 -07003189 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003190 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003191 char *param = audio_device_address_to_parameter(device, address);
3192 mpClientInterface->setParameters(output, String8(param));
3193 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003194 }
3195
Eric Laurentd4692962014-05-05 18:13:44 -07003196 // Here is where we step through and resolve any "dynamic" fields
3197 String8 reply;
3198 char *value;
3199 if (profile->mSamplingRates[0] == 0) {
3200 reply = mpClientInterface->getParameters(output,
3201 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003202 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003203 reply.string());
3204 value = strpbrk((char *)reply.string(), "=");
3205 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003206 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003207 }
Eric Laurentd4692962014-05-05 18:13:44 -07003208 }
3209 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3210 reply = mpClientInterface->getParameters(output,
3211 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003212 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003213 reply.string());
3214 value = strpbrk((char *)reply.string(), "=");
3215 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003216 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003217 }
Eric Laurentd4692962014-05-05 18:13:44 -07003218 }
3219 if (profile->mChannelMasks[0] == 0) {
3220 reply = mpClientInterface->getParameters(output,
3221 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003222 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003223 reply.string());
3224 value = strpbrk((char *)reply.string(), "=");
3225 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003226 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003227 }
Eric Laurentd4692962014-05-05 18:13:44 -07003228 }
3229 if (((profile->mSamplingRates[0] == 0) &&
3230 (profile->mSamplingRates.size() < 2)) ||
3231 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3232 (profile->mFormats.size() < 2)) ||
3233 ((profile->mChannelMasks[0] == 0) &&
3234 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003235 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003236 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003237 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003238 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3239 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003240 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003241 config.sample_rate = profile->pickSamplingRate();
3242 config.channel_mask = profile->pickChannelMask();
3243 config.format = profile->pickFormat();
3244 config.offload_info.sample_rate = config.sample_rate;
3245 config.offload_info.channel_mask = config.channel_mask;
3246 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003247 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003248 &output,
3249 &config,
3250 &desc->mDevice,
3251 address,
3252 &desc->mLatency,
3253 desc->mFlags);
3254 if (status == NO_ERROR) {
3255 desc->mSamplingRate = config.sample_rate;
3256 desc->mChannelMask = config.channel_mask;
3257 desc->mFormat = config.format;
3258 } else {
3259 output = AUDIO_IO_HANDLE_NONE;
3260 }
Eric Laurentd4692962014-05-05 18:13:44 -07003261 }
3262
Eric Laurentcf2c0212014-07-25 16:20:43 -07003263 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003264 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003265 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003266 sp<AudioPolicyMix> policyMix;
3267 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003268 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3269 address.string());
3270 }
François Gaffie036e1e92015-03-19 10:16:24 +01003271 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003272 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003273
Eric Laurentc722f302014-12-10 11:21:49 -08003274 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3275 // no duplicated output for direct outputs and
3276 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003277 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003278
Eric Laurentd4692962014-05-05 18:13:44 -07003279 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003280 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003281
Eric Laurentd4692962014-05-05 18:13:44 -07003282 //TODO: configure audio effect output stage here
3283
3284 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003285 duplicatedOutput =
3286 mpClientInterface->openDuplicateOutput(output,
3287 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003288 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003289 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003290 sp<SwAudioOutputDescriptor> dupOutputDesc =
3291 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3292 dupOutputDesc->mOutput1 = mPrimaryOutput;
3293 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003294 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3295 dupOutputDesc->mFormat = desc->mFormat;
3296 dupOutputDesc->mChannelMask = desc->mChannelMask;
3297 dupOutputDesc->mLatency = desc->mLatency;
3298 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003299 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003300 } else {
3301 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003302 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003303 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003304 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003305 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003306 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003307 }
Eric Laurente552edb2014-03-10 17:42:56 -07003308 }
3309 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003310 } else {
3311 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003312 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003313 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003314 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003315 profiles.removeAt(profile_index);
3316 profile_index--;
3317 } else {
3318 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003319 devDesc->importAudioPort(profile);
3320
François Gaffie53615e22015-03-19 09:24:12 +01003321 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003322 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3323 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003324 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003325 NULL/*patch handle*/, address.string());
3326 }
Eric Laurente552edb2014-03-10 17:42:56 -07003327 ALOGV("checkOutputsForDevice(): adding output %d", output);
3328 }
3329 }
3330
3331 if (profiles.isEmpty()) {
3332 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3333 return BAD_VALUE;
3334 }
Eric Laurentd4692962014-05-05 18:13:44 -07003335 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003336 // check if one opened output is not needed any more after disconnecting one device
3337 for (size_t i = 0; i < mOutputs.size(); i++) {
3338 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003339 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003340 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003341 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003342 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003343 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003344 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003345 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3346 mOutputs.keyAt(i));
3347 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003348 }
Eric Laurente552edb2014-03-10 17:42:56 -07003349 }
3350 }
Eric Laurentd4692962014-05-05 18:13:44 -07003351 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003352 for (size_t i = 0; i < mHwModules.size(); i++)
3353 {
3354 if (mHwModules[i]->mHandle == 0) {
3355 continue;
3356 }
3357 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3358 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003359 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003360 if (profile->mSupportedDevices.types() & device) {
3361 ALOGV("checkOutputsForDevice(): "
3362 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003363 if (profile->mSamplingRates[0] == 0) {
3364 profile->mSamplingRates.clear();
3365 profile->mSamplingRates.add(0);
3366 }
3367 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3368 profile->mFormats.clear();
3369 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3370 }
3371 if (profile->mChannelMasks[0] == 0) {
3372 profile->mChannelMasks.clear();
3373 profile->mChannelMasks.add(0);
3374 }
3375 }
3376 }
3377 }
3378 }
3379 return NO_ERROR;
3380}
3381
Eric Laurentd4692962014-05-05 18:13:44 -07003382status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003383 audio_policy_dev_state_t state,
3384 SortedVector<audio_io_handle_t>& inputs,
3385 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003386{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003387 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003388 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3389 // first list already open inputs that can be routed to this device
3390 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3391 desc = mInputs.valueAt(input_index);
3392 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3393 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3394 inputs.add(mInputs.keyAt(input_index));
3395 }
3396 }
3397
3398 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003399 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003400 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3401 {
3402 if (mHwModules[module_idx]->mHandle == 0) {
3403 continue;
3404 }
3405 for (size_t profile_index = 0;
3406 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3407 profile_index++)
3408 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003409 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3410
3411 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003412 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003413 address == profile->mSupportedDevices[0]->mAddress) {
3414 profiles.add(profile);
3415 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3416 profile_index, module_idx);
3417 }
Eric Laurentd4692962014-05-05 18:13:44 -07003418 }
3419 }
3420 }
3421
3422 if (profiles.isEmpty() && inputs.isEmpty()) {
3423 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3424 return BAD_VALUE;
3425 }
3426
3427 // open inputs for matching profiles if needed. Direct inputs are also opened to
3428 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3429 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3430
Eric Laurent1c333e22014-05-20 10:48:17 -07003431 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003432 // nothing to do if one input is already opened for this profile
3433 size_t input_index;
3434 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3435 desc = mInputs.valueAt(input_index);
3436 if (desc->mProfile == profile) {
3437 break;
3438 }
3439 }
3440 if (input_index != mInputs.size()) {
3441 continue;
3442 }
3443
3444 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3445 desc = new AudioInputDescriptor(profile);
3446 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003447 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3448 config.sample_rate = desc->mSamplingRate;
3449 config.channel_mask = desc->mChannelMask;
3450 config.format = desc->mFormat;
3451 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003452 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003453 &input,
3454 &config,
3455 &desc->mDevice,
3456 address,
3457 AUDIO_SOURCE_MIC,
3458 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003459
Eric Laurentcf2c0212014-07-25 16:20:43 -07003460 if (status == NO_ERROR) {
3461 desc->mSamplingRate = config.sample_rate;
3462 desc->mChannelMask = config.channel_mask;
3463 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003464
Eric Laurentd4692962014-05-05 18:13:44 -07003465 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003466 char *param = audio_device_address_to_parameter(device, address);
3467 mpClientInterface->setParameters(input, String8(param));
3468 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003469 }
3470
3471 // Here is where we step through and resolve any "dynamic" fields
3472 String8 reply;
3473 char *value;
3474 if (profile->mSamplingRates[0] == 0) {
3475 reply = mpClientInterface->getParameters(input,
3476 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3477 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3478 reply.string());
3479 value = strpbrk((char *)reply.string(), "=");
3480 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003481 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003482 }
3483 }
3484 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3485 reply = mpClientInterface->getParameters(input,
3486 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3487 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3488 value = strpbrk((char *)reply.string(), "=");
3489 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003490 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003491 }
3492 }
3493 if (profile->mChannelMasks[0] == 0) {
3494 reply = mpClientInterface->getParameters(input,
3495 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3496 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3497 reply.string());
3498 value = strpbrk((char *)reply.string(), "=");
3499 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003500 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003501 }
3502 }
3503 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3504 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3505 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3506 ALOGW("checkInputsForDevice() direct input missing param");
3507 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003508 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003509 }
3510
3511 if (input != 0) {
3512 addInput(input, desc);
3513 }
3514 } // endif input != 0
3515
Eric Laurentcf2c0212014-07-25 16:20:43 -07003516 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003517 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003518 profiles.removeAt(profile_index);
3519 profile_index--;
3520 } else {
3521 inputs.add(input);
3522 ALOGV("checkInputsForDevice(): adding input %d", input);
3523 }
3524 } // end scan profiles
3525
3526 if (profiles.isEmpty()) {
3527 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3528 return BAD_VALUE;
3529 }
3530 } else {
3531 // Disconnect
3532 // check if one opened input is not needed any more after disconnecting one device
3533 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3534 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003535 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3536 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003537 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3538 mInputs.keyAt(input_index));
3539 inputs.add(mInputs.keyAt(input_index));
3540 }
3541 }
3542 // Clear any profiles associated with the disconnected device.
3543 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3544 if (mHwModules[module_index]->mHandle == 0) {
3545 continue;
3546 }
3547 for (size_t profile_index = 0;
3548 profile_index < mHwModules[module_index]->mInputProfiles.size();
3549 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003550 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003551 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003552 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003553 profile_index, module_index);
3554 if (profile->mSamplingRates[0] == 0) {
3555 profile->mSamplingRates.clear();
3556 profile->mSamplingRates.add(0);
3557 }
3558 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3559 profile->mFormats.clear();
3560 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3561 }
3562 if (profile->mChannelMasks[0] == 0) {
3563 profile->mChannelMasks.clear();
3564 profile->mChannelMasks.add(0);
3565 }
3566 }
3567 }
3568 }
3569 } // end disconnect
3570
3571 return NO_ERROR;
3572}
3573
3574
Eric Laurente0720872014-03-11 09:30:41 -07003575void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003576{
3577 ALOGV("closeOutput(%d)", output);
3578
Eric Laurentc75307b2015-03-17 15:29:32 -07003579 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003580 if (outputDesc == NULL) {
3581 ALOGW("closeOutput() unknown output %d", output);
3582 return;
3583 }
François Gaffie036e1e92015-03-19 10:16:24 +01003584 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003585
Eric Laurente552edb2014-03-10 17:42:56 -07003586 // look for duplicated outputs connected to the output being removed.
3587 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003588 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003589 if (dupOutputDesc->isDuplicated() &&
3590 (dupOutputDesc->mOutput1 == outputDesc ||
3591 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003592 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003593 if (dupOutputDesc->mOutput1 == outputDesc) {
3594 outputDesc2 = dupOutputDesc->mOutput2;
3595 } else {
3596 outputDesc2 = dupOutputDesc->mOutput1;
3597 }
3598 // As all active tracks on duplicated output will be deleted,
3599 // and as they were also referenced on the other output, the reference
3600 // count for their stream type must be adjusted accordingly on
3601 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003602 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003603 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003604 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003605 }
3606 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3607 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3608
3609 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003610 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003611 }
3612 }
3613
Eric Laurent05b90f82014-08-27 15:32:29 -07003614 nextAudioPortGeneration();
3615
3616 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3617 if (index >= 0) {
3618 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3619 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3620 mAudioPatches.removeItemsAt(index);
3621 mpClientInterface->onAudioPatchListUpdate();
3622 }
3623
Eric Laurente552edb2014-03-10 17:42:56 -07003624 AudioParameter param;
3625 param.add(String8("closing"), String8("true"));
3626 mpClientInterface->setParameters(output, param.toString());
3627
3628 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003629 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003630 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003631}
3632
3633void AudioPolicyManager::closeInput(audio_io_handle_t input)
3634{
3635 ALOGV("closeInput(%d)", input);
3636
3637 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3638 if (inputDesc == NULL) {
3639 ALOGW("closeInput() unknown input %d", input);
3640 return;
3641 }
3642
Eric Laurent6a94d692014-05-20 11:18:06 -07003643 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003644
3645 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3646 if (index >= 0) {
3647 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3648 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3649 mAudioPatches.removeItemsAt(index);
3650 mpClientInterface->onAudioPatchListUpdate();
3651 }
3652
3653 mpClientInterface->closeInput(input);
3654 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003655}
3656
Eric Laurentc75307b2015-03-17 15:29:32 -07003657SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3658 audio_devices_t device,
3659 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003660{
3661 SortedVector<audio_io_handle_t> outputs;
3662
3663 ALOGVV("getOutputsForDevice() device %04x", device);
3664 for (size_t i = 0; i < openOutputs.size(); i++) {
3665 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003666 i, openOutputs.valueAt(i)->isDuplicated(),
3667 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003668 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3669 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3670 outputs.add(openOutputs.keyAt(i));
3671 }
3672 }
3673 return outputs;
3674}
3675
Eric Laurente0720872014-03-11 09:30:41 -07003676bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003677 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003678{
3679 if (outputs1.size() != outputs2.size()) {
3680 return false;
3681 }
3682 for (size_t i = 0; i < outputs1.size(); i++) {
3683 if (outputs1[i] != outputs2[i]) {
3684 return false;
3685 }
3686 }
3687 return true;
3688}
3689
Eric Laurente0720872014-03-11 09:30:41 -07003690void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003691{
3692 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3693 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3694 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3695 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3696
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003697 // also take into account external policy-related changes: add all outputs which are
3698 // associated with policies in the "before" and "after" output vectors
3699 ALOGVV("checkOutputForStrategy(): policy related outputs");
3700 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003701 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003702 if (desc != 0 && desc->mPolicyMix != NULL) {
3703 srcOutputs.add(desc->mIoHandle);
3704 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3705 }
3706 }
3707 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003708 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003709 if (desc != 0 && desc->mPolicyMix != NULL) {
3710 dstOutputs.add(desc->mIoHandle);
3711 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3712 }
3713 }
3714
Eric Laurente552edb2014-03-10 17:42:56 -07003715 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3716 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3717 strategy, srcOutputs[0], dstOutputs[0]);
3718 // mute strategy while moving tracks from one output to another
3719 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003720 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003721 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003722 setStrategyMute(strategy, true, desc);
3723 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003724 }
3725 }
3726
3727 // Move effects associated to this strategy from previous output to new output
3728 if (strategy == STRATEGY_MEDIA) {
3729 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3730 SortedVector<audio_io_handle_t> moved;
3731 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003732 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3733 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3734 effectDesc->mIo != fxOutput) {
3735 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003736 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3737 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003738 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003739 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003740 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003741 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003742 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003743 }
3744 }
3745 }
3746 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003747 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003748 if (i == AUDIO_STREAM_PATCH) {
3749 continue;
3750 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003751 if (getStrategy((audio_stream_type_t)i) == strategy) {
3752 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003753 }
3754 }
3755 }
3756}
3757
Eric Laurente0720872014-03-11 09:30:41 -07003758void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003759{
François Gaffie2110e042015-03-24 08:41:51 +01003760 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003761 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003762 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003763 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003764 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003765 checkOutputForStrategy(STRATEGY_SONIFICATION);
3766 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003767 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003768 checkOutputForStrategy(STRATEGY_MEDIA);
3769 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003770 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003771}
3772
Eric Laurente0720872014-03-11 09:30:41 -07003773void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003774{
François Gaffie53615e22015-03-19 09:24:12 +01003775 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003776 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003777 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003778 return;
3779 }
3780
Eric Laurent3a4311c2014-03-17 12:00:47 -07003781 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003782 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3783 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3784 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003785 // suspend A2DP output if:
3786 // (NOT already suspended) &&
3787 // ((SCO device is connected &&
3788 // (forced usage for communication || for record is SCO))) ||
3789 // (phone state is ringing || in call)
3790 //
3791 // restore A2DP output if:
3792 // (Already suspended) &&
3793 // ((SCO device is NOT connected ||
3794 // (forced usage NOT for communication && NOT for record is SCO))) &&
3795 // (phone state is NOT ringing && NOT in call)
3796 //
3797 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003798 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003799 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3800 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3801 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3802 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003803
3804 mpClientInterface->restoreOutput(a2dpOutput);
3805 mA2dpSuspended = false;
3806 }
3807 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003808 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003809 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3810 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3811 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3812 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003813
3814 mpClientInterface->suspendOutput(a2dpOutput);
3815 mA2dpSuspended = true;
3816 }
3817 }
3818}
3819
Eric Laurentc75307b2015-03-17 15:29:32 -07003820audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3821 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003822{
3823 audio_devices_t device = AUDIO_DEVICE_NONE;
3824
Eric Laurent6a94d692014-05-20 11:18:06 -07003825 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3826 if (index >= 0) {
3827 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3828 if (patchDesc->mUid != mUidCached) {
3829 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3830 outputDesc->device(), outputDesc->mPatchHandle);
3831 return outputDesc->device();
3832 }
3833 }
3834
Eric Laurente552edb2014-03-10 17:42:56 -07003835 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003836 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003837 // use device for strategy enforced audible
3838 // 2: we are in call or the strategy phone is active on the output:
3839 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003840 // 3: the strategy for enforced audible is active but not enforced on the output:
3841 // use the device for strategy enforced audible
3842 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003843 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003844 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003845 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003846 // 6: the strategy accessibility is active on the output:
3847 // use device for strategy accessibility
3848 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003849 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003850 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003851 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003852 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003853 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003854 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003855 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003856 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3857 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003858 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003859 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003860 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003861 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003862 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003863 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003864 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003865 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003866 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003867 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003868 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003869 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003870 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003871 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003872 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003873 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003874 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003875 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003876 }
3877
Eric Laurent1c333e22014-05-20 10:48:17 -07003878 ALOGV("getNewOutputDevice() selected device %x", device);
3879 return device;
3880}
3881
3882audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3883{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003884 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003885
3886 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3887 if (index >= 0) {
3888 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3889 if (patchDesc->mUid != mUidCached) {
3890 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3891 inputDesc->mDevice, inputDesc->mPatchHandle);
3892 return inputDesc->mDevice;
3893 }
3894 }
3895
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003896 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003897
Eric Laurente552edb2014-03-10 17:42:56 -07003898 return device;
3899}
3900
Eric Laurente0720872014-03-11 09:30:41 -07003901uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003902 return (uint32_t)getStrategy(stream);
3903}
3904
Eric Laurente0720872014-03-11 09:30:41 -07003905audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003906 // By checking the range of stream before calling getStrategy, we avoid
3907 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3908 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003909 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003910 return AUDIO_DEVICE_NONE;
3911 }
3912 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003913 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003914 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3915 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3916 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003917 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003918 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003919 devices = outputDesc->device();
3920 break;
3921 }
Eric Laurente552edb2014-03-10 17:42:56 -07003922 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003923
3924 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3925 and doesn't really need to.*/
3926 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3927 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3928 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3929 }
3930
Eric Laurente552edb2014-03-10 17:42:56 -07003931 return devices;
3932}
3933
François Gaffie2110e042015-03-24 08:41:51 +01003934routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3935{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003936 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003937 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003938}
3939
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003940uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3941 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003942 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3943 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3944 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003945 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3946 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3947 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003948 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003949 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003950}
3951
Eric Laurente0720872014-03-11 09:30:41 -07003952void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003953 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003954 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003955 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3956 updateDevicesAndOutputs();
3957 break;
3958 default:
3959 break;
3960 }
3961}
3962
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003963uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3964 switch(event) {
3965 case STARTING_OUTPUT:
3966 mBeaconMuteRefCount++;
3967 break;
3968 case STOPPING_OUTPUT:
3969 if (mBeaconMuteRefCount > 0) {
3970 mBeaconMuteRefCount--;
3971 }
3972 break;
3973 case STARTING_BEACON:
3974 mBeaconPlayingRefCount++;
3975 break;
3976 case STOPPING_BEACON:
3977 if (mBeaconPlayingRefCount > 0) {
3978 mBeaconPlayingRefCount--;
3979 }
3980 break;
3981 }
3982
3983 if (mBeaconMuteRefCount > 0) {
3984 // any playback causes beacon to be muted
3985 return setBeaconMute(true);
3986 } else {
3987 // no other playback: unmute when beacon starts playing, mute when it stops
3988 return setBeaconMute(mBeaconPlayingRefCount == 0);
3989 }
3990}
3991
3992uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3993 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3994 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3995 // keep track of muted state to avoid repeating mute/unmute operations
3996 if (mBeaconMuted != mute) {
3997 // mute/unmute AUDIO_STREAM_TTS on all outputs
3998 ALOGV("\t muting %d", mute);
3999 uint32_t maxLatency = 0;
4000 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004001 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004002 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004003 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004004 0 /*delay*/, AUDIO_DEVICE_NONE);
4005 const uint32_t latency = desc->latency() * 2;
4006 if (latency > maxLatency) {
4007 maxLatency = latency;
4008 }
4009 }
4010 mBeaconMuted = mute;
4011 return maxLatency;
4012 }
4013 return 0;
4014}
4015
Eric Laurente0720872014-03-11 09:30:41 -07004016audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004017 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004018{
Paul McLeanaa981192015-03-21 09:55:15 -07004019 // Routing
4020 // see if we have an explicit route
4021 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4022 // (getStrategy(stream)).
4023 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4024 // and activity count is non-zero
4025 // the device = the device from the descriptor in the RouteMap, and exit.
4026 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4027 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4028 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004029 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004030 return route->mDeviceDescriptor->type();
4031 }
4032 }
4033
Eric Laurente552edb2014-03-10 17:42:56 -07004034 if (fromCache) {
4035 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4036 strategy, mDeviceForStrategy[strategy]);
4037 return mDeviceForStrategy[strategy];
4038 }
François Gaffie2110e042015-03-24 08:41:51 +01004039 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004040}
4041
Eric Laurente0720872014-03-11 09:30:41 -07004042void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004043{
4044 for (int i = 0; i < NUM_STRATEGIES; i++) {
4045 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4046 }
4047 mPreviousOutputs = mOutputs;
4048}
4049
Eric Laurent1f2f2232014-06-02 12:01:23 -07004050uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004051 audio_devices_t prevDevice,
4052 uint32_t delayMs)
4053{
4054 // mute/unmute strategies using an incompatible device combination
4055 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4056 // if unmuting, unmute only after the specified delay
4057 if (outputDesc->isDuplicated()) {
4058 return 0;
4059 }
4060
4061 uint32_t muteWaitMs = 0;
4062 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004063 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004064
4065 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4066 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004067 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004068 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4069 bool doMute = false;
4070
4071 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4072 doMute = true;
4073 outputDesc->mStrategyMutedByDevice[i] = true;
4074 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4075 doMute = true;
4076 outputDesc->mStrategyMutedByDevice[i] = false;
4077 }
Eric Laurent99401132014-05-07 19:48:15 -07004078 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004079 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004080 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004081 // skip output if it does not share any device with current output
4082 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4083 == AUDIO_DEVICE_NONE) {
4084 continue;
4085 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004086 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4087 mute ? "muting" : "unmuting", i, curDevice);
4088 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004089 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004090 if (mute) {
4091 // FIXME: should not need to double latency if volume could be applied
4092 // immediately by the audioflinger mixer. We must account for the delay
4093 // between now and the next time the audioflinger thread for this output
4094 // will process a buffer (which corresponds to one buffer size,
4095 // usually 1/2 or 1/4 of the latency).
4096 if (muteWaitMs < desc->latency() * 2) {
4097 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004098 }
4099 }
4100 }
4101 }
4102 }
4103 }
4104
Eric Laurent99401132014-05-07 19:48:15 -07004105 // temporary mute output if device selection changes to avoid volume bursts due to
4106 // different per device volumes
4107 if (outputDesc->isActive() && (device != prevDevice)) {
4108 if (muteWaitMs < outputDesc->latency() * 2) {
4109 muteWaitMs = outputDesc->latency() * 2;
4110 }
4111 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004112 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004113 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004114 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004115 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004116 muteWaitMs *2, device);
4117 }
4118 }
4119 }
4120
Eric Laurente552edb2014-03-10 17:42:56 -07004121 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4122 if (muteWaitMs > delayMs) {
4123 muteWaitMs -= delayMs;
4124 usleep(muteWaitMs * 1000);
4125 return muteWaitMs;
4126 }
4127 return 0;
4128}
4129
Eric Laurentc75307b2015-03-17 15:29:32 -07004130uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004131 audio_devices_t device,
4132 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004133 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004134 audio_patch_handle_t *patchHandle,
4135 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004136{
Eric Laurentc75307b2015-03-17 15:29:32 -07004137 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004138 AudioParameter param;
4139 uint32_t muteWaitMs;
4140
4141 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004142 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4143 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004144 return muteWaitMs;
4145 }
4146 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4147 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004148 if ((device != AUDIO_DEVICE_NONE) &&
4149 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004150 return 0;
4151 }
4152
4153 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004154 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004155
4156 audio_devices_t prevDevice = outputDesc->mDevice;
4157
Paul McLeanaa981192015-03-21 09:55:15 -07004158 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004159
4160 if (device != AUDIO_DEVICE_NONE) {
4161 outputDesc->mDevice = device;
4162 }
4163 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4164
4165 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004166 // the requested device is AUDIO_DEVICE_NONE
4167 // OR the requested device is the same as current device
4168 // AND force is not specified
4169 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004170 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004171 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4172 !force &&
4173 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004174 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004175 return muteWaitMs;
4176 }
4177
4178 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004179
Eric Laurente552edb2014-03-10 17:42:56 -07004180 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004181 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004182 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004183 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004184 DeviceVector deviceList = (address == NULL) ?
4185 mAvailableOutputDevices.getDevicesFromType(device)
4186 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004187 if (!deviceList.isEmpty()) {
4188 struct audio_patch patch;
4189 outputDesc->toAudioPortConfig(&patch.sources[0]);
4190 patch.num_sources = 1;
4191 patch.num_sinks = 0;
4192 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4193 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004194 patch.num_sinks++;
4195 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004196 ssize_t index;
4197 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4198 index = mAudioPatches.indexOfKey(*patchHandle);
4199 } else {
4200 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4201 }
4202 sp< AudioPatch> patchDesc;
4203 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4204 if (index >= 0) {
4205 patchDesc = mAudioPatches.valueAt(index);
4206 afPatchHandle = patchDesc->mAfPatchHandle;
4207 }
4208
Eric Laurent1c333e22014-05-20 10:48:17 -07004209 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004210 &afPatchHandle,
4211 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004212 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4213 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004214 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004215 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004216 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004217 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004218 addAudioPatch(patchDesc->mHandle, patchDesc);
4219 } else {
4220 patchDesc->mPatch = patch;
4221 }
4222 patchDesc->mAfPatchHandle = afPatchHandle;
4223 patchDesc->mUid = mUidCached;
4224 if (patchHandle) {
4225 *patchHandle = patchDesc->mHandle;
4226 }
4227 outputDesc->mPatchHandle = patchDesc->mHandle;
4228 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004229 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004230 }
4231 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004232
4233 // inform all input as well
4234 for (size_t i = 0; i < mInputs.size(); i++) {
4235 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004236 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004237 AudioParameter inputCmd = AudioParameter();
4238 ALOGV("%s: inform input %d of device:%d", __func__,
4239 inputDescriptor->mIoHandle, device);
4240 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4241 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4242 inputCmd.toString(),
4243 delayMs);
4244 }
4245 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004246 }
Eric Laurente552edb2014-03-10 17:42:56 -07004247
4248 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004249 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004250
4251 return muteWaitMs;
4252}
4253
Eric Laurentc75307b2015-03-17 15:29:32 -07004254status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004255 int delayMs,
4256 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004257{
Eric Laurent6a94d692014-05-20 11:18:06 -07004258 ssize_t index;
4259 if (patchHandle) {
4260 index = mAudioPatches.indexOfKey(*patchHandle);
4261 } else {
4262 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4263 }
4264 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004265 return INVALID_OPERATION;
4266 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004267 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4268 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004269 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4270 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004271 removeAudioPatch(patchDesc->mHandle);
4272 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004273 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004274 return status;
4275}
4276
4277status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4278 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004279 bool force,
4280 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004281{
4282 status_t status = NO_ERROR;
4283
Eric Laurent1f2f2232014-06-02 12:01:23 -07004284 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004285 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4286 inputDesc->mDevice = device;
4287
4288 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4289 if (!deviceList.isEmpty()) {
4290 struct audio_patch patch;
4291 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004292 // AUDIO_SOURCE_HOTWORD is for internal use only:
4293 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004294 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4295 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004296 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4297 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004298 patch.num_sinks = 1;
4299 //only one input device for now
4300 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004301 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004302 ssize_t index;
4303 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4304 index = mAudioPatches.indexOfKey(*patchHandle);
4305 } else {
4306 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4307 }
4308 sp< AudioPatch> patchDesc;
4309 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4310 if (index >= 0) {
4311 patchDesc = mAudioPatches.valueAt(index);
4312 afPatchHandle = patchDesc->mAfPatchHandle;
4313 }
4314
Eric Laurent1c333e22014-05-20 10:48:17 -07004315 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004316 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004317 0);
4318 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004319 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004320 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004321 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004322 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004323 addAudioPatch(patchDesc->mHandle, patchDesc);
4324 } else {
4325 patchDesc->mPatch = patch;
4326 }
4327 patchDesc->mAfPatchHandle = afPatchHandle;
4328 patchDesc->mUid = mUidCached;
4329 if (patchHandle) {
4330 *patchHandle = patchDesc->mHandle;
4331 }
4332 inputDesc->mPatchHandle = patchDesc->mHandle;
4333 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004334 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004335 }
4336 }
4337 }
4338 return status;
4339}
4340
Eric Laurent6a94d692014-05-20 11:18:06 -07004341status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4342 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004343{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004344 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004345 ssize_t index;
4346 if (patchHandle) {
4347 index = mAudioPatches.indexOfKey(*patchHandle);
4348 } else {
4349 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4350 }
4351 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004352 return INVALID_OPERATION;
4353 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004354 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4355 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004356 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4357 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004358 removeAudioPatch(patchDesc->mHandle);
4359 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004360 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004361 return status;
4362}
4363
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004364sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004365 String8 address,
4366 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004367 audio_format_t& format,
4368 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004369 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004370{
4371 // Choose an input profile based on the requested capture parameters: select the first available
4372 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004373 //
4374 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4375 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004376
4377 for (size_t i = 0; i < mHwModules.size(); i++)
4378 {
4379 if (mHwModules[i]->mHandle == 0) {
4380 continue;
4381 }
4382 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4383 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004384 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004385 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004386 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004387 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004388 format,
4389 &format /*updatedFormat*/,
4390 channelMask,
4391 &channelMask /*updatedChannelMask*/,
4392 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004393
Eric Laurente552edb2014-03-10 17:42:56 -07004394 return profile;
4395 }
4396 }
4397 }
4398 return NULL;
4399}
4400
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004401
4402audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004403 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004404{
François Gaffie036e1e92015-03-19 10:16:24 +01004405 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4406 audio_devices_t selectedDeviceFromMix =
4407 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004408
François Gaffie036e1e92015-03-19 10:16:24 +01004409 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4410 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004411 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004412 return getDeviceForInputSource(inputSource);
4413}
4414
4415audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4416{
Paul McLean466dc8e2015-04-17 13:15:36 -06004417 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4418 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004419 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004420 return route->mDeviceDescriptor->type();
4421 }
4422 }
4423
4424 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004425}
4426
Eric Laurente0720872014-03-11 09:30:41 -07004427float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004428 int index,
4429 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004430{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004431 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004432
4433 // if a headset is connected, apply the following rules to ring tones and notifications
4434 // to avoid sound level bursts in user's ears:
4435 // - always attenuate ring tones and notifications volume by 6dB
4436 // - if music is playing, always limit the volume to current music volume,
4437 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004438 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004439 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4440 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4441 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4442 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4443 ((stream_strategy == STRATEGY_SONIFICATION)
4444 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004445 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004446 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004447 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4448 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004449 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004450 // when the phone is ringing we must consider that music could have been paused just before
4451 // by the music application and behave as if music was active if the last music track was
4452 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004453 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004454 mLimitRingtoneVolume) {
4455 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004456 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4457 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004458 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004459 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4460 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4461 if (volumeDb > minVolDB) {
4462 volumeDb = minVolDB;
4463 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004464 }
4465 }
4466 }
4467
Eric Laurentffbc80f2015-03-18 18:30:19 -07004468 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004469}
4470
Eric Laurente0720872014-03-11 09:30:41 -07004471status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004472 int index,
4473 const sp<AudioOutputDescriptor>& outputDesc,
4474 audio_devices_t device,
4475 int delayMs,
4476 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004477{
Eric Laurente552edb2014-03-10 17:42:56 -07004478 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004479 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004480 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004481 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004482 return NO_ERROR;
4483 }
François Gaffie2110e042015-03-24 08:41:51 +01004484 audio_policy_forced_cfg_t forceUseForComm =
4485 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004486 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004487 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4488 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004489 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004490 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004491 return INVALID_OPERATION;
4492 }
4493
Eric Laurentc75307b2015-03-17 15:29:32 -07004494 if (device == AUDIO_DEVICE_NONE) {
4495 device = outputDesc->device();
4496 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004497
Eric Laurentffbc80f2015-03-18 18:30:19 -07004498 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004499 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004500 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004501 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004502
Eric Laurentffbc80f2015-03-18 18:30:19 -07004503 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004504
Eric Laurent3b73df72014-03-11 09:06:29 -07004505 if (stream == AUDIO_STREAM_VOICE_CALL ||
4506 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004507 float voiceVolume;
4508 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004509 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004510 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004511 } else {
4512 voiceVolume = 1.0;
4513 }
4514
Eric Laurentc75307b2015-03-17 15:29:32 -07004515 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004516 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4517 mLastVoiceVolume = voiceVolume;
4518 }
4519 }
4520
4521 return NO_ERROR;
4522}
4523
Eric Laurentc75307b2015-03-17 15:29:32 -07004524void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4525 audio_devices_t device,
4526 int delayMs,
4527 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004528{
Eric Laurentc75307b2015-03-17 15:29:32 -07004529 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004530
Eric Laurent3b73df72014-03-11 09:06:29 -07004531 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004532 if (stream == AUDIO_STREAM_PATCH) {
4533 continue;
4534 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004535 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004536 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4537 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004538 device,
4539 delayMs,
4540 force);
4541 }
4542}
4543
Eric Laurente0720872014-03-11 09:30:41 -07004544void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004545 bool on,
4546 const sp<AudioOutputDescriptor>& outputDesc,
4547 int delayMs,
4548 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004549{
4550 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004551 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004552 if (stream == AUDIO_STREAM_PATCH) {
4553 continue;
4554 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004555 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004556 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004557 }
4558 }
4559}
4560
Eric Laurente0720872014-03-11 09:30:41 -07004561void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004562 bool on,
4563 const sp<AudioOutputDescriptor>& outputDesc,
4564 int delayMs,
4565 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004566{
Eric Laurentc75307b2015-03-17 15:29:32 -07004567 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004568 if (device == AUDIO_DEVICE_NONE) {
4569 device = outputDesc->device();
4570 }
4571
Eric Laurentc75307b2015-03-17 15:29:32 -07004572 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4573 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004574
4575 if (on) {
4576 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004577 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004578 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004579 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004580 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004581 }
4582 }
4583 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4584 outputDesc->mMuteCount[stream]++;
4585 } else {
4586 if (outputDesc->mMuteCount[stream] == 0) {
4587 ALOGV("setStreamMute() unmuting non muted stream!");
4588 return;
4589 }
4590 if (--outputDesc->mMuteCount[stream] == 0) {
4591 checkAndSetVolume(stream,
4592 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004593 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004594 device,
4595 delayMs);
4596 }
4597 }
4598}
4599
Eric Laurente0720872014-03-11 09:30:41 -07004600void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004601 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004602{
4603 // if the stream pertains to sonification strategy and we are in call we must
4604 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4605 // in the device used for phone strategy and play the tone if the selected device does not
4606 // interfere with the device used for phone strategy
4607 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4608 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004609 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004610 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4611 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004612 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004613 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4614 stream, starting, outputDesc->mDevice, stateChange);
4615 if (outputDesc->mRefCount[stream]) {
4616 int muteCount = 1;
4617 if (stateChange) {
4618 muteCount = outputDesc->mRefCount[stream];
4619 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004620 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004621 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4622 for (int i = 0; i < muteCount; i++) {
4623 setStreamMute(stream, starting, mPrimaryOutput);
4624 }
4625 } else {
4626 ALOGV("handleIncallSonification() high visibility");
4627 if (outputDesc->device() &
4628 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4629 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4630 for (int i = 0; i < muteCount; i++) {
4631 setStreamMute(stream, starting, mPrimaryOutput);
4632 }
4633 }
4634 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004635 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4636 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004637 } else {
4638 mpClientInterface->stopTone();
4639 }
4640 }
4641 }
4642 }
4643}
4644
Paul McLeanaa981192015-03-21 09:55:15 -07004645// --- SessionRoute class implementation
4646void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004647 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4648 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004649 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4650 mRefCount, mActivityCount);
4651}
4652
4653// --- SessionRouteMap class implementation
4654bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4655{
4656 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4657}
4658
Eric Laurent493404d2015-04-21 15:07:36 -07004659bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4660{
4661 if (indexOfKey(session) >= 0) {
4662 if (valueFor(session)->mChanged) {
4663 valueFor(session)->mChanged = false;
4664 return true;
4665 }
4666 }
4667 return false;
4668}
4669
Paul McLeanaa981192015-03-21 09:55:15 -07004670void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4671{
4672 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4673 if (route != 0) {
4674 ALOG_ASSERT(route->mRefCount > 0);
4675 --route->mRefCount;
4676 if (route->mRefCount <= 0) {
4677 removeItem(session);
4678 }
4679 }
4680}
4681
4682int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4683{
4684 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4685 return route != 0 ? ++(route->mActivityCount) : -1;
4686}
4687
4688int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4689{
4690 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4691 if (route != 0 && route->mActivityCount > 0) {
4692 return --(route->mActivityCount);
4693 } else {
4694 return -1;
4695 }
4696}
4697
4698void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4699 ALOGI("%s ----", caption);
4700 for(size_t index = 0; index < size(); index++) {
4701 valueAt(index)->log(" ");
4702 }
4703}
4704
Paul McLean466dc8e2015-04-17 13:15:36 -06004705void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4706 audio_stream_type_t streamType,
4707 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004708 sp<DeviceDescriptor> descriptor,
4709 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004710{
4711 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4712 ALOGE("Adding Output Route to InputRouteMap");
4713 return;
4714 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4715 ALOGE("Adding Input Route to OutputRouteMap");
4716 return;
4717 }
4718
4719 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4720
4721 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004722 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4723 ((route->mDeviceDescriptor != 0) &&
4724 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004725 route->mChanged = true;
4726 }
4727 route->mRefCount++;
4728 route->mDeviceDescriptor = descriptor;
4729 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004730 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004731 route->mRefCount++;
4732 add(session, route);
4733 if (descriptor != 0) {
4734 route->mChanged = true;
4735 }
4736 }
4737}
4738
Eric Laurente0720872014-03-11 09:30:41 -07004739void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004740{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004741 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004742 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004743 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004744 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004745 mAvailableOutputDevices.add(mDefaultOutputDevice);
4746 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004747
4748 module = new HwModule("primary");
4749
Eric Laurent322b4d22015-04-03 15:57:54 -07004750 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4751 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004752 profile->mSamplingRates.add(44100);
4753 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4754 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004755 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004756 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4757 module->mOutputProfiles.add(profile);
4758
Eric Laurent322b4d22015-04-03 15:57:54 -07004759 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4760 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004761 profile->mSamplingRates.add(8000);
4762 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4763 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004764 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004765 module->mInputProfiles.add(profile);
4766
4767 mHwModules.add(module);
4768}
4769
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004770audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4771{
4772 // flags to stream type mapping
4773 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4774 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4775 }
4776 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4777 return AUDIO_STREAM_BLUETOOTH_SCO;
4778 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004779 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4780 return AUDIO_STREAM_TTS;
4781 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004782
4783 // usage to stream type mapping
4784 switch (attr->usage) {
4785 case AUDIO_USAGE_MEDIA:
4786 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004787 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4788 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004789 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004790 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4791 return AUDIO_STREAM_ALARM;
4792 }
4793 if (isStreamActive(AUDIO_STREAM_RING)) {
4794 return AUDIO_STREAM_RING;
4795 }
4796 if (isInCall()) {
4797 return AUDIO_STREAM_VOICE_CALL;
4798 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004799 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004800 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4801 return AUDIO_STREAM_SYSTEM;
4802 case AUDIO_USAGE_VOICE_COMMUNICATION:
4803 return AUDIO_STREAM_VOICE_CALL;
4804
4805 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4806 return AUDIO_STREAM_DTMF;
4807
4808 case AUDIO_USAGE_ALARM:
4809 return AUDIO_STREAM_ALARM;
4810 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4811 return AUDIO_STREAM_RING;
4812
4813 case AUDIO_USAGE_NOTIFICATION:
4814 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4815 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4816 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4817 case AUDIO_USAGE_NOTIFICATION_EVENT:
4818 return AUDIO_STREAM_NOTIFICATION;
4819
4820 case AUDIO_USAGE_UNKNOWN:
4821 default:
4822 return AUDIO_STREAM_MUSIC;
4823 }
4824}
Eric Laurente83b55d2014-11-14 10:06:21 -08004825
François Gaffie53615e22015-03-19 09:24:12 +01004826bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4827{
Eric Laurente83b55d2014-11-14 10:06:21 -08004828 // has flags that map to a strategy?
4829 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4830 return true;
4831 }
4832
4833 // has known usage?
4834 switch (paa->usage) {
4835 case AUDIO_USAGE_UNKNOWN:
4836 case AUDIO_USAGE_MEDIA:
4837 case AUDIO_USAGE_VOICE_COMMUNICATION:
4838 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4839 case AUDIO_USAGE_ALARM:
4840 case AUDIO_USAGE_NOTIFICATION:
4841 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4842 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4843 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4844 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4845 case AUDIO_USAGE_NOTIFICATION_EVENT:
4846 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4847 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4848 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4849 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004850 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004851 break;
4852 default:
4853 return false;
4854 }
4855 return true;
4856}
4857
François Gaffiead3183e2015-03-18 16:55:35 +01004858bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4859 routing_strategy strategy, uint32_t inPastMs,
4860 nsecs_t sysTime) const
4861{
4862 if ((sysTime == 0) && (inPastMs != 0)) {
4863 sysTime = systemTime();
4864 }
4865 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4866 if (i == AUDIO_STREAM_PATCH) {
4867 continue;
4868 }
4869 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4870 (NUM_STRATEGIES == strategy)) &&
4871 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4872 return true;
4873 }
4874 }
4875 return false;
4876}
4877
François Gaffie2110e042015-03-24 08:41:51 +01004878audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4879{
4880 return mEngine->getForceUse(usage);
4881}
4882
4883bool AudioPolicyManager::isInCall()
4884{
4885 return isStateInCall(mEngine->getPhoneState());
4886}
4887
4888bool AudioPolicyManager::isStateInCall(int state)
4889{
4890 return is_state_in_call(state);
4891}
4892
Eric Laurente552edb2014-03-10 17:42:56 -07004893}; // namespace android