blob: b5ff9a90cccdc6fb65e84a555f938414e39b6a6d [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];
Eric Laurent275e8e92014-11-30 15:14:47 -0800588 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700589 NULL /*updatedSamplingRate*/, format, channelMask,
590 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
591 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700592 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
593 return profile;
594 }
Eric Laurente552edb2014-03-10 17:42:56 -0700595 }
596 }
597 return 0;
598}
599
Eric Laurente0720872014-03-11 09:30:41 -0700600audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100601 uint32_t samplingRate,
602 audio_format_t format,
603 audio_channel_mask_t channelMask,
604 audio_output_flags_t flags,
605 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700606{
Eric Laurent3b73df72014-03-11 09:06:29 -0700607 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700608 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
609 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
610 device, stream, samplingRate, format, channelMask, flags);
611
Eric Laurente83b55d2014-11-14 10:06:21 -0800612 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
613 stream, samplingRate,format, channelMask,
614 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700615}
616
Eric Laurente83b55d2014-11-14 10:06:21 -0800617status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
618 audio_io_handle_t *output,
619 audio_session_t session,
620 audio_stream_type_t *stream,
621 uint32_t samplingRate,
622 audio_format_t format,
623 audio_channel_mask_t channelMask,
624 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700625 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800626 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700627{
Eric Laurente83b55d2014-11-14 10:06:21 -0800628 audio_attributes_t attributes;
629 if (attr != NULL) {
630 if (!isValidAttributes(attr)) {
631 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
632 attr->usage, attr->content_type, attr->flags,
633 attr->tags);
634 return BAD_VALUE;
635 }
636 attributes = *attr;
637 } else {
638 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
639 ALOGE("getOutputForAttr(): invalid stream type");
640 return BAD_VALUE;
641 }
642 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700643 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700644 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100645 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
646 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
647 if (!audio_is_linear_pcm(format)) {
648 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800649 }
François Gaffie036e1e92015-03-19 10:16:24 +0100650 *stream = streamTypefromAttributesInt(&attributes);
651 *output = desc->mIoHandle;
652 ALOGV("getOutputForAttr() returns output %d", *output);
653 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800654 }
655 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
656 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
657 return BAD_VALUE;
658 }
659
Eric Laurent93c3d412014-08-01 14:48:35 -0700660 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
François Gaffie53615e22015-03-19 09:24:12 +0100661 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700662
Eric Laurente83b55d2014-11-14 10:06:21 -0800663 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700664 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700665
Eric Laurente83b55d2014-11-14 10:06:21 -0800666 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700667 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
668 }
669
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700670 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700671 device, samplingRate, format, channelMask, flags);
672
Eric Laurente83b55d2014-11-14 10:06:21 -0800673 *stream = streamTypefromAttributesInt(&attributes);
674 *output = getOutputForDevice(device, session, *stream,
675 samplingRate, format, channelMask,
676 flags, offloadInfo);
677 if (*output == AUDIO_IO_HANDLE_NONE) {
678 return INVALID_OPERATION;
679 }
Paul McLeanaa981192015-03-21 09:55:15 -0700680
681 // Explicit routing?
682 sp<DeviceDescriptor> deviceDesc;
683
684 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent322b4d22015-04-03 15:57:54 -0700685 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
Paul McLeanaa981192015-03-21 09:55:15 -0700686 deviceDesc = mAvailableOutputDevices[i];
687 break;
688 }
689 }
690 mOutputRoutes.addRoute(session, *stream, deviceDesc);
Eric Laurente83b55d2014-11-14 10:06:21 -0800691 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700692}
693
694audio_io_handle_t AudioPolicyManager::getOutputForDevice(
695 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800696 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700697 audio_stream_type_t stream,
698 uint32_t samplingRate,
699 audio_format_t format,
700 audio_channel_mask_t channelMask,
701 audio_output_flags_t flags,
702 const audio_offload_info_t *offloadInfo)
703{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700704 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700705 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700706 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707
Eric Laurente552edb2014-03-10 17:42:56 -0700708#ifdef AUDIO_POLICY_TEST
709 if (mCurOutput != 0) {
710 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
711 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
712
713 if (mTestOutputs[mCurOutput] == 0) {
714 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700715 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
716 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700717 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700718 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700719 outputDesc->mFlags =
720 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700721 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700722 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
723 config.sample_rate = mTestSamplingRate;
724 config.channel_mask = mTestChannels;
725 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700726 if (offloadInfo != NULL) {
727 config.offload_info = *offloadInfo;
728 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700729 status = mpClientInterface->openOutput(0,
730 &mTestOutputs[mCurOutput],
731 &config,
732 &outputDesc->mDevice,
733 String8(""),
734 &outputDesc->mLatency,
735 outputDesc->mFlags);
736 if (status == NO_ERROR) {
737 outputDesc->mSamplingRate = config.sample_rate;
738 outputDesc->mFormat = config.format;
739 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700740 AudioParameter outputCmd = AudioParameter();
741 outputCmd.addInt(String8("set_id"),mCurOutput);
742 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
743 addOutput(mTestOutputs[mCurOutput], outputDesc);
744 }
745 }
746 return mTestOutputs[mCurOutput];
747 }
748#endif //AUDIO_POLICY_TEST
749
750 // open a direct output if required by specified parameters
751 //force direct flag if offload flag is set: offloading implies a direct output stream
752 // and all common behaviors are driven by checking only the direct flag
753 // this should normally be set appropriately in the policy configuration file
754 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700755 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700756 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700757 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
758 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
759 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800760 // only allow deep buffering for music stream type
761 if (stream != AUDIO_STREAM_MUSIC) {
762 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
763 }
Eric Laurente552edb2014-03-10 17:42:56 -0700764
Eric Laurentb732cf52014-09-24 19:08:21 -0700765 sp<IOProfile> profile;
766
767 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700768 // and not explicitly requested
769 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
770 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700771 audio_channel_count_from_out_mask(channelMask) <= 2) {
772 goto non_direct_output;
773 }
774
Eric Laurente552edb2014-03-10 17:42:56 -0700775 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
776 // creating an offloaded track and tearing it down immediately after start when audioflinger
777 // detects there is an active non offloadable effect.
778 // FIXME: We should check the audio session here but we do not have it in this context.
779 // This may prevent offloading in rare situations where effects are left active by apps
780 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700781
Eric Laurente552edb2014-03-10 17:42:56 -0700782 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100783 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700784 profile = getProfileForDirectOutput(device,
785 samplingRate,
786 format,
787 channelMask,
788 (audio_output_flags_t)flags);
789 }
790
Eric Laurent1c333e22014-05-20 10:48:17 -0700791 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700792 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700793
794 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700795 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700796 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
797 outputDesc = desc;
798 // reuse direct output if currently open and configured with same parameters
799 if ((samplingRate == outputDesc->mSamplingRate) &&
800 (format == outputDesc->mFormat) &&
801 (channelMask == outputDesc->mChannelMask)) {
802 outputDesc->mDirectOpenCount++;
803 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
804 return mOutputs.keyAt(i);
805 }
806 }
807 }
808 // close direct output if currently open and configured with different parameters
809 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700810 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700811 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700812 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700813 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700814 outputDesc->mLatency = 0;
815 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700816 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
817 config.sample_rate = samplingRate;
818 config.channel_mask = channelMask;
819 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700820 if (offloadInfo != NULL) {
821 config.offload_info = *offloadInfo;
822 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700823 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700824 &output,
825 &config,
826 &outputDesc->mDevice,
827 String8(""),
828 &outputDesc->mLatency,
829 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700830
831 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700832 if (status != NO_ERROR ||
833 (samplingRate != 0 && samplingRate != config.sample_rate) ||
834 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
835 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700836 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
837 "format %d %d, channelMask %04x %04x", output, samplingRate,
838 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
839 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700840 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700841 mpClientInterface->closeOutput(output);
842 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800843 // fall back to mixer output if possible when the direct output could not be open
844 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
845 goto non_direct_output;
846 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800847 // fall back to mixer output if possible when the direct output could not be open
848 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
849 goto non_direct_output;
850 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700851 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700852 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700853 outputDesc->mSamplingRate = config.sample_rate;
854 outputDesc->mChannelMask = config.channel_mask;
855 outputDesc->mFormat = config.format;
856 outputDesc->mRefCount[stream] = 0;
857 outputDesc->mStopTime[stream] = 0;
858 outputDesc->mDirectOpenCount = 1;
859
Eric Laurente552edb2014-03-10 17:42:56 -0700860 audio_io_handle_t srcOutput = getOutputForEffect();
861 addOutput(output, outputDesc);
862 audio_io_handle_t dstOutput = getOutputForEffect();
863 if (dstOutput == output) {
864 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
865 }
866 mPreviousOutputs = mOutputs;
867 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700868 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700869 return output;
870 }
871
Eric Laurentb732cf52014-09-24 19:08:21 -0700872non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700873 // ignoring channel mask due to downmix capability in mixer
874
875 // open a non direct output
876
877 // for non direct outputs, only PCM is supported
878 if (audio_is_linear_pcm(format)) {
879 // get which output is suitable for the specified stream. The actual
880 // routing change will happen when startOutput() will be called
881 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
882
Eric Laurent8838a382014-09-08 16:44:28 -0700883 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
884 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
885 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700886 }
887 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
888 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
889
Paul McLeanaa981192015-03-21 09:55:15 -0700890 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700891
892 return output;
893}
894
Eric Laurente0720872014-03-11 09:30:41 -0700895audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700896 audio_output_flags_t flags,
897 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700898{
899 // select one output among several that provide a path to a particular device or set of
900 // devices (the list was previously build by getOutputsForDevice()).
901 // The priority is as follows:
902 // 1: the output with the highest number of requested policy flags
903 // 2: the primary output
904 // 3: the first output in the list
905
906 if (outputs.size() == 0) {
907 return 0;
908 }
909 if (outputs.size() == 1) {
910 return outputs[0];
911 }
912
913 int maxCommonFlags = 0;
914 audio_io_handle_t outputFlags = 0;
915 audio_io_handle_t outputPrimary = 0;
916
917 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700918 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700919 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700920 // if a valid format is specified, skip output if not compatible
921 if (format != AUDIO_FORMAT_INVALID) {
922 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
923 if (format != outputDesc->mFormat) {
924 continue;
925 }
926 } else if (!audio_is_linear_pcm(format)) {
927 continue;
928 }
929 }
930
Eric Laurent3b73df72014-03-11 09:06:29 -0700931 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700932 if (commonFlags > maxCommonFlags) {
933 outputFlags = outputs[i];
934 maxCommonFlags = commonFlags;
935 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
936 }
937 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
938 outputPrimary = outputs[i];
939 }
940 }
941 }
942
943 if (outputFlags != 0) {
944 return outputFlags;
945 }
946 if (outputPrimary != 0) {
947 return outputPrimary;
948 }
949
950 return outputs[0];
951}
952
Eric Laurente0720872014-03-11 09:30:41 -0700953status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700954 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800955 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700956{
Paul McLeanaa981192015-03-21 09:55:15 -0700957 ALOGV("startOutput() output %d, stream %d, session %d",
958 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700959 ssize_t index = mOutputs.indexOfKey(output);
960 if (index < 0) {
961 ALOGW("startOutput() unknown output %d", output);
962 return BAD_VALUE;
963 }
964
Eric Laurentc75307b2015-03-17 15:29:32 -0700965 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
966
967 audio_devices_t newDevice;
968 if (outputDesc->mPolicyMix != NULL) {
969 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
970 } else {
971 newDevice = AUDIO_DEVICE_NONE;
972 }
973
974 uint32_t delayMs = 0;
975
976 // Routing?
977 mOutputRoutes.incRouteActivity(session);
978
979 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
980
981 if (status != NO_ERROR) {
982 mOutputRoutes.decRouteActivity(session);
983 }
984 // Automatically enable the remote submix input when output is started on a re routing mix
985 // of type MIX_TYPE_RECORDERS
986 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
987 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
988 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
989 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
990 outputDesc->mPolicyMix->mRegistrationId,
991 "remote-submix");
992 }
993
994 if (delayMs != 0) {
995 usleep(delayMs * 1000);
996 }
997
998 return status;
999}
1000
1001status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1002 audio_stream_type_t stream,
1003 audio_devices_t device,
1004 uint32_t *delayMs)
1005{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001006 // cannot start playback of STREAM_TTS if any other output is being used
1007 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001008
1009 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001010 if (stream == AUDIO_STREAM_TTS) {
1011 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001012 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001013 return INVALID_OPERATION;
1014 } else {
1015 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1016 }
1017 } else {
1018 // some playback other than beacon starts
1019 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1020 }
1021
Eric Laurente552edb2014-03-10 17:42:56 -07001022 // increment usage count for this stream on the requested output:
1023 // NOTE that the usage count is the same for duplicated output and hardware output which is
1024 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1025 outputDesc->changeRefCount(stream, 1);
1026
1027 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001028 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001029 if (device == AUDIO_DEVICE_NONE) {
1030 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001031 }
Eric Laurente552edb2014-03-10 17:42:56 -07001032 routing_strategy strategy = getStrategy(stream);
1033 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001034 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1035 (beaconMuteLatency > 0);
1036 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001037 bool force = false;
1038 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001039 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001040 if (desc != outputDesc) {
1041 // force a device change if any other output is managed by the same hw
1042 // module and has a current device selection that differs from selected device.
1043 // In this case, the audio HAL must receive the new device selection so that it can
1044 // change the device currently selected by the other active output.
1045 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001046 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001047 force = true;
1048 }
1049 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001050 // a notification so that audio focus effect can propagate, or that a mute/unmute
1051 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001052 uint32_t latency = desc->latency();
1053 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1054 waitMs = latency;
1055 }
1056 }
1057 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001058 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001059
1060 // handle special case for sonification while in call
1061 if (isInCall()) {
1062 handleIncallSonification(stream, true, false);
1063 }
1064
1065 // apply volume rules for current stream and device if necessary
1066 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001067 mStreams.valueFor(stream).getVolumeIndex(device),
1068 outputDesc,
1069 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001070
1071 // update the outputs if starting an output with a stream that can affect notification
1072 // routing
1073 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001074
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001075 // force reevaluating accessibility routing when ringtone or alarm starts
1076 if (strategy == STRATEGY_SONIFICATION) {
1077 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1078 }
Eric Laurente552edb2014-03-10 17:42:56 -07001079 }
1080 return NO_ERROR;
1081}
1082
1083
Eric Laurente0720872014-03-11 09:30:41 -07001084status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001085 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001086 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001087{
1088 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1089 ssize_t index = mOutputs.indexOfKey(output);
1090 if (index < 0) {
1091 ALOGW("stopOutput() unknown output %d", output);
1092 return BAD_VALUE;
1093 }
1094
Eric Laurentc75307b2015-03-17 15:29:32 -07001095 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001096
Eric Laurentc75307b2015-03-17 15:29:32 -07001097 if (outputDesc->mRefCount[stream] == 1) {
1098 // Automatically disable the remote submix input when output is stopped on a
1099 // re routing mix of type MIX_TYPE_RECORDERS
1100 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1101 outputDesc->mPolicyMix != NULL &&
1102 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1103 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1104 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1105 outputDesc->mPolicyMix->mRegistrationId,
1106 "remote-submix");
1107 }
1108 }
1109
1110 // Routing?
1111 if (outputDesc->mRefCount[stream] > 0) {
1112 mOutputRoutes.decRouteActivity(session);
1113 }
1114
1115 return stopSource(outputDesc, stream);
1116}
1117
1118status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
1119 audio_stream_type_t stream)
1120{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001121 // always handle stream stop, check which stream type is stopping
1122 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1123
Eric Laurente552edb2014-03-10 17:42:56 -07001124 // handle special case for sonification while in call
1125 if (isInCall()) {
1126 handleIncallSonification(stream, false, false);
1127 }
1128
1129 if (outputDesc->mRefCount[stream] > 0) {
1130 // decrement usage count of this stream on the output
1131 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001132
Eric Laurente552edb2014-03-10 17:42:56 -07001133 // store time at which the stream was stopped - see isStreamActive()
1134 if (outputDesc->mRefCount[stream] == 0) {
1135 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001136 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001137 // delay the device switch by twice the latency because stopOutput() is executed when
1138 // the track stop() command is received and at that time the audio track buffer can
1139 // still contain data that needs to be drained. The latency only covers the audio HAL
1140 // and kernel buffers. Also the latency does not always include additional delay in the
1141 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001142 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001143
1144 // force restoring the device selection on other active outputs if it differs from the
1145 // one being selected for this output
1146 for (size_t i = 0; i < mOutputs.size(); i++) {
1147 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001148 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001149 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001150 desc->isActive() &&
1151 outputDesc->sharesHwModuleWith(desc) &&
1152 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001153 setOutputDevice(desc,
1154 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001155 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001156 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001157 }
1158 }
1159 // update the outputs if stopping one with a stream that can affect notification routing
1160 handleNotificationRoutingForStream(stream);
1161 }
1162 return NO_ERROR;
1163 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001164 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001165 return INVALID_OPERATION;
1166 }
1167}
1168
Eric Laurente83b55d2014-11-14 10:06:21 -08001169void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001170 audio_stream_type_t stream __unused,
1171 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001172{
1173 ALOGV("releaseOutput() %d", output);
1174 ssize_t index = mOutputs.indexOfKey(output);
1175 if (index < 0) {
1176 ALOGW("releaseOutput() releasing unknown output %d", output);
1177 return;
1178 }
1179
1180#ifdef AUDIO_POLICY_TEST
1181 int testIndex = testOutputIndex(output);
1182 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001183 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001184 if (outputDesc->isActive()) {
1185 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001186 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001187 mTestOutputs[testIndex] = 0;
1188 }
1189 return;
1190 }
1191#endif //AUDIO_POLICY_TEST
1192
Paul McLeanaa981192015-03-21 09:55:15 -07001193 // Routing
1194 mOutputRoutes.removeRoute(session);
1195
Eric Laurentc75307b2015-03-17 15:29:32 -07001196 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001197 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001198 if (desc->mDirectOpenCount <= 0) {
1199 ALOGW("releaseOutput() invalid open count %d for output %d",
1200 desc->mDirectOpenCount, output);
1201 return;
1202 }
1203 if (--desc->mDirectOpenCount == 0) {
1204 closeOutput(output);
1205 // If effects where present on the output, audioflinger moved them to the primary
1206 // output by default: move them back to the appropriate output.
1207 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001208 if (dstOutput != mPrimaryOutput->mIoHandle) {
1209 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1210 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001211 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001212 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001213 }
1214 }
1215}
1216
1217
Eric Laurentcaf7f482014-11-25 17:50:47 -08001218status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1219 audio_io_handle_t *input,
1220 audio_session_t session,
1221 uint32_t samplingRate,
1222 audio_format_t format,
1223 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001224 audio_input_flags_t flags,
1225 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001226{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001227 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1228 "session %d, flags %#x",
1229 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001230
Eric Laurentcaf7f482014-11-25 17:50:47 -08001231 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001232 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001233 audio_devices_t device;
1234 // handle legacy remote submix case where the address was not always specified
1235 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001236 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001237 audio_source_t inputSource = attr->source;
1238 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001239 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001240
Eric Laurentc447ded2015-01-06 08:47:05 -08001241 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1242 inputSource = AUDIO_SOURCE_MIC;
1243 }
1244 halInputSource = inputSource;
1245
1246 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001247 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
François Gaffie036e1e92015-03-19 10:16:24 +01001248 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, policyMix);
1249 if (ret != NO_ERROR) {
1250 return ret;
1251 }
1252 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001253 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1254 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001255 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001256 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001257 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001258 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 return BAD_VALUE;
1260 }
Eric Laurentc722f302014-12-10 11:21:49 -08001261 if (policyMix != NULL) {
1262 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001263 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1264 // there is an external policy, but this input is attached to a mix of recorders,
1265 // meaning it receives audio injected into the framework, so the recorder doesn't
1266 // know about it and is therefore considered "legacy"
1267 *inputType = API_INPUT_LEGACY;
1268 } else {
1269 // recording a mix of players defined by an external policy, we're rerouting for
1270 // an external policy
1271 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1272 }
Eric Laurentc722f302014-12-10 11:21:49 -08001273 } else if (audio_is_remote_submix_device(device)) {
1274 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001275 *inputType = API_INPUT_MIX_CAPTURE;
1276 } else {
1277 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001278 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001279 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001280 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001281 case AUDIO_SOURCE_VOICE_UPLINK:
1282 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1283 break;
1284 case AUDIO_SOURCE_VOICE_DOWNLINK:
1285 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1286 break;
1287 case AUDIO_SOURCE_VOICE_CALL:
1288 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1289 break;
1290 default:
1291 break;
1292 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001293 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001294 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1295 if (index >= 0) {
1296 *input = mSoundTriggerSessions.valueFor(session);
1297 isSoundTrigger = true;
1298 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1299 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1300 } else {
1301 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1302 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001303 }
1304 }
1305
Eric Laurent275e8e92014-11-30 15:14:47 -08001306 sp<IOProfile> profile = getInputProfile(device, address,
1307 samplingRate, format, channelMask,
1308 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001309 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001310 //retry without flags
1311 audio_input_flags_t log_flags = flags;
1312 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001313 profile = getInputProfile(device, address,
1314 samplingRate, format, channelMask,
1315 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001316 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001317 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1318 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001319 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001320 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001321 }
Eric Laurente552edb2014-03-10 17:42:56 -07001322 }
1323
Eric Laurent322b4d22015-04-03 15:57:54 -07001324 if (profile->getModuleHandle() == 0) {
1325 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001326 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001327 }
1328
1329 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1330 config.sample_rate = samplingRate;
1331 config.channel_mask = channelMask;
1332 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001333
Eric Laurent322b4d22015-04-03 15:57:54 -07001334 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001335 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001336 &config,
1337 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001338 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001339 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001340 flags);
1341
1342 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001343 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001344 (samplingRate != config.sample_rate) ||
1345 (format != config.format) ||
1346 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001347 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001348 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001349 if (*input != AUDIO_IO_HANDLE_NONE) {
1350 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001351 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001352 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001353 }
1354
Eric Laurent1f2f2232014-06-02 12:01:23 -07001355 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001356 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001357 inputDesc->mRefCount = 0;
1358 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001359 inputDesc->mSamplingRate = samplingRate;
1360 inputDesc->mFormat = format;
1361 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001362 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001363 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001364 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001365 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001366
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001367 ALOGV("getInputForAttr() returns input type = %d", inputType);
1368
Eric Laurentcaf7f482014-11-25 17:50:47 -08001369 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001370 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001371 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001372}
1373
Eric Laurent4dc68062014-07-28 17:26:49 -07001374status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1375 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001376{
1377 ALOGV("startInput() input %d", input);
1378 ssize_t index = mInputs.indexOfKey(input);
1379 if (index < 0) {
1380 ALOGW("startInput() unknown input %d", input);
1381 return BAD_VALUE;
1382 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001383 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001384
Eric Laurentc722f302014-12-10 11:21:49 -08001385 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001386 if (index < 0) {
1387 ALOGW("startInput() unknown session %d on input %d", session, input);
1388 return BAD_VALUE;
1389 }
1390
Glenn Kasten74a8e252014-07-24 14:09:55 -07001391 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001392 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001393
1394 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001395 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001396 if (activeInput != 0 && activeInput != input) {
1397
1398 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1399 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001400 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001401 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001402 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001403 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1404 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001405 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001406 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001407 return INVALID_OPERATION;
1408 }
1409 }
1410 }
1411
Glenn Kasten74a8e252014-07-24 14:09:55 -07001412 if (inputDesc->mRefCount == 0) {
François Gaffie53615e22015-03-19 09:24:12 +01001413 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001414 SoundTrigger::setCaptureState(true);
1415 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001416 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001417
Eric Laurentc722f302014-12-10 11:21:49 -08001418 // automatically enable the remote submix output when input is started if not
1419 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001420 // For remote submix (a virtual device), we open only one input per capture request.
1421 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001422 String8 address = String8("");
1423 if (inputDesc->mPolicyMix == NULL) {
1424 address = String8("0");
1425 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1426 address = inputDesc->mPolicyMix->mRegistrationId;
1427 }
1428 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001429 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001430 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001431 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001432 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001433 }
Eric Laurente552edb2014-03-10 17:42:56 -07001434 }
1435
Eric Laurente552edb2014-03-10 17:42:56 -07001436 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1437
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001438 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001439 return NO_ERROR;
1440}
1441
Eric Laurent4dc68062014-07-28 17:26:49 -07001442status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1443 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001444{
1445 ALOGV("stopInput() input %d", input);
1446 ssize_t index = mInputs.indexOfKey(input);
1447 if (index < 0) {
1448 ALOGW("stopInput() unknown input %d", input);
1449 return BAD_VALUE;
1450 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001451 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001452
Eric Laurentc722f302014-12-10 11:21:49 -08001453 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001454 if (index < 0) {
1455 ALOGW("stopInput() unknown session %d on input %d", session, input);
1456 return BAD_VALUE;
1457 }
1458
Eric Laurente552edb2014-03-10 17:42:56 -07001459 if (inputDesc->mRefCount == 0) {
1460 ALOGW("stopInput() input %d already stopped", input);
1461 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001462 }
1463
1464 inputDesc->mRefCount--;
1465 if (inputDesc->mRefCount == 0) {
1466
Eric Laurentc722f302014-12-10 11:21:49 -08001467 // automatically disable the remote submix output when input is stopped if not
1468 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001469 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001470 String8 address = String8("");
1471 if (inputDesc->mPolicyMix == NULL) {
1472 address = String8("0");
1473 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1474 address = inputDesc->mPolicyMix->mRegistrationId;
1475 }
1476 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001477 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001478 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001479 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001480 }
Eric Laurente552edb2014-03-10 17:42:56 -07001481 }
1482
Eric Laurent1c333e22014-05-20 10:48:17 -07001483 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001484
François Gaffie53615e22015-03-19 09:24:12 +01001485 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001486 SoundTrigger::setCaptureState(false);
1487 }
Eric Laurente552edb2014-03-10 17:42:56 -07001488 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001489 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001490}
1491
Eric Laurent4dc68062014-07-28 17:26:49 -07001492void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1493 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001494{
1495 ALOGV("releaseInput() %d", input);
1496 ssize_t index = mInputs.indexOfKey(input);
1497 if (index < 0) {
1498 ALOGW("releaseInput() releasing unknown input %d", input);
1499 return;
1500 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001501 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1502 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001503
Eric Laurentc722f302014-12-10 11:21:49 -08001504 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001505 if (index < 0) {
1506 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1507 return;
1508 }
Eric Laurentc722f302014-12-10 11:21:49 -08001509 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001510 if (inputDesc->mOpenRefCount == 0) {
1511 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1512 return;
1513 }
1514 inputDesc->mOpenRefCount--;
1515 if (inputDesc->mOpenRefCount > 0) {
1516 ALOGV("releaseInput() exit > 0");
1517 return;
1518 }
1519
Eric Laurent05b90f82014-08-27 15:32:29 -07001520 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001521 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001522 ALOGV("releaseInput() exit");
1523}
1524
Eric Laurentd4692962014-05-05 18:13:44 -07001525void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001526 bool patchRemoved = false;
1527
Eric Laurentd4692962014-05-05 18:13:44 -07001528 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001529 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1530 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1531 if (patch_index >= 0) {
1532 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1533 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1534 mAudioPatches.removeItemsAt(patch_index);
1535 patchRemoved = true;
1536 }
Eric Laurentd4692962014-05-05 18:13:44 -07001537 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1538 }
1539 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001540 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001541
1542 if (patchRemoved) {
1543 mpClientInterface->onAudioPatchListUpdate();
1544 }
Eric Laurentd4692962014-05-05 18:13:44 -07001545}
1546
Eric Laurente0720872014-03-11 09:30:41 -07001547void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001548 int indexMin,
1549 int indexMax)
1550{
1551 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001552 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001553 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1554 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001555 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001556 }
Eric Laurente552edb2014-03-10 17:42:56 -07001557}
1558
Eric Laurente0720872014-03-11 09:30:41 -07001559status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001560 int index,
1561 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001562{
1563
Eric Laurentc75307b2015-03-17 15:29:32 -07001564 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1565 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001566 return BAD_VALUE;
1567 }
1568 if (!audio_is_output_device(device)) {
1569 return BAD_VALUE;
1570 }
1571
1572 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001573 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001574
1575 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1576 stream, device, index);
1577
1578 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1579 // clear all device specific values
1580 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001581 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001582 }
François Gaffiedfd74092015-03-19 12:10:59 +01001583 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001584
Eric Laurent31551f82014-10-10 18:21:56 -07001585 // update volume on all outputs whose current device is also selected by the same
1586 // strategy as the device specified by the caller
1587 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001588
1589
1590 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1591 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1592 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001593 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001594 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1595 }
1596 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1597 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001598 return NO_ERROR;
1599 }
Eric Laurente552edb2014-03-10 17:42:56 -07001600 status_t status = NO_ERROR;
1601 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001602 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1603 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001604 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001605 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001606 if (volStatus != NO_ERROR) {
1607 status = volStatus;
1608 }
1609 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001610 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1611 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001612 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001613 }
Eric Laurente552edb2014-03-10 17:42:56 -07001614 }
1615 return status;
1616}
1617
Eric Laurente0720872014-03-11 09:30:41 -07001618status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001619 int *index,
1620 audio_devices_t device)
1621{
1622 if (index == NULL) {
1623 return BAD_VALUE;
1624 }
1625 if (!audio_is_output_device(device)) {
1626 return BAD_VALUE;
1627 }
1628 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1629 // the strategy the stream belongs to.
1630 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1631 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1632 }
François Gaffiedfd74092015-03-19 12:10:59 +01001633 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001634
Eric Laurentc75307b2015-03-17 15:29:32 -07001635 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001636 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1637 return NO_ERROR;
1638}
1639
Eric Laurente0720872014-03-11 09:30:41 -07001640audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001641 const SortedVector<audio_io_handle_t>& outputs)
1642{
1643 // select one output among several suitable for global effects.
1644 // The priority is as follows:
1645 // 1: An offloaded output. If the effect ends up not being offloadable,
1646 // AudioFlinger will invalidate the track and the offloaded output
1647 // will be closed causing the effect to be moved to a PCM output.
1648 // 2: A deep buffer output
1649 // 3: the first output in the list
1650
1651 if (outputs.size() == 0) {
1652 return 0;
1653 }
1654
1655 audio_io_handle_t outputOffloaded = 0;
1656 audio_io_handle_t outputDeepBuffer = 0;
1657
1658 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001659 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001660 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001661 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1662 outputOffloaded = outputs[i];
1663 }
1664 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1665 outputDeepBuffer = outputs[i];
1666 }
1667 }
1668
1669 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1670 outputOffloaded, outputDeepBuffer);
1671 if (outputOffloaded != 0) {
1672 return outputOffloaded;
1673 }
1674 if (outputDeepBuffer != 0) {
1675 return outputDeepBuffer;
1676 }
1677
1678 return outputs[0];
1679}
1680
Eric Laurente0720872014-03-11 09:30:41 -07001681audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001682{
1683 // apply simple rule where global effects are attached to the same output as MUSIC streams
1684
Eric Laurent3b73df72014-03-11 09:06:29 -07001685 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001686 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1687 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1688
1689 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1690 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1691 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1692
1693 return output;
1694}
1695
Eric Laurente0720872014-03-11 09:30:41 -07001696status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001697 audio_io_handle_t io,
1698 uint32_t strategy,
1699 int session,
1700 int id)
1701{
1702 ssize_t index = mOutputs.indexOfKey(io);
1703 if (index < 0) {
1704 index = mInputs.indexOfKey(io);
1705 if (index < 0) {
1706 ALOGW("registerEffect() unknown io %d", io);
1707 return INVALID_OPERATION;
1708 }
1709 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001710 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001711}
1712
Eric Laurentc75307b2015-03-17 15:29:32 -07001713bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1714{
1715 return mOutputs.isStreamActive(stream, inPastMs);
1716}
1717
1718bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1719{
1720 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1721}
1722
Eric Laurente0720872014-03-11 09:30:41 -07001723bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001724{
1725 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001726 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001727 if (inputDescriptor->mRefCount == 0) {
1728 continue;
1729 }
1730 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001731 return true;
1732 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001733 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1734 // corresponds to an active capture triggered by a hardware hotword recognition
1735 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1736 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1737 // FIXME: we should not assume that the first session is the active one and keep
1738 // activity count per session. Same in startInput().
1739 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1740 if (index >= 0) {
1741 return true;
1742 }
1743 }
Eric Laurente552edb2014-03-10 17:42:56 -07001744 }
1745 return false;
1746}
1747
Eric Laurent275e8e92014-11-30 15:14:47 -08001748// Register a list of custom mixes with their attributes and format.
1749// When a mix is registered, corresponding input and output profiles are
1750// added to the remote submix hw module. The profile contains only the
1751// parameters (sampling rate, format...) specified by the mix.
1752// The corresponding input remote submix device is also connected.
1753//
1754// When a remote submix device is connected, the address is checked to select the
1755// appropriate profile and the corresponding input or output stream is opened.
1756//
1757// When capture starts, getInputForAttr() will:
1758// - 1 look for a mix matching the address passed in attribtutes tags if any
1759// - 2 if none found, getDeviceForInputSource() will:
1760// - 2.1 look for a mix matching the attributes source
1761// - 2.2 if none found, default to device selection by policy rules
1762// At this time, the corresponding output remote submix device is also connected
1763// and active playback use cases can be transferred to this mix if needed when reconnecting
1764// after AudioTracks are invalidated
1765//
1766// When playback starts, getOutputForAttr() will:
1767// - 1 look for a mix matching the address passed in attribtutes tags if any
1768// - 2 if none found, look for a mix matching the attributes usage
1769// - 3 if none found, default to device and output selection by policy rules.
1770
1771status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1772{
1773 sp<HwModule> module;
1774 for (size_t i = 0; i < mHwModules.size(); i++) {
1775 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1776 mHwModules[i]->mHandle != 0) {
1777 module = mHwModules[i];
1778 break;
1779 }
1780 }
1781
1782 if (module == 0) {
1783 return INVALID_OPERATION;
1784 }
1785
1786 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1787
1788 for (size_t i = 0; i < mixes.size(); i++) {
1789 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001790
1791 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001792 continue;
1793 }
1794 audio_config_t outputConfig = mixes[i].mFormat;
1795 audio_config_t inputConfig = mixes[i].mFormat;
1796 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1797 // stereo and let audio flinger do the channel conversion if needed.
1798 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1799 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1800 module->addOutputProfile(address, &outputConfig,
1801 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1802 module->addInputProfile(address, &inputConfig,
1803 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001804
Eric Laurentc722f302014-12-10 11:21:49 -08001805 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001806 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001807 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001808 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001809 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001810 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001811 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001812 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001813 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001814 }
1815 return NO_ERROR;
1816}
1817
1818status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1819{
1820 sp<HwModule> module;
1821 for (size_t i = 0; i < mHwModules.size(); i++) {
1822 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1823 mHwModules[i]->mHandle != 0) {
1824 module = mHwModules[i];
1825 break;
1826 }
1827 }
1828
1829 if (module == 0) {
1830 return INVALID_OPERATION;
1831 }
1832
1833 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1834
1835 for (size_t i = 0; i < mixes.size(); i++) {
1836 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001837
1838 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001839 continue;
1840 }
1841
Eric Laurentc722f302014-12-10 11:21:49 -08001842 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1843 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1844 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001845 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001846 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001847 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001848 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001849
1850 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1851 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1852 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001853 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001854 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001855 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001856 }
1857 module->removeOutputProfile(address);
1858 module->removeInputProfile(address);
1859 }
1860 return NO_ERROR;
1861}
1862
Eric Laurente552edb2014-03-10 17:42:56 -07001863
Eric Laurente0720872014-03-11 09:30:41 -07001864status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001865{
1866 const size_t SIZE = 256;
1867 char buffer[SIZE];
1868 String8 result;
1869
1870 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1871 result.append(buffer);
1872
Eric Laurentc75307b2015-03-17 15:29:32 -07001873 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001874 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001875 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001876 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001877 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001878 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001879 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001880 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001881 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001882 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001883 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001884 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001885 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001886 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001887 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001888 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001889 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001890 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001891 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001892
François Gaffie53615e22015-03-19 09:24:12 +01001893 mAvailableOutputDevices.dump(fd, String8("output"));
1894 mAvailableInputDevices.dump(fd, String8("input"));
1895 mHwModules.dump(fd);
1896 mOutputs.dump(fd);
1897 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001898 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001899 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001900 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001901
1902 return NO_ERROR;
1903}
1904
1905// This function checks for the parameters which can be offloaded.
1906// This can be enhanced depending on the capability of the DSP and policy
1907// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001908bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001909{
1910 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001911 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001912 offloadInfo.sample_rate, offloadInfo.channel_mask,
1913 offloadInfo.format,
1914 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1915 offloadInfo.has_video);
1916
1917 // Check if offload has been disabled
1918 char propValue[PROPERTY_VALUE_MAX];
1919 if (property_get("audio.offload.disable", propValue, "0")) {
1920 if (atoi(propValue) != 0) {
1921 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1922 return false;
1923 }
1924 }
1925
1926 // Check if stream type is music, then only allow offload as of now.
1927 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1928 {
1929 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1930 return false;
1931 }
1932
1933 //TODO: enable audio offloading with video when ready
1934 if (offloadInfo.has_video)
1935 {
1936 ALOGV("isOffloadSupported: has_video == true, returning false");
1937 return false;
1938 }
1939
1940 //If duration is less than minimum value defined in property, return false
1941 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1942 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1943 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1944 return false;
1945 }
1946 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1947 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1948 return false;
1949 }
1950
1951 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1952 // creating an offloaded track and tearing it down immediately after start when audioflinger
1953 // detects there is an active non offloadable effect.
1954 // FIXME: We should check the audio session here but we do not have it in this context.
1955 // This may prevent offloading in rare situations where effects are left active by apps
1956 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01001957 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001958 return false;
1959 }
1960
1961 // See if there is a profile to support this.
1962 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001963 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001964 offloadInfo.sample_rate,
1965 offloadInfo.format,
1966 offloadInfo.channel_mask,
1967 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001968 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1969 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001970}
1971
Eric Laurent6a94d692014-05-20 11:18:06 -07001972status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1973 audio_port_type_t type,
1974 unsigned int *num_ports,
1975 struct audio_port *ports,
1976 unsigned int *generation)
1977{
1978 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1979 generation == NULL) {
1980 return BAD_VALUE;
1981 }
1982 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1983 if (ports == NULL) {
1984 *num_ports = 0;
1985 }
1986
1987 size_t portsWritten = 0;
1988 size_t portsMax = *num_ports;
1989 *num_ports = 0;
1990 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1991 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1992 for (size_t i = 0;
1993 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
1994 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
1995 }
1996 *num_ports += mAvailableOutputDevices.size();
1997 }
1998 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1999 for (size_t i = 0;
2000 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2001 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2002 }
2003 *num_ports += mAvailableInputDevices.size();
2004 }
2005 }
2006 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2007 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2008 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2009 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2010 }
2011 *num_ports += mInputs.size();
2012 }
2013 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002014 size_t numOutputs = 0;
2015 for (size_t i = 0; i < mOutputs.size(); i++) {
2016 if (!mOutputs[i]->isDuplicated()) {
2017 numOutputs++;
2018 if (portsWritten < portsMax) {
2019 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2020 }
2021 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002022 }
Eric Laurent84c70242014-06-23 08:46:27 -07002023 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002024 }
2025 }
2026 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002027 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002028 return NO_ERROR;
2029}
2030
2031status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2032{
2033 return NO_ERROR;
2034}
2035
Eric Laurent6a94d692014-05-20 11:18:06 -07002036status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2037 audio_patch_handle_t *handle,
2038 uid_t uid)
2039{
2040 ALOGV("createAudioPatch()");
2041
2042 if (handle == NULL || patch == NULL) {
2043 return BAD_VALUE;
2044 }
2045 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2046
Eric Laurent874c42872014-08-08 15:13:39 -07002047 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2048 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2049 return BAD_VALUE;
2050 }
2051 // only one source per audio patch supported for now
2052 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002053 return INVALID_OPERATION;
2054 }
Eric Laurent874c42872014-08-08 15:13:39 -07002055
2056 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002057 return INVALID_OPERATION;
2058 }
Eric Laurent874c42872014-08-08 15:13:39 -07002059 for (size_t i = 0; i < patch->num_sinks; i++) {
2060 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2061 return INVALID_OPERATION;
2062 }
2063 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002064
2065 sp<AudioPatch> patchDesc;
2066 ssize_t index = mAudioPatches.indexOfKey(*handle);
2067
Eric Laurent6a94d692014-05-20 11:18:06 -07002068 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2069 patch->sources[0].role,
2070 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002071#if LOG_NDEBUG == 0
2072 for (size_t i = 0; i < patch->num_sinks; i++) {
2073 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2074 patch->sinks[i].role,
2075 patch->sinks[i].type);
2076 }
2077#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002078
2079 if (index >= 0) {
2080 patchDesc = mAudioPatches.valueAt(index);
2081 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2082 mUidCached, patchDesc->mUid, uid);
2083 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2084 return INVALID_OPERATION;
2085 }
2086 } else {
2087 *handle = 0;
2088 }
2089
2090 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002091 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002092 if (outputDesc == NULL) {
2093 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2094 return BAD_VALUE;
2095 }
Eric Laurent84c70242014-06-23 08:46:27 -07002096 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2097 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002098 if (patchDesc != 0) {
2099 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2100 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2101 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2102 return BAD_VALUE;
2103 }
2104 }
Eric Laurent874c42872014-08-08 15:13:39 -07002105 DeviceVector devices;
2106 for (size_t i = 0; i < patch->num_sinks; i++) {
2107 // Only support mix to devices connection
2108 // TODO add support for mix to mix connection
2109 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2110 ALOGV("createAudioPatch() source mix but sink is not a device");
2111 return INVALID_OPERATION;
2112 }
2113 sp<DeviceDescriptor> devDesc =
2114 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2115 if (devDesc == 0) {
2116 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2117 return BAD_VALUE;
2118 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002119
François Gaffie53615e22015-03-19 09:24:12 +01002120 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002121 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002122 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002123 NULL, // updatedSamplingRate
2124 patch->sources[0].format,
2125 patch->sources[0].channel_mask,
2126 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2127 ALOGV("createAudioPatch() profile not supported for device %08x", devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002128 return INVALID_OPERATION;
2129 }
2130 devices.add(devDesc);
2131 }
2132 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002133 return INVALID_OPERATION;
2134 }
Eric Laurent874c42872014-08-08 15:13:39 -07002135
Eric Laurent6a94d692014-05-20 11:18:06 -07002136 // TODO: reconfigure output format and channels here
2137 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002138 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002139 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002140 index = mAudioPatches.indexOfKey(*handle);
2141 if (index >= 0) {
2142 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2143 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2144 }
2145 patchDesc = mAudioPatches.valueAt(index);
2146 patchDesc->mUid = uid;
2147 ALOGV("createAudioPatch() success");
2148 } else {
2149 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2150 return INVALID_OPERATION;
2151 }
2152 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2153 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2154 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002155 // only one sink supported when connecting an input device to a mix
2156 if (patch->num_sinks > 1) {
2157 return INVALID_OPERATION;
2158 }
François Gaffie53615e22015-03-19 09:24:12 +01002159 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002160 if (inputDesc == NULL) {
2161 return BAD_VALUE;
2162 }
2163 if (patchDesc != 0) {
2164 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2165 return BAD_VALUE;
2166 }
2167 }
2168 sp<DeviceDescriptor> devDesc =
2169 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2170 if (devDesc == 0) {
2171 return BAD_VALUE;
2172 }
2173
François Gaffie53615e22015-03-19 09:24:12 +01002174 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002175 devDesc->mAddress,
2176 patch->sinks[0].sample_rate,
2177 NULL, /*updatedSampleRate*/
2178 patch->sinks[0].format,
2179 patch->sinks[0].channel_mask,
2180 // FIXME for the parameter type,
2181 // and the NONE
2182 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002183 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002184 return INVALID_OPERATION;
2185 }
2186 // TODO: reconfigure output format and channels here
2187 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002188 devDesc->type(), inputDesc->mIoHandle);
2189 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002190 index = mAudioPatches.indexOfKey(*handle);
2191 if (index >= 0) {
2192 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2193 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2194 }
2195 patchDesc = mAudioPatches.valueAt(index);
2196 patchDesc->mUid = uid;
2197 ALOGV("createAudioPatch() success");
2198 } else {
2199 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2200 return INVALID_OPERATION;
2201 }
2202 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2203 // device to device connection
2204 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002205 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002206 return BAD_VALUE;
2207 }
2208 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002209 sp<DeviceDescriptor> srcDeviceDesc =
2210 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002211 if (srcDeviceDesc == 0) {
2212 return BAD_VALUE;
2213 }
Eric Laurent874c42872014-08-08 15:13:39 -07002214
Eric Laurent6a94d692014-05-20 11:18:06 -07002215 //update source and sink with our own data as the data passed in the patch may
2216 // be incomplete.
2217 struct audio_patch newPatch = *patch;
2218 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002219
Eric Laurent874c42872014-08-08 15:13:39 -07002220 for (size_t i = 0; i < patch->num_sinks; i++) {
2221 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2222 ALOGV("createAudioPatch() source device but one sink is not a device");
2223 return INVALID_OPERATION;
2224 }
2225
2226 sp<DeviceDescriptor> sinkDeviceDesc =
2227 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2228 if (sinkDeviceDesc == 0) {
2229 return BAD_VALUE;
2230 }
2231 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2232
Eric Laurent3bcf8592015-04-03 12:13:24 -07002233 // create a software bridge in PatchPanel if:
2234 // - source and sink devices are on differnt HW modules OR
2235 // - audio HAL version is < 3.0
2236 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2237 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2238 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002239 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002240 return INVALID_OPERATION;
2241 }
Eric Laurent874c42872014-08-08 15:13:39 -07002242 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002243 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002244 // if the sink device is reachable via an opened output stream, request to go via
2245 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002246 audio_io_handle_t output = selectOutput(outputs,
2247 AUDIO_OUTPUT_FLAG_NONE,
2248 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002249 if (output != AUDIO_IO_HANDLE_NONE) {
2250 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2251 if (outputDesc->isDuplicated()) {
2252 return INVALID_OPERATION;
2253 }
2254 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002255 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002256 newPatch.num_sources = 2;
2257 }
Eric Laurent83b88082014-06-20 18:31:16 -07002258 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002259 }
2260 // TODO: check from routing capabilities in config file and other conflicting patches
2261
2262 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2263 if (index >= 0) {
2264 afPatchHandle = patchDesc->mAfPatchHandle;
2265 }
2266
2267 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2268 &afPatchHandle,
2269 0);
2270 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2271 status, afPatchHandle);
2272 if (status == NO_ERROR) {
2273 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002274 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002275 addAudioPatch(patchDesc->mHandle, patchDesc);
2276 } else {
2277 patchDesc->mPatch = newPatch;
2278 }
2279 patchDesc->mAfPatchHandle = afPatchHandle;
2280 *handle = patchDesc->mHandle;
2281 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002282 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002283 } else {
2284 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2285 status);
2286 return INVALID_OPERATION;
2287 }
2288 } else {
2289 return BAD_VALUE;
2290 }
2291 } else {
2292 return BAD_VALUE;
2293 }
2294 return NO_ERROR;
2295}
2296
2297status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2298 uid_t uid)
2299{
2300 ALOGV("releaseAudioPatch() patch %d", handle);
2301
2302 ssize_t index = mAudioPatches.indexOfKey(handle);
2303
2304 if (index < 0) {
2305 return BAD_VALUE;
2306 }
2307 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2308 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2309 mUidCached, patchDesc->mUid, uid);
2310 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2311 return INVALID_OPERATION;
2312 }
2313
2314 struct audio_patch *patch = &patchDesc->mPatch;
2315 patchDesc->mUid = mUidCached;
2316 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002317 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002318 if (outputDesc == NULL) {
2319 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2320 return BAD_VALUE;
2321 }
2322
Eric Laurentc75307b2015-03-17 15:29:32 -07002323 setOutputDevice(outputDesc,
2324 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002325 true,
2326 0,
2327 NULL);
2328 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2329 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002330 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002331 if (inputDesc == NULL) {
2332 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2333 return BAD_VALUE;
2334 }
2335 setInputDevice(inputDesc->mIoHandle,
2336 getNewInputDevice(inputDesc->mIoHandle),
2337 true,
2338 NULL);
2339 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2340 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2341 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2342 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2343 status, patchDesc->mAfPatchHandle);
2344 removeAudioPatch(patchDesc->mHandle);
2345 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002346 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002347 } else {
2348 return BAD_VALUE;
2349 }
2350 } else {
2351 return BAD_VALUE;
2352 }
2353 return NO_ERROR;
2354}
2355
2356status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2357 struct audio_patch *patches,
2358 unsigned int *generation)
2359{
François Gaffie53615e22015-03-19 09:24:12 +01002360 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002361 return BAD_VALUE;
2362 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002363 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002364 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002365}
2366
Eric Laurente1715a42014-05-20 11:30:42 -07002367status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002368{
Eric Laurente1715a42014-05-20 11:30:42 -07002369 ALOGV("setAudioPortConfig()");
2370
2371 if (config == NULL) {
2372 return BAD_VALUE;
2373 }
2374 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2375 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002376 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2377 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002378 }
2379
Eric Laurenta121f902014-06-03 13:32:54 -07002380 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002381 if (config->type == AUDIO_PORT_TYPE_MIX) {
2382 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002383 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002384 if (outputDesc == NULL) {
2385 return BAD_VALUE;
2386 }
Eric Laurent84c70242014-06-23 08:46:27 -07002387 ALOG_ASSERT(!outputDesc->isDuplicated(),
2388 "setAudioPortConfig() called on duplicated output %d",
2389 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002390 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002391 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002392 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002393 if (inputDesc == NULL) {
2394 return BAD_VALUE;
2395 }
Eric Laurenta121f902014-06-03 13:32:54 -07002396 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002397 } else {
2398 return BAD_VALUE;
2399 }
2400 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2401 sp<DeviceDescriptor> deviceDesc;
2402 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2403 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2404 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2405 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2406 } else {
2407 return BAD_VALUE;
2408 }
2409 if (deviceDesc == NULL) {
2410 return BAD_VALUE;
2411 }
Eric Laurenta121f902014-06-03 13:32:54 -07002412 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002413 } else {
2414 return BAD_VALUE;
2415 }
2416
Eric Laurenta121f902014-06-03 13:32:54 -07002417 struct audio_port_config backupConfig;
2418 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2419 if (status == NO_ERROR) {
2420 struct audio_port_config newConfig;
2421 audioPortConfig->toAudioPortConfig(&newConfig, config);
2422 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002423 }
Eric Laurenta121f902014-06-03 13:32:54 -07002424 if (status != NO_ERROR) {
2425 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002426 }
Eric Laurente1715a42014-05-20 11:30:42 -07002427
2428 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002429}
2430
2431void AudioPolicyManager::clearAudioPatches(uid_t uid)
2432{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002433 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2435 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002436 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002437 }
2438 }
2439}
2440
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002441status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2442 audio_io_handle_t *ioHandle,
2443 audio_devices_t *device)
2444{
2445 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2446 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002447 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002448
François Gaffiedf372692015-03-19 10:43:27 +01002449 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002450}
2451
Eric Laurente552edb2014-03-10 17:42:56 -07002452// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002453// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002454// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002455uint32_t AudioPolicyManager::nextAudioPortGeneration()
2456{
2457 return android_atomic_inc(&mAudioPortGeneration);
2458}
2459
Eric Laurente0720872014-03-11 09:30:41 -07002460AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002461 :
2462#ifdef AUDIO_POLICY_TEST
2463 Thread(false),
2464#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002465 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002466 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002467 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002468 mAudioPortGeneration(1),
2469 mBeaconMuteRefCount(0),
2470 mBeaconPlayingRefCount(0),
2471 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002472{
François Gaffie2110e042015-03-24 08:41:51 +01002473 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2474 if (!engineInstance) {
2475 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2476 return;
2477 }
2478 // Retrieve the Policy Manager Interface
2479 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2480 if (mEngine == NULL) {
2481 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2482 return;
2483 }
2484 mEngine->setObserver(this);
2485 status_t status = mEngine->initCheck();
2486 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2487
Eric Laurent6a94d692014-05-20 11:18:06 -07002488 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002489 mpClientInterface = clientInterface;
2490
Paul McLeane743a472015-01-28 11:07:31 -08002491 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002492 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2493 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2494 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2495 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2496 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2497 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002498 ALOGE("could not load audio policy configuration file, setting defaults");
2499 defaultAudioPolicyConfig();
2500 }
2501 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002502 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002503
François Gaffie2110e042015-03-24 08:41:51 +01002504 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2505 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002506
2507 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002508 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2509 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002510 for (size_t i = 0; i < mHwModules.size(); i++) {
2511 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2512 if (mHwModules[i]->mHandle == 0) {
2513 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2514 continue;
2515 }
2516 // open all output streams needed to access attached devices
2517 // except for direct output streams that are only opened when they are actually
2518 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002519 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002520 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2521 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002522 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002523
Eric Laurent3a4311c2014-03-17 12:00:47 -07002524 if (outProfile->mSupportedDevices.isEmpty()) {
2525 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2526 continue;
2527 }
2528
Eric Laurentd78f1532014-09-16 16:38:20 -07002529 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2530 continue;
2531 }
Eric Laurent83b88082014-06-20 18:31:16 -07002532 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002533 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2534 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002535 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002536 // chose first device present in mSupportedDevices also part of
2537 // outputDeviceTypes
2538 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002539 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002540 if ((profileType & outputDeviceTypes) != 0) {
2541 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002542 }
Eric Laurente552edb2014-03-10 17:42:56 -07002543 }
2544 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002545 if ((profileType & outputDeviceTypes) == 0) {
2546 continue;
2547 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002548 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2549 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002550
2551 outputDesc->mDevice = profileType;
2552 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2553 config.sample_rate = outputDesc->mSamplingRate;
2554 config.channel_mask = outputDesc->mChannelMask;
2555 config.format = outputDesc->mFormat;
2556 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002557 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002558 &output,
2559 &config,
2560 &outputDesc->mDevice,
2561 String8(""),
2562 &outputDesc->mLatency,
2563 outputDesc->mFlags);
2564
2565 if (status != NO_ERROR) {
2566 ALOGW("Cannot open output stream for device %08x on hw module %s",
2567 outputDesc->mDevice,
2568 mHwModules[i]->mName);
2569 } else {
2570 outputDesc->mSamplingRate = config.sample_rate;
2571 outputDesc->mChannelMask = config.channel_mask;
2572 outputDesc->mFormat = config.format;
2573
2574 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002575 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002576 ssize_t index =
2577 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2578 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002579 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2580 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002581 }
2582 }
2583 if (mPrimaryOutput == 0 &&
2584 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002585 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002586 }
2587 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002588 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002589 outputDesc->mDevice,
2590 true);
2591 }
Eric Laurente552edb2014-03-10 17:42:56 -07002592 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002593 // open input streams needed to access attached devices to validate
2594 // mAvailableInputDevices list
2595 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2596 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002597 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002598
Eric Laurent3a4311c2014-03-17 12:00:47 -07002599 if (inProfile->mSupportedDevices.isEmpty()) {
2600 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2601 continue;
2602 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002603 // chose first device present in mSupportedDevices also part of
2604 // inputDeviceTypes
2605 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2606 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002607 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002608 if (profileType & inputDeviceTypes) {
2609 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002610 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002611 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002612 if ((profileType & inputDeviceTypes) == 0) {
2613 continue;
2614 }
2615 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2616
2617 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2618 inputDesc->mDevice = profileType;
2619
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002620 // find the address
2621 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2622 // the inputs vector must be of size 1, but we don't want to crash here
2623 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2624 : String8("");
2625 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2626 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2627
Eric Laurentd78f1532014-09-16 16:38:20 -07002628 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2629 config.sample_rate = inputDesc->mSamplingRate;
2630 config.channel_mask = inputDesc->mChannelMask;
2631 config.format = inputDesc->mFormat;
2632 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002633 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002634 &input,
2635 &config,
2636 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002637 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002638 AUDIO_SOURCE_MIC,
2639 AUDIO_INPUT_FLAG_NONE);
2640
2641 if (status == NO_ERROR) {
2642 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002643 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002644 ssize_t index =
2645 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2646 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002647 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2648 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002649 }
2650 }
2651 mpClientInterface->closeInput(input);
2652 } else {
2653 ALOGW("Cannot open input stream for device %08x on hw module %s",
2654 inputDesc->mDevice,
2655 mHwModules[i]->mName);
2656 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002657 }
2658 }
2659 // make sure all attached devices have been allocated a unique ID
2660 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002661 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002662 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002663 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2664 continue;
2665 }
François Gaffie2110e042015-03-24 08:41:51 +01002666 // The device is now validated and can be appended to the available devices of the engine
2667 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2668 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002669 i++;
2670 }
2671 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002672 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002673 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002674 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2675 continue;
2676 }
François Gaffie2110e042015-03-24 08:41:51 +01002677 // The device is now validated and can be appended to the available devices of the engine
2678 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2679 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002680 i++;
2681 }
2682 // make sure default device is reachable
2683 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002684 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002685 }
Eric Laurente552edb2014-03-10 17:42:56 -07002686
2687 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2688
2689 updateDevicesAndOutputs();
2690
2691#ifdef AUDIO_POLICY_TEST
2692 if (mPrimaryOutput != 0) {
2693 AudioParameter outputCmd = AudioParameter();
2694 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002695 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002696
2697 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2698 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002699 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2700 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002701 mTestLatencyMs = 0;
2702 mCurOutput = 0;
2703 mDirectOutput = false;
2704 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2705 mTestOutputs[i] = 0;
2706 }
2707
2708 const size_t SIZE = 256;
2709 char buffer[SIZE];
2710 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2711 run(buffer, ANDROID_PRIORITY_AUDIO);
2712 }
2713#endif //AUDIO_POLICY_TEST
2714}
2715
Eric Laurente0720872014-03-11 09:30:41 -07002716AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002717{
2718#ifdef AUDIO_POLICY_TEST
2719 exit();
2720#endif //AUDIO_POLICY_TEST
2721 for (size_t i = 0; i < mOutputs.size(); i++) {
2722 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002723 }
2724 for (size_t i = 0; i < mInputs.size(); i++) {
2725 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002726 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002727 mAvailableOutputDevices.clear();
2728 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002729 mOutputs.clear();
2730 mInputs.clear();
2731 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002732}
2733
Eric Laurente0720872014-03-11 09:30:41 -07002734status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002735{
2736 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2737}
2738
2739#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002740bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002741{
2742 ALOGV("entering threadLoop()");
2743 while (!exitPending())
2744 {
2745 String8 command;
2746 int valueInt;
2747 String8 value;
2748
2749 Mutex::Autolock _l(mLock);
2750 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2751
2752 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2753 AudioParameter param = AudioParameter(command);
2754
2755 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2756 valueInt != 0) {
2757 ALOGV("Test command %s received", command.string());
2758 String8 target;
2759 if (param.get(String8("target"), target) != NO_ERROR) {
2760 target = "Manager";
2761 }
2762 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2763 param.remove(String8("test_cmd_policy_output"));
2764 mCurOutput = valueInt;
2765 }
2766 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2767 param.remove(String8("test_cmd_policy_direct"));
2768 if (value == "false") {
2769 mDirectOutput = false;
2770 } else if (value == "true") {
2771 mDirectOutput = true;
2772 }
2773 }
2774 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2775 param.remove(String8("test_cmd_policy_input"));
2776 mTestInput = valueInt;
2777 }
2778
2779 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2780 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002781 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002782 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002783 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002784 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002785 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002786 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002787 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002788 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002789 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002790 if (target == "Manager") {
2791 mTestFormat = format;
2792 } else if (mTestOutputs[mCurOutput] != 0) {
2793 AudioParameter outputParam = AudioParameter();
2794 outputParam.addInt(String8("format"), format);
2795 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2796 }
2797 }
2798 }
2799 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2800 param.remove(String8("test_cmd_policy_channels"));
2801 int channels = 0;
2802
2803 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002804 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002805 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002806 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002807 }
2808 if (channels != 0) {
2809 if (target == "Manager") {
2810 mTestChannels = channels;
2811 } else if (mTestOutputs[mCurOutput] != 0) {
2812 AudioParameter outputParam = AudioParameter();
2813 outputParam.addInt(String8("channels"), channels);
2814 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2815 }
2816 }
2817 }
2818 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2819 param.remove(String8("test_cmd_policy_sampleRate"));
2820 if (valueInt >= 0 && valueInt <= 96000) {
2821 int samplingRate = valueInt;
2822 if (target == "Manager") {
2823 mTestSamplingRate = samplingRate;
2824 } else if (mTestOutputs[mCurOutput] != 0) {
2825 AudioParameter outputParam = AudioParameter();
2826 outputParam.addInt(String8("sampling_rate"), samplingRate);
2827 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2828 }
2829 }
2830 }
2831
2832 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2833 param.remove(String8("test_cmd_policy_reopen"));
2834
Eric Laurentc75307b2015-03-17 15:29:32 -07002835 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07002836
Eric Laurentc75307b2015-03-17 15:29:32 -07002837 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07002838
Eric Laurentc75307b2015-03-17 15:29:32 -07002839 removeOutput(mPrimaryOutput->mIoHandle);
2840 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
2841 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07002842 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002843 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2844 config.sample_rate = outputDesc->mSamplingRate;
2845 config.channel_mask = outputDesc->mChannelMask;
2846 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07002847 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002848 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07002849 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002850 &config,
2851 &outputDesc->mDevice,
2852 String8(""),
2853 &outputDesc->mLatency,
2854 outputDesc->mFlags);
2855 if (status != NO_ERROR) {
2856 ALOGE("Failed to reopen hardware output stream, "
2857 "samplingRate: %d, format %d, channels %d",
2858 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07002859 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002860 outputDesc->mSamplingRate = config.sample_rate;
2861 outputDesc->mChannelMask = config.channel_mask;
2862 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07002863 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07002864 AudioParameter outputCmd = AudioParameter();
2865 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002866 mpClientInterface->setParameters(handle, outputCmd.toString());
2867 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07002868 }
2869 }
2870
2871
2872 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2873 }
2874 }
2875 return false;
2876}
2877
Eric Laurente0720872014-03-11 09:30:41 -07002878void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002879{
2880 {
2881 AutoMutex _l(mLock);
2882 requestExit();
2883 mWaitWorkCV.signal();
2884 }
2885 requestExitAndWait();
2886}
2887
Eric Laurente0720872014-03-11 09:30:41 -07002888int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002889{
2890 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2891 if (output == mTestOutputs[i]) return i;
2892 }
2893 return 0;
2894}
2895#endif //AUDIO_POLICY_TEST
2896
2897// ---
2898
Eric Laurentc75307b2015-03-17 15:29:32 -07002899void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002900{
François Gaffie98cc1912015-03-18 17:52:40 +01002901 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07002902 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002903 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002904}
2905
François Gaffie53615e22015-03-19 09:24:12 +01002906void AudioPolicyManager::removeOutput(audio_io_handle_t output)
2907{
2908 mOutputs.removeItem(output);
2909}
2910
Eric Laurent1f2f2232014-06-02 12:01:23 -07002911void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002912{
François Gaffie98cc1912015-03-18 17:52:40 +01002913 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07002914 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002915 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002916}
Eric Laurente552edb2014-03-10 17:42:56 -07002917
Eric Laurentc75307b2015-03-17 15:29:32 -07002918void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08002919 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002920 const String8 address /*in*/,
2921 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08002922 sp<DeviceDescriptor> devDesc =
2923 desc->mProfile->mSupportedDevices.getDevice(device, address);
2924 if (devDesc != 0) {
2925 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
2926 desc->mIoHandle, address.string());
2927 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002928 }
2929}
2930
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002931status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01002932 audio_policy_dev_state_t state,
2933 SortedVector<audio_io_handle_t>& outputs,
2934 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002935{
François Gaffie53615e22015-03-19 09:24:12 +01002936 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07002937 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002938 // erase all current sample rates, formats and channel masks
2939 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07002940
Eric Laurent3b73df72014-03-11 09:06:29 -07002941 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002942 // first list already open outputs that can be routed to this device
2943 for (size_t i = 0; i < mOutputs.size(); i++) {
2944 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07002945 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01002946 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002947 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2948 outputs.add(mOutputs.keyAt(i));
2949 } else {
2950 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08002951 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002952 }
Eric Laurente552edb2014-03-10 17:42:56 -07002953 }
2954 }
2955 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002956 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002957 for (size_t i = 0; i < mHwModules.size(); i++)
2958 {
2959 if (mHwModules[i]->mHandle == 0) {
2960 continue;
2961 }
2962 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2963 {
Eric Laurent275e8e92014-11-30 15:14:47 -08002964 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
2965 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01002966 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08002967 address == profile->mSupportedDevices[0]->mAddress) {
2968 profiles.add(profile);
2969 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
2970 }
Eric Laurente552edb2014-03-10 17:42:56 -07002971 }
2972 }
2973 }
2974
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002975 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
2976
Eric Laurente552edb2014-03-10 17:42:56 -07002977 if (profiles.isEmpty() && outputs.isEmpty()) {
2978 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2979 return BAD_VALUE;
2980 }
2981
2982 // open outputs for matching profiles if needed. Direct outputs are also opened to
2983 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2984 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002985 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002986
2987 // nothing to do if one output is already opened for this profile
2988 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002989 for (j = 0; j < outputs.size(); j++) {
2990 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07002991 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002992 // matching profile: save the sample rates, format and channel masks supported
2993 // by the profile in our device descriptor
2994 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07002995 break;
2996 }
2997 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002998 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002999 continue;
3000 }
3001
Eric Laurent83b88082014-06-20 18:31:16 -07003002 ALOGV("opening output for device %08x with params %s profile %p",
3003 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003004 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003005 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003006 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3007 config.sample_rate = desc->mSamplingRate;
3008 config.channel_mask = desc->mChannelMask;
3009 config.format = desc->mFormat;
3010 config.offload_info.sample_rate = desc->mSamplingRate;
3011 config.offload_info.channel_mask = desc->mChannelMask;
3012 config.offload_info.format = desc->mFormat;
3013 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003014 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003015 &output,
3016 &config,
3017 &desc->mDevice,
3018 address,
3019 &desc->mLatency,
3020 desc->mFlags);
3021 if (status == NO_ERROR) {
3022 desc->mSamplingRate = config.sample_rate;
3023 desc->mChannelMask = config.channel_mask;
3024 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003025
Eric Laurentd4692962014-05-05 18:13:44 -07003026 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003027 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003028 char *param = audio_device_address_to_parameter(device, address);
3029 mpClientInterface->setParameters(output, String8(param));
3030 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003031 }
3032
Eric Laurentd4692962014-05-05 18:13:44 -07003033 // Here is where we step through and resolve any "dynamic" fields
3034 String8 reply;
3035 char *value;
3036 if (profile->mSamplingRates[0] == 0) {
3037 reply = mpClientInterface->getParameters(output,
3038 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003039 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003040 reply.string());
3041 value = strpbrk((char *)reply.string(), "=");
3042 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003043 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003044 }
Eric Laurentd4692962014-05-05 18:13:44 -07003045 }
3046 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3047 reply = mpClientInterface->getParameters(output,
3048 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003049 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003050 reply.string());
3051 value = strpbrk((char *)reply.string(), "=");
3052 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003053 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003054 }
Eric Laurentd4692962014-05-05 18:13:44 -07003055 }
3056 if (profile->mChannelMasks[0] == 0) {
3057 reply = mpClientInterface->getParameters(output,
3058 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003059 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003060 reply.string());
3061 value = strpbrk((char *)reply.string(), "=");
3062 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003063 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003064 }
Eric Laurentd4692962014-05-05 18:13:44 -07003065 }
3066 if (((profile->mSamplingRates[0] == 0) &&
3067 (profile->mSamplingRates.size() < 2)) ||
3068 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3069 (profile->mFormats.size() < 2)) ||
3070 ((profile->mChannelMasks[0] == 0) &&
3071 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003072 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003073 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003074 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003075 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3076 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003077 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003078 config.sample_rate = profile->pickSamplingRate();
3079 config.channel_mask = profile->pickChannelMask();
3080 config.format = profile->pickFormat();
3081 config.offload_info.sample_rate = config.sample_rate;
3082 config.offload_info.channel_mask = config.channel_mask;
3083 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003084 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003085 &output,
3086 &config,
3087 &desc->mDevice,
3088 address,
3089 &desc->mLatency,
3090 desc->mFlags);
3091 if (status == NO_ERROR) {
3092 desc->mSamplingRate = config.sample_rate;
3093 desc->mChannelMask = config.channel_mask;
3094 desc->mFormat = config.format;
3095 } else {
3096 output = AUDIO_IO_HANDLE_NONE;
3097 }
Eric Laurentd4692962014-05-05 18:13:44 -07003098 }
3099
Eric Laurentcf2c0212014-07-25 16:20:43 -07003100 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003101 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003102 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003103 sp<AudioPolicyMix> policyMix;
3104 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003105 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3106 address.string());
3107 }
François Gaffie036e1e92015-03-19 10:16:24 +01003108 policyMix->setOutput(desc);
3109 desc->mPolicyMix = &(policyMix->getMix());
3110
Eric Laurentc722f302014-12-10 11:21:49 -08003111 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3112 // no duplicated output for direct outputs and
3113 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003114 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003115
Eric Laurentd4692962014-05-05 18:13:44 -07003116 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003117 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003118
Eric Laurentd4692962014-05-05 18:13:44 -07003119 //TODO: configure audio effect output stage here
3120
3121 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003122 duplicatedOutput =
3123 mpClientInterface->openDuplicateOutput(output,
3124 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003125 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003126 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003127 sp<SwAudioOutputDescriptor> dupOutputDesc =
3128 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3129 dupOutputDesc->mOutput1 = mPrimaryOutput;
3130 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003131 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3132 dupOutputDesc->mFormat = desc->mFormat;
3133 dupOutputDesc->mChannelMask = desc->mChannelMask;
3134 dupOutputDesc->mLatency = desc->mLatency;
3135 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003136 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003137 } else {
3138 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003139 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003140 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003141 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003142 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003143 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003144 }
Eric Laurente552edb2014-03-10 17:42:56 -07003145 }
3146 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003147 } else {
3148 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003149 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003150 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003151 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003152 profiles.removeAt(profile_index);
3153 profile_index--;
3154 } else {
3155 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003156 devDesc->importAudioPort(profile);
3157
François Gaffie53615e22015-03-19 09:24:12 +01003158 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003159 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3160 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003161 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003162 NULL/*patch handle*/, address.string());
3163 }
Eric Laurente552edb2014-03-10 17:42:56 -07003164 ALOGV("checkOutputsForDevice(): adding output %d", output);
3165 }
3166 }
3167
3168 if (profiles.isEmpty()) {
3169 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3170 return BAD_VALUE;
3171 }
Eric Laurentd4692962014-05-05 18:13:44 -07003172 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003173 // check if one opened output is not needed any more after disconnecting one device
3174 for (size_t i = 0; i < mOutputs.size(); i++) {
3175 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003176 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003177 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003178 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003179 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003180 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003181 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003182 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3183 mOutputs.keyAt(i));
3184 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003185 }
Eric Laurente552edb2014-03-10 17:42:56 -07003186 }
3187 }
Eric Laurentd4692962014-05-05 18:13:44 -07003188 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003189 for (size_t i = 0; i < mHwModules.size(); i++)
3190 {
3191 if (mHwModules[i]->mHandle == 0) {
3192 continue;
3193 }
3194 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3195 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003196 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003197 if (profile->mSupportedDevices.types() & device) {
3198 ALOGV("checkOutputsForDevice(): "
3199 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003200 if (profile->mSamplingRates[0] == 0) {
3201 profile->mSamplingRates.clear();
3202 profile->mSamplingRates.add(0);
3203 }
3204 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3205 profile->mFormats.clear();
3206 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3207 }
3208 if (profile->mChannelMasks[0] == 0) {
3209 profile->mChannelMasks.clear();
3210 profile->mChannelMasks.add(0);
3211 }
3212 }
3213 }
3214 }
3215 }
3216 return NO_ERROR;
3217}
3218
Eric Laurentd4692962014-05-05 18:13:44 -07003219status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003220 audio_policy_dev_state_t state,
3221 SortedVector<audio_io_handle_t>& inputs,
3222 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003223{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003224 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003225 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3226 // first list already open inputs that can be routed to this device
3227 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3228 desc = mInputs.valueAt(input_index);
3229 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3230 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3231 inputs.add(mInputs.keyAt(input_index));
3232 }
3233 }
3234
3235 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003236 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003237 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3238 {
3239 if (mHwModules[module_idx]->mHandle == 0) {
3240 continue;
3241 }
3242 for (size_t profile_index = 0;
3243 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3244 profile_index++)
3245 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003246 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3247
3248 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003249 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003250 address == profile->mSupportedDevices[0]->mAddress) {
3251 profiles.add(profile);
3252 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3253 profile_index, module_idx);
3254 }
Eric Laurentd4692962014-05-05 18:13:44 -07003255 }
3256 }
3257 }
3258
3259 if (profiles.isEmpty() && inputs.isEmpty()) {
3260 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3261 return BAD_VALUE;
3262 }
3263
3264 // open inputs for matching profiles if needed. Direct inputs are also opened to
3265 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3266 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3267
Eric Laurent1c333e22014-05-20 10:48:17 -07003268 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003269 // nothing to do if one input is already opened for this profile
3270 size_t input_index;
3271 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3272 desc = mInputs.valueAt(input_index);
3273 if (desc->mProfile == profile) {
3274 break;
3275 }
3276 }
3277 if (input_index != mInputs.size()) {
3278 continue;
3279 }
3280
3281 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3282 desc = new AudioInputDescriptor(profile);
3283 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003284 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3285 config.sample_rate = desc->mSamplingRate;
3286 config.channel_mask = desc->mChannelMask;
3287 config.format = desc->mFormat;
3288 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003289 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003290 &input,
3291 &config,
3292 &desc->mDevice,
3293 address,
3294 AUDIO_SOURCE_MIC,
3295 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003296
Eric Laurentcf2c0212014-07-25 16:20:43 -07003297 if (status == NO_ERROR) {
3298 desc->mSamplingRate = config.sample_rate;
3299 desc->mChannelMask = config.channel_mask;
3300 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003301
Eric Laurentd4692962014-05-05 18:13:44 -07003302 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003303 char *param = audio_device_address_to_parameter(device, address);
3304 mpClientInterface->setParameters(input, String8(param));
3305 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003306 }
3307
3308 // Here is where we step through and resolve any "dynamic" fields
3309 String8 reply;
3310 char *value;
3311 if (profile->mSamplingRates[0] == 0) {
3312 reply = mpClientInterface->getParameters(input,
3313 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3314 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3315 reply.string());
3316 value = strpbrk((char *)reply.string(), "=");
3317 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003318 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003319 }
3320 }
3321 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3322 reply = mpClientInterface->getParameters(input,
3323 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3324 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3325 value = strpbrk((char *)reply.string(), "=");
3326 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003327 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003328 }
3329 }
3330 if (profile->mChannelMasks[0] == 0) {
3331 reply = mpClientInterface->getParameters(input,
3332 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3333 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3334 reply.string());
3335 value = strpbrk((char *)reply.string(), "=");
3336 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003337 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003338 }
3339 }
3340 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3341 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3342 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3343 ALOGW("checkInputsForDevice() direct input missing param");
3344 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003345 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003346 }
3347
3348 if (input != 0) {
3349 addInput(input, desc);
3350 }
3351 } // endif input != 0
3352
Eric Laurentcf2c0212014-07-25 16:20:43 -07003353 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003354 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003355 profiles.removeAt(profile_index);
3356 profile_index--;
3357 } else {
3358 inputs.add(input);
3359 ALOGV("checkInputsForDevice(): adding input %d", input);
3360 }
3361 } // end scan profiles
3362
3363 if (profiles.isEmpty()) {
3364 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3365 return BAD_VALUE;
3366 }
3367 } else {
3368 // Disconnect
3369 // check if one opened input is not needed any more after disconnecting one device
3370 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3371 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003372 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3373 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003374 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3375 mInputs.keyAt(input_index));
3376 inputs.add(mInputs.keyAt(input_index));
3377 }
3378 }
3379 // Clear any profiles associated with the disconnected device.
3380 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3381 if (mHwModules[module_index]->mHandle == 0) {
3382 continue;
3383 }
3384 for (size_t profile_index = 0;
3385 profile_index < mHwModules[module_index]->mInputProfiles.size();
3386 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003387 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003388 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003389 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003390 profile_index, module_index);
3391 if (profile->mSamplingRates[0] == 0) {
3392 profile->mSamplingRates.clear();
3393 profile->mSamplingRates.add(0);
3394 }
3395 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3396 profile->mFormats.clear();
3397 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3398 }
3399 if (profile->mChannelMasks[0] == 0) {
3400 profile->mChannelMasks.clear();
3401 profile->mChannelMasks.add(0);
3402 }
3403 }
3404 }
3405 }
3406 } // end disconnect
3407
3408 return NO_ERROR;
3409}
3410
3411
Eric Laurente0720872014-03-11 09:30:41 -07003412void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003413{
3414 ALOGV("closeOutput(%d)", output);
3415
Eric Laurentc75307b2015-03-17 15:29:32 -07003416 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003417 if (outputDesc == NULL) {
3418 ALOGW("closeOutput() unknown output %d", output);
3419 return;
3420 }
François Gaffie036e1e92015-03-19 10:16:24 +01003421 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003422
Eric Laurente552edb2014-03-10 17:42:56 -07003423 // look for duplicated outputs connected to the output being removed.
3424 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003425 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003426 if (dupOutputDesc->isDuplicated() &&
3427 (dupOutputDesc->mOutput1 == outputDesc ||
3428 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003429 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003430 if (dupOutputDesc->mOutput1 == outputDesc) {
3431 outputDesc2 = dupOutputDesc->mOutput2;
3432 } else {
3433 outputDesc2 = dupOutputDesc->mOutput1;
3434 }
3435 // As all active tracks on duplicated output will be deleted,
3436 // and as they were also referenced on the other output, the reference
3437 // count for their stream type must be adjusted accordingly on
3438 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003439 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003440 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003441 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003442 }
3443 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3444 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3445
3446 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003447 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003448 }
3449 }
3450
Eric Laurent05b90f82014-08-27 15:32:29 -07003451 nextAudioPortGeneration();
3452
3453 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3454 if (index >= 0) {
3455 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3456 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3457 mAudioPatches.removeItemsAt(index);
3458 mpClientInterface->onAudioPatchListUpdate();
3459 }
3460
Eric Laurente552edb2014-03-10 17:42:56 -07003461 AudioParameter param;
3462 param.add(String8("closing"), String8("true"));
3463 mpClientInterface->setParameters(output, param.toString());
3464
3465 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003466 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003467 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003468}
3469
3470void AudioPolicyManager::closeInput(audio_io_handle_t input)
3471{
3472 ALOGV("closeInput(%d)", input);
3473
3474 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3475 if (inputDesc == NULL) {
3476 ALOGW("closeInput() unknown input %d", input);
3477 return;
3478 }
3479
Eric Laurent6a94d692014-05-20 11:18:06 -07003480 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003481
3482 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3483 if (index >= 0) {
3484 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3485 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3486 mAudioPatches.removeItemsAt(index);
3487 mpClientInterface->onAudioPatchListUpdate();
3488 }
3489
3490 mpClientInterface->closeInput(input);
3491 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003492}
3493
Eric Laurentc75307b2015-03-17 15:29:32 -07003494SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3495 audio_devices_t device,
3496 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003497{
3498 SortedVector<audio_io_handle_t> outputs;
3499
3500 ALOGVV("getOutputsForDevice() device %04x", device);
3501 for (size_t i = 0; i < openOutputs.size(); i++) {
3502 ALOGVV("output %d isDuplicated=%d device=%04x",
3503 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3504 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3505 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3506 outputs.add(openOutputs.keyAt(i));
3507 }
3508 }
3509 return outputs;
3510}
3511
Eric Laurente0720872014-03-11 09:30:41 -07003512bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003513 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003514{
3515 if (outputs1.size() != outputs2.size()) {
3516 return false;
3517 }
3518 for (size_t i = 0; i < outputs1.size(); i++) {
3519 if (outputs1[i] != outputs2[i]) {
3520 return false;
3521 }
3522 }
3523 return true;
3524}
3525
Eric Laurente0720872014-03-11 09:30:41 -07003526void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003527{
3528 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3529 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3530 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3531 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3532
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003533 // also take into account external policy-related changes: add all outputs which are
3534 // associated with policies in the "before" and "after" output vectors
3535 ALOGVV("checkOutputForStrategy(): policy related outputs");
3536 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003537 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003538 if (desc != 0 && desc->mPolicyMix != NULL) {
3539 srcOutputs.add(desc->mIoHandle);
3540 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3541 }
3542 }
3543 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003544 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003545 if (desc != 0 && desc->mPolicyMix != NULL) {
3546 dstOutputs.add(desc->mIoHandle);
3547 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3548 }
3549 }
3550
Eric Laurente552edb2014-03-10 17:42:56 -07003551 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3552 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3553 strategy, srcOutputs[0], dstOutputs[0]);
3554 // mute strategy while moving tracks from one output to another
3555 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003556 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003557 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003558 setStrategyMute(strategy, true, desc);
3559 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003560 }
3561 }
3562
3563 // Move effects associated to this strategy from previous output to new output
3564 if (strategy == STRATEGY_MEDIA) {
3565 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3566 SortedVector<audio_io_handle_t> moved;
3567 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003568 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3569 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3570 effectDesc->mIo != fxOutput) {
3571 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003572 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3573 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003574 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003575 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003576 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003577 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003578 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003579 }
3580 }
3581 }
3582 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003583 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003584 if (i == AUDIO_STREAM_PATCH) {
3585 continue;
3586 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003587 if (getStrategy((audio_stream_type_t)i) == strategy) {
3588 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003589 }
3590 }
3591 }
3592}
3593
Eric Laurente0720872014-03-11 09:30:41 -07003594void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003595{
François Gaffie2110e042015-03-24 08:41:51 +01003596 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003597 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003598 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003599 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003600 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003601 checkOutputForStrategy(STRATEGY_SONIFICATION);
3602 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003603 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003604 checkOutputForStrategy(STRATEGY_MEDIA);
3605 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003606 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003607}
3608
Eric Laurente0720872014-03-11 09:30:41 -07003609void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003610{
François Gaffie53615e22015-03-19 09:24:12 +01003611 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003612 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003613 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003614 return;
3615 }
3616
Eric Laurent3a4311c2014-03-17 12:00:47 -07003617 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003618 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3619 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3620 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003621 // suspend A2DP output if:
3622 // (NOT already suspended) &&
3623 // ((SCO device is connected &&
3624 // (forced usage for communication || for record is SCO))) ||
3625 // (phone state is ringing || in call)
3626 //
3627 // restore A2DP output if:
3628 // (Already suspended) &&
3629 // ((SCO device is NOT connected ||
3630 // (forced usage NOT for communication && NOT for record is SCO))) &&
3631 // (phone state is NOT ringing && NOT in call)
3632 //
3633 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003634 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003635 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3636 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3637 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3638 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003639
3640 mpClientInterface->restoreOutput(a2dpOutput);
3641 mA2dpSuspended = false;
3642 }
3643 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003644 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003645 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3646 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3647 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3648 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003649
3650 mpClientInterface->suspendOutput(a2dpOutput);
3651 mA2dpSuspended = true;
3652 }
3653 }
3654}
3655
Eric Laurentc75307b2015-03-17 15:29:32 -07003656audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3657 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003658{
3659 audio_devices_t device = AUDIO_DEVICE_NONE;
3660
Eric Laurent6a94d692014-05-20 11:18:06 -07003661 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3662 if (index >= 0) {
3663 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3664 if (patchDesc->mUid != mUidCached) {
3665 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3666 outputDesc->device(), outputDesc->mPatchHandle);
3667 return outputDesc->device();
3668 }
3669 }
3670
Eric Laurente552edb2014-03-10 17:42:56 -07003671 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003672 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003673 // use device for strategy enforced audible
3674 // 2: we are in call or the strategy phone is active on the output:
3675 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003676 // 3: the strategy for enforced audible is active but not enforced on the output:
3677 // use the device for strategy enforced audible
3678 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003679 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003680 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003681 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003682 // 6: the strategy accessibility is active on the output:
3683 // use device for strategy accessibility
3684 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003685 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003686 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003687 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003688 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003689 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003690 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003691 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003692 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3693 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003694 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003695 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003696 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003697 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003698 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003699 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003700 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003701 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003702 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003703 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003704 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003705 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003706 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003707 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003708 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003709 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003710 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003711 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003712 }
3713
Eric Laurent1c333e22014-05-20 10:48:17 -07003714 ALOGV("getNewOutputDevice() selected device %x", device);
3715 return device;
3716}
3717
3718audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3719{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003720 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003721
3722 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3723 if (index >= 0) {
3724 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3725 if (patchDesc->mUid != mUidCached) {
3726 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3727 inputDesc->mDevice, inputDesc->mPatchHandle);
3728 return inputDesc->mDevice;
3729 }
3730 }
3731
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003732 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003733
3734 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003735 return device;
3736}
3737
Eric Laurente0720872014-03-11 09:30:41 -07003738uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003739 return (uint32_t)getStrategy(stream);
3740}
3741
Eric Laurente0720872014-03-11 09:30:41 -07003742audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003743 // By checking the range of stream before calling getStrategy, we avoid
3744 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3745 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003746 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003747 return AUDIO_DEVICE_NONE;
3748 }
3749 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003750 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003751 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3752 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3753 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003754 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003755 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003756 devices = outputDesc->device();
3757 break;
3758 }
Eric Laurente552edb2014-03-10 17:42:56 -07003759 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003760
3761 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3762 and doesn't really need to.*/
3763 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3764 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3765 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3766 }
3767
Eric Laurente552edb2014-03-10 17:42:56 -07003768 return devices;
3769}
3770
François Gaffie2110e042015-03-24 08:41:51 +01003771routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3772{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003773 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003774 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003775}
3776
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003777uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3778 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003779 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3780 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3781 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003782 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3783 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3784 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003785 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003786 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003787}
3788
Eric Laurente0720872014-03-11 09:30:41 -07003789void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003790 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003791 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003792 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3793 updateDevicesAndOutputs();
3794 break;
3795 default:
3796 break;
3797 }
3798}
3799
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003800uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3801 switch(event) {
3802 case STARTING_OUTPUT:
3803 mBeaconMuteRefCount++;
3804 break;
3805 case STOPPING_OUTPUT:
3806 if (mBeaconMuteRefCount > 0) {
3807 mBeaconMuteRefCount--;
3808 }
3809 break;
3810 case STARTING_BEACON:
3811 mBeaconPlayingRefCount++;
3812 break;
3813 case STOPPING_BEACON:
3814 if (mBeaconPlayingRefCount > 0) {
3815 mBeaconPlayingRefCount--;
3816 }
3817 break;
3818 }
3819
3820 if (mBeaconMuteRefCount > 0) {
3821 // any playback causes beacon to be muted
3822 return setBeaconMute(true);
3823 } else {
3824 // no other playback: unmute when beacon starts playing, mute when it stops
3825 return setBeaconMute(mBeaconPlayingRefCount == 0);
3826 }
3827}
3828
3829uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3830 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3831 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3832 // keep track of muted state to avoid repeating mute/unmute operations
3833 if (mBeaconMuted != mute) {
3834 // mute/unmute AUDIO_STREAM_TTS on all outputs
3835 ALOGV("\t muting %d", mute);
3836 uint32_t maxLatency = 0;
3837 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003838 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003839 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07003840 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003841 0 /*delay*/, AUDIO_DEVICE_NONE);
3842 const uint32_t latency = desc->latency() * 2;
3843 if (latency > maxLatency) {
3844 maxLatency = latency;
3845 }
3846 }
3847 mBeaconMuted = mute;
3848 return maxLatency;
3849 }
3850 return 0;
3851}
3852
Eric Laurente0720872014-03-11 09:30:41 -07003853audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01003854 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003855{
Paul McLeanaa981192015-03-21 09:55:15 -07003856 // Routing
3857 // see if we have an explicit route
3858 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
3859 // (getStrategy(stream)).
3860 // if the strategy from the stream type in the RouteMap is the same as the argument above,
3861 // and activity count is non-zero
3862 // the device = the device from the descriptor in the RouteMap, and exit.
3863 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
3864 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
3865 routing_strategy strat = getStrategy(route->mStreamType);
3866 if (strat == strategy && route->mDeviceDescriptor != 0 /*&& route->mActivityCount != 0*/) {
3867 return route->mDeviceDescriptor->type();
3868 }
3869 }
3870
Eric Laurente552edb2014-03-10 17:42:56 -07003871 if (fromCache) {
3872 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3873 strategy, mDeviceForStrategy[strategy]);
3874 return mDeviceForStrategy[strategy];
3875 }
François Gaffie2110e042015-03-24 08:41:51 +01003876 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07003877}
3878
Eric Laurente0720872014-03-11 09:30:41 -07003879void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003880{
3881 for (int i = 0; i < NUM_STRATEGIES; i++) {
3882 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3883 }
3884 mPreviousOutputs = mOutputs;
3885}
3886
Eric Laurent1f2f2232014-06-02 12:01:23 -07003887uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003888 audio_devices_t prevDevice,
3889 uint32_t delayMs)
3890{
3891 // mute/unmute strategies using an incompatible device combination
3892 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3893 // if unmuting, unmute only after the specified delay
3894 if (outputDesc->isDuplicated()) {
3895 return 0;
3896 }
3897
3898 uint32_t muteWaitMs = 0;
3899 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003900 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003901
3902 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3903 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07003904 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07003905 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3906 bool doMute = false;
3907
3908 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3909 doMute = true;
3910 outputDesc->mStrategyMutedByDevice[i] = true;
3911 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3912 doMute = true;
3913 outputDesc->mStrategyMutedByDevice[i] = false;
3914 }
Eric Laurent99401132014-05-07 19:48:15 -07003915 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003916 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003917 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003918 // skip output if it does not share any device with current output
3919 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3920 == AUDIO_DEVICE_NONE) {
3921 continue;
3922 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003923 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
3924 mute ? "muting" : "unmuting", i, curDevice);
3925 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01003926 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003927 if (mute) {
3928 // FIXME: should not need to double latency if volume could be applied
3929 // immediately by the audioflinger mixer. We must account for the delay
3930 // between now and the next time the audioflinger thread for this output
3931 // will process a buffer (which corresponds to one buffer size,
3932 // usually 1/2 or 1/4 of the latency).
3933 if (muteWaitMs < desc->latency() * 2) {
3934 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003935 }
3936 }
3937 }
3938 }
3939 }
3940 }
3941
Eric Laurent99401132014-05-07 19:48:15 -07003942 // temporary mute output if device selection changes to avoid volume bursts due to
3943 // different per device volumes
3944 if (outputDesc->isActive() && (device != prevDevice)) {
3945 if (muteWaitMs < outputDesc->latency() * 2) {
3946 muteWaitMs = outputDesc->latency() * 2;
3947 }
3948 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01003949 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003950 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07003951 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07003952 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07003953 muteWaitMs *2, device);
3954 }
3955 }
3956 }
3957
Eric Laurente552edb2014-03-10 17:42:56 -07003958 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3959 if (muteWaitMs > delayMs) {
3960 muteWaitMs -= delayMs;
3961 usleep(muteWaitMs * 1000);
3962 return muteWaitMs;
3963 }
3964 return 0;
3965}
3966
Eric Laurentc75307b2015-03-17 15:29:32 -07003967uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003968 audio_devices_t device,
3969 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003970 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003971 audio_patch_handle_t *patchHandle,
3972 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07003973{
Eric Laurentc75307b2015-03-17 15:29:32 -07003974 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003975 AudioParameter param;
3976 uint32_t muteWaitMs;
3977
3978 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003979 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
3980 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003981 return muteWaitMs;
3982 }
3983 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
3984 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07003985 if ((device != AUDIO_DEVICE_NONE) &&
3986 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003987 return 0;
3988 }
3989
3990 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07003991 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003992
3993 audio_devices_t prevDevice = outputDesc->mDevice;
3994
Paul McLeanaa981192015-03-21 09:55:15 -07003995 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003996
3997 if (device != AUDIO_DEVICE_NONE) {
3998 outputDesc->mDevice = device;
3999 }
4000 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4001
4002 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004003 // the requested device is AUDIO_DEVICE_NONE
4004 // OR the requested device is the same as current device
4005 // AND force is not specified
4006 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004007 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004008 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4009 !force &&
4010 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004011 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004012 return muteWaitMs;
4013 }
4014
4015 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004016
Eric Laurente552edb2014-03-10 17:42:56 -07004017 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004018 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004019 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004020 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004021 DeviceVector deviceList = (address == NULL) ?
4022 mAvailableOutputDevices.getDevicesFromType(device)
4023 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004024 if (!deviceList.isEmpty()) {
4025 struct audio_patch patch;
4026 outputDesc->toAudioPortConfig(&patch.sources[0]);
4027 patch.num_sources = 1;
4028 patch.num_sinks = 0;
4029 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4030 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004031 patch.num_sinks++;
4032 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004033 ssize_t index;
4034 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4035 index = mAudioPatches.indexOfKey(*patchHandle);
4036 } else {
4037 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4038 }
4039 sp< AudioPatch> patchDesc;
4040 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4041 if (index >= 0) {
4042 patchDesc = mAudioPatches.valueAt(index);
4043 afPatchHandle = patchDesc->mAfPatchHandle;
4044 }
4045
Eric Laurent1c333e22014-05-20 10:48:17 -07004046 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004047 &afPatchHandle,
4048 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004049 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4050 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004051 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004052 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004053 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004054 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004055 addAudioPatch(patchDesc->mHandle, patchDesc);
4056 } else {
4057 patchDesc->mPatch = patch;
4058 }
4059 patchDesc->mAfPatchHandle = afPatchHandle;
4060 patchDesc->mUid = mUidCached;
4061 if (patchHandle) {
4062 *patchHandle = patchDesc->mHandle;
4063 }
4064 outputDesc->mPatchHandle = patchDesc->mHandle;
4065 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004066 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004067 }
4068 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004069
4070 // inform all input as well
4071 for (size_t i = 0; i < mInputs.size(); i++) {
4072 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004073 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004074 AudioParameter inputCmd = AudioParameter();
4075 ALOGV("%s: inform input %d of device:%d", __func__,
4076 inputDescriptor->mIoHandle, device);
4077 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4078 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4079 inputCmd.toString(),
4080 delayMs);
4081 }
4082 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004083 }
Eric Laurente552edb2014-03-10 17:42:56 -07004084
4085 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004086 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004087
4088 return muteWaitMs;
4089}
4090
Eric Laurentc75307b2015-03-17 15:29:32 -07004091status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004092 int delayMs,
4093 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004094{
Eric Laurent6a94d692014-05-20 11:18:06 -07004095 ssize_t index;
4096 if (patchHandle) {
4097 index = mAudioPatches.indexOfKey(*patchHandle);
4098 } else {
4099 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4100 }
4101 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004102 return INVALID_OPERATION;
4103 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004104 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4105 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004106 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4107 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004108 removeAudioPatch(patchDesc->mHandle);
4109 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004110 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004111 return status;
4112}
4113
4114status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4115 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004116 bool force,
4117 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004118{
4119 status_t status = NO_ERROR;
4120
Eric Laurent1f2f2232014-06-02 12:01:23 -07004121 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004122 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4123 inputDesc->mDevice = device;
4124
4125 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4126 if (!deviceList.isEmpty()) {
4127 struct audio_patch patch;
4128 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004129 // AUDIO_SOURCE_HOTWORD is for internal use only:
4130 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004131 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4132 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004133 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4134 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004135 patch.num_sinks = 1;
4136 //only one input device for now
4137 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004138 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004139 ssize_t index;
4140 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4141 index = mAudioPatches.indexOfKey(*patchHandle);
4142 } else {
4143 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4144 }
4145 sp< AudioPatch> patchDesc;
4146 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4147 if (index >= 0) {
4148 patchDesc = mAudioPatches.valueAt(index);
4149 afPatchHandle = patchDesc->mAfPatchHandle;
4150 }
4151
Eric Laurent1c333e22014-05-20 10:48:17 -07004152 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004153 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004154 0);
4155 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004156 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004157 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004158 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004159 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004160 addAudioPatch(patchDesc->mHandle, patchDesc);
4161 } else {
4162 patchDesc->mPatch = patch;
4163 }
4164 patchDesc->mAfPatchHandle = afPatchHandle;
4165 patchDesc->mUid = mUidCached;
4166 if (patchHandle) {
4167 *patchHandle = patchDesc->mHandle;
4168 }
4169 inputDesc->mPatchHandle = patchDesc->mHandle;
4170 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004171 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004172 }
4173 }
4174 }
4175 return status;
4176}
4177
Eric Laurent6a94d692014-05-20 11:18:06 -07004178status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4179 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004180{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004181 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004182 ssize_t index;
4183 if (patchHandle) {
4184 index = mAudioPatches.indexOfKey(*patchHandle);
4185 } else {
4186 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4187 }
4188 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004189 return INVALID_OPERATION;
4190 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004191 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4192 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004193 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4194 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004195 removeAudioPatch(patchDesc->mHandle);
4196 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004197 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004198 return status;
4199}
4200
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004201sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004202 String8 address,
4203 uint32_t& samplingRate,
4204 audio_format_t format,
4205 audio_channel_mask_t channelMask,
4206 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004207{
4208 // Choose an input profile based on the requested capture parameters: select the first available
4209 // profile supporting all requested parameters.
4210
4211 for (size_t i = 0; i < mHwModules.size(); i++)
4212 {
4213 if (mHwModules[i]->mHandle == 0) {
4214 continue;
4215 }
4216 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4217 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004218 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004219 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004220 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004221 &samplingRate /*updatedSamplingRate*/,
4222 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004223
Eric Laurente552edb2014-03-10 17:42:56 -07004224 return profile;
4225 }
4226 }
4227 }
4228 return NULL;
4229}
4230
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004231
4232audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004233 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004234{
François Gaffie036e1e92015-03-19 10:16:24 +01004235 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4236 audio_devices_t selectedDeviceFromMix =
4237 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004238
François Gaffie036e1e92015-03-19 10:16:24 +01004239 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4240 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004241 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004242 return getDeviceForInputSource(inputSource);
4243}
4244
4245audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4246{
François Gaffie2110e042015-03-24 08:41:51 +01004247 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004248}
4249
Eric Laurente0720872014-03-11 09:30:41 -07004250float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004251 int index,
4252 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004253{
4254 float volume = 1.0;
Eric Laurente552edb2014-03-10 17:42:56 -07004255
François Gaffie2110e042015-03-24 08:41:51 +01004256 volume = mEngine->volIndexToAmpl(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004257
4258 // if a headset is connected, apply the following rules to ring tones and notifications
4259 // to avoid sound level bursts in user's ears:
4260 // - always attenuate ring tones and notifications volume by 6dB
4261 // - if music is playing, always limit the volume to current music volume,
4262 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004263 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004264 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4265 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4266 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4267 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4268 ((stream_strategy == STRATEGY_SONIFICATION)
4269 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004270 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004271 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004272 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4273 mStreams.canBeMuted(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004274 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4275 // when the phone is ringing we must consider that music could have been paused just before
4276 // by the music application and behave as if music was active if the last music track was
4277 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004278 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004279 mLimitRingtoneVolume) {
4280 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004281 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
Eric Laurentc75307b2015-03-17 15:29:32 -07004282 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004283 musicDevice);
4284 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4285 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4286 if (volume > minVol) {
4287 volume = minVol;
4288 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4289 }
4290 }
4291 }
4292
4293 return volume;
4294}
4295
Eric Laurente0720872014-03-11 09:30:41 -07004296status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004297 int index,
4298 const sp<AudioOutputDescriptor>& outputDesc,
4299 audio_devices_t device,
4300 int delayMs,
4301 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004302{
Eric Laurente552edb2014-03-10 17:42:56 -07004303 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004304 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004305 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004306 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004307 return NO_ERROR;
4308 }
François Gaffie2110e042015-03-24 08:41:51 +01004309 audio_policy_forced_cfg_t forceUseForComm =
4310 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004311 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004312 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4313 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004314 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004315 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004316 return INVALID_OPERATION;
4317 }
4318
Eric Laurentc75307b2015-03-17 15:29:32 -07004319 if (device == AUDIO_DEVICE_NONE) {
4320 device = outputDesc->device();
4321 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004322
Eric Laurentc75307b2015-03-17 15:29:32 -07004323 float volume = computeVolume(stream, index, device);
4324 if (outputDesc->isFixedVolume(device)) {
4325 volume = 1.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004326 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004327
4328 outputDesc->setVolume(volume, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004329
Eric Laurent3b73df72014-03-11 09:06:29 -07004330 if (stream == AUDIO_STREAM_VOICE_CALL ||
4331 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004332 float voiceVolume;
4333 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004334 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004335 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004336 } else {
4337 voiceVolume = 1.0;
4338 }
4339
Eric Laurentc75307b2015-03-17 15:29:32 -07004340 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004341 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4342 mLastVoiceVolume = voiceVolume;
4343 }
4344 }
4345
4346 return NO_ERROR;
4347}
4348
Eric Laurentc75307b2015-03-17 15:29:32 -07004349void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4350 audio_devices_t device,
4351 int delayMs,
4352 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004353{
Eric Laurentc75307b2015-03-17 15:29:32 -07004354 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004355
Eric Laurent3b73df72014-03-11 09:06:29 -07004356 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004357 if (stream == AUDIO_STREAM_PATCH) {
4358 continue;
4359 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004360 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004361 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4362 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004363 device,
4364 delayMs,
4365 force);
4366 }
4367}
4368
Eric Laurente0720872014-03-11 09:30:41 -07004369void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004370 bool on,
4371 const sp<AudioOutputDescriptor>& outputDesc,
4372 int delayMs,
4373 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004374{
4375 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004376 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004377 if (stream == AUDIO_STREAM_PATCH) {
4378 continue;
4379 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004380 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004381 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004382 }
4383 }
4384}
4385
Eric Laurente0720872014-03-11 09:30:41 -07004386void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004387 bool on,
4388 const sp<AudioOutputDescriptor>& outputDesc,
4389 int delayMs,
4390 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004391{
Eric Laurentc75307b2015-03-17 15:29:32 -07004392 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004393 if (device == AUDIO_DEVICE_NONE) {
4394 device = outputDesc->device();
4395 }
4396
Eric Laurentc75307b2015-03-17 15:29:32 -07004397 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4398 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004399
4400 if (on) {
4401 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004402 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004403 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004404 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004405 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004406 }
4407 }
4408 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4409 outputDesc->mMuteCount[stream]++;
4410 } else {
4411 if (outputDesc->mMuteCount[stream] == 0) {
4412 ALOGV("setStreamMute() unmuting non muted stream!");
4413 return;
4414 }
4415 if (--outputDesc->mMuteCount[stream] == 0) {
4416 checkAndSetVolume(stream,
4417 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004418 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004419 device,
4420 delayMs);
4421 }
4422 }
4423}
4424
Eric Laurente0720872014-03-11 09:30:41 -07004425void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004426 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004427{
4428 // if the stream pertains to sonification strategy and we are in call we must
4429 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4430 // in the device used for phone strategy and play the tone if the selected device does not
4431 // interfere with the device used for phone strategy
4432 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4433 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004434 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004435 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4436 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004437 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004438 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4439 stream, starting, outputDesc->mDevice, stateChange);
4440 if (outputDesc->mRefCount[stream]) {
4441 int muteCount = 1;
4442 if (stateChange) {
4443 muteCount = outputDesc->mRefCount[stream];
4444 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004445 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004446 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4447 for (int i = 0; i < muteCount; i++) {
4448 setStreamMute(stream, starting, mPrimaryOutput);
4449 }
4450 } else {
4451 ALOGV("handleIncallSonification() high visibility");
4452 if (outputDesc->device() &
4453 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4454 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4455 for (int i = 0; i < muteCount; i++) {
4456 setStreamMute(stream, starting, mPrimaryOutput);
4457 }
4458 }
4459 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004460 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4461 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004462 } else {
4463 mpClientInterface->stopTone();
4464 }
4465 }
4466 }
4467 }
4468}
4469
Paul McLeanaa981192015-03-21 09:55:15 -07004470// --- SessionRoute class implementation
4471void AudioPolicyManager::SessionRoute::log(const char* prefix) {
4472 ALOGI("%s[SessionRoute strm:0x%X, sess:0x%X, dev:0x%X refs:%d act:%d",
4473 prefix, mStreamType, mSession,
4474 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4475 mRefCount, mActivityCount);
4476}
4477
4478// --- SessionRouteMap class implementation
4479bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4480{
4481 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4482}
4483
4484void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4485 audio_stream_type_t streamType,
4486 sp<DeviceDescriptor> deviceDescriptor)
4487{
4488 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4489 if (route != NULL) {
4490 route->mRefCount++;
4491 route->mDeviceDescriptor = deviceDescriptor;
4492 } else {
4493 route = new AudioPolicyManager::SessionRoute(session, streamType, deviceDescriptor);
4494 route->mRefCount++;
4495 add(session, route);
4496 }
4497}
4498
4499void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4500{
4501 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4502 if (route != 0) {
4503 ALOG_ASSERT(route->mRefCount > 0);
4504 --route->mRefCount;
4505 if (route->mRefCount <= 0) {
4506 removeItem(session);
4507 }
4508 }
4509}
4510
4511int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4512{
4513 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4514 return route != 0 ? ++(route->mActivityCount) : -1;
4515}
4516
4517int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4518{
4519 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4520 if (route != 0 && route->mActivityCount > 0) {
4521 return --(route->mActivityCount);
4522 } else {
4523 return -1;
4524 }
4525}
4526
4527void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4528 ALOGI("%s ----", caption);
4529 for(size_t index = 0; index < size(); index++) {
4530 valueAt(index)->log(" ");
4531 }
4532}
4533
Eric Laurente0720872014-03-11 09:30:41 -07004534void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004535{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004536 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004537 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004538 sp<DeviceDescriptor> defaultInputDevice =
4539 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004540 mAvailableOutputDevices.add(mDefaultOutputDevice);
4541 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004542
4543 module = new HwModule("primary");
4544
Eric Laurent322b4d22015-04-03 15:57:54 -07004545 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4546 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004547 profile->mSamplingRates.add(44100);
4548 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4549 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004550 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004551 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4552 module->mOutputProfiles.add(profile);
4553
Eric Laurent322b4d22015-04-03 15:57:54 -07004554 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4555 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004556 profile->mSamplingRates.add(8000);
4557 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4558 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004559 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004560 module->mInputProfiles.add(profile);
4561
4562 mHwModules.add(module);
4563}
4564
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004565audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4566{
4567 // flags to stream type mapping
4568 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4569 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4570 }
4571 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4572 return AUDIO_STREAM_BLUETOOTH_SCO;
4573 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004574 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4575 return AUDIO_STREAM_TTS;
4576 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004577
4578 // usage to stream type mapping
4579 switch (attr->usage) {
4580 case AUDIO_USAGE_MEDIA:
4581 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004582 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4583 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004584 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004585 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4586 return AUDIO_STREAM_ALARM;
4587 }
4588 if (isStreamActive(AUDIO_STREAM_RING)) {
4589 return AUDIO_STREAM_RING;
4590 }
4591 if (isInCall()) {
4592 return AUDIO_STREAM_VOICE_CALL;
4593 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004594 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004595 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4596 return AUDIO_STREAM_SYSTEM;
4597 case AUDIO_USAGE_VOICE_COMMUNICATION:
4598 return AUDIO_STREAM_VOICE_CALL;
4599
4600 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4601 return AUDIO_STREAM_DTMF;
4602
4603 case AUDIO_USAGE_ALARM:
4604 return AUDIO_STREAM_ALARM;
4605 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4606 return AUDIO_STREAM_RING;
4607
4608 case AUDIO_USAGE_NOTIFICATION:
4609 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4610 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4611 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4612 case AUDIO_USAGE_NOTIFICATION_EVENT:
4613 return AUDIO_STREAM_NOTIFICATION;
4614
4615 case AUDIO_USAGE_UNKNOWN:
4616 default:
4617 return AUDIO_STREAM_MUSIC;
4618 }
4619}
Eric Laurente83b55d2014-11-14 10:06:21 -08004620
François Gaffie53615e22015-03-19 09:24:12 +01004621bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4622{
Eric Laurente83b55d2014-11-14 10:06:21 -08004623 // has flags that map to a strategy?
4624 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4625 return true;
4626 }
4627
4628 // has known usage?
4629 switch (paa->usage) {
4630 case AUDIO_USAGE_UNKNOWN:
4631 case AUDIO_USAGE_MEDIA:
4632 case AUDIO_USAGE_VOICE_COMMUNICATION:
4633 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4634 case AUDIO_USAGE_ALARM:
4635 case AUDIO_USAGE_NOTIFICATION:
4636 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4637 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4638 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4639 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4640 case AUDIO_USAGE_NOTIFICATION_EVENT:
4641 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4642 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4643 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4644 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004645 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004646 break;
4647 default:
4648 return false;
4649 }
4650 return true;
4651}
4652
François Gaffiead3183e2015-03-18 16:55:35 +01004653bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4654 routing_strategy strategy, uint32_t inPastMs,
4655 nsecs_t sysTime) const
4656{
4657 if ((sysTime == 0) && (inPastMs != 0)) {
4658 sysTime = systemTime();
4659 }
4660 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4661 if (i == AUDIO_STREAM_PATCH) {
4662 continue;
4663 }
4664 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4665 (NUM_STRATEGIES == strategy)) &&
4666 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4667 return true;
4668 }
4669 }
4670 return false;
4671}
4672
François Gaffie2110e042015-03-24 08:41:51 +01004673audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4674{
4675 return mEngine->getForceUse(usage);
4676}
4677
4678bool AudioPolicyManager::isInCall()
4679{
4680 return isStateInCall(mEngine->getPhoneState());
4681}
4682
4683bool AudioPolicyManager::isStateInCall(int state)
4684{
4685 return is_state_in_call(state);
4686}
4687
Eric Laurente552edb2014-03-10 17:42:56 -07004688}; // namespace android