blob: 8d16ab4eeaa3c94b1fbd8a22430c274922d279b3 [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()
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 Laurentc1c88e22010-08-27 17:10:36 -0700266 checkOutputForAllStrategies();
Eric Laurent075a1f62010-11-02 12:02:20 -0700267 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268#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 }
278
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
291 // change routing is necessary
292 setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
293
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);
298 // 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 }
303 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
329 bool forceVolumeReeval = false;
330 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 }
360 forceVolumeReeval = true;
361 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 Laurentc1c88e22010-08-27 17:10:36 -0700371 checkOutputForAllStrategies();
Eric Laurent075a1f62010-11-02 12:02:20 -0700372 checkA2dpSuspend();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373#endif
374 updateDeviceForStrategy();
375 setOutputDevice(mHardwareOutput, newDevice);
376 if (forceVolumeReeval) {
377 applyStreamVolumes(mHardwareOutput, newDevice);
378 }
Eric Laurentc1c88e22010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394}
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
459 // open a direct output if required by specified parameters
460 if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
461
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);
470 outputDesc->mRefCount[stream] = 0;
471 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 Laurentde070132010-07-13 04:45:46 -0700539status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
540 AudioSystem::stream_type stream,
541 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542{
Eric Laurentde070132010-07-13 04:45:46 -0700543 LOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 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 Laurentde070132010-07-13 04:45:46 -0700577status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
578 AudioSystem::stream_type stream,
579 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580{
Eric Laurentde070132010-07-13 04:45:46 -0700581 LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 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 Laurentde070132010-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);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 }
614#endif
615 if (output != mHardwareOutput) {
616 setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true);
617 }
618 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 Laurentde070132010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914status_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 Laurentde070132010-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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 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 Laurented7c6712010-12-01 14:25:39 -08001003 mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0),
1004 mMusicStopTime(0), mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1005 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent075a1f62010-11-02 12:02:20 -07001006 mA2dpSuspended(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007{
1008 mpClientInterface = clientInterface;
1009
1010 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1011 mForceUse[i] = AudioSystem::FORCE_NONE;
1012 }
1013
1014 // devices available by default are speaker, ear piece and microphone
1015 mAvailableOutputDevices = AudioSystem::DEVICE_OUT_EARPIECE |
1016 AudioSystem::DEVICE_OUT_SPEAKER;
1017 mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1018
1019#ifdef WITH_A2DP
1020 mA2dpOutput = 0;
1021 mDuplicatedOutput = 0;
1022 mA2dpDeviceAddress = String8("");
1023#endif
1024 mScoDeviceAddress = String8("");
1025
1026 // open hardware output
1027 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1028 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1029 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1030 &outputDesc->mSamplingRate,
1031 &outputDesc->mFormat,
1032 &outputDesc->mChannels,
1033 &outputDesc->mLatency,
1034 outputDesc->mFlags);
1035
1036 if (mHardwareOutput == 0) {
1037 LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d",
1038 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1039 } else {
1040 addOutput(mHardwareOutput, outputDesc);
1041 setOutputDevice(mHardwareOutput, (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER, true);
Eric Laurentde070132010-07-13 04:45:46 -07001042 //TODO: configure audio effect output stage here
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 }
1044
1045 updateDeviceForStrategy();
1046#ifdef AUDIO_POLICY_TEST
1047 AudioParameter outputCmd = AudioParameter();
1048 outputCmd.addInt(String8("set_id"), 0);
1049 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1050
1051 mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1052 mTestSamplingRate = 44100;
1053 mTestFormat = AudioSystem::PCM_16_BIT;
1054 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO;
1055 mTestLatencyMs = 0;
1056 mCurOutput = 0;
1057 mDirectOutput = false;
1058 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1059 mTestOutputs[i] = 0;
1060 }
1061
1062 const size_t SIZE = 256;
1063 char buffer[SIZE];
1064 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1065 run(buffer, ANDROID_PRIORITY_AUDIO);
1066#endif //AUDIO_POLICY_TEST
1067}
1068
1069AudioPolicyManagerBase::~AudioPolicyManagerBase()
1070{
1071#ifdef AUDIO_POLICY_TEST
1072 exit();
1073#endif //AUDIO_POLICY_TEST
1074 for (size_t i = 0; i < mOutputs.size(); i++) {
1075 mpClientInterface->closeOutput(mOutputs.keyAt(i));
1076 delete mOutputs.valueAt(i);
1077 }
1078 mOutputs.clear();
1079 for (size_t i = 0; i < mInputs.size(); i++) {
1080 mpClientInterface->closeInput(mInputs.keyAt(i));
1081 delete mInputs.valueAt(i);
1082 }
1083 mInputs.clear();
1084}
1085
1086#ifdef AUDIO_POLICY_TEST
1087bool AudioPolicyManagerBase::threadLoop()
1088{
1089 LOGV("entering threadLoop()");
1090 while (!exitPending())
1091 {
1092 String8 command;
1093 int valueInt;
1094 String8 value;
1095
1096 Mutex::Autolock _l(mLock);
1097 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1098
1099 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1100 AudioParameter param = AudioParameter(command);
1101
1102 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1103 valueInt != 0) {
1104 LOGV("Test command %s received", command.string());
1105 String8 target;
1106 if (param.get(String8("target"), target) != NO_ERROR) {
1107 target = "Manager";
1108 }
1109 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1110 param.remove(String8("test_cmd_policy_output"));
1111 mCurOutput = valueInt;
1112 }
1113 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1114 param.remove(String8("test_cmd_policy_direct"));
1115 if (value == "false") {
1116 mDirectOutput = false;
1117 } else if (value == "true") {
1118 mDirectOutput = true;
1119 }
1120 }
1121 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1122 param.remove(String8("test_cmd_policy_input"));
1123 mTestInput = valueInt;
1124 }
1125
1126 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1127 param.remove(String8("test_cmd_policy_format"));
1128 int format = AudioSystem::INVALID_FORMAT;
1129 if (value == "PCM 16 bits") {
1130 format = AudioSystem::PCM_16_BIT;
1131 } else if (value == "PCM 8 bits") {
1132 format = AudioSystem::PCM_8_BIT;
1133 } else if (value == "Compressed MP3") {
1134 format = AudioSystem::MP3;
1135 }
1136 if (format != AudioSystem::INVALID_FORMAT) {
1137 if (target == "Manager") {
1138 mTestFormat = format;
1139 } else if (mTestOutputs[mCurOutput] != 0) {
1140 AudioParameter outputParam = AudioParameter();
1141 outputParam.addInt(String8("format"), format);
1142 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1143 }
1144 }
1145 }
1146 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1147 param.remove(String8("test_cmd_policy_channels"));
1148 int channels = 0;
1149
1150 if (value == "Channels Stereo") {
1151 channels = AudioSystem::CHANNEL_OUT_STEREO;
1152 } else if (value == "Channels Mono") {
1153 channels = AudioSystem::CHANNEL_OUT_MONO;
1154 }
1155 if (channels != 0) {
1156 if (target == "Manager") {
1157 mTestChannels = channels;
1158 } else if (mTestOutputs[mCurOutput] != 0) {
1159 AudioParameter outputParam = AudioParameter();
1160 outputParam.addInt(String8("channels"), channels);
1161 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1162 }
1163 }
1164 }
1165 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1166 param.remove(String8("test_cmd_policy_sampleRate"));
1167 if (valueInt >= 0 && valueInt <= 96000) {
1168 int samplingRate = valueInt;
1169 if (target == "Manager") {
1170 mTestSamplingRate = samplingRate;
1171 } else if (mTestOutputs[mCurOutput] != 0) {
1172 AudioParameter outputParam = AudioParameter();
1173 outputParam.addInt(String8("sampling_rate"), samplingRate);
1174 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1175 }
1176 }
1177 }
1178
1179 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1180 param.remove(String8("test_cmd_policy_reopen"));
1181
1182 mpClientInterface->closeOutput(mHardwareOutput);
1183 delete mOutputs.valueFor(mHardwareOutput);
1184 mOutputs.removeItem(mHardwareOutput);
1185
1186 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1187 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1188 mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1189 &outputDesc->mSamplingRate,
1190 &outputDesc->mFormat,
1191 &outputDesc->mChannels,
1192 &outputDesc->mLatency,
1193 outputDesc->mFlags);
1194 if (mHardwareOutput == 0) {
1195 LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1196 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1197 } else {
1198 AudioParameter outputCmd = AudioParameter();
1199 outputCmd.addInt(String8("set_id"), 0);
1200 mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1201 addOutput(mHardwareOutput, outputDesc);
1202 }
1203 }
1204
1205
1206 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1207 }
1208 }
1209 return false;
1210}
1211
1212void AudioPolicyManagerBase::exit()
1213{
1214 {
1215 AutoMutex _l(mLock);
1216 requestExit();
1217 mWaitWorkCV.signal();
1218 }
1219 requestExitAndWait();
1220}
1221
1222int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1223{
1224 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1225 if (output == mTestOutputs[i]) return i;
1226 }
1227 return 0;
1228}
1229#endif //AUDIO_POLICY_TEST
1230
1231// ---
1232
1233void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1234{
1235 outputDesc->mId = id;
1236 mOutputs.add(id, outputDesc);
1237}
1238
1239
1240#ifdef WITH_A2DP
1241status_t AudioPolicyManagerBase::handleA2dpConnection(AudioSystem::audio_devices device,
1242 const char *device_address)
1243{
1244 // when an A2DP device is connected, open an A2DP and a duplicated output
1245 LOGV("opening A2DP output for device %s", device_address);
1246 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1247 outputDesc->mDevice = device;
1248 mA2dpOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1249 &outputDesc->mSamplingRate,
1250 &outputDesc->mFormat,
1251 &outputDesc->mChannels,
1252 &outputDesc->mLatency,
1253 outputDesc->mFlags);
1254 if (mA2dpOutput) {
1255 // add A2DP output descriptor
1256 addOutput(mA2dpOutput, outputDesc);
Eric Laurentde070132010-07-13 04:45:46 -07001257
1258 //TODO: configure audio effect output stage here
1259
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 // set initial stream volume for A2DP device
1261 applyStreamVolumes(mA2dpOutput, device);
1262 if (a2dpUsedForSonification()) {
1263 mDuplicatedOutput = mpClientInterface->openDuplicateOutput(mA2dpOutput, mHardwareOutput);
1264 }
1265 if (mDuplicatedOutput != 0 ||
1266 !a2dpUsedForSonification()) {
1267 // If both A2DP and duplicated outputs are open, send device address to A2DP hardware
1268 // interface
1269 AudioParameter param;
1270 param.add(String8("a2dp_sink_address"), String8(device_address));
1271 mpClientInterface->setParameters(mA2dpOutput, param.toString());
1272 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1273
1274 if (a2dpUsedForSonification()) {
1275 // add duplicated output descriptor
1276 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor();
1277 dupOutputDesc->mOutput1 = mOutputs.valueFor(mHardwareOutput);
1278 dupOutputDesc->mOutput2 = mOutputs.valueFor(mA2dpOutput);
1279 dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate;
1280 dupOutputDesc->mFormat = outputDesc->mFormat;
1281 dupOutputDesc->mChannels = outputDesc->mChannels;
1282 dupOutputDesc->mLatency = outputDesc->mLatency;
1283 addOutput(mDuplicatedOutput, dupOutputDesc);
1284 applyStreamVolumes(mDuplicatedOutput, device);
1285 }
1286 } else {
1287 LOGW("getOutput() could not open duplicated output for %d and %d",
1288 mHardwareOutput, mA2dpOutput);
1289 mpClientInterface->closeOutput(mA2dpOutput);
1290 mOutputs.removeItem(mA2dpOutput);
1291 mA2dpOutput = 0;
1292 delete outputDesc;
1293 return NO_INIT;
1294 }
1295 } else {
1296 LOGW("setDeviceConnectionState() could not open A2DP output for device %x", device);
1297 delete outputDesc;
1298 return NO_INIT;
1299 }
1300 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1301
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302 if (!a2dpUsedForSonification()) {
1303 // mute music on A2DP output if a notification or ringtone is playing
1304 uint32_t refCount = hwOutputDesc->strategyRefCount(STRATEGY_SONIFICATION);
1305 for (uint32_t i = 0; i < refCount; i++) {
1306 setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
1307 }
1308 }
Eric Laurent075a1f62010-11-02 12:02:20 -07001309
1310 mA2dpSuspended = false;
1311
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312 return NO_ERROR;
1313}
1314
1315status_t AudioPolicyManagerBase::handleA2dpDisconnection(AudioSystem::audio_devices device,
1316 const char *device_address)
1317{
1318 if (mA2dpOutput == 0) {
1319 LOGW("setDeviceConnectionState() disconnecting A2DP and no A2DP output!");
1320 return INVALID_OPERATION;
1321 }
1322
1323 if (mA2dpDeviceAddress != device_address) {
1324 LOGW("setDeviceConnectionState() disconnecting unknow A2DP sink address %s", device_address);
1325 return INVALID_OPERATION;
1326 }
1327
1328 // mute media strategy to avoid outputting sound on hardware output while music stream
1329 // is switched from A2DP output and before music is paused by music application
1330 setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
1331 setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
1332
1333 if (!a2dpUsedForSonification()) {
1334 // unmute music on A2DP output if a notification or ringtone is playing
1335 uint32_t refCount = mOutputs.valueFor(mHardwareOutput)->strategyRefCount(STRATEGY_SONIFICATION);
1336 for (uint32_t i = 0; i < refCount; i++) {
1337 setStrategyMute(STRATEGY_MEDIA, false, mA2dpOutput);
1338 }
1339 }
1340 mA2dpDeviceAddress = "";
Eric Laurent075a1f62010-11-02 12:02:20 -07001341 mA2dpSuspended = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 return NO_ERROR;
1343}
1344
1345void AudioPolicyManagerBase::closeA2dpOutputs()
1346{
1347 LOGV("setDeviceConnectionState() closing A2DP and duplicated output!");
1348
1349 if (mDuplicatedOutput != 0) {
1350 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueFor(mDuplicatedOutput);
1351 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1352 // As all active tracks on duplicated output will be deleted,
1353 // and as they were also referenced on hardware output, the reference
1354 // count for their stream type must be adjusted accordingly on
1355 // hardware output.
1356 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1357 int refCount = dupOutputDesc->mRefCount[i];
1358 hwOutputDesc->changeRefCount((AudioSystem::stream_type)i,-refCount);
1359 }
1360
1361 mpClientInterface->closeOutput(mDuplicatedOutput);
1362 delete mOutputs.valueFor(mDuplicatedOutput);
1363 mOutputs.removeItem(mDuplicatedOutput);
1364 mDuplicatedOutput = 0;
1365 }
1366 if (mA2dpOutput != 0) {
1367 AudioParameter param;
1368 param.add(String8("closing"), String8("true"));
1369 mpClientInterface->setParameters(mA2dpOutput, param.toString());
Eric Laurentde070132010-07-13 04:45:46 -07001370
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371 mpClientInterface->closeOutput(mA2dpOutput);
1372 delete mOutputs.valueFor(mA2dpOutput);
1373 mOutputs.removeItem(mA2dpOutput);
1374 mA2dpOutput = 0;
1375 }
1376}
1377
Eric Laurentc1c88e22010-08-27 17:10:36 -07001378void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379{
1380 uint32_t prevDevice = getDeviceForStrategy(strategy);
1381 uint32_t curDevice = getDeviceForStrategy(strategy, false);
1382 bool a2dpWasUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(prevDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
1383 bool a2dpIsUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(curDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
Eric Laurentde070132010-07-13 04:45:46 -07001384 audio_io_handle_t srcOutput = 0;
1385 audio_io_handle_t dstOutput = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386
1387 if (a2dpWasUsed && !a2dpIsUsed) {
1388 bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001389 dstOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001391 LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy);
1392 srcOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001394 LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy);
1395 srcOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397 }
1398 if (a2dpIsUsed && !a2dpWasUsed) {
1399 bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2);
Eric Laurentde070132010-07-13 04:45:46 -07001400 srcOutput = mHardwareOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 if (dupUsed) {
Eric Laurentde070132010-07-13 04:45:46 -07001402 LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy);
1403 dstOutput = mDuplicatedOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 } else {
Eric Laurentde070132010-07-13 04:45:46 -07001405 LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy);
1406 dstOutput = mA2dpOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 }
Eric Laurentde070132010-07-13 04:45:46 -07001408 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
Eric Laurentde070132010-07-13 04:45:46 -07001410 if (srcOutput != 0 && dstOutput != 0) {
1411 // Move effects associated to this strategy from previous output to new output
1412 for (size_t i = 0; i < mEffects.size(); i++) {
1413 EffectDescriptor *desc = mEffects.valueAt(i);
1414 if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE &&
1415 desc->mStrategy == strategy &&
1416 desc->mOutput == srcOutput) {
1417 LOGV("checkOutputForStrategy() moving effect %d to output %d", mEffects.keyAt(i), dstOutput);
1418 mpClientInterface->moveEffects(desc->mSession, srcOutput, dstOutput);
1419 desc->mOutput = dstOutput;
1420 }
1421 }
1422 // Move tracks associated to this strategy from previous output to new output
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1424 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
Eric Laurentde070132010-07-13 04:45:46 -07001425 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 }
1427 }
1428 }
1429}
1430
Eric Laurentc1c88e22010-08-27 17:10:36 -07001431void AudioPolicyManagerBase::checkOutputForAllStrategies()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432{
Eric Laurentc1c88e22010-08-27 17:10:36 -07001433 checkOutputForStrategy(STRATEGY_PHONE);
1434 checkOutputForStrategy(STRATEGY_SONIFICATION);
1435 checkOutputForStrategy(STRATEGY_MEDIA);
1436 checkOutputForStrategy(STRATEGY_DTMF);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437}
1438
Eric Laurent075a1f62010-11-02 12:02:20 -07001439void AudioPolicyManagerBase::checkA2dpSuspend()
1440{
1441 // suspend A2DP output if:
1442 // (NOT already suspended) &&
1443 // ((SCO device is connected &&
1444 // (forced usage for communication || for record is SCO))) ||
1445 // (phone state is ringing || in call)
1446 //
1447 // restore A2DP output if:
1448 // (Already suspended) &&
1449 // ((SCO device is NOT connected ||
1450 // (forced usage NOT for communication && NOT for record is SCO))) &&
1451 // (phone state is NOT ringing && NOT in call)
1452 //
1453 if (mA2dpOutput == 0) {
1454 return;
1455 }
1456
1457 if (mA2dpSuspended) {
1458 if (((mScoDeviceAddress == "") ||
1459 ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1460 (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1461 ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1462 (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1463
1464 mpClientInterface->restoreOutput(mA2dpOutput);
1465 mA2dpSuspended = false;
1466 }
1467 } else {
1468 if (((mScoDeviceAddress != "") &&
1469 ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1470 (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1471 ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1472 (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1473
1474 mpClientInterface->suspendOutput(mA2dpOutput);
1475 mA2dpSuspended = true;
1476 }
1477 }
1478}
1479
1480
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481#endif
1482
1483uint32_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1484{
1485 uint32_t device = 0;
1486
1487 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1488 // check the following by order of priority to request a routing change if necessary:
1489 // 1: we are in call or the strategy phone is active on the hardware output:
1490 // use device for strategy phone
1491 // 2: the strategy sonification is active on the hardware output:
1492 // use device for strategy sonification
1493 // 3: the strategy media is active on the hardware output:
1494 // use device for strategy media
1495 // 4: the strategy DTMF is active on the hardware output:
1496 // use device for strategy DTMF
1497 if (mPhoneState == AudioSystem::MODE_IN_CALL ||
1498 outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1499 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1500 } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1501 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
1502 } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1503 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1504 } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1505 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1506 }
1507
1508 LOGV("getNewDevice() selected device %x", device);
1509 return device;
1510}
1511
Eric Laurentde070132010-07-13 04:45:46 -07001512uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1513 return (uint32_t)getStrategy(stream);
1514}
1515
1516AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1517 AudioSystem::stream_type stream) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001518 // stream to strategy mapping
1519 switch (stream) {
1520 case AudioSystem::VOICE_CALL:
1521 case AudioSystem::BLUETOOTH_SCO:
1522 return STRATEGY_PHONE;
1523 case AudioSystem::RING:
1524 case AudioSystem::NOTIFICATION:
1525 case AudioSystem::ALARM:
1526 case AudioSystem::ENFORCED_AUDIBLE:
1527 return STRATEGY_SONIFICATION;
1528 case AudioSystem::DTMF:
1529 return STRATEGY_DTMF;
1530 default:
1531 LOGE("unknown stream type");
1532 case AudioSystem::SYSTEM:
1533 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1534 // while key clicks are played produces a poor result
1535 case AudioSystem::TTS:
1536 case AudioSystem::MUSIC:
1537 return STRATEGY_MEDIA;
1538 }
1539}
1540
1541uint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1542{
1543 uint32_t device = 0;
1544
1545 if (fromCache) {
1546 LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
1547 return mDeviceForStrategy[strategy];
1548 }
1549
1550 switch (strategy) {
1551 case STRATEGY_DTMF:
1552 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
1553 // when off call, DTMF strategy follows the same rules as MEDIA strategy
1554 device = getDeviceForStrategy(STRATEGY_MEDIA, false);
1555 break;
1556 }
1557 // when in call, DTMF and PHONE strategies follow the same rules
1558 // FALL THROUGH
1559
1560 case STRATEGY_PHONE:
1561 // for phone strategy, we first consider the forced use and then the available devices by order
1562 // of priority
1563 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
1564 case AudioSystem::FORCE_BT_SCO:
1565 if (mPhoneState != AudioSystem::MODE_IN_CALL || strategy != STRATEGY_DTMF) {
1566 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1567 if (device) break;
1568 }
1569 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
1570 if (device) break;
1571 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
1572 if (device) break;
1573 // if SCO device is requested but no SCO device is available, fall back to default case
1574 // FALL THROUGH
1575
1576 default: // FORCE_NONE
1577 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1578 if (device) break;
1579 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1580 if (device) break;
1581#ifdef WITH_A2DP
1582 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
1583 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
1584 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1585 if (device) break;
1586 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1587 if (device) break;
1588 }
1589#endif
1590 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1591 if (device == 0) {
1592 LOGE("getDeviceForStrategy() earpiece device not found");
1593 }
1594 break;
1595
1596 case AudioSystem::FORCE_SPEAKER:
1597 if (mPhoneState != AudioSystem::MODE_IN_CALL || strategy != STRATEGY_DTMF) {
1598 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1599 if (device) break;
1600 }
1601#ifdef WITH_A2DP
1602 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1603 // A2DP speaker when forcing to speaker output
1604 if (mPhoneState != AudioSystem::MODE_IN_CALL) {
1605 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1606 if (device) break;
1607 }
1608#endif
1609 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1610 if (device == 0) {
1611 LOGE("getDeviceForStrategy() speaker device not found");
1612 }
1613 break;
1614 }
1615 break;
1616
1617 case STRATEGY_SONIFICATION:
1618
1619 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1620 // handleIncallSonification().
1621 if (mPhoneState == AudioSystem::MODE_IN_CALL) {
1622 device = getDeviceForStrategy(STRATEGY_PHONE, false);
1623 break;
1624 }
1625 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1626 if (device == 0) {
1627 LOGE("getDeviceForStrategy() speaker device not found");
1628 }
1629 // The second device used for sonification is the same as the device used by media strategy
1630 // FALL THROUGH
1631
1632 case STRATEGY_MEDIA: {
1633 uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1634 if (device2 == 0) {
1635 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1636 }
1637 if (device2 == 0) {
1638 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1639 }
1640#ifdef WITH_A2DP
1641 if (mA2dpOutput != 0) {
1642 if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
1643 break;
1644 }
1645 if (device2 == 0) {
1646 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1647 }
1648 if (device2 == 0) {
1649 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1650 }
1651 if (device2 == 0) {
1652 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1653 }
1654 }
1655#endif
1656 if (device2 == 0) {
1657 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1658 }
1659
1660 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise
1661 device |= device2;
1662 if (device == 0) {
1663 LOGE("getDeviceForStrategy() speaker device not found");
1664 }
1665 } break;
1666
1667 default:
1668 LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1669 break;
1670 }
1671
1672 LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1673 return device;
1674}
1675
1676void AudioPolicyManagerBase::updateDeviceForStrategy()
1677{
1678 for (int i = 0; i < NUM_STRATEGIES; i++) {
1679 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false);
1680 }
1681}
1682
1683void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
1684{
1685 LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
1686 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1687
1688
1689 if (outputDesc->isDuplicated()) {
1690 setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
1691 setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
1692 return;
1693 }
1694#ifdef WITH_A2DP
1695 // filter devices according to output selected
1696 if (output == mA2dpOutput) {
1697 device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
1698 } else {
1699 device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
1700 }
1701#endif
1702
1703 uint32_t prevDevice = (uint32_t)outputDesc->device();
1704 // Do not change the routing if:
1705 // - the requestede device is 0
1706 // - the requested device is the same as current device and force is not specified.
1707 // Doing this check here allows the caller to call setOutputDevice() without conditions
1708 if ((device == 0 || device == prevDevice) && !force) {
1709 LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
1710 return;
1711 }
1712
1713 outputDesc->mDevice = device;
1714 // mute media streams if both speaker and headset are selected
1715 if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) {
1716 setStrategyMute(STRATEGY_MEDIA, true, output);
1717 // wait for the PCM output buffers to empty before proceeding with the rest of the command
1718 usleep(outputDesc->mLatency*2*1000);
1719 }
Eric Laurent075a1f62010-11-02 12:02:20 -07001720
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721 // do the routing
1722 AudioParameter param = AudioParameter();
1723 param.addInt(String8(AudioParameter::keyRouting), (int)device);
1724 mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
1725 // update stream volumes according to new device
1726 applyStreamVolumes(output, device, delayMs);
1727
Mathias Agopian65ab4712010-07-14 17:59:35 -07001728 // if changing from a combined headset + speaker route, unmute media streams
1729 if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) {
1730 setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
1731 }
1732}
1733
1734uint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
1735{
1736 uint32_t device;
1737
1738 switch(inputSource) {
1739 case AUDIO_SOURCE_DEFAULT:
1740 case AUDIO_SOURCE_MIC:
1741 case AUDIO_SOURCE_VOICE_RECOGNITION:
1742 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1743 mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1744 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1745 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
1746 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
1747 } else {
1748 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1749 }
1750 break;
1751 case AUDIO_SOURCE_CAMCORDER:
1752 if (hasBackMicrophone()) {
1753 device = AudioSystem::DEVICE_IN_BACK_MIC;
1754 } else {
1755 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1756 }
1757 break;
1758 case AUDIO_SOURCE_VOICE_UPLINK:
1759 case AUDIO_SOURCE_VOICE_DOWNLINK:
1760 case AUDIO_SOURCE_VOICE_CALL:
1761 device = AudioSystem::DEVICE_IN_VOICE_CALL;
1762 break;
1763 default:
1764 LOGW("getInput() invalid input source %d", inputSource);
1765 device = 0;
1766 break;
1767 }
1768 LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1769 return device;
1770}
1771
1772audio_io_handle_t AudioPolicyManagerBase::getActiveInput()
1773{
1774 for (size_t i = 0; i < mInputs.size(); i++) {
1775 if (mInputs.valueAt(i)->mRefCount > 0) {
1776 return mInputs.keyAt(i);
1777 }
1778 }
1779 return 0;
1780}
1781
1782float AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
1783{
1784 float volume = 1.0;
1785 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1786 StreamDescriptor &streamDesc = mStreams[stream];
1787
1788 if (device == 0) {
1789 device = outputDesc->device();
1790 }
1791
1792 int volInt = (100 * (index - streamDesc.mIndexMin)) / (streamDesc.mIndexMax - streamDesc.mIndexMin);
1793 volume = AudioSystem::linearToLog(volInt);
1794
1795 // if a headset is connected, apply the following rules to ring tones and notifications
1796 // to avoid sound level bursts in user's ears:
1797 // - always attenuate ring tones and notifications volume by 6dB
1798 // - if music is playing, always limit the volume to current music volume,
1799 // with a minimum threshold at -36dB so that notification is always perceived.
1800 if ((device &
1801 (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
1802 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1803 AudioSystem::DEVICE_OUT_WIRED_HEADSET |
1804 AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
1805 (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) &&
1806 streamDesc.mCanBeMuted) {
1807 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
1808 // when the phone is ringing we must consider that music could have been paused just before
1809 // by the music application and behave as if music was active if the last music track was
1810 // just stopped
1811 if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
1812 float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device);
1813 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
1814 if (volume > minVol) {
1815 volume = minVol;
1816 LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
1817 }
1818 }
1819 }
1820
1821 return volume;
1822}
1823
1824status_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1825{
1826
1827 // do not change actual stream volume if the stream is muted
1828 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1829 LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1830 return NO_ERROR;
1831 }
1832
1833 // do not change in call volume if bluetooth is connected and vice versa
1834 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1835 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1836 LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1837 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1838 return INVALID_OPERATION;
1839 }
1840
1841 float volume = computeVolume(stream, index, output, device);
Eric Laurented7c6712010-12-01 14:25:39 -08001842 // We actually change the volume if:
1843 // - the float value returned by computeVolume() changed
1844 // - the force flag is set
1845 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1846 force) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1848 LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1849 if (stream == AudioSystem::VOICE_CALL ||
1850 stream == AudioSystem::DTMF ||
1851 stream == AudioSystem::BLUETOOTH_SCO) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852 // offset value to reflect actual hardware volume that never reaches 0
1853 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
1854 volume = 0.01 + 0.99 * volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855 }
1856 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1857 }
1858
Eric Laurented7c6712010-12-01 14:25:39 -08001859 if (stream == AudioSystem::VOICE_CALL ||
1860 stream == AudioSystem::BLUETOOTH_SCO) {
1861 float voiceVolume;
1862 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1863 if (stream == AudioSystem::VOICE_CALL) {
1864 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1865 } else {
1866 voiceVolume = 1.0;
1867 }
1868 if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) {
1869 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
1870 mLastVoiceVolume = voiceVolume;
1871 }
1872 }
1873
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874 return NO_ERROR;
1875}
1876
1877void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs)
1878{
1879 LOGV("applyStreamVolumes() for output %d and device %x", output, device);
1880
1881 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1882 checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs);
1883 }
1884}
1885
1886void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
1887{
1888 LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
1889 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
1890 if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
1891 setStreamMute(stream, on, output, delayMs);
1892 }
1893 }
1894}
1895
1896void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
1897{
1898 StreamDescriptor &streamDesc = mStreams[stream];
1899 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1900
1901 LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
1902
1903 if (on) {
1904 if (outputDesc->mMuteCount[stream] == 0) {
1905 if (streamDesc.mCanBeMuted) {
1906 checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
1907 }
1908 }
1909 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
1910 outputDesc->mMuteCount[stream]++;
1911 } else {
1912 if (outputDesc->mMuteCount[stream] == 0) {
1913 LOGW("setStreamMute() unmuting non muted stream!");
1914 return;
1915 }
1916 if (--outputDesc->mMuteCount[stream] == 0) {
1917 checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
1918 }
1919 }
1920}
1921
1922void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
1923{
1924 // if the stream pertains to sonification strategy and we are in call we must
1925 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
1926 // in the device used for phone strategy and play the tone if the selected device does not
1927 // interfere with the device used for phone strategy
1928 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
1929 // many times as there are active tracks on the output
1930
1931 if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
1932 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
1933 LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
1934 stream, starting, outputDesc->mDevice, stateChange);
1935 if (outputDesc->mRefCount[stream]) {
1936 int muteCount = 1;
1937 if (stateChange) {
1938 muteCount = outputDesc->mRefCount[stream];
1939 }
1940 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
1941 LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
1942 for (int i = 0; i < muteCount; i++) {
1943 setStreamMute(stream, starting, mHardwareOutput);
1944 }
1945 } else {
1946 LOGV("handleIncallSonification() high visibility");
1947 if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) {
1948 LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
1949 for (int i = 0; i < muteCount; i++) {
1950 setStreamMute(stream, starting, mHardwareOutput);
1951 }
1952 }
1953 if (starting) {
1954 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
1955 } else {
1956 mpClientInterface->stopTone();
1957 }
1958 }
1959 }
1960 }
1961}
1962
1963bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
1964 uint32_t samplingRate,
1965 uint32_t format,
1966 uint32_t channels,
1967 AudioSystem::output_flags flags,
1968 uint32_t device)
1969{
1970 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
1971 (format !=0 && !AudioSystem::isLinearPCM(format)));
1972}
1973
Eric Laurentde070132010-07-13 04:45:46 -07001974uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
1975{
1976 return MAX_EFFECTS_CPU_LOAD;
1977}
1978
1979uint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
1980{
1981 return MAX_EFFECTS_MEMORY;
1982}
1983
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984// --- AudioOutputDescriptor class implementation
1985
1986AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor()
1987 : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
1988 mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0)
1989{
1990 // clear usage count for all stream types
1991 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1992 mRefCount[i] = 0;
1993 mCurVolume[i] = -1.0;
1994 mMuteCount[i] = 0;
1995 }
1996}
1997
1998uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
1999{
2000 uint32_t device = 0;
2001 if (isDuplicated()) {
2002 device = mOutput1->mDevice | mOutput2->mDevice;
2003 } else {
2004 device = mDevice;
2005 }
2006 return device;
2007}
2008
2009void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2010{
2011 // forward usage count change to attached outputs
2012 if (isDuplicated()) {
2013 mOutput1->changeRefCount(stream, delta);
2014 mOutput2->changeRefCount(stream, delta);
2015 }
2016 if ((delta + (int)mRefCount[stream]) < 0) {
2017 LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2018 mRefCount[stream] = 0;
2019 return;
2020 }
2021 mRefCount[stream] += delta;
2022 LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2023}
2024
2025uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2026{
2027 uint32_t refcount = 0;
2028 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2029 refcount += mRefCount[i];
2030 }
2031 return refcount;
2032}
2033
2034uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2035{
2036 uint32_t refCount = 0;
2037 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2038 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2039 refCount += mRefCount[i];
2040 }
2041 }
2042 return refCount;
2043}
2044
2045
2046status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2047{
2048 const size_t SIZE = 256;
2049 char buffer[SIZE];
2050 String8 result;
2051
2052 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2053 result.append(buffer);
2054 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2055 result.append(buffer);
2056 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2057 result.append(buffer);
2058 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2059 result.append(buffer);
2060 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2061 result.append(buffer);
2062 snprintf(buffer, SIZE, " Devices %08x\n", device());
2063 result.append(buffer);
2064 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2065 result.append(buffer);
2066 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2067 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2068 result.append(buffer);
2069 }
2070 write(fd, result.string(), result.size());
2071
2072 return NO_ERROR;
2073}
2074
2075// --- AudioInputDescriptor class implementation
2076
2077AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor()
2078 : mSamplingRate(0), mFormat(0), mChannels(0),
2079 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
2080{
2081}
2082
2083status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2084{
2085 const size_t SIZE = 256;
2086 char buffer[SIZE];
2087 String8 result;
2088
2089 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2090 result.append(buffer);
2091 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2092 result.append(buffer);
2093 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2094 result.append(buffer);
2095 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2096 result.append(buffer);
2097 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2098 result.append(buffer);
2099 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2100 result.append(buffer);
2101 write(fd, result.string(), result.size());
2102
2103 return NO_ERROR;
2104}
2105
2106// --- StreamDescriptor class implementation
2107
2108void AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size)
2109{
2110 snprintf(buffer, size, " %02d %02d %02d %d\n",
2111 mIndexMin,
2112 mIndexMax,
2113 mIndexCur,
2114 mCanBeMuted);
2115}
2116
Eric Laurentde070132010-07-13 04:45:46 -07002117// --- EffectDescriptor class implementation
2118
2119status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2120{
2121 const size_t SIZE = 256;
2122 char buffer[SIZE];
2123 String8 result;
2124
2125 snprintf(buffer, SIZE, " Output: %d\n", mOutput);
2126 result.append(buffer);
2127 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2128 result.append(buffer);
2129 snprintf(buffer, SIZE, " Session: %d\n", mSession);
2130 result.append(buffer);
2131 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
2132 result.append(buffer);
2133 write(fd, result.string(), result.size());
2134
2135 return NO_ERROR;
2136}
2137
2138
Mathias Agopian65ab4712010-07-14 17:59:35 -07002139
2140}; // namespace android