blob: 01f2b6106402348859535151bdd57868cadedb9d [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
François Gaffie2110e042015-03-24 08:41:51 +0100264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800305 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700306 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
307
308 // release existing RX patch if any
309 if (mCallRxPatch != 0) {
310 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
311 mCallRxPatch.clear();
312 }
313 // release TX patch if any
314 if (mCallTxPatch != 0) {
315 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
316 mCallTxPatch.clear();
317 }
318
319 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
320 // via setOutputDevice() on primary output.
321 // Otherwise, create two audio patches for TX and RX path.
322 if (availablePrimaryOutputDevices() & rxDevice) {
323 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
324 // If the TX device is also on the primary HW module, setOutputDevice() will take care
325 // of it due to legacy implementation. If not, create a patch.
326 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
327 == AUDIO_DEVICE_NONE) {
328 createTxPatch = true;
329 }
330 } else {
331 // create RX path audio patch
332 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
333 ALOG_ASSERT(!deviceList.isEmpty(),
334 "updateCallRouting() selected device not in output device list");
335 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
336 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
337 ALOG_ASSERT(!deviceList.isEmpty(),
338 "updateCallRouting() no telephony RX device");
339 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
340
341 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
342 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
343
344 // request to reuse existing output stream if one is already opened to reach the RX device
345 SortedVector<audio_io_handle_t> outputs =
346 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700347 audio_io_handle_t output = selectOutput(outputs,
348 AUDIO_OUTPUT_FLAG_NONE,
349 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700350 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700351 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700352 ALOG_ASSERT(!outputDesc->isDuplicated(),
353 "updateCallRouting() RX device output is duplicated");
354 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700355 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700356 patch.num_sources = 2;
357 }
358
359 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
360 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
361 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
362 status);
363 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100364 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700365 mCallRxPatch->mAfPatchHandle = afPatchHandle;
366 mCallRxPatch->mUid = mUidCached;
367 }
368 createTxPatch = true;
369 }
370 if (createTxPatch) {
371
372 struct audio_patch patch;
373 patch.num_sources = 1;
374 patch.num_sinks = 1;
375 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
376 ALOG_ASSERT(!deviceList.isEmpty(),
377 "updateCallRouting() selected device not in input device list");
378 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
379 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
380 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
381 ALOG_ASSERT(!deviceList.isEmpty(),
382 "updateCallRouting() no telephony TX device");
383 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
384 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
385
386 SortedVector<audio_io_handle_t> outputs =
387 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700388 audio_io_handle_t output = selectOutput(outputs,
389 AUDIO_OUTPUT_FLAG_NONE,
390 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700391 // request to reuse existing output stream if one is already opened to reach the TX
392 // path output device
393 if (output != AUDIO_IO_HANDLE_NONE) {
394 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
395 ALOG_ASSERT(!outputDesc->isDuplicated(),
396 "updateCallRouting() RX device output is duplicated");
397 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700398 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700399 patch.num_sources = 2;
400 }
401
402 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
403 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
404 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
405 status);
406 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100407 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700408 mCallTxPatch->mAfPatchHandle = afPatchHandle;
409 mCallTxPatch->mUid = mUidCached;
410 }
411 }
412}
413
Eric Laurente0720872014-03-11 09:30:41 -0700414void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700415{
416 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100417 // store previous phone state for management of sonification strategy below
418 int oldState = mEngine->getPhoneState();
419
420 if (mEngine->setPhoneState(state) != NO_ERROR) {
421 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700422 return;
423 }
François Gaffie2110e042015-03-24 08:41:51 +0100424 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700425 // if leaving call state, handle special case of active streams
426 // pertaining to sonification strategy see handleIncallSonification()
427 if (isInCall()) {
428 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (stream == AUDIO_STREAM_PATCH) {
431 continue;
432 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700433 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800435
436 // force reevaluating accessibility routing when call starts
437 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 /**
441 * Switching to or from incall state or switching between telephony and VoIP lead to force
442 * routing command.
443 */
444 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
445 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700446
447 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700448 checkA2dpSuspend();
449 checkOutputForAllStrategies();
450 updateDevicesAndOutputs();
451
Eric Laurentc75307b2015-03-17 15:29:32 -0700452 sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -0700453
Eric Laurente552edb2014-03-10 17:42:56 -0700454 int delayMs = 0;
455 if (isStateInCall(state)) {
456 nsecs_t sysTime = systemTime();
457 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700458 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700459 // mute media and sonification strategies and delay device switch by the largest
460 // latency of any output where either strategy is active.
461 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100462 if ((isStrategyActive(desc, STRATEGY_MEDIA,
463 SONIFICATION_HEADSET_MUSIC_DELAY,
464 sysTime) ||
465 isStrategyActive(desc, STRATEGY_SONIFICATION,
466 SONIFICATION_HEADSET_MUSIC_DELAY,
467 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700468 (delayMs < (int)desc->latency()*2)) {
469 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700470 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700471 setStrategyMute(STRATEGY_MEDIA, true, desc);
472 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700473 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700474 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
475 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700476 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
477 }
478 }
479
Eric Laurentc2730ba2014-07-20 15:47:07 -0700480 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
481 // the device returned is not necessarily reachable via this output
482 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
483 // force routing command to audio hardware when ending call
484 // even if no device change is needed
485 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
486 rxDevice = hwOutputDesc->device();
487 }
Eric Laurente552edb2014-03-10 17:42:56 -0700488
Eric Laurentc2730ba2014-07-20 15:47:07 -0700489 if (state == AUDIO_MODE_IN_CALL) {
490 updateCallRouting(rxDevice, delayMs);
491 } else if (oldState == AUDIO_MODE_IN_CALL) {
492 if (mCallRxPatch != 0) {
493 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
494 mCallRxPatch.clear();
495 }
496 if (mCallTxPatch != 0) {
497 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
498 mCallTxPatch.clear();
499 }
500 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
501 } else {
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 }
Eric Laurente552edb2014-03-10 17:42:56 -0700504 // if entering in call state, handle special case of active streams
505 // pertaining to sonification strategy see handleIncallSonification()
506 if (isStateInCall(state)) {
507 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700508 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800509 if (stream == AUDIO_STREAM_PATCH) {
510 continue;
511 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700512 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700513 }
514 }
515
516 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700517 if (state == AUDIO_MODE_RINGTONE &&
518 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 mLimitRingtoneVolume = true;
520 } else {
521 mLimitRingtoneVolume = false;
522 }
523}
524
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700525audio_mode_t AudioPolicyManager::getPhoneState() {
526 return mEngine->getPhoneState();
527}
528
Eric Laurente0720872014-03-11 09:30:41 -0700529void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700530 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700531{
François Gaffie2110e042015-03-24 08:41:51 +0100532 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700533
François Gaffie2110e042015-03-24 08:41:51 +0100534 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
535 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
536 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700537 }
François Gaffie2110e042015-03-24 08:41:51 +0100538 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
539 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
540 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700541
542 // check for device and output changes triggered by new force usage
543 checkA2dpSuspend();
544 checkOutputForAllStrategies();
545 updateDevicesAndOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100546 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700547 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
548 updateCallRouting(newDevice);
549 }
Eric Laurente552edb2014-03-10 17:42:56 -0700550 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700551 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
552 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
553 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
554 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700555 }
Eric Laurente552edb2014-03-10 17:42:56 -0700556 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700558 }
559 }
560
François Gaffie53615e22015-03-19 09:24:12 +0100561 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700562 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700563 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700564 }
565
566}
567
Eric Laurente0720872014-03-11 09:30:41 -0700568void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700569{
570 ALOGV("setSystemProperty() property %s, value %s", property, value);
571}
572
573// Find a direct output profile compatible with the parameters passed, even if the input flags do
574// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700576 audio_devices_t device,
577 uint32_t samplingRate,
578 audio_format_t format,
579 audio_channel_mask_t channelMask,
580 audio_output_flags_t flags)
581{
582 for (size_t i = 0; i < mHwModules.size(); i++) {
583 if (mHwModules[i]->mHandle == 0) {
584 continue;
585 }
586 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700587 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Andy Hungf129b032015-04-07 13:45:50 -0700588 bool found = profile->isCompatibleProfile(device, String8(""),
589 samplingRate, NULL /*updatedSamplingRate*/,
590 format, NULL /*updatedFormat*/,
591 channelMask, NULL /*updatedChannelMask*/,
Glenn Kastencbd48022014-07-24 13:46:44 -0700592 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
593 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700594 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
595 return profile;
596 }
Eric Laurente552edb2014-03-10 17:42:56 -0700597 }
598 }
599 return 0;
600}
601
Eric Laurente0720872014-03-11 09:30:41 -0700602audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags,
607 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700608{
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
611 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
612 device, stream, samplingRate, format, channelMask, flags);
613
Eric Laurente83b55d2014-11-14 10:06:21 -0800614 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
615 stream, samplingRate,format, channelMask,
616 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700617}
618
Eric Laurente83b55d2014-11-14 10:06:21 -0800619status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
620 audio_io_handle_t *output,
621 audio_session_t session,
622 audio_stream_type_t *stream,
623 uint32_t samplingRate,
624 audio_format_t format,
625 audio_channel_mask_t channelMask,
626 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700627 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800628 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700629{
Eric Laurente83b55d2014-11-14 10:06:21 -0800630 audio_attributes_t attributes;
631 if (attr != NULL) {
632 if (!isValidAttributes(attr)) {
633 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
634 attr->usage, attr->content_type, attr->flags,
635 attr->tags);
636 return BAD_VALUE;
637 }
638 attributes = *attr;
639 } else {
640 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
641 ALOGE("getOutputForAttr(): invalid stream type");
642 return BAD_VALUE;
643 }
644 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700645 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700646 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100647 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
648 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
649 if (!audio_is_linear_pcm(format)) {
650 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800651 }
François Gaffie036e1e92015-03-19 10:16:24 +0100652 *stream = streamTypefromAttributesInt(&attributes);
653 *output = desc->mIoHandle;
654 ALOGV("getOutputForAttr() returns output %d", *output);
655 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800656 }
657 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
658 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
659 return BAD_VALUE;
660 }
661
Eric Laurent93c3d412014-08-01 14:48:35 -0700662 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
François Gaffie53615e22015-03-19 09:24:12 +0100663 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700664
Eric Laurente83b55d2014-11-14 10:06:21 -0800665 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700666 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700667
Eric Laurente83b55d2014-11-14 10:06:21 -0800668 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700669 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
670 }
671
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700672 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700673 device, samplingRate, format, channelMask, flags);
674
Eric Laurente83b55d2014-11-14 10:06:21 -0800675 *stream = streamTypefromAttributesInt(&attributes);
676 *output = getOutputForDevice(device, session, *stream,
677 samplingRate, format, channelMask,
678 flags, offloadInfo);
679 if (*output == AUDIO_IO_HANDLE_NONE) {
680 return INVALID_OPERATION;
681 }
Paul McLeanaa981192015-03-21 09:55:15 -0700682
683 // Explicit routing?
684 sp<DeviceDescriptor> deviceDesc;
685
686 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent322b4d22015-04-03 15:57:54 -0700687 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
Paul McLeanaa981192015-03-21 09:55:15 -0700688 deviceDesc = mAvailableOutputDevices[i];
689 break;
690 }
691 }
692 mOutputRoutes.addRoute(session, *stream, deviceDesc);
Eric Laurente83b55d2014-11-14 10:06:21 -0800693 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700694}
695
696audio_io_handle_t AudioPolicyManager::getOutputForDevice(
697 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800698 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700699 audio_stream_type_t stream,
700 uint32_t samplingRate,
701 audio_format_t format,
702 audio_channel_mask_t channelMask,
703 audio_output_flags_t flags,
704 const audio_offload_info_t *offloadInfo)
705{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700706 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700708 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700709
Eric Laurente552edb2014-03-10 17:42:56 -0700710#ifdef AUDIO_POLICY_TEST
711 if (mCurOutput != 0) {
712 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
713 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
714
715 if (mTestOutputs[mCurOutput] == 0) {
716 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700717 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
718 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700719 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700720 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700721 outputDesc->mFlags =
722 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700723 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700724 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
725 config.sample_rate = mTestSamplingRate;
726 config.channel_mask = mTestChannels;
727 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700728 if (offloadInfo != NULL) {
729 config.offload_info = *offloadInfo;
730 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700731 status = mpClientInterface->openOutput(0,
732 &mTestOutputs[mCurOutput],
733 &config,
734 &outputDesc->mDevice,
735 String8(""),
736 &outputDesc->mLatency,
737 outputDesc->mFlags);
738 if (status == NO_ERROR) {
739 outputDesc->mSamplingRate = config.sample_rate;
740 outputDesc->mFormat = config.format;
741 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700742 AudioParameter outputCmd = AudioParameter();
743 outputCmd.addInt(String8("set_id"),mCurOutput);
744 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
745 addOutput(mTestOutputs[mCurOutput], outputDesc);
746 }
747 }
748 return mTestOutputs[mCurOutput];
749 }
750#endif //AUDIO_POLICY_TEST
751
752 // open a direct output if required by specified parameters
753 //force direct flag if offload flag is set: offloading implies a direct output stream
754 // and all common behaviors are driven by checking only the direct flag
755 // this should normally be set appropriately in the policy configuration file
756 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700757 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700758 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700759 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
760 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
761 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800762 // only allow deep buffering for music stream type
763 if (stream != AUDIO_STREAM_MUSIC) {
764 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
765 }
Eric Laurente552edb2014-03-10 17:42:56 -0700766
Eric Laurentb732cf52014-09-24 19:08:21 -0700767 sp<IOProfile> profile;
768
769 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700770 // and not explicitly requested
771 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
772 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700773 audio_channel_count_from_out_mask(channelMask) <= 2) {
774 goto non_direct_output;
775 }
776
Eric Laurente552edb2014-03-10 17:42:56 -0700777 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
778 // creating an offloaded track and tearing it down immediately after start when audioflinger
779 // detects there is an active non offloadable effect.
780 // FIXME: We should check the audio session here but we do not have it in this context.
781 // This may prevent offloading in rare situations where effects are left active by apps
782 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700783
Eric Laurente552edb2014-03-10 17:42:56 -0700784 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100785 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700786 profile = getProfileForDirectOutput(device,
787 samplingRate,
788 format,
789 channelMask,
790 (audio_output_flags_t)flags);
791 }
792
Eric Laurent1c333e22014-05-20 10:48:17 -0700793 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700794 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700795
796 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700797 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700798 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
799 outputDesc = desc;
800 // reuse direct output if currently open and configured with same parameters
801 if ((samplingRate == outputDesc->mSamplingRate) &&
802 (format == outputDesc->mFormat) &&
803 (channelMask == outputDesc->mChannelMask)) {
804 outputDesc->mDirectOpenCount++;
805 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
806 return mOutputs.keyAt(i);
807 }
808 }
809 }
810 // close direct output if currently open and configured with different parameters
811 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700812 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700813 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700814 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700815 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700816 outputDesc->mLatency = 0;
817 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700818 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
819 config.sample_rate = samplingRate;
820 config.channel_mask = channelMask;
821 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700822 if (offloadInfo != NULL) {
823 config.offload_info = *offloadInfo;
824 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700825 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700826 &output,
827 &config,
828 &outputDesc->mDevice,
829 String8(""),
830 &outputDesc->mLatency,
831 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700832
833 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700834 if (status != NO_ERROR ||
835 (samplingRate != 0 && samplingRate != config.sample_rate) ||
836 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
837 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700838 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
839 "format %d %d, channelMask %04x %04x", output, samplingRate,
840 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
841 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700842 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700843 mpClientInterface->closeOutput(output);
844 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800845 // fall back to mixer output if possible when the direct output could not be open
846 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
847 goto non_direct_output;
848 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800849 // fall back to mixer output if possible when the direct output could not be open
850 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
851 goto non_direct_output;
852 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700853 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700854 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700855 outputDesc->mSamplingRate = config.sample_rate;
856 outputDesc->mChannelMask = config.channel_mask;
857 outputDesc->mFormat = config.format;
858 outputDesc->mRefCount[stream] = 0;
859 outputDesc->mStopTime[stream] = 0;
860 outputDesc->mDirectOpenCount = 1;
861
Eric Laurente552edb2014-03-10 17:42:56 -0700862 audio_io_handle_t srcOutput = getOutputForEffect();
863 addOutput(output, outputDesc);
864 audio_io_handle_t dstOutput = getOutputForEffect();
865 if (dstOutput == output) {
866 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
867 }
868 mPreviousOutputs = mOutputs;
869 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700870 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700871 return output;
872 }
873
Eric Laurentb732cf52014-09-24 19:08:21 -0700874non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700875 // ignoring channel mask due to downmix capability in mixer
876
877 // open a non direct output
878
879 // for non direct outputs, only PCM is supported
880 if (audio_is_linear_pcm(format)) {
881 // get which output is suitable for the specified stream. The actual
882 // routing change will happen when startOutput() will be called
883 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
884
Eric Laurent8838a382014-09-08 16:44:28 -0700885 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
886 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
887 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700888 }
889 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
890 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
891
Paul McLeanaa981192015-03-21 09:55:15 -0700892 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700893
894 return output;
895}
896
Eric Laurente0720872014-03-11 09:30:41 -0700897audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700898 audio_output_flags_t flags,
899 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700900{
901 // select one output among several that provide a path to a particular device or set of
902 // devices (the list was previously build by getOutputsForDevice()).
903 // The priority is as follows:
904 // 1: the output with the highest number of requested policy flags
905 // 2: the primary output
906 // 3: the first output in the list
907
908 if (outputs.size() == 0) {
909 return 0;
910 }
911 if (outputs.size() == 1) {
912 return outputs[0];
913 }
914
915 int maxCommonFlags = 0;
916 audio_io_handle_t outputFlags = 0;
917 audio_io_handle_t outputPrimary = 0;
918
919 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700920 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700921 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700922 // if a valid format is specified, skip output if not compatible
923 if (format != AUDIO_FORMAT_INVALID) {
924 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
925 if (format != outputDesc->mFormat) {
926 continue;
927 }
928 } else if (!audio_is_linear_pcm(format)) {
929 continue;
930 }
931 }
932
Eric Laurent3b73df72014-03-11 09:06:29 -0700933 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700934 if (commonFlags > maxCommonFlags) {
935 outputFlags = outputs[i];
936 maxCommonFlags = commonFlags;
937 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
938 }
939 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
940 outputPrimary = outputs[i];
941 }
942 }
943 }
944
945 if (outputFlags != 0) {
946 return outputFlags;
947 }
948 if (outputPrimary != 0) {
949 return outputPrimary;
950 }
951
952 return outputs[0];
953}
954
Eric Laurente0720872014-03-11 09:30:41 -0700955status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700956 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800957 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700958{
Paul McLeanaa981192015-03-21 09:55:15 -0700959 ALOGV("startOutput() output %d, stream %d, session %d",
960 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700961 ssize_t index = mOutputs.indexOfKey(output);
962 if (index < 0) {
963 ALOGW("startOutput() unknown output %d", output);
964 return BAD_VALUE;
965 }
966
Eric Laurentc75307b2015-03-17 15:29:32 -0700967 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
968
969 audio_devices_t newDevice;
970 if (outputDesc->mPolicyMix != NULL) {
971 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -0700972 } else if (mOutputRoutes.hasRouteChanged(session)) {
973 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -0700974 } else {
975 newDevice = AUDIO_DEVICE_NONE;
976 }
977
978 uint32_t delayMs = 0;
979
980 // Routing?
981 mOutputRoutes.incRouteActivity(session);
982
983 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
984
985 if (status != NO_ERROR) {
986 mOutputRoutes.decRouteActivity(session);
987 }
988 // Automatically enable the remote submix input when output is started on a re routing mix
989 // of type MIX_TYPE_RECORDERS
990 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
991 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
992 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
993 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
994 outputDesc->mPolicyMix->mRegistrationId,
995 "remote-submix");
996 }
997
998 if (delayMs != 0) {
999 usleep(delayMs * 1000);
1000 }
1001
1002 return status;
1003}
1004
1005status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1006 audio_stream_type_t stream,
1007 audio_devices_t device,
1008 uint32_t *delayMs)
1009{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001010 // cannot start playback of STREAM_TTS if any other output is being used
1011 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001012
1013 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001014 if (stream == AUDIO_STREAM_TTS) {
1015 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001016 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001017 return INVALID_OPERATION;
1018 } else {
1019 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1020 }
1021 } else {
1022 // some playback other than beacon starts
1023 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1024 }
1025
Eric Laurente552edb2014-03-10 17:42:56 -07001026 // increment usage count for this stream on the requested output:
1027 // NOTE that the usage count is the same for duplicated output and hardware output which is
1028 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1029 outputDesc->changeRefCount(stream, 1);
1030
Eric Laurent493404d2015-04-21 15:07:36 -07001031 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001032 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001033 if (device == AUDIO_DEVICE_NONE) {
1034 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001035 }
Eric Laurente552edb2014-03-10 17:42:56 -07001036 routing_strategy strategy = getStrategy(stream);
1037 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001038 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1039 (beaconMuteLatency > 0);
1040 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001041 bool force = false;
1042 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001043 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001044 if (desc != outputDesc) {
1045 // force a device change if any other output is managed by the same hw
1046 // module and has a current device selection that differs from selected device.
1047 // In this case, the audio HAL must receive the new device selection so that it can
1048 // change the device currently selected by the other active output.
1049 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001050 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001051 force = true;
1052 }
1053 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001054 // a notification so that audio focus effect can propagate, or that a mute/unmute
1055 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001056 uint32_t latency = desc->latency();
1057 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1058 waitMs = latency;
1059 }
1060 }
1061 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001062 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001063
1064 // handle special case for sonification while in call
1065 if (isInCall()) {
1066 handleIncallSonification(stream, true, false);
1067 }
1068
1069 // apply volume rules for current stream and device if necessary
1070 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001071 mStreams.valueFor(stream).getVolumeIndex(device),
1072 outputDesc,
1073 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001074
1075 // update the outputs if starting an output with a stream that can affect notification
1076 // routing
1077 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001078
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001079 // force reevaluating accessibility routing when ringtone or alarm starts
1080 if (strategy == STRATEGY_SONIFICATION) {
1081 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1082 }
Eric Laurente552edb2014-03-10 17:42:56 -07001083 }
1084 return NO_ERROR;
1085}
1086
1087
Eric Laurente0720872014-03-11 09:30:41 -07001088status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001089 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001090 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001091{
1092 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1093 ssize_t index = mOutputs.indexOfKey(output);
1094 if (index < 0) {
1095 ALOGW("stopOutput() unknown output %d", output);
1096 return BAD_VALUE;
1097 }
1098
Eric Laurentc75307b2015-03-17 15:29:32 -07001099 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001100
Eric Laurentc75307b2015-03-17 15:29:32 -07001101 if (outputDesc->mRefCount[stream] == 1) {
1102 // Automatically disable the remote submix input when output is stopped on a
1103 // re routing mix of type MIX_TYPE_RECORDERS
1104 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1105 outputDesc->mPolicyMix != NULL &&
1106 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1107 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1108 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1109 outputDesc->mPolicyMix->mRegistrationId,
1110 "remote-submix");
1111 }
1112 }
1113
1114 // Routing?
1115 if (outputDesc->mRefCount[stream] > 0) {
1116 mOutputRoutes.decRouteActivity(session);
1117 }
1118
1119 return stopSource(outputDesc, stream);
1120}
1121
1122status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
1123 audio_stream_type_t stream)
1124{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001125 // always handle stream stop, check which stream type is stopping
1126 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1127
Eric Laurente552edb2014-03-10 17:42:56 -07001128 // handle special case for sonification while in call
1129 if (isInCall()) {
1130 handleIncallSonification(stream, false, false);
1131 }
1132
1133 if (outputDesc->mRefCount[stream] > 0) {
1134 // decrement usage count of this stream on the output
1135 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001136
Eric Laurente552edb2014-03-10 17:42:56 -07001137 // store time at which the stream was stopped - see isStreamActive()
1138 if (outputDesc->mRefCount[stream] == 0) {
1139 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001140 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001141 // delay the device switch by twice the latency because stopOutput() is executed when
1142 // the track stop() command is received and at that time the audio track buffer can
1143 // still contain data that needs to be drained. The latency only covers the audio HAL
1144 // and kernel buffers. Also the latency does not always include additional delay in the
1145 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001146 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001147
1148 // force restoring the device selection on other active outputs if it differs from the
1149 // one being selected for this output
1150 for (size_t i = 0; i < mOutputs.size(); i++) {
1151 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001152 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001153 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001154 desc->isActive() &&
1155 outputDesc->sharesHwModuleWith(desc) &&
1156 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001157 setOutputDevice(desc,
1158 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001159 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001161 }
1162 }
1163 // update the outputs if stopping one with a stream that can affect notification routing
1164 handleNotificationRoutingForStream(stream);
1165 }
1166 return NO_ERROR;
1167 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001168 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001169 return INVALID_OPERATION;
1170 }
1171}
1172
Eric Laurente83b55d2014-11-14 10:06:21 -08001173void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001174 audio_stream_type_t stream __unused,
1175 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001176{
1177 ALOGV("releaseOutput() %d", output);
1178 ssize_t index = mOutputs.indexOfKey(output);
1179 if (index < 0) {
1180 ALOGW("releaseOutput() releasing unknown output %d", output);
1181 return;
1182 }
1183
1184#ifdef AUDIO_POLICY_TEST
1185 int testIndex = testOutputIndex(output);
1186 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001187 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001188 if (outputDesc->isActive()) {
1189 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001190 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001191 mTestOutputs[testIndex] = 0;
1192 }
1193 return;
1194 }
1195#endif //AUDIO_POLICY_TEST
1196
Paul McLeanaa981192015-03-21 09:55:15 -07001197 // Routing
1198 mOutputRoutes.removeRoute(session);
1199
Eric Laurentc75307b2015-03-17 15:29:32 -07001200 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001201 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001202 if (desc->mDirectOpenCount <= 0) {
1203 ALOGW("releaseOutput() invalid open count %d for output %d",
1204 desc->mDirectOpenCount, output);
1205 return;
1206 }
1207 if (--desc->mDirectOpenCount == 0) {
1208 closeOutput(output);
1209 // If effects where present on the output, audioflinger moved them to the primary
1210 // output by default: move them back to the appropriate output.
1211 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurentc75307b2015-03-17 15:29:32 -07001212 if (dstOutput != mPrimaryOutput->mIoHandle) {
1213 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1214 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001215 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001216 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001217 }
1218 }
1219}
1220
1221
Eric Laurentcaf7f482014-11-25 17:50:47 -08001222status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1223 audio_io_handle_t *input,
1224 audio_session_t session,
1225 uint32_t samplingRate,
1226 audio_format_t format,
1227 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001228 audio_input_flags_t flags,
1229 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001230{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001231 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1232 "session %d, flags %#x",
1233 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001234
Eric Laurentcaf7f482014-11-25 17:50:47 -08001235 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001236 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001237 audio_devices_t device;
1238 // handle legacy remote submix case where the address was not always specified
1239 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001240 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001241 audio_source_t inputSource = attr->source;
1242 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001243 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001244
Eric Laurentc447ded2015-01-06 08:47:05 -08001245 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1246 inputSource = AUDIO_SOURCE_MIC;
1247 }
1248 halInputSource = inputSource;
1249
1250 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001251 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001252 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001253 if (ret != NO_ERROR) {
1254 return ret;
1255 }
1256 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001257 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1258 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001260 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001261 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001262 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001263 return BAD_VALUE;
1264 }
Eric Laurentc722f302014-12-10 11:21:49 -08001265 if (policyMix != NULL) {
1266 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001267 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1268 // there is an external policy, but this input is attached to a mix of recorders,
1269 // meaning it receives audio injected into the framework, so the recorder doesn't
1270 // know about it and is therefore considered "legacy"
1271 *inputType = API_INPUT_LEGACY;
1272 } else {
1273 // recording a mix of players defined by an external policy, we're rerouting for
1274 // an external policy
1275 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1276 }
Eric Laurentc722f302014-12-10 11:21:49 -08001277 } else if (audio_is_remote_submix_device(device)) {
1278 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001279 *inputType = API_INPUT_MIX_CAPTURE;
1280 } else {
1281 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001282 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001283 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001284 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001285 case AUDIO_SOURCE_VOICE_UPLINK:
1286 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1287 break;
1288 case AUDIO_SOURCE_VOICE_DOWNLINK:
1289 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1290 break;
1291 case AUDIO_SOURCE_VOICE_CALL:
1292 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1293 break;
1294 default:
1295 break;
1296 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001297 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001298 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1299 if (index >= 0) {
1300 *input = mSoundTriggerSessions.valueFor(session);
1301 isSoundTrigger = true;
1302 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1303 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1304 } else {
1305 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1306 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001307 }
1308 }
1309
Andy Hungf129b032015-04-07 13:45:50 -07001310 // find a compatible input profile (not necessarily identical in parameters)
1311 sp<IOProfile> profile;
1312 // samplingRate and flags may be updated by getInputProfile
1313 uint32_t profileSamplingRate = samplingRate;
1314 audio_format_t profileFormat = format;
1315 audio_channel_mask_t profileChannelMask = channelMask;
1316 audio_input_flags_t profileFlags = flags;
1317 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001318 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001319 profileSamplingRate, profileFormat, profileChannelMask,
1320 profileFlags);
1321 if (profile != 0) {
1322 break; // success
1323 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1324 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1325 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001326 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1327 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001328 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001329 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001330 }
Eric Laurente552edb2014-03-10 17:42:56 -07001331 }
1332
Eric Laurent322b4d22015-04-03 15:57:54 -07001333 if (profile->getModuleHandle() == 0) {
1334 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001335 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001336 }
1337
1338 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001339 config.sample_rate = profileSamplingRate;
1340 config.channel_mask = profileChannelMask;
1341 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001342
Eric Laurent322b4d22015-04-03 15:57:54 -07001343 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001344 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001345 &config,
1346 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001347 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001348 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001349 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001350
1351 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001352 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001353 (profileSamplingRate != config.sample_rate) ||
1354 (profileFormat != config.format) ||
1355 (profileChannelMask != config.channel_mask)) {
1356 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1357 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001358 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001359 if (*input != AUDIO_IO_HANDLE_NONE) {
1360 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001361 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001362 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001363 }
1364
Eric Laurent1f2f2232014-06-02 12:01:23 -07001365 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001366 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001367 inputDesc->mRefCount = 0;
1368 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001369 inputDesc->mSamplingRate = profileSamplingRate;
1370 inputDesc->mFormat = profileFormat;
1371 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001372 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001373 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001374 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001375 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001376
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001377 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001378
Eric Laurentcaf7f482014-11-25 17:50:47 -08001379 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001380 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001381 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001382}
1383
Eric Laurent4dc68062014-07-28 17:26:49 -07001384status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1385 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001386{
1387 ALOGV("startInput() input %d", input);
1388 ssize_t index = mInputs.indexOfKey(input);
1389 if (index < 0) {
1390 ALOGW("startInput() unknown input %d", input);
1391 return BAD_VALUE;
1392 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001393 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001394
Eric Laurentc722f302014-12-10 11:21:49 -08001395 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001396 if (index < 0) {
1397 ALOGW("startInput() unknown session %d on input %d", session, input);
1398 return BAD_VALUE;
1399 }
1400
Glenn Kasten74a8e252014-07-24 14:09:55 -07001401 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001402 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001403
1404 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001405 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001406 if (activeInput != 0 && activeInput != input) {
1407
1408 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1409 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001410 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001411 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001412 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001413 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1414 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001415 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001416 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001417 return INVALID_OPERATION;
1418 }
1419 }
1420 }
1421
Glenn Kasten74a8e252014-07-24 14:09:55 -07001422 if (inputDesc->mRefCount == 0) {
François Gaffie53615e22015-03-19 09:24:12 +01001423 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001424 SoundTrigger::setCaptureState(true);
1425 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001426 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001427
Eric Laurentc722f302014-12-10 11:21:49 -08001428 // automatically enable the remote submix output when input is started if not
1429 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001430 // For remote submix (a virtual device), we open only one input per capture request.
1431 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001432 String8 address = String8("");
1433 if (inputDesc->mPolicyMix == NULL) {
1434 address = String8("0");
1435 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1436 address = inputDesc->mPolicyMix->mRegistrationId;
1437 }
1438 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001439 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001440 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001441 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001442 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001443 }
Eric Laurente552edb2014-03-10 17:42:56 -07001444 }
1445
Eric Laurente552edb2014-03-10 17:42:56 -07001446 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1447
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001448 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001449 return NO_ERROR;
1450}
1451
Eric Laurent4dc68062014-07-28 17:26:49 -07001452status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1453 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001454{
1455 ALOGV("stopInput() input %d", input);
1456 ssize_t index = mInputs.indexOfKey(input);
1457 if (index < 0) {
1458 ALOGW("stopInput() unknown input %d", input);
1459 return BAD_VALUE;
1460 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001461 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001462
Eric Laurentc722f302014-12-10 11:21:49 -08001463 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001464 if (index < 0) {
1465 ALOGW("stopInput() unknown session %d on input %d", session, input);
1466 return BAD_VALUE;
1467 }
1468
Eric Laurente552edb2014-03-10 17:42:56 -07001469 if (inputDesc->mRefCount == 0) {
1470 ALOGW("stopInput() input %d already stopped", input);
1471 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001472 }
1473
1474 inputDesc->mRefCount--;
1475 if (inputDesc->mRefCount == 0) {
1476
Eric Laurentc722f302014-12-10 11:21:49 -08001477 // automatically disable the remote submix output when input is stopped if not
1478 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001479 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001480 String8 address = String8("");
1481 if (inputDesc->mPolicyMix == NULL) {
1482 address = String8("0");
1483 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1484 address = inputDesc->mPolicyMix->mRegistrationId;
1485 }
1486 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001487 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001488 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001489 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001490 }
Eric Laurente552edb2014-03-10 17:42:56 -07001491 }
1492
Eric Laurent1c333e22014-05-20 10:48:17 -07001493 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001494
François Gaffie53615e22015-03-19 09:24:12 +01001495 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001496 SoundTrigger::setCaptureState(false);
1497 }
Eric Laurente552edb2014-03-10 17:42:56 -07001498 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001499 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001500}
1501
Eric Laurent4dc68062014-07-28 17:26:49 -07001502void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1503 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001504{
1505 ALOGV("releaseInput() %d", input);
1506 ssize_t index = mInputs.indexOfKey(input);
1507 if (index < 0) {
1508 ALOGW("releaseInput() releasing unknown input %d", input);
1509 return;
1510 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001511 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1512 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001513
Eric Laurentc722f302014-12-10 11:21:49 -08001514 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001515 if (index < 0) {
1516 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1517 return;
1518 }
Eric Laurentc722f302014-12-10 11:21:49 -08001519 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001520 if (inputDesc->mOpenRefCount == 0) {
1521 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1522 return;
1523 }
1524 inputDesc->mOpenRefCount--;
1525 if (inputDesc->mOpenRefCount > 0) {
1526 ALOGV("releaseInput() exit > 0");
1527 return;
1528 }
1529
Eric Laurent05b90f82014-08-27 15:32:29 -07001530 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001531 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001532 ALOGV("releaseInput() exit");
1533}
1534
Eric Laurentd4692962014-05-05 18:13:44 -07001535void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001536 bool patchRemoved = false;
1537
Eric Laurentd4692962014-05-05 18:13:44 -07001538 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001539 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1540 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1541 if (patch_index >= 0) {
1542 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1543 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1544 mAudioPatches.removeItemsAt(patch_index);
1545 patchRemoved = true;
1546 }
Eric Laurentd4692962014-05-05 18:13:44 -07001547 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1548 }
1549 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001550 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001551
1552 if (patchRemoved) {
1553 mpClientInterface->onAudioPatchListUpdate();
1554 }
Eric Laurentd4692962014-05-05 18:13:44 -07001555}
1556
Eric Laurente0720872014-03-11 09:30:41 -07001557void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001558 int indexMin,
1559 int indexMax)
1560{
1561 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001562 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001563 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1564 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001565 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001566 }
Eric Laurente552edb2014-03-10 17:42:56 -07001567}
1568
Eric Laurente0720872014-03-11 09:30:41 -07001569status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001570 int index,
1571 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001572{
1573
Eric Laurentc75307b2015-03-17 15:29:32 -07001574 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1575 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001576 return BAD_VALUE;
1577 }
1578 if (!audio_is_output_device(device)) {
1579 return BAD_VALUE;
1580 }
1581
1582 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001583 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001584
1585 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1586 stream, device, index);
1587
1588 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1589 // clear all device specific values
1590 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001591 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001592 }
François Gaffiedfd74092015-03-19 12:10:59 +01001593 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001594
Eric Laurent31551f82014-10-10 18:21:56 -07001595 // update volume on all outputs whose current device is also selected by the same
1596 // strategy as the device specified by the caller
1597 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001598
1599
1600 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1601 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1602 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001603 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001604 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1605 }
1606 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1607 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001608 return NO_ERROR;
1609 }
Eric Laurente552edb2014-03-10 17:42:56 -07001610 status_t status = NO_ERROR;
1611 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001612 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1613 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001614 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001615 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001616 if (volStatus != NO_ERROR) {
1617 status = volStatus;
1618 }
1619 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001620 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1621 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001622 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001623 }
Eric Laurente552edb2014-03-10 17:42:56 -07001624 }
1625 return status;
1626}
1627
Eric Laurente0720872014-03-11 09:30:41 -07001628status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001629 int *index,
1630 audio_devices_t device)
1631{
1632 if (index == NULL) {
1633 return BAD_VALUE;
1634 }
1635 if (!audio_is_output_device(device)) {
1636 return BAD_VALUE;
1637 }
1638 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1639 // the strategy the stream belongs to.
1640 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1641 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1642 }
François Gaffiedfd74092015-03-19 12:10:59 +01001643 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001644
Eric Laurentc75307b2015-03-17 15:29:32 -07001645 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001646 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1647 return NO_ERROR;
1648}
1649
Eric Laurente0720872014-03-11 09:30:41 -07001650audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001651 const SortedVector<audio_io_handle_t>& outputs)
1652{
1653 // select one output among several suitable for global effects.
1654 // The priority is as follows:
1655 // 1: An offloaded output. If the effect ends up not being offloadable,
1656 // AudioFlinger will invalidate the track and the offloaded output
1657 // will be closed causing the effect to be moved to a PCM output.
1658 // 2: A deep buffer output
1659 // 3: the first output in the list
1660
1661 if (outputs.size() == 0) {
1662 return 0;
1663 }
1664
1665 audio_io_handle_t outputOffloaded = 0;
1666 audio_io_handle_t outputDeepBuffer = 0;
1667
1668 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001669 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001670 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001671 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1672 outputOffloaded = outputs[i];
1673 }
1674 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1675 outputDeepBuffer = outputs[i];
1676 }
1677 }
1678
1679 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1680 outputOffloaded, outputDeepBuffer);
1681 if (outputOffloaded != 0) {
1682 return outputOffloaded;
1683 }
1684 if (outputDeepBuffer != 0) {
1685 return outputDeepBuffer;
1686 }
1687
1688 return outputs[0];
1689}
1690
Eric Laurente0720872014-03-11 09:30:41 -07001691audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001692{
1693 // apply simple rule where global effects are attached to the same output as MUSIC streams
1694
Eric Laurent3b73df72014-03-11 09:06:29 -07001695 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001696 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1697 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1698
1699 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1700 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1701 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1702
1703 return output;
1704}
1705
Eric Laurente0720872014-03-11 09:30:41 -07001706status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001707 audio_io_handle_t io,
1708 uint32_t strategy,
1709 int session,
1710 int id)
1711{
1712 ssize_t index = mOutputs.indexOfKey(io);
1713 if (index < 0) {
1714 index = mInputs.indexOfKey(io);
1715 if (index < 0) {
1716 ALOGW("registerEffect() unknown io %d", io);
1717 return INVALID_OPERATION;
1718 }
1719 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001720 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001721}
1722
Eric Laurentc75307b2015-03-17 15:29:32 -07001723bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1724{
1725 return mOutputs.isStreamActive(stream, inPastMs);
1726}
1727
1728bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1729{
1730 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1731}
1732
Eric Laurente0720872014-03-11 09:30:41 -07001733bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001734{
1735 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001736 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001737 if (inputDescriptor->mRefCount == 0) {
1738 continue;
1739 }
1740 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001741 return true;
1742 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001743 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1744 // corresponds to an active capture triggered by a hardware hotword recognition
1745 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1746 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1747 // FIXME: we should not assume that the first session is the active one and keep
1748 // activity count per session. Same in startInput().
1749 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1750 if (index >= 0) {
1751 return true;
1752 }
1753 }
Eric Laurente552edb2014-03-10 17:42:56 -07001754 }
1755 return false;
1756}
1757
Eric Laurent275e8e92014-11-30 15:14:47 -08001758// Register a list of custom mixes with their attributes and format.
1759// When a mix is registered, corresponding input and output profiles are
1760// added to the remote submix hw module. The profile contains only the
1761// parameters (sampling rate, format...) specified by the mix.
1762// The corresponding input remote submix device is also connected.
1763//
1764// When a remote submix device is connected, the address is checked to select the
1765// appropriate profile and the corresponding input or output stream is opened.
1766//
1767// When capture starts, getInputForAttr() will:
1768// - 1 look for a mix matching the address passed in attribtutes tags if any
1769// - 2 if none found, getDeviceForInputSource() will:
1770// - 2.1 look for a mix matching the attributes source
1771// - 2.2 if none found, default to device selection by policy rules
1772// At this time, the corresponding output remote submix device is also connected
1773// and active playback use cases can be transferred to this mix if needed when reconnecting
1774// after AudioTracks are invalidated
1775//
1776// When playback starts, getOutputForAttr() will:
1777// - 1 look for a mix matching the address passed in attribtutes tags if any
1778// - 2 if none found, look for a mix matching the attributes usage
1779// - 3 if none found, default to device and output selection by policy rules.
1780
1781status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1782{
1783 sp<HwModule> module;
1784 for (size_t i = 0; i < mHwModules.size(); i++) {
1785 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1786 mHwModules[i]->mHandle != 0) {
1787 module = mHwModules[i];
1788 break;
1789 }
1790 }
1791
1792 if (module == 0) {
1793 return INVALID_OPERATION;
1794 }
1795
1796 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1797
1798 for (size_t i = 0; i < mixes.size(); i++) {
1799 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001800
1801 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001802 continue;
1803 }
1804 audio_config_t outputConfig = mixes[i].mFormat;
1805 audio_config_t inputConfig = mixes[i].mFormat;
1806 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1807 // stereo and let audio flinger do the channel conversion if needed.
1808 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1809 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1810 module->addOutputProfile(address, &outputConfig,
1811 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1812 module->addInputProfile(address, &inputConfig,
1813 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001814
Eric Laurentc722f302014-12-10 11:21:49 -08001815 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001816 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001817 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001818 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001819 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001820 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001821 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001822 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001823 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001824 }
1825 return NO_ERROR;
1826}
1827
1828status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1829{
1830 sp<HwModule> module;
1831 for (size_t i = 0; i < mHwModules.size(); i++) {
1832 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1833 mHwModules[i]->mHandle != 0) {
1834 module = mHwModules[i];
1835 break;
1836 }
1837 }
1838
1839 if (module == 0) {
1840 return INVALID_OPERATION;
1841 }
1842
1843 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1844
1845 for (size_t i = 0; i < mixes.size(); i++) {
1846 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001847
1848 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001849 continue;
1850 }
1851
Eric Laurentc722f302014-12-10 11:21:49 -08001852 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1853 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1854 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001855 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001856 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001857 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001858 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001859
1860 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1861 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1862 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001863 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001864 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001865 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001866 }
1867 module->removeOutputProfile(address);
1868 module->removeInputProfile(address);
1869 }
1870 return NO_ERROR;
1871}
1872
Eric Laurente552edb2014-03-10 17:42:56 -07001873
Eric Laurente0720872014-03-11 09:30:41 -07001874status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001875{
1876 const size_t SIZE = 256;
1877 char buffer[SIZE];
1878 String8 result;
1879
1880 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1881 result.append(buffer);
1882
Eric Laurentc75307b2015-03-17 15:29:32 -07001883 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001884 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001885 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001886 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001887 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001888 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001889 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001890 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001891 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001892 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001893 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001894 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001895 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001896 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001897 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001898 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001899 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001900 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001901 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001902
François Gaffie53615e22015-03-19 09:24:12 +01001903 mAvailableOutputDevices.dump(fd, String8("output"));
1904 mAvailableInputDevices.dump(fd, String8("input"));
1905 mHwModules.dump(fd);
1906 mOutputs.dump(fd);
1907 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001908 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001909 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001910 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001911
1912 return NO_ERROR;
1913}
1914
1915// This function checks for the parameters which can be offloaded.
1916// This can be enhanced depending on the capability of the DSP and policy
1917// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001918bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001919{
1920 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001921 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001922 offloadInfo.sample_rate, offloadInfo.channel_mask,
1923 offloadInfo.format,
1924 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1925 offloadInfo.has_video);
1926
1927 // Check if offload has been disabled
1928 char propValue[PROPERTY_VALUE_MAX];
1929 if (property_get("audio.offload.disable", propValue, "0")) {
1930 if (atoi(propValue) != 0) {
1931 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1932 return false;
1933 }
1934 }
1935
1936 // Check if stream type is music, then only allow offload as of now.
1937 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1938 {
1939 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1940 return false;
1941 }
1942
1943 //TODO: enable audio offloading with video when ready
1944 if (offloadInfo.has_video)
1945 {
1946 ALOGV("isOffloadSupported: has_video == true, returning false");
1947 return false;
1948 }
1949
1950 //If duration is less than minimum value defined in property, return false
1951 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1952 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1953 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1954 return false;
1955 }
1956 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1957 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1958 return false;
1959 }
1960
1961 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1962 // creating an offloaded track and tearing it down immediately after start when audioflinger
1963 // detects there is an active non offloadable effect.
1964 // FIXME: We should check the audio session here but we do not have it in this context.
1965 // This may prevent offloading in rare situations where effects are left active by apps
1966 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01001967 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001968 return false;
1969 }
1970
1971 // See if there is a profile to support this.
1972 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001973 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001974 offloadInfo.sample_rate,
1975 offloadInfo.format,
1976 offloadInfo.channel_mask,
1977 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001978 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1979 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001980}
1981
Eric Laurent6a94d692014-05-20 11:18:06 -07001982status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1983 audio_port_type_t type,
1984 unsigned int *num_ports,
1985 struct audio_port *ports,
1986 unsigned int *generation)
1987{
1988 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1989 generation == NULL) {
1990 return BAD_VALUE;
1991 }
1992 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1993 if (ports == NULL) {
1994 *num_ports = 0;
1995 }
1996
1997 size_t portsWritten = 0;
1998 size_t portsMax = *num_ports;
1999 *num_ports = 0;
2000 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2001 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2002 for (size_t i = 0;
2003 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2004 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2005 }
2006 *num_ports += mAvailableOutputDevices.size();
2007 }
2008 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2009 for (size_t i = 0;
2010 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2011 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2012 }
2013 *num_ports += mAvailableInputDevices.size();
2014 }
2015 }
2016 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2017 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2018 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2019 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2020 }
2021 *num_ports += mInputs.size();
2022 }
2023 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002024 size_t numOutputs = 0;
2025 for (size_t i = 0; i < mOutputs.size(); i++) {
2026 if (!mOutputs[i]->isDuplicated()) {
2027 numOutputs++;
2028 if (portsWritten < portsMax) {
2029 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2030 }
2031 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002032 }
Eric Laurent84c70242014-06-23 08:46:27 -07002033 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002034 }
2035 }
2036 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002037 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002038 return NO_ERROR;
2039}
2040
2041status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2042{
2043 return NO_ERROR;
2044}
2045
Eric Laurent6a94d692014-05-20 11:18:06 -07002046status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2047 audio_patch_handle_t *handle,
2048 uid_t uid)
2049{
2050 ALOGV("createAudioPatch()");
2051
2052 if (handle == NULL || patch == NULL) {
2053 return BAD_VALUE;
2054 }
2055 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2056
Eric Laurent874c42872014-08-08 15:13:39 -07002057 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2058 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2059 return BAD_VALUE;
2060 }
2061 // only one source per audio patch supported for now
2062 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002063 return INVALID_OPERATION;
2064 }
Eric Laurent874c42872014-08-08 15:13:39 -07002065
2066 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002067 return INVALID_OPERATION;
2068 }
Eric Laurent874c42872014-08-08 15:13:39 -07002069 for (size_t i = 0; i < patch->num_sinks; i++) {
2070 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2071 return INVALID_OPERATION;
2072 }
2073 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002074
2075 sp<AudioPatch> patchDesc;
2076 ssize_t index = mAudioPatches.indexOfKey(*handle);
2077
Eric Laurent6a94d692014-05-20 11:18:06 -07002078 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2079 patch->sources[0].role,
2080 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002081#if LOG_NDEBUG == 0
2082 for (size_t i = 0; i < patch->num_sinks; i++) {
2083 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2084 patch->sinks[i].role,
2085 patch->sinks[i].type);
2086 }
2087#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002088
2089 if (index >= 0) {
2090 patchDesc = mAudioPatches.valueAt(index);
2091 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2092 mUidCached, patchDesc->mUid, uid);
2093 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2094 return INVALID_OPERATION;
2095 }
2096 } else {
2097 *handle = 0;
2098 }
2099
2100 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002101 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002102 if (outputDesc == NULL) {
2103 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2104 return BAD_VALUE;
2105 }
Eric Laurent84c70242014-06-23 08:46:27 -07002106 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2107 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002108 if (patchDesc != 0) {
2109 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2110 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2111 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2112 return BAD_VALUE;
2113 }
2114 }
Eric Laurent874c42872014-08-08 15:13:39 -07002115 DeviceVector devices;
2116 for (size_t i = 0; i < patch->num_sinks; i++) {
2117 // Only support mix to devices connection
2118 // TODO add support for mix to mix connection
2119 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2120 ALOGV("createAudioPatch() source mix but sink is not a device");
2121 return INVALID_OPERATION;
2122 }
2123 sp<DeviceDescriptor> devDesc =
2124 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2125 if (devDesc == 0) {
2126 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2127 return BAD_VALUE;
2128 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002129
François Gaffie53615e22015-03-19 09:24:12 +01002130 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002131 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002132 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002133 NULL, // updatedSamplingRate
2134 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002135 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002136 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002137 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002138 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002139 ALOGV("createAudioPatch() profile not supported for device %08x",
2140 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002141 return INVALID_OPERATION;
2142 }
2143 devices.add(devDesc);
2144 }
2145 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002146 return INVALID_OPERATION;
2147 }
Eric Laurent874c42872014-08-08 15:13:39 -07002148
Eric Laurent6a94d692014-05-20 11:18:06 -07002149 // TODO: reconfigure output format and channels here
2150 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002151 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002152 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002153 index = mAudioPatches.indexOfKey(*handle);
2154 if (index >= 0) {
2155 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2156 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2157 }
2158 patchDesc = mAudioPatches.valueAt(index);
2159 patchDesc->mUid = uid;
2160 ALOGV("createAudioPatch() success");
2161 } else {
2162 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2163 return INVALID_OPERATION;
2164 }
2165 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2166 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2167 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002168 // only one sink supported when connecting an input device to a mix
2169 if (patch->num_sinks > 1) {
2170 return INVALID_OPERATION;
2171 }
François Gaffie53615e22015-03-19 09:24:12 +01002172 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002173 if (inputDesc == NULL) {
2174 return BAD_VALUE;
2175 }
2176 if (patchDesc != 0) {
2177 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2178 return BAD_VALUE;
2179 }
2180 }
2181 sp<DeviceDescriptor> devDesc =
2182 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2183 if (devDesc == 0) {
2184 return BAD_VALUE;
2185 }
2186
François Gaffie53615e22015-03-19 09:24:12 +01002187 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002188 devDesc->mAddress,
2189 patch->sinks[0].sample_rate,
2190 NULL, /*updatedSampleRate*/
2191 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002192 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002193 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002194 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002195 // FIXME for the parameter type,
2196 // and the NONE
2197 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002198 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002199 return INVALID_OPERATION;
2200 }
2201 // TODO: reconfigure output format and channels here
2202 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002203 devDesc->type(), inputDesc->mIoHandle);
2204 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002205 index = mAudioPatches.indexOfKey(*handle);
2206 if (index >= 0) {
2207 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2208 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2209 }
2210 patchDesc = mAudioPatches.valueAt(index);
2211 patchDesc->mUid = uid;
2212 ALOGV("createAudioPatch() success");
2213 } else {
2214 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2215 return INVALID_OPERATION;
2216 }
2217 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2218 // device to device connection
2219 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002220 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002221 return BAD_VALUE;
2222 }
2223 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002224 sp<DeviceDescriptor> srcDeviceDesc =
2225 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002226 if (srcDeviceDesc == 0) {
2227 return BAD_VALUE;
2228 }
Eric Laurent874c42872014-08-08 15:13:39 -07002229
Eric Laurent6a94d692014-05-20 11:18:06 -07002230 //update source and sink with our own data as the data passed in the patch may
2231 // be incomplete.
2232 struct audio_patch newPatch = *patch;
2233 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002234
Eric Laurent874c42872014-08-08 15:13:39 -07002235 for (size_t i = 0; i < patch->num_sinks; i++) {
2236 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2237 ALOGV("createAudioPatch() source device but one sink is not a device");
2238 return INVALID_OPERATION;
2239 }
2240
2241 sp<DeviceDescriptor> sinkDeviceDesc =
2242 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2243 if (sinkDeviceDesc == 0) {
2244 return BAD_VALUE;
2245 }
2246 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2247
Eric Laurent3bcf8592015-04-03 12:13:24 -07002248 // create a software bridge in PatchPanel if:
2249 // - source and sink devices are on differnt HW modules OR
2250 // - audio HAL version is < 3.0
2251 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2252 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2253 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002254 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002255 return INVALID_OPERATION;
2256 }
Eric Laurent874c42872014-08-08 15:13:39 -07002257 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002258 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002259 // if the sink device is reachable via an opened output stream, request to go via
2260 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002261 audio_io_handle_t output = selectOutput(outputs,
2262 AUDIO_OUTPUT_FLAG_NONE,
2263 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002264 if (output != AUDIO_IO_HANDLE_NONE) {
2265 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2266 if (outputDesc->isDuplicated()) {
2267 return INVALID_OPERATION;
2268 }
2269 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002270 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002271 newPatch.num_sources = 2;
2272 }
Eric Laurent83b88082014-06-20 18:31:16 -07002273 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002274 }
2275 // TODO: check from routing capabilities in config file and other conflicting patches
2276
2277 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2278 if (index >= 0) {
2279 afPatchHandle = patchDesc->mAfPatchHandle;
2280 }
2281
2282 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2283 &afPatchHandle,
2284 0);
2285 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2286 status, afPatchHandle);
2287 if (status == NO_ERROR) {
2288 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002289 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002290 addAudioPatch(patchDesc->mHandle, patchDesc);
2291 } else {
2292 patchDesc->mPatch = newPatch;
2293 }
2294 patchDesc->mAfPatchHandle = afPatchHandle;
2295 *handle = patchDesc->mHandle;
2296 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002297 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002298 } else {
2299 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2300 status);
2301 return INVALID_OPERATION;
2302 }
2303 } else {
2304 return BAD_VALUE;
2305 }
2306 } else {
2307 return BAD_VALUE;
2308 }
2309 return NO_ERROR;
2310}
2311
2312status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2313 uid_t uid)
2314{
2315 ALOGV("releaseAudioPatch() patch %d", handle);
2316
2317 ssize_t index = mAudioPatches.indexOfKey(handle);
2318
2319 if (index < 0) {
2320 return BAD_VALUE;
2321 }
2322 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2323 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2324 mUidCached, patchDesc->mUid, uid);
2325 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2326 return INVALID_OPERATION;
2327 }
2328
2329 struct audio_patch *patch = &patchDesc->mPatch;
2330 patchDesc->mUid = mUidCached;
2331 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002332 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002333 if (outputDesc == NULL) {
2334 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2335 return BAD_VALUE;
2336 }
2337
Eric Laurentc75307b2015-03-17 15:29:32 -07002338 setOutputDevice(outputDesc,
2339 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002340 true,
2341 0,
2342 NULL);
2343 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2344 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002345 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002346 if (inputDesc == NULL) {
2347 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2348 return BAD_VALUE;
2349 }
2350 setInputDevice(inputDesc->mIoHandle,
2351 getNewInputDevice(inputDesc->mIoHandle),
2352 true,
2353 NULL);
2354 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2355 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2356 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2357 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2358 status, patchDesc->mAfPatchHandle);
2359 removeAudioPatch(patchDesc->mHandle);
2360 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002361 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002362 } else {
2363 return BAD_VALUE;
2364 }
2365 } else {
2366 return BAD_VALUE;
2367 }
2368 return NO_ERROR;
2369}
2370
2371status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2372 struct audio_patch *patches,
2373 unsigned int *generation)
2374{
François Gaffie53615e22015-03-19 09:24:12 +01002375 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002376 return BAD_VALUE;
2377 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002378 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002379 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002380}
2381
Eric Laurente1715a42014-05-20 11:30:42 -07002382status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002383{
Eric Laurente1715a42014-05-20 11:30:42 -07002384 ALOGV("setAudioPortConfig()");
2385
2386 if (config == NULL) {
2387 return BAD_VALUE;
2388 }
2389 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2390 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002391 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2392 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002393 }
2394
Eric Laurenta121f902014-06-03 13:32:54 -07002395 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002396 if (config->type == AUDIO_PORT_TYPE_MIX) {
2397 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002398 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002399 if (outputDesc == NULL) {
2400 return BAD_VALUE;
2401 }
Eric Laurent84c70242014-06-23 08:46:27 -07002402 ALOG_ASSERT(!outputDesc->isDuplicated(),
2403 "setAudioPortConfig() called on duplicated output %d",
2404 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002405 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002406 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002407 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002408 if (inputDesc == NULL) {
2409 return BAD_VALUE;
2410 }
Eric Laurenta121f902014-06-03 13:32:54 -07002411 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002412 } else {
2413 return BAD_VALUE;
2414 }
2415 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2416 sp<DeviceDescriptor> deviceDesc;
2417 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2418 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2419 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2420 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2421 } else {
2422 return BAD_VALUE;
2423 }
2424 if (deviceDesc == NULL) {
2425 return BAD_VALUE;
2426 }
Eric Laurenta121f902014-06-03 13:32:54 -07002427 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002428 } else {
2429 return BAD_VALUE;
2430 }
2431
Eric Laurenta121f902014-06-03 13:32:54 -07002432 struct audio_port_config backupConfig;
2433 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2434 if (status == NO_ERROR) {
2435 struct audio_port_config newConfig;
2436 audioPortConfig->toAudioPortConfig(&newConfig, config);
2437 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002438 }
Eric Laurenta121f902014-06-03 13:32:54 -07002439 if (status != NO_ERROR) {
2440 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002441 }
Eric Laurente1715a42014-05-20 11:30:42 -07002442
2443 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002444}
2445
2446void AudioPolicyManager::clearAudioPatches(uid_t uid)
2447{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002448 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002449 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2450 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002451 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002452 }
2453 }
2454}
2455
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002456status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2457 audio_io_handle_t *ioHandle,
2458 audio_devices_t *device)
2459{
2460 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2461 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002462 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002463
François Gaffiedf372692015-03-19 10:43:27 +01002464 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002465}
2466
Eric Laurent493404d2015-04-21 15:07:36 -07002467status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2468 const audio_attributes_t *attributes __unused,
2469 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002470{
2471 return INVALID_OPERATION;
2472}
2473
Eric Laurent493404d2015-04-21 15:07:36 -07002474status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002475{
2476 return INVALID_OPERATION;
2477}
2478
Eric Laurente552edb2014-03-10 17:42:56 -07002479// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002480// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002481// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002482uint32_t AudioPolicyManager::nextAudioPortGeneration()
2483{
2484 return android_atomic_inc(&mAudioPortGeneration);
2485}
2486
Eric Laurente0720872014-03-11 09:30:41 -07002487AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002488 :
2489#ifdef AUDIO_POLICY_TEST
2490 Thread(false),
2491#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002492 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002493 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002494 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002495 mAudioPortGeneration(1),
2496 mBeaconMuteRefCount(0),
2497 mBeaconPlayingRefCount(0),
2498 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002499{
François Gaffie2110e042015-03-24 08:41:51 +01002500 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2501 if (!engineInstance) {
2502 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2503 return;
2504 }
2505 // Retrieve the Policy Manager Interface
2506 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2507 if (mEngine == NULL) {
2508 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2509 return;
2510 }
2511 mEngine->setObserver(this);
2512 status_t status = mEngine->initCheck();
2513 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2514
Eric Laurent6a94d692014-05-20 11:18:06 -07002515 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002516 mpClientInterface = clientInterface;
2517
Paul McLeane743a472015-01-28 11:07:31 -08002518 mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002519 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2520 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2521 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2522 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2523 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2524 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002525 ALOGE("could not load audio policy configuration file, setting defaults");
2526 defaultAudioPolicyConfig();
2527 }
2528 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002529 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002530
François Gaffie2110e042015-03-24 08:41:51 +01002531 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2532 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002533
2534 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002535 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2536 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002537 for (size_t i = 0; i < mHwModules.size(); i++) {
2538 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2539 if (mHwModules[i]->mHandle == 0) {
2540 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2541 continue;
2542 }
2543 // open all output streams needed to access attached devices
2544 // except for direct output streams that are only opened when they are actually
2545 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002546 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002547 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2548 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002549 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002550
Eric Laurent3a4311c2014-03-17 12:00:47 -07002551 if (outProfile->mSupportedDevices.isEmpty()) {
2552 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2553 continue;
2554 }
2555
Eric Laurentd78f1532014-09-16 16:38:20 -07002556 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2557 continue;
2558 }
Eric Laurent83b88082014-06-20 18:31:16 -07002559 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002560 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2561 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002562 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002563 // chose first device present in mSupportedDevices also part of
2564 // outputDeviceTypes
2565 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002566 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002567 if ((profileType & outputDeviceTypes) != 0) {
2568 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002569 }
Eric Laurente552edb2014-03-10 17:42:56 -07002570 }
2571 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002572 if ((profileType & outputDeviceTypes) == 0) {
2573 continue;
2574 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002575 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2576 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002577
2578 outputDesc->mDevice = profileType;
2579 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2580 config.sample_rate = outputDesc->mSamplingRate;
2581 config.channel_mask = outputDesc->mChannelMask;
2582 config.format = outputDesc->mFormat;
2583 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002584 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002585 &output,
2586 &config,
2587 &outputDesc->mDevice,
2588 String8(""),
2589 &outputDesc->mLatency,
2590 outputDesc->mFlags);
2591
2592 if (status != NO_ERROR) {
2593 ALOGW("Cannot open output stream for device %08x on hw module %s",
2594 outputDesc->mDevice,
2595 mHwModules[i]->mName);
2596 } else {
2597 outputDesc->mSamplingRate = config.sample_rate;
2598 outputDesc->mChannelMask = config.channel_mask;
2599 outputDesc->mFormat = config.format;
2600
2601 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002602 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002603 ssize_t index =
2604 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2605 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002606 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2607 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002608 }
2609 }
2610 if (mPrimaryOutput == 0 &&
2611 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002612 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002613 }
2614 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002615 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002616 outputDesc->mDevice,
2617 true);
2618 }
Eric Laurente552edb2014-03-10 17:42:56 -07002619 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002620 // open input streams needed to access attached devices to validate
2621 // mAvailableInputDevices list
2622 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2623 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002624 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002625
Eric Laurent3a4311c2014-03-17 12:00:47 -07002626 if (inProfile->mSupportedDevices.isEmpty()) {
2627 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2628 continue;
2629 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002630 // chose first device present in mSupportedDevices also part of
2631 // inputDeviceTypes
2632 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2633 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002634 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002635 if (profileType & inputDeviceTypes) {
2636 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002637 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002638 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002639 if ((profileType & inputDeviceTypes) == 0) {
2640 continue;
2641 }
2642 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2643
2644 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2645 inputDesc->mDevice = profileType;
2646
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002647 // find the address
2648 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2649 // the inputs vector must be of size 1, but we don't want to crash here
2650 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2651 : String8("");
2652 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2653 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2654
Eric Laurentd78f1532014-09-16 16:38:20 -07002655 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2656 config.sample_rate = inputDesc->mSamplingRate;
2657 config.channel_mask = inputDesc->mChannelMask;
2658 config.format = inputDesc->mFormat;
2659 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002660 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002661 &input,
2662 &config,
2663 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002664 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002665 AUDIO_SOURCE_MIC,
2666 AUDIO_INPUT_FLAG_NONE);
2667
2668 if (status == NO_ERROR) {
2669 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002670 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002671 ssize_t index =
2672 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2673 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002674 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2675 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002676 }
2677 }
2678 mpClientInterface->closeInput(input);
2679 } else {
2680 ALOGW("Cannot open input stream for device %08x on hw module %s",
2681 inputDesc->mDevice,
2682 mHwModules[i]->mName);
2683 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002684 }
2685 }
2686 // make sure all attached devices have been allocated a unique ID
2687 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002688 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002689 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002690 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2691 continue;
2692 }
François Gaffie2110e042015-03-24 08:41:51 +01002693 // The device is now validated and can be appended to the available devices of the engine
2694 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2695 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002696 i++;
2697 }
2698 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002699 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002700 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002701 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2702 continue;
2703 }
François Gaffie2110e042015-03-24 08:41:51 +01002704 // The device is now validated and can be appended to the available devices of the engine
2705 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2706 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002707 i++;
2708 }
2709 // make sure default device is reachable
2710 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002711 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002712 }
Eric Laurente552edb2014-03-10 17:42:56 -07002713
2714 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2715
2716 updateDevicesAndOutputs();
2717
2718#ifdef AUDIO_POLICY_TEST
2719 if (mPrimaryOutput != 0) {
2720 AudioParameter outputCmd = AudioParameter();
2721 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002722 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002723
2724 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2725 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002726 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2727 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002728 mTestLatencyMs = 0;
2729 mCurOutput = 0;
2730 mDirectOutput = false;
2731 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2732 mTestOutputs[i] = 0;
2733 }
2734
2735 const size_t SIZE = 256;
2736 char buffer[SIZE];
2737 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2738 run(buffer, ANDROID_PRIORITY_AUDIO);
2739 }
2740#endif //AUDIO_POLICY_TEST
2741}
2742
Eric Laurente0720872014-03-11 09:30:41 -07002743AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002744{
2745#ifdef AUDIO_POLICY_TEST
2746 exit();
2747#endif //AUDIO_POLICY_TEST
2748 for (size_t i = 0; i < mOutputs.size(); i++) {
2749 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002750 }
2751 for (size_t i = 0; i < mInputs.size(); i++) {
2752 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002753 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002754 mAvailableOutputDevices.clear();
2755 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002756 mOutputs.clear();
2757 mInputs.clear();
2758 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002759}
2760
Eric Laurente0720872014-03-11 09:30:41 -07002761status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002762{
2763 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2764}
2765
2766#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002767bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002768{
2769 ALOGV("entering threadLoop()");
2770 while (!exitPending())
2771 {
2772 String8 command;
2773 int valueInt;
2774 String8 value;
2775
2776 Mutex::Autolock _l(mLock);
2777 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2778
2779 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2780 AudioParameter param = AudioParameter(command);
2781
2782 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2783 valueInt != 0) {
2784 ALOGV("Test command %s received", command.string());
2785 String8 target;
2786 if (param.get(String8("target"), target) != NO_ERROR) {
2787 target = "Manager";
2788 }
2789 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2790 param.remove(String8("test_cmd_policy_output"));
2791 mCurOutput = valueInt;
2792 }
2793 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2794 param.remove(String8("test_cmd_policy_direct"));
2795 if (value == "false") {
2796 mDirectOutput = false;
2797 } else if (value == "true") {
2798 mDirectOutput = true;
2799 }
2800 }
2801 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2802 param.remove(String8("test_cmd_policy_input"));
2803 mTestInput = valueInt;
2804 }
2805
2806 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2807 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002808 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002809 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002810 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002811 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002812 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002813 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002814 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002815 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002816 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002817 if (target == "Manager") {
2818 mTestFormat = format;
2819 } else if (mTestOutputs[mCurOutput] != 0) {
2820 AudioParameter outputParam = AudioParameter();
2821 outputParam.addInt(String8("format"), format);
2822 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2823 }
2824 }
2825 }
2826 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2827 param.remove(String8("test_cmd_policy_channels"));
2828 int channels = 0;
2829
2830 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002831 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002832 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002833 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002834 }
2835 if (channels != 0) {
2836 if (target == "Manager") {
2837 mTestChannels = channels;
2838 } else if (mTestOutputs[mCurOutput] != 0) {
2839 AudioParameter outputParam = AudioParameter();
2840 outputParam.addInt(String8("channels"), channels);
2841 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2842 }
2843 }
2844 }
2845 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2846 param.remove(String8("test_cmd_policy_sampleRate"));
2847 if (valueInt >= 0 && valueInt <= 96000) {
2848 int samplingRate = valueInt;
2849 if (target == "Manager") {
2850 mTestSamplingRate = samplingRate;
2851 } else if (mTestOutputs[mCurOutput] != 0) {
2852 AudioParameter outputParam = AudioParameter();
2853 outputParam.addInt(String8("sampling_rate"), samplingRate);
2854 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2855 }
2856 }
2857 }
2858
2859 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2860 param.remove(String8("test_cmd_policy_reopen"));
2861
Eric Laurentc75307b2015-03-17 15:29:32 -07002862 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07002863
Eric Laurentc75307b2015-03-17 15:29:32 -07002864 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07002865
Eric Laurentc75307b2015-03-17 15:29:32 -07002866 removeOutput(mPrimaryOutput->mIoHandle);
2867 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
2868 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07002869 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002870 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2871 config.sample_rate = outputDesc->mSamplingRate;
2872 config.channel_mask = outputDesc->mChannelMask;
2873 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07002874 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002875 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07002876 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002877 &config,
2878 &outputDesc->mDevice,
2879 String8(""),
2880 &outputDesc->mLatency,
2881 outputDesc->mFlags);
2882 if (status != NO_ERROR) {
2883 ALOGE("Failed to reopen hardware output stream, "
2884 "samplingRate: %d, format %d, channels %d",
2885 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07002886 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002887 outputDesc->mSamplingRate = config.sample_rate;
2888 outputDesc->mChannelMask = config.channel_mask;
2889 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07002890 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07002891 AudioParameter outputCmd = AudioParameter();
2892 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002893 mpClientInterface->setParameters(handle, outputCmd.toString());
2894 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07002895 }
2896 }
2897
2898
2899 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2900 }
2901 }
2902 return false;
2903}
2904
Eric Laurente0720872014-03-11 09:30:41 -07002905void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002906{
2907 {
2908 AutoMutex _l(mLock);
2909 requestExit();
2910 mWaitWorkCV.signal();
2911 }
2912 requestExitAndWait();
2913}
2914
Eric Laurente0720872014-03-11 09:30:41 -07002915int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002916{
2917 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2918 if (output == mTestOutputs[i]) return i;
2919 }
2920 return 0;
2921}
2922#endif //AUDIO_POLICY_TEST
2923
2924// ---
2925
Eric Laurentc75307b2015-03-17 15:29:32 -07002926void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002927{
François Gaffie98cc1912015-03-18 17:52:40 +01002928 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07002929 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002930 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002931}
2932
François Gaffie53615e22015-03-19 09:24:12 +01002933void AudioPolicyManager::removeOutput(audio_io_handle_t output)
2934{
2935 mOutputs.removeItem(output);
2936}
2937
Eric Laurent1f2f2232014-06-02 12:01:23 -07002938void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002939{
François Gaffie98cc1912015-03-18 17:52:40 +01002940 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07002941 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002942 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002943}
Eric Laurente552edb2014-03-10 17:42:56 -07002944
Eric Laurentc75307b2015-03-17 15:29:32 -07002945void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08002946 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002947 const String8 address /*in*/,
2948 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08002949 sp<DeviceDescriptor> devDesc =
2950 desc->mProfile->mSupportedDevices.getDevice(device, address);
2951 if (devDesc != 0) {
2952 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
2953 desc->mIoHandle, address.string());
2954 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002955 }
2956}
2957
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002958status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01002959 audio_policy_dev_state_t state,
2960 SortedVector<audio_io_handle_t>& outputs,
2961 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002962{
François Gaffie53615e22015-03-19 09:24:12 +01002963 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07002964 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07002965 // erase all current sample rates, formats and channel masks
2966 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07002967
Eric Laurent3b73df72014-03-11 09:06:29 -07002968 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002969 // first list already open outputs that can be routed to this device
2970 for (size_t i = 0; i < mOutputs.size(); i++) {
2971 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07002972 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01002973 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002974 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2975 outputs.add(mOutputs.keyAt(i));
2976 } else {
2977 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08002978 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07002979 }
Eric Laurente552edb2014-03-10 17:42:56 -07002980 }
2981 }
2982 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002983 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002984 for (size_t i = 0; i < mHwModules.size(); i++)
2985 {
2986 if (mHwModules[i]->mHandle == 0) {
2987 continue;
2988 }
2989 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2990 {
Eric Laurent275e8e92014-11-30 15:14:47 -08002991 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
2992 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01002993 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08002994 address == profile->mSupportedDevices[0]->mAddress) {
2995 profiles.add(profile);
2996 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
2997 }
Eric Laurente552edb2014-03-10 17:42:56 -07002998 }
2999 }
3000 }
3001
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003002 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3003
Eric Laurente552edb2014-03-10 17:42:56 -07003004 if (profiles.isEmpty() && outputs.isEmpty()) {
3005 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3006 return BAD_VALUE;
3007 }
3008
3009 // open outputs for matching profiles if needed. Direct outputs are also opened to
3010 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3011 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003012 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003013
3014 // nothing to do if one output is already opened for this profile
3015 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003016 for (j = 0; j < outputs.size(); j++) {
3017 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003018 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003019 // matching profile: save the sample rates, format and channel masks supported
3020 // by the profile in our device descriptor
3021 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003022 break;
3023 }
3024 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003025 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003026 continue;
3027 }
3028
Eric Laurent83b88082014-06-20 18:31:16 -07003029 ALOGV("opening output for device %08x with params %s profile %p",
3030 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003031 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003032 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003033 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3034 config.sample_rate = desc->mSamplingRate;
3035 config.channel_mask = desc->mChannelMask;
3036 config.format = desc->mFormat;
3037 config.offload_info.sample_rate = desc->mSamplingRate;
3038 config.offload_info.channel_mask = desc->mChannelMask;
3039 config.offload_info.format = desc->mFormat;
3040 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003041 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003042 &output,
3043 &config,
3044 &desc->mDevice,
3045 address,
3046 &desc->mLatency,
3047 desc->mFlags);
3048 if (status == NO_ERROR) {
3049 desc->mSamplingRate = config.sample_rate;
3050 desc->mChannelMask = config.channel_mask;
3051 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003052
Eric Laurentd4692962014-05-05 18:13:44 -07003053 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003054 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003055 char *param = audio_device_address_to_parameter(device, address);
3056 mpClientInterface->setParameters(output, String8(param));
3057 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003058 }
3059
Eric Laurentd4692962014-05-05 18:13:44 -07003060 // Here is where we step through and resolve any "dynamic" fields
3061 String8 reply;
3062 char *value;
3063 if (profile->mSamplingRates[0] == 0) {
3064 reply = mpClientInterface->getParameters(output,
3065 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003066 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003067 reply.string());
3068 value = strpbrk((char *)reply.string(), "=");
3069 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003070 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003071 }
Eric Laurentd4692962014-05-05 18:13:44 -07003072 }
3073 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3074 reply = mpClientInterface->getParameters(output,
3075 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003076 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003077 reply.string());
3078 value = strpbrk((char *)reply.string(), "=");
3079 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003080 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003081 }
Eric Laurentd4692962014-05-05 18:13:44 -07003082 }
3083 if (profile->mChannelMasks[0] == 0) {
3084 reply = mpClientInterface->getParameters(output,
3085 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003086 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003087 reply.string());
3088 value = strpbrk((char *)reply.string(), "=");
3089 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003090 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003091 }
Eric Laurentd4692962014-05-05 18:13:44 -07003092 }
3093 if (((profile->mSamplingRates[0] == 0) &&
3094 (profile->mSamplingRates.size() < 2)) ||
3095 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3096 (profile->mFormats.size() < 2)) ||
3097 ((profile->mChannelMasks[0] == 0) &&
3098 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003099 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003100 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003101 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003102 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3103 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003104 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003105 config.sample_rate = profile->pickSamplingRate();
3106 config.channel_mask = profile->pickChannelMask();
3107 config.format = profile->pickFormat();
3108 config.offload_info.sample_rate = config.sample_rate;
3109 config.offload_info.channel_mask = config.channel_mask;
3110 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003111 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003112 &output,
3113 &config,
3114 &desc->mDevice,
3115 address,
3116 &desc->mLatency,
3117 desc->mFlags);
3118 if (status == NO_ERROR) {
3119 desc->mSamplingRate = config.sample_rate;
3120 desc->mChannelMask = config.channel_mask;
3121 desc->mFormat = config.format;
3122 } else {
3123 output = AUDIO_IO_HANDLE_NONE;
3124 }
Eric Laurentd4692962014-05-05 18:13:44 -07003125 }
3126
Eric Laurentcf2c0212014-07-25 16:20:43 -07003127 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003128 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003129 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003130 sp<AudioPolicyMix> policyMix;
3131 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003132 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3133 address.string());
3134 }
François Gaffie036e1e92015-03-19 10:16:24 +01003135 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003136 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003137
Eric Laurentc722f302014-12-10 11:21:49 -08003138 } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
3139 // no duplicated output for direct outputs and
3140 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003141 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003142
Eric Laurentd4692962014-05-05 18:13:44 -07003143 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003144 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003145
Eric Laurentd4692962014-05-05 18:13:44 -07003146 //TODO: configure audio effect output stage here
3147
3148 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003149 duplicatedOutput =
3150 mpClientInterface->openDuplicateOutput(output,
3151 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003152 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003153 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003154 sp<SwAudioOutputDescriptor> dupOutputDesc =
3155 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3156 dupOutputDesc->mOutput1 = mPrimaryOutput;
3157 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003158 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3159 dupOutputDesc->mFormat = desc->mFormat;
3160 dupOutputDesc->mChannelMask = desc->mChannelMask;
3161 dupOutputDesc->mLatency = desc->mLatency;
3162 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003163 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003164 } else {
3165 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003166 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003167 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003168 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003169 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003170 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003171 }
Eric Laurente552edb2014-03-10 17:42:56 -07003172 }
3173 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003174 } else {
3175 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003176 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003177 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003178 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003179 profiles.removeAt(profile_index);
3180 profile_index--;
3181 } else {
3182 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003183 devDesc->importAudioPort(profile);
3184
François Gaffie53615e22015-03-19 09:24:12 +01003185 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003186 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3187 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003188 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003189 NULL/*patch handle*/, address.string());
3190 }
Eric Laurente552edb2014-03-10 17:42:56 -07003191 ALOGV("checkOutputsForDevice(): adding output %d", output);
3192 }
3193 }
3194
3195 if (profiles.isEmpty()) {
3196 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3197 return BAD_VALUE;
3198 }
Eric Laurentd4692962014-05-05 18:13:44 -07003199 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003200 // check if one opened output is not needed any more after disconnecting one device
3201 for (size_t i = 0; i < mOutputs.size(); i++) {
3202 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003203 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003204 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003205 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003206 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003207 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003208 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003209 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3210 mOutputs.keyAt(i));
3211 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003212 }
Eric Laurente552edb2014-03-10 17:42:56 -07003213 }
3214 }
Eric Laurentd4692962014-05-05 18:13:44 -07003215 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003216 for (size_t i = 0; i < mHwModules.size(); i++)
3217 {
3218 if (mHwModules[i]->mHandle == 0) {
3219 continue;
3220 }
3221 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3222 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003223 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003224 if (profile->mSupportedDevices.types() & device) {
3225 ALOGV("checkOutputsForDevice(): "
3226 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003227 if (profile->mSamplingRates[0] == 0) {
3228 profile->mSamplingRates.clear();
3229 profile->mSamplingRates.add(0);
3230 }
3231 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3232 profile->mFormats.clear();
3233 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3234 }
3235 if (profile->mChannelMasks[0] == 0) {
3236 profile->mChannelMasks.clear();
3237 profile->mChannelMasks.add(0);
3238 }
3239 }
3240 }
3241 }
3242 }
3243 return NO_ERROR;
3244}
3245
Eric Laurentd4692962014-05-05 18:13:44 -07003246status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003247 audio_policy_dev_state_t state,
3248 SortedVector<audio_io_handle_t>& inputs,
3249 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003250{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003251 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003252 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3253 // first list already open inputs that can be routed to this device
3254 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3255 desc = mInputs.valueAt(input_index);
3256 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3257 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3258 inputs.add(mInputs.keyAt(input_index));
3259 }
3260 }
3261
3262 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003263 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003264 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3265 {
3266 if (mHwModules[module_idx]->mHandle == 0) {
3267 continue;
3268 }
3269 for (size_t profile_index = 0;
3270 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3271 profile_index++)
3272 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003273 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3274
3275 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003276 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003277 address == profile->mSupportedDevices[0]->mAddress) {
3278 profiles.add(profile);
3279 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3280 profile_index, module_idx);
3281 }
Eric Laurentd4692962014-05-05 18:13:44 -07003282 }
3283 }
3284 }
3285
3286 if (profiles.isEmpty() && inputs.isEmpty()) {
3287 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3288 return BAD_VALUE;
3289 }
3290
3291 // open inputs for matching profiles if needed. Direct inputs are also opened to
3292 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3293 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3294
Eric Laurent1c333e22014-05-20 10:48:17 -07003295 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003296 // nothing to do if one input is already opened for this profile
3297 size_t input_index;
3298 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3299 desc = mInputs.valueAt(input_index);
3300 if (desc->mProfile == profile) {
3301 break;
3302 }
3303 }
3304 if (input_index != mInputs.size()) {
3305 continue;
3306 }
3307
3308 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3309 desc = new AudioInputDescriptor(profile);
3310 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003311 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3312 config.sample_rate = desc->mSamplingRate;
3313 config.channel_mask = desc->mChannelMask;
3314 config.format = desc->mFormat;
3315 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003316 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003317 &input,
3318 &config,
3319 &desc->mDevice,
3320 address,
3321 AUDIO_SOURCE_MIC,
3322 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003323
Eric Laurentcf2c0212014-07-25 16:20:43 -07003324 if (status == NO_ERROR) {
3325 desc->mSamplingRate = config.sample_rate;
3326 desc->mChannelMask = config.channel_mask;
3327 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003328
Eric Laurentd4692962014-05-05 18:13:44 -07003329 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003330 char *param = audio_device_address_to_parameter(device, address);
3331 mpClientInterface->setParameters(input, String8(param));
3332 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003333 }
3334
3335 // Here is where we step through and resolve any "dynamic" fields
3336 String8 reply;
3337 char *value;
3338 if (profile->mSamplingRates[0] == 0) {
3339 reply = mpClientInterface->getParameters(input,
3340 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3341 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3342 reply.string());
3343 value = strpbrk((char *)reply.string(), "=");
3344 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003345 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003346 }
3347 }
3348 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3349 reply = mpClientInterface->getParameters(input,
3350 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3351 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3352 value = strpbrk((char *)reply.string(), "=");
3353 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003354 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003355 }
3356 }
3357 if (profile->mChannelMasks[0] == 0) {
3358 reply = mpClientInterface->getParameters(input,
3359 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3360 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3361 reply.string());
3362 value = strpbrk((char *)reply.string(), "=");
3363 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003364 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003365 }
3366 }
3367 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3368 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3369 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3370 ALOGW("checkInputsForDevice() direct input missing param");
3371 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003372 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003373 }
3374
3375 if (input != 0) {
3376 addInput(input, desc);
3377 }
3378 } // endif input != 0
3379
Eric Laurentcf2c0212014-07-25 16:20:43 -07003380 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003381 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003382 profiles.removeAt(profile_index);
3383 profile_index--;
3384 } else {
3385 inputs.add(input);
3386 ALOGV("checkInputsForDevice(): adding input %d", input);
3387 }
3388 } // end scan profiles
3389
3390 if (profiles.isEmpty()) {
3391 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3392 return BAD_VALUE;
3393 }
3394 } else {
3395 // Disconnect
3396 // check if one opened input is not needed any more after disconnecting one device
3397 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3398 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003399 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3400 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003401 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3402 mInputs.keyAt(input_index));
3403 inputs.add(mInputs.keyAt(input_index));
3404 }
3405 }
3406 // Clear any profiles associated with the disconnected device.
3407 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3408 if (mHwModules[module_index]->mHandle == 0) {
3409 continue;
3410 }
3411 for (size_t profile_index = 0;
3412 profile_index < mHwModules[module_index]->mInputProfiles.size();
3413 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003414 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003415 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003416 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003417 profile_index, module_index);
3418 if (profile->mSamplingRates[0] == 0) {
3419 profile->mSamplingRates.clear();
3420 profile->mSamplingRates.add(0);
3421 }
3422 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3423 profile->mFormats.clear();
3424 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3425 }
3426 if (profile->mChannelMasks[0] == 0) {
3427 profile->mChannelMasks.clear();
3428 profile->mChannelMasks.add(0);
3429 }
3430 }
3431 }
3432 }
3433 } // end disconnect
3434
3435 return NO_ERROR;
3436}
3437
3438
Eric Laurente0720872014-03-11 09:30:41 -07003439void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003440{
3441 ALOGV("closeOutput(%d)", output);
3442
Eric Laurentc75307b2015-03-17 15:29:32 -07003443 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003444 if (outputDesc == NULL) {
3445 ALOGW("closeOutput() unknown output %d", output);
3446 return;
3447 }
François Gaffie036e1e92015-03-19 10:16:24 +01003448 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003449
Eric Laurente552edb2014-03-10 17:42:56 -07003450 // look for duplicated outputs connected to the output being removed.
3451 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003452 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003453 if (dupOutputDesc->isDuplicated() &&
3454 (dupOutputDesc->mOutput1 == outputDesc ||
3455 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003456 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003457 if (dupOutputDesc->mOutput1 == outputDesc) {
3458 outputDesc2 = dupOutputDesc->mOutput2;
3459 } else {
3460 outputDesc2 = dupOutputDesc->mOutput1;
3461 }
3462 // As all active tracks on duplicated output will be deleted,
3463 // and as they were also referenced on the other output, the reference
3464 // count for their stream type must be adjusted accordingly on
3465 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003466 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003467 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003468 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003469 }
3470 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3471 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3472
3473 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003474 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003475 }
3476 }
3477
Eric Laurent05b90f82014-08-27 15:32:29 -07003478 nextAudioPortGeneration();
3479
3480 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3481 if (index >= 0) {
3482 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3483 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3484 mAudioPatches.removeItemsAt(index);
3485 mpClientInterface->onAudioPatchListUpdate();
3486 }
3487
Eric Laurente552edb2014-03-10 17:42:56 -07003488 AudioParameter param;
3489 param.add(String8("closing"), String8("true"));
3490 mpClientInterface->setParameters(output, param.toString());
3491
3492 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003493 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003494 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003495}
3496
3497void AudioPolicyManager::closeInput(audio_io_handle_t input)
3498{
3499 ALOGV("closeInput(%d)", input);
3500
3501 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3502 if (inputDesc == NULL) {
3503 ALOGW("closeInput() unknown input %d", input);
3504 return;
3505 }
3506
Eric Laurent6a94d692014-05-20 11:18:06 -07003507 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003508
3509 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3510 if (index >= 0) {
3511 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3512 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3513 mAudioPatches.removeItemsAt(index);
3514 mpClientInterface->onAudioPatchListUpdate();
3515 }
3516
3517 mpClientInterface->closeInput(input);
3518 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003519}
3520
Eric Laurentc75307b2015-03-17 15:29:32 -07003521SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3522 audio_devices_t device,
3523 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003524{
3525 SortedVector<audio_io_handle_t> outputs;
3526
3527 ALOGVV("getOutputsForDevice() device %04x", device);
3528 for (size_t i = 0; i < openOutputs.size(); i++) {
3529 ALOGVV("output %d isDuplicated=%d device=%04x",
3530 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3531 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3532 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3533 outputs.add(openOutputs.keyAt(i));
3534 }
3535 }
3536 return outputs;
3537}
3538
Eric Laurente0720872014-03-11 09:30:41 -07003539bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003540 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003541{
3542 if (outputs1.size() != outputs2.size()) {
3543 return false;
3544 }
3545 for (size_t i = 0; i < outputs1.size(); i++) {
3546 if (outputs1[i] != outputs2[i]) {
3547 return false;
3548 }
3549 }
3550 return true;
3551}
3552
Eric Laurente0720872014-03-11 09:30:41 -07003553void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003554{
3555 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3556 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3557 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3558 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3559
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003560 // also take into account external policy-related changes: add all outputs which are
3561 // associated with policies in the "before" and "after" output vectors
3562 ALOGVV("checkOutputForStrategy(): policy related outputs");
3563 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003564 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003565 if (desc != 0 && desc->mPolicyMix != NULL) {
3566 srcOutputs.add(desc->mIoHandle);
3567 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3568 }
3569 }
3570 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003571 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003572 if (desc != 0 && desc->mPolicyMix != NULL) {
3573 dstOutputs.add(desc->mIoHandle);
3574 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3575 }
3576 }
3577
Eric Laurente552edb2014-03-10 17:42:56 -07003578 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3579 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3580 strategy, srcOutputs[0], dstOutputs[0]);
3581 // mute strategy while moving tracks from one output to another
3582 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003583 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003584 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003585 setStrategyMute(strategy, true, desc);
3586 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003587 }
3588 }
3589
3590 // Move effects associated to this strategy from previous output to new output
3591 if (strategy == STRATEGY_MEDIA) {
3592 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3593 SortedVector<audio_io_handle_t> moved;
3594 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003595 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3596 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3597 effectDesc->mIo != fxOutput) {
3598 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003599 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3600 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003601 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003602 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003603 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003604 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003605 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003606 }
3607 }
3608 }
3609 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003610 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003611 if (i == AUDIO_STREAM_PATCH) {
3612 continue;
3613 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003614 if (getStrategy((audio_stream_type_t)i) == strategy) {
3615 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003616 }
3617 }
3618 }
3619}
3620
Eric Laurente0720872014-03-11 09:30:41 -07003621void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003622{
François Gaffie2110e042015-03-24 08:41:51 +01003623 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003624 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003625 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003626 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003627 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003628 checkOutputForStrategy(STRATEGY_SONIFICATION);
3629 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003630 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003631 checkOutputForStrategy(STRATEGY_MEDIA);
3632 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003633 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003634}
3635
Eric Laurente0720872014-03-11 09:30:41 -07003636void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003637{
François Gaffie53615e22015-03-19 09:24:12 +01003638 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003639 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003640 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003641 return;
3642 }
3643
Eric Laurent3a4311c2014-03-17 12:00:47 -07003644 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003645 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3646 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3647 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003648 // suspend A2DP output if:
3649 // (NOT already suspended) &&
3650 // ((SCO device is connected &&
3651 // (forced usage for communication || for record is SCO))) ||
3652 // (phone state is ringing || in call)
3653 //
3654 // restore A2DP output if:
3655 // (Already suspended) &&
3656 // ((SCO device is NOT connected ||
3657 // (forced usage NOT for communication && NOT for record is SCO))) &&
3658 // (phone state is NOT ringing && NOT in call)
3659 //
3660 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003661 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003662 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3663 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3664 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3665 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003666
3667 mpClientInterface->restoreOutput(a2dpOutput);
3668 mA2dpSuspended = false;
3669 }
3670 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003671 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003672 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3673 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3674 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3675 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003676
3677 mpClientInterface->suspendOutput(a2dpOutput);
3678 mA2dpSuspended = true;
3679 }
3680 }
3681}
3682
Eric Laurentc75307b2015-03-17 15:29:32 -07003683audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3684 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003685{
3686 audio_devices_t device = AUDIO_DEVICE_NONE;
3687
Eric Laurent6a94d692014-05-20 11:18:06 -07003688 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3689 if (index >= 0) {
3690 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3691 if (patchDesc->mUid != mUidCached) {
3692 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3693 outputDesc->device(), outputDesc->mPatchHandle);
3694 return outputDesc->device();
3695 }
3696 }
3697
Eric Laurente552edb2014-03-10 17:42:56 -07003698 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003699 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003700 // use device for strategy enforced audible
3701 // 2: we are in call or the strategy phone is active on the output:
3702 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003703 // 3: the strategy for enforced audible is active but not enforced on the output:
3704 // use the device for strategy enforced audible
3705 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003706 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003707 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003708 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003709 // 6: the strategy accessibility is active on the output:
3710 // use device for strategy accessibility
3711 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003712 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003713 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003714 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003715 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003716 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003717 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003718 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003719 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3720 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003721 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003722 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003723 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003724 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003725 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003726 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003727 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003728 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003729 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003730 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003731 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003732 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003733 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003734 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003735 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003736 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003737 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003738 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003739 }
3740
Eric Laurent1c333e22014-05-20 10:48:17 -07003741 ALOGV("getNewOutputDevice() selected device %x", device);
3742 return device;
3743}
3744
3745audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3746{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003747 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003748
3749 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3750 if (index >= 0) {
3751 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3752 if (patchDesc->mUid != mUidCached) {
3753 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3754 inputDesc->mDevice, inputDesc->mPatchHandle);
3755 return inputDesc->mDevice;
3756 }
3757 }
3758
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003759 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003760
3761 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003762 return device;
3763}
3764
Eric Laurente0720872014-03-11 09:30:41 -07003765uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003766 return (uint32_t)getStrategy(stream);
3767}
3768
Eric Laurente0720872014-03-11 09:30:41 -07003769audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003770 // By checking the range of stream before calling getStrategy, we avoid
3771 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3772 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003773 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003774 return AUDIO_DEVICE_NONE;
3775 }
3776 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003777 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003778 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3779 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3780 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003781 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003782 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003783 devices = outputDesc->device();
3784 break;
3785 }
Eric Laurente552edb2014-03-10 17:42:56 -07003786 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003787
3788 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3789 and doesn't really need to.*/
3790 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3791 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3792 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3793 }
3794
Eric Laurente552edb2014-03-10 17:42:56 -07003795 return devices;
3796}
3797
François Gaffie2110e042015-03-24 08:41:51 +01003798routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3799{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003800 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003801 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003802}
3803
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003804uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3805 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003806 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3807 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3808 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003809 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3810 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3811 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003812 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003813 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003814}
3815
Eric Laurente0720872014-03-11 09:30:41 -07003816void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003817 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003818 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003819 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3820 updateDevicesAndOutputs();
3821 break;
3822 default:
3823 break;
3824 }
3825}
3826
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003827uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3828 switch(event) {
3829 case STARTING_OUTPUT:
3830 mBeaconMuteRefCount++;
3831 break;
3832 case STOPPING_OUTPUT:
3833 if (mBeaconMuteRefCount > 0) {
3834 mBeaconMuteRefCount--;
3835 }
3836 break;
3837 case STARTING_BEACON:
3838 mBeaconPlayingRefCount++;
3839 break;
3840 case STOPPING_BEACON:
3841 if (mBeaconPlayingRefCount > 0) {
3842 mBeaconPlayingRefCount--;
3843 }
3844 break;
3845 }
3846
3847 if (mBeaconMuteRefCount > 0) {
3848 // any playback causes beacon to be muted
3849 return setBeaconMute(true);
3850 } else {
3851 // no other playback: unmute when beacon starts playing, mute when it stops
3852 return setBeaconMute(mBeaconPlayingRefCount == 0);
3853 }
3854}
3855
3856uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3857 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3858 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
3859 // keep track of muted state to avoid repeating mute/unmute operations
3860 if (mBeaconMuted != mute) {
3861 // mute/unmute AUDIO_STREAM_TTS on all outputs
3862 ALOGV("\t muting %d", mute);
3863 uint32_t maxLatency = 0;
3864 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003865 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003866 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07003867 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003868 0 /*delay*/, AUDIO_DEVICE_NONE);
3869 const uint32_t latency = desc->latency() * 2;
3870 if (latency > maxLatency) {
3871 maxLatency = latency;
3872 }
3873 }
3874 mBeaconMuted = mute;
3875 return maxLatency;
3876 }
3877 return 0;
3878}
3879
Eric Laurente0720872014-03-11 09:30:41 -07003880audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01003881 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003882{
Paul McLeanaa981192015-03-21 09:55:15 -07003883 // Routing
3884 // see if we have an explicit route
3885 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
3886 // (getStrategy(stream)).
3887 // if the strategy from the stream type in the RouteMap is the same as the argument above,
3888 // and activity count is non-zero
3889 // the device = the device from the descriptor in the RouteMap, and exit.
3890 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
3891 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
3892 routing_strategy strat = getStrategy(route->mStreamType);
3893 if (strat == strategy && route->mDeviceDescriptor != 0 /*&& route->mActivityCount != 0*/) {
3894 return route->mDeviceDescriptor->type();
3895 }
3896 }
3897
Eric Laurente552edb2014-03-10 17:42:56 -07003898 if (fromCache) {
3899 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3900 strategy, mDeviceForStrategy[strategy]);
3901 return mDeviceForStrategy[strategy];
3902 }
François Gaffie2110e042015-03-24 08:41:51 +01003903 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07003904}
3905
Eric Laurente0720872014-03-11 09:30:41 -07003906void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003907{
3908 for (int i = 0; i < NUM_STRATEGIES; i++) {
3909 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3910 }
3911 mPreviousOutputs = mOutputs;
3912}
3913
Eric Laurent1f2f2232014-06-02 12:01:23 -07003914uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003915 audio_devices_t prevDevice,
3916 uint32_t delayMs)
3917{
3918 // mute/unmute strategies using an incompatible device combination
3919 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3920 // if unmuting, unmute only after the specified delay
3921 if (outputDesc->isDuplicated()) {
3922 return 0;
3923 }
3924
3925 uint32_t muteWaitMs = 0;
3926 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003927 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003928
3929 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3930 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07003931 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07003932 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3933 bool doMute = false;
3934
3935 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3936 doMute = true;
3937 outputDesc->mStrategyMutedByDevice[i] = true;
3938 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3939 doMute = true;
3940 outputDesc->mStrategyMutedByDevice[i] = false;
3941 }
Eric Laurent99401132014-05-07 19:48:15 -07003942 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003943 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003944 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003945 // skip output if it does not share any device with current output
3946 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3947 == AUDIO_DEVICE_NONE) {
3948 continue;
3949 }
Eric Laurentc75307b2015-03-17 15:29:32 -07003950 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
3951 mute ? "muting" : "unmuting", i, curDevice);
3952 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01003953 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003954 if (mute) {
3955 // FIXME: should not need to double latency if volume could be applied
3956 // immediately by the audioflinger mixer. We must account for the delay
3957 // between now and the next time the audioflinger thread for this output
3958 // will process a buffer (which corresponds to one buffer size,
3959 // usually 1/2 or 1/4 of the latency).
3960 if (muteWaitMs < desc->latency() * 2) {
3961 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003962 }
3963 }
3964 }
3965 }
3966 }
3967 }
3968
Eric Laurent99401132014-05-07 19:48:15 -07003969 // temporary mute output if device selection changes to avoid volume bursts due to
3970 // different per device volumes
3971 if (outputDesc->isActive() && (device != prevDevice)) {
3972 if (muteWaitMs < outputDesc->latency() * 2) {
3973 muteWaitMs = outputDesc->latency() * 2;
3974 }
3975 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01003976 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003977 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07003978 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07003979 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07003980 muteWaitMs *2, device);
3981 }
3982 }
3983 }
3984
Eric Laurente552edb2014-03-10 17:42:56 -07003985 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3986 if (muteWaitMs > delayMs) {
3987 muteWaitMs -= delayMs;
3988 usleep(muteWaitMs * 1000);
3989 return muteWaitMs;
3990 }
3991 return 0;
3992}
3993
Eric Laurentc75307b2015-03-17 15:29:32 -07003994uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003995 audio_devices_t device,
3996 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003997 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003998 audio_patch_handle_t *patchHandle,
3999 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004000{
Eric Laurentc75307b2015-03-17 15:29:32 -07004001 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004002 AudioParameter param;
4003 uint32_t muteWaitMs;
4004
4005 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004006 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4007 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004008 return muteWaitMs;
4009 }
4010 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4011 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004012 if ((device != AUDIO_DEVICE_NONE) &&
4013 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004014 return 0;
4015 }
4016
4017 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004018 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004019
4020 audio_devices_t prevDevice = outputDesc->mDevice;
4021
Paul McLeanaa981192015-03-21 09:55:15 -07004022 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004023
4024 if (device != AUDIO_DEVICE_NONE) {
4025 outputDesc->mDevice = device;
4026 }
4027 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4028
4029 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004030 // the requested device is AUDIO_DEVICE_NONE
4031 // OR the requested device is the same as current device
4032 // AND force is not specified
4033 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004034 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004035 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4036 !force &&
4037 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004038 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004039 return muteWaitMs;
4040 }
4041
4042 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004043
Eric Laurente552edb2014-03-10 17:42:56 -07004044 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004045 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004046 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004047 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004048 DeviceVector deviceList = (address == NULL) ?
4049 mAvailableOutputDevices.getDevicesFromType(device)
4050 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004051 if (!deviceList.isEmpty()) {
4052 struct audio_patch patch;
4053 outputDesc->toAudioPortConfig(&patch.sources[0]);
4054 patch.num_sources = 1;
4055 patch.num_sinks = 0;
4056 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4057 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004058 patch.num_sinks++;
4059 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004060 ssize_t index;
4061 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4062 index = mAudioPatches.indexOfKey(*patchHandle);
4063 } else {
4064 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4065 }
4066 sp< AudioPatch> patchDesc;
4067 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4068 if (index >= 0) {
4069 patchDesc = mAudioPatches.valueAt(index);
4070 afPatchHandle = patchDesc->mAfPatchHandle;
4071 }
4072
Eric Laurent1c333e22014-05-20 10:48:17 -07004073 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004074 &afPatchHandle,
4075 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004076 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4077 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004078 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004079 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004080 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004081 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004082 addAudioPatch(patchDesc->mHandle, patchDesc);
4083 } else {
4084 patchDesc->mPatch = patch;
4085 }
4086 patchDesc->mAfPatchHandle = afPatchHandle;
4087 patchDesc->mUid = mUidCached;
4088 if (patchHandle) {
4089 *patchHandle = patchDesc->mHandle;
4090 }
4091 outputDesc->mPatchHandle = patchDesc->mHandle;
4092 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004093 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004094 }
4095 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004096
4097 // inform all input as well
4098 for (size_t i = 0; i < mInputs.size(); i++) {
4099 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004100 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004101 AudioParameter inputCmd = AudioParameter();
4102 ALOGV("%s: inform input %d of device:%d", __func__,
4103 inputDescriptor->mIoHandle, device);
4104 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4105 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4106 inputCmd.toString(),
4107 delayMs);
4108 }
4109 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004110 }
Eric Laurente552edb2014-03-10 17:42:56 -07004111
4112 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004113 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004114
4115 return muteWaitMs;
4116}
4117
Eric Laurentc75307b2015-03-17 15:29:32 -07004118status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004119 int delayMs,
4120 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004121{
Eric Laurent6a94d692014-05-20 11:18:06 -07004122 ssize_t index;
4123 if (patchHandle) {
4124 index = mAudioPatches.indexOfKey(*patchHandle);
4125 } else {
4126 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4127 }
4128 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004129 return INVALID_OPERATION;
4130 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004131 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4132 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004133 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4134 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004135 removeAudioPatch(patchDesc->mHandle);
4136 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004137 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004138 return status;
4139}
4140
4141status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4142 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004143 bool force,
4144 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004145{
4146 status_t status = NO_ERROR;
4147
Eric Laurent1f2f2232014-06-02 12:01:23 -07004148 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004149 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4150 inputDesc->mDevice = device;
4151
4152 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4153 if (!deviceList.isEmpty()) {
4154 struct audio_patch patch;
4155 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004156 // AUDIO_SOURCE_HOTWORD is for internal use only:
4157 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004158 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4159 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004160 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4161 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004162 patch.num_sinks = 1;
4163 //only one input device for now
4164 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004165 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004166 ssize_t index;
4167 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4168 index = mAudioPatches.indexOfKey(*patchHandle);
4169 } else {
4170 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4171 }
4172 sp< AudioPatch> patchDesc;
4173 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4174 if (index >= 0) {
4175 patchDesc = mAudioPatches.valueAt(index);
4176 afPatchHandle = patchDesc->mAfPatchHandle;
4177 }
4178
Eric Laurent1c333e22014-05-20 10:48:17 -07004179 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004180 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004181 0);
4182 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004183 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004184 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004185 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004186 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004187 addAudioPatch(patchDesc->mHandle, patchDesc);
4188 } else {
4189 patchDesc->mPatch = patch;
4190 }
4191 patchDesc->mAfPatchHandle = afPatchHandle;
4192 patchDesc->mUid = mUidCached;
4193 if (patchHandle) {
4194 *patchHandle = patchDesc->mHandle;
4195 }
4196 inputDesc->mPatchHandle = patchDesc->mHandle;
4197 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004198 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004199 }
4200 }
4201 }
4202 return status;
4203}
4204
Eric Laurent6a94d692014-05-20 11:18:06 -07004205status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4206 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004207{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004208 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004209 ssize_t index;
4210 if (patchHandle) {
4211 index = mAudioPatches.indexOfKey(*patchHandle);
4212 } else {
4213 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4214 }
4215 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004216 return INVALID_OPERATION;
4217 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004218 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4219 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004220 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4221 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004222 removeAudioPatch(patchDesc->mHandle);
4223 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004224 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004225 return status;
4226}
4227
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004228sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004229 String8 address,
4230 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004231 audio_format_t& format,
4232 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004233 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004234{
4235 // Choose an input profile based on the requested capture parameters: select the first available
4236 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004237 //
4238 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4239 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004240
4241 for (size_t i = 0; i < mHwModules.size(); i++)
4242 {
4243 if (mHwModules[i]->mHandle == 0) {
4244 continue;
4245 }
4246 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4247 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004248 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004249 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004250 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004251 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004252 format,
4253 &format /*updatedFormat*/,
4254 channelMask,
4255 &channelMask /*updatedChannelMask*/,
4256 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004257
Eric Laurente552edb2014-03-10 17:42:56 -07004258 return profile;
4259 }
4260 }
4261 }
4262 return NULL;
4263}
4264
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004265
4266audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004267 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004268{
François Gaffie036e1e92015-03-19 10:16:24 +01004269 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4270 audio_devices_t selectedDeviceFromMix =
4271 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004272
François Gaffie036e1e92015-03-19 10:16:24 +01004273 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4274 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004275 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004276 return getDeviceForInputSource(inputSource);
4277}
4278
4279audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4280{
François Gaffie2110e042015-03-24 08:41:51 +01004281 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004282}
4283
Eric Laurente0720872014-03-11 09:30:41 -07004284float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004285 int index,
4286 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004287{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004288 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004289
4290 // if a headset is connected, apply the following rules to ring tones and notifications
4291 // to avoid sound level bursts in user's ears:
4292 // - always attenuate ring tones and notifications volume by 6dB
4293 // - if music is playing, always limit the volume to current music volume,
4294 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004295 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004296 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4297 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4298 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4299 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4300 ((stream_strategy == STRATEGY_SONIFICATION)
4301 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004302 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004303 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004304 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4305 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004306 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004307 // when the phone is ringing we must consider that music could have been paused just before
4308 // by the music application and behave as if music was active if the last music track was
4309 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004310 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004311 mLimitRingtoneVolume) {
4312 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004313 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4314 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004315 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004316 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4317 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4318 if (volumeDb > minVolDB) {
4319 volumeDb = minVolDB;
4320 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004321 }
4322 }
4323 }
4324
Eric Laurentffbc80f2015-03-18 18:30:19 -07004325 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004326}
4327
Eric Laurente0720872014-03-11 09:30:41 -07004328status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004329 int index,
4330 const sp<AudioOutputDescriptor>& outputDesc,
4331 audio_devices_t device,
4332 int delayMs,
4333 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004334{
Eric Laurente552edb2014-03-10 17:42:56 -07004335 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004336 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004337 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004338 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004339 return NO_ERROR;
4340 }
François Gaffie2110e042015-03-24 08:41:51 +01004341 audio_policy_forced_cfg_t forceUseForComm =
4342 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004343 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004344 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4345 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004346 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004347 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004348 return INVALID_OPERATION;
4349 }
4350
Eric Laurentc75307b2015-03-17 15:29:32 -07004351 if (device == AUDIO_DEVICE_NONE) {
4352 device = outputDesc->device();
4353 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004354
Eric Laurentffbc80f2015-03-18 18:30:19 -07004355 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004356 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004357 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004358 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004359
Eric Laurentffbc80f2015-03-18 18:30:19 -07004360 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004361
Eric Laurent3b73df72014-03-11 09:06:29 -07004362 if (stream == AUDIO_STREAM_VOICE_CALL ||
4363 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004364 float voiceVolume;
4365 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004366 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004367 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004368 } else {
4369 voiceVolume = 1.0;
4370 }
4371
Eric Laurentc75307b2015-03-17 15:29:32 -07004372 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004373 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4374 mLastVoiceVolume = voiceVolume;
4375 }
4376 }
4377
4378 return NO_ERROR;
4379}
4380
Eric Laurentc75307b2015-03-17 15:29:32 -07004381void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4382 audio_devices_t device,
4383 int delayMs,
4384 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004385{
Eric Laurentc75307b2015-03-17 15:29:32 -07004386 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004387
Eric Laurent3b73df72014-03-11 09:06:29 -07004388 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004389 if (stream == AUDIO_STREAM_PATCH) {
4390 continue;
4391 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004392 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004393 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4394 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004395 device,
4396 delayMs,
4397 force);
4398 }
4399}
4400
Eric Laurente0720872014-03-11 09:30:41 -07004401void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004402 bool on,
4403 const sp<AudioOutputDescriptor>& outputDesc,
4404 int delayMs,
4405 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004406{
4407 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004408 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004409 if (stream == AUDIO_STREAM_PATCH) {
4410 continue;
4411 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004412 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004413 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004414 }
4415 }
4416}
4417
Eric Laurente0720872014-03-11 09:30:41 -07004418void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004419 bool on,
4420 const sp<AudioOutputDescriptor>& outputDesc,
4421 int delayMs,
4422 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004423{
Eric Laurentc75307b2015-03-17 15:29:32 -07004424 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004425 if (device == AUDIO_DEVICE_NONE) {
4426 device = outputDesc->device();
4427 }
4428
Eric Laurentc75307b2015-03-17 15:29:32 -07004429 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4430 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004431
4432 if (on) {
4433 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004434 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004435 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004436 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004437 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004438 }
4439 }
4440 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4441 outputDesc->mMuteCount[stream]++;
4442 } else {
4443 if (outputDesc->mMuteCount[stream] == 0) {
4444 ALOGV("setStreamMute() unmuting non muted stream!");
4445 return;
4446 }
4447 if (--outputDesc->mMuteCount[stream] == 0) {
4448 checkAndSetVolume(stream,
4449 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004450 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004451 device,
4452 delayMs);
4453 }
4454 }
4455}
4456
Eric Laurente0720872014-03-11 09:30:41 -07004457void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004458 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004459{
4460 // if the stream pertains to sonification strategy and we are in call we must
4461 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4462 // in the device used for phone strategy and play the tone if the selected device does not
4463 // interfere with the device used for phone strategy
4464 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4465 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004466 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004467 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4468 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004469 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004470 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4471 stream, starting, outputDesc->mDevice, stateChange);
4472 if (outputDesc->mRefCount[stream]) {
4473 int muteCount = 1;
4474 if (stateChange) {
4475 muteCount = outputDesc->mRefCount[stream];
4476 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004477 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004478 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4479 for (int i = 0; i < muteCount; i++) {
4480 setStreamMute(stream, starting, mPrimaryOutput);
4481 }
4482 } else {
4483 ALOGV("handleIncallSonification() high visibility");
4484 if (outputDesc->device() &
4485 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4486 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4487 for (int i = 0; i < muteCount; i++) {
4488 setStreamMute(stream, starting, mPrimaryOutput);
4489 }
4490 }
4491 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004492 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4493 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004494 } else {
4495 mpClientInterface->stopTone();
4496 }
4497 }
4498 }
4499 }
4500}
4501
Paul McLeanaa981192015-03-21 09:55:15 -07004502// --- SessionRoute class implementation
4503void AudioPolicyManager::SessionRoute::log(const char* prefix) {
4504 ALOGI("%s[SessionRoute strm:0x%X, sess:0x%X, dev:0x%X refs:%d act:%d",
4505 prefix, mStreamType, mSession,
4506 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4507 mRefCount, mActivityCount);
4508}
4509
4510// --- SessionRouteMap class implementation
4511bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4512{
4513 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4514}
4515
Eric Laurent493404d2015-04-21 15:07:36 -07004516bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4517{
4518 if (indexOfKey(session) >= 0) {
4519 if (valueFor(session)->mChanged) {
4520 valueFor(session)->mChanged = false;
4521 return true;
4522 }
4523 }
4524 return false;
4525}
4526
Paul McLeanaa981192015-03-21 09:55:15 -07004527void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4528 audio_stream_type_t streamType,
4529 sp<DeviceDescriptor> deviceDescriptor)
4530{
4531 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4532 if (route != NULL) {
Eric Laurent493404d2015-04-21 15:07:36 -07004533 if ((route->mDeviceDescriptor == 0 && deviceDescriptor != 0) ||
4534 (!route->mDeviceDescriptor->equals(deviceDescriptor))) {
4535 route->mChanged = true;
4536 }
Paul McLeanaa981192015-03-21 09:55:15 -07004537 route->mRefCount++;
4538 route->mDeviceDescriptor = deviceDescriptor;
4539 } else {
4540 route = new AudioPolicyManager::SessionRoute(session, streamType, deviceDescriptor);
4541 route->mRefCount++;
4542 add(session, route);
Eric Laurent493404d2015-04-21 15:07:36 -07004543 if (deviceDescriptor != 0) {
4544 route->mChanged = true;
4545 }
Paul McLeanaa981192015-03-21 09:55:15 -07004546 }
4547}
4548
4549void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4550{
4551 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4552 if (route != 0) {
4553 ALOG_ASSERT(route->mRefCount > 0);
4554 --route->mRefCount;
4555 if (route->mRefCount <= 0) {
4556 removeItem(session);
4557 }
4558 }
4559}
4560
4561int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4562{
4563 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4564 return route != 0 ? ++(route->mActivityCount) : -1;
4565}
4566
4567int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4568{
4569 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4570 if (route != 0 && route->mActivityCount > 0) {
4571 return --(route->mActivityCount);
4572 } else {
4573 return -1;
4574 }
4575}
4576
4577void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4578 ALOGI("%s ----", caption);
4579 for(size_t index = 0; index < size(); index++) {
4580 valueAt(index)->log(" ");
4581 }
4582}
4583
Eric Laurente0720872014-03-11 09:30:41 -07004584void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004585{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004586 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004587 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004588 sp<DeviceDescriptor> defaultInputDevice =
4589 new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004590 mAvailableOutputDevices.add(mDefaultOutputDevice);
4591 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004592
4593 module = new HwModule("primary");
4594
Eric Laurent322b4d22015-04-03 15:57:54 -07004595 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4596 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004597 profile->mSamplingRates.add(44100);
4598 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4599 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004600 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004601 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4602 module->mOutputProfiles.add(profile);
4603
Eric Laurent322b4d22015-04-03 15:57:54 -07004604 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4605 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004606 profile->mSamplingRates.add(8000);
4607 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4608 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004609 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004610 module->mInputProfiles.add(profile);
4611
4612 mHwModules.add(module);
4613}
4614
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004615audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4616{
4617 // flags to stream type mapping
4618 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4619 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4620 }
4621 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4622 return AUDIO_STREAM_BLUETOOTH_SCO;
4623 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004624 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4625 return AUDIO_STREAM_TTS;
4626 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004627
4628 // usage to stream type mapping
4629 switch (attr->usage) {
4630 case AUDIO_USAGE_MEDIA:
4631 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004632 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4633 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004634 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004635 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4636 return AUDIO_STREAM_ALARM;
4637 }
4638 if (isStreamActive(AUDIO_STREAM_RING)) {
4639 return AUDIO_STREAM_RING;
4640 }
4641 if (isInCall()) {
4642 return AUDIO_STREAM_VOICE_CALL;
4643 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004644 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004645 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4646 return AUDIO_STREAM_SYSTEM;
4647 case AUDIO_USAGE_VOICE_COMMUNICATION:
4648 return AUDIO_STREAM_VOICE_CALL;
4649
4650 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4651 return AUDIO_STREAM_DTMF;
4652
4653 case AUDIO_USAGE_ALARM:
4654 return AUDIO_STREAM_ALARM;
4655 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4656 return AUDIO_STREAM_RING;
4657
4658 case AUDIO_USAGE_NOTIFICATION:
4659 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4660 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4661 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4662 case AUDIO_USAGE_NOTIFICATION_EVENT:
4663 return AUDIO_STREAM_NOTIFICATION;
4664
4665 case AUDIO_USAGE_UNKNOWN:
4666 default:
4667 return AUDIO_STREAM_MUSIC;
4668 }
4669}
Eric Laurente83b55d2014-11-14 10:06:21 -08004670
François Gaffie53615e22015-03-19 09:24:12 +01004671bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4672{
Eric Laurente83b55d2014-11-14 10:06:21 -08004673 // has flags that map to a strategy?
4674 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4675 return true;
4676 }
4677
4678 // has known usage?
4679 switch (paa->usage) {
4680 case AUDIO_USAGE_UNKNOWN:
4681 case AUDIO_USAGE_MEDIA:
4682 case AUDIO_USAGE_VOICE_COMMUNICATION:
4683 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4684 case AUDIO_USAGE_ALARM:
4685 case AUDIO_USAGE_NOTIFICATION:
4686 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4687 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4688 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4689 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4690 case AUDIO_USAGE_NOTIFICATION_EVENT:
4691 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4692 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4693 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4694 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004695 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004696 break;
4697 default:
4698 return false;
4699 }
4700 return true;
4701}
4702
François Gaffiead3183e2015-03-18 16:55:35 +01004703bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4704 routing_strategy strategy, uint32_t inPastMs,
4705 nsecs_t sysTime) const
4706{
4707 if ((sysTime == 0) && (inPastMs != 0)) {
4708 sysTime = systemTime();
4709 }
4710 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4711 if (i == AUDIO_STREAM_PATCH) {
4712 continue;
4713 }
4714 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4715 (NUM_STRATEGIES == strategy)) &&
4716 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4717 return true;
4718 }
4719 }
4720 return false;
4721}
4722
François Gaffie2110e042015-03-24 08:41:51 +01004723audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4724{
4725 return mEngine->getForceUse(usage);
4726}
4727
4728bool AudioPolicyManager::isInCall()
4729{
4730 return isStateInCall(mEngine->getPhoneState());
4731}
4732
4733bool AudioPolicyManager::isStateInCall(int state)
4734{
4735 return is_state_in_call(state);
4736}
4737
Eric Laurente552edb2014-03-10 17:42:56 -07004738}; // namespace android