blob: 3f92011c4973e01c7b2cf25d0f054dab2eae0fb8 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080017#define LOG_TAG "APM::AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
Eric Laurentd4692962014-05-05 18:13:44 -070027#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070028#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070029
François Gaffie2110e042015-03-24 08:41:51 +010030#include <AudioPolicyManagerInterface.h>
31#include <AudioPolicyEngineInstance.h>
Eric Laurente552edb2014-03-10 17:42:56 -070032#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070033#include <utils/Log.h>
34#include <hardware/audio.h>
35#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070036#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080037#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070038#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070039#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070040#include "audio_policy_conf.h"
François Gaffie53615e22015-03-19 09:24:12 +010041#include <ConfigParsingUtils.h>
42#include <policy.h>
Eric Laurente552edb2014-03-10 17:42:56 -070043
Eric Laurent3b73df72014-03-11 09:06:29 -070044namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070045
46// ----------------------------------------------------------------------------
47// AudioPolicyInterface implementation
48// ----------------------------------------------------------------------------
49
Eric Laurente0720872014-03-11 09:30:41 -070050status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Paul McLeane743a472015-01-28 11:07:31 -080051 audio_policy_dev_state_t state,
52 const char *device_address,
53 const char *device_name)
Eric Laurente552edb2014-03-10 17:42:56 -070054{
Paul McLeane743a472015-01-28 11:07:31 -080055 return setDeviceConnectionStateInt(device, state, device_address, device_name);
Eric Laurentc73ca6e2014-12-12 14:34:22 -080056}
57
58status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device,
Eric Laurenta1d525f2015-01-29 13:36:45 -080059 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080060 const char *device_address,
61 const char *device_name)
Eric Laurentc73ca6e2014-12-12 14:34:22 -080062{
Paul McLeane743a472015-01-28 11:07:31 -080063 ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s",
64- device, state, device_address, device_name);
Eric Laurente552edb2014-03-10 17:42:56 -070065
66 // connect/disconnect only 1 device at a time
67 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
68
François Gaffie53615e22015-03-19 09:24:12 +010069 sp<DeviceDescriptor> devDesc =
70 mHwModules.getDeviceDescriptor(device, device_address, device_name);
Paul McLeane743a472015-01-28 11:07:31 -080071
Eric Laurente552edb2014-03-10 17:42:56 -070072 // handle output devices
73 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -070074 SortedVector <audio_io_handle_t> outputs;
75
Eric Laurent3a4311c2014-03-17 12:00:47 -070076 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
77
Eric Laurente552edb2014-03-10 17:42:56 -070078 // save a copy of the opened output descriptors before any output is opened or closed
79 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
80 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -070081 switch (state)
82 {
83 // handle output device connection
Eric Laurent3ae5f312015-02-03 17:12:08 -080084 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -070086 ALOGW("setDeviceConnectionState() device already connected: %x", device);
87 return INVALID_OPERATION;
88 }
89 ALOGV("setDeviceConnectionState() connecting device %x", device);
90
Eric Laurente552edb2014-03-10 17:42:56 -070091 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -070092 index = mAvailableOutputDevices.add(devDesc);
93 if (index >= 0) {
François Gaffie53615e22015-03-19 09:24:12 +010094 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -070095 if (module == 0) {
96 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
97 device);
98 mAvailableOutputDevices.remove(devDesc);
99 return INVALID_OPERATION;
100 }
Paul McLeane743a472015-01-28 11:07:31 -0800101 mAvailableOutputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 } else {
103 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700104 }
105
Eric Laurenta1d525f2015-01-29 13:36:45 -0800106 if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700107 mAvailableOutputDevices.remove(devDesc);
108 return INVALID_OPERATION;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 // Propagate device availability to Engine
111 mEngine->setDeviceConnectionState(devDesc, state);
112
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700113 // outputs should never be empty here
114 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
115 "checkOutputsForDevice() returned no outputs but status OK");
116 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
117 outputs.size());
Eric Laurent3ae5f312015-02-03 17:12:08 -0800118
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800119 // Send connect to HALs
Eric Laurent3ae5f312015-02-03 17:12:08 -0800120 AudioParameter param = AudioParameter(devDesc->mAddress);
121 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
122 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
123
124 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700125 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700126 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700127 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700128 ALOGW("setDeviceConnectionState() device not connected: %x", device);
129 return INVALID_OPERATION;
130 }
131
Paul McLean5c477aa2014-08-20 16:47:57 -0700132 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
133
Paul McLeane743a472015-01-28 11:07:31 -0800134 // Send Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800135 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700136 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
137 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
138
Eric Laurente552edb2014-03-10 17:42:56 -0700139 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700140 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
Eric Laurenta1d525f2015-01-29 13:36:45 -0800142 checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress);
François Gaffie2110e042015-03-24 08:41:51 +0100143
144 // Propagate device availability to Engine
145 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurente552edb2014-03-10 17:42:56 -0700146 } break;
147
148 default:
149 ALOGE("setDeviceConnectionState() invalid state: %x", state);
150 return BAD_VALUE;
151 }
152
Eric Laurent3a4311c2014-03-17 12:00:47 -0700153 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
154 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700155 checkA2dpSuspend();
156 checkOutputForAllStrategies();
157 // outputs must be closed after checkOutputForAllStrategies() is executed
158 if (!outputs.isEmpty()) {
159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700160 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700161 // close unused outputs after device disconnection or direct outputs that have been
162 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700163 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700164 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
165 (desc->mDirectOpenCount == 0))) {
166 closeOutput(outputs[i]);
167 }
168 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700169 // check again after closing A2DP output to reset mA2dpSuspended if needed
170 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700171 }
172
173 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700174 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700175 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
176 updateCallRouting(newDevice);
177 }
Eric Laurente552edb2014-03-10 17:42:56 -0700178 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
180 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) {
181 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700182 // do not force device change on duplicated output because if device is 0, it will
183 // also force a device 0 for the two outputs it is duplicated to which may override
184 // a valid device selection on those outputs.
Eric Laurentc75307b2015-03-17 15:29:32 -0700185 bool force = !desc->isDuplicated()
François Gaffie53615e22015-03-19 09:24:12 +0100186 && (!device_distinguishes_on_address(device)
Eric Laurentc2730ba2014-07-20 15:47:07 -0700187 // always force when disconnecting (a non-duplicated device)
188 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
Eric Laurentc75307b2015-03-17 15:29:32 -0700189 setOutputDevice(desc, newDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700190 }
Eric Laurente552edb2014-03-10 17:42:56 -0700191 }
192
Eric Laurent72aa32f2014-05-30 18:51:48 -0700193 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700194 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700195 } // end if is output device
196
Eric Laurente552edb2014-03-10 17:42:56 -0700197 // handle input devices
198 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700199 SortedVector <audio_io_handle_t> inputs;
200
Eric Laurent3a4311c2014-03-17 12:00:47 -0700201 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700202 switch (state)
203 {
204 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700205 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700206 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700207 ALOGW("setDeviceConnectionState() device already connected: %d", device);
208 return INVALID_OPERATION;
209 }
François Gaffie53615e22015-03-19 09:24:12 +0100210 sp<HwModule> module = mHwModules.getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 if (module == NULL) {
212 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
213 device);
214 return INVALID_OPERATION;
215 }
Eric Laurenta1d525f2015-01-29 13:36:45 -0800216 if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) {
Eric Laurentd4692962014-05-05 18:13:44 -0700217 return INVALID_OPERATION;
218 }
219
Eric Laurent3a4311c2014-03-17 12:00:47 -0700220 index = mAvailableInputDevices.add(devDesc);
221 if (index >= 0) {
Paul McLeane743a472015-01-28 11:07:31 -0800222 mAvailableInputDevices[index]->attach(module);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 } else {
224 return NO_MEMORY;
225 }
Eric Laurent3ae5f312015-02-03 17:12:08 -0800226
227 // Set connect to HALs
228 AudioParameter param = AudioParameter(devDesc->mAddress);
229 param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device);
230 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // Propagate device availability to Engine
233 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700234 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700235
236 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %d", device);
240 return INVALID_OPERATION;
241 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700242
243 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
244
245 // Set Disconnect to HALs
Eric Laurenta1d525f2015-01-29 13:36:45 -0800246 AudioParameter param = AudioParameter(devDesc->mAddress);
Paul McLean5c477aa2014-08-20 16:47:57 -0700247 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
248 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
249
Eric Laurenta1d525f2015-01-29 13:36:45 -0800250 checkInputsForDevice(device, state, inputs, devDesc->mAddress);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700251 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700252
François Gaffie2110e042015-03-24 08:41:51 +0100253 // Propagate device availability to Engine
254 mEngine->setDeviceConnectionState(devDesc, state);
Eric Laurentd4692962014-05-05 18:13:44 -0700255 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700256
257 default:
258 ALOGE("setDeviceConnectionState() invalid state: %x", state);
259 return BAD_VALUE;
260 }
261
Eric Laurentd4692962014-05-05 18:13:44 -0700262 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700263
Eric Laurent87ffa392015-05-22 10:32:38 -0700264 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700265 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
266 updateCallRouting(newDevice);
267 }
268
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700270 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700271 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700272
273 ALOGW("setDeviceConnectionState() invalid device: %x", device);
274 return BAD_VALUE;
275}
276
Eric Laurente0720872014-03-11 09:30:41 -0700277audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +0100278 const char *device_address)
Eric Laurente552edb2014-03-10 17:42:56 -0700279{
François Gaffie53615e22015-03-19 09:24:12 +0100280 sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, "");
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 DeviceVector *deviceVector;
283
Eric Laurente552edb2014-03-10 17:42:56 -0700284 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700285 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700286 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700287 deviceVector = &mAvailableInputDevices;
288 } else {
289 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
290 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
François Gaffie53615e22015-03-19 09:24:12 +0100292 return deviceVector->getDeviceConnectionState(devDesc);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800293}
294
Eric Laurentc2730ba2014-07-20 15:47:07 -0700295void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
296{
297 bool createTxPatch = false;
298 struct audio_patch patch;
299 patch.num_sources = 1;
300 patch.num_sinks = 1;
301 status_t status;
302 audio_patch_handle_t afPatchHandle;
303 DeviceVector deviceList;
304
Eric Laurent87ffa392015-05-22 10:32:38 -0700305 if(!hasPrimaryOutput()) {
306 return;
307 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800308 audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700309 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
310
311 // release existing RX patch if any
312 if (mCallRxPatch != 0) {
313 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
314 mCallRxPatch.clear();
315 }
316 // release TX patch if any
317 if (mCallTxPatch != 0) {
318 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
319 mCallTxPatch.clear();
320 }
321
322 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
323 // via setOutputDevice() on primary output.
324 // Otherwise, create two audio patches for TX and RX path.
325 if (availablePrimaryOutputDevices() & rxDevice) {
326 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
327 // If the TX device is also on the primary HW module, setOutputDevice() will take care
328 // of it due to legacy implementation. If not, create a patch.
329 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
330 == AUDIO_DEVICE_NONE) {
331 createTxPatch = true;
332 }
333 } else {
334 // create RX path audio patch
335 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
336 ALOG_ASSERT(!deviceList.isEmpty(),
337 "updateCallRouting() selected device not in output device list");
338 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
339 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
340 ALOG_ASSERT(!deviceList.isEmpty(),
341 "updateCallRouting() no telephony RX device");
342 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
343
344 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
345 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
346
347 // request to reuse existing output stream if one is already opened to reach the RX device
348 SortedVector<audio_io_handle_t> outputs =
349 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700350 audio_io_handle_t output = selectOutput(outputs,
351 AUDIO_OUTPUT_FLAG_NONE,
352 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700353 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700354 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700355 ALOG_ASSERT(!outputDesc->isDuplicated(),
356 "updateCallRouting() RX device output is duplicated");
357 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700358 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700359 patch.num_sources = 2;
360 }
361
362 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
363 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
364 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
365 status);
366 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100367 mCallRxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700368 mCallRxPatch->mAfPatchHandle = afPatchHandle;
369 mCallRxPatch->mUid = mUidCached;
370 }
371 createTxPatch = true;
372 }
373 if (createTxPatch) {
374
375 struct audio_patch patch;
376 patch.num_sources = 1;
377 patch.num_sinks = 1;
378 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
379 ALOG_ASSERT(!deviceList.isEmpty(),
380 "updateCallRouting() selected device not in input device list");
381 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
382 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
383 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
384 ALOG_ASSERT(!deviceList.isEmpty(),
385 "updateCallRouting() no telephony TX device");
386 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
387 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
388
389 SortedVector<audio_io_handle_t> outputs =
390 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700391 audio_io_handle_t output = selectOutput(outputs,
392 AUDIO_OUTPUT_FLAG_NONE,
393 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700394 // request to reuse existing output stream if one is already opened to reach the TX
395 // path output device
396 if (output != AUDIO_IO_HANDLE_NONE) {
397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
398 ALOG_ASSERT(!outputDesc->isDuplicated(),
399 "updateCallRouting() RX device output is duplicated");
400 outputDesc->toAudioPortConfig(&patch.sources[1]);
Eric Laurent3bcf8592015-04-03 12:13:24 -0700401 patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700402 patch.num_sources = 2;
403 }
404
405 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
406 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
407 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
408 status);
409 if (status == NO_ERROR) {
François Gaffie98cc1912015-03-18 17:52:40 +0100410 mCallTxPatch = new AudioPatch(&patch, mUidCached);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700411 mCallTxPatch->mAfPatchHandle = afPatchHandle;
412 mCallTxPatch->mUid = mUidCached;
413 }
414 }
415}
416
Eric Laurente0720872014-03-11 09:30:41 -0700417void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700418{
419 ALOGV("setPhoneState() state %d", state);
François Gaffie2110e042015-03-24 08:41:51 +0100420 // store previous phone state for management of sonification strategy below
421 int oldState = mEngine->getPhoneState();
422
423 if (mEngine->setPhoneState(state) != NO_ERROR) {
424 ALOGW("setPhoneState() invalid or same state %d", state);
Eric Laurente552edb2014-03-10 17:42:56 -0700425 return;
426 }
François Gaffie2110e042015-03-24 08:41:51 +0100427 /// Opens: can these line be executed after the switch of volume curves???
Eric Laurente552edb2014-03-10 17:42:56 -0700428 // if leaving call state, handle special case of active streams
429 // pertaining to sonification strategy see handleIncallSonification()
430 if (isInCall()) {
431 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700432 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (stream == AUDIO_STREAM_PATCH) {
434 continue;
435 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700436 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700437 }
Eric Laurent2cbe89a2014-12-19 11:49:08 -0800438
439 // force reevaluating accessibility routing when call starts
440 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 }
442
François Gaffie2110e042015-03-24 08:41:51 +0100443 /**
444 * Switching to or from incall state or switching between telephony and VoIP lead to force
445 * routing command.
446 */
447 bool force = ((is_state_in_call(oldState) != is_state_in_call(state))
448 || (is_state_in_call(state) && (state != oldState)));
Eric Laurente552edb2014-03-10 17:42:56 -0700449
450 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700451 checkA2dpSuspend();
452 checkOutputForAllStrategies();
453 updateDevicesAndOutputs();
454
Eric Laurente552edb2014-03-10 17:42:56 -0700455 int delayMs = 0;
456 if (isStateInCall(state)) {
457 nsecs_t sysTime = systemTime();
458 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700459 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700460 // mute media and sonification strategies and delay device switch by the largest
461 // latency of any output where either strategy is active.
462 // This avoid sending the ring tone or music tail into the earpiece or headset.
François Gaffiead3183e2015-03-18 16:55:35 +0100463 if ((isStrategyActive(desc, STRATEGY_MEDIA,
464 SONIFICATION_HEADSET_MUSIC_DELAY,
465 sysTime) ||
466 isStrategyActive(desc, STRATEGY_SONIFICATION,
467 SONIFICATION_HEADSET_MUSIC_DELAY,
468 sysTime)) &&
Eric Laurentc75307b2015-03-17 15:29:32 -0700469 (delayMs < (int)desc->latency()*2)) {
470 delayMs = desc->latency()*2;
Eric Laurente552edb2014-03-10 17:42:56 -0700471 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700472 setStrategyMute(STRATEGY_MEDIA, true, desc);
473 setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700474 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
Eric Laurentc75307b2015-03-17 15:29:32 -0700475 setStrategyMute(STRATEGY_SONIFICATION, true, desc);
476 setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS,
Eric Laurente552edb2014-03-10 17:42:56 -0700477 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
478 }
479 }
480
Eric Laurent87ffa392015-05-22 10:32:38 -0700481 if (hasPrimaryOutput()) {
482 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
483 // the device returned is not necessarily reachable via this output
484 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
485 // force routing command to audio hardware when ending call
486 // even if no device change is needed
487 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
488 rxDevice = mPrimaryOutput->device();
489 }
Eric Laurente552edb2014-03-10 17:42:56 -0700490
Eric Laurent87ffa392015-05-22 10:32:38 -0700491 if (state == AUDIO_MODE_IN_CALL) {
492 updateCallRouting(rxDevice, delayMs);
493 } else if (oldState == AUDIO_MODE_IN_CALL) {
494 if (mCallRxPatch != 0) {
495 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
496 mCallRxPatch.clear();
497 }
498 if (mCallTxPatch != 0) {
499 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
500 mCallTxPatch.clear();
501 }
502 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
503 } else {
504 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 }
Eric Laurentc2730ba2014-07-20 15:47:07 -0700506 }
Eric Laurente552edb2014-03-10 17:42:56 -0700507 // if entering in call state, handle special case of active streams
508 // pertaining to sonification strategy see handleIncallSonification()
509 if (isStateInCall(state)) {
510 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700511 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800512 if (stream == AUDIO_STREAM_PATCH) {
513 continue;
514 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700515 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700516 }
517 }
518
519 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 if (state == AUDIO_MODE_RINGTONE &&
521 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700522 mLimitRingtoneVolume = true;
523 } else {
524 mLimitRingtoneVolume = false;
525 }
526}
527
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700528audio_mode_t AudioPolicyManager::getPhoneState() {
529 return mEngine->getPhoneState();
530}
531
Eric Laurente0720872014-03-11 09:30:41 -0700532void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700533 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700534{
François Gaffie2110e042015-03-24 08:41:51 +0100535 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -0700536
François Gaffie2110e042015-03-24 08:41:51 +0100537 if (mEngine->setForceUse(usage, config) != NO_ERROR) {
538 ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage);
539 return;
Eric Laurente552edb2014-03-10 17:42:56 -0700540 }
François Gaffie2110e042015-03-24 08:41:51 +0100541 bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) ||
542 (usage == AUDIO_POLICY_FORCE_FOR_DOCK) ||
543 (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM);
Eric Laurente552edb2014-03-10 17:42:56 -0700544
545 // check for device and output changes triggered by new force usage
546 checkA2dpSuspend();
547 checkOutputForAllStrategies();
548 updateDevicesAndOutputs();
Eric Laurent87ffa392015-05-22 10:32:38 -0700549 if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700550 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
551 updateCallRouting(newDevice);
552 }
Eric Laurente552edb2014-03-10 17:42:56 -0700553 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700554 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
555 audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
556 if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
557 setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
Eric Laurentc2730ba2014-07-20 15:47:07 -0700558 }
Eric Laurente552edb2014-03-10 17:42:56 -0700559 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700560 applyStreamVolumes(outputDesc, newDevice, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700561 }
562 }
563
François Gaffie53615e22015-03-19 09:24:12 +0100564 audio_io_handle_t activeInput = mInputs.getActiveInput();
Eric Laurente552edb2014-03-10 17:42:56 -0700565 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700566 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700567 }
568
569}
570
Eric Laurente0720872014-03-11 09:30:41 -0700571void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700572{
573 ALOGV("setSystemProperty() property %s, value %s", property, value);
574}
575
576// Find a direct output profile compatible with the parameters passed, even if the input flags do
577// not explicitly request a direct output
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800578sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700579 audio_devices_t device,
580 uint32_t samplingRate,
581 audio_format_t format,
582 audio_channel_mask_t channelMask,
583 audio_output_flags_t flags)
584{
585 for (size_t i = 0; i < mHwModules.size(); i++) {
586 if (mHwModules[i]->mHandle == 0) {
587 continue;
588 }
589 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700590 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Andy Hungf129b032015-04-07 13:45:50 -0700591 bool found = profile->isCompatibleProfile(device, String8(""),
592 samplingRate, NULL /*updatedSamplingRate*/,
593 format, NULL /*updatedFormat*/,
594 channelMask, NULL /*updatedChannelMask*/,
Glenn Kastencbd48022014-07-24 13:46:44 -0700595 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
596 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700597 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
598 return profile;
599 }
Eric Laurente552edb2014-03-10 17:42:56 -0700600 }
601 }
602 return 0;
603}
604
Eric Laurente0720872014-03-11 09:30:41 -0700605audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +0100606 uint32_t samplingRate,
607 audio_format_t format,
608 audio_channel_mask_t channelMask,
609 audio_output_flags_t flags,
610 const audio_offload_info_t *offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -0700611{
Eric Laurent3b73df72014-03-11 09:06:29 -0700612 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700613 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
614 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
615 device, stream, samplingRate, format, channelMask, flags);
616
Eric Laurente83b55d2014-11-14 10:06:21 -0800617 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
618 stream, samplingRate,format, channelMask,
619 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700620}
621
Eric Laurente83b55d2014-11-14 10:06:21 -0800622status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
623 audio_io_handle_t *output,
624 audio_session_t session,
625 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700626 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800627 uint32_t samplingRate,
628 audio_format_t format,
629 audio_channel_mask_t channelMask,
630 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700631 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800632 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700633{
Eric Laurente83b55d2014-11-14 10:06:21 -0800634 audio_attributes_t attributes;
635 if (attr != NULL) {
636 if (!isValidAttributes(attr)) {
637 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
638 attr->usage, attr->content_type, attr->flags,
639 attr->tags);
640 return BAD_VALUE;
641 }
642 attributes = *attr;
643 } else {
644 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
645 ALOGE("getOutputForAttr(): invalid stream type");
646 return BAD_VALUE;
647 }
648 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700649 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700650 sp<SwAudioOutputDescriptor> desc;
François Gaffie036e1e92015-03-19 10:16:24 +0100651 if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) {
652 ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr");
653 if (!audio_is_linear_pcm(format)) {
654 return BAD_VALUE;
Eric Laurent275e8e92014-11-30 15:14:47 -0800655 }
François Gaffie036e1e92015-03-19 10:16:24 +0100656 *stream = streamTypefromAttributesInt(&attributes);
657 *output = desc->mIoHandle;
658 ALOGV("getOutputForAttr() returns output %d", *output);
659 return NO_ERROR;
Eric Laurent275e8e92014-11-30 15:14:47 -0800660 }
661 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
662 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
663 return BAD_VALUE;
664 }
665
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700666 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x"
667 " session %d selectedDeviceId %d",
668 attributes.usage, attributes.content_type, attributes.tags, attributes.flags,
669 session, selectedDeviceId);
670
671 *stream = streamTypefromAttributesInt(&attributes);
672
673 // Explicit routing?
674 sp<DeviceDescriptor> deviceDesc;
675 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
676 if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) {
677 deviceDesc = mAvailableOutputDevices[i];
678 break;
679 }
680 }
681 mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700682
Eric Laurente83b55d2014-11-14 10:06:21 -0800683 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700684 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700685
Eric Laurente83b55d2014-11-14 10:06:21 -0800686 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700687 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
688 }
689
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700690 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700691 device, samplingRate, format, channelMask, flags);
692
Eric Laurente83b55d2014-11-14 10:06:21 -0800693 *output = getOutputForDevice(device, session, *stream,
694 samplingRate, format, channelMask,
695 flags, offloadInfo);
696 if (*output == AUDIO_IO_HANDLE_NONE) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700697 mOutputRoutes.removeRoute(session);
Eric Laurente83b55d2014-11-14 10:06:21 -0800698 return INVALID_OPERATION;
699 }
Paul McLeanaa981192015-03-21 09:55:15 -0700700
Eric Laurente83b55d2014-11-14 10:06:21 -0800701 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700702}
703
704audio_io_handle_t AudioPolicyManager::getOutputForDevice(
705 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800706 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700707 audio_stream_type_t stream,
708 uint32_t samplingRate,
709 audio_format_t format,
710 audio_channel_mask_t channelMask,
711 audio_output_flags_t flags,
712 const audio_offload_info_t *offloadInfo)
713{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700714 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700715 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700716 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700717
Eric Laurente552edb2014-03-10 17:42:56 -0700718#ifdef AUDIO_POLICY_TEST
719 if (mCurOutput != 0) {
720 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
721 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
722
723 if (mTestOutputs[mCurOutput] == 0) {
724 ALOGV("getOutput() opening test output");
Eric Laurentc75307b2015-03-17 15:29:32 -0700725 sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL,
726 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700727 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700728 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700729 outputDesc->mFlags =
730 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700731 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700732 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
733 config.sample_rate = mTestSamplingRate;
734 config.channel_mask = mTestChannels;
735 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700736 if (offloadInfo != NULL) {
737 config.offload_info = *offloadInfo;
738 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700739 status = mpClientInterface->openOutput(0,
740 &mTestOutputs[mCurOutput],
741 &config,
742 &outputDesc->mDevice,
743 String8(""),
744 &outputDesc->mLatency,
745 outputDesc->mFlags);
746 if (status == NO_ERROR) {
747 outputDesc->mSamplingRate = config.sample_rate;
748 outputDesc->mFormat = config.format;
749 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700750 AudioParameter outputCmd = AudioParameter();
751 outputCmd.addInt(String8("set_id"),mCurOutput);
752 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
753 addOutput(mTestOutputs[mCurOutput], outputDesc);
754 }
755 }
756 return mTestOutputs[mCurOutput];
757 }
758#endif //AUDIO_POLICY_TEST
759
760 // open a direct output if required by specified parameters
761 //force direct flag if offload flag is set: offloading implies a direct output stream
762 // and all common behaviors are driven by checking only the direct flag
763 // this should normally be set appropriately in the policy configuration file
764 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700765 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700766 }
Eric Laurent93c3d412014-08-01 14:48:35 -0700767 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
768 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
769 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800770 // only allow deep buffering for music stream type
771 if (stream != AUDIO_STREAM_MUSIC) {
772 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
773 }
Ravi Kumar Alamandac36a8892015-04-24 16:35:49 -0700774 if (stream == AUDIO_STREAM_TTS) {
775 flags = AUDIO_OUTPUT_FLAG_TTS;
776 }
Eric Laurente552edb2014-03-10 17:42:56 -0700777
Eric Laurentb732cf52014-09-24 19:08:21 -0700778 sp<IOProfile> profile;
779
780 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -0700781 // and not explicitly requested
782 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
783 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -0700784 audio_channel_count_from_out_mask(channelMask) <= 2) {
785 goto non_direct_output;
786 }
787
Eric Laurente552edb2014-03-10 17:42:56 -0700788 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
789 // creating an offloaded track and tearing it down immediately after start when audioflinger
790 // detects there is an active non offloadable effect.
791 // FIXME: We should check the audio session here but we do not have it in this context.
792 // This may prevent offloading in rare situations where effects are left active by apps
793 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -0700794
Eric Laurente552edb2014-03-10 17:42:56 -0700795 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
François Gaffie45ed3b02015-03-19 10:35:14 +0100796 !mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -0700797 profile = getProfileForDirectOutput(device,
798 samplingRate,
799 format,
800 channelMask,
801 (audio_output_flags_t)flags);
802 }
803
Eric Laurent1c333e22014-05-20 10:48:17 -0700804 if (profile != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700805 sp<SwAudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700806
807 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700808 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700809 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
810 outputDesc = desc;
811 // reuse direct output if currently open and configured with same parameters
812 if ((samplingRate == outputDesc->mSamplingRate) &&
813 (format == outputDesc->mFormat) &&
814 (channelMask == outputDesc->mChannelMask)) {
815 outputDesc->mDirectOpenCount++;
816 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
817 return mOutputs.keyAt(i);
818 }
819 }
820 }
821 // close direct output if currently open and configured with different parameters
822 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700823 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700824 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700825 outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -0700826 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -0700827 outputDesc->mLatency = 0;
828 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700829 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
830 config.sample_rate = samplingRate;
831 config.channel_mask = channelMask;
832 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -0700833 if (offloadInfo != NULL) {
834 config.offload_info = *offloadInfo;
835 }
Eric Laurent322b4d22015-04-03 15:57:54 -0700836 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -0700837 &output,
838 &config,
839 &outputDesc->mDevice,
840 String8(""),
841 &outputDesc->mLatency,
842 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -0700843
844 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -0700845 if (status != NO_ERROR ||
846 (samplingRate != 0 && samplingRate != config.sample_rate) ||
847 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
848 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700849 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
850 "format %d %d, channelMask %04x %04x", output, samplingRate,
851 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
852 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700853 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700854 mpClientInterface->closeOutput(output);
855 }
Eric Laurenta82797f2015-01-30 11:49:43 -0800856 // fall back to mixer output if possible when the direct output could not be open
857 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
858 goto non_direct_output;
859 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800860 // fall back to mixer output if possible when the direct output could not be open
861 if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) {
862 goto non_direct_output;
863 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700864 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -0700865 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700866 outputDesc->mSamplingRate = config.sample_rate;
867 outputDesc->mChannelMask = config.channel_mask;
868 outputDesc->mFormat = config.format;
869 outputDesc->mRefCount[stream] = 0;
870 outputDesc->mStopTime[stream] = 0;
871 outputDesc->mDirectOpenCount = 1;
872
Eric Laurente552edb2014-03-10 17:42:56 -0700873 audio_io_handle_t srcOutput = getOutputForEffect();
874 addOutput(output, outputDesc);
875 audio_io_handle_t dstOutput = getOutputForEffect();
876 if (dstOutput == output) {
877 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
878 }
879 mPreviousOutputs = mOutputs;
880 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700881 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700882 return output;
883 }
884
Eric Laurentb732cf52014-09-24 19:08:21 -0700885non_direct_output:
Eric Laurente552edb2014-03-10 17:42:56 -0700886 // ignoring channel mask due to downmix capability in mixer
887
888 // open a non direct output
889
890 // for non direct outputs, only PCM is supported
891 if (audio_is_linear_pcm(format)) {
892 // get which output is suitable for the specified stream. The actual
893 // routing change will happen when startOutput() will be called
894 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
895
Eric Laurent8838a382014-09-08 16:44:28 -0700896 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
897 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
898 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -0700899 }
900 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
901 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
902
Paul McLeanaa981192015-03-21 09:55:15 -0700903 ALOGV(" getOutputForDevice() returns output %d", output);
Eric Laurente552edb2014-03-10 17:42:56 -0700904
905 return output;
906}
907
Eric Laurente0720872014-03-11 09:30:41 -0700908audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700909 audio_output_flags_t flags,
910 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -0700911{
912 // select one output among several that provide a path to a particular device or set of
913 // devices (the list was previously build by getOutputsForDevice()).
914 // The priority is as follows:
915 // 1: the output with the highest number of requested policy flags
916 // 2: the primary output
917 // 3: the first output in the list
918
919 if (outputs.size() == 0) {
920 return 0;
921 }
922 if (outputs.size() == 1) {
923 return outputs[0];
924 }
925
926 int maxCommonFlags = 0;
927 audio_io_handle_t outputFlags = 0;
928 audio_io_handle_t outputPrimary = 0;
929
930 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -0700931 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700932 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -0700933 // if a valid format is specified, skip output if not compatible
934 if (format != AUDIO_FORMAT_INVALID) {
935 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
936 if (format != outputDesc->mFormat) {
937 continue;
938 }
939 } else if (!audio_is_linear_pcm(format)) {
940 continue;
941 }
942 }
943
Eric Laurent3b73df72014-03-11 09:06:29 -0700944 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700945 if (commonFlags > maxCommonFlags) {
946 outputFlags = outputs[i];
947 maxCommonFlags = commonFlags;
948 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
949 }
950 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
951 outputPrimary = outputs[i];
952 }
953 }
954 }
955
956 if (outputFlags != 0) {
957 return outputFlags;
958 }
959 if (outputPrimary != 0) {
960 return outputPrimary;
961 }
962
963 return outputs[0];
964}
965
Eric Laurente0720872014-03-11 09:30:41 -0700966status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700967 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800968 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -0700969{
Paul McLeanaa981192015-03-21 09:55:15 -0700970 ALOGV("startOutput() output %d, stream %d, session %d",
971 output, stream, session);
Eric Laurente552edb2014-03-10 17:42:56 -0700972 ssize_t index = mOutputs.indexOfKey(output);
973 if (index < 0) {
974 ALOGW("startOutput() unknown output %d", output);
975 return BAD_VALUE;
976 }
977
Eric Laurentc75307b2015-03-17 15:29:32 -0700978 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
979
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700980 // Routing?
981 mOutputRoutes.incRouteActivity(session);
982
Eric Laurentc75307b2015-03-17 15:29:32 -0700983 audio_devices_t newDevice;
984 if (outputDesc->mPolicyMix != NULL) {
985 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurent493404d2015-04-21 15:07:36 -0700986 } else if (mOutputRoutes.hasRouteChanged(session)) {
987 newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700988 checkStrategyRoute(getStrategy(stream), output);
Eric Laurentc75307b2015-03-17 15:29:32 -0700989 } else {
990 newDevice = AUDIO_DEVICE_NONE;
991 }
992
993 uint32_t delayMs = 0;
994
Eric Laurentc75307b2015-03-17 15:29:32 -0700995 status_t status = startSource(outputDesc, stream, newDevice, &delayMs);
996
997 if (status != NO_ERROR) {
998 mOutputRoutes.decRouteActivity(session);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700999 return status;
Eric Laurentc75307b2015-03-17 15:29:32 -07001000 }
1001 // Automatically enable the remote submix input when output is started on a re routing mix
1002 // of type MIX_TYPE_RECORDERS
1003 if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL &&
1004 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1005 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1006 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1007 outputDesc->mPolicyMix->mRegistrationId,
1008 "remote-submix");
1009 }
1010
1011 if (delayMs != 0) {
1012 usleep(delayMs * 1000);
1013 }
1014
1015 return status;
1016}
1017
1018status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc,
1019 audio_stream_type_t stream,
1020 audio_devices_t device,
1021 uint32_t *delayMs)
1022{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001023 // cannot start playback of STREAM_TTS if any other output is being used
1024 uint32_t beaconMuteLatency = 0;
Eric Laurentc75307b2015-03-17 15:29:32 -07001025
1026 *delayMs = 0;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001027 if (stream == AUDIO_STREAM_TTS) {
1028 ALOGV("\t found BEACON stream");
François Gaffie53615e22015-03-19 09:24:12 +01001029 if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001030 return INVALID_OPERATION;
1031 } else {
1032 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1033 }
1034 } else {
1035 // some playback other than beacon starts
1036 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1037 }
1038
Eric Laurente552edb2014-03-10 17:42:56 -07001039 // increment usage count for this stream on the requested output:
1040 // NOTE that the usage count is the same for duplicated output and hardware output which is
1041 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1042 outputDesc->changeRefCount(stream, 1);
1043
Eric Laurent493404d2015-04-21 15:07:36 -07001044 if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001045 // starting an output being rerouted?
Eric Laurentc75307b2015-03-17 15:29:32 -07001046 if (device == AUDIO_DEVICE_NONE) {
1047 device = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurent275e8e92014-11-30 15:14:47 -08001048 }
Eric Laurente552edb2014-03-10 17:42:56 -07001049 routing_strategy strategy = getStrategy(stream);
1050 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001051 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1052 (beaconMuteLatency > 0);
1053 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001054 bool force = false;
1055 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001056 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001057 if (desc != outputDesc) {
1058 // force a device change if any other output is managed by the same hw
1059 // module and has a current device selection that differs from selected device.
1060 // In this case, the audio HAL must receive the new device selection so that it can
1061 // change the device currently selected by the other active output.
1062 if (outputDesc->sharesHwModuleWith(desc) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07001063 desc->device() != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07001064 force = true;
1065 }
1066 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001067 // a notification so that audio focus effect can propagate, or that a mute/unmute
1068 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001069 uint32_t latency = desc->latency();
1070 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1071 waitMs = latency;
1072 }
1073 }
1074 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001075 uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force);
Eric Laurente552edb2014-03-10 17:42:56 -07001076
1077 // handle special case for sonification while in call
1078 if (isInCall()) {
1079 handleIncallSonification(stream, true, false);
1080 }
1081
1082 // apply volume rules for current stream and device if necessary
1083 checkAndSetVolume(stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07001084 mStreams.valueFor(stream).getVolumeIndex(device),
1085 outputDesc,
1086 device);
Eric Laurente552edb2014-03-10 17:42:56 -07001087
1088 // update the outputs if starting an output with a stream that can affect notification
1089 // routing
1090 handleNotificationRoutingForStream(stream);
Eric Laurentc722f302014-12-10 11:21:49 -08001091
Eric Laurent2cbe89a2014-12-19 11:49:08 -08001092 // force reevaluating accessibility routing when ringtone or alarm starts
1093 if (strategy == STRATEGY_SONIFICATION) {
1094 mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
1095 }
Eric Laurente552edb2014-03-10 17:42:56 -07001096 }
1097 return NO_ERROR;
1098}
1099
1100
Eric Laurente0720872014-03-11 09:30:41 -07001101status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001102 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001103 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001104{
1105 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1106 ssize_t index = mOutputs.indexOfKey(output);
1107 if (index < 0) {
1108 ALOGW("stopOutput() unknown output %d", output);
1109 return BAD_VALUE;
1110 }
1111
Eric Laurentc75307b2015-03-17 15:29:32 -07001112 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001113
Eric Laurentc75307b2015-03-17 15:29:32 -07001114 if (outputDesc->mRefCount[stream] == 1) {
1115 // Automatically disable the remote submix input when output is stopped on a
1116 // re routing mix of type MIX_TYPE_RECORDERS
1117 if (audio_is_remote_submix_device(outputDesc->mDevice) &&
1118 outputDesc->mPolicyMix != NULL &&
1119 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) {
1120 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
1121 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1122 outputDesc->mPolicyMix->mRegistrationId,
1123 "remote-submix");
1124 }
1125 }
1126
1127 // Routing?
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001128 bool forceDeviceUpdate = false;
Eric Laurentc75307b2015-03-17 15:29:32 -07001129 if (outputDesc->mRefCount[stream] > 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001130 int activityCount = mOutputRoutes.decRouteActivity(session);
1131 forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0));
1132
1133 if (forceDeviceUpdate) {
1134 checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE);
1135 }
Eric Laurentc75307b2015-03-17 15:29:32 -07001136 }
1137
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001138 return stopSource(outputDesc, stream, forceDeviceUpdate);
Eric Laurentc75307b2015-03-17 15:29:32 -07001139}
1140
1141status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001142 audio_stream_type_t stream,
1143 bool forceDeviceUpdate)
Eric Laurentc75307b2015-03-17 15:29:32 -07001144{
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001145 // always handle stream stop, check which stream type is stopping
1146 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1147
Eric Laurente552edb2014-03-10 17:42:56 -07001148 // handle special case for sonification while in call
1149 if (isInCall()) {
1150 handleIncallSonification(stream, false, false);
1151 }
1152
1153 if (outputDesc->mRefCount[stream] > 0) {
1154 // decrement usage count of this stream on the output
1155 outputDesc->changeRefCount(stream, -1);
Paul McLeanaa981192015-03-21 09:55:15 -07001156
Eric Laurente552edb2014-03-10 17:42:56 -07001157 // store time at which the stream was stopped - see isStreamActive()
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001158 if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) {
Eric Laurente552edb2014-03-10 17:42:56 -07001159 outputDesc->mStopTime[stream] = systemTime();
Eric Laurentc75307b2015-03-17 15:29:32 -07001160 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001161 // delay the device switch by twice the latency because stopOutput() is executed when
1162 // the track stop() command is received and at that time the audio track buffer can
1163 // still contain data that needs to be drained. The latency only covers the audio HAL
1164 // and kernel buffers. Also the latency does not always include additional delay in the
1165 // audio path (audio DSP, CODEC ...)
Eric Laurentc75307b2015-03-17 15:29:32 -07001166 setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001167
1168 // force restoring the device selection on other active outputs if it differs from the
1169 // one being selected for this output
1170 for (size_t i = 0; i < mOutputs.size(); i++) {
1171 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001172 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07001173 if (desc != outputDesc &&
Eric Laurente552edb2014-03-10 17:42:56 -07001174 desc->isActive() &&
1175 outputDesc->sharesHwModuleWith(desc) &&
1176 (newDevice != desc->device())) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001177 setOutputDevice(desc,
1178 getNewOutputDevice(desc, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001179 true,
Eric Laurentc75307b2015-03-17 15:29:32 -07001180 outputDesc->latency()*2);
Eric Laurente552edb2014-03-10 17:42:56 -07001181 }
1182 }
1183 // update the outputs if stopping one with a stream that can affect notification routing
1184 handleNotificationRoutingForStream(stream);
1185 }
1186 return NO_ERROR;
1187 } else {
Eric Laurentc75307b2015-03-17 15:29:32 -07001188 ALOGW("stopOutput() refcount is already 0");
Eric Laurente552edb2014-03-10 17:42:56 -07001189 return INVALID_OPERATION;
1190 }
1191}
1192
Eric Laurente83b55d2014-11-14 10:06:21 -08001193void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001194 audio_stream_type_t stream __unused,
1195 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001196{
1197 ALOGV("releaseOutput() %d", output);
1198 ssize_t index = mOutputs.indexOfKey(output);
1199 if (index < 0) {
1200 ALOGW("releaseOutput() releasing unknown output %d", output);
1201 return;
1202 }
1203
1204#ifdef AUDIO_POLICY_TEST
1205 int testIndex = testOutputIndex(output);
1206 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001207 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001208 if (outputDesc->isActive()) {
1209 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01001210 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001211 mTestOutputs[testIndex] = 0;
1212 }
1213 return;
1214 }
1215#endif //AUDIO_POLICY_TEST
1216
Paul McLeanaa981192015-03-21 09:55:15 -07001217 // Routing
1218 mOutputRoutes.removeRoute(session);
1219
Eric Laurentc75307b2015-03-17 15:29:32 -07001220 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001221 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001222 if (desc->mDirectOpenCount <= 0) {
1223 ALOGW("releaseOutput() invalid open count %d for output %d",
1224 desc->mDirectOpenCount, output);
1225 return;
1226 }
1227 if (--desc->mDirectOpenCount == 0) {
1228 closeOutput(output);
1229 // If effects where present on the output, audioflinger moved them to the primary
1230 // output by default: move them back to the appropriate output.
1231 audio_io_handle_t dstOutput = getOutputForEffect();
Eric Laurent87ffa392015-05-22 10:32:38 -07001232 if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001233 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX,
1234 mPrimaryOutput->mIoHandle, dstOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07001235 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001236 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001237 }
1238 }
1239}
1240
1241
Eric Laurentcaf7f482014-11-25 17:50:47 -08001242status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1243 audio_io_handle_t *input,
1244 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001245 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001246 uint32_t samplingRate,
1247 audio_format_t format,
1248 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001249 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -06001250 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001251 input_type_t *inputType)
Eric Laurente552edb2014-03-10 17:42:56 -07001252{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001253 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1254 "session %d, flags %#x",
1255 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001256
Eric Laurentcaf7f482014-11-25 17:50:47 -08001257 *input = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001258 *inputType = API_INPUT_INVALID;
Eric Laurent275e8e92014-11-30 15:14:47 -08001259 audio_devices_t device;
1260 // handle legacy remote submix case where the address was not always specified
1261 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001262 bool isSoundTrigger = false;
Eric Laurentc447ded2015-01-06 08:47:05 -08001263 audio_source_t inputSource = attr->source;
1264 audio_source_t halInputSource;
Eric Laurentc722f302014-12-10 11:21:49 -08001265 AudioMix *policyMix = NULL;
Eric Laurent275e8e92014-11-30 15:14:47 -08001266
Eric Laurentc447ded2015-01-06 08:47:05 -08001267 if (inputSource == AUDIO_SOURCE_DEFAULT) {
1268 inputSource = AUDIO_SOURCE_MIC;
1269 }
1270 halInputSource = inputSource;
1271
Paul McLean466dc8e2015-04-17 13:15:36 -06001272 // Explicit routing?
1273 sp<DeviceDescriptor> deviceDesc;
1274 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1275 if (mAvailableInputDevices[i]->getId() == selectedDeviceId) {
1276 deviceDesc = mAvailableInputDevices[i];
1277 break;
1278 }
1279 }
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001280 mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06001281
Eric Laurentc447ded2015-01-06 08:47:05 -08001282 if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX &&
Eric Laurent275e8e92014-11-30 15:14:47 -08001283 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001284 status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix);
François Gaffie036e1e92015-03-19 10:16:24 +01001285 if (ret != NO_ERROR) {
1286 return ret;
1287 }
1288 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001289 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1290 address = String8(attr->tags + strlen("addr="));
Eric Laurent275e8e92014-11-30 15:14:47 -08001291 } else {
Eric Laurentc447ded2015-01-06 08:47:05 -08001292 device = getDeviceAndMixForInputSource(inputSource, &policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08001293 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc447ded2015-01-06 08:47:05 -08001294 ALOGW("getInputForAttr() could not find device for source %d", inputSource);
Eric Laurent275e8e92014-11-30 15:14:47 -08001295 return BAD_VALUE;
1296 }
Eric Laurentc722f302014-12-10 11:21:49 -08001297 if (policyMix != NULL) {
1298 address = policyMix->mRegistrationId;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001299 if (policyMix->mMixType == MIX_TYPE_RECORDERS) {
1300 // there is an external policy, but this input is attached to a mix of recorders,
1301 // meaning it receives audio injected into the framework, so the recorder doesn't
1302 // know about it and is therefore considered "legacy"
1303 *inputType = API_INPUT_LEGACY;
1304 } else {
1305 // recording a mix of players defined by an external policy, we're rerouting for
1306 // an external policy
1307 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE;
1308 }
Eric Laurentc722f302014-12-10 11:21:49 -08001309 } else if (audio_is_remote_submix_device(device)) {
1310 address = String8("0");
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001311 *inputType = API_INPUT_MIX_CAPTURE;
1312 } else {
1313 *inputType = API_INPUT_LEGACY;
Eric Laurentc722f302014-12-10 11:21:49 -08001314 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001315 // adapt channel selection to input source
Eric Laurentc447ded2015-01-06 08:47:05 -08001316 switch (inputSource) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001317 case AUDIO_SOURCE_VOICE_UPLINK:
1318 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1319 break;
1320 case AUDIO_SOURCE_VOICE_DOWNLINK:
1321 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1322 break;
1323 case AUDIO_SOURCE_VOICE_CALL:
1324 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1325 break;
1326 default:
1327 break;
1328 }
Eric Laurentc447ded2015-01-06 08:47:05 -08001329 if (inputSource == AUDIO_SOURCE_HOTWORD) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001330 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1331 if (index >= 0) {
1332 *input = mSoundTriggerSessions.valueFor(session);
1333 isSoundTrigger = true;
1334 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1335 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1336 } else {
1337 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1338 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001339 }
1340 }
1341
Andy Hungf129b032015-04-07 13:45:50 -07001342 // find a compatible input profile (not necessarily identical in parameters)
1343 sp<IOProfile> profile;
1344 // samplingRate and flags may be updated by getInputProfile
1345 uint32_t profileSamplingRate = samplingRate;
1346 audio_format_t profileFormat = format;
1347 audio_channel_mask_t profileChannelMask = channelMask;
1348 audio_input_flags_t profileFlags = flags;
1349 for (;;) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001350 profile = getInputProfile(device, address,
Andy Hungf129b032015-04-07 13:45:50 -07001351 profileSamplingRate, profileFormat, profileChannelMask,
1352 profileFlags);
1353 if (profile != 0) {
1354 break; // success
1355 } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
1356 profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
1357 } else { // fail
Eric Laurentcaf7f482014-11-25 17:50:47 -08001358 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1359 "format %#x, channelMask 0x%X, flags %#x",
Andy Hungf129b032015-04-07 13:45:50 -07001360 device, samplingRate, format, channelMask, flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001361 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001362 }
Eric Laurente552edb2014-03-10 17:42:56 -07001363 }
1364
Eric Laurent322b4d22015-04-03 15:57:54 -07001365 if (profile->getModuleHandle() == 0) {
1366 ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName());
Eric Laurentcaf7f482014-11-25 17:50:47 -08001367 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001368 }
1369
1370 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Andy Hungf129b032015-04-07 13:45:50 -07001371 config.sample_rate = profileSamplingRate;
1372 config.channel_mask = profileChannelMask;
1373 config.format = profileFormat;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001374
Eric Laurent322b4d22015-04-03 15:57:54 -07001375 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcaf7f482014-11-25 17:50:47 -08001376 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001377 &config,
1378 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001379 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001380 halInputSource,
Andy Hungf129b032015-04-07 13:45:50 -07001381 profileFlags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001382
1383 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001384 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Andy Hungf129b032015-04-07 13:45:50 -07001385 (profileSamplingRate != config.sample_rate) ||
1386 (profileFormat != config.format) ||
1387 (profileChannelMask != config.channel_mask)) {
1388 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d,"
1389 " channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001390 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001391 if (*input != AUDIO_IO_HANDLE_NONE) {
1392 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001393 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001394 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001395 }
1396
Eric Laurent1f2f2232014-06-02 12:01:23 -07001397 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentc447ded2015-01-06 08:47:05 -08001398 inputDesc->mInputSource = inputSource;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001399 inputDesc->mRefCount = 0;
1400 inputDesc->mOpenRefCount = 1;
Andy Hungf129b032015-04-07 13:45:50 -07001401 inputDesc->mSamplingRate = profileSamplingRate;
1402 inputDesc->mFormat = profileFormat;
1403 inputDesc->mChannelMask = profileChannelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001404 inputDesc->mDevice = device;
Eric Laurentc722f302014-12-10 11:21:49 -08001405 inputDesc->mSessions.add(session);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001406 inputDesc->mIsSoundTrigger = isSoundTrigger;
Eric Laurentc722f302014-12-10 11:21:49 -08001407 inputDesc->mPolicyMix = policyMix;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001408
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07001409 ALOGV("getInputForAttr() returns input type = %d", *inputType);
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -08001410
Eric Laurentcaf7f482014-11-25 17:50:47 -08001411 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001412 mpClientInterface->onAudioPortListUpdate();
Paul McLean466dc8e2015-04-17 13:15:36 -06001413
Eric Laurentcaf7f482014-11-25 17:50:47 -08001414 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001415}
1416
Eric Laurent4dc68062014-07-28 17:26:49 -07001417status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1418 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001419{
1420 ALOGV("startInput() input %d", input);
1421 ssize_t index = mInputs.indexOfKey(input);
1422 if (index < 0) {
1423 ALOGW("startInput() unknown input %d", input);
1424 return BAD_VALUE;
1425 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001426 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001427
Eric Laurentc722f302014-12-10 11:21:49 -08001428 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001429 if (index < 0) {
1430 ALOGW("startInput() unknown session %d on input %d", session, input);
1431 return BAD_VALUE;
1432 }
1433
Glenn Kasten74a8e252014-07-24 14:09:55 -07001434 // virtual input devices are compatible with other input devices
François Gaffie53615e22015-03-19 09:24:12 +01001435 if (!is_virtual_input_device(inputDesc->mDevice)) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001436
1437 // for a non-virtual input device, check if there is another (non-virtual) active input
François Gaffie53615e22015-03-19 09:24:12 +01001438 audio_io_handle_t activeInput = mInputs.getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001439 if (activeInput != 0 && activeInput != input) {
1440
1441 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1442 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001443 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001444 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001445 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurentc722f302014-12-10 11:21:49 -08001446 stopInput(activeInput, activeDesc->mSessions.itemAt(0));
1447 releaseInput(activeInput, activeDesc->mSessions.itemAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001448 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001449 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001450 return INVALID_OPERATION;
1451 }
1452 }
1453 }
1454
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001455 // Routing?
1456 mInputRoutes.incRouteActivity(session);
1457
Paul McLean466dc8e2015-04-17 13:15:36 -06001458 if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001459 // if input maps to a dynamic policy with an activity listener, notify of state change
1460 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001461 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001462 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1463 MIX_STATE_MIXING);
1464 }
1465
François Gaffie53615e22015-03-19 09:24:12 +01001466 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001467 SoundTrigger::setCaptureState(true);
1468 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001469 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001470
Eric Laurentc722f302014-12-10 11:21:49 -08001471 // automatically enable the remote submix output when input is started if not
1472 // used by a policy mix of type MIX_TYPE_RECORDERS
Glenn Kasten74a8e252014-07-24 14:09:55 -07001473 // For remote submix (a virtual device), we open only one input per capture request.
1474 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001475 String8 address = String8("");
1476 if (inputDesc->mPolicyMix == NULL) {
1477 address = String8("0");
1478 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1479 address = inputDesc->mPolicyMix->mRegistrationId;
1480 }
1481 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001482 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001483 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001484 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001485 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001486 }
Eric Laurente552edb2014-03-10 17:42:56 -07001487 }
1488
Eric Laurente552edb2014-03-10 17:42:56 -07001489 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1490
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001491 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001492 return NO_ERROR;
1493}
1494
Eric Laurent4dc68062014-07-28 17:26:49 -07001495status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1496 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001497{
1498 ALOGV("stopInput() input %d", input);
1499 ssize_t index = mInputs.indexOfKey(input);
1500 if (index < 0) {
1501 ALOGW("stopInput() unknown input %d", input);
1502 return BAD_VALUE;
1503 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001504 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001505
Eric Laurentc722f302014-12-10 11:21:49 -08001506 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001507 if (index < 0) {
1508 ALOGW("stopInput() unknown session %d on input %d", session, input);
1509 return BAD_VALUE;
1510 }
1511
Eric Laurente552edb2014-03-10 17:42:56 -07001512 if (inputDesc->mRefCount == 0) {
1513 ALOGW("stopInput() input %d already stopped", input);
1514 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001515 }
1516
1517 inputDesc->mRefCount--;
Paul McLean466dc8e2015-04-17 13:15:36 -06001518
1519 // Routing?
1520 mInputRoutes.decRouteActivity(session);
1521
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001522 if (inputDesc->mRefCount == 0) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001523 // if input maps to a dynamic policy with an activity listener, notify of state change
1524 if ((inputDesc->mPolicyMix != NULL)
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001525 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) {
Jean-Michel Trivi2b0c1fc2015-04-15 17:29:28 -07001526 mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
1527 MIX_STATE_IDLE);
1528 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001529
Eric Laurentc722f302014-12-10 11:21:49 -08001530 // automatically disable the remote submix output when input is stopped if not
1531 // used by a policy mix of type MIX_TYPE_RECORDERS
Eric Laurente552edb2014-03-10 17:42:56 -07001532 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
Eric Laurentc722f302014-12-10 11:21:49 -08001533 String8 address = String8("");
1534 if (inputDesc->mPolicyMix == NULL) {
1535 address = String8("0");
1536 } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) {
1537 address = inputDesc->mPolicyMix->mRegistrationId;
1538 }
1539 if (address != "") {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001540 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001541 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001542 address, "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001543 }
Eric Laurente552edb2014-03-10 17:42:56 -07001544 }
1545
Eric Laurent1c333e22014-05-20 10:48:17 -07001546 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001547
François Gaffie53615e22015-03-19 09:24:12 +01001548 if (mInputs.activeInputsCount() == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001549 SoundTrigger::setCaptureState(false);
1550 }
Eric Laurente552edb2014-03-10 17:42:56 -07001551 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001552 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001553}
1554
Eric Laurent4dc68062014-07-28 17:26:49 -07001555void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1556 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001557{
1558 ALOGV("releaseInput() %d", input);
1559 ssize_t index = mInputs.indexOfKey(input);
1560 if (index < 0) {
1561 ALOGW("releaseInput() releasing unknown input %d", input);
1562 return;
1563 }
Paul McLean466dc8e2015-04-17 13:15:36 -06001564
1565 // Routing
1566 mInputRoutes.removeRoute(session);
1567
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001568 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1569 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001570
Eric Laurentc722f302014-12-10 11:21:49 -08001571 index = inputDesc->mSessions.indexOf(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001572 if (index < 0) {
1573 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1574 return;
1575 }
Eric Laurentc722f302014-12-10 11:21:49 -08001576 inputDesc->mSessions.remove(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001577 if (inputDesc->mOpenRefCount == 0) {
1578 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1579 return;
1580 }
1581 inputDesc->mOpenRefCount--;
1582 if (inputDesc->mOpenRefCount > 0) {
1583 ALOGV("releaseInput() exit > 0");
1584 return;
1585 }
1586
Eric Laurent05b90f82014-08-27 15:32:29 -07001587 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001588 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001589 ALOGV("releaseInput() exit");
1590}
1591
Eric Laurentd4692962014-05-05 18:13:44 -07001592void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001593 bool patchRemoved = false;
1594
Eric Laurentd4692962014-05-05 18:13:44 -07001595 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001596 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1597 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1598 if (patch_index >= 0) {
1599 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1600 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1601 mAudioPatches.removeItemsAt(patch_index);
1602 patchRemoved = true;
1603 }
Eric Laurentd4692962014-05-05 18:13:44 -07001604 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1605 }
1606 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001607 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001608
1609 if (patchRemoved) {
1610 mpClientInterface->onAudioPatchListUpdate();
1611 }
Eric Laurentd4692962014-05-05 18:13:44 -07001612}
1613
Eric Laurente0720872014-03-11 09:30:41 -07001614void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001615 int indexMin,
1616 int indexMax)
1617{
1618 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
François Gaffie2110e042015-03-24 08:41:51 +01001619 mEngine->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001620 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1621 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffie2110e042015-03-24 08:41:51 +01001622 mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001623 }
Eric Laurente552edb2014-03-10 17:42:56 -07001624}
1625
Eric Laurente0720872014-03-11 09:30:41 -07001626status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
François Gaffie53615e22015-03-19 09:24:12 +01001627 int index,
1628 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07001629{
1630
Eric Laurentc75307b2015-03-17 15:29:32 -07001631 if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) ||
1632 (index > mStreams.valueFor(stream).getVolumeIndexMax())) {
Eric Laurente552edb2014-03-10 17:42:56 -07001633 return BAD_VALUE;
1634 }
1635 if (!audio_is_output_device(device)) {
1636 return BAD_VALUE;
1637 }
1638
1639 // Force max volume if stream cannot be muted
Eric Laurentc75307b2015-03-17 15:29:32 -07001640 if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07001641
1642 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1643 stream, device, index);
1644
1645 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1646 // clear all device specific values
1647 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
François Gaffiedfd74092015-03-19 12:10:59 +01001648 mStreams.clearCurrentVolumeIndex(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07001649 }
François Gaffiedfd74092015-03-19 12:10:59 +01001650 mStreams.addCurrentVolumeIndex(stream, device, index);
Eric Laurente552edb2014-03-10 17:42:56 -07001651
Eric Laurent31551f82014-10-10 18:21:56 -07001652 // update volume on all outputs whose current device is also selected by the same
1653 // strategy as the device specified by the caller
1654 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001655
1656
1657 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1658 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1659 if (stream == AUDIO_STREAM_MUSIC) {
François Gaffiedfd74092015-03-19 12:10:59 +01001660 mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001661 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1662 }
1663 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1664 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001665 return NO_ERROR;
1666 }
Eric Laurente552edb2014-03-10 17:42:56 -07001667 status_t status = NO_ERROR;
1668 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001669 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
1670 audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001671 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001672 status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07001673 if (volStatus != NO_ERROR) {
1674 status = volStatus;
1675 }
1676 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001677 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1678 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
Eric Laurentc75307b2015-03-17 15:29:32 -07001679 index, desc, curDevice);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001680 }
Eric Laurente552edb2014-03-10 17:42:56 -07001681 }
1682 return status;
1683}
1684
Eric Laurente0720872014-03-11 09:30:41 -07001685status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001686 int *index,
1687 audio_devices_t device)
1688{
1689 if (index == NULL) {
1690 return BAD_VALUE;
1691 }
1692 if (!audio_is_output_device(device)) {
1693 return BAD_VALUE;
1694 }
1695 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1696 // the strategy the stream belongs to.
1697 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1698 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1699 }
François Gaffiedfd74092015-03-19 12:10:59 +01001700 device = Volume::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001701
Eric Laurentc75307b2015-03-17 15:29:32 -07001702 *index = mStreams.valueFor(stream).getVolumeIndex(device);
Eric Laurente552edb2014-03-10 17:42:56 -07001703 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1704 return NO_ERROR;
1705}
1706
Eric Laurente0720872014-03-11 09:30:41 -07001707audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001708 const SortedVector<audio_io_handle_t>& outputs)
1709{
1710 // select one output among several suitable for global effects.
1711 // The priority is as follows:
1712 // 1: An offloaded output. If the effect ends up not being offloadable,
1713 // AudioFlinger will invalidate the track and the offloaded output
1714 // will be closed causing the effect to be moved to a PCM output.
1715 // 2: A deep buffer output
1716 // 3: the first output in the list
1717
1718 if (outputs.size() == 0) {
1719 return 0;
1720 }
1721
1722 audio_io_handle_t outputOffloaded = 0;
1723 audio_io_handle_t outputDeepBuffer = 0;
1724
1725 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07001726 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001727 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001728 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1729 outputOffloaded = outputs[i];
1730 }
1731 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1732 outputDeepBuffer = outputs[i];
1733 }
1734 }
1735
1736 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1737 outputOffloaded, outputDeepBuffer);
1738 if (outputOffloaded != 0) {
1739 return outputOffloaded;
1740 }
1741 if (outputDeepBuffer != 0) {
1742 return outputDeepBuffer;
1743 }
1744
1745 return outputs[0];
1746}
1747
Eric Laurente0720872014-03-11 09:30:41 -07001748audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001749{
1750 // apply simple rule where global effects are attached to the same output as MUSIC streams
1751
Eric Laurent3b73df72014-03-11 09:06:29 -07001752 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001753 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1754 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1755
1756 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1757 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1758 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1759
1760 return output;
1761}
1762
Eric Laurente0720872014-03-11 09:30:41 -07001763status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001764 audio_io_handle_t io,
1765 uint32_t strategy,
1766 int session,
1767 int id)
1768{
1769 ssize_t index = mOutputs.indexOfKey(io);
1770 if (index < 0) {
1771 index = mInputs.indexOfKey(io);
1772 if (index < 0) {
1773 ALOGW("registerEffect() unknown io %d", io);
1774 return INVALID_OPERATION;
1775 }
1776 }
François Gaffie45ed3b02015-03-19 10:35:14 +01001777 return mEffects.registerEffect(desc, io, strategy, session, id);
Eric Laurente552edb2014-03-10 17:42:56 -07001778}
1779
Eric Laurentc75307b2015-03-17 15:29:32 -07001780bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
1781{
1782 return mOutputs.isStreamActive(stream, inPastMs);
1783}
1784
1785bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
1786{
1787 return mOutputs.isStreamActiveRemotely(stream, inPastMs);
1788}
1789
Eric Laurente0720872014-03-11 09:30:41 -07001790bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001791{
1792 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001793 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001794 if (inputDescriptor->mRefCount == 0) {
1795 continue;
1796 }
1797 if (inputDescriptor->mInputSource == (int)source) {
Eric Laurente552edb2014-03-10 17:42:56 -07001798 return true;
1799 }
Eric Laurenta34c9ce2014-12-19 11:16:32 -08001800 // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it
1801 // corresponds to an active capture triggered by a hardware hotword recognition
1802 if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
1803 (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) {
1804 // FIXME: we should not assume that the first session is the active one and keep
1805 // activity count per session. Same in startInput().
1806 ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0));
1807 if (index >= 0) {
1808 return true;
1809 }
1810 }
Eric Laurente552edb2014-03-10 17:42:56 -07001811 }
1812 return false;
1813}
1814
Eric Laurent275e8e92014-11-30 15:14:47 -08001815// Register a list of custom mixes with their attributes and format.
1816// When a mix is registered, corresponding input and output profiles are
1817// added to the remote submix hw module. The profile contains only the
1818// parameters (sampling rate, format...) specified by the mix.
1819// The corresponding input remote submix device is also connected.
1820//
1821// When a remote submix device is connected, the address is checked to select the
1822// appropriate profile and the corresponding input or output stream is opened.
1823//
1824// When capture starts, getInputForAttr() will:
1825// - 1 look for a mix matching the address passed in attribtutes tags if any
1826// - 2 if none found, getDeviceForInputSource() will:
1827// - 2.1 look for a mix matching the attributes source
1828// - 2.2 if none found, default to device selection by policy rules
1829// At this time, the corresponding output remote submix device is also connected
1830// and active playback use cases can be transferred to this mix if needed when reconnecting
1831// after AudioTracks are invalidated
1832//
1833// When playback starts, getOutputForAttr() will:
1834// - 1 look for a mix matching the address passed in attribtutes tags if any
1835// - 2 if none found, look for a mix matching the attributes usage
1836// - 3 if none found, default to device and output selection by policy rules.
1837
1838status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
1839{
1840 sp<HwModule> module;
1841 for (size_t i = 0; i < mHwModules.size(); i++) {
1842 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1843 mHwModules[i]->mHandle != 0) {
1844 module = mHwModules[i];
1845 break;
1846 }
1847 }
1848
1849 if (module == 0) {
1850 return INVALID_OPERATION;
1851 }
1852
1853 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
1854
1855 for (size_t i = 0; i < mixes.size(); i++) {
1856 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001857
1858 if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001859 continue;
1860 }
1861 audio_config_t outputConfig = mixes[i].mFormat;
1862 audio_config_t inputConfig = mixes[i].mFormat;
1863 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
1864 // stereo and let audio flinger do the channel conversion if needed.
1865 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1866 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
1867 module->addOutputProfile(address, &outputConfig,
1868 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
1869 module->addInputProfile(address, &inputConfig,
1870 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
François Gaffie036e1e92015-03-19 10:16:24 +01001871
Eric Laurentc722f302014-12-10 11:21:49 -08001872 if (mixes[i].mMixType == MIX_TYPE_PLAYERS) {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001873 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001874 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001875 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001876 } else {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001877 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001878 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001879 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001880 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001881 }
1882 return NO_ERROR;
1883}
1884
1885status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
1886{
1887 sp<HwModule> module;
1888 for (size_t i = 0; i < mHwModules.size(); i++) {
1889 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
1890 mHwModules[i]->mHandle != 0) {
1891 module = mHwModules[i];
1892 break;
1893 }
1894 }
1895
1896 if (module == 0) {
1897 return INVALID_OPERATION;
1898 }
1899
1900 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
1901
1902 for (size_t i = 0; i < mixes.size(); i++) {
1903 String8 address = mixes[i].mRegistrationId;
François Gaffie036e1e92015-03-19 10:16:24 +01001904
1905 if (mPolicyMixes.unregisterMix(address) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001906 continue;
1907 }
1908
Eric Laurentc722f302014-12-10 11:21:49 -08001909 if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) ==
1910 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1911 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001912 setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
Eric Laurentc722f302014-12-10 11:21:49 -08001913 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001914 address.string(), "remote-submix");
Eric Laurentc722f302014-12-10 11:21:49 -08001915 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001916
1917 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
1918 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
1919 {
Eric Laurentc73ca6e2014-12-12 14:34:22 -08001920 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001921 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
Paul McLeane743a472015-01-28 11:07:31 -08001922 address.string(), "remote-submix");
Eric Laurent275e8e92014-11-30 15:14:47 -08001923 }
1924 module->removeOutputProfile(address);
1925 module->removeInputProfile(address);
1926 }
1927 return NO_ERROR;
1928}
1929
Eric Laurente552edb2014-03-10 17:42:56 -07001930
Eric Laurente0720872014-03-11 09:30:41 -07001931status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001932{
1933 const size_t SIZE = 256;
1934 char buffer[SIZE];
1935 String8 result;
1936
1937 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1938 result.append(buffer);
1939
Eric Laurent87ffa392015-05-22 10:32:38 -07001940 snprintf(buffer, SIZE, " Primary Output: %d\n",
1941 hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE);
Eric Laurente552edb2014-03-10 17:42:56 -07001942 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001943 snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState());
Eric Laurente552edb2014-03-10 17:42:56 -07001944 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001945 snprintf(buffer, SIZE, " Force use for communications %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001946 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07001947 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001948 snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA));
Eric Laurente552edb2014-03-10 17:42:56 -07001949 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001950 snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD));
Eric Laurente552edb2014-03-10 17:42:56 -07001951 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001952 snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK));
Eric Laurente552edb2014-03-10 17:42:56 -07001953 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001954 snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM));
Eric Laurente552edb2014-03-10 17:42:56 -07001955 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001956 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
François Gaffie2110e042015-03-24 08:41:51 +01001957 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO));
Jungshik Jang7b24ee32014-07-15 19:38:42 +09001958 result.append(buffer);
François Gaffie2110e042015-03-24 08:41:51 +01001959 write(fd, result.string(), result.size());
Eric Laurente552edb2014-03-10 17:42:56 -07001960
François Gaffie53615e22015-03-19 09:24:12 +01001961 mAvailableOutputDevices.dump(fd, String8("output"));
1962 mAvailableInputDevices.dump(fd, String8("input"));
1963 mHwModules.dump(fd);
1964 mOutputs.dump(fd);
1965 mInputs.dump(fd);
François Gaffiedfd74092015-03-19 12:10:59 +01001966 mStreams.dump(fd);
François Gaffie45ed3b02015-03-19 10:35:14 +01001967 mEffects.dump(fd);
François Gaffie53615e22015-03-19 09:24:12 +01001968 mAudioPatches.dump(fd);
Eric Laurente552edb2014-03-10 17:42:56 -07001969
1970 return NO_ERROR;
1971}
1972
1973// This function checks for the parameters which can be offloaded.
1974// This can be enhanced depending on the capability of the DSP and policy
1975// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001976bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001977{
1978 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001979 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001980 offloadInfo.sample_rate, offloadInfo.channel_mask,
1981 offloadInfo.format,
1982 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1983 offloadInfo.has_video);
1984
1985 // Check if offload has been disabled
1986 char propValue[PROPERTY_VALUE_MAX];
1987 if (property_get("audio.offload.disable", propValue, "0")) {
1988 if (atoi(propValue) != 0) {
1989 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1990 return false;
1991 }
1992 }
1993
1994 // Check if stream type is music, then only allow offload as of now.
1995 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1996 {
1997 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1998 return false;
1999 }
2000
2001 //TODO: enable audio offloading with video when ready
2002 if (offloadInfo.has_video)
2003 {
2004 ALOGV("isOffloadSupported: has_video == true, returning false");
2005 return false;
2006 }
2007
2008 //If duration is less than minimum value defined in property, return false
2009 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2010 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2011 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2012 return false;
2013 }
2014 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2015 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2016 return false;
2017 }
2018
2019 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2020 // creating an offloaded track and tearing it down immediately after start when audioflinger
2021 // detects there is an active non offloadable effect.
2022 // FIXME: We should check the audio session here but we do not have it in this context.
2023 // This may prevent offloading in rare situations where effects are left active by apps
2024 // in the background.
François Gaffie45ed3b02015-03-19 10:35:14 +01002025 if (mEffects.isNonOffloadableEffectEnabled()) {
Eric Laurente552edb2014-03-10 17:42:56 -07002026 return false;
2027 }
2028
2029 // See if there is a profile to support this.
2030 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002031 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002032 offloadInfo.sample_rate,
2033 offloadInfo.format,
2034 offloadInfo.channel_mask,
2035 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002036 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2037 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002038}
2039
Eric Laurent6a94d692014-05-20 11:18:06 -07002040status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2041 audio_port_type_t type,
2042 unsigned int *num_ports,
2043 struct audio_port *ports,
2044 unsigned int *generation)
2045{
2046 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2047 generation == NULL) {
2048 return BAD_VALUE;
2049 }
2050 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2051 if (ports == NULL) {
2052 *num_ports = 0;
2053 }
2054
2055 size_t portsWritten = 0;
2056 size_t portsMax = *num_ports;
2057 *num_ports = 0;
2058 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2059 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2060 for (size_t i = 0;
2061 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2062 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2063 }
2064 *num_ports += mAvailableOutputDevices.size();
2065 }
2066 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2067 for (size_t i = 0;
2068 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2069 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2070 }
2071 *num_ports += mAvailableInputDevices.size();
2072 }
2073 }
2074 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2075 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2076 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2077 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2078 }
2079 *num_ports += mInputs.size();
2080 }
2081 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002082 size_t numOutputs = 0;
2083 for (size_t i = 0; i < mOutputs.size(); i++) {
2084 if (!mOutputs[i]->isDuplicated()) {
2085 numOutputs++;
2086 if (portsWritten < portsMax) {
2087 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2088 }
2089 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002090 }
Eric Laurent84c70242014-06-23 08:46:27 -07002091 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002092 }
2093 }
2094 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002095 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002096 return NO_ERROR;
2097}
2098
2099status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2100{
2101 return NO_ERROR;
2102}
2103
Eric Laurent6a94d692014-05-20 11:18:06 -07002104status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2105 audio_patch_handle_t *handle,
2106 uid_t uid)
2107{
2108 ALOGV("createAudioPatch()");
2109
2110 if (handle == NULL || patch == NULL) {
2111 return BAD_VALUE;
2112 }
2113 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2114
Eric Laurent874c42872014-08-08 15:13:39 -07002115 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2116 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2117 return BAD_VALUE;
2118 }
2119 // only one source per audio patch supported for now
2120 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002121 return INVALID_OPERATION;
2122 }
Eric Laurent874c42872014-08-08 15:13:39 -07002123
2124 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002125 return INVALID_OPERATION;
2126 }
Eric Laurent874c42872014-08-08 15:13:39 -07002127 for (size_t i = 0; i < patch->num_sinks; i++) {
2128 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2129 return INVALID_OPERATION;
2130 }
2131 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002132
2133 sp<AudioPatch> patchDesc;
2134 ssize_t index = mAudioPatches.indexOfKey(*handle);
2135
Eric Laurent6a94d692014-05-20 11:18:06 -07002136 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2137 patch->sources[0].role,
2138 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002139#if LOG_NDEBUG == 0
2140 for (size_t i = 0; i < patch->num_sinks; i++) {
2141 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2142 patch->sinks[i].role,
2143 patch->sinks[i].type);
2144 }
2145#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002146
2147 if (index >= 0) {
2148 patchDesc = mAudioPatches.valueAt(index);
2149 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2150 mUidCached, patchDesc->mUid, uid);
2151 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2152 return INVALID_OPERATION;
2153 }
2154 } else {
2155 *handle = 0;
2156 }
2157
2158 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002159 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002160 if (outputDesc == NULL) {
2161 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2162 return BAD_VALUE;
2163 }
Eric Laurent84c70242014-06-23 08:46:27 -07002164 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2165 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002166 if (patchDesc != 0) {
2167 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2168 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2169 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2170 return BAD_VALUE;
2171 }
2172 }
Eric Laurent874c42872014-08-08 15:13:39 -07002173 DeviceVector devices;
2174 for (size_t i = 0; i < patch->num_sinks; i++) {
2175 // Only support mix to devices connection
2176 // TODO add support for mix to mix connection
2177 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2178 ALOGV("createAudioPatch() source mix but sink is not a device");
2179 return INVALID_OPERATION;
2180 }
2181 sp<DeviceDescriptor> devDesc =
2182 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2183 if (devDesc == 0) {
2184 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2185 return BAD_VALUE;
2186 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002187
François Gaffie53615e22015-03-19 09:24:12 +01002188 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002189 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002190 patch->sources[0].sample_rate,
François Gaffie53615e22015-03-19 09:24:12 +01002191 NULL, // updatedSamplingRate
2192 patch->sources[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002193 NULL, // updatedFormat
François Gaffie53615e22015-03-19 09:24:12 +01002194 patch->sources[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002195 NULL, // updatedChannelMask
François Gaffie53615e22015-03-19 09:24:12 +01002196 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
Andy Hungf129b032015-04-07 13:45:50 -07002197 ALOGV("createAudioPatch() profile not supported for device %08x",
2198 devDesc->type());
Eric Laurent874c42872014-08-08 15:13:39 -07002199 return INVALID_OPERATION;
2200 }
2201 devices.add(devDesc);
2202 }
2203 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002204 return INVALID_OPERATION;
2205 }
Eric Laurent874c42872014-08-08 15:13:39 -07002206
Eric Laurent6a94d692014-05-20 11:18:06 -07002207 // TODO: reconfigure output format and channels here
2208 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002209 devices.types(), outputDesc->mIoHandle);
Eric Laurentc75307b2015-03-17 15:29:32 -07002210 setOutputDevice(outputDesc, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002211 index = mAudioPatches.indexOfKey(*handle);
2212 if (index >= 0) {
2213 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2214 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2215 }
2216 patchDesc = mAudioPatches.valueAt(index);
2217 patchDesc->mUid = uid;
2218 ALOGV("createAudioPatch() success");
2219 } else {
2220 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2221 return INVALID_OPERATION;
2222 }
2223 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2224 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2225 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002226 // only one sink supported when connecting an input device to a mix
2227 if (patch->num_sinks > 1) {
2228 return INVALID_OPERATION;
2229 }
François Gaffie53615e22015-03-19 09:24:12 +01002230 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002231 if (inputDesc == NULL) {
2232 return BAD_VALUE;
2233 }
2234 if (patchDesc != 0) {
2235 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2236 return BAD_VALUE;
2237 }
2238 }
2239 sp<DeviceDescriptor> devDesc =
2240 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2241 if (devDesc == 0) {
2242 return BAD_VALUE;
2243 }
2244
François Gaffie53615e22015-03-19 09:24:12 +01002245 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(),
Eric Laurent275e8e92014-11-30 15:14:47 -08002246 devDesc->mAddress,
2247 patch->sinks[0].sample_rate,
2248 NULL, /*updatedSampleRate*/
2249 patch->sinks[0].format,
Andy Hungf129b032015-04-07 13:45:50 -07002250 NULL, /*updatedFormat*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002251 patch->sinks[0].channel_mask,
Andy Hungf129b032015-04-07 13:45:50 -07002252 NULL, /*updatedChannelMask*/
Eric Laurent275e8e92014-11-30 15:14:47 -08002253 // FIXME for the parameter type,
2254 // and the NONE
2255 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002256 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002257 return INVALID_OPERATION;
2258 }
2259 // TODO: reconfigure output format and channels here
2260 ALOGV("createAudioPatch() setting device %08x on output %d",
François Gaffie53615e22015-03-19 09:24:12 +01002261 devDesc->type(), inputDesc->mIoHandle);
2262 setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002263 index = mAudioPatches.indexOfKey(*handle);
2264 if (index >= 0) {
2265 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2266 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2267 }
2268 patchDesc = mAudioPatches.valueAt(index);
2269 patchDesc->mUid = uid;
2270 ALOGV("createAudioPatch() success");
2271 } else {
2272 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2273 return INVALID_OPERATION;
2274 }
2275 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2276 // device to device connection
2277 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002278 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002279 return BAD_VALUE;
2280 }
2281 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002282 sp<DeviceDescriptor> srcDeviceDesc =
2283 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002284 if (srcDeviceDesc == 0) {
2285 return BAD_VALUE;
2286 }
Eric Laurent874c42872014-08-08 15:13:39 -07002287
Eric Laurent6a94d692014-05-20 11:18:06 -07002288 //update source and sink with our own data as the data passed in the patch may
2289 // be incomplete.
2290 struct audio_patch newPatch = *patch;
2291 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002292
Eric Laurent874c42872014-08-08 15:13:39 -07002293 for (size_t i = 0; i < patch->num_sinks; i++) {
2294 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2295 ALOGV("createAudioPatch() source device but one sink is not a device");
2296 return INVALID_OPERATION;
2297 }
2298
2299 sp<DeviceDescriptor> sinkDeviceDesc =
2300 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2301 if (sinkDeviceDesc == 0) {
2302 return BAD_VALUE;
2303 }
2304 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2305
Eric Laurent3bcf8592015-04-03 12:13:24 -07002306 // create a software bridge in PatchPanel if:
2307 // - source and sink devices are on differnt HW modules OR
2308 // - audio HAL version is < 3.0
2309 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) ||
2310 (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) {
2311 // support only one sink device for now to simplify output selection logic
Eric Laurent874c42872014-08-08 15:13:39 -07002312 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002313 return INVALID_OPERATION;
2314 }
Eric Laurent874c42872014-08-08 15:13:39 -07002315 SortedVector<audio_io_handle_t> outputs =
François Gaffie53615e22015-03-19 09:24:12 +01002316 getOutputsForDevice(sinkDeviceDesc->type(), mOutputs);
Eric Laurent874c42872014-08-08 15:13:39 -07002317 // if the sink device is reachable via an opened output stream, request to go via
2318 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002319 audio_io_handle_t output = selectOutput(outputs,
2320 AUDIO_OUTPUT_FLAG_NONE,
2321 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002322 if (output != AUDIO_IO_HANDLE_NONE) {
2323 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2324 if (outputDesc->isDuplicated()) {
2325 return INVALID_OPERATION;
2326 }
2327 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
Eric Laurent3bcf8592015-04-03 12:13:24 -07002328 newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH;
Eric Laurent874c42872014-08-08 15:13:39 -07002329 newPatch.num_sources = 2;
2330 }
Eric Laurent83b88082014-06-20 18:31:16 -07002331 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002332 }
2333 // TODO: check from routing capabilities in config file and other conflicting patches
2334
2335 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2336 if (index >= 0) {
2337 afPatchHandle = patchDesc->mAfPatchHandle;
2338 }
2339
2340 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2341 &afPatchHandle,
2342 0);
2343 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2344 status, afPatchHandle);
2345 if (status == NO_ERROR) {
2346 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01002347 patchDesc = new AudioPatch(&newPatch, uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002348 addAudioPatch(patchDesc->mHandle, patchDesc);
2349 } else {
2350 patchDesc->mPatch = newPatch;
2351 }
2352 patchDesc->mAfPatchHandle = afPatchHandle;
2353 *handle = patchDesc->mHandle;
2354 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002355 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002356 } else {
2357 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2358 status);
2359 return INVALID_OPERATION;
2360 }
2361 } else {
2362 return BAD_VALUE;
2363 }
2364 } else {
2365 return BAD_VALUE;
2366 }
2367 return NO_ERROR;
2368}
2369
2370status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2371 uid_t uid)
2372{
2373 ALOGV("releaseAudioPatch() patch %d", handle);
2374
2375 ssize_t index = mAudioPatches.indexOfKey(handle);
2376
2377 if (index < 0) {
2378 return BAD_VALUE;
2379 }
2380 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2381 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2382 mUidCached, patchDesc->mUid, uid);
2383 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2384 return INVALID_OPERATION;
2385 }
2386
2387 struct audio_patch *patch = &patchDesc->mPatch;
2388 patchDesc->mUid = mUidCached;
2389 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002390 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002391 if (outputDesc == NULL) {
2392 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2393 return BAD_VALUE;
2394 }
2395
Eric Laurentc75307b2015-03-17 15:29:32 -07002396 setOutputDevice(outputDesc,
2397 getNewOutputDevice(outputDesc, true /*fromCache*/),
Eric Laurent6a94d692014-05-20 11:18:06 -07002398 true,
2399 0,
2400 NULL);
2401 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2402 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
François Gaffie53615e22015-03-19 09:24:12 +01002403 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002404 if (inputDesc == NULL) {
2405 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2406 return BAD_VALUE;
2407 }
2408 setInputDevice(inputDesc->mIoHandle,
2409 getNewInputDevice(inputDesc->mIoHandle),
2410 true,
2411 NULL);
2412 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2413 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2414 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2415 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2416 status, patchDesc->mAfPatchHandle);
2417 removeAudioPatch(patchDesc->mHandle);
2418 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002419 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002420 } else {
2421 return BAD_VALUE;
2422 }
2423 } else {
2424 return BAD_VALUE;
2425 }
2426 return NO_ERROR;
2427}
2428
2429status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2430 struct audio_patch *patches,
2431 unsigned int *generation)
2432{
François Gaffie53615e22015-03-19 09:24:12 +01002433 if (generation == NULL) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002434 return BAD_VALUE;
2435 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 *generation = curAudioPortGeneration();
François Gaffie53615e22015-03-19 09:24:12 +01002437 return mAudioPatches.listAudioPatches(num_patches, patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002438}
2439
Eric Laurente1715a42014-05-20 11:30:42 -07002440status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002441{
Eric Laurente1715a42014-05-20 11:30:42 -07002442 ALOGV("setAudioPortConfig()");
2443
2444 if (config == NULL) {
2445 return BAD_VALUE;
2446 }
2447 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2448 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002449 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2450 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002451 }
2452
Eric Laurenta121f902014-06-03 13:32:54 -07002453 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002454 if (config->type == AUDIO_PORT_TYPE_MIX) {
2455 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002456 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002457 if (outputDesc == NULL) {
2458 return BAD_VALUE;
2459 }
Eric Laurent84c70242014-06-23 08:46:27 -07002460 ALOG_ASSERT(!outputDesc->isDuplicated(),
2461 "setAudioPortConfig() called on duplicated output %d",
2462 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002463 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002464 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
François Gaffie53615e22015-03-19 09:24:12 +01002465 sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002466 if (inputDesc == NULL) {
2467 return BAD_VALUE;
2468 }
Eric Laurenta121f902014-06-03 13:32:54 -07002469 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002470 } else {
2471 return BAD_VALUE;
2472 }
2473 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2474 sp<DeviceDescriptor> deviceDesc;
2475 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2476 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2477 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2478 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2479 } else {
2480 return BAD_VALUE;
2481 }
2482 if (deviceDesc == NULL) {
2483 return BAD_VALUE;
2484 }
Eric Laurenta121f902014-06-03 13:32:54 -07002485 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002486 } else {
2487 return BAD_VALUE;
2488 }
2489
Eric Laurenta121f902014-06-03 13:32:54 -07002490 struct audio_port_config backupConfig;
2491 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2492 if (status == NO_ERROR) {
2493 struct audio_port_config newConfig;
2494 audioPortConfig->toAudioPortConfig(&newConfig, config);
2495 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002496 }
Eric Laurenta121f902014-06-03 13:32:54 -07002497 if (status != NO_ERROR) {
2498 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002499 }
Eric Laurente1715a42014-05-20 11:30:42 -07002500
2501 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002502}
2503
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002504void AudioPolicyManager::releaseResourcesForUid(uid_t uid)
2505{
2506 clearAudioPatches(uid);
2507 clearSessionRoutes(uid);
2508}
2509
Eric Laurent6a94d692014-05-20 11:18:06 -07002510void AudioPolicyManager::clearAudioPatches(uid_t uid)
2511{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002512 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002513 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2514 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002515 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002516 }
2517 }
2518}
2519
Eric Laurent8c7e6da2015-04-21 17:37:00 -07002520
2521void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy,
2522 audio_io_handle_t ouptutToSkip)
2523{
2524 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
2525 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
2526 for (size_t j = 0; j < mOutputs.size(); j++) {
2527 if (mOutputs.keyAt(j) == ouptutToSkip) {
2528 continue;
2529 }
2530 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j);
2531 if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) {
2532 continue;
2533 }
2534 // If the default device for this strategy is on another output mix,
2535 // invalidate all tracks in this strategy to force re connection.
2536 // Otherwise select new device on the output mix.
2537 if (outputs.indexOf(mOutputs.keyAt(j)) < 0) {
2538 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
2539 if (stream == AUDIO_STREAM_PATCH) {
2540 continue;
2541 }
2542 if (getStrategy((audio_stream_type_t)stream) == strategy) {
2543 mpClientInterface->invalidateStream((audio_stream_type_t)stream);
2544 }
2545 }
2546 } else {
2547 audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/);
2548 setOutputDevice(outputDesc, newDevice, false);
2549 }
2550 }
2551}
2552
2553void AudioPolicyManager::clearSessionRoutes(uid_t uid)
2554{
2555 // remove output routes associated with this uid
2556 SortedVector<routing_strategy> affectedStrategies;
2557 for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) {
2558 sp<SessionRoute> route = mOutputRoutes.valueAt(i);
2559 if (route->mUid == uid) {
2560 mOutputRoutes.removeItemsAt(i);
2561 if (route->mDeviceDescriptor != 0) {
2562 affectedStrategies.add(getStrategy(route->mStreamType));
2563 }
2564 }
2565 }
2566 // reroute outputs if necessary
2567 for (size_t i = 0; i < affectedStrategies.size(); i++) {
2568 checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE);
2569 }
2570
2571 // remove input routes associated with this uid
2572 SortedVector<audio_source_t> affectedSources;
2573 for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) {
2574 sp<SessionRoute> route = mInputRoutes.valueAt(i);
2575 if (route->mUid == uid) {
2576 mInputRoutes.removeItemsAt(i);
2577 if (route->mDeviceDescriptor != 0) {
2578 affectedSources.add(route->mSource);
2579 }
2580 }
2581 }
2582 // reroute inputs if necessary
2583 SortedVector<audio_io_handle_t> inputsToClose;
2584 for (size_t i = 0; i < mInputs.size(); i++) {
2585 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i);
2586 if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) {
2587 inputsToClose.add(inputDesc->mIoHandle);
2588 }
2589 }
2590 for (size_t i = 0; i < inputsToClose.size(); i++) {
2591 closeInput(inputsToClose[i]);
2592 }
2593}
2594
2595
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002596status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2597 audio_io_handle_t *ioHandle,
2598 audio_devices_t *device)
2599{
2600 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2601 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
Eric Laurentc73ca6e2014-12-12 14:34:22 -08002602 *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002603
François Gaffiedf372692015-03-19 10:43:27 +01002604 return mSoundTriggerSessions.acquireSession(*session, *ioHandle);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002605}
2606
Eric Laurent493404d2015-04-21 15:07:36 -07002607status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused,
2608 const audio_attributes_t *attributes __unused,
2609 audio_io_handle_t *handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002610{
2611 return INVALID_OPERATION;
2612}
2613
Eric Laurent493404d2015-04-21 15:07:36 -07002614status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused)
Eric Laurent554a2772015-04-10 11:29:24 -07002615{
2616 return INVALID_OPERATION;
2617}
2618
Eric Laurente552edb2014-03-10 17:42:56 -07002619// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002620// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002621// ----------------------------------------------------------------------------
Eric Laurent6a94d692014-05-20 11:18:06 -07002622uint32_t AudioPolicyManager::nextAudioPortGeneration()
2623{
2624 return android_atomic_inc(&mAudioPortGeneration);
2625}
2626
Eric Laurente0720872014-03-11 09:30:41 -07002627AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002628 :
2629#ifdef AUDIO_POLICY_TEST
2630 Thread(false),
2631#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -07002632 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002633 mA2dpSuspended(false),
Paul McLeane743a472015-01-28 11:07:31 -08002634 mSpeakerDrcEnabled(false),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002635 mAudioPortGeneration(1),
2636 mBeaconMuteRefCount(0),
2637 mBeaconPlayingRefCount(0),
2638 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002639{
François Gaffie2110e042015-03-24 08:41:51 +01002640 audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
2641 if (!engineInstance) {
2642 ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__);
2643 return;
2644 }
2645 // Retrieve the Policy Manager Interface
2646 mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
2647 if (mEngine == NULL) {
2648 ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
2649 return;
2650 }
2651 mEngine->setObserver(this);
2652 status_t status = mEngine->initCheck();
2653 ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
2654
Eric Laurent6a94d692014-05-20 11:18:06 -07002655 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002656 mpClientInterface = clientInterface;
2657
Eric Laurent7288ab82015-05-06 18:44:02 -07002658 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie53615e22015-03-19 09:24:12 +01002659 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE,
2660 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2661 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
2662 if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE,
2663 mHwModules, mAvailableInputDevices, mAvailableOutputDevices,
2664 mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07002665 ALOGE("could not load audio policy configuration file, setting defaults");
2666 defaultAudioPolicyConfig();
2667 }
2668 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002669 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002670
François Gaffie2110e042015-03-24 08:41:51 +01002671 // must be done after reading the policy (since conditionned by Speaker Drc Enabling)
2672 mEngine->initializeVolumeCurves(mSpeakerDrcEnabled);
Eric Laurente552edb2014-03-10 17:42:56 -07002673
2674 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002675 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2676 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002677 for (size_t i = 0; i < mHwModules.size(); i++) {
2678 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2679 if (mHwModules[i]->mHandle == 0) {
2680 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2681 continue;
2682 }
2683 // open all output streams needed to access attached devices
2684 // except for direct output streams that are only opened when they are actually
2685 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002686 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002687 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2688 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002689 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002690
Eric Laurent3a4311c2014-03-17 12:00:47 -07002691 if (outProfile->mSupportedDevices.isEmpty()) {
2692 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2693 continue;
2694 }
2695
Eric Laurentd78f1532014-09-16 16:38:20 -07002696 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2697 continue;
2698 }
Eric Laurent83b88082014-06-20 18:31:16 -07002699 audio_devices_t profileType = outProfile->mSupportedDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +01002700 if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) {
2701 profileType = mDefaultOutputDevice->type();
Eric Laurent83b88082014-06-20 18:31:16 -07002702 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002703 // chose first device present in mSupportedDevices also part of
2704 // outputDeviceTypes
2705 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002706 profileType = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002707 if ((profileType & outputDeviceTypes) != 0) {
2708 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002709 }
Eric Laurente552edb2014-03-10 17:42:56 -07002710 }
2711 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002712 if ((profileType & outputDeviceTypes) == 0) {
2713 continue;
2714 }
Eric Laurentc75307b2015-03-17 15:29:32 -07002715 sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,
2716 mpClientInterface);
Eric Laurentd78f1532014-09-16 16:38:20 -07002717
2718 outputDesc->mDevice = profileType;
2719 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2720 config.sample_rate = outputDesc->mSamplingRate;
2721 config.channel_mask = outputDesc->mChannelMask;
2722 config.format = outputDesc->mFormat;
2723 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002724 status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002725 &output,
2726 &config,
2727 &outputDesc->mDevice,
2728 String8(""),
2729 &outputDesc->mLatency,
2730 outputDesc->mFlags);
2731
2732 if (status != NO_ERROR) {
2733 ALOGW("Cannot open output stream for device %08x on hw module %s",
2734 outputDesc->mDevice,
2735 mHwModules[i]->mName);
2736 } else {
2737 outputDesc->mSamplingRate = config.sample_rate;
2738 outputDesc->mChannelMask = config.channel_mask;
2739 outputDesc->mFormat = config.format;
2740
2741 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002742 audio_devices_t type = outProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002743 ssize_t index =
2744 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
2745 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002746 if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) {
2747 mAvailableOutputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002748 }
2749 }
2750 if (mPrimaryOutput == 0 &&
2751 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Eric Laurentc75307b2015-03-17 15:29:32 -07002752 mPrimaryOutput = outputDesc;
Eric Laurentd78f1532014-09-16 16:38:20 -07002753 }
2754 addOutput(output, outputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07002755 setOutputDevice(outputDesc,
Eric Laurentd78f1532014-09-16 16:38:20 -07002756 outputDesc->mDevice,
2757 true);
2758 }
Eric Laurente552edb2014-03-10 17:42:56 -07002759 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002760 // open input streams needed to access attached devices to validate
2761 // mAvailableInputDevices list
2762 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2763 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002764 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002765
Eric Laurent3a4311c2014-03-17 12:00:47 -07002766 if (inProfile->mSupportedDevices.isEmpty()) {
2767 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2768 continue;
2769 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002770 // chose first device present in mSupportedDevices also part of
2771 // inputDeviceTypes
2772 audio_devices_t profileType = AUDIO_DEVICE_NONE;
2773 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002774 profileType = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002775 if (profileType & inputDeviceTypes) {
2776 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002777 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002778 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002779 if ((profileType & inputDeviceTypes) == 0) {
2780 continue;
2781 }
2782 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
2783
2784 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
2785 inputDesc->mDevice = profileType;
2786
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002787 // find the address
2788 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
2789 // the inputs vector must be of size 1, but we don't want to crash here
2790 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
2791 : String8("");
2792 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
2793 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
2794
Eric Laurentd78f1532014-09-16 16:38:20 -07002795 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2796 config.sample_rate = inputDesc->mSamplingRate;
2797 config.channel_mask = inputDesc->mChannelMask;
2798 config.format = inputDesc->mFormat;
2799 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07002800 status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(),
Eric Laurentd78f1532014-09-16 16:38:20 -07002801 &input,
2802 &config,
2803 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002804 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07002805 AUDIO_SOURCE_MIC,
2806 AUDIO_INPUT_FLAG_NONE);
2807
2808 if (status == NO_ERROR) {
2809 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
François Gaffie53615e22015-03-19 09:24:12 +01002810 audio_devices_t type = inProfile->mSupportedDevices[k]->type();
Eric Laurentd78f1532014-09-16 16:38:20 -07002811 ssize_t index =
2812 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
2813 // give a valid ID to an attached device once confirmed it is reachable
Paul McLeane743a472015-01-28 11:07:31 -08002814 if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) {
2815 mAvailableInputDevices[index]->attach(mHwModules[i]);
Eric Laurentd78f1532014-09-16 16:38:20 -07002816 }
2817 }
2818 mpClientInterface->closeInput(input);
2819 } else {
2820 ALOGW("Cannot open input stream for device %08x on hw module %s",
2821 inputDesc->mDevice,
2822 mHwModules[i]->mName);
2823 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002824 }
2825 }
2826 // make sure all attached devices have been allocated a unique ID
2827 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002828 if (!mAvailableOutputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002829 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002830 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2831 continue;
2832 }
François Gaffie2110e042015-03-24 08:41:51 +01002833 // The device is now validated and can be appended to the available devices of the engine
2834 mEngine->setDeviceConnectionState(mAvailableOutputDevices[i],
2835 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002836 i++;
2837 }
2838 for (size_t i = 0; i < mAvailableInputDevices.size();) {
Paul McLeane743a472015-01-28 11:07:31 -08002839 if (!mAvailableInputDevices[i]->isAttached()) {
François Gaffie53615e22015-03-19 09:24:12 +01002840 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002841 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2842 continue;
2843 }
François Gaffie2110e042015-03-24 08:41:51 +01002844 // The device is now validated and can be appended to the available devices of the engine
2845 mEngine->setDeviceConnectionState(mAvailableInputDevices[i],
2846 AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002847 i++;
2848 }
2849 // make sure default device is reachable
2850 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
François Gaffie53615e22015-03-19 09:24:12 +01002851 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002852 }
Eric Laurente552edb2014-03-10 17:42:56 -07002853
2854 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2855
2856 updateDevicesAndOutputs();
2857
2858#ifdef AUDIO_POLICY_TEST
2859 if (mPrimaryOutput != 0) {
2860 AudioParameter outputCmd = AudioParameter();
2861 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07002862 mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString());
Eric Laurente552edb2014-03-10 17:42:56 -07002863
2864 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2865 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002866 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2867 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002868 mTestLatencyMs = 0;
2869 mCurOutput = 0;
2870 mDirectOutput = false;
2871 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2872 mTestOutputs[i] = 0;
2873 }
2874
2875 const size_t SIZE = 256;
2876 char buffer[SIZE];
2877 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2878 run(buffer, ANDROID_PRIORITY_AUDIO);
2879 }
2880#endif //AUDIO_POLICY_TEST
2881}
2882
Eric Laurente0720872014-03-11 09:30:41 -07002883AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002884{
2885#ifdef AUDIO_POLICY_TEST
2886 exit();
2887#endif //AUDIO_POLICY_TEST
2888 for (size_t i = 0; i < mOutputs.size(); i++) {
2889 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002890 }
2891 for (size_t i = 0; i < mInputs.size(); i++) {
2892 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002893 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002894 mAvailableOutputDevices.clear();
2895 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002896 mOutputs.clear();
2897 mInputs.clear();
2898 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002899}
2900
Eric Laurente0720872014-03-11 09:30:41 -07002901status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002902{
Eric Laurent87ffa392015-05-22 10:32:38 -07002903 return hasPrimaryOutput() ? NO_ERROR : NO_INIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002904}
2905
2906#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002907bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002908{
2909 ALOGV("entering threadLoop()");
2910 while (!exitPending())
2911 {
2912 String8 command;
2913 int valueInt;
2914 String8 value;
2915
2916 Mutex::Autolock _l(mLock);
2917 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2918
2919 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2920 AudioParameter param = AudioParameter(command);
2921
2922 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2923 valueInt != 0) {
2924 ALOGV("Test command %s received", command.string());
2925 String8 target;
2926 if (param.get(String8("target"), target) != NO_ERROR) {
2927 target = "Manager";
2928 }
2929 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2930 param.remove(String8("test_cmd_policy_output"));
2931 mCurOutput = valueInt;
2932 }
2933 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2934 param.remove(String8("test_cmd_policy_direct"));
2935 if (value == "false") {
2936 mDirectOutput = false;
2937 } else if (value == "true") {
2938 mDirectOutput = true;
2939 }
2940 }
2941 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2942 param.remove(String8("test_cmd_policy_input"));
2943 mTestInput = valueInt;
2944 }
2945
2946 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2947 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002948 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002949 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002950 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002951 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002952 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002953 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002954 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002955 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002956 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002957 if (target == "Manager") {
2958 mTestFormat = format;
2959 } else if (mTestOutputs[mCurOutput] != 0) {
2960 AudioParameter outputParam = AudioParameter();
2961 outputParam.addInt(String8("format"), format);
2962 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2963 }
2964 }
2965 }
2966 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2967 param.remove(String8("test_cmd_policy_channels"));
2968 int channels = 0;
2969
2970 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002971 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002972 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002973 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002974 }
2975 if (channels != 0) {
2976 if (target == "Manager") {
2977 mTestChannels = channels;
2978 } else if (mTestOutputs[mCurOutput] != 0) {
2979 AudioParameter outputParam = AudioParameter();
2980 outputParam.addInt(String8("channels"), channels);
2981 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2982 }
2983 }
2984 }
2985 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2986 param.remove(String8("test_cmd_policy_sampleRate"));
2987 if (valueInt >= 0 && valueInt <= 96000) {
2988 int samplingRate = valueInt;
2989 if (target == "Manager") {
2990 mTestSamplingRate = samplingRate;
2991 } else if (mTestOutputs[mCurOutput] != 0) {
2992 AudioParameter outputParam = AudioParameter();
2993 outputParam.addInt(String8("sampling_rate"), samplingRate);
2994 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2995 }
2996 }
2997 }
2998
2999 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3000 param.remove(String8("test_cmd_policy_reopen"));
3001
Eric Laurentc75307b2015-03-17 15:29:32 -07003002 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput););
Eric Laurente552edb2014-03-10 17:42:56 -07003003
Eric Laurentc75307b2015-03-17 15:29:32 -07003004 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle();
Eric Laurente552edb2014-03-10 17:42:56 -07003005
Eric Laurentc75307b2015-03-17 15:29:32 -07003006 removeOutput(mPrimaryOutput->mIoHandle);
3007 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL,
3008 mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003009 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003010 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3011 config.sample_rate = outputDesc->mSamplingRate;
3012 config.channel_mask = outputDesc->mChannelMask;
3013 config.format = outputDesc->mFormat;
Eric Laurentc75307b2015-03-17 15:29:32 -07003014 audio_io_handle_t handle;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003015 status_t status = mpClientInterface->openOutput(moduleHandle,
Eric Laurentc75307b2015-03-17 15:29:32 -07003016 &handle,
Eric Laurentcf2c0212014-07-25 16:20:43 -07003017 &config,
3018 &outputDesc->mDevice,
3019 String8(""),
3020 &outputDesc->mLatency,
3021 outputDesc->mFlags);
3022 if (status != NO_ERROR) {
3023 ALOGE("Failed to reopen hardware output stream, "
3024 "samplingRate: %d, format %d, channels %d",
3025 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003026 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003027 outputDesc->mSamplingRate = config.sample_rate;
3028 outputDesc->mChannelMask = config.channel_mask;
3029 outputDesc->mFormat = config.format;
Eric Laurentc75307b2015-03-17 15:29:32 -07003030 mPrimaryOutput = outputDesc;
Eric Laurente552edb2014-03-10 17:42:56 -07003031 AudioParameter outputCmd = AudioParameter();
3032 outputCmd.addInt(String8("set_id"), 0);
Eric Laurentc75307b2015-03-17 15:29:32 -07003033 mpClientInterface->setParameters(handle, outputCmd.toString());
3034 addOutput(handle, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07003035 }
3036 }
3037
3038
3039 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3040 }
3041 }
3042 return false;
3043}
3044
Eric Laurente0720872014-03-11 09:30:41 -07003045void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003046{
3047 {
3048 AutoMutex _l(mLock);
3049 requestExit();
3050 mWaitWorkCV.signal();
3051 }
3052 requestExitAndWait();
3053}
3054
Eric Laurente0720872014-03-11 09:30:41 -07003055int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003056{
3057 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3058 if (output == mTestOutputs[i]) return i;
3059 }
3060 return 0;
3061}
3062#endif //AUDIO_POLICY_TEST
3063
3064// ---
3065
Eric Laurentc75307b2015-03-17 15:29:32 -07003066void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003067{
François Gaffie98cc1912015-03-18 17:52:40 +01003068 outputDesc->setIoHandle(output);
Eric Laurent1c333e22014-05-20 10:48:17 -07003069 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003070 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003071}
3072
François Gaffie53615e22015-03-19 09:24:12 +01003073void AudioPolicyManager::removeOutput(audio_io_handle_t output)
3074{
3075 mOutputs.removeItem(output);
3076}
3077
Eric Laurent1f2f2232014-06-02 12:01:23 -07003078void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003079{
François Gaffie98cc1912015-03-18 17:52:40 +01003080 inputDesc->setIoHandle(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003081 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003082 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003083}
Eric Laurente552edb2014-03-10 17:42:56 -07003084
Eric Laurentc75307b2015-03-17 15:29:32 -07003085void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -08003086 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003087 const String8 address /*in*/,
3088 SortedVector<audio_io_handle_t>& outputs /*out*/) {
keunyoung3190e672014-12-30 13:00:52 -08003089 sp<DeviceDescriptor> devDesc =
3090 desc->mProfile->mSupportedDevices.getDevice(device, address);
3091 if (devDesc != 0) {
3092 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
3093 desc->mIoHandle, address.string());
3094 outputs.add(desc->mIoHandle);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003095 }
3096}
3097
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003098status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
François Gaffie53615e22015-03-19 09:24:12 +01003099 audio_policy_dev_state_t state,
3100 SortedVector<audio_io_handle_t>& outputs,
3101 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003102{
François Gaffie53615e22015-03-19 09:24:12 +01003103 audio_devices_t device = devDesc->type();
Eric Laurentc75307b2015-03-17 15:29:32 -07003104 sp<SwAudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003105 // erase all current sample rates, formats and channel masks
3106 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003107
Eric Laurent3b73df72014-03-11 09:06:29 -07003108 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003109 // first list already open outputs that can be routed to this device
3110 for (size_t i = 0; i < mOutputs.size(); i++) {
3111 desc = mOutputs.valueAt(i);
Eric Laurentc75307b2015-03-17 15:29:32 -07003112 if (!desc->isDuplicated() && (desc->supportedDevices() & device)) {
François Gaffie53615e22015-03-19 09:24:12 +01003113 if (!device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003114 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3115 outputs.add(mOutputs.keyAt(i));
3116 } else {
3117 ALOGV(" checking address match due to device 0x%x", device);
keunyoung3190e672014-12-30 13:00:52 -08003118 findIoHandlesByAddress(desc, device, address, outputs);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003119 }
Eric Laurente552edb2014-03-10 17:42:56 -07003120 }
3121 }
3122 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003123 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003124 for (size_t i = 0; i < mHwModules.size(); i++)
3125 {
3126 if (mHwModules[i]->mHandle == 0) {
3127 continue;
3128 }
3129 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3130 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003131 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3132 if (profile->mSupportedDevices.types() & device) {
François Gaffie53615e22015-03-19 09:24:12 +01003133 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003134 address == profile->mSupportedDevices[0]->mAddress) {
3135 profiles.add(profile);
3136 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3137 }
Eric Laurente552edb2014-03-10 17:42:56 -07003138 }
3139 }
3140 }
3141
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003142 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3143
Eric Laurente552edb2014-03-10 17:42:56 -07003144 if (profiles.isEmpty() && outputs.isEmpty()) {
3145 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3146 return BAD_VALUE;
3147 }
3148
3149 // open outputs for matching profiles if needed. Direct outputs are also opened to
3150 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3151 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003152 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003153
3154 // nothing to do if one output is already opened for this profile
3155 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003156 for (j = 0; j < outputs.size(); j++) {
3157 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003158 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003159 // matching profile: save the sample rates, format and channel masks supported
3160 // by the profile in our device descriptor
3161 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003162 break;
3163 }
3164 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003165 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003166 continue;
3167 }
3168
Eric Laurent83b88082014-06-20 18:31:16 -07003169 ALOGV("opening output for device %08x with params %s profile %p",
3170 device, address.string(), profile.get());
Eric Laurentc75307b2015-03-17 15:29:32 -07003171 desc = new SwAudioOutputDescriptor(profile, mpClientInterface);
Eric Laurente552edb2014-03-10 17:42:56 -07003172 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003173 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3174 config.sample_rate = desc->mSamplingRate;
3175 config.channel_mask = desc->mChannelMask;
3176 config.format = desc->mFormat;
3177 config.offload_info.sample_rate = desc->mSamplingRate;
3178 config.offload_info.channel_mask = desc->mChannelMask;
3179 config.offload_info.format = desc->mFormat;
3180 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003181 status_t status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003182 &output,
3183 &config,
3184 &desc->mDevice,
3185 address,
3186 &desc->mLatency,
3187 desc->mFlags);
3188 if (status == NO_ERROR) {
3189 desc->mSamplingRate = config.sample_rate;
3190 desc->mChannelMask = config.channel_mask;
3191 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003192
Eric Laurentd4692962014-05-05 18:13:44 -07003193 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003194 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003195 char *param = audio_device_address_to_parameter(device, address);
3196 mpClientInterface->setParameters(output, String8(param));
3197 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003198 }
3199
Eric Laurentd4692962014-05-05 18:13:44 -07003200 // Here is where we step through and resolve any "dynamic" fields
3201 String8 reply;
3202 char *value;
3203 if (profile->mSamplingRates[0] == 0) {
3204 reply = mpClientInterface->getParameters(output,
3205 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003206 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003207 reply.string());
3208 value = strpbrk((char *)reply.string(), "=");
3209 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003210 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003211 }
Eric Laurentd4692962014-05-05 18:13:44 -07003212 }
3213 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3214 reply = mpClientInterface->getParameters(output,
3215 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003216 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003217 reply.string());
3218 value = strpbrk((char *)reply.string(), "=");
3219 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003220 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003221 }
Eric Laurentd4692962014-05-05 18:13:44 -07003222 }
3223 if (profile->mChannelMasks[0] == 0) {
3224 reply = mpClientInterface->getParameters(output,
3225 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003226 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003227 reply.string());
3228 value = strpbrk((char *)reply.string(), "=");
3229 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003230 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003231 }
Eric Laurentd4692962014-05-05 18:13:44 -07003232 }
3233 if (((profile->mSamplingRates[0] == 0) &&
3234 (profile->mSamplingRates.size() < 2)) ||
3235 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3236 (profile->mFormats.size() < 2)) ||
3237 ((profile->mChannelMasks[0] == 0) &&
3238 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003239 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003240 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003241 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003242 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3243 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003244 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003245 config.sample_rate = profile->pickSamplingRate();
3246 config.channel_mask = profile->pickChannelMask();
3247 config.format = profile->pickFormat();
3248 config.offload_info.sample_rate = config.sample_rate;
3249 config.offload_info.channel_mask = config.channel_mask;
3250 config.offload_info.format = config.format;
Eric Laurent322b4d22015-04-03 15:57:54 -07003251 status = mpClientInterface->openOutput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003252 &output,
3253 &config,
3254 &desc->mDevice,
3255 address,
3256 &desc->mLatency,
3257 desc->mFlags);
3258 if (status == NO_ERROR) {
3259 desc->mSamplingRate = config.sample_rate;
3260 desc->mChannelMask = config.channel_mask;
3261 desc->mFormat = config.format;
3262 } else {
3263 output = AUDIO_IO_HANDLE_NONE;
3264 }
Eric Laurentd4692962014-05-05 18:13:44 -07003265 }
3266
Eric Laurentcf2c0212014-07-25 16:20:43 -07003267 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003268 addOutput(output, desc);
François Gaffie53615e22015-03-19 09:24:12 +01003269 if (device_distinguishes_on_address(device) && address != "0") {
François Gaffie036e1e92015-03-19 10:16:24 +01003270 sp<AudioPolicyMix> policyMix;
3271 if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003272 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3273 address.string());
3274 }
François Gaffie036e1e92015-03-19 10:16:24 +01003275 policyMix->setOutput(desc);
Jean-Michel Trividacc06f2015-04-08 18:16:39 -07003276 desc->mPolicyMix = policyMix->getMix();
François Gaffie036e1e92015-03-19 10:16:24 +01003277
Eric Laurent87ffa392015-05-22 10:32:38 -07003278 } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
3279 hasPrimaryOutput()) {
Eric Laurentc722f302014-12-10 11:21:49 -08003280 // no duplicated output for direct outputs and
3281 // outputs used by dynamic policy mixes
Eric Laurentcf2c0212014-07-25 16:20:43 -07003282 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003283
Eric Laurentd4692962014-05-05 18:13:44 -07003284 // set initial stream volume for device
Eric Laurentc75307b2015-03-17 15:29:32 -07003285 applyStreamVolumes(desc, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003286
Eric Laurentd4692962014-05-05 18:13:44 -07003287 //TODO: configure audio effect output stage here
3288
3289 // open a duplicating output thread for the new output and the primary output
Eric Laurentc75307b2015-03-17 15:29:32 -07003290 duplicatedOutput =
3291 mpClientInterface->openDuplicateOutput(output,
3292 mPrimaryOutput->mIoHandle);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003293 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003294 // add duplicated output descriptor
Eric Laurentc75307b2015-03-17 15:29:32 -07003295 sp<SwAudioOutputDescriptor> dupOutputDesc =
3296 new SwAudioOutputDescriptor(NULL, mpClientInterface);
3297 dupOutputDesc->mOutput1 = mPrimaryOutput;
3298 dupOutputDesc->mOutput2 = desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003299 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3300 dupOutputDesc->mFormat = desc->mFormat;
3301 dupOutputDesc->mChannelMask = desc->mChannelMask;
3302 dupOutputDesc->mLatency = desc->mLatency;
3303 addOutput(duplicatedOutput, dupOutputDesc);
Eric Laurentc75307b2015-03-17 15:29:32 -07003304 applyStreamVolumes(dupOutputDesc, device, 0, true);
Eric Laurentd4692962014-05-05 18:13:44 -07003305 } else {
3306 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07003307 mPrimaryOutput->mIoHandle, output);
Eric Laurentd4692962014-05-05 18:13:44 -07003308 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003309 removeOutput(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003310 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003311 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003312 }
Eric Laurente552edb2014-03-10 17:42:56 -07003313 }
3314 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003315 } else {
3316 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003317 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003318 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003319 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003320 profiles.removeAt(profile_index);
3321 profile_index--;
3322 } else {
3323 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003324 devDesc->importAudioPort(profile);
3325
François Gaffie53615e22015-03-19 09:24:12 +01003326 if (device_distinguishes_on_address(device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003327 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3328 device, address.string());
Eric Laurentc75307b2015-03-17 15:29:32 -07003329 setOutputDevice(desc, device, true/*force*/, 0/*delay*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003330 NULL/*patch handle*/, address.string());
3331 }
Eric Laurente552edb2014-03-10 17:42:56 -07003332 ALOGV("checkOutputsForDevice(): adding output %d", output);
3333 }
3334 }
3335
3336 if (profiles.isEmpty()) {
3337 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3338 return BAD_VALUE;
3339 }
Eric Laurentd4692962014-05-05 18:13:44 -07003340 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003341 // check if one opened output is not needed any more after disconnecting one device
3342 for (size_t i = 0; i < mOutputs.size(); i++) {
3343 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003344 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003345 // exact match on device
François Gaffie53615e22015-03-19 09:24:12 +01003346 if (device_distinguishes_on_address(device) &&
Eric Laurentc75307b2015-03-17 15:29:32 -07003347 (desc->supportedDevices() == device)) {
keunyoung3190e672014-12-30 13:00:52 -08003348 findIoHandlesByAddress(desc, device, address, outputs);
Eric Laurentc75307b2015-03-17 15:29:32 -07003349 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003350 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3351 mOutputs.keyAt(i));
3352 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003353 }
Eric Laurente552edb2014-03-10 17:42:56 -07003354 }
3355 }
Eric Laurentd4692962014-05-05 18:13:44 -07003356 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003357 for (size_t i = 0; i < mHwModules.size(); i++)
3358 {
3359 if (mHwModules[i]->mHandle == 0) {
3360 continue;
3361 }
3362 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3363 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003364 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003365 if (profile->mSupportedDevices.types() & device) {
3366 ALOGV("checkOutputsForDevice(): "
3367 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003368 if (profile->mSamplingRates[0] == 0) {
3369 profile->mSamplingRates.clear();
3370 profile->mSamplingRates.add(0);
3371 }
3372 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3373 profile->mFormats.clear();
3374 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3375 }
3376 if (profile->mChannelMasks[0] == 0) {
3377 profile->mChannelMasks.clear();
3378 profile->mChannelMasks.add(0);
3379 }
3380 }
3381 }
3382 }
3383 }
3384 return NO_ERROR;
3385}
3386
Eric Laurentd4692962014-05-05 18:13:44 -07003387status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01003388 audio_policy_dev_state_t state,
3389 SortedVector<audio_io_handle_t>& inputs,
3390 const String8 address)
Eric Laurentd4692962014-05-05 18:13:44 -07003391{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003392 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003393 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3394 // first list already open inputs that can be routed to this device
3395 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3396 desc = mInputs.valueAt(input_index);
3397 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3398 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3399 inputs.add(mInputs.keyAt(input_index));
3400 }
3401 }
3402
3403 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003404 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003405 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3406 {
3407 if (mHwModules[module_idx]->mHandle == 0) {
3408 continue;
3409 }
3410 for (size_t profile_index = 0;
3411 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3412 profile_index++)
3413 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003414 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3415
3416 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
François Gaffie53615e22015-03-19 09:24:12 +01003417 if (!device_distinguishes_on_address(device) ||
Eric Laurent275e8e92014-11-30 15:14:47 -08003418 address == profile->mSupportedDevices[0]->mAddress) {
3419 profiles.add(profile);
3420 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3421 profile_index, module_idx);
3422 }
Eric Laurentd4692962014-05-05 18:13:44 -07003423 }
3424 }
3425 }
3426
3427 if (profiles.isEmpty() && inputs.isEmpty()) {
3428 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3429 return BAD_VALUE;
3430 }
3431
3432 // open inputs for matching profiles if needed. Direct inputs are also opened to
3433 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3434 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3435
Eric Laurent1c333e22014-05-20 10:48:17 -07003436 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003437 // nothing to do if one input is already opened for this profile
3438 size_t input_index;
3439 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3440 desc = mInputs.valueAt(input_index);
3441 if (desc->mProfile == profile) {
3442 break;
3443 }
3444 }
3445 if (input_index != mInputs.size()) {
3446 continue;
3447 }
3448
3449 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3450 desc = new AudioInputDescriptor(profile);
3451 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003452 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3453 config.sample_rate = desc->mSamplingRate;
3454 config.channel_mask = desc->mChannelMask;
3455 config.format = desc->mFormat;
3456 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent322b4d22015-04-03 15:57:54 -07003457 status_t status = mpClientInterface->openInput(profile->getModuleHandle(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07003458 &input,
3459 &config,
3460 &desc->mDevice,
3461 address,
3462 AUDIO_SOURCE_MIC,
3463 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003464
Eric Laurentcf2c0212014-07-25 16:20:43 -07003465 if (status == NO_ERROR) {
3466 desc->mSamplingRate = config.sample_rate;
3467 desc->mChannelMask = config.channel_mask;
3468 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003469
Eric Laurentd4692962014-05-05 18:13:44 -07003470 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003471 char *param = audio_device_address_to_parameter(device, address);
3472 mpClientInterface->setParameters(input, String8(param));
3473 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003474 }
3475
3476 // Here is where we step through and resolve any "dynamic" fields
3477 String8 reply;
3478 char *value;
3479 if (profile->mSamplingRates[0] == 0) {
3480 reply = mpClientInterface->getParameters(input,
3481 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3482 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3483 reply.string());
3484 value = strpbrk((char *)reply.string(), "=");
3485 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003486 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003487 }
3488 }
3489 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3490 reply = mpClientInterface->getParameters(input,
3491 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3492 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3493 value = strpbrk((char *)reply.string(), "=");
3494 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003495 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003496 }
3497 }
3498 if (profile->mChannelMasks[0] == 0) {
3499 reply = mpClientInterface->getParameters(input,
3500 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3501 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3502 reply.string());
3503 value = strpbrk((char *)reply.string(), "=");
3504 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003505 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003506 }
3507 }
3508 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3509 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3510 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3511 ALOGW("checkInputsForDevice() direct input missing param");
3512 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003513 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003514 }
3515
3516 if (input != 0) {
3517 addInput(input, desc);
3518 }
3519 } // endif input != 0
3520
Eric Laurentcf2c0212014-07-25 16:20:43 -07003521 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003522 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003523 profiles.removeAt(profile_index);
3524 profile_index--;
3525 } else {
3526 inputs.add(input);
3527 ALOGV("checkInputsForDevice(): adding input %d", input);
3528 }
3529 } // end scan profiles
3530
3531 if (profiles.isEmpty()) {
3532 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3533 return BAD_VALUE;
3534 }
3535 } else {
3536 // Disconnect
3537 // check if one opened input is not needed any more after disconnecting one device
3538 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3539 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003540 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3541 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003542 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3543 mInputs.keyAt(input_index));
3544 inputs.add(mInputs.keyAt(input_index));
3545 }
3546 }
3547 // Clear any profiles associated with the disconnected device.
3548 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3549 if (mHwModules[module_index]->mHandle == 0) {
3550 continue;
3551 }
3552 for (size_t profile_index = 0;
3553 profile_index < mHwModules[module_index]->mInputProfiles.size();
3554 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003555 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003556 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003557 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003558 profile_index, module_index);
3559 if (profile->mSamplingRates[0] == 0) {
3560 profile->mSamplingRates.clear();
3561 profile->mSamplingRates.add(0);
3562 }
3563 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3564 profile->mFormats.clear();
3565 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3566 }
3567 if (profile->mChannelMasks[0] == 0) {
3568 profile->mChannelMasks.clear();
3569 profile->mChannelMasks.add(0);
3570 }
3571 }
3572 }
3573 }
3574 } // end disconnect
3575
3576 return NO_ERROR;
3577}
3578
3579
Eric Laurente0720872014-03-11 09:30:41 -07003580void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003581{
3582 ALOGV("closeOutput(%d)", output);
3583
Eric Laurentc75307b2015-03-17 15:29:32 -07003584 sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003585 if (outputDesc == NULL) {
3586 ALOGW("closeOutput() unknown output %d", output);
3587 return;
3588 }
François Gaffie036e1e92015-03-19 10:16:24 +01003589 mPolicyMixes.closeOutput(outputDesc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003590
Eric Laurente552edb2014-03-10 17:42:56 -07003591 // look for duplicated outputs connected to the output being removed.
3592 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003593 sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003594 if (dupOutputDesc->isDuplicated() &&
3595 (dupOutputDesc->mOutput1 == outputDesc ||
3596 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003597 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003598 if (dupOutputDesc->mOutput1 == outputDesc) {
3599 outputDesc2 = dupOutputDesc->mOutput2;
3600 } else {
3601 outputDesc2 = dupOutputDesc->mOutput1;
3602 }
3603 // As all active tracks on duplicated output will be deleted,
3604 // and as they were also referenced on the other output, the reference
3605 // count for their stream type must be adjusted accordingly on
3606 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003607 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003608 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003609 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003610 }
3611 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3612 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3613
3614 mpClientInterface->closeOutput(duplicatedOutput);
François Gaffie53615e22015-03-19 09:24:12 +01003615 removeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003616 }
3617 }
3618
Eric Laurent05b90f82014-08-27 15:32:29 -07003619 nextAudioPortGeneration();
3620
3621 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3622 if (index >= 0) {
3623 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3624 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3625 mAudioPatches.removeItemsAt(index);
3626 mpClientInterface->onAudioPatchListUpdate();
3627 }
3628
Eric Laurente552edb2014-03-10 17:42:56 -07003629 AudioParameter param;
3630 param.add(String8("closing"), String8("true"));
3631 mpClientInterface->setParameters(output, param.toString());
3632
3633 mpClientInterface->closeOutput(output);
François Gaffie53615e22015-03-19 09:24:12 +01003634 removeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003635 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003636}
3637
3638void AudioPolicyManager::closeInput(audio_io_handle_t input)
3639{
3640 ALOGV("closeInput(%d)", input);
3641
3642 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3643 if (inputDesc == NULL) {
3644 ALOGW("closeInput() unknown input %d", input);
3645 return;
3646 }
3647
Eric Laurent6a94d692014-05-20 11:18:06 -07003648 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003649
3650 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3651 if (index >= 0) {
3652 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3653 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3654 mAudioPatches.removeItemsAt(index);
3655 mpClientInterface->onAudioPatchListUpdate();
3656 }
3657
3658 mpClientInterface->closeInput(input);
3659 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003660}
3661
Eric Laurentc75307b2015-03-17 15:29:32 -07003662SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(
3663 audio_devices_t device,
3664 SwAudioOutputCollection openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003665{
3666 SortedVector<audio_io_handle_t> outputs;
3667
3668 ALOGVV("getOutputsForDevice() device %04x", device);
3669 for (size_t i = 0; i < openOutputs.size(); i++) {
3670 ALOGVV("output %d isDuplicated=%d device=%04x",
Eric Laurent8c7e6da2015-04-21 17:37:00 -07003671 i, openOutputs.valueAt(i)->isDuplicated(),
3672 openOutputs.valueAt(i)->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07003673 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3674 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3675 outputs.add(openOutputs.keyAt(i));
3676 }
3677 }
3678 return outputs;
3679}
3680
Eric Laurente0720872014-03-11 09:30:41 -07003681bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
François Gaffie53615e22015-03-19 09:24:12 +01003682 SortedVector<audio_io_handle_t>& outputs2)
Eric Laurente552edb2014-03-10 17:42:56 -07003683{
3684 if (outputs1.size() != outputs2.size()) {
3685 return false;
3686 }
3687 for (size_t i = 0; i < outputs1.size(); i++) {
3688 if (outputs1[i] != outputs2[i]) {
3689 return false;
3690 }
3691 }
3692 return true;
3693}
3694
Eric Laurente0720872014-03-11 09:30:41 -07003695void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003696{
3697 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3698 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3699 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3700 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3701
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003702 // also take into account external policy-related changes: add all outputs which are
3703 // associated with policies in the "before" and "after" output vectors
3704 ALOGVV("checkOutputForStrategy(): policy related outputs");
3705 for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003706 const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003707 if (desc != 0 && desc->mPolicyMix != NULL) {
3708 srcOutputs.add(desc->mIoHandle);
3709 ALOGVV(" previous outputs: adding %d", desc->mIoHandle);
3710 }
3711 }
3712 for (size_t i = 0 ; i < mOutputs.size() ; i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003713 const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivife472e22014-12-16 14:23:13 -08003714 if (desc != 0 && desc->mPolicyMix != NULL) {
3715 dstOutputs.add(desc->mIoHandle);
3716 ALOGVV(" new outputs: adding %d", desc->mIoHandle);
3717 }
3718 }
3719
Eric Laurente552edb2014-03-10 17:42:56 -07003720 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3721 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3722 strategy, srcOutputs[0], dstOutputs[0]);
3723 // mute strategy while moving tracks from one output to another
3724 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003725 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003726 if (isStrategyActive(desc, strategy)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07003727 setStrategyMute(strategy, true, desc);
3728 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07003729 }
3730 }
3731
3732 // Move effects associated to this strategy from previous output to new output
3733 if (strategy == STRATEGY_MEDIA) {
3734 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3735 SortedVector<audio_io_handle_t> moved;
3736 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003737 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3738 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3739 effectDesc->mIo != fxOutput) {
3740 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003741 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3742 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003743 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003744 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003745 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003746 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003747 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003748 }
3749 }
3750 }
3751 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003752 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003753 if (i == AUDIO_STREAM_PATCH) {
3754 continue;
3755 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003756 if (getStrategy((audio_stream_type_t)i) == strategy) {
3757 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003758 }
3759 }
3760 }
3761}
3762
Eric Laurente0720872014-03-11 09:30:41 -07003763void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003764{
François Gaffie2110e042015-03-24 08:41:51 +01003765 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003766 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003767 checkOutputForStrategy(STRATEGY_PHONE);
François Gaffie2110e042015-03-24 08:41:51 +01003768 if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jon Eklund966095e2014-09-09 15:39:49 -05003769 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07003770 checkOutputForStrategy(STRATEGY_SONIFICATION);
3771 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003772 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07003773 checkOutputForStrategy(STRATEGY_MEDIA);
3774 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08003775 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07003776}
3777
Eric Laurente0720872014-03-11 09:30:41 -07003778void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003779{
François Gaffie53615e22015-03-19 09:24:12 +01003780 audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput();
Eric Laurente552edb2014-03-10 17:42:56 -07003781 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003782 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003783 return;
3784 }
3785
Eric Laurent3a4311c2014-03-17 12:00:47 -07003786 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08003787 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
3788 ~AUDIO_DEVICE_BIT_IN) != 0) ||
3789 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07003790 // suspend A2DP output if:
3791 // (NOT already suspended) &&
3792 // ((SCO device is connected &&
3793 // (forced usage for communication || for record is SCO))) ||
3794 // (phone state is ringing || in call)
3795 //
3796 // restore A2DP output if:
3797 // (Already suspended) &&
3798 // ((SCO device is NOT connected ||
3799 // (forced usage NOT for communication && NOT for record is SCO))) &&
3800 // (phone state is NOT ringing && NOT in call)
3801 //
3802 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003803 if ((!isScoConnected ||
François Gaffie2110e042015-03-24 08:41:51 +01003804 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) &&
3805 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) &&
3806 ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) &&
3807 (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003808
3809 mpClientInterface->restoreOutput(a2dpOutput);
3810 mA2dpSuspended = false;
3811 }
3812 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003813 if ((isScoConnected &&
François Gaffie2110e042015-03-24 08:41:51 +01003814 ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) ||
3815 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) ||
3816 ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) ||
3817 (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003818
3819 mpClientInterface->suspendOutput(a2dpOutput);
3820 mA2dpSuspended = true;
3821 }
3822 }
3823}
3824
Eric Laurentc75307b2015-03-17 15:29:32 -07003825audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
3826 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003827{
3828 audio_devices_t device = AUDIO_DEVICE_NONE;
3829
Eric Laurent6a94d692014-05-20 11:18:06 -07003830 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3831 if (index >= 0) {
3832 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3833 if (patchDesc->mUid != mUidCached) {
3834 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3835 outputDesc->device(), outputDesc->mPatchHandle);
3836 return outputDesc->device();
3837 }
3838 }
3839
Eric Laurente552edb2014-03-10 17:42:56 -07003840 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05003841 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003842 // use device for strategy enforced audible
3843 // 2: we are in call or the strategy phone is active on the output:
3844 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05003845 // 3: the strategy for enforced audible is active but not enforced on the output:
3846 // use the device for strategy enforced audible
3847 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003848 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05003849 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003850 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08003851 // 6: the strategy accessibility is active on the output:
3852 // use device for strategy accessibility
3853 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003854 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08003855 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07003856 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08003857 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003858 // use device for strategy t-t-s
François Gaffiead3183e2015-03-18 16:55:35 +01003859 if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01003860 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07003861 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3862 } else if (isInCall() ||
François Gaffiead3183e2015-03-18 16:55:35 +01003863 isStrategyActive(outputDesc, STRATEGY_PHONE)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003864 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003865 } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
Jon Eklund966095e2014-09-09 15:39:49 -05003866 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003867 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003868 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003869 } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003870 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003871 } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003872 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003873 } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003874 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003875 } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003876 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003877 } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003878 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
François Gaffiead3183e2015-03-18 16:55:35 +01003879 } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08003880 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07003881 }
3882
Eric Laurent1c333e22014-05-20 10:48:17 -07003883 ALOGV("getNewOutputDevice() selected device %x", device);
3884 return device;
3885}
3886
3887audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3888{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003889 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003890
3891 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3892 if (index >= 0) {
3893 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3894 if (patchDesc->mUid != mUidCached) {
3895 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3896 inputDesc->mDevice, inputDesc->mPatchHandle);
3897 return inputDesc->mDevice;
3898 }
3899 }
3900
Eric Laurentc73ca6e2014-12-12 14:34:22 -08003901 audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource);
Eric Laurent1c333e22014-05-20 10:48:17 -07003902
Eric Laurente552edb2014-03-10 17:42:56 -07003903 return device;
3904}
3905
Eric Laurente0720872014-03-11 09:30:41 -07003906uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003907 return (uint32_t)getStrategy(stream);
3908}
3909
Eric Laurente0720872014-03-11 09:30:41 -07003910audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003911 // By checking the range of stream before calling getStrategy, we avoid
3912 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3913 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08003914 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003915 return AUDIO_DEVICE_NONE;
3916 }
3917 audio_devices_t devices;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08003918 routing_strategy strategy = getStrategy(stream);
Eric Laurent6a94d692014-05-20 11:18:06 -07003919 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3920 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3921 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003922 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
François Gaffiead3183e2015-03-18 16:55:35 +01003923 if (isStrategyActive(outputDesc, strategy)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003924 devices = outputDesc->device();
3925 break;
3926 }
Eric Laurente552edb2014-03-10 17:42:56 -07003927 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05003928
3929 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
3930 and doesn't really need to.*/
3931 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3932 devices |= AUDIO_DEVICE_OUT_SPEAKER;
3933 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3934 }
3935
Eric Laurente552edb2014-03-10 17:42:56 -07003936 return devices;
3937}
3938
François Gaffie2110e042015-03-24 08:41:51 +01003939routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const
3940{
Eric Laurent223fd5c2014-11-11 13:43:36 -08003941 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
François Gaffie2110e042015-03-24 08:41:51 +01003942 return mEngine->getStrategyForStream(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003943}
3944
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003945uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3946 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003947 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
3948 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
3949 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003950 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3951 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3952 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003953 // usage to strategy mapping
François Gaffie2110e042015-03-24 08:41:51 +01003954 return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003955}
3956
Eric Laurente0720872014-03-11 09:30:41 -07003957void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003958 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003959 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003960 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3961 updateDevicesAndOutputs();
3962 break;
3963 default:
3964 break;
3965 }
3966}
3967
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07003968uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
3969 switch(event) {
3970 case STARTING_OUTPUT:
3971 mBeaconMuteRefCount++;
3972 break;
3973 case STOPPING_OUTPUT:
3974 if (mBeaconMuteRefCount > 0) {
3975 mBeaconMuteRefCount--;
3976 }
3977 break;
3978 case STARTING_BEACON:
3979 mBeaconPlayingRefCount++;
3980 break;
3981 case STOPPING_BEACON:
3982 if (mBeaconPlayingRefCount > 0) {
3983 mBeaconPlayingRefCount--;
3984 }
3985 break;
3986 }
3987
3988 if (mBeaconMuteRefCount > 0) {
3989 // any playback causes beacon to be muted
3990 return setBeaconMute(true);
3991 } else {
3992 // no other playback: unmute when beacon starts playing, mute when it stops
3993 return setBeaconMute(mBeaconPlayingRefCount == 0);
3994 }
3995}
3996
3997uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
3998 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
3999 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4000 // keep track of muted state to avoid repeating mute/unmute operations
4001 if (mBeaconMuted != mute) {
4002 // mute/unmute AUDIO_STREAM_TTS on all outputs
4003 ALOGV("\t muting %d", mute);
4004 uint32_t maxLatency = 0;
4005 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004006 sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004007 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
Eric Laurentc75307b2015-03-17 15:29:32 -07004008 desc,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004009 0 /*delay*/, AUDIO_DEVICE_NONE);
4010 const uint32_t latency = desc->latency() * 2;
4011 if (latency > maxLatency) {
4012 maxLatency = latency;
4013 }
4014 }
4015 mBeaconMuted = mute;
4016 return maxLatency;
4017 }
4018 return 0;
4019}
4020
Eric Laurente0720872014-03-11 09:30:41 -07004021audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
François Gaffie53615e22015-03-19 09:24:12 +01004022 bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004023{
Paul McLeanaa981192015-03-21 09:55:15 -07004024 // Routing
4025 // see if we have an explicit route
4026 // scan the whole RouteMap, for each entry, convert the stream type to a strategy
4027 // (getStrategy(stream)).
4028 // if the strategy from the stream type in the RouteMap is the same as the argument above,
4029 // and activity count is non-zero
4030 // the device = the device from the descriptor in the RouteMap, and exit.
4031 for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) {
4032 sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex);
4033 routing_strategy strat = getStrategy(route->mStreamType);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004034 if (strat == strategy && route->isActive()) {
Paul McLeanaa981192015-03-21 09:55:15 -07004035 return route->mDeviceDescriptor->type();
4036 }
4037 }
4038
Eric Laurente552edb2014-03-10 17:42:56 -07004039 if (fromCache) {
4040 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4041 strategy, mDeviceForStrategy[strategy]);
4042 return mDeviceForStrategy[strategy];
4043 }
François Gaffie2110e042015-03-24 08:41:51 +01004044 return mEngine->getDeviceForStrategy(strategy);
Eric Laurente552edb2014-03-10 17:42:56 -07004045}
4046
Eric Laurente0720872014-03-11 09:30:41 -07004047void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004048{
4049 for (int i = 0; i < NUM_STRATEGIES; i++) {
4050 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4051 }
4052 mPreviousOutputs = mOutputs;
4053}
4054
Eric Laurent1f2f2232014-06-02 12:01:23 -07004055uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004056 audio_devices_t prevDevice,
4057 uint32_t delayMs)
4058{
4059 // mute/unmute strategies using an incompatible device combination
4060 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4061 // if unmuting, unmute only after the specified delay
4062 if (outputDesc->isDuplicated()) {
4063 return 0;
4064 }
4065
4066 uint32_t muteWaitMs = 0;
4067 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004068 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004069
4070 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4071 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurentc75307b2015-03-17 15:29:32 -07004072 curDevice = curDevice & outputDesc->supportedDevices();
Eric Laurente552edb2014-03-10 17:42:56 -07004073 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4074 bool doMute = false;
4075
4076 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4077 doMute = true;
4078 outputDesc->mStrategyMutedByDevice[i] = true;
4079 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4080 doMute = true;
4081 outputDesc->mStrategyMutedByDevice[i] = false;
4082 }
Eric Laurent99401132014-05-07 19:48:15 -07004083 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004084 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004085 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004086 // skip output if it does not share any device with current output
4087 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4088 == AUDIO_DEVICE_NONE) {
4089 continue;
4090 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004091 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)",
4092 mute ? "muting" : "unmuting", i, curDevice);
4093 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs);
François Gaffiead3183e2015-03-18 16:55:35 +01004094 if (isStrategyActive(desc, (routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004095 if (mute) {
4096 // FIXME: should not need to double latency if volume could be applied
4097 // immediately by the audioflinger mixer. We must account for the delay
4098 // between now and the next time the audioflinger thread for this output
4099 // will process a buffer (which corresponds to one buffer size,
4100 // usually 1/2 or 1/4 of the latency).
4101 if (muteWaitMs < desc->latency() * 2) {
4102 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004103 }
4104 }
4105 }
4106 }
4107 }
4108 }
4109
Eric Laurent99401132014-05-07 19:48:15 -07004110 // temporary mute output if device selection changes to avoid volume bursts due to
4111 // different per device volumes
4112 if (outputDesc->isActive() && (device != prevDevice)) {
4113 if (muteWaitMs < outputDesc->latency() * 2) {
4114 muteWaitMs = outputDesc->latency() * 2;
4115 }
4116 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
François Gaffiead3183e2015-03-18 16:55:35 +01004117 if (isStrategyActive(outputDesc, (routing_strategy)i)) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004118 setStrategyMute((routing_strategy)i, true, outputDesc);
Eric Laurent99401132014-05-07 19:48:15 -07004119 // do tempMute unmute after twice the mute wait time
Eric Laurentc75307b2015-03-17 15:29:32 -07004120 setStrategyMute((routing_strategy)i, false, outputDesc,
Eric Laurent99401132014-05-07 19:48:15 -07004121 muteWaitMs *2, device);
4122 }
4123 }
4124 }
4125
Eric Laurente552edb2014-03-10 17:42:56 -07004126 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4127 if (muteWaitMs > delayMs) {
4128 muteWaitMs -= delayMs;
4129 usleep(muteWaitMs * 1000);
4130 return muteWaitMs;
4131 }
4132 return 0;
4133}
4134
Eric Laurentc75307b2015-03-17 15:29:32 -07004135uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004136 audio_devices_t device,
4137 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004138 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004139 audio_patch_handle_t *patchHandle,
4140 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004141{
Eric Laurentc75307b2015-03-17 15:29:32 -07004142 ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004143 AudioParameter param;
4144 uint32_t muteWaitMs;
4145
4146 if (outputDesc->isDuplicated()) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004147 muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs);
4148 muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004149 return muteWaitMs;
4150 }
4151 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4152 // output profile
Eric Laurentc75307b2015-03-17 15:29:32 -07004153 if ((device != AUDIO_DEVICE_NONE) &&
4154 ((device & outputDesc->supportedDevices()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004155 return 0;
4156 }
4157
4158 // filter devices according to output selected
Eric Laurentc75307b2015-03-17 15:29:32 -07004159 device = (audio_devices_t)(device & outputDesc->supportedDevices());
Eric Laurente552edb2014-03-10 17:42:56 -07004160
4161 audio_devices_t prevDevice = outputDesc->mDevice;
4162
Paul McLeanaa981192015-03-21 09:55:15 -07004163 ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004164
4165 if (device != AUDIO_DEVICE_NONE) {
4166 outputDesc->mDevice = device;
4167 }
4168 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4169
4170 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004171 // the requested device is AUDIO_DEVICE_NONE
4172 // OR the requested device is the same as current device
4173 // AND force is not specified
4174 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004175 // Doing this check here allows the caller to call setOutputDevice() without conditions
Paul McLeanaa981192015-03-21 09:55:15 -07004176 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) &&
4177 !force &&
4178 outputDesc->mPatchHandle != 0) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004179 ALOGV("setOutputDevice() setting same device 0x%04x or null device", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004180 return muteWaitMs;
4181 }
4182
4183 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004184
Eric Laurente552edb2014-03-10 17:42:56 -07004185 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004186 if (device == AUDIO_DEVICE_NONE) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004187 resetOutputDevice(outputDesc, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004188 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004189 DeviceVector deviceList = (address == NULL) ?
4190 mAvailableOutputDevices.getDevicesFromType(device)
4191 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004192 if (!deviceList.isEmpty()) {
4193 struct audio_patch patch;
4194 outputDesc->toAudioPortConfig(&patch.sources[0]);
4195 patch.num_sources = 1;
4196 patch.num_sinks = 0;
4197 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4198 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004199 patch.num_sinks++;
4200 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004201 ssize_t index;
4202 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4203 index = mAudioPatches.indexOfKey(*patchHandle);
4204 } else {
4205 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4206 }
4207 sp< AudioPatch> patchDesc;
4208 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4209 if (index >= 0) {
4210 patchDesc = mAudioPatches.valueAt(index);
4211 afPatchHandle = patchDesc->mAfPatchHandle;
4212 }
4213
Eric Laurent1c333e22014-05-20 10:48:17 -07004214 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004215 &afPatchHandle,
4216 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004217 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4218 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004219 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004220 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004221 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004222 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004223 addAudioPatch(patchDesc->mHandle, patchDesc);
4224 } else {
4225 patchDesc->mPatch = patch;
4226 }
4227 patchDesc->mAfPatchHandle = afPatchHandle;
4228 patchDesc->mUid = mUidCached;
4229 if (patchHandle) {
4230 *patchHandle = patchDesc->mHandle;
4231 }
4232 outputDesc->mPatchHandle = patchDesc->mHandle;
4233 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004234 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004235 }
4236 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004237
4238 // inform all input as well
4239 for (size_t i = 0; i < mInputs.size(); i++) {
4240 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
François Gaffie53615e22015-03-19 09:24:12 +01004241 if (!is_virtual_input_device(inputDescriptor->mDevice)) {
bryant_liuf5e7e792014-08-19 20:07:05 +08004242 AudioParameter inputCmd = AudioParameter();
4243 ALOGV("%s: inform input %d of device:%d", __func__,
4244 inputDescriptor->mIoHandle, device);
4245 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4246 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4247 inputCmd.toString(),
4248 delayMs);
4249 }
4250 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004251 }
Eric Laurente552edb2014-03-10 17:42:56 -07004252
4253 // update stream volumes according to new device
Eric Laurentc75307b2015-03-17 15:29:32 -07004254 applyStreamVolumes(outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004255
4256 return muteWaitMs;
4257}
4258
Eric Laurentc75307b2015-03-17 15:29:32 -07004259status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Eric Laurent6a94d692014-05-20 11:18:06 -07004260 int delayMs,
4261 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004262{
Eric Laurent6a94d692014-05-20 11:18:06 -07004263 ssize_t index;
4264 if (patchHandle) {
4265 index = mAudioPatches.indexOfKey(*patchHandle);
4266 } else {
4267 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4268 }
4269 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004270 return INVALID_OPERATION;
4271 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004272 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4273 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004274 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4275 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004276 removeAudioPatch(patchDesc->mHandle);
4277 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004278 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004279 return status;
4280}
4281
4282status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4283 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004284 bool force,
4285 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004286{
4287 status_t status = NO_ERROR;
4288
Eric Laurent1f2f2232014-06-02 12:01:23 -07004289 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004290 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4291 inputDesc->mDevice = device;
4292
4293 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4294 if (!deviceList.isEmpty()) {
4295 struct audio_patch patch;
4296 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004297 // AUDIO_SOURCE_HOTWORD is for internal use only:
4298 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004299 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4300 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004301 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4302 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004303 patch.num_sinks = 1;
4304 //only one input device for now
4305 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004306 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004307 ssize_t index;
4308 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4309 index = mAudioPatches.indexOfKey(*patchHandle);
4310 } else {
4311 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4312 }
4313 sp< AudioPatch> patchDesc;
4314 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4315 if (index >= 0) {
4316 patchDesc = mAudioPatches.valueAt(index);
4317 afPatchHandle = patchDesc->mAfPatchHandle;
4318 }
4319
Eric Laurent1c333e22014-05-20 10:48:17 -07004320 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004321 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004322 0);
4323 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004324 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004325 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004326 if (index < 0) {
François Gaffie98cc1912015-03-18 17:52:40 +01004327 patchDesc = new AudioPatch(&patch, mUidCached);
Eric Laurent6a94d692014-05-20 11:18:06 -07004328 addAudioPatch(patchDesc->mHandle, patchDesc);
4329 } else {
4330 patchDesc->mPatch = patch;
4331 }
4332 patchDesc->mAfPatchHandle = afPatchHandle;
4333 patchDesc->mUid = mUidCached;
4334 if (patchHandle) {
4335 *patchHandle = patchDesc->mHandle;
4336 }
4337 inputDesc->mPatchHandle = patchDesc->mHandle;
4338 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004339 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004340 }
4341 }
4342 }
4343 return status;
4344}
4345
Eric Laurent6a94d692014-05-20 11:18:06 -07004346status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4347 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004348{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004349 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004350 ssize_t index;
4351 if (patchHandle) {
4352 index = mAudioPatches.indexOfKey(*patchHandle);
4353 } else {
4354 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4355 }
4356 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004357 return INVALID_OPERATION;
4358 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004359 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4360 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004361 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4362 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004363 removeAudioPatch(patchDesc->mHandle);
4364 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004365 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004366 return status;
4367}
4368
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08004369sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
François Gaffie53615e22015-03-19 09:24:12 +01004370 String8 address,
4371 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -07004372 audio_format_t& format,
4373 audio_channel_mask_t& channelMask,
François Gaffie53615e22015-03-19 09:24:12 +01004374 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004375{
4376 // Choose an input profile based on the requested capture parameters: select the first available
4377 // profile supporting all requested parameters.
Andy Hungf129b032015-04-07 13:45:50 -07004378 //
4379 // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return
4380 // the best matching profile, not the first one.
Eric Laurente552edb2014-03-10 17:42:56 -07004381
4382 for (size_t i = 0; i < mHwModules.size(); i++)
4383 {
4384 if (mHwModules[i]->mHandle == 0) {
4385 continue;
4386 }
4387 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
4388 {
Eric Laurent1c333e22014-05-20 10:48:17 -07004389 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07004390 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08004391 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07004392 &samplingRate /*updatedSamplingRate*/,
Andy Hungf129b032015-04-07 13:45:50 -07004393 format,
4394 &format /*updatedFormat*/,
4395 channelMask,
4396 &channelMask /*updatedChannelMask*/,
4397 (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08004398
Eric Laurente552edb2014-03-10 17:42:56 -07004399 return profile;
4400 }
4401 }
4402 }
4403 return NULL;
4404}
4405
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004406
4407audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource,
François Gaffie53615e22015-03-19 09:24:12 +01004408 AudioMix **policyMix)
Eric Laurente552edb2014-03-10 17:42:56 -07004409{
François Gaffie036e1e92015-03-19 10:16:24 +01004410 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
4411 audio_devices_t selectedDeviceFromMix =
4412 mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix);
Eric Laurent275e8e92014-11-30 15:14:47 -08004413
François Gaffie036e1e92015-03-19 10:16:24 +01004414 if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) {
4415 return selectedDeviceFromMix;
Eric Laurent275e8e92014-11-30 15:14:47 -08004416 }
Eric Laurentc73ca6e2014-12-12 14:34:22 -08004417 return getDeviceForInputSource(inputSource);
4418}
4419
4420audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
4421{
Paul McLean466dc8e2015-04-17 13:15:36 -06004422 for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) {
4423 sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex);
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004424 if (inputSource == route->mSource && route->isActive()) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004425 return route->mDeviceDescriptor->type();
4426 }
4427 }
4428
4429 return mEngine->getDeviceForInputSource(inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -07004430}
4431
Eric Laurente0720872014-03-11 09:30:41 -07004432float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004433 int index,
4434 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004435{
Eric Laurentffbc80f2015-03-18 18:30:19 -07004436 float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index);
Eric Laurente552edb2014-03-10 17:42:56 -07004437
4438 // if a headset is connected, apply the following rules to ring tones and notifications
4439 // to avoid sound level bursts in user's ears:
4440 // - always attenuate ring tones and notifications volume by 6dB
4441 // - if music is playing, always limit the volume to current music volume,
4442 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004443 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004444 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4445 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4446 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4447 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4448 ((stream_strategy == STRATEGY_SONIFICATION)
4449 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004450 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004451 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
François Gaffie2110e042015-03-24 08:41:51 +01004452 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) &&
4453 mStreams.canBeMuted(stream)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004454 volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB;
Eric Laurente552edb2014-03-10 17:42:56 -07004455 // when the phone is ringing we must consider that music could have been paused just before
4456 // by the music application and behave as if music was active if the last music track was
4457 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004458 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004459 mLimitRingtoneVolume) {
4460 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004461 float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC,
4462 mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004463 musicDevice);
Eric Laurentffbc80f2015-03-18 18:30:19 -07004464 float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ?
4465 musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB;
4466 if (volumeDb > minVolDB) {
4467 volumeDb = minVolDB;
4468 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB);
Eric Laurente552edb2014-03-10 17:42:56 -07004469 }
4470 }
4471 }
4472
Eric Laurentffbc80f2015-03-18 18:30:19 -07004473 return volumeDb;
Eric Laurente552edb2014-03-10 17:42:56 -07004474}
4475
Eric Laurente0720872014-03-11 09:30:41 -07004476status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004477 int index,
4478 const sp<AudioOutputDescriptor>& outputDesc,
4479 audio_devices_t device,
4480 int delayMs,
4481 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004482{
Eric Laurente552edb2014-03-10 17:42:56 -07004483 // do not change actual stream volume if the stream is muted
Eric Laurentc75307b2015-03-17 15:29:32 -07004484 if (outputDesc->mMuteCount[stream] != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07004485 ALOGVV("checkAndSetVolume() stream %d muted count %d",
Eric Laurentc75307b2015-03-17 15:29:32 -07004486 stream, outputDesc->mMuteCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004487 return NO_ERROR;
4488 }
François Gaffie2110e042015-03-24 08:41:51 +01004489 audio_policy_forced_cfg_t forceUseForComm =
4490 mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
Eric Laurente552edb2014-03-10 17:42:56 -07004491 // do not change in call volume if bluetooth is connected and vice versa
François Gaffie2110e042015-03-24 08:41:51 +01004492 if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
4493 (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004494 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
François Gaffie2110e042015-03-24 08:41:51 +01004495 stream, forceUseForComm);
Eric Laurente552edb2014-03-10 17:42:56 -07004496 return INVALID_OPERATION;
4497 }
4498
Eric Laurentc75307b2015-03-17 15:29:32 -07004499 if (device == AUDIO_DEVICE_NONE) {
4500 device = outputDesc->device();
4501 }
Eric Laurent275e8e92014-11-30 15:14:47 -08004502
Eric Laurentffbc80f2015-03-18 18:30:19 -07004503 float volumeDb = computeVolume(stream, index, device);
Eric Laurentc75307b2015-03-17 15:29:32 -07004504 if (outputDesc->isFixedVolume(device)) {
Eric Laurentffbc80f2015-03-18 18:30:19 -07004505 volumeDb = 0.0f;
Eric Laurent275e8e92014-11-30 15:14:47 -08004506 }
Eric Laurentc75307b2015-03-17 15:29:32 -07004507
Eric Laurentffbc80f2015-03-18 18:30:19 -07004508 outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
Eric Laurente552edb2014-03-10 17:42:56 -07004509
Eric Laurent3b73df72014-03-11 09:06:29 -07004510 if (stream == AUDIO_STREAM_VOICE_CALL ||
4511 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004512 float voiceVolume;
4513 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004514 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004515 voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax();
Eric Laurente552edb2014-03-10 17:42:56 -07004516 } else {
4517 voiceVolume = 1.0;
4518 }
4519
Eric Laurentc75307b2015-03-17 15:29:32 -07004520 if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) {
Eric Laurente552edb2014-03-10 17:42:56 -07004521 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4522 mLastVoiceVolume = voiceVolume;
4523 }
4524 }
4525
4526 return NO_ERROR;
4527}
4528
Eric Laurentc75307b2015-03-17 15:29:32 -07004529void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
4530 audio_devices_t device,
4531 int delayMs,
4532 bool force)
Eric Laurente552edb2014-03-10 17:42:56 -07004533{
Eric Laurentc75307b2015-03-17 15:29:32 -07004534 ALOGVV("applyStreamVolumes() for device %08x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004535
Eric Laurent3b73df72014-03-11 09:06:29 -07004536 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004537 if (stream == AUDIO_STREAM_PATCH) {
4538 continue;
4539 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004540 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004541 mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device),
4542 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004543 device,
4544 delayMs,
4545 force);
4546 }
4547}
4548
Eric Laurente0720872014-03-11 09:30:41 -07004549void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurentc75307b2015-03-17 15:29:32 -07004550 bool on,
4551 const sp<AudioOutputDescriptor>& outputDesc,
4552 int delayMs,
4553 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004554{
4555 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004556 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004557 if (stream == AUDIO_STREAM_PATCH) {
4558 continue;
4559 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004560 if (getStrategy((audio_stream_type_t)stream) == strategy) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004561 setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004562 }
4563 }
4564}
4565
Eric Laurente0720872014-03-11 09:30:41 -07004566void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurentc75307b2015-03-17 15:29:32 -07004567 bool on,
4568 const sp<AudioOutputDescriptor>& outputDesc,
4569 int delayMs,
4570 audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004571{
Eric Laurentc75307b2015-03-17 15:29:32 -07004572 const StreamDescriptor& streamDesc = mStreams.valueFor(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004573 if (device == AUDIO_DEVICE_NONE) {
4574 device = outputDesc->device();
4575 }
4576
Eric Laurentc75307b2015-03-17 15:29:32 -07004577 ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x",
4578 stream, on, outputDesc->mMuteCount[stream], device);
Eric Laurente552edb2014-03-10 17:42:56 -07004579
4580 if (on) {
4581 if (outputDesc->mMuteCount[stream] == 0) {
François Gaffiedfd74092015-03-19 12:10:59 +01004582 if (streamDesc.canBeMuted() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004583 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
François Gaffie2110e042015-03-24 08:41:51 +01004584 (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004585 checkAndSetVolume(stream, 0, outputDesc, device, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004586 }
4587 }
4588 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4589 outputDesc->mMuteCount[stream]++;
4590 } else {
4591 if (outputDesc->mMuteCount[stream] == 0) {
4592 ALOGV("setStreamMute() unmuting non muted stream!");
4593 return;
4594 }
4595 if (--outputDesc->mMuteCount[stream] == 0) {
4596 checkAndSetVolume(stream,
4597 streamDesc.getVolumeIndex(device),
Eric Laurentc75307b2015-03-17 15:29:32 -07004598 outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004599 device,
4600 delayMs);
4601 }
4602 }
4603}
4604
Eric Laurente0720872014-03-11 09:30:41 -07004605void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004606 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004607{
Eric Laurent87ffa392015-05-22 10:32:38 -07004608 if(!hasPrimaryOutput()) {
4609 return;
4610 }
4611
Eric Laurente552edb2014-03-10 17:42:56 -07004612 // if the stream pertains to sonification strategy and we are in call we must
4613 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4614 // in the device used for phone strategy and play the tone if the selected device does not
4615 // interfere with the device used for phone strategy
4616 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4617 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004618 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004619 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4620 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurentc75307b2015-03-17 15:29:32 -07004621 sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004622 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4623 stream, starting, outputDesc->mDevice, stateChange);
4624 if (outputDesc->mRefCount[stream]) {
4625 int muteCount = 1;
4626 if (stateChange) {
4627 muteCount = outputDesc->mRefCount[stream];
4628 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004629 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004630 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4631 for (int i = 0; i < muteCount; i++) {
4632 setStreamMute(stream, starting, mPrimaryOutput);
4633 }
4634 } else {
4635 ALOGV("handleIncallSonification() high visibility");
4636 if (outputDesc->device() &
4637 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4638 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4639 for (int i = 0; i < muteCount; i++) {
4640 setStreamMute(stream, starting, mPrimaryOutput);
4641 }
4642 }
4643 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004644 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4645 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004646 } else {
4647 mpClientInterface->stopTone();
4648 }
4649 }
4650 }
4651 }
4652}
4653
Paul McLeanaa981192015-03-21 09:55:15 -07004654// --- SessionRoute class implementation
4655void AudioPolicyManager::SessionRoute::log(const char* prefix) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004656 ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
4657 prefix, mStreamType, mSource, mSession,
Paul McLeanaa981192015-03-21 09:55:15 -07004658 mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
4659 mRefCount, mActivityCount);
4660}
4661
4662// --- SessionRouteMap class implementation
4663bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session)
4664{
4665 return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4666}
4667
Eric Laurent493404d2015-04-21 15:07:36 -07004668bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session)
4669{
4670 if (indexOfKey(session) >= 0) {
4671 if (valueFor(session)->mChanged) {
4672 valueFor(session)->mChanged = false;
4673 return true;
4674 }
4675 }
4676 return false;
4677}
4678
Paul McLeanaa981192015-03-21 09:55:15 -07004679void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session)
4680{
4681 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4682 if (route != 0) {
4683 ALOG_ASSERT(route->mRefCount > 0);
4684 --route->mRefCount;
4685 if (route->mRefCount <= 0) {
4686 removeItem(session);
4687 }
4688 }
4689}
4690
4691int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session)
4692{
4693 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4694 return route != 0 ? ++(route->mActivityCount) : -1;
4695}
4696
4697int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session)
4698{
4699 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4700 if (route != 0 && route->mActivityCount > 0) {
4701 return --(route->mActivityCount);
4702 } else {
4703 return -1;
4704 }
4705}
4706
4707void AudioPolicyManager::SessionRouteMap::log(const char* caption) {
4708 ALOGI("%s ----", caption);
4709 for(size_t index = 0; index < size(); index++) {
4710 valueAt(index)->log(" ");
4711 }
4712}
4713
Paul McLean466dc8e2015-04-17 13:15:36 -06004714void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session,
4715 audio_stream_type_t streamType,
4716 audio_source_t source,
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004717 sp<DeviceDescriptor> descriptor,
4718 uid_t uid)
Paul McLean466dc8e2015-04-17 13:15:36 -06004719{
4720 if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
4721 ALOGE("Adding Output Route to InputRouteMap");
4722 return;
4723 } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
4724 ALOGE("Adding Input Route to OutputRouteMap");
4725 return;
4726 }
4727
4728 sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
4729
4730 if (route != 0) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004731 if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
4732 ((route->mDeviceDescriptor != 0) &&
4733 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
Paul McLean466dc8e2015-04-17 13:15:36 -06004734 route->mChanged = true;
4735 }
4736 route->mRefCount++;
4737 route->mDeviceDescriptor = descriptor;
4738 } else {
Eric Laurent8c7e6da2015-04-21 17:37:00 -07004739 route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid);
Paul McLean466dc8e2015-04-17 13:15:36 -06004740 route->mRefCount++;
4741 add(session, route);
4742 if (descriptor != 0) {
4743 route->mChanged = true;
4744 }
4745 }
4746}
4747
Eric Laurente0720872014-03-11 09:30:41 -07004748void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004749{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004750 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004751 sp<IOProfile> profile;
Paul McLeane743a472015-01-28 11:07:31 -08004752 sp<DeviceDescriptor> defaultInputDevice =
Eric Laurent7288ab82015-05-06 18:44:02 -07004753 new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004754 mAvailableOutputDevices.add(mDefaultOutputDevice);
4755 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004756
4757 module = new HwModule("primary");
4758
Eric Laurent322b4d22015-04-03 15:57:54 -07004759 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE);
4760 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004761 profile->mSamplingRates.add(44100);
4762 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4763 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004764 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004765 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4766 module->mOutputProfiles.add(profile);
4767
Eric Laurent322b4d22015-04-03 15:57:54 -07004768 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK);
4769 profile->attach(module);
Eric Laurente552edb2014-03-10 17:42:56 -07004770 profile->mSamplingRates.add(8000);
4771 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4772 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004773 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004774 module->mInputProfiles.add(profile);
4775
4776 mHwModules.add(module);
4777}
4778
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004779audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
4780{
4781 // flags to stream type mapping
4782 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4783 return AUDIO_STREAM_ENFORCED_AUDIBLE;
4784 }
4785 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
4786 return AUDIO_STREAM_BLUETOOTH_SCO;
4787 }
Jean-Michel Trivi79ad4382015-01-29 10:49:39 -08004788 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4789 return AUDIO_STREAM_TTS;
4790 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004791
4792 // usage to stream type mapping
4793 switch (attr->usage) {
4794 case AUDIO_USAGE_MEDIA:
4795 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004796 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4797 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004798 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08004799 if (isStreamActive(AUDIO_STREAM_ALARM)) {
4800 return AUDIO_STREAM_ALARM;
4801 }
4802 if (isStreamActive(AUDIO_STREAM_RING)) {
4803 return AUDIO_STREAM_RING;
4804 }
4805 if (isInCall()) {
4806 return AUDIO_STREAM_VOICE_CALL;
4807 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08004808 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004809 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4810 return AUDIO_STREAM_SYSTEM;
4811 case AUDIO_USAGE_VOICE_COMMUNICATION:
4812 return AUDIO_STREAM_VOICE_CALL;
4813
4814 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4815 return AUDIO_STREAM_DTMF;
4816
4817 case AUDIO_USAGE_ALARM:
4818 return AUDIO_STREAM_ALARM;
4819 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4820 return AUDIO_STREAM_RING;
4821
4822 case AUDIO_USAGE_NOTIFICATION:
4823 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4824 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4825 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4826 case AUDIO_USAGE_NOTIFICATION_EVENT:
4827 return AUDIO_STREAM_NOTIFICATION;
4828
4829 case AUDIO_USAGE_UNKNOWN:
4830 default:
4831 return AUDIO_STREAM_MUSIC;
4832 }
4833}
Eric Laurente83b55d2014-11-14 10:06:21 -08004834
François Gaffie53615e22015-03-19 09:24:12 +01004835bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa)
4836{
Eric Laurente83b55d2014-11-14 10:06:21 -08004837 // has flags that map to a strategy?
4838 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
4839 return true;
4840 }
4841
4842 // has known usage?
4843 switch (paa->usage) {
4844 case AUDIO_USAGE_UNKNOWN:
4845 case AUDIO_USAGE_MEDIA:
4846 case AUDIO_USAGE_VOICE_COMMUNICATION:
4847 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4848 case AUDIO_USAGE_ALARM:
4849 case AUDIO_USAGE_NOTIFICATION:
4850 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4851 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4852 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4853 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4854 case AUDIO_USAGE_NOTIFICATION_EVENT:
4855 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4856 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4857 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4858 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08004859 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08004860 break;
4861 default:
4862 return false;
4863 }
4864 return true;
4865}
4866
François Gaffiead3183e2015-03-18 16:55:35 +01004867bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
4868 routing_strategy strategy, uint32_t inPastMs,
4869 nsecs_t sysTime) const
4870{
4871 if ((sysTime == 0) && (inPastMs != 0)) {
4872 sysTime = systemTime();
4873 }
4874 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4875 if (i == AUDIO_STREAM_PATCH) {
4876 continue;
4877 }
4878 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
4879 (NUM_STRATEGIES == strategy)) &&
4880 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
4881 return true;
4882 }
4883 }
4884 return false;
4885}
4886
François Gaffie2110e042015-03-24 08:41:51 +01004887audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
4888{
4889 return mEngine->getForceUse(usage);
4890}
4891
4892bool AudioPolicyManager::isInCall()
4893{
4894 return isStateInCall(mEngine->getPhoneState());
4895}
4896
4897bool AudioPolicyManager::isStateInCall(int state)
4898{
4899 return is_state_in_call(state);
4900}
4901
Eric Laurente552edb2014-03-10 17:42:56 -07004902}; // namespace android