blob: eeca7abca79d78c8fc8c395c98d0f8b98477be8a [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -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
17#define LOG_TAG "AudioPolicyManagerBase"
18//#define LOG_NDEBUG 0
19#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);
Mathias Agopian65ab4712010-07-14 17:59:35 -070084 }
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 = "";
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112 }
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 Laurentc1c88e22010-08-27 17:10:36 -0700124 checkOutputForAllStrategies();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 // A2DP outputs must be closed after checkOutputForAllStrategies() is executed
126 if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) {
127 closeA2dpOutputs();
128 }
Eric Laurent075a1f62010-11-02 12:02:20 -0700129 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130#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 Trivif1fb01a2010-11-15 12:11:32 -0800238 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 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 Trivif1fb01a2010-11-15 12:11:32 -0800251 if (!isStateInCall(oldState) && isStateInCall(state)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 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 Trivif1fb01a2010-11-15 12:11:32 -0800256 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 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 Trivif1fb01a2010-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;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 }
267
268 // check for device and output changes triggered by new phone state
269 newDevice = getNewDevice(mHardwareOutput, false);
270#ifdef WITH_A2DP
Eric Laurentc1c88e22010-08-27 17:10:36 -0700271 checkOutputForAllStrategies();
Eric Laurent075a1f62010-11-02 12:02:20 -0700272 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273#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 Trivif1fb01a2010-11-15 12:11:32 -0800280 if (isStateInCall(oldState) && newDevice == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 newDevice = hwOutputDesc->device();
282 }
283
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 Trivif1fb01a2010-11-15 12:11:32 -0800288 if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 // 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
296 // change routing is necessary
297 setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
298
299 // if entering in call state, handle special case of active streams
300 // pertaining to sonification strategy see handleIncallSonification()
Jean-Michel Trivif1fb01a2010-11-15 12:11:32 -0800301 if (isStateInCall(state)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 LOGV("setPhoneState() in call state management: new state is %d", state);
303 // 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 }
308 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
334 bool forceVolumeReeval = false;
335 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 Bharathib235dee2010-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) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 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 Bharathib235dee2010-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) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 LOGW("setForceUse() invalid config %d for FOR_DOCK", config);
369 }
370 forceVolumeReeval = true;
371 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 Laurentc1c88e22010-08-27 17:10:36 -0700381 checkOutputForAllStrategies();
Eric Laurent075a1f62010-11-02 12:02:20 -0700382 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383#endif
384 updateDeviceForStrategy();
385 setOutputDevice(mHardwareOutput, newDevice);
386 if (forceVolumeReeval) {
387 applyStreamVolumes(mHardwareOutput, newDevice);
388 }
Eric Laurentc1c88e22010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404}
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
469 // open a direct output if required by specified parameters
470 if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
471
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);
480 outputDesc->mRefCount[stream] = 0;
481 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 Laurentde070132010-07-13 04:45:46 -0700549status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
550 AudioSystem::stream_type stream,
551 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552{
Eric Laurentde070132010-07-13 04:45:46 -0700553 LOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 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 Trivif1fb01a2010-11-15 12:11:32 -0800577 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 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 Laurentde070132010-07-13 04:45:46 -0700587status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
588 AudioSystem::stream_type stream,
589 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590{
Eric Laurentde070132010-07-13 04:45:46 -0700591 LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 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 Trivif1fb01a2010-11-15 12:11:32 -0800602 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 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 Laurentde070132010-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);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 }
624#endif
625 if (output != mHardwareOutput) {
626 setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true);
627 }
628 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 Trivi56ecd202010-11-09 14:06:52 -0800751 param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
752 LOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753
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 Laurentde070132010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922status_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 Laurentde070132010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 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 Laurent094b1482010-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 Laurent075a1f62010-11-02 12:02:20 -07001014 mA2dpSuspended(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015{
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 Laurentde070132010-07-13 04:45:46 -07001050 //TODO: configure audio effect output stage here
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 }
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 Laurentde070132010-07-13 04:45:46 -07001265
1266 //TODO: configure audio effect output stage here
1267
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268 // 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
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 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 Laurent075a1f62010-11-02 12:02:20 -07001317
1318 mA2dpSuspended = false;
1319
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320 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
1336 // mute media strategy to avoid outputting sound on hardware output while music stream
1337 // is switched from A2DP output and before music is paused by music application
1338 setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
1339 setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
1340
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 Laurent075a1f62010-11-02 12:02:20 -07001349 mA2dpSuspended = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 return NO_ERROR;
1351}
1352
1353void AudioPolicyManagerBase::closeA2dpOutputs()
1354{
Praveen Bharathib235dee2010-10-06 15:23:14 -05001355
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356 LOGV("setDeviceConnectionState() closing A2DP and duplicated output!");
1357
1358 if (mDuplicatedOutput != 0) {
1359 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
1370 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 Laurentde070132010-07-13 04:45:46 -07001379
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 mpClientInterface->closeOutput(mA2dpOutput);
1381 delete mOutputs.valueFor(mA2dpOutput);
1382 mOutputs.removeItem(mA2dpOutput);
1383 mA2dpOutput = 0;
1384 }
1385}
1386
Eric Laurentc1c88e22010-08-27 17:10:36 -07001387void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388{
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 Laurentde070132010-07-13 04:45:46 -07001393 audio_io_handle_t srcOutput = 0;
1394 audio_io_handle_t dstOutput = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395
1396 if (a2dpWasUsed && !a2dpIsUsed) {
1397 bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001398 dstOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001400 LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy);
1401 srcOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001403 LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy);
1404 srcOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 }
1407 if (a2dpIsUsed && !a2dpWasUsed) {
1408 bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001409 srcOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001411 LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy);
1412 dstOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001414 LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy);
1415 dstOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416 }
Eric Laurentde070132010-07-13 04:45:46 -07001417 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418
Eric Laurentde070132010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1433 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
Eric Laurentde070132010-07-13 04:45:46 -07001434 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 }
1436 }
1437 }
1438}
1439
Eric Laurentc1c88e22010-08-27 17:10:36 -07001440void AudioPolicyManagerBase::checkOutputForAllStrategies()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441{
Eric Laurentc1c88e22010-08-27 17:10:36 -07001442 checkOutputForStrategy(STRATEGY_PHONE);
1443 checkOutputForStrategy(STRATEGY_SONIFICATION);
1444 checkOutputForStrategy(STRATEGY_MEDIA);
1445 checkOutputForStrategy(STRATEGY_DTMF);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446}
1447
Eric Laurent075a1f62010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490#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 Trivif1fb01a2010-11-15 12:11:32 -08001506 if (isInCall() ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 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 Laurentde070132010-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) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 // 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 Trivif1fb01a2010-11-15 12:11:32 -08001561 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 // 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 Trivif1fb01a2010-11-15 12:11:32 -08001574 if (!isInCall() || strategy != STRATEGY_DTMF) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575 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;
Eric Laurenteb0d0c42011-01-09 15:33:01 -08001590 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1591 if (device) break;
1592 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1593 if (device) break;
Praveen Bharathib235dee2010-10-06 15:23:14 -05001594 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1595 if (device) break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596#ifdef WITH_A2DP
1597 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Jean-Michel Trivif1fb01a2010-11-15 12:11:32 -08001598 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1600 if (device) break;
1601 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1602 if (device) break;
1603 }
1604#endif
1605 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1606 if (device == 0) {
1607 LOGE("getDeviceForStrategy() earpiece device not found");
1608 }
1609 break;
1610
1611 case AudioSystem::FORCE_SPEAKER:
Eric Laurenteb0d0c42011-01-09 15:33:01 -08001612 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1613 if (device) break;
1614 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1615 if (device) break;
Eric Laurent0b456a62010-12-16 09:44:42 -08001616 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1617 if (device) break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618#ifdef WITH_A2DP
1619 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1620 // A2DP speaker when forcing to speaker output
Jean-Michel Trivif1fb01a2010-11-15 12:11:32 -08001621 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1623 if (device) break;
1624 }
1625#endif
1626 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1627 if (device == 0) {
1628 LOGE("getDeviceForStrategy() speaker device not found");
1629 }
1630 break;
1631 }
1632 break;
1633
1634 case STRATEGY_SONIFICATION:
1635
1636 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1637 // handleIncallSonification().
Jean-Michel Trivif1fb01a2010-11-15 12:11:32 -08001638 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 device = getDeviceForStrategy(STRATEGY_PHONE, false);
1640 break;
1641 }
1642 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1643 if (device == 0) {
1644 LOGE("getDeviceForStrategy() speaker device not found");
1645 }
1646 // The second device used for sonification is the same as the device used by media strategy
1647 // FALL THROUGH
1648
1649 case STRATEGY_MEDIA: {
Eric Laurentf3a8d322010-12-14 16:31:33 -08001650 uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 if (device2 == 0) {
1652 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1653 }
Praveen Bharathib235dee2010-10-06 15:23:14 -05001654 if (device2 == 0) {
Eric Laurentf3a8d322010-12-14 16:31:33 -08001655 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
Praveen Bharathib235dee2010-10-06 15:23:14 -05001656 }
1657 if (device2 == 0) {
1658 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1659 }
Eric Laurentf3a8d322010-12-14 16:31:33 -08001660 if (device2 == 0) {
1661 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1662 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663#ifdef WITH_A2DP
1664 if (mA2dpOutput != 0) {
1665 if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
1666 break;
1667 }
1668 if (device2 == 0) {
1669 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1670 }
1671 if (device2 == 0) {
1672 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1673 }
1674 if (device2 == 0) {
1675 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1676 }
1677 }
1678#endif
1679 if (device2 == 0) {
1680 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1681 }
1682
1683 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise
1684 device |= device2;
1685 if (device == 0) {
1686 LOGE("getDeviceForStrategy() speaker device not found");
1687 }
1688 } break;
1689
1690 default:
1691 LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1692 break;
1693 }
1694
1695 LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1696 return device;
1697}
1698
1699void AudioPolicyManagerBase::updateDeviceForStrategy()
1700{
1701 for (int i = 0; i < NUM_STRATEGIES; i++) {
1702 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false);
1703 }
1704}
1705
1706void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
1707{
1708 LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
1709 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1710
1711
1712 if (outputDesc->isDuplicated()) {
1713 setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
1714 setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
1715 return;
1716 }
1717#ifdef WITH_A2DP
1718 // filter devices according to output selected
1719 if (output == mA2dpOutput) {
1720 device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
1721 } else {
1722 device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
1723 }
1724#endif
1725
1726 uint32_t prevDevice = (uint32_t)outputDesc->device();
1727 // Do not change the routing if:
1728 // - the requestede device is 0
1729 // - the requested device is the same as current device and force is not specified.
1730 // Doing this check here allows the caller to call setOutputDevice() without conditions
1731 if ((device == 0 || device == prevDevice) && !force) {
1732 LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
1733 return;
1734 }
1735
1736 outputDesc->mDevice = device;
1737 // mute media streams if both speaker and headset are selected
1738 if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) {
1739 setStrategyMute(STRATEGY_MEDIA, true, output);
1740 // wait for the PCM output buffers to empty before proceeding with the rest of the command
1741 usleep(outputDesc->mLatency*2*1000);
1742 }
Eric Laurent075a1f62010-11-02 12:02:20 -07001743
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744 // do the routing
1745 AudioParameter param = AudioParameter();
1746 param.addInt(String8(AudioParameter::keyRouting), (int)device);
1747 mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
1748 // update stream volumes according to new device
1749 applyStreamVolumes(output, device, delayMs);
1750
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751 // if changing from a combined headset + speaker route, unmute media streams
1752 if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) {
1753 setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
1754 }
1755}
1756
1757uint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
1758{
1759 uint32_t device;
1760
1761 switch(inputSource) {
1762 case AUDIO_SOURCE_DEFAULT:
1763 case AUDIO_SOURCE_MIC:
1764 case AUDIO_SOURCE_VOICE_RECOGNITION:
Jean-Michel Trivic643d772010-11-08 18:38:14 -08001765 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1767 mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1768 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1769 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
1770 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
1771 } else {
1772 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1773 }
1774 break;
1775 case AUDIO_SOURCE_CAMCORDER:
1776 if (hasBackMicrophone()) {
1777 device = AudioSystem::DEVICE_IN_BACK_MIC;
1778 } else {
1779 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1780 }
1781 break;
1782 case AUDIO_SOURCE_VOICE_UPLINK:
1783 case AUDIO_SOURCE_VOICE_DOWNLINK:
1784 case AUDIO_SOURCE_VOICE_CALL:
1785 device = AudioSystem::DEVICE_IN_VOICE_CALL;
1786 break;
1787 default:
1788 LOGW("getInput() invalid input source %d", inputSource);
1789 device = 0;
1790 break;
1791 }
1792 LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1793 return device;
1794}
1795
1796audio_io_handle_t AudioPolicyManagerBase::getActiveInput()
1797{
1798 for (size_t i = 0; i < mInputs.size(); i++) {
1799 if (mInputs.valueAt(i)->mRefCount > 0) {
1800 return mInputs.keyAt(i);
1801 }
1802 }
1803 return 0;
1804}
1805
1806float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
1807{
1808 float volume = 1.0;
1809 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1810 StreamDescriptor &streamDesc = mStreams[stream];
1811
1812 if (device == 0) {
1813 device = outputDesc->device();
1814 }
1815
1816 int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin);
1817 volume = AudioSystem::linearToLog(volInt);
1818
1819 // if a headset is connected, apply the following rules to ring tones and notifications
1820 // to avoid sound level bursts in user's ears:
1821 // - always attenuate ring tones and notifications volume by 6dB
1822 // - if music is playing, always limit the volume to current music volume,
1823 // with a minimum threshold at -36dB so that notification is always perceived.
1824 if ((device &
1825 (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
1826 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1827 AudioSystem::DEVICE_OUT_WIRED_HEADSET |
Praveen Bharathib235dee2010-10-06 15:23:14 -05001828 AudioSystem::DEVICE_OUT_WIRED_HEADPHONE |
1829 AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET |
1830 AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)) &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831 (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) &&
1832 streamDesc.mCanBeMuted) {
1833 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
1834 // when the phone is ringing we must consider that music could have been paused just before
1835 // by the music application and behave as if music was active if the last music track was
1836 // just stopped
1837 if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
1838 float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device);
1839 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
1840 if (volume > minVol) {
1841 volume = minVol;
1842 LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
1843 }
1844 }
1845 }
1846
1847 return volume;
1848}
1849
1850status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1851{
1852
1853 // do not change actual stream volume if the stream is muted
1854 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1855 LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1856 return NO_ERROR;
1857 }
1858
1859 // do not change in call volume if bluetooth is connected and vice versa
1860 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1861 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1862 LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1863 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1864 return INVALID_OPERATION;
1865 }
1866
1867 float volume = computeVolume(stream, index, output, device);
Mathias Agopian094c96d2010-07-14 18:48:58 -07001868 // We actually change the volume if:
1869 // - the float value returned by computeVolume() changed
1870 // - the force flag is set
1871 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1872 force) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1874 LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1875 if (stream == AudioSystem::VOICE_CALL ||
1876 stream == AudioSystem::DTMF ||
1877 stream == AudioSystem::BLUETOOTH_SCO) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 // offset value to reflect actual hardware volume that never reaches 0
1879 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
1880 volume = 0.01 + 0.99 * volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881 }
1882 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1883 }
1884
Mathias Agopian094c96d2010-07-14 18:48:58 -07001885 if (stream == AudioSystem::VOICE_CALL ||
1886 stream == AudioSystem::BLUETOOTH_SCO) {
1887 float voiceVolume;
1888 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1889 if (stream == AudioSystem::VOICE_CALL) {
1890 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1891 } else {
1892 voiceVolume = 1.0;
1893 }
1894 if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) {
1895 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1896 mLastVoiceVolume = voiceVolume;
1897 }
1898 }
1899
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 return NO_ERROR;
1901}
1902
1903void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs)
1904{
1905 LOGV("applyStreamVolumes() for output %d and device %x", output, device);
1906
1907 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1908 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs);
1909 }
1910}
1911
1912void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
1913{
1914 LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
1915 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1916 if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
1917 setStreamMute(stream, on, output, delayMs);
1918 }
1919 }
1920}
1921
1922void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
1923{
1924 StreamDescriptor &streamDesc = mStreams[stream];
1925 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1926
1927 LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
1928
1929 if (on) {
1930 if (outputDesc->mMuteCount[stream] == 0) {
1931 if (streamDesc.mCanBeMuted) {
1932 checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
1933 }
1934 }
1935 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
1936 outputDesc->mMuteCount[stream]++;
1937 } else {
1938 if (outputDesc->mMuteCount[stream] == 0) {
1939 LOGW("setStreamMute() unmuting non muted stream!");
1940 return;
1941 }
1942 if (--outputDesc->mMuteCount[stream] == 0) {
1943 checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
1944 }
1945 }
1946}
1947
1948void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
1949{
1950 // if the stream pertains to sonification strategy and we are in call we must
1951 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1952 // in the device used for phone strategy and play the tone if the selected device does not
1953 // interfere with the device used for phone strategy
1954 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1955 // many times as there are active tracks on the output
1956
1957 if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
1958 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
1959 LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1960 stream, starting, outputDesc->mDevice, stateChange);
1961 if (outputDesc->mRefCount[stream]) {
1962 int muteCount = 1;
1963 if (stateChange) {
1964 muteCount = outputDesc->mRefCount[stream];
1965 }
1966 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
1967 LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1968 for (int i = 0; i < muteCount; i++) {
1969 setStreamMute(stream, starting, mHardwareOutput);
1970 }
1971 } else {
1972 LOGV("handleIncallSonification() high visibility");
1973 if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) {
1974 LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1975 for (int i = 0; i < muteCount; i++) {
1976 setStreamMute(stream, starting, mHardwareOutput);
1977 }
1978 }
1979 if (starting) {
1980 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
1981 } else {
1982 mpClientInterface->stopTone();
1983 }
1984 }
1985 }
1986 }
1987}
1988
Jean-Michel Trivif1fb01a2010-11-15 12:11:32 -08001989bool AudioPolicyManagerBase::isInCall()
1990{
1991 return isStateInCall(mPhoneState);
1992}
1993
1994bool AudioPolicyManagerBase::isStateInCall(int state) {
1995 return ((state == AudioSystem::MODE_IN_CALL) ||
1996 (state == AudioSystem::MODE_IN_COMMUNICATION));
1997}
1998
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
2000 uint32_t samplingRate,
2001 uint32_t format,
2002 uint32_t channels,
2003 AudioSystem::output_flags flags,
2004 uint32_t device)
2005{
2006 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2007 (format !=0 && !AudioSystem::isLinearPCM(format)));
2008}
2009
Eric Laurentde070132010-07-13 04:45:46 -07002010uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2011{
2012 return MAX_EFFECTS_CPU_LOAD;
2013}
2014
2015uint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2016{
2017 return MAX_EFFECTS_MEMORY;
2018}
2019
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020// --- AudioOutputDescriptor class implementation
2021
2022AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor()
2023 : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
2024 mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0)
2025{
2026 // clear usage count for all stream types
2027 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2028 mRefCount[i] = 0;
2029 mCurVolume[i] = -1.0;
2030 mMuteCount[i] = 0;
2031 }
2032}
2033
2034uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2035{
2036 uint32_t device = 0;
2037 if (isDuplicated()) {
2038 device = mOutput1->mDevice | mOutput2->mDevice;
2039 } else {
2040 device = mDevice;
2041 }
2042 return device;
2043}
2044
2045void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2046{
2047 // forward usage count change to attached outputs
2048 if (isDuplicated()) {
2049 mOutput1->changeRefCount(stream, delta);
2050 mOutput2->changeRefCount(stream, delta);
2051 }
2052 if ((delta + (int)mRefCount[stream]) < 0) {
2053 LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2054 mRefCount[stream] = 0;
2055 return;
2056 }
2057 mRefCount[stream] += delta;
2058 LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2059}
2060
2061uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2062{
2063 uint32_t refcount = 0;
2064 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2065 refcount += mRefCount[i];
2066 }
2067 return refcount;
2068}
2069
2070uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2071{
2072 uint32_t refCount = 0;
2073 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2074 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2075 refCount += mRefCount[i];
2076 }
2077 }
2078 return refCount;
2079}
2080
2081
2082status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2083{
2084 const size_t SIZE = 256;
2085 char buffer[SIZE];
2086 String8 result;
2087
2088 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2089 result.append(buffer);
2090 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2091 result.append(buffer);
2092 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2093 result.append(buffer);
2094 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2095 result.append(buffer);
2096 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2097 result.append(buffer);
2098 snprintf(buffer, SIZE, " Devices %08x\n", device());
2099 result.append(buffer);
2100 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2101 result.append(buffer);
2102 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2103 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2104 result.append(buffer);
2105 }
2106 write(fd, result.string(), result.size());
2107
2108 return NO_ERROR;
2109}
2110
2111// --- AudioInputDescriptor class implementation
2112
2113AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor()
2114 : mSamplingRate(0), mFormat(0), mChannels(0),
2115 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
2116{
2117}
2118
2119status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2120{
2121 const size_t SIZE = 256;
2122 char buffer[SIZE];
2123 String8 result;
2124
2125 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2126 result.append(buffer);
2127 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2128 result.append(buffer);
2129 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2130 result.append(buffer);
2131 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2132 result.append(buffer);
2133 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2134 result.append(buffer);
2135 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2136 result.append(buffer);
2137 write(fd, result.string(), result.size());
2138
2139 return NO_ERROR;
2140}
2141
2142// --- StreamDescriptor class implementation
2143
2144void AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size)
2145{
2146 snprintf(buffer, size, " %02d %02d %02d %d\n",
2147 mIndexMin,
2148 mIndexMax,
2149 mIndexCur,
2150 mCanBeMuted);
2151}
2152
Eric Laurentde070132010-07-13 04:45:46 -07002153// --- EffectDescriptor class implementation
2154
2155status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2156{
2157 const size_t SIZE = 256;
2158 char buffer[SIZE];
2159 String8 result;
2160
2161 snprintf(buffer, SIZE, " Output: %d\n", mOutput);
2162 result.append(buffer);
2163 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2164 result.append(buffer);
2165 snprintf(buffer, SIZE, " Session: %d\n", mSession);
2166 result.append(buffer);
2167 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
2168 result.append(buffer);
2169 write(fd, result.string(), result.size());
2170
2171 return NO_ERROR;
2172}
2173
2174
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175
2176}; // namespace android