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