blob: 0da353a46445c8b3abf5e9e777dd9e90cd8be1dc [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 Trivibfa2f132010-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 Trivibfa2f132010-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 Trivibfa2f132010-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 Trivibfa2f132010-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 Trivibfa2f132010-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 Trivibfa2f132010-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 Trivibfa2f132010-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 &&
346 config != AudioSystem::FORCE_WIRED_ACCESSORY && config != AudioSystem::FORCE_NONE) {
347 LOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
348 return;
349 }
350 mForceUse[usage] = config;
351 break;
352 case AudioSystem::FOR_RECORD:
353 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
354 config != AudioSystem::FORCE_NONE) {
355 LOGW("setForceUse() invalid config %d for FOR_RECORD", config);
356 return;
357 }
358 mForceUse[usage] = config;
359 break;
360 case AudioSystem::FOR_DOCK:
361 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
362 config != AudioSystem::FORCE_BT_DESK_DOCK && config != AudioSystem::FORCE_WIRED_ACCESSORY) {
363 LOGW("setForceUse() invalid config %d for FOR_DOCK", config);
364 }
365 forceVolumeReeval = true;
366 mForceUse[usage] = config;
367 break;
368 default:
369 LOGW("setForceUse() invalid usage %d", usage);
370 break;
371 }
372
373 // check for device and output changes triggered by new phone state
374 uint32_t newDevice = getNewDevice(mHardwareOutput, false);
375#ifdef WITH_A2DP
Eric Laurentc1c88e22010-08-27 17:10:36 -0700376 checkOutputForAllStrategies();
Eric Laurent075a1f62010-11-02 12:02:20 -0700377 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378#endif
379 updateDeviceForStrategy();
380 setOutputDevice(mHardwareOutput, newDevice);
381 if (forceVolumeReeval) {
382 applyStreamVolumes(mHardwareOutput, newDevice);
383 }
Eric Laurentc1c88e22010-08-27 17:10:36 -0700384
385 audio_io_handle_t activeInput = getActiveInput();
386 if (activeInput != 0) {
387 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
388 newDevice = getDeviceForInputSource(inputDesc->mInputSource);
389 if (newDevice != inputDesc->mDevice) {
390 LOGV("setForceUse() changing device from %x to %x for input %d",
391 inputDesc->mDevice, newDevice, activeInput);
392 inputDesc->mDevice = newDevice;
393 AudioParameter param = AudioParameter();
394 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
395 mpClientInterface->setParameters(activeInput, param.toString());
396 }
397 }
398
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399}
400
401AudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
402{
403 return mForceUse[usage];
404}
405
406void AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
407{
408 LOGV("setSystemProperty() property %s, value %s", property, value);
409 if (strcmp(property, "ro.camera.sound.forced") == 0) {
410 if (atoi(value)) {
411 LOGV("ENFORCED_AUDIBLE cannot be muted");
412 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
413 } else {
414 LOGV("ENFORCED_AUDIBLE can be muted");
415 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
416 }
417 }
418}
419
420audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
421 uint32_t samplingRate,
422 uint32_t format,
423 uint32_t channels,
424 AudioSystem::output_flags flags)
425{
426 audio_io_handle_t output = 0;
427 uint32_t latency = 0;
428 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
429 uint32_t device = getDeviceForStrategy(strategy);
430 LOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags);
431
432#ifdef AUDIO_POLICY_TEST
433 if (mCurOutput != 0) {
434 LOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d",
435 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
436
437 if (mTestOutputs[mCurOutput] == 0) {
438 LOGV("getOutput() opening test output");
439 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
440 outputDesc->mDevice = mTestDevice;
441 outputDesc->mSamplingRate = mTestSamplingRate;
442 outputDesc->mFormat = mTestFormat;
443 outputDesc->mChannels = mTestChannels;
444 outputDesc->mLatency = mTestLatencyMs;
445 outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
446 outputDesc->mRefCount[stream] = 0;
447 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice,
448 &outputDesc->mSamplingRate,
449 &outputDesc->mFormat,
450 &outputDesc->mChannels,
451 &outputDesc->mLatency,
452 outputDesc->mFlags);
453 if (mTestOutputs[mCurOutput]) {
454 AudioParameter outputCmd = AudioParameter();
455 outputCmd.addInt(String8("set_id"),mCurOutput);
456 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
457 addOutput(mTestOutputs[mCurOutput], outputDesc);
458 }
459 }
460 return mTestOutputs[mCurOutput];
461 }
462#endif //AUDIO_POLICY_TEST
463
464 // open a direct output if required by specified parameters
465 if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
466
467 LOGV("getOutput() opening direct output device %x", device);
468 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
469 outputDesc->mDevice = device;
470 outputDesc->mSamplingRate = samplingRate;
471 outputDesc->mFormat = format;
472 outputDesc->mChannels = channels;
473 outputDesc->mLatency = 0;
474 outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
475 outputDesc->mRefCount[stream] = 0;
476 output = mpClientInterface->openOutput(&outputDesc->mDevice,
477 &outputDesc->mSamplingRate,
478 &outputDesc->mFormat,
479 &outputDesc->mChannels,
480 &outputDesc->mLatency,
481 outputDesc->mFlags);
482
483 // only accept an output with the requeted parameters
484 if (output == 0 ||
485 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
486 (format != 0 && format != outputDesc->mFormat) ||
487 (channels != 0 && channels != outputDesc->mChannels)) {
488 LOGV("getOutput() failed opening direct output: samplingRate %d, format %d, channels %d",
489 samplingRate, format, channels);
490 if (output != 0) {
491 mpClientInterface->closeOutput(output);
492 }
493 delete outputDesc;
494 return 0;
495 }
496 addOutput(output, outputDesc);
497 return output;
498 }
499
500 if (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO &&
501 channels != AudioSystem::CHANNEL_OUT_STEREO) {
502 return 0;
503 }
504 // open a non direct output
505
506 // get which output is suitable for the specified stream. The actual routing change will happen
507 // when startOutput() will be called
508 uint32_t a2dpDevice = device & AudioSystem::DEVICE_OUT_ALL_A2DP;
509 if (AudioSystem::popCount((AudioSystem::audio_devices)device) == 2) {
510#ifdef WITH_A2DP
511 if (a2dpUsedForSonification() && a2dpDevice != 0) {
512 // if playing on 2 devices among which one is A2DP, use duplicated output
513 LOGV("getOutput() using duplicated output");
514 LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device in multiple %x selected but A2DP output not opened", device);
515 output = mDuplicatedOutput;
516 } else
517#endif
518 {
519 // if playing on 2 devices among which none is A2DP, use hardware output
520 output = mHardwareOutput;
521 }
522 LOGV("getOutput() using output %d for 2 devices %x", output, device);
523 } else {
524#ifdef WITH_A2DP
525 if (a2dpDevice != 0) {
526 // if playing on A2DP device, use a2dp output
527 LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device %x selected but A2DP output not opened", device);
528 output = mA2dpOutput;
529 } else
530#endif
531 {
532 // if playing on not A2DP device, use hardware output
533 output = mHardwareOutput;
534 }
535 }
536
537
538 LOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d, format %d, channels %x, flags %x",
539 stream, samplingRate, format, channels, flags);
540
541 return output;
542}
543
Eric Laurentde070132010-07-13 04:45:46 -0700544status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
545 AudioSystem::stream_type stream,
546 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547{
Eric Laurentde070132010-07-13 04:45:46 -0700548 LOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 ssize_t index = mOutputs.indexOfKey(output);
550 if (index < 0) {
551 LOGW("startOutput() unknow output %d", output);
552 return BAD_VALUE;
553 }
554
555 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
556 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
557
558#ifdef WITH_A2DP
559 if (mA2dpOutput != 0 && !a2dpUsedForSonification() && strategy == STRATEGY_SONIFICATION) {
560 setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
561 }
562#endif
563
564 // incremenent usage count for this stream on the requested output:
565 // NOTE that the usage count is the same for duplicated output and hardware output which is
566 // necassary for a correct control of hardware output routing by startOutput() and stopOutput()
567 outputDesc->changeRefCount(stream, 1);
568
569 setOutputDevice(output, getNewDevice(output));
570
571 // handle special case for sonification while in call
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -0800572 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 handleIncallSonification(stream, true, false);
574 }
575
576 // apply volume rules for current stream and device if necessary
577 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, outputDesc->device());
578
579 return NO_ERROR;
580}
581
Eric Laurentde070132010-07-13 04:45:46 -0700582status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
583 AudioSystem::stream_type stream,
584 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585{
Eric Laurentde070132010-07-13 04:45:46 -0700586 LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 ssize_t index = mOutputs.indexOfKey(output);
588 if (index < 0) {
589 LOGW("stopOutput() unknow output %d", output);
590 return BAD_VALUE;
591 }
592
593 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
594 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
595
596 // handle special case for sonification while in call
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -0800597 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 handleIncallSonification(stream, false, false);
599 }
600
601 if (outputDesc->mRefCount[stream] > 0) {
602 // decrement usage count of this stream on the output
603 outputDesc->changeRefCount(stream, -1);
604 // store time at which the last music track was stopped - see computeVolume()
605 if (stream == AudioSystem::MUSIC) {
606 mMusicStopTime = systemTime();
607 }
608
609 setOutputDevice(output, getNewDevice(output));
610
611#ifdef WITH_A2DP
Eric Laurentde070132010-07-13 04:45:46 -0700612 if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
613 strategy == STRATEGY_SONIFICATION) {
614 setStrategyMute(STRATEGY_MEDIA,
615 false,
616 mA2dpOutput,
617 mOutputs.valueFor(mHardwareOutput)->mLatency*2);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 }
619#endif
620 if (output != mHardwareOutput) {
621 setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true);
622 }
623 return NO_ERROR;
624 } else {
625 LOGW("stopOutput() refcount is already 0 for output %d", output);
626 return INVALID_OPERATION;
627 }
628}
629
630void AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
631{
632 LOGV("releaseOutput() %d", output);
633 ssize_t index = mOutputs.indexOfKey(output);
634 if (index < 0) {
635 LOGW("releaseOutput() releasing unknown output %d", output);
636 return;
637 }
638
639#ifdef AUDIO_POLICY_TEST
640 int testIndex = testOutputIndex(output);
641 if (testIndex != 0) {
642 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
643 if (outputDesc->refCount() == 0) {
644 mpClientInterface->closeOutput(output);
645 delete mOutputs.valueAt(index);
646 mOutputs.removeItem(output);
647 mTestOutputs[testIndex] = 0;
648 }
649 return;
650 }
651#endif //AUDIO_POLICY_TEST
652
653 if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
654 mpClientInterface->closeOutput(output);
655 delete mOutputs.valueAt(index);
656 mOutputs.removeItem(output);
657 }
658}
659
660audio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
661 uint32_t samplingRate,
662 uint32_t format,
663 uint32_t channels,
664 AudioSystem::audio_in_acoustics acoustics)
665{
666 audio_io_handle_t input = 0;
667 uint32_t device = getDeviceForInputSource(inputSource);
668
669 LOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x", inputSource, samplingRate, format, channels, acoustics);
670
671 if (device == 0) {
672 return 0;
673 }
674
675 // adapt channel selection to input source
676 switch(inputSource) {
677 case AUDIO_SOURCE_VOICE_UPLINK:
678 channels = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
679 break;
680 case AUDIO_SOURCE_VOICE_DOWNLINK:
681 channels = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
682 break;
683 case AUDIO_SOURCE_VOICE_CALL:
684 channels = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
685 break;
686 default:
687 break;
688 }
689
690 AudioInputDescriptor *inputDesc = new AudioInputDescriptor();
691
692 inputDesc->mInputSource = inputSource;
693 inputDesc->mDevice = device;
694 inputDesc->mSamplingRate = samplingRate;
695 inputDesc->mFormat = format;
696 inputDesc->mChannels = channels;
697 inputDesc->mAcoustics = acoustics;
698 inputDesc->mRefCount = 0;
699 input = mpClientInterface->openInput(&inputDesc->mDevice,
700 &inputDesc->mSamplingRate,
701 &inputDesc->mFormat,
702 &inputDesc->mChannels,
703 inputDesc->mAcoustics);
704
705 // only accept input with the exact requested set of parameters
706 if (input == 0 ||
707 (samplingRate != inputDesc->mSamplingRate) ||
708 (format != inputDesc->mFormat) ||
709 (channels != inputDesc->mChannels)) {
710 LOGV("getInput() failed opening input: samplingRate %d, format %d, channels %d",
711 samplingRate, format, channels);
712 if (input != 0) {
713 mpClientInterface->closeInput(input);
714 }
715 delete inputDesc;
716 return 0;
717 }
718 mInputs.add(input, inputDesc);
719 return input;
720}
721
722status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
723{
724 LOGV("startInput() input %d", input);
725 ssize_t index = mInputs.indexOfKey(input);
726 if (index < 0) {
727 LOGW("startInput() unknow input %d", input);
728 return BAD_VALUE;
729 }
730 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
731
732#ifdef AUDIO_POLICY_TEST
733 if (mTestInput == 0)
734#endif //AUDIO_POLICY_TEST
735 {
736 // refuse 2 active AudioRecord clients at the same time
737 if (getActiveInput() != 0) {
738 LOGW("startInput() input %d failed: other input already started", input);
739 return INVALID_OPERATION;
740 }
741 }
742
743 AudioParameter param = AudioParameter();
744 param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
745
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -0800746 param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
747 LOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748
749 mpClientInterface->setParameters(input, param.toString());
750
751 inputDesc->mRefCount = 1;
752 return NO_ERROR;
753}
754
755status_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
756{
757 LOGV("stopInput() input %d", input);
758 ssize_t index = mInputs.indexOfKey(input);
759 if (index < 0) {
760 LOGW("stopInput() unknow input %d", input);
761 return BAD_VALUE;
762 }
763 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
764
765 if (inputDesc->mRefCount == 0) {
766 LOGW("stopInput() input %d already stopped", input);
767 return INVALID_OPERATION;
768 } else {
769 AudioParameter param = AudioParameter();
770 param.addInt(String8(AudioParameter::keyRouting), 0);
771 mpClientInterface->setParameters(input, param.toString());
772 inputDesc->mRefCount = 0;
773 return NO_ERROR;
774 }
775}
776
777void AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
778{
779 LOGV("releaseInput() %d", input);
780 ssize_t index = mInputs.indexOfKey(input);
781 if (index < 0) {
782 LOGW("releaseInput() releasing unknown input %d", input);
783 return;
784 }
785 mpClientInterface->closeInput(input);
786 delete mInputs.valueAt(index);
787 mInputs.removeItem(input);
788 LOGV("releaseInput() exit");
789}
790
791void AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
792 int indexMin,
793 int indexMax)
794{
795 LOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
796 if (indexMin < 0 || indexMin >= indexMax) {
797 LOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
798 return;
799 }
800 mStreams[stream].mIndexMin = indexMin;
801 mStreams[stream].mIndexMax = indexMax;
802}
803
804status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
805{
806
807 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
808 return BAD_VALUE;
809 }
810
811 // Force max volume if stream cannot be muted
812 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
813
814 LOGV("setStreamVolumeIndex() stream %d, index %d", stream, index);
815 mStreams[stream].mIndexCur = index;
816
817 // compute and apply stream volume on all outputs according to connected device
818 status_t status = NO_ERROR;
819 for (size_t i = 0; i < mOutputs.size(); i++) {
820 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());
821 if (volStatus != NO_ERROR) {
822 status = volStatus;
823 }
824 }
825 return status;
826}
827
828status_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
829{
830 if (index == 0) {
831 return BAD_VALUE;
832 }
833 LOGV("getStreamVolumeIndex() stream %d", stream);
834 *index = mStreams[stream].mIndexCur;
835 return NO_ERROR;
836}
837
Eric Laurentde070132010-07-13 04:45:46 -0700838audio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc)
839{
840 LOGV("getOutputForEffect()");
841 // apply simple rule where global effects are attached to the same output as MUSIC streams
842 return getOutput(AudioSystem::MUSIC);
843}
844
845status_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc,
846 audio_io_handle_t output,
847 uint32_t strategy,
848 int session,
849 int id)
850{
851 ssize_t index = mOutputs.indexOfKey(output);
852 if (index < 0) {
853 LOGW("registerEffect() unknown output %d", output);
854 return INVALID_OPERATION;
855 }
856
857 if (mTotalEffectsCpuLoad + desc->cpuLoad > getMaxEffectsCpuLoad()) {
858 LOGW("registerEffect() CPU Load limit exceeded for Fx %s, CPU %f MIPS",
859 desc->name, (float)desc->cpuLoad/10);
860 return INVALID_OPERATION;
861 }
862 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
863 LOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
864 desc->name, desc->memoryUsage);
865 return INVALID_OPERATION;
866 }
867 mTotalEffectsCpuLoad += desc->cpuLoad;
868 mTotalEffectsMemory += desc->memoryUsage;
869 LOGV("registerEffect() effect %s, output %d, strategy %d session %d id %d",
870 desc->name, output, strategy, session, id);
871
872 LOGV("registerEffect() CPU %d, memory %d", desc->cpuLoad, desc->memoryUsage);
873 LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
874
875 EffectDescriptor *pDesc = new EffectDescriptor();
876 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
877 pDesc->mOutput = output;
878 pDesc->mStrategy = (routing_strategy)strategy;
879 pDesc->mSession = session;
880 mEffects.add(id, pDesc);
881
882 return NO_ERROR;
883}
884
885status_t AudioPolicyManagerBase::unregisterEffect(int id)
886{
887 ssize_t index = mEffects.indexOfKey(id);
888 if (index < 0) {
889 LOGW("unregisterEffect() unknown effect ID %d", id);
890 return INVALID_OPERATION;
891 }
892
893 EffectDescriptor *pDesc = mEffects.valueAt(index);
894
895 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
896 LOGW("unregisterEffect() CPU load %d too high for total %d",
897 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
898 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
899 }
900 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
901 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
902 LOGW("unregisterEffect() memory %d too big for total %d",
903 pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
904 pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
905 }
906 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
907 LOGV("unregisterEffect() effect %s, ID %d, CPU %d, memory %d",
908 pDesc->mDesc.name, id, pDesc->mDesc.cpuLoad, pDesc->mDesc.memoryUsage);
909 LOGV(" total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
910
911 mEffects.removeItem(id);
912 delete pDesc;
913
914 return NO_ERROR;
915}
916
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917status_t AudioPolicyManagerBase::dump(int fd)
918{
919 const size_t SIZE = 256;
920 char buffer[SIZE];
921 String8 result;
922
923 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
924 result.append(buffer);
925 snprintf(buffer, SIZE, " Hardware Output: %d\n", mHardwareOutput);
926 result.append(buffer);
927#ifdef WITH_A2DP
928 snprintf(buffer, SIZE, " A2DP Output: %d\n", mA2dpOutput);
929 result.append(buffer);
930 snprintf(buffer, SIZE, " Duplicated Output: %d\n", mDuplicatedOutput);
931 result.append(buffer);
932 snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
933 result.append(buffer);
934#endif
935 snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
936 result.append(buffer);
937 snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
938 result.append(buffer);
939 snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
940 result.append(buffer);
941 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
942 result.append(buffer);
943 snprintf(buffer, SIZE, " Ringer mode: %d\n", mRingerMode);
944 result.append(buffer);
945 snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
946 result.append(buffer);
947 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
948 result.append(buffer);
949 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
950 result.append(buffer);
951 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
952 result.append(buffer);
953 write(fd, result.string(), result.size());
954
955 snprintf(buffer, SIZE, "\nOutputs dump:\n");
956 write(fd, buffer, strlen(buffer));
957 for (size_t i = 0; i < mOutputs.size(); i++) {
958 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
959 write(fd, buffer, strlen(buffer));
960 mOutputs.valueAt(i)->dump(fd);
961 }
962
963 snprintf(buffer, SIZE, "\nInputs dump:\n");
964 write(fd, buffer, strlen(buffer));
965 for (size_t i = 0; i < mInputs.size(); i++) {
966 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
967 write(fd, buffer, strlen(buffer));
968 mInputs.valueAt(i)->dump(fd);
969 }
970
971 snprintf(buffer, SIZE, "\nStreams dump:\n");
972 write(fd, buffer, strlen(buffer));
973 snprintf(buffer, SIZE, " Stream Index Min Index Max Index Cur Can be muted\n");
974 write(fd, buffer, strlen(buffer));
975 for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
976 snprintf(buffer, SIZE, " %02d", i);
977 mStreams[i].dump(buffer + 3, SIZE);
978 write(fd, buffer, strlen(buffer));
979 }
980
Eric Laurentde070132010-07-13 04:45:46 -0700981 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
982 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
983 write(fd, buffer, strlen(buffer));
984
985 snprintf(buffer, SIZE, "Registered effects:\n");
986 write(fd, buffer, strlen(buffer));
987 for (size_t i = 0; i < mEffects.size(); i++) {
988 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
989 write(fd, buffer, strlen(buffer));
990 mEffects.valueAt(i)->dump(fd);
991 }
992
993
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 return NO_ERROR;
995}
996
997// ----------------------------------------------------------------------------
998// AudioPolicyManagerBase
999// ----------------------------------------------------------------------------
1000
1001AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1002 :
1003#ifdef AUDIO_POLICY_TEST
1004 Thread(false),
1005#endif //AUDIO_POLICY_TEST
Eric Laurented7c6712010-12-01 14:25:39 -08001006 mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0),
1007 mMusicStopTime(0), mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1008 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent075a1f62010-11-02 12:02:20 -07001009 mA2dpSuspended(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010{
1011 mpClientInterface = clientInterface;
1012
1013 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1014 mForceUse[i] = AudioSystem::FORCE_NONE;
1015 }
1016
1017 // devices available by default are speaker, ear piece and microphone
1018 mAvailableOutputDevices = AudioSystem::DEVICE_OUT_EARPIECE |
1019 AudioSystem::DEVICE_OUT_SPEAKER;
1020 mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1021
1022#ifdef WITH_A2DP
1023 mA2dpOutput = 0;
1024 mDuplicatedOutput = 0;
1025 mA2dpDeviceAddress = String8("");
1026#endif
1027 mScoDeviceAddress = String8("");
1028
1029 // open hardware output
1030 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1031 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1032 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1033 &outputDesc->mSamplingRate,
1034 &outputDesc->mFormat,
1035 &outputDesc->mChannels,
1036 &outputDesc->mLatency,
1037 outputDesc->mFlags);
1038
1039 if (mHardwareOutput == 0) {
1040 LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d",
1041 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1042 } else {
1043 addOutput(mHardwareOutput, outputDesc);
1044 setOutputDevice(mHardwareOutput, (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER, true);
Eric Laurentde070132010-07-13 04:45:46 -07001045 //TODO: configure audio effect output stage here
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 }
1047
1048 updateDeviceForStrategy();
1049#ifdef AUDIO_POLICY_TEST
1050 AudioParameter outputCmd = AudioParameter();
1051 outputCmd.addInt(String8("set_id"), 0);
1052 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1053
1054 mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1055 mTestSamplingRate = 44100;
1056 mTestFormat = AudioSystem::PCM_16_BIT;
1057 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO;
1058 mTestLatencyMs = 0;
1059 mCurOutput = 0;
1060 mDirectOutput = false;
1061 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1062 mTestOutputs[i] = 0;
1063 }
1064
1065 const size_t SIZE = 256;
1066 char buffer[SIZE];
1067 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1068 run(buffer, ANDROID_PRIORITY_AUDIO);
1069#endif //AUDIO_POLICY_TEST
1070}
1071
1072AudioPolicyManagerBase::~AudioPolicyManagerBase()
1073{
1074#ifdef AUDIO_POLICY_TEST
1075 exit();
1076#endif //AUDIO_POLICY_TEST
1077 for (size_t i = 0; i < mOutputs.size(); i++) {
1078 mpClientInterface->closeOutput(mOutputs.keyAt(i));
1079 delete mOutputs.valueAt(i);
1080 }
1081 mOutputs.clear();
1082 for (size_t i = 0; i < mInputs.size(); i++) {
1083 mpClientInterface->closeInput(mInputs.keyAt(i));
1084 delete mInputs.valueAt(i);
1085 }
1086 mInputs.clear();
1087}
1088
1089#ifdef AUDIO_POLICY_TEST
1090bool AudioPolicyManagerBase::threadLoop()
1091{
1092 LOGV("entering threadLoop()");
1093 while (!exitPending())
1094 {
1095 String8 command;
1096 int valueInt;
1097 String8 value;
1098
1099 Mutex::Autolock _l(mLock);
1100 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1101
1102 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1103 AudioParameter param = AudioParameter(command);
1104
1105 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1106 valueInt != 0) {
1107 LOGV("Test command %s received", command.string());
1108 String8 target;
1109 if (param.get(String8("target"), target) != NO_ERROR) {
1110 target = "Manager";
1111 }
1112 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1113 param.remove(String8("test_cmd_policy_output"));
1114 mCurOutput = valueInt;
1115 }
1116 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1117 param.remove(String8("test_cmd_policy_direct"));
1118 if (value == "false") {
1119 mDirectOutput = false;
1120 } else if (value == "true") {
1121 mDirectOutput = true;
1122 }
1123 }
1124 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1125 param.remove(String8("test_cmd_policy_input"));
1126 mTestInput = valueInt;
1127 }
1128
1129 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1130 param.remove(String8("test_cmd_policy_format"));
1131 int format = AudioSystem::INVALID_FORMAT;
1132 if (value == "PCM 16 bits") {
1133 format = AudioSystem::PCM_16_BIT;
1134 } else if (value == "PCM 8 bits") {
1135 format = AudioSystem::PCM_8_BIT;
1136 } else if (value == "Compressed MP3") {
1137 format = AudioSystem::MP3;
1138 }
1139 if (format != AudioSystem::INVALID_FORMAT) {
1140 if (target == "Manager") {
1141 mTestFormat = format;
1142 } else if (mTestOutputs[mCurOutput] != 0) {
1143 AudioParameter outputParam = AudioParameter();
1144 outputParam.addInt(String8("format"), format);
1145 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1146 }
1147 }
1148 }
1149 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1150 param.remove(String8("test_cmd_policy_channels"));
1151 int channels = 0;
1152
1153 if (value == "Channels Stereo") {
1154 channels = AudioSystem::CHANNEL_OUT_STEREO;
1155 } else if (value == "Channels Mono") {
1156 channels = AudioSystem::CHANNEL_OUT_MONO;
1157 }
1158 if (channels != 0) {
1159 if (target == "Manager") {
1160 mTestChannels = channels;
1161 } else if (mTestOutputs[mCurOutput] != 0) {
1162 AudioParameter outputParam = AudioParameter();
1163 outputParam.addInt(String8("channels"), channels);
1164 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1165 }
1166 }
1167 }
1168 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1169 param.remove(String8("test_cmd_policy_sampleRate"));
1170 if (valueInt >= 0 && valueInt <= 96000) {
1171 int samplingRate = valueInt;
1172 if (target == "Manager") {
1173 mTestSamplingRate = samplingRate;
1174 } else if (mTestOutputs[mCurOutput] != 0) {
1175 AudioParameter outputParam = AudioParameter();
1176 outputParam.addInt(String8("sampling_rate"), samplingRate);
1177 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1178 }
1179 }
1180 }
1181
1182 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1183 param.remove(String8("test_cmd_policy_reopen"));
1184
1185 mpClientInterface->closeOutput(mHardwareOutput);
1186 delete mOutputs.valueFor(mHardwareOutput);
1187 mOutputs.removeItem(mHardwareOutput);
1188
1189 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1190 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1191 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1192 &outputDesc->mSamplingRate,
1193 &outputDesc->mFormat,
1194 &outputDesc->mChannels,
1195 &outputDesc->mLatency,
1196 outputDesc->mFlags);
1197 if (mHardwareOutput == 0) {
1198 LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1199 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1200 } else {
1201 AudioParameter outputCmd = AudioParameter();
1202 outputCmd.addInt(String8("set_id"), 0);
1203 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1204 addOutput(mHardwareOutput, outputDesc);
1205 }
1206 }
1207
1208
1209 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1210 }
1211 }
1212 return false;
1213}
1214
1215void AudioPolicyManagerBase::exit()
1216{
1217 {
1218 AutoMutex _l(mLock);
1219 requestExit();
1220 mWaitWorkCV.signal();
1221 }
1222 requestExitAndWait();
1223}
1224
1225int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1226{
1227 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1228 if (output == mTestOutputs[i]) return i;
1229 }
1230 return 0;
1231}
1232#endif //AUDIO_POLICY_TEST
1233
1234// ---
1235
1236void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1237{
1238 outputDesc->mId = id;
1239 mOutputs.add(id, outputDesc);
1240}
1241
1242
1243#ifdef WITH_A2DP
1244status_t AudioPolicyManagerBase::handleA2dpConnection(AudioSystem::audio_devices device,
1245 const char *device_address)
1246{
1247 // when an A2DP device is connected, open an A2DP and a duplicated output
1248 LOGV("opening A2DP output for device %s", device_address);
1249 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1250 outputDesc->mDevice = device;
1251 mA2dpOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1252 &outputDesc->mSamplingRate,
1253 &outputDesc->mFormat,
1254 &outputDesc->mChannels,
1255 &outputDesc->mLatency,
1256 outputDesc->mFlags);
1257 if (mA2dpOutput) {
1258 // add A2DP output descriptor
1259 addOutput(mA2dpOutput, outputDesc);
Eric Laurentde070132010-07-13 04:45:46 -07001260
1261 //TODO: configure audio effect output stage here
1262
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 // set initial stream volume for A2DP device
1264 applyStreamVolumes(mA2dpOutput, device);
1265 if (a2dpUsedForSonification()) {
1266 mDuplicatedOutput = mpClientInterface->openDuplicateOutput(mA2dpOutput, mHardwareOutput);
1267 }
1268 if (mDuplicatedOutput != 0 ||
1269 !a2dpUsedForSonification()) {
1270 // If both A2DP and duplicated outputs are open, send device address to A2DP hardware
1271 // interface
1272 AudioParameter param;
1273 param.add(String8("a2dp_sink_address"), String8(device_address));
1274 mpClientInterface->setParameters(mA2dpOutput, param.toString());
1275 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1276
1277 if (a2dpUsedForSonification()) {
1278 // add duplicated output descriptor
1279 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor();
1280 dupOutputDesc->mOutput1 = mOutputs.valueFor(mHardwareOutput);
1281 dupOutputDesc->mOutput2 = mOutputs.valueFor(mA2dpOutput);
1282 dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate;
1283 dupOutputDesc->mFormat = outputDesc->mFormat;
1284 dupOutputDesc->mChannels = outputDesc->mChannels;
1285 dupOutputDesc->mLatency = outputDesc->mLatency;
1286 addOutput(mDuplicatedOutput, dupOutputDesc);
1287 applyStreamVolumes(mDuplicatedOutput, device);
1288 }
1289 } else {
1290 LOGW("getOutput() could not open duplicated output for %d and %d",
1291 mHardwareOutput, mA2dpOutput);
1292 mpClientInterface->closeOutput(mA2dpOutput);
1293 mOutputs.removeItem(mA2dpOutput);
1294 mA2dpOutput = 0;
1295 delete outputDesc;
1296 return NO_INIT;
1297 }
1298 } else {
1299 LOGW("setDeviceConnectionState() could not open A2DP output for device %x", device);
1300 delete outputDesc;
1301 return NO_INIT;
1302 }
1303 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1304
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 if (!a2dpUsedForSonification()) {
1306 // mute music on A2DP output if a notification or ringtone is playing
1307 uint32_t refCount = hwOutputDesc->strategyRefCount(STRATEGY_SONIFICATION);
1308 for (uint32_t i = 0; i < refCount; i++) {
1309 setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
1310 }
1311 }
Eric Laurent075a1f62010-11-02 12:02:20 -07001312
1313 mA2dpSuspended = false;
1314
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315 return NO_ERROR;
1316}
1317
1318status_t AudioPolicyManagerBase::handleA2dpDisconnection(AudioSystem::audio_devices device,
1319 const char *device_address)
1320{
1321 if (mA2dpOutput == 0) {
1322 LOGW("setDeviceConnectionState() disconnecting A2DP and no A2DP output!");
1323 return INVALID_OPERATION;
1324 }
1325
1326 if (mA2dpDeviceAddress != device_address) {
1327 LOGW("setDeviceConnectionState() disconnecting unknow A2DP sink address %s", device_address);
1328 return INVALID_OPERATION;
1329 }
1330
1331 // mute media strategy to avoid outputting sound on hardware output while music stream
1332 // is switched from A2DP output and before music is paused by music application
1333 setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
1334 setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
1335
1336 if (!a2dpUsedForSonification()) {
1337 // unmute music on A2DP output if a notification or ringtone is playing
1338 uint32_t refCount = mOutputs.valueFor(mHardwareOutput)->strategyRefCount(STRATEGY_SONIFICATION);
1339 for (uint32_t i = 0; i < refCount; i++) {
1340 setStrategyMute(STRATEGY_MEDIA, false, mA2dpOutput);
1341 }
1342 }
1343 mA2dpDeviceAddress = "";
Eric Laurent075a1f62010-11-02 12:02:20 -07001344 mA2dpSuspended = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 return NO_ERROR;
1346}
1347
1348void AudioPolicyManagerBase::closeA2dpOutputs()
1349{
1350 LOGV("setDeviceConnectionState() closing A2DP and duplicated output!");
1351
1352 if (mDuplicatedOutput != 0) {
1353 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueFor(mDuplicatedOutput);
1354 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1355 // As all active tracks on duplicated output will be deleted,
1356 // and as they were also referenced on hardware output, the reference
1357 // count for their stream type must be adjusted accordingly on
1358 // hardware output.
1359 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1360 int refCount = dupOutputDesc->mRefCount[i];
1361 hwOutputDesc->changeRefCount((AudioSystem::stream_type)i,-refCount);
1362 }
1363
1364 mpClientInterface->closeOutput(mDuplicatedOutput);
1365 delete mOutputs.valueFor(mDuplicatedOutput);
1366 mOutputs.removeItem(mDuplicatedOutput);
1367 mDuplicatedOutput = 0;
1368 }
1369 if (mA2dpOutput != 0) {
1370 AudioParameter param;
1371 param.add(String8("closing"), String8("true"));
1372 mpClientInterface->setParameters(mA2dpOutput, param.toString());
Eric Laurentde070132010-07-13 04:45:46 -07001373
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374 mpClientInterface->closeOutput(mA2dpOutput);
1375 delete mOutputs.valueFor(mA2dpOutput);
1376 mOutputs.removeItem(mA2dpOutput);
1377 mA2dpOutput = 0;
1378 }
1379}
1380
Eric Laurentc1c88e22010-08-27 17:10:36 -07001381void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382{
1383 uint32_t prevDevice = getDeviceForStrategy(strategy);
1384 uint32_t curDevice = getDeviceForStrategy(strategy, false);
1385 bool a2dpWasUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(prevDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
1386 bool a2dpIsUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(curDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
Eric Laurentde070132010-07-13 04:45:46 -07001387 audio_io_handle_t srcOutput = 0;
1388 audio_io_handle_t dstOutput = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001389
1390 if (a2dpWasUsed && !a2dpIsUsed) {
1391 bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001392 dstOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001394 LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy);
1395 srcOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001397 LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy);
1398 srcOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400 }
1401 if (a2dpIsUsed && !a2dpWasUsed) {
1402 bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001403 srcOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001405 LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy);
1406 dstOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001408 LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy);
1409 dstOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 }
Eric Laurentde070132010-07-13 04:45:46 -07001411 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412
Eric Laurentde070132010-07-13 04:45:46 -07001413 if (srcOutput != 0 && dstOutput != 0) {
1414 // Move effects associated to this strategy from previous output to new output
1415 for (size_t i = 0; i < mEffects.size(); i++) {
1416 EffectDescriptor *desc = mEffects.valueAt(i);
1417 if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE &&
1418 desc->mStrategy == strategy &&
1419 desc->mOutput == srcOutput) {
1420 LOGV("checkOutputForStrategy() moving effect %d to output %d", mEffects.keyAt(i), dstOutput);
1421 mpClientInterface->moveEffects(desc->mSession, srcOutput, dstOutput);
1422 desc->mOutput = dstOutput;
1423 }
1424 }
1425 // Move tracks associated to this strategy from previous output to new output
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1427 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
Eric Laurentde070132010-07-13 04:45:46 -07001428 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429 }
1430 }
1431 }
1432}
1433
Eric Laurentc1c88e22010-08-27 17:10:36 -07001434void AudioPolicyManagerBase::checkOutputForAllStrategies()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435{
Eric Laurentc1c88e22010-08-27 17:10:36 -07001436 checkOutputForStrategy(STRATEGY_PHONE);
1437 checkOutputForStrategy(STRATEGY_SONIFICATION);
1438 checkOutputForStrategy(STRATEGY_MEDIA);
1439 checkOutputForStrategy(STRATEGY_DTMF);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440}
1441
Eric Laurent075a1f62010-11-02 12:02:20 -07001442void AudioPolicyManagerBase::checkA2dpSuspend()
1443{
1444 // suspend A2DP output if:
1445 // (NOT already suspended) &&
1446 // ((SCO device is connected &&
1447 // (forced usage for communication || for record is SCO))) ||
1448 // (phone state is ringing || in call)
1449 //
1450 // restore A2DP output if:
1451 // (Already suspended) &&
1452 // ((SCO device is NOT connected ||
1453 // (forced usage NOT for communication && NOT for record is SCO))) &&
1454 // (phone state is NOT ringing && NOT in call)
1455 //
1456 if (mA2dpOutput == 0) {
1457 return;
1458 }
1459
1460 if (mA2dpSuspended) {
1461 if (((mScoDeviceAddress == "") ||
1462 ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1463 (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1464 ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1465 (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1466
1467 mpClientInterface->restoreOutput(mA2dpOutput);
1468 mA2dpSuspended = false;
1469 }
1470 } else {
1471 if (((mScoDeviceAddress != "") &&
1472 ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1473 (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1474 ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1475 (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1476
1477 mpClientInterface->suspendOutput(mA2dpOutput);
1478 mA2dpSuspended = true;
1479 }
1480 }
1481}
1482
1483
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484#endif
1485
1486uint32_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1487{
1488 uint32_t device = 0;
1489
1490 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1491 // check the following by order of priority to request a routing change if necessary:
1492 // 1: we are in call or the strategy phone is active on the hardware output:
1493 // use device for strategy phone
1494 // 2: the strategy sonification is active on the hardware output:
1495 // use device for strategy sonification
1496 // 3: the strategy media is active on the hardware output:
1497 // use device for strategy media
1498 // 4: the strategy DTMF is active on the hardware output:
1499 // use device for strategy DTMF
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001500 if (isInCall() ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501 outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1502 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1503 } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1504 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
1505 } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1506 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1507 } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1508 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1509 }
1510
1511 LOGV("getNewDevice() selected device %x", device);
1512 return device;
1513}
1514
Eric Laurentde070132010-07-13 04:45:46 -07001515uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1516 return (uint32_t)getStrategy(stream);
1517}
1518
1519AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1520 AudioSystem::stream_type stream) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 // stream to strategy mapping
1522 switch (stream) {
1523 case AudioSystem::VOICE_CALL:
1524 case AudioSystem::BLUETOOTH_SCO:
1525 return STRATEGY_PHONE;
1526 case AudioSystem::RING:
1527 case AudioSystem::NOTIFICATION:
1528 case AudioSystem::ALARM:
1529 case AudioSystem::ENFORCED_AUDIBLE:
1530 return STRATEGY_SONIFICATION;
1531 case AudioSystem::DTMF:
1532 return STRATEGY_DTMF;
1533 default:
1534 LOGE("unknown stream type");
1535 case AudioSystem::SYSTEM:
1536 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1537 // while key clicks are played produces a poor result
1538 case AudioSystem::TTS:
1539 case AudioSystem::MUSIC:
1540 return STRATEGY_MEDIA;
1541 }
1542}
1543
1544uint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1545{
1546 uint32_t device = 0;
1547
1548 if (fromCache) {
1549 LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
1550 return mDeviceForStrategy[strategy];
1551 }
1552
1553 switch (strategy) {
1554 case STRATEGY_DTMF:
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001555 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 // when off call, DTMF strategy follows the same rules as MEDIA strategy
1557 device = getDeviceForStrategy(STRATEGY_MEDIA, false);
1558 break;
1559 }
1560 // when in call, DTMF and PHONE strategies follow the same rules
1561 // FALL THROUGH
1562
1563 case STRATEGY_PHONE:
1564 // for phone strategy, we first consider the forced use and then the available devices by order
1565 // of priority
1566 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
1567 case AudioSystem::FORCE_BT_SCO:
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001568 if (!isInCall() || strategy != STRATEGY_DTMF) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1570 if (device) break;
1571 }
1572 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
1573 if (device) break;
1574 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
1575 if (device) break;
1576 // if SCO device is requested but no SCO device is available, fall back to default case
1577 // FALL THROUGH
1578
1579 default: // FORCE_NONE
1580 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1581 if (device) break;
1582 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1583 if (device) break;
1584#ifdef WITH_A2DP
1585 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001586 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1588 if (device) break;
1589 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1590 if (device) break;
1591 }
1592#endif
1593 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1594 if (device == 0) {
1595 LOGE("getDeviceForStrategy() earpiece device not found");
1596 }
1597 break;
1598
1599 case AudioSystem::FORCE_SPEAKER:
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001600 if (!isInCall() || strategy != STRATEGY_DTMF) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001601 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1602 if (device) break;
1603 }
1604#ifdef WITH_A2DP
1605 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1606 // A2DP speaker when forcing to speaker output
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001607 if (!isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1609 if (device) break;
1610 }
1611#endif
1612 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1613 if (device == 0) {
1614 LOGE("getDeviceForStrategy() speaker device not found");
1615 }
1616 break;
1617 }
1618 break;
1619
1620 case STRATEGY_SONIFICATION:
1621
1622 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1623 // handleIncallSonification().
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001624 if (isInCall()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625 device = getDeviceForStrategy(STRATEGY_PHONE, false);
1626 break;
1627 }
1628 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1629 if (device == 0) {
1630 LOGE("getDeviceForStrategy() speaker device not found");
1631 }
1632 // The second device used for sonification is the same as the device used by media strategy
1633 // FALL THROUGH
1634
1635 case STRATEGY_MEDIA: {
1636 uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1637 if (device2 == 0) {
1638 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1639 }
1640 if (device2 == 0) {
1641 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1642 }
1643#ifdef WITH_A2DP
1644 if (mA2dpOutput != 0) {
1645 if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
1646 break;
1647 }
1648 if (device2 == 0) {
1649 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1650 }
1651 if (device2 == 0) {
1652 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1653 }
1654 if (device2 == 0) {
1655 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1656 }
1657 }
1658#endif
1659 if (device2 == 0) {
1660 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1661 }
1662
1663 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise
1664 device |= device2;
1665 if (device == 0) {
1666 LOGE("getDeviceForStrategy() speaker device not found");
1667 }
1668 } break;
1669
1670 default:
1671 LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1672 break;
1673 }
1674
1675 LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1676 return device;
1677}
1678
1679void AudioPolicyManagerBase::updateDeviceForStrategy()
1680{
1681 for (int i = 0; i < NUM_STRATEGIES; i++) {
1682 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false);
1683 }
1684}
1685
1686void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
1687{
1688 LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
1689 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1690
1691
1692 if (outputDesc->isDuplicated()) {
1693 setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
1694 setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
1695 return;
1696 }
1697#ifdef WITH_A2DP
1698 // filter devices according to output selected
1699 if (output == mA2dpOutput) {
1700 device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
1701 } else {
1702 device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
1703 }
1704#endif
1705
1706 uint32_t prevDevice = (uint32_t)outputDesc->device();
1707 // Do not change the routing if:
1708 // - the requestede device is 0
1709 // - the requested device is the same as current device and force is not specified.
1710 // Doing this check here allows the caller to call setOutputDevice() without conditions
1711 if ((device == 0 || device == prevDevice) && !force) {
1712 LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
1713 return;
1714 }
1715
1716 outputDesc->mDevice = device;
1717 // mute media streams if both speaker and headset are selected
1718 if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) {
1719 setStrategyMute(STRATEGY_MEDIA, true, output);
1720 // wait for the PCM output buffers to empty before proceeding with the rest of the command
1721 usleep(outputDesc->mLatency*2*1000);
1722 }
Eric Laurent075a1f62010-11-02 12:02:20 -07001723
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 // do the routing
1725 AudioParameter param = AudioParameter();
1726 param.addInt(String8(AudioParameter::keyRouting), (int)device);
1727 mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
1728 // update stream volumes according to new device
1729 applyStreamVolumes(output, device, delayMs);
1730
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 // if changing from a combined headset + speaker route, unmute media streams
1732 if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) {
1733 setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
1734 }
1735}
1736
1737uint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
1738{
1739 uint32_t device;
1740
1741 switch(inputSource) {
1742 case AUDIO_SOURCE_DEFAULT:
1743 case AUDIO_SOURCE_MIC:
1744 case AUDIO_SOURCE_VOICE_RECOGNITION:
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001745 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001746 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1747 mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1748 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1749 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
1750 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
1751 } else {
1752 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1753 }
1754 break;
1755 case AUDIO_SOURCE_CAMCORDER:
1756 if (hasBackMicrophone()) {
1757 device = AudioSystem::DEVICE_IN_BACK_MIC;
1758 } else {
1759 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1760 }
1761 break;
1762 case AUDIO_SOURCE_VOICE_UPLINK:
1763 case AUDIO_SOURCE_VOICE_DOWNLINK:
1764 case AUDIO_SOURCE_VOICE_CALL:
1765 device = AudioSystem::DEVICE_IN_VOICE_CALL;
1766 break;
1767 default:
1768 LOGW("getInput() invalid input source %d", inputSource);
1769 device = 0;
1770 break;
1771 }
1772 LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1773 return device;
1774}
1775
1776audio_io_handle_t AudioPolicyManagerBase::getActiveInput()
1777{
1778 for (size_t i = 0; i < mInputs.size(); i++) {
1779 if (mInputs.valueAt(i)->mRefCount > 0) {
1780 return mInputs.keyAt(i);
1781 }
1782 }
1783 return 0;
1784}
1785
1786float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
1787{
1788 float volume = 1.0;
1789 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1790 StreamDescriptor &streamDesc = mStreams[stream];
1791
1792 if (device == 0) {
1793 device = outputDesc->device();
1794 }
1795
1796 int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin);
1797 volume = AudioSystem::linearToLog(volInt);
1798
1799 // if a headset is connected, apply the following rules to ring tones and notifications
1800 // to avoid sound level bursts in user's ears:
1801 // - always attenuate ring tones and notifications volume by 6dB
1802 // - if music is playing, always limit the volume to current music volume,
1803 // with a minimum threshold at -36dB so that notification is always perceived.
1804 if ((device &
1805 (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
1806 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1807 AudioSystem::DEVICE_OUT_WIRED_HEADSET |
1808 AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
1809 (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) &&
1810 streamDesc.mCanBeMuted) {
1811 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
1812 // when the phone is ringing we must consider that music could have been paused just before
1813 // by the music application and behave as if music was active if the last music track was
1814 // just stopped
1815 if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
1816 float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device);
1817 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
1818 if (volume > minVol) {
1819 volume = minVol;
1820 LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
1821 }
1822 }
1823 }
1824
1825 return volume;
1826}
1827
1828status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1829{
1830
1831 // do not change actual stream volume if the stream is muted
1832 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1833 LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1834 return NO_ERROR;
1835 }
1836
1837 // do not change in call volume if bluetooth is connected and vice versa
1838 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1839 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1840 LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1841 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1842 return INVALID_OPERATION;
1843 }
1844
1845 float volume = computeVolume(stream, index, output, device);
Eric Laurented7c6712010-12-01 14:25:39 -08001846 // We actually change the volume if:
1847 // - the float value returned by computeVolume() changed
1848 // - the force flag is set
1849 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1850 force) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1852 LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1853 if (stream == AudioSystem::VOICE_CALL ||
1854 stream == AudioSystem::DTMF ||
1855 stream == AudioSystem::BLUETOOTH_SCO) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 // offset value to reflect actual hardware volume that never reaches 0
1857 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
1858 volume = 0.01 + 0.99 * volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859 }
1860 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1861 }
1862
Eric Laurented7c6712010-12-01 14:25:39 -08001863 if (stream == AudioSystem::VOICE_CALL ||
1864 stream == AudioSystem::BLUETOOTH_SCO) {
1865 float voiceVolume;
1866 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1867 if (stream == AudioSystem::VOICE_CALL) {
1868 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1869 } else {
1870 voiceVolume = 1.0;
1871 }
1872 if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) {
1873 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1874 mLastVoiceVolume = voiceVolume;
1875 }
1876 }
1877
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 return NO_ERROR;
1879}
1880
1881void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs)
1882{
1883 LOGV("applyStreamVolumes() for output %d and device %x", output, device);
1884
1885 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1886 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs);
1887 }
1888}
1889
1890void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
1891{
1892 LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
1893 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1894 if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
1895 setStreamMute(stream, on, output, delayMs);
1896 }
1897 }
1898}
1899
1900void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
1901{
1902 StreamDescriptor &streamDesc = mStreams[stream];
1903 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1904
1905 LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
1906
1907 if (on) {
1908 if (outputDesc->mMuteCount[stream] == 0) {
1909 if (streamDesc.mCanBeMuted) {
1910 checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
1911 }
1912 }
1913 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
1914 outputDesc->mMuteCount[stream]++;
1915 } else {
1916 if (outputDesc->mMuteCount[stream] == 0) {
1917 LOGW("setStreamMute() unmuting non muted stream!");
1918 return;
1919 }
1920 if (--outputDesc->mMuteCount[stream] == 0) {
1921 checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
1922 }
1923 }
1924}
1925
1926void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
1927{
1928 // if the stream pertains to sonification strategy and we are in call we must
1929 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1930 // in the device used for phone strategy and play the tone if the selected device does not
1931 // interfere with the device used for phone strategy
1932 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1933 // many times as there are active tracks on the output
1934
1935 if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
1936 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
1937 LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1938 stream, starting, outputDesc->mDevice, stateChange);
1939 if (outputDesc->mRefCount[stream]) {
1940 int muteCount = 1;
1941 if (stateChange) {
1942 muteCount = outputDesc->mRefCount[stream];
1943 }
1944 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
1945 LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1946 for (int i = 0; i < muteCount; i++) {
1947 setStreamMute(stream, starting, mHardwareOutput);
1948 }
1949 } else {
1950 LOGV("handleIncallSonification() high visibility");
1951 if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) {
1952 LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1953 for (int i = 0; i < muteCount; i++) {
1954 setStreamMute(stream, starting, mHardwareOutput);
1955 }
1956 }
1957 if (starting) {
1958 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
1959 } else {
1960 mpClientInterface->stopTone();
1961 }
1962 }
1963 }
1964 }
1965}
1966
Jean-Michel Trivibfa2f132010-11-15 12:11:32 -08001967bool AudioPolicyManagerBase::isInCall()
1968{
1969 return isStateInCall(mPhoneState);
1970}
1971
1972bool AudioPolicyManagerBase::isStateInCall(int state) {
1973 return ((state == AudioSystem::MODE_IN_CALL) ||
1974 (state == AudioSystem::MODE_IN_COMMUNICATION));
1975}
1976
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
1978 uint32_t samplingRate,
1979 uint32_t format,
1980 uint32_t channels,
1981 AudioSystem::output_flags flags,
1982 uint32_t device)
1983{
1984 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
1985 (format !=0 && !AudioSystem::isLinearPCM(format)));
1986}
1987
Eric Laurentde070132010-07-13 04:45:46 -07001988uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
1989{
1990 return MAX_EFFECTS_CPU_LOAD;
1991}
1992
1993uint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
1994{
1995 return MAX_EFFECTS_MEMORY;
1996}
1997
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998// --- AudioOutputDescriptor class implementation
1999
2000AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor()
2001 : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
2002 mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0)
2003{
2004 // clear usage count for all stream types
2005 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2006 mRefCount[i] = 0;
2007 mCurVolume[i] = -1.0;
2008 mMuteCount[i] = 0;
2009 }
2010}
2011
2012uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2013{
2014 uint32_t device = 0;
2015 if (isDuplicated()) {
2016 device = mOutput1->mDevice | mOutput2->mDevice;
2017 } else {
2018 device = mDevice;
2019 }
2020 return device;
2021}
2022
2023void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2024{
2025 // forward usage count change to attached outputs
2026 if (isDuplicated()) {
2027 mOutput1->changeRefCount(stream, delta);
2028 mOutput2->changeRefCount(stream, delta);
2029 }
2030 if ((delta + (int)mRefCount[stream]) < 0) {
2031 LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2032 mRefCount[stream] = 0;
2033 return;
2034 }
2035 mRefCount[stream] += delta;
2036 LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2037}
2038
2039uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2040{
2041 uint32_t refcount = 0;
2042 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2043 refcount += mRefCount[i];
2044 }
2045 return refcount;
2046}
2047
2048uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2049{
2050 uint32_t refCount = 0;
2051 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2052 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2053 refCount += mRefCount[i];
2054 }
2055 }
2056 return refCount;
2057}
2058
2059
2060status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2061{
2062 const size_t SIZE = 256;
2063 char buffer[SIZE];
2064 String8 result;
2065
2066 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2067 result.append(buffer);
2068 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2069 result.append(buffer);
2070 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2071 result.append(buffer);
2072 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2073 result.append(buffer);
2074 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2075 result.append(buffer);
2076 snprintf(buffer, SIZE, " Devices %08x\n", device());
2077 result.append(buffer);
2078 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2079 result.append(buffer);
2080 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2081 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2082 result.append(buffer);
2083 }
2084 write(fd, result.string(), result.size());
2085
2086 return NO_ERROR;
2087}
2088
2089// --- AudioInputDescriptor class implementation
2090
2091AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor()
2092 : mSamplingRate(0), mFormat(0), mChannels(0),
2093 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
2094{
2095}
2096
2097status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2098{
2099 const size_t SIZE = 256;
2100 char buffer[SIZE];
2101 String8 result;
2102
2103 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2104 result.append(buffer);
2105 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2106 result.append(buffer);
2107 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2108 result.append(buffer);
2109 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2110 result.append(buffer);
2111 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2112 result.append(buffer);
2113 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2114 result.append(buffer);
2115 write(fd, result.string(), result.size());
2116
2117 return NO_ERROR;
2118}
2119
2120// --- StreamDescriptor class implementation
2121
2122void AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size)
2123{
2124 snprintf(buffer, size, " %02d %02d %02d %d\n",
2125 mIndexMin,
2126 mIndexMax,
2127 mIndexCur,
2128 mCanBeMuted);
2129}
2130
Eric Laurentde070132010-07-13 04:45:46 -07002131// --- EffectDescriptor class implementation
2132
2133status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2134{
2135 const size_t SIZE = 256;
2136 char buffer[SIZE];
2137 String8 result;
2138
2139 snprintf(buffer, SIZE, " Output: %d\n", mOutput);
2140 result.append(buffer);
2141 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2142 result.append(buffer);
2143 snprintf(buffer, SIZE, " Session: %d\n", mSession);
2144 result.append(buffer);
2145 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
2146 result.append(buffer);
2147 write(fd, result.string(), result.size());
2148
2149 return NO_ERROR;
2150}
2151
2152
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153
2154}; // namespace android