blob: 855af9f3a8a3d5728ad0228a80c01b1d766239a8 [file] [log] [blame]
Eric Laurentcef3cd72009-12-10 01:03:50 -08001/*
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
17#define LOG_TAG "AudioPolicyManagerBase"
Eric Laurentf1451082010-01-28 13:42:59 -080018//#define LOG_NDEBUG 0
Eric Laurentcef3cd72009-12-10 01:03:50 -080019#include <utils/Log.h>
20#include <hardware_legacy/AudioPolicyManagerBase.h>
21#include <media/mediarecorder.h>
22
23namespace android {
24
25
26// ----------------------------------------------------------------------------
27// AudioPolicyInterface implementation
28// ----------------------------------------------------------------------------
29
30
31status_t AudioPolicyManagerBase::setDeviceConnectionState(AudioSystem::audio_devices device,
32 AudioSystem::device_connection_state state,
33 const char *device_address)
34{
35
36 LOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
37
38 // connect/disconnect only 1 device at a time
39 if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
40
41 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
42 LOGE("setDeviceConnectionState() invalid address: %s", device_address);
43 return BAD_VALUE;
44 }
45
46 // handle output devices
47 if (AudioSystem::isOutputDevice(device)) {
48
49#ifndef WITH_A2DP
50 if (AudioSystem::isA2dpDevice(device)) {
51 LOGE("setDeviceConnectionState() invalid device: %x", device);
52 return BAD_VALUE;
53 }
54#endif
55
56 switch (state)
57 {
58 // handle output device connection
59 case AudioSystem::DEVICE_STATE_AVAILABLE:
60 if (mAvailableOutputDevices & device) {
61 LOGW("setDeviceConnectionState() device already connected: %x", device);
62 return INVALID_OPERATION;
63 }
64 LOGV("setDeviceConnectionState() connecting device %x", device);
65
66 // register new device as available
67 mAvailableOutputDevices |= device;
68
69#ifdef WITH_A2DP
70 // handle A2DP device connection
71 if (AudioSystem::isA2dpDevice(device)) {
72 status_t status = handleA2dpConnection(device, device_address);
73 if (status != NO_ERROR) {
74 mAvailableOutputDevices &= ~device;
75 return status;
76 }
77 } else
78#endif
79 {
80 if (AudioSystem::isBluetoothScoDevice(device)) {
81 LOGV("setDeviceConnectionState() BT SCO device, address %s", device_address);
82 // keep track of SCO device address
83 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
Eric Laurentcef3cd72009-12-10 01:03:50 -080084 }
85 }
86 break;
87 // handle output device disconnection
88 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
89 if (!(mAvailableOutputDevices & device)) {
90 LOGW("setDeviceConnectionState() device not connected: %x", device);
91 return INVALID_OPERATION;
92 }
93
94
95 LOGV("setDeviceConnectionState() disconnecting device %x", device);
96 // remove device from available output devices
97 mAvailableOutputDevices &= ~device;
98
99#ifdef WITH_A2DP
100 // handle A2DP device disconnection
101 if (AudioSystem::isA2dpDevice(device)) {
102 status_t status = handleA2dpDisconnection(device, device_address);
103 if (status != NO_ERROR) {
104 mAvailableOutputDevices |= device;
105 return status;
106 }
107 } else
108#endif
109 {
110 if (AudioSystem::isBluetoothScoDevice(device)) {
111 mScoDeviceAddress = "";
Eric Laurentcef3cd72009-12-10 01:03:50 -0800112 }
113 }
114 } break;
115
116 default:
117 LOGE("setDeviceConnectionState() invalid state: %x", state);
118 return BAD_VALUE;
119 }
120
121 // request routing change if necessary
122 uint32_t newDevice = getNewDevice(mHardwareOutput, false);
123#ifdef WITH_A2DP
Eric Laurentb8453f42010-08-27 17:10:36 -0700124 checkOutputForAllStrategies();
Eric Laurentcef3cd72009-12-10 01:03:50 -0800125 // A2DP outputs must be closed after checkOutputForAllStrategies() is executed
126 if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) {
127 closeA2dpOutputs();
128 }
Eric Laurentb87b53d2010-11-02 12:02:20 -0700129 checkA2dpSuspend();
Eric Laurentcef3cd72009-12-10 01:03:50 -0800130#endif
131 updateDeviceForStrategy();
132 setOutputDevice(mHardwareOutput, newDevice);
133
134 if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
135 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
136 } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
137 device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
138 device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
139 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
140 } else {
141 return NO_ERROR;
142 }
143 }
144 // handle input devices
145 if (AudioSystem::isInputDevice(device)) {
146
147 switch (state)
148 {
149 // handle input device connection
150 case AudioSystem::DEVICE_STATE_AVAILABLE: {
151 if (mAvailableInputDevices & device) {
152 LOGW("setDeviceConnectionState() device already connected: %d", device);
153 return INVALID_OPERATION;
154 }
155 mAvailableInputDevices |= device;
156 }
157 break;
158
159 // handle input device disconnection
160 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
161 if (!(mAvailableInputDevices & device)) {
162 LOGW("setDeviceConnectionState() device not connected: %d", device);
163 return INVALID_OPERATION;
164 }
165 mAvailableInputDevices &= ~device;
166 } break;
167
168 default:
169 LOGE("setDeviceConnectionState() invalid state: %x", state);
170 return BAD_VALUE;
171 }
172
173 audio_io_handle_t activeInput = getActiveInput();
174 if (activeInput != 0) {
175 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
176 uint32_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
177 if (newDevice != inputDesc->mDevice) {
178 LOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
179 inputDesc->mDevice, newDevice, activeInput);
180 inputDesc->mDevice = newDevice;
181 AudioParameter param = AudioParameter();
182 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
183 mpClientInterface->setParameters(activeInput, param.toString());
184 }
185 }
186
187 return NO_ERROR;
188 }
189
190 LOGW("setDeviceConnectionState() invalid device: %x", device);
191 return BAD_VALUE;
192}
193
194AudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(AudioSystem::audio_devices device,
195 const char *device_address)
196{
197 AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
198 String8 address = String8(device_address);
199 if (AudioSystem::isOutputDevice(device)) {
200 if (device & mAvailableOutputDevices) {
201#ifdef WITH_A2DP
202 if (AudioSystem::isA2dpDevice(device) &&
203 address != "" && mA2dpDeviceAddress != address) {
204 return state;
205 }
206#endif
207 if (AudioSystem::isBluetoothScoDevice(device) &&
208 address != "" && mScoDeviceAddress != address) {
209 return state;
210 }
211 state = AudioSystem::DEVICE_STATE_AVAILABLE;
212 }
213 } else if (AudioSystem::isInputDevice(device)) {
214 if (device & mAvailableInputDevices) {
215 state = AudioSystem::DEVICE_STATE_AVAILABLE;
216 }
217 }
218
219 return state;
220}
221
222void AudioPolicyManagerBase::setPhoneState(int state)
223{
224 LOGV("setPhoneState() state %d", state);
225 uint32_t newDevice = 0;
226 if (state < 0 || state >= AudioSystem::NUM_MODES) {
227 LOGW("setPhoneState() invalid state %d", state);
228 return;
229 }
230
231 if (state == mPhoneState ) {
232 LOGW("setPhoneState() setting same state %d", state);
233 return;
234 }
235
236 // if leaving call state, handle special case of active streams
237 // pertaining to sonification strategy see handleIncallSonification()
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800238 if (isInCall()) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800239 LOGV("setPhoneState() in call state management: new state is %d", state);
240 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
241 handleIncallSonification(stream, false, true);
242 }
243 }
244
245 // store previous phone state for management of sonification strategy below
246 int oldState = mPhoneState;
247 mPhoneState = state;
248 bool force = false;
249
250 // are we entering or starting a call
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800251 if (!isStateInCall(oldState) && isStateInCall(state)) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800252 LOGV(" Entering call in setPhoneState()");
253 // force routing command to audio hardware when starting a call
254 // even if no device change is needed
255 force = true;
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800256 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800257 LOGV(" Exiting call in setPhoneState()");
258 // force routing command to audio hardware when exiting a call
259 // even if no device change is needed
260 force = true;
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800261 } else if (isStateInCall(state) && (state != oldState)) {
262 LOGV(" Switching between telephony and VoIP in setPhoneState()");
263 // force routing command to audio hardware when switching between telephony and VoIP
264 // even if no device change is needed
265 force = true;
Eric Laurentcef3cd72009-12-10 01:03:50 -0800266 }
267
268 // check for device and output changes triggered by new phone state
269 newDevice = getNewDevice(mHardwareOutput, false);
270#ifdef WITH_A2DP
Eric Laurentb8453f42010-08-27 17:10:36 -0700271 checkOutputForAllStrategies();
Eric Laurentb87b53d2010-11-02 12:02:20 -0700272 checkA2dpSuspend();
Eric Laurentcef3cd72009-12-10 01:03:50 -0800273#endif
274 updateDeviceForStrategy();
275
276 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
277
278 // force routing command to audio hardware when ending call
279 // even if no device change is needed
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800280 if (isStateInCall(oldState) && newDevice == 0) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800281 newDevice = hwOutputDesc->device();
282 }
Eric Laurent22e1ca32010-02-23 09:48:31 -0800283
284 // when changing from ring tone to in call mode, mute the ringing tone
285 // immediately and delay the route change to avoid sending the ring tone
286 // tail into the earpiece or headset.
287 int delayMs = 0;
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800288 if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
Eric Laurent22e1ca32010-02-23 09:48:31 -0800289 // delay the device change command by twice the output latency to have some margin
290 // and be sure that audio buffers not yet affected by the mute are out when
291 // we actually apply the route change
292 delayMs = hwOutputDesc->mLatency*2;
293 setStreamMute(AudioSystem::RING, true, mHardwareOutput);
294 }
295
Eric Laurentcef3cd72009-12-10 01:03:50 -0800296 // change routing is necessary
Eric Laurent22e1ca32010-02-23 09:48:31 -0800297 setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800298
299 // if entering in call state, handle special case of active streams
300 // pertaining to sonification strategy see handleIncallSonification()
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800301 if (isStateInCall(state)) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800302 LOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent22e1ca32010-02-23 09:48:31 -0800303 // unmute the ringing tone after a sufficient delay if it was muted before
304 // setting output device above
305 if (oldState == AudioSystem::MODE_RINGTONE) {
306 setStreamMute(AudioSystem::RING, false, mHardwareOutput, MUTE_TIME_MS);
307 }
Eric Laurentcef3cd72009-12-10 01:03:50 -0800308 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
309 handleIncallSonification(stream, true, true);
310 }
311 }
312
313 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
314 if (state == AudioSystem::MODE_RINGTONE &&
315 (hwOutputDesc->mRefCount[AudioSystem::MUSIC] ||
316 (systemTime() - mMusicStopTime) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) {
317 mLimitRingtoneVolume = true;
318 } else {
319 mLimitRingtoneVolume = false;
320 }
321}
322
323void AudioPolicyManagerBase::setRingerMode(uint32_t mode, uint32_t mask)
324{
325 LOGV("setRingerMode() mode %x, mask %x", mode, mask);
326
327 mRingerMode = mode;
328}
329
330void AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
331{
332 LOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
333
Jean-Michel Trivi758559e2010-03-09 09:26:08 -0800334 bool forceVolumeReeval = false;
Eric Laurentcef3cd72009-12-10 01:03:50 -0800335 switch(usage) {
336 case AudioSystem::FOR_COMMUNICATION:
337 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
338 config != AudioSystem::FORCE_NONE) {
339 LOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
340 return;
341 }
342 mForceUse[usage] = config;
343 break;
344 case AudioSystem::FOR_MEDIA:
345 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500346 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
347 config != AudioSystem::FORCE_ANALOG_DOCK &&
348 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800349 LOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
350 return;
351 }
352 mForceUse[usage] = config;
353 break;
354 case AudioSystem::FOR_RECORD:
355 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
356 config != AudioSystem::FORCE_NONE) {
357 LOGW("setForceUse() invalid config %d for FOR_RECORD", config);
358 return;
359 }
360 mForceUse[usage] = config;
361 break;
362 case AudioSystem::FOR_DOCK:
363 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500364 config != AudioSystem::FORCE_BT_DESK_DOCK &&
365 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
366 config != AudioSystem::FORCE_ANALOG_DOCK &&
367 config != AudioSystem::FORCE_DIGITAL_DOCK) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800368 LOGW("setForceUse() invalid config %d for FOR_DOCK", config);
369 }
Jean-Michel Trivi758559e2010-03-09 09:26:08 -0800370 forceVolumeReeval = true;
Eric Laurentcef3cd72009-12-10 01:03:50 -0800371 mForceUse[usage] = config;
372 break;
373 default:
374 LOGW("setForceUse() invalid usage %d", usage);
375 break;
376 }
377
378 // check for device and output changes triggered by new phone state
379 uint32_t newDevice = getNewDevice(mHardwareOutput, false);
380#ifdef WITH_A2DP
Eric Laurentb8453f42010-08-27 17:10:36 -0700381 checkOutputForAllStrategies();
Eric Laurentb87b53d2010-11-02 12:02:20 -0700382 checkA2dpSuspend();
Eric Laurentcef3cd72009-12-10 01:03:50 -0800383#endif
384 updateDeviceForStrategy();
385 setOutputDevice(mHardwareOutput, newDevice);
Jean-Michel Trivi758559e2010-03-09 09:26:08 -0800386 if (forceVolumeReeval) {
387 applyStreamVolumes(mHardwareOutput, newDevice);
388 }
Eric Laurentb8453f42010-08-27 17:10:36 -0700389
390 audio_io_handle_t activeInput = getActiveInput();
391 if (activeInput != 0) {
392 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
393 newDevice = getDeviceForInputSource(inputDesc->mInputSource);
394 if (newDevice != inputDesc->mDevice) {
395 LOGV("setForceUse() changing device from %x to %x for input %d",
396 inputDesc->mDevice, newDevice, activeInput);
397 inputDesc->mDevice = newDevice;
398 AudioParameter param = AudioParameter();
399 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
400 mpClientInterface->setParameters(activeInput, param.toString());
401 }
402 }
403
Eric Laurentcef3cd72009-12-10 01:03:50 -0800404}
405
406AudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
407{
408 return mForceUse[usage];
409}
410
411void AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
412{
413 LOGV("setSystemProperty() property %s, value %s", property, value);
414 if (strcmp(property, "ro.camera.sound.forced") == 0) {
415 if (atoi(value)) {
416 LOGV("ENFORCED_AUDIBLE cannot be muted");
417 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
418 } else {
419 LOGV("ENFORCED_AUDIBLE can be muted");
420 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
421 }
422 }
423}
424
425audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
426 uint32_t samplingRate,
427 uint32_t format,
428 uint32_t channels,
429 AudioSystem::output_flags flags)
430{
431 audio_io_handle_t output = 0;
432 uint32_t latency = 0;
433 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
434 uint32_t device = getDeviceForStrategy(strategy);
435 LOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags);
436
437#ifdef AUDIO_POLICY_TEST
438 if (mCurOutput != 0) {
439 LOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d",
440 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
441
442 if (mTestOutputs[mCurOutput] == 0) {
443 LOGV("getOutput() opening test output");
444 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
445 outputDesc->mDevice = mTestDevice;
446 outputDesc->mSamplingRate = mTestSamplingRate;
447 outputDesc->mFormat = mTestFormat;
448 outputDesc->mChannels = mTestChannels;
449 outputDesc->mLatency = mTestLatencyMs;
450 outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
451 outputDesc->mRefCount[stream] = 0;
452 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice,
453 &outputDesc->mSamplingRate,
454 &outputDesc->mFormat,
455 &outputDesc->mChannels,
456 &outputDesc->mLatency,
457 outputDesc->mFlags);
458 if (mTestOutputs[mCurOutput]) {
459 AudioParameter outputCmd = AudioParameter();
460 outputCmd.addInt(String8("set_id"),mCurOutput);
461 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
462 addOutput(mTestOutputs[mCurOutput], outputDesc);
463 }
464 }
465 return mTestOutputs[mCurOutput];
466 }
467#endif //AUDIO_POLICY_TEST
468
Eric Laurentef9500f2010-03-11 14:47:00 -0800469 // open a direct output if required by specified parameters
470 if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800471
472 LOGV("getOutput() opening direct output device %x", device);
473 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
474 outputDesc->mDevice = device;
475 outputDesc->mSamplingRate = samplingRate;
476 outputDesc->mFormat = format;
477 outputDesc->mChannels = channels;
478 outputDesc->mLatency = 0;
479 outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
Eric Laurentef9500f2010-03-11 14:47:00 -0800480 outputDesc->mRefCount[stream] = 0;
Eric Laurentcef3cd72009-12-10 01:03:50 -0800481 output = mpClientInterface->openOutput(&outputDesc->mDevice,
482 &outputDesc->mSamplingRate,
483 &outputDesc->mFormat,
484 &outputDesc->mChannels,
485 &outputDesc->mLatency,
486 outputDesc->mFlags);
487
488 // only accept an output with the requeted parameters
489 if (output == 0 ||
490 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
491 (format != 0 && format != outputDesc->mFormat) ||
492 (channels != 0 && channels != outputDesc->mChannels)) {
493 LOGV("getOutput() failed opening direct output: samplingRate %d, format %d, channels %d",
494 samplingRate, format, channels);
495 if (output != 0) {
496 mpClientInterface->closeOutput(output);
497 }
498 delete outputDesc;
499 return 0;
500 }
501 addOutput(output, outputDesc);
502 return output;
503 }
504
505 if (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO &&
506 channels != AudioSystem::CHANNEL_OUT_STEREO) {
507 return 0;
508 }
509 // open a non direct output
510
511 // get which output is suitable for the specified stream. The actual routing change will happen
512 // when startOutput() will be called
513 uint32_t a2dpDevice = device & AudioSystem::DEVICE_OUT_ALL_A2DP;
514 if (AudioSystem::popCount((AudioSystem::audio_devices)device) == 2) {
515#ifdef WITH_A2DP
516 if (a2dpUsedForSonification() && a2dpDevice != 0) {
517 // if playing on 2 devices among which one is A2DP, use duplicated output
518 LOGV("getOutput() using duplicated output");
519 LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device in multiple %x selected but A2DP output not opened", device);
520 output = mDuplicatedOutput;
521 } else
522#endif
523 {
524 // if playing on 2 devices among which none is A2DP, use hardware output
525 output = mHardwareOutput;
526 }
527 LOGV("getOutput() using output %d for 2 devices %x", output, device);
528 } else {
529#ifdef WITH_A2DP
530 if (a2dpDevice != 0) {
531 // if playing on A2DP device, use a2dp output
532 LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device %x selected but A2DP output not opened", device);
533 output = mA2dpOutput;
534 } else
535#endif
536 {
537 // if playing on not A2DP device, use hardware output
538 output = mHardwareOutput;
539 }
540 }
541
542
543 LOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d, format %d, channels %x, flags %x",
544 stream, samplingRate, format, channels, flags);
545
546 return output;
547}
548
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700549status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
550 AudioSystem::stream_type stream,
551 int session)
Eric Laurentcef3cd72009-12-10 01:03:50 -0800552{
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700553 LOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800554 ssize_t index = mOutputs.indexOfKey(output);
555 if (index < 0) {
556 LOGW("startOutput() unknow output %d", output);
557 return BAD_VALUE;
558 }
559
560 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
561 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
562
563#ifdef WITH_A2DP
564 if (mA2dpOutput != 0 && !a2dpUsedForSonification() && strategy == STRATEGY_SONIFICATION) {
565 setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
566 }
567#endif
568
569 // incremenent usage count for this stream on the requested output:
570 // NOTE that the usage count is the same for duplicated output and hardware output which is
571 // necassary for a correct control of hardware output routing by startOutput() and stopOutput()
572 outputDesc->changeRefCount(stream, 1);
573
574 setOutputDevice(output, getNewDevice(output));
575
576 // handle special case for sonification while in call
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800577 if (isInCall()) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800578 handleIncallSonification(stream, true, false);
579 }
580
581 // apply volume rules for current stream and device if necessary
582 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, outputDesc->device());
583
584 return NO_ERROR;
585}
586
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700587status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
588 AudioSystem::stream_type stream,
589 int session)
Eric Laurentcef3cd72009-12-10 01:03:50 -0800590{
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700591 LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800592 ssize_t index = mOutputs.indexOfKey(output);
593 if (index < 0) {
594 LOGW("stopOutput() unknow output %d", output);
595 return BAD_VALUE;
596 }
597
598 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
599 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
600
601 // handle special case for sonification while in call
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -0800602 if (isInCall()) {
Eric Laurentcef3cd72009-12-10 01:03:50 -0800603 handleIncallSonification(stream, false, false);
604 }
605
606 if (outputDesc->mRefCount[stream] > 0) {
607 // decrement usage count of this stream on the output
608 outputDesc->changeRefCount(stream, -1);
609 // store time at which the last music track was stopped - see computeVolume()
610 if (stream == AudioSystem::MUSIC) {
611 mMusicStopTime = systemTime();
612 }
613
614 setOutputDevice(output, getNewDevice(output));
615
616#ifdef WITH_A2DP
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700617 if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
618 strategy == STRATEGY_SONIFICATION) {
619 setStrategyMute(STRATEGY_MEDIA,
620 false,
621 mA2dpOutput,
622 mOutputs.valueFor(mHardwareOutput)->mLatency*2);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800623 }
624#endif
Eric Laurentef9500f2010-03-11 14:47:00 -0800625 if (output != mHardwareOutput) {
626 setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true);
627 }
Eric Laurentcef3cd72009-12-10 01:03:50 -0800628 return NO_ERROR;
629 } else {
630 LOGW("stopOutput() refcount is already 0 for output %d", output);
631 return INVALID_OPERATION;
632 }
633}
634
635void AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
636{
637 LOGV("releaseOutput() %d", output);
638 ssize_t index = mOutputs.indexOfKey(output);
639 if (index < 0) {
640 LOGW("releaseOutput() releasing unknown output %d", output);
641 return;
642 }
643
644#ifdef AUDIO_POLICY_TEST
645 int testIndex = testOutputIndex(output);
646 if (testIndex != 0) {
647 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
648 if (outputDesc->refCount() == 0) {
649 mpClientInterface->closeOutput(output);
650 delete mOutputs.valueAt(index);
651 mOutputs.removeItem(output);
652 mTestOutputs[testIndex] = 0;
653 }
654 return;
655 }
656#endif //AUDIO_POLICY_TEST
657
658 if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
659 mpClientInterface->closeOutput(output);
660 delete mOutputs.valueAt(index);
661 mOutputs.removeItem(output);
662 }
663}
664
665audio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
666 uint32_t samplingRate,
667 uint32_t format,
668 uint32_t channels,
669 AudioSystem::audio_in_acoustics acoustics)
670{
671 audio_io_handle_t input = 0;
672 uint32_t device = getDeviceForInputSource(inputSource);
673
674 LOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x", inputSource, samplingRate, format, channels, acoustics);
675
676 if (device == 0) {
677 return 0;
678 }
679
680 // adapt channel selection to input source
681 switch(inputSource) {
682 case AUDIO_SOURCE_VOICE_UPLINK:
683 channels = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
684 break;
685 case AUDIO_SOURCE_VOICE_DOWNLINK:
686 channels = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
687 break;
688 case AUDIO_SOURCE_VOICE_CALL:
689 channels = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
690 break;
691 default:
692 break;
693 }
694
695 AudioInputDescriptor *inputDesc = new AudioInputDescriptor();
696
697 inputDesc->mInputSource = inputSource;
698 inputDesc->mDevice = device;
699 inputDesc->mSamplingRate = samplingRate;
700 inputDesc->mFormat = format;
701 inputDesc->mChannels = channels;
702 inputDesc->mAcoustics = acoustics;
703 inputDesc->mRefCount = 0;
704 input = mpClientInterface->openInput(&inputDesc->mDevice,
705 &inputDesc->mSamplingRate,
706 &inputDesc->mFormat,
707 &inputDesc->mChannels,
708 inputDesc->mAcoustics);
709
710 // only accept input with the exact requested set of parameters
711 if (input == 0 ||
712 (samplingRate != inputDesc->mSamplingRate) ||
713 (format != inputDesc->mFormat) ||
714 (channels != inputDesc->mChannels)) {
715 LOGV("getInput() failed opening input: samplingRate %d, format %d, channels %d",
716 samplingRate, format, channels);
717 if (input != 0) {
718 mpClientInterface->closeInput(input);
719 }
720 delete inputDesc;
721 return 0;
722 }
723 mInputs.add(input, inputDesc);
724 return input;
725}
726
727status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
728{
729 LOGV("startInput() input %d", input);
730 ssize_t index = mInputs.indexOfKey(input);
731 if (index < 0) {
732 LOGW("startInput() unknow input %d", input);
733 return BAD_VALUE;
734 }
735 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
736
737#ifdef AUDIO_POLICY_TEST
738 if (mTestInput == 0)
739#endif //AUDIO_POLICY_TEST
740 {
741 // refuse 2 active AudioRecord clients at the same time
742 if (getActiveInput() != 0) {
743 LOGW("startInput() input %d failed: other input already started", input);
744 return INVALID_OPERATION;
745 }
746 }
747
748 AudioParameter param = AudioParameter();
749 param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
750
Jean-Michel Trivi1a22bdb2010-11-09 14:06:52 -0800751 param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
752 LOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800753
754 mpClientInterface->setParameters(input, param.toString());
755
756 inputDesc->mRefCount = 1;
757 return NO_ERROR;
758}
759
760status_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
761{
762 LOGV("stopInput() input %d", input);
763 ssize_t index = mInputs.indexOfKey(input);
764 if (index < 0) {
765 LOGW("stopInput() unknow input %d", input);
766 return BAD_VALUE;
767 }
768 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
769
770 if (inputDesc->mRefCount == 0) {
771 LOGW("stopInput() input %d already stopped", input);
772 return INVALID_OPERATION;
773 } else {
774 AudioParameter param = AudioParameter();
775 param.addInt(String8(AudioParameter::keyRouting), 0);
776 mpClientInterface->setParameters(input, param.toString());
777 inputDesc->mRefCount = 0;
778 return NO_ERROR;
779 }
780}
781
782void AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
783{
784 LOGV("releaseInput() %d", input);
785 ssize_t index = mInputs.indexOfKey(input);
786 if (index < 0) {
787 LOGW("releaseInput() releasing unknown input %d", input);
788 return;
789 }
790 mpClientInterface->closeInput(input);
791 delete mInputs.valueAt(index);
792 mInputs.removeItem(input);
793 LOGV("releaseInput() exit");
794}
795
796void AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
797 int indexMin,
798 int indexMax)
799{
800 LOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
801 if (indexMin < 0 || indexMin >= indexMax) {
802 LOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
803 return;
804 }
805 mStreams[stream].mIndexMin = indexMin;
806 mStreams[stream].mIndexMax = indexMax;
807}
808
809status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
810{
811
812 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
813 return BAD_VALUE;
814 }
815
816 // Force max volume if stream cannot be muted
817 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
818
819 LOGV("setStreamVolumeIndex() stream %d, index %d", stream, index);
820 mStreams[stream].mIndexCur = index;
821
822 // compute and apply stream volume on all outputs according to connected device
823 status_t status = NO_ERROR;
824 for (size_t i = 0; i < mOutputs.size(); i++) {
825 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());
826 if (volStatus != NO_ERROR) {
827 status = volStatus;
828 }
829 }
830 return status;
831}
832
833status_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
834{
835 if (index == 0) {
836 return BAD_VALUE;
837 }
838 LOGV("getStreamVolumeIndex() stream %d", stream);
839 *index = mStreams[stream].mIndexCur;
840 return NO_ERROR;
841}
842
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700843audio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc)
844{
845 LOGV("getOutputForEffect()");
846 // apply simple rule where global effects are attached to the same output as MUSIC streams
847 return getOutput(AudioSystem::MUSIC);
848}
849
850status_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc,
851 audio_io_handle_t output,
852 uint32_t strategy,
853 int session,
854 int id)
855{
856 ssize_t index = mOutputs.indexOfKey(output);
857 if (index < 0) {
858 LOGW("registerEffect() unknown output %d", output);
859 return INVALID_OPERATION;
860 }
861
862 if (mTotalEffectsCpuLoad + desc->cpuLoad > getMaxEffectsCpuLoad()) {
863 LOGW("registerEffect() CPU Load limit exceeded for Fx %s, CPU %f MIPS",
864 desc->name, (float)desc->cpuLoad/10);
865 return INVALID_OPERATION;
866 }
867 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
868 LOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
869 desc->name, desc->memoryUsage);
870 return INVALID_OPERATION;
871 }
872 mTotalEffectsCpuLoad += desc->cpuLoad;
873 mTotalEffectsMemory += desc->memoryUsage;
874 LOGV("registerEffect() effect %s, output %d, strategy %d session %d id %d",
875 desc->name, output, strategy, session, id);
876
877 LOGV("registerEffect() CPU %d, memory %d", desc->cpuLoad, desc->memoryUsage);
878 LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
879
880 EffectDescriptor *pDesc = new EffectDescriptor();
881 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
882 pDesc->mOutput = output;
883 pDesc->mStrategy = (routing_strategy)strategy;
884 pDesc->mSession = session;
885 mEffects.add(id, pDesc);
886
887 return NO_ERROR;
888}
889
890status_t AudioPolicyManagerBase::unregisterEffect(int id)
891{
892 ssize_t index = mEffects.indexOfKey(id);
893 if (index < 0) {
894 LOGW("unregisterEffect() unknown effect ID %d", id);
895 return INVALID_OPERATION;
896 }
897
898 EffectDescriptor *pDesc = mEffects.valueAt(index);
899
900 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
901 LOGW("unregisterEffect() CPU load %d too high for total %d",
902 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
903 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
904 }
905 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
906 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
907 LOGW("unregisterEffect() memory %d too big for total %d",
908 pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
909 pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
910 }
911 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
912 LOGV("unregisterEffect() effect %s, ID %d, CPU %d, memory %d",
913 pDesc->mDesc.name, id, pDesc->mDesc.cpuLoad, pDesc->mDesc.memoryUsage);
914 LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
915
916 mEffects.removeItem(id);
917 delete pDesc;
918
919 return NO_ERROR;
920}
921
Eric Laurentcef3cd72009-12-10 01:03:50 -0800922status_t AudioPolicyManagerBase::dump(int fd)
923{
924 const size_t SIZE = 256;
925 char buffer[SIZE];
926 String8 result;
927
928 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
929 result.append(buffer);
930 snprintf(buffer, SIZE, " Hardware Output: %d\n", mHardwareOutput);
931 result.append(buffer);
932#ifdef WITH_A2DP
933 snprintf(buffer, SIZE, " A2DP Output: %d\n", mA2dpOutput);
934 result.append(buffer);
935 snprintf(buffer, SIZE, " Duplicated Output: %d\n", mDuplicatedOutput);
936 result.append(buffer);
937 snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
938 result.append(buffer);
939#endif
940 snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
941 result.append(buffer);
942 snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
943 result.append(buffer);
944 snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
945 result.append(buffer);
946 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
947 result.append(buffer);
948 snprintf(buffer, SIZE, " Ringer mode: %d\n", mRingerMode);
949 result.append(buffer);
950 snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
951 result.append(buffer);
952 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
953 result.append(buffer);
954 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
955 result.append(buffer);
956 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
957 result.append(buffer);
958 write(fd, result.string(), result.size());
959
960 snprintf(buffer, SIZE, "\nOutputs dump:\n");
961 write(fd, buffer, strlen(buffer));
962 for (size_t i = 0; i < mOutputs.size(); i++) {
963 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
964 write(fd, buffer, strlen(buffer));
965 mOutputs.valueAt(i)->dump(fd);
966 }
967
968 snprintf(buffer, SIZE, "\nInputs dump:\n");
969 write(fd, buffer, strlen(buffer));
970 for (size_t i = 0; i < mInputs.size(); i++) {
971 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
972 write(fd, buffer, strlen(buffer));
973 mInputs.valueAt(i)->dump(fd);
974 }
975
976 snprintf(buffer, SIZE, "\nStreams dump:\n");
977 write(fd, buffer, strlen(buffer));
978 snprintf(buffer, SIZE, " Stream Index Min Index Max Index Cur Can be muted\n");
979 write(fd, buffer, strlen(buffer));
980 for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
981 snprintf(buffer, SIZE, " %02d", i);
982 mStreams[i].dump(buffer + 3, SIZE);
983 write(fd, buffer, strlen(buffer));
984 }
985
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700986 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
987 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
988 write(fd, buffer, strlen(buffer));
989
990 snprintf(buffer, SIZE, "Registered effects:\n");
991 write(fd, buffer, strlen(buffer));
992 for (size_t i = 0; i < mEffects.size(); i++) {
993 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
994 write(fd, buffer, strlen(buffer));
995 mEffects.valueAt(i)->dump(fd);
996 }
997
998
Eric Laurentcef3cd72009-12-10 01:03:50 -0800999 return NO_ERROR;
1000}
1001
1002// ----------------------------------------------------------------------------
1003// AudioPolicyManagerBase
1004// ----------------------------------------------------------------------------
1005
1006AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1007 :
1008#ifdef AUDIO_POLICY_TEST
1009 Thread(false),
1010#endif //AUDIO_POLICY_TEST
Eric Laurentfad77872010-12-01 12:11:10 -08001011 mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0),
1012 mMusicStopTime(0), mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1013 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurentb87b53d2010-11-02 12:02:20 -07001014 mA2dpSuspended(false)
Eric Laurentcef3cd72009-12-10 01:03:50 -08001015{
1016 mpClientInterface = clientInterface;
1017
1018 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1019 mForceUse[i] = AudioSystem::FORCE_NONE;
1020 }
1021
1022 // devices available by default are speaker, ear piece and microphone
1023 mAvailableOutputDevices = AudioSystem::DEVICE_OUT_EARPIECE |
1024 AudioSystem::DEVICE_OUT_SPEAKER;
1025 mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1026
1027#ifdef WITH_A2DP
1028 mA2dpOutput = 0;
1029 mDuplicatedOutput = 0;
1030 mA2dpDeviceAddress = String8("");
1031#endif
1032 mScoDeviceAddress = String8("");
1033
1034 // open hardware output
1035 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1036 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1037 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1038 &outputDesc->mSamplingRate,
1039 &outputDesc->mFormat,
1040 &outputDesc->mChannels,
1041 &outputDesc->mLatency,
1042 outputDesc->mFlags);
1043
1044 if (mHardwareOutput == 0) {
1045 LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d",
1046 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1047 } else {
1048 addOutput(mHardwareOutput, outputDesc);
1049 setOutputDevice(mHardwareOutput, (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER, true);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001050 //TODO: configure audio effect output stage here
Eric Laurentcef3cd72009-12-10 01:03:50 -08001051 }
1052
1053 updateDeviceForStrategy();
1054#ifdef AUDIO_POLICY_TEST
1055 AudioParameter outputCmd = AudioParameter();
1056 outputCmd.addInt(String8("set_id"), 0);
1057 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1058
1059 mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1060 mTestSamplingRate = 44100;
1061 mTestFormat = AudioSystem::PCM_16_BIT;
1062 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO;
1063 mTestLatencyMs = 0;
1064 mCurOutput = 0;
1065 mDirectOutput = false;
1066 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1067 mTestOutputs[i] = 0;
1068 }
1069
1070 const size_t SIZE = 256;
1071 char buffer[SIZE];
1072 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1073 run(buffer, ANDROID_PRIORITY_AUDIO);
1074#endif //AUDIO_POLICY_TEST
1075}
1076
1077AudioPolicyManagerBase::~AudioPolicyManagerBase()
1078{
1079#ifdef AUDIO_POLICY_TEST
1080 exit();
1081#endif //AUDIO_POLICY_TEST
1082 for (size_t i = 0; i < mOutputs.size(); i++) {
1083 mpClientInterface->closeOutput(mOutputs.keyAt(i));
1084 delete mOutputs.valueAt(i);
1085 }
1086 mOutputs.clear();
1087 for (size_t i = 0; i < mInputs.size(); i++) {
1088 mpClientInterface->closeInput(mInputs.keyAt(i));
1089 delete mInputs.valueAt(i);
1090 }
1091 mInputs.clear();
1092}
1093
1094#ifdef AUDIO_POLICY_TEST
1095bool AudioPolicyManagerBase::threadLoop()
1096{
1097 LOGV("entering threadLoop()");
1098 while (!exitPending())
1099 {
1100 String8 command;
1101 int valueInt;
1102 String8 value;
1103
1104 Mutex::Autolock _l(mLock);
1105 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1106
1107 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1108 AudioParameter param = AudioParameter(command);
1109
1110 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1111 valueInt != 0) {
1112 LOGV("Test command %s received", command.string());
1113 String8 target;
1114 if (param.get(String8("target"), target) != NO_ERROR) {
1115 target = "Manager";
1116 }
1117 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1118 param.remove(String8("test_cmd_policy_output"));
1119 mCurOutput = valueInt;
1120 }
1121 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1122 param.remove(String8("test_cmd_policy_direct"));
1123 if (value == "false") {
1124 mDirectOutput = false;
1125 } else if (value == "true") {
1126 mDirectOutput = true;
1127 }
1128 }
1129 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1130 param.remove(String8("test_cmd_policy_input"));
1131 mTestInput = valueInt;
1132 }
1133
1134 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1135 param.remove(String8("test_cmd_policy_format"));
1136 int format = AudioSystem::INVALID_FORMAT;
1137 if (value == "PCM 16 bits") {
1138 format = AudioSystem::PCM_16_BIT;
1139 } else if (value == "PCM 8 bits") {
1140 format = AudioSystem::PCM_8_BIT;
1141 } else if (value == "Compressed MP3") {
1142 format = AudioSystem::MP3;
1143 }
1144 if (format != AudioSystem::INVALID_FORMAT) {
1145 if (target == "Manager") {
1146 mTestFormat = format;
1147 } else if (mTestOutputs[mCurOutput] != 0) {
1148 AudioParameter outputParam = AudioParameter();
1149 outputParam.addInt(String8("format"), format);
1150 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1151 }
1152 }
1153 }
1154 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1155 param.remove(String8("test_cmd_policy_channels"));
1156 int channels = 0;
1157
1158 if (value == "Channels Stereo") {
1159 channels = AudioSystem::CHANNEL_OUT_STEREO;
1160 } else if (value == "Channels Mono") {
1161 channels = AudioSystem::CHANNEL_OUT_MONO;
1162 }
1163 if (channels != 0) {
1164 if (target == "Manager") {
1165 mTestChannels = channels;
1166 } else if (mTestOutputs[mCurOutput] != 0) {
1167 AudioParameter outputParam = AudioParameter();
1168 outputParam.addInt(String8("channels"), channels);
1169 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1170 }
1171 }
1172 }
1173 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1174 param.remove(String8("test_cmd_policy_sampleRate"));
1175 if (valueInt >= 0 && valueInt <= 96000) {
1176 int samplingRate = valueInt;
1177 if (target == "Manager") {
1178 mTestSamplingRate = samplingRate;
1179 } else if (mTestOutputs[mCurOutput] != 0) {
1180 AudioParameter outputParam = AudioParameter();
1181 outputParam.addInt(String8("sampling_rate"), samplingRate);
1182 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1183 }
1184 }
1185 }
1186
1187 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1188 param.remove(String8("test_cmd_policy_reopen"));
1189
1190 mpClientInterface->closeOutput(mHardwareOutput);
1191 delete mOutputs.valueFor(mHardwareOutput);
1192 mOutputs.removeItem(mHardwareOutput);
1193
1194 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1195 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1196 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1197 &outputDesc->mSamplingRate,
1198 &outputDesc->mFormat,
1199 &outputDesc->mChannels,
1200 &outputDesc->mLatency,
1201 outputDesc->mFlags);
1202 if (mHardwareOutput == 0) {
1203 LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1204 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1205 } else {
1206 AudioParameter outputCmd = AudioParameter();
1207 outputCmd.addInt(String8("set_id"), 0);
1208 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1209 addOutput(mHardwareOutput, outputDesc);
1210 }
1211 }
1212
1213
1214 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1215 }
1216 }
1217 return false;
1218}
1219
1220void AudioPolicyManagerBase::exit()
1221{
1222 {
1223 AutoMutex _l(mLock);
1224 requestExit();
1225 mWaitWorkCV.signal();
1226 }
1227 requestExitAndWait();
1228}
1229
1230int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1231{
1232 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1233 if (output == mTestOutputs[i]) return i;
1234 }
1235 return 0;
1236}
1237#endif //AUDIO_POLICY_TEST
1238
1239// ---
1240
1241void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1242{
1243 outputDesc->mId = id;
1244 mOutputs.add(id, outputDesc);
1245}
1246
1247
1248#ifdef WITH_A2DP
1249status_t AudioPolicyManagerBase::handleA2dpConnection(AudioSystem::audio_devices device,
1250 const char *device_address)
1251{
1252 // when an A2DP device is connected, open an A2DP and a duplicated output
1253 LOGV("opening A2DP output for device %s", device_address);
1254 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1255 outputDesc->mDevice = device;
1256 mA2dpOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1257 &outputDesc->mSamplingRate,
1258 &outputDesc->mFormat,
1259 &outputDesc->mChannels,
1260 &outputDesc->mLatency,
1261 outputDesc->mFlags);
1262 if (mA2dpOutput) {
1263 // add A2DP output descriptor
1264 addOutput(mA2dpOutput, outputDesc);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001265
1266 //TODO: configure audio effect output stage here
1267
Eric Laurentcef3cd72009-12-10 01:03:50 -08001268 // set initial stream volume for A2DP device
1269 applyStreamVolumes(mA2dpOutput, device);
1270 if (a2dpUsedForSonification()) {
1271 mDuplicatedOutput = mpClientInterface->openDuplicateOutput(mA2dpOutput, mHardwareOutput);
1272 }
1273 if (mDuplicatedOutput != 0 ||
1274 !a2dpUsedForSonification()) {
1275 // If both A2DP and duplicated outputs are open, send device address to A2DP hardware
1276 // interface
1277 AudioParameter param;
1278 param.add(String8("a2dp_sink_address"), String8(device_address));
1279 mpClientInterface->setParameters(mA2dpOutput, param.toString());
1280 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1281
1282 if (a2dpUsedForSonification()) {
1283 // add duplicated output descriptor
1284 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor();
1285 dupOutputDesc->mOutput1 = mOutputs.valueFor(mHardwareOutput);
1286 dupOutputDesc->mOutput2 = mOutputs.valueFor(mA2dpOutput);
1287 dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate;
1288 dupOutputDesc->mFormat = outputDesc->mFormat;
1289 dupOutputDesc->mChannels = outputDesc->mChannels;
1290 dupOutputDesc->mLatency = outputDesc->mLatency;
1291 addOutput(mDuplicatedOutput, dupOutputDesc);
1292 applyStreamVolumes(mDuplicatedOutput, device);
1293 }
1294 } else {
1295 LOGW("getOutput() could not open duplicated output for %d and %d",
1296 mHardwareOutput, mA2dpOutput);
1297 mpClientInterface->closeOutput(mA2dpOutput);
1298 mOutputs.removeItem(mA2dpOutput);
1299 mA2dpOutput = 0;
1300 delete outputDesc;
1301 return NO_INIT;
1302 }
1303 } else {
1304 LOGW("setDeviceConnectionState() could not open A2DP output for device %x", device);
1305 delete outputDesc;
1306 return NO_INIT;
1307 }
1308 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1309
Eric Laurentcef3cd72009-12-10 01:03:50 -08001310 if (!a2dpUsedForSonification()) {
1311 // mute music on A2DP output if a notification or ringtone is playing
1312 uint32_t refCount = hwOutputDesc->strategyRefCount(STRATEGY_SONIFICATION);
1313 for (uint32_t i = 0; i < refCount; i++) {
1314 setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
1315 }
1316 }
Eric Laurentb87b53d2010-11-02 12:02:20 -07001317
1318 mA2dpSuspended = false;
1319
Eric Laurentcef3cd72009-12-10 01:03:50 -08001320 return NO_ERROR;
1321}
1322
1323status_t AudioPolicyManagerBase::handleA2dpDisconnection(AudioSystem::audio_devices device,
1324 const char *device_address)
1325{
1326 if (mA2dpOutput == 0) {
1327 LOGW("setDeviceConnectionState() disconnecting A2DP and no A2DP output!");
1328 return INVALID_OPERATION;
1329 }
1330
1331 if (mA2dpDeviceAddress != device_address) {
1332 LOGW("setDeviceConnectionState() disconnecting unknow A2DP sink address %s", device_address);
1333 return INVALID_OPERATION;
1334 }
1335
Eric Laurent22e1ca32010-02-23 09:48:31 -08001336 // mute media strategy to avoid outputting sound on hardware output while music stream
Eric Laurentcef3cd72009-12-10 01:03:50 -08001337 // is switched from A2DP output and before music is paused by music application
1338 setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
Eric Laurent22e1ca32010-02-23 09:48:31 -08001339 setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
Eric Laurentcef3cd72009-12-10 01:03:50 -08001340
1341 if (!a2dpUsedForSonification()) {
1342 // unmute music on A2DP output if a notification or ringtone is playing
1343 uint32_t refCount = mOutputs.valueFor(mHardwareOutput)->strategyRefCount(STRATEGY_SONIFICATION);
1344 for (uint32_t i = 0; i < refCount; i++) {
1345 setStrategyMute(STRATEGY_MEDIA, false, mA2dpOutput);
1346 }
1347 }
1348 mA2dpDeviceAddress = "";
Eric Laurentb87b53d2010-11-02 12:02:20 -07001349 mA2dpSuspended = false;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001350 return NO_ERROR;
1351}
1352
1353void AudioPolicyManagerBase::closeA2dpOutputs()
1354{
Praveen Bharathi21e941b2010-10-06 15:23:14 -05001355
Eric Laurentcef3cd72009-12-10 01:03:50 -08001356 LOGV("setDeviceConnectionState() closing A2DP and duplicated output!");
1357
1358 if (mDuplicatedOutput != 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001359 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueFor(mDuplicatedOutput);
1360 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1361 // As all active tracks on duplicated output will be deleted,
1362 // and as they were also referenced on hardware output, the reference
1363 // count for their stream type must be adjusted accordingly on
1364 // hardware output.
1365 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1366 int refCount = dupOutputDesc->mRefCount[i];
1367 hwOutputDesc->changeRefCount((AudioSystem::stream_type)i,-refCount);
1368 }
1369
Eric Laurentcef3cd72009-12-10 01:03:50 -08001370 mpClientInterface->closeOutput(mDuplicatedOutput);
1371 delete mOutputs.valueFor(mDuplicatedOutput);
1372 mOutputs.removeItem(mDuplicatedOutput);
1373 mDuplicatedOutput = 0;
1374 }
1375 if (mA2dpOutput != 0) {
1376 AudioParameter param;
1377 param.add(String8("closing"), String8("true"));
1378 mpClientInterface->setParameters(mA2dpOutput, param.toString());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001379
Eric Laurentcef3cd72009-12-10 01:03:50 -08001380 mpClientInterface->closeOutput(mA2dpOutput);
1381 delete mOutputs.valueFor(mA2dpOutput);
1382 mOutputs.removeItem(mA2dpOutput);
1383 mA2dpOutput = 0;
1384 }
1385}
1386
Eric Laurentb8453f42010-08-27 17:10:36 -07001387void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
Eric Laurentcef3cd72009-12-10 01:03:50 -08001388{
1389 uint32_t prevDevice = getDeviceForStrategy(strategy);
1390 uint32_t curDevice = getDeviceForStrategy(strategy, false);
1391 bool a2dpWasUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(prevDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
1392 bool a2dpIsUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(curDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001393 audio_io_handle_t srcOutput = 0;
1394 audio_io_handle_t dstOutput = 0;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001395
1396 if (a2dpWasUsed && !a2dpIsUsed) {
1397 bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001398 dstOutput = mHardwareOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001399 if (dupUsed) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001400 LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy);
1401 srcOutput = mDuplicatedOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001402 } else {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001403 LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy);
1404 srcOutput = mA2dpOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001405 }
Eric Laurentcef3cd72009-12-10 01:03:50 -08001406 }
1407 if (a2dpIsUsed && !a2dpWasUsed) {
1408 bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001409 srcOutput = mHardwareOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001410 if (dupUsed) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001411 LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy);
1412 dstOutput = mDuplicatedOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001413 } else {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001414 LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy);
1415 dstOutput = mA2dpOutput;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001416 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001417 }
Eric Laurentcef3cd72009-12-10 01:03:50 -08001418
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001419 if (srcOutput != 0 && dstOutput != 0) {
1420 // Move effects associated to this strategy from previous output to new output
1421 for (size_t i = 0; i < mEffects.size(); i++) {
1422 EffectDescriptor *desc = mEffects.valueAt(i);
1423 if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE &&
1424 desc->mStrategy == strategy &&
1425 desc->mOutput == srcOutput) {
1426 LOGV("checkOutputForStrategy() moving effect %d to output %d", mEffects.keyAt(i), dstOutput);
1427 mpClientInterface->moveEffects(desc->mSession, srcOutput, dstOutput);
1428 desc->mOutput = dstOutput;
1429 }
1430 }
1431 // Move tracks associated to this strategy from previous output to new output
Eric Laurentcef3cd72009-12-10 01:03:50 -08001432 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1433 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001434 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput);
Eric Laurentcef3cd72009-12-10 01:03:50 -08001435 }
1436 }
1437 }
1438}
1439
Eric Laurentb8453f42010-08-27 17:10:36 -07001440void AudioPolicyManagerBase::checkOutputForAllStrategies()
Eric Laurentcef3cd72009-12-10 01:03:50 -08001441{
Eric Laurentb8453f42010-08-27 17:10:36 -07001442 checkOutputForStrategy(STRATEGY_PHONE);
1443 checkOutputForStrategy(STRATEGY_SONIFICATION);
1444 checkOutputForStrategy(STRATEGY_MEDIA);
1445 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurentcef3cd72009-12-10 01:03:50 -08001446}
1447
Eric Laurentb87b53d2010-11-02 12:02:20 -07001448void AudioPolicyManagerBase::checkA2dpSuspend()
1449{
1450 // suspend A2DP output if:
1451 // (NOT already suspended) &&
1452 // ((SCO device is connected &&
1453 // (forced usage for communication || for record is SCO))) ||
1454 // (phone state is ringing || in call)
1455 //
1456 // restore A2DP output if:
1457 // (Already suspended) &&
1458 // ((SCO device is NOT connected ||
1459 // (forced usage NOT for communication && NOT for record is SCO))) &&
1460 // (phone state is NOT ringing && NOT in call)
1461 //
1462 if (mA2dpOutput == 0) {
1463 return;
1464 }
1465
1466 if (mA2dpSuspended) {
1467 if (((mScoDeviceAddress == "") ||
1468 ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1469 (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1470 ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1471 (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1472
1473 mpClientInterface->restoreOutput(mA2dpOutput);
1474 mA2dpSuspended = false;
1475 }
1476 } else {
1477 if (((mScoDeviceAddress != "") &&
1478 ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1479 (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1480 ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1481 (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1482
1483 mpClientInterface->suspendOutput(mA2dpOutput);
1484 mA2dpSuspended = true;
1485 }
1486 }
1487}
1488
1489
Eric Laurentcef3cd72009-12-10 01:03:50 -08001490#endif
1491
1492uint32_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1493{
1494 uint32_t device = 0;
1495
1496 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1497 // check the following by order of priority to request a routing change if necessary:
1498 // 1: we are in call or the strategy phone is active on the hardware output:
1499 // use device for strategy phone
1500 // 2: the strategy sonification is active on the hardware output:
1501 // use device for strategy sonification
1502 // 3: the strategy media is active on the hardware output:
1503 // use device for strategy media
1504 // 4: the strategy DTMF is active on the hardware output:
1505 // use device for strategy DTMF
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001506 if (isInCall() ||
Eric Laurentcef3cd72009-12-10 01:03:50 -08001507 outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1508 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1509 } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1510 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
1511 } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1512 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1513 } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1514 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1515 }
1516
1517 LOGV("getNewDevice() selected device %x", device);
1518 return device;
1519}
1520
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001521uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1522 return (uint32_t)getStrategy(stream);
1523}
1524
1525AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1526 AudioSystem::stream_type stream) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001527 // stream to strategy mapping
1528 switch (stream) {
1529 case AudioSystem::VOICE_CALL:
1530 case AudioSystem::BLUETOOTH_SCO:
1531 return STRATEGY_PHONE;
1532 case AudioSystem::RING:
1533 case AudioSystem::NOTIFICATION:
1534 case AudioSystem::ALARM:
1535 case AudioSystem::ENFORCED_AUDIBLE:
1536 return STRATEGY_SONIFICATION;
1537 case AudioSystem::DTMF:
1538 return STRATEGY_DTMF;
1539 default:
1540 LOGE("unknown stream type");
1541 case AudioSystem::SYSTEM:
1542 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1543 // while key clicks are played produces a poor result
1544 case AudioSystem::TTS:
1545 case AudioSystem::MUSIC:
1546 return STRATEGY_MEDIA;
1547 }
1548}
1549
1550uint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1551{
1552 uint32_t device = 0;
1553
1554 if (fromCache) {
1555 LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
1556 return mDeviceForStrategy[strategy];
1557 }
1558
1559 switch (strategy) {
1560 case STRATEGY_DTMF:
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001561 if (!isInCall()) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001562 // when off call, DTMF strategy follows the same rules as MEDIA strategy
1563 device = getDeviceForStrategy(STRATEGY_MEDIA, false);
1564 break;
1565 }
1566 // when in call, DTMF and PHONE strategies follow the same rules
1567 // FALL THROUGH
1568
1569 case STRATEGY_PHONE:
1570 // for phone strategy, we first consider the forced use and then the available devices by order
1571 // of priority
1572 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
1573 case AudioSystem::FORCE_BT_SCO:
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001574 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001575 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1576 if (device) break;
1577 }
1578 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
1579 if (device) break;
1580 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
1581 if (device) break;
1582 // if SCO device is requested but no SCO device is available, fall back to default case
1583 // FALL THROUGH
1584
1585 default: // FORCE_NONE
1586 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1587 if (device) break;
1588 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1589 if (device) break;
Praveen Bharathi21e941b2010-10-06 15:23:14 -05001590 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1591 if (device) break;
Jean-Michel Trivie4b8c422010-03-13 12:33:15 -08001592#ifdef WITH_A2DP
1593 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001594 if (!isInCall()) {
Jean-Michel Trivie4b8c422010-03-13 12:33:15 -08001595 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1596 if (device) break;
1597 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1598 if (device) break;
1599 }
1600#endif
Eric Laurentcef3cd72009-12-10 01:03:50 -08001601 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1602 if (device == 0) {
1603 LOGE("getDeviceForStrategy() earpiece device not found");
1604 }
1605 break;
1606
1607 case AudioSystem::FORCE_SPEAKER:
Eric Laurentaef0cbb2010-12-16 09:44:42 -08001608 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1609 if (device) break;
Jean-Michel Trivie4b8c422010-03-13 12:33:15 -08001610#ifdef WITH_A2DP
1611 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1612 // A2DP speaker when forcing to speaker output
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001613 if (!isInCall()) {
Jean-Michel Trivie4b8c422010-03-13 12:33:15 -08001614 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1615 if (device) break;
1616 }
1617#endif
Eric Laurentcef3cd72009-12-10 01:03:50 -08001618 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1619 if (device == 0) {
1620 LOGE("getDeviceForStrategy() speaker device not found");
1621 }
1622 break;
1623 }
1624 break;
1625
1626 case STRATEGY_SONIFICATION:
1627
1628 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1629 // handleIncallSonification().
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001630 if (isInCall()) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001631 device = getDeviceForStrategy(STRATEGY_PHONE, false);
1632 break;
1633 }
1634 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1635 if (device == 0) {
1636 LOGE("getDeviceForStrategy() speaker device not found");
1637 }
1638 // The second device used for sonification is the same as the device used by media strategy
1639 // FALL THROUGH
1640
1641 case STRATEGY_MEDIA: {
Eric Laurent2c61bee2010-12-14 16:31:33 -08001642 uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurentfe2e0752010-03-06 15:46:31 -08001643 if (device2 == 0) {
1644 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1645 }
Praveen Bharathi21e941b2010-10-06 15:23:14 -05001646 if (device2 == 0) {
Eric Laurent2c61bee2010-12-14 16:31:33 -08001647 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
Praveen Bharathi21e941b2010-10-06 15:23:14 -05001648 }
1649 if (device2 == 0) {
1650 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1651 }
Eric Laurent2c61bee2010-12-14 16:31:33 -08001652 if (device2 == 0) {
1653 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1654 }
Eric Laurentcef3cd72009-12-10 01:03:50 -08001655#ifdef WITH_A2DP
1656 if (mA2dpOutput != 0) {
1657 if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
1658 break;
1659 }
1660 if (device2 == 0) {
1661 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1662 }
1663 if (device2 == 0) {
1664 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1665 }
1666 if (device2 == 0) {
1667 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1668 }
1669 }
1670#endif
1671 if (device2 == 0) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001672 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1673 }
1674
1675 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise
1676 device |= device2;
1677 if (device == 0) {
1678 LOGE("getDeviceForStrategy() speaker device not found");
1679 }
1680 } break;
1681
1682 default:
1683 LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1684 break;
1685 }
1686
1687 LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1688 return device;
1689}
1690
1691void AudioPolicyManagerBase::updateDeviceForStrategy()
1692{
1693 for (int i = 0; i < NUM_STRATEGIES; i++) {
1694 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false);
1695 }
1696}
1697
1698void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
1699{
1700 LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
1701 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1702
1703
1704 if (outputDesc->isDuplicated()) {
1705 setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
1706 setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
1707 return;
1708 }
1709#ifdef WITH_A2DP
1710 // filter devices according to output selected
Eric Laurentef9500f2010-03-11 14:47:00 -08001711 if (output == mA2dpOutput) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001712 device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
Eric Laurentef9500f2010-03-11 14:47:00 -08001713 } else {
1714 device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001715 }
1716#endif
1717
1718 uint32_t prevDevice = (uint32_t)outputDesc->device();
1719 // Do not change the routing if:
1720 // - the requestede device is 0
1721 // - the requested device is the same as current device and force is not specified.
1722 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentef9500f2010-03-11 14:47:00 -08001723 if ((device == 0 || device == prevDevice) && !force) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001724 LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
1725 return;
1726 }
1727
1728 outputDesc->mDevice = device;
1729 // mute media streams if both speaker and headset are selected
1730 if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) {
1731 setStrategyMute(STRATEGY_MEDIA, true, output);
1732 // wait for the PCM output buffers to empty before proceeding with the rest of the command
1733 usleep(outputDesc->mLatency*2*1000);
1734 }
Eric Laurentb87b53d2010-11-02 12:02:20 -07001735
Eric Laurentcef3cd72009-12-10 01:03:50 -08001736 // do the routing
1737 AudioParameter param = AudioParameter();
1738 param.addInt(String8(AudioParameter::keyRouting), (int)device);
1739 mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
1740 // update stream volumes according to new device
1741 applyStreamVolumes(output, device, delayMs);
1742
Eric Laurentcef3cd72009-12-10 01:03:50 -08001743 // if changing from a combined headset + speaker route, unmute media streams
1744 if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) {
1745 setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
1746 }
1747}
1748
1749uint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
1750{
1751 uint32_t device;
1752
1753 switch(inputSource) {
1754 case AUDIO_SOURCE_DEFAULT:
1755 case AUDIO_SOURCE_MIC:
1756 case AUDIO_SOURCE_VOICE_RECOGNITION:
Jean-Michel Trivi820b9e02010-11-08 18:38:14 -08001757 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurentcef3cd72009-12-10 01:03:50 -08001758 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1759 mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1760 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1761 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
1762 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
1763 } else {
1764 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1765 }
1766 break;
1767 case AUDIO_SOURCE_CAMCORDER:
1768 if (hasBackMicrophone()) {
1769 device = AudioSystem::DEVICE_IN_BACK_MIC;
1770 } else {
1771 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1772 }
1773 break;
1774 case AUDIO_SOURCE_VOICE_UPLINK:
1775 case AUDIO_SOURCE_VOICE_DOWNLINK:
1776 case AUDIO_SOURCE_VOICE_CALL:
1777 device = AudioSystem::DEVICE_IN_VOICE_CALL;
1778 break;
1779 default:
1780 LOGW("getInput() invalid input source %d", inputSource);
1781 device = 0;
1782 break;
1783 }
1784 LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1785 return device;
1786}
1787
1788audio_io_handle_t AudioPolicyManagerBase::getActiveInput()
1789{
1790 for (size_t i = 0; i < mInputs.size(); i++) {
1791 if (mInputs.valueAt(i)->mRefCount > 0) {
1792 return mInputs.keyAt(i);
1793 }
1794 }
1795 return 0;
1796}
1797
1798float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
1799{
1800 float volume = 1.0;
1801 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1802 StreamDescriptor &streamDesc = mStreams[stream];
1803
1804 if (device == 0) {
1805 device = outputDesc->device();
1806 }
1807
1808 int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin);
1809 volume = AudioSystem::linearToLog(volInt);
1810
Eric Laurentef9500f2010-03-11 14:47:00 -08001811 // if a headset is connected, apply the following rules to ring tones and notifications
Eric Laurentcef3cd72009-12-10 01:03:50 -08001812 // to avoid sound level bursts in user's ears:
1813 // - always attenuate ring tones and notifications volume by 6dB
1814 // - if music is playing, always limit the volume to current music volume,
1815 // with a minimum threshold at -36dB so that notification is always perceived.
1816 if ((device &
1817 (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
1818 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1819 AudioSystem::DEVICE_OUT_WIRED_HEADSET |
Praveen Bharathi21e941b2010-10-06 15:23:14 -05001820 AudioSystem::DEVICE_OUT_WIRED_HEADPHONE |
1821 AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET |
1822 AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)) &&
Eric Laurentcef3cd72009-12-10 01:03:50 -08001823 (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) &&
1824 streamDesc.mCanBeMuted) {
1825 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
1826 // when the phone is ringing we must consider that music could have been paused just before
1827 // by the music application and behave as if music was active if the last music track was
1828 // just stopped
1829 if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
1830 float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device);
1831 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
1832 if (volume > minVol) {
1833 volume = minVol;
1834 LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
1835 }
1836 }
1837 }
1838
1839 return volume;
1840}
1841
1842status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1843{
1844
1845 // do not change actual stream volume if the stream is muted
1846 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1847 LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1848 return NO_ERROR;
1849 }
1850
1851 // do not change in call volume if bluetooth is connected and vice versa
1852 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1853 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1854 LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1855 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1856 return INVALID_OPERATION;
1857 }
1858
1859 float volume = computeVolume(stream, index, output, device);
Eric Laurentcc9c4242010-05-25 09:31:59 -07001860 // We actually change the volume if:
1861 // - the float value returned by computeVolume() changed
1862 // - the force flag is set
1863 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1864 force) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001865 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1866 LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1867 if (stream == AudioSystem::VOICE_CALL ||
1868 stream == AudioSystem::DTMF ||
1869 stream == AudioSystem::BLUETOOTH_SCO) {
Eric Laurentcef3cd72009-12-10 01:03:50 -08001870 // offset value to reflect actual hardware volume that never reaches 0
1871 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
1872 volume = 0.01 + 0.99 * volume;
Eric Laurentcef3cd72009-12-10 01:03:50 -08001873 }
1874 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1875 }
1876
Eric Laurentcc9c4242010-05-25 09:31:59 -07001877 if (stream == AudioSystem::VOICE_CALL ||
1878 stream == AudioSystem::BLUETOOTH_SCO) {
1879 float voiceVolume;
1880 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1881 if (stream == AudioSystem::VOICE_CALL) {
1882 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1883 } else {
1884 voiceVolume = 1.0;
1885 }
1886 if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) {
1887 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1888 mLastVoiceVolume = voiceVolume;
1889 }
1890 }
1891
Eric Laurentcef3cd72009-12-10 01:03:50 -08001892 return NO_ERROR;
1893}
1894
1895void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs)
1896{
1897 LOGV("applyStreamVolumes() for output %d and device %x", output, device);
1898
1899 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1900 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs);
1901 }
1902}
1903
1904void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
1905{
1906 LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
1907 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1908 if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
1909 setStreamMute(stream, on, output, delayMs);
1910 }
1911 }
1912}
1913
1914void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
1915{
1916 StreamDescriptor &streamDesc = mStreams[stream];
1917 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1918
1919 LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
1920
1921 if (on) {
1922 if (outputDesc->mMuteCount[stream] == 0) {
1923 if (streamDesc.mCanBeMuted) {
1924 checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
1925 }
1926 }
1927 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
1928 outputDesc->mMuteCount[stream]++;
1929 } else {
1930 if (outputDesc->mMuteCount[stream] == 0) {
1931 LOGW("setStreamMute() unmuting non muted stream!");
1932 return;
1933 }
1934 if (--outputDesc->mMuteCount[stream] == 0) {
1935 checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
1936 }
1937 }
1938}
1939
1940void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
1941{
1942 // if the stream pertains to sonification strategy and we are in call we must
1943 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1944 // in the device used for phone strategy and play the tone if the selected device does not
1945 // interfere with the device used for phone strategy
1946 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1947 // many times as there are active tracks on the output
1948
1949 if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
1950 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
1951 LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1952 stream, starting, outputDesc->mDevice, stateChange);
1953 if (outputDesc->mRefCount[stream]) {
1954 int muteCount = 1;
1955 if (stateChange) {
1956 muteCount = outputDesc->mRefCount[stream];
1957 }
1958 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
1959 LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1960 for (int i = 0; i < muteCount; i++) {
1961 setStreamMute(stream, starting, mHardwareOutput);
1962 }
1963 } else {
1964 LOGV("handleIncallSonification() high visibility");
1965 if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) {
1966 LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1967 for (int i = 0; i < muteCount; i++) {
1968 setStreamMute(stream, starting, mHardwareOutput);
1969 }
1970 }
1971 if (starting) {
1972 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
1973 } else {
1974 mpClientInterface->stopTone();
1975 }
1976 }
1977 }
1978 }
1979}
1980
Jean-Michel Trivi8f677d62010-11-15 12:11:32 -08001981bool AudioPolicyManagerBase::isInCall()
1982{
1983 return isStateInCall(mPhoneState);
1984}
1985
1986bool AudioPolicyManagerBase::isStateInCall(int state) {
1987 return ((state == AudioSystem::MODE_IN_CALL) ||
1988 (state == AudioSystem::MODE_IN_COMMUNICATION));
1989}
1990
Eric Laurentef9500f2010-03-11 14:47:00 -08001991bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
1992 uint32_t samplingRate,
1993 uint32_t format,
1994 uint32_t channels,
1995 AudioSystem::output_flags flags,
1996 uint32_t device)
1997{
1998 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
1999 (format !=0 && !AudioSystem::isLinearPCM(format)));
2000}
2001
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002002uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2003{
2004 return MAX_EFFECTS_CPU_LOAD;
2005}
2006
2007uint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2008{
2009 return MAX_EFFECTS_MEMORY;
2010}
2011
Eric Laurentcef3cd72009-12-10 01:03:50 -08002012// --- AudioOutputDescriptor class implementation
2013
2014AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor()
2015 : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
2016 mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0)
2017{
2018 // clear usage count for all stream types
2019 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2020 mRefCount[i] = 0;
2021 mCurVolume[i] = -1.0;
2022 mMuteCount[i] = 0;
2023 }
2024}
2025
2026uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2027{
2028 uint32_t device = 0;
2029 if (isDuplicated()) {
2030 device = mOutput1->mDevice | mOutput2->mDevice;
2031 } else {
2032 device = mDevice;
2033 }
2034 return device;
2035}
2036
2037void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2038{
2039 // forward usage count change to attached outputs
2040 if (isDuplicated()) {
2041 mOutput1->changeRefCount(stream, delta);
2042 mOutput2->changeRefCount(stream, delta);
2043 }
2044 if ((delta + (int)mRefCount[stream]) < 0) {
2045 LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2046 mRefCount[stream] = 0;
2047 return;
2048 }
2049 mRefCount[stream] += delta;
2050 LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2051}
2052
2053uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2054{
2055 uint32_t refcount = 0;
2056 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2057 refcount += mRefCount[i];
2058 }
2059 return refcount;
2060}
2061
2062uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2063{
2064 uint32_t refCount = 0;
2065 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2066 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2067 refCount += mRefCount[i];
2068 }
2069 }
2070 return refCount;
2071}
2072
2073
2074status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2075{
2076 const size_t SIZE = 256;
2077 char buffer[SIZE];
2078 String8 result;
2079
2080 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2081 result.append(buffer);
2082 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2083 result.append(buffer);
2084 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2085 result.append(buffer);
2086 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2087 result.append(buffer);
2088 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2089 result.append(buffer);
2090 snprintf(buffer, SIZE, " Devices %08x\n", device());
2091 result.append(buffer);
2092 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2093 result.append(buffer);
2094 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2095 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2096 result.append(buffer);
2097 }
2098 write(fd, result.string(), result.size());
2099
2100 return NO_ERROR;
2101}
2102
2103// --- AudioInputDescriptor class implementation
2104
2105AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor()
2106 : mSamplingRate(0), mFormat(0), mChannels(0),
2107 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
2108{
2109}
2110
2111status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2112{
2113 const size_t SIZE = 256;
2114 char buffer[SIZE];
2115 String8 result;
2116
2117 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2118 result.append(buffer);
2119 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2120 result.append(buffer);
2121 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2122 result.append(buffer);
2123 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2124 result.append(buffer);
2125 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2126 result.append(buffer);
2127 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2128 result.append(buffer);
2129 write(fd, result.string(), result.size());
2130
2131 return NO_ERROR;
2132}
2133
2134// --- StreamDescriptor class implementation
2135
2136void AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size)
2137{
2138 snprintf(buffer, size, " %02d %02d %02d %d\n",
2139 mIndexMin,
2140 mIndexMax,
2141 mIndexCur,
2142 mCanBeMuted);
2143}
2144
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002145// --- EffectDescriptor class implementation
2146
2147status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2148{
2149 const size_t SIZE = 256;
2150 char buffer[SIZE];
2151 String8 result;
2152
2153 snprintf(buffer, SIZE, " Output: %d\n", mOutput);
2154 result.append(buffer);
2155 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2156 result.append(buffer);
2157 snprintf(buffer, SIZE, " Session: %d\n", mSession);
2158 result.append(buffer);
2159 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
2160 result.append(buffer);
2161 write(fd, result.string(), result.size());
2162
2163 return NO_ERROR;
2164}
2165
2166
Eric Laurentcef3cd72009-12-10 01:03:50 -08002167
2168}; // namespace android