2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "AudioPolicyManagerBase"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 #include <hardware_legacy/AudioPolicyManagerBase.h>
21 #include <hardware/audio_effect.h>
22 #include <hardware/audio.h>
24 #include <hardware_legacy/audio_policy_conf.h>
26 namespace android_audio_legacy {
28 // ----------------------------------------------------------------------------
29 // AudioPolicyInterface implementation
30 // ----------------------------------------------------------------------------
33 status_t AudioPolicyManagerBase::setDeviceConnectionState(AudioSystem::audio_devices device,
34 AudioSystem::device_connection_state state,
35 const char *device_address)
37 audio_io_handle_t output = 0;
39 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
41 // connect/disconnect only 1 device at a time
42 if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
44 if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
45 ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
49 // handle output devices
50 if (AudioSystem::isOutputDevice(device)) {
52 if (!mHasA2dp && AudioSystem::isA2dpDevice(device)) {
53 ALOGE("setDeviceConnectionState() invalid device: %x", device);
59 // handle output device connection
60 case AudioSystem::DEVICE_STATE_AVAILABLE:
61 if (mAvailableOutputDevices & device) {
62 ALOGW("setDeviceConnectionState() device already connected: %x", device);
63 return INVALID_OPERATION;
65 ALOGV("setDeviceConnectionState() connecting device %x", device);
67 // register new device as available
68 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
70 output = checkOutputForDevice((audio_devices_t)device, state);
72 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
73 return INVALID_OPERATION;
75 // handle A2DP device connection
76 if (mHasA2dp && AudioSystem::isA2dpDevice(device)) {
78 param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
79 mpClientInterface->setParameters(output, param.toString());
80 mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
81 mA2dpSuspended = false;
82 } else if (AudioSystem::isBluetoothScoDevice(device)) {
83 ALOGV("setDeviceConnectionState() BT SCO device, address %s", device_address);
84 // keep track of SCO device address
85 mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
88 // handle output device disconnection
89 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
90 if (!(mAvailableOutputDevices & device)) {
91 ALOGW("setDeviceConnectionState() device not connected: %x", device);
92 return INVALID_OPERATION;
96 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
97 // remove device from available output devices
98 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
100 output = checkOutputForDevice((audio_devices_t)device, state);
101 // handle A2DP device disconnection
102 if (mHasA2dp && AudioSystem::isA2dpDevice(device)) {
103 mA2dpDeviceAddress = "";
104 mA2dpSuspended = false;
105 } else if (AudioSystem::isBluetoothScoDevice(device)) {
106 mScoDeviceAddress = "";
111 ALOGE("setDeviceConnectionState() invalid state: %x", state);
116 checkOutputForAllStrategies();
117 // outputs must be closed after checkOutputForAllStrategies() is executed
118 if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && output != 0) {
122 updateDeviceForStrategy();
123 for (size_t i = 0; i < mOutputs.size(); i++) {
124 setOutputDevice(mOutputs.keyAt(i), getNewDevice(mOutputs.keyAt(i), true /*fromCache*/));
127 if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
128 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
129 } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
130 device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
131 device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
132 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
137 // handle input devices
138 if (AudioSystem::isInputDevice(device)) {
142 // handle input device connection
143 case AudioSystem::DEVICE_STATE_AVAILABLE: {
144 if (mAvailableInputDevices & device) {
145 ALOGW("setDeviceConnectionState() device already connected: %d", device);
146 return INVALID_OPERATION;
148 mAvailableInputDevices = (audio_devices_t)(mAvailableInputDevices | device);
152 // handle input device disconnection
153 case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
154 if (!(mAvailableInputDevices & device)) {
155 ALOGW("setDeviceConnectionState() device not connected: %d", device);
156 return INVALID_OPERATION;
158 mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
162 ALOGE("setDeviceConnectionState() invalid state: %x", state);
166 audio_io_handle_t activeInput = getActiveInput();
167 if (activeInput != 0) {
168 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
169 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
170 if ((newDevice != 0) && (newDevice != inputDesc->mDevice)) {
171 ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
172 inputDesc->mDevice, newDevice, activeInput);
173 inputDesc->mDevice = newDevice;
174 AudioParameter param = AudioParameter();
175 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
176 mpClientInterface->setParameters(activeInput, param.toString());
183 ALOGW("setDeviceConnectionState() invalid device: %x", device);
187 AudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(AudioSystem::audio_devices device,
188 const char *device_address)
190 AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
191 String8 address = String8(device_address);
192 if (AudioSystem::isOutputDevice(device)) {
193 if (device & mAvailableOutputDevices) {
194 if (AudioSystem::isA2dpDevice(device) &&
195 (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
198 if (AudioSystem::isBluetoothScoDevice(device) &&
199 address != "" && mScoDeviceAddress != address) {
202 state = AudioSystem::DEVICE_STATE_AVAILABLE;
204 } else if (AudioSystem::isInputDevice(device)) {
205 if (device & mAvailableInputDevices) {
206 state = AudioSystem::DEVICE_STATE_AVAILABLE;
213 void AudioPolicyManagerBase::setPhoneState(int state)
215 ALOGV("setPhoneState() state %d", state);
216 audio_devices_t newDevice = (audio_devices_t)0;
217 if (state < 0 || state >= AudioSystem::NUM_MODES) {
218 ALOGW("setPhoneState() invalid state %d", state);
222 if (state == mPhoneState ) {
223 ALOGW("setPhoneState() setting same state %d", state);
227 // if leaving call state, handle special case of active streams
228 // pertaining to sonification strategy see handleIncallSonification()
230 ALOGV("setPhoneState() in call state management: new state is %d", state);
231 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
232 handleIncallSonification(stream, false, true);
236 // store previous phone state for management of sonification strategy below
237 int oldState = mPhoneState;
241 // are we entering or starting a call
242 if (!isStateInCall(oldState) && isStateInCall(state)) {
243 ALOGV(" Entering call in setPhoneState()");
244 // force routing command to audio hardware when starting a call
245 // even if no device change is needed
247 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
248 ALOGV(" Exiting call in setPhoneState()");
249 // force routing command to audio hardware when exiting a call
250 // even if no device change is needed
252 } else if (isStateInCall(state) && (state != oldState)) {
253 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
254 // force routing command to audio hardware when switching between telephony and VoIP
255 // even if no device change is needed
259 // check for device and output changes triggered by new phone state
260 newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
262 checkOutputForAllStrategies();
263 updateDeviceForStrategy();
265 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
267 // force routing command to audio hardware when ending call
268 // even if no device change is needed
269 if (isStateInCall(oldState) && newDevice == 0) {
270 newDevice = hwOutputDesc->device();
273 // when changing from ring tone to in call mode, mute the ringing tone
274 // immediately and delay the route change to avoid sending the ring tone
275 // tail into the earpiece or headset.
277 if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
278 // delay the device change command by twice the output latency to have some margin
279 // and be sure that audio buffers not yet affected by the mute are out when
280 // we actually apply the route change
281 delayMs = hwOutputDesc->mLatency*2;
282 setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
285 // change routing is necessary
286 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
288 // if entering in call state, handle special case of active streams
289 // pertaining to sonification strategy see handleIncallSonification()
290 if (isStateInCall(state)) {
291 ALOGV("setPhoneState() in call state management: new state is %d", state);
292 // unmute the ringing tone after a sufficient delay if it was muted before
293 // setting output device above
294 if (oldState == AudioSystem::MODE_RINGTONE) {
295 setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
297 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
298 handleIncallSonification(stream, true, true);
302 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
303 if (state == AudioSystem::MODE_RINGTONE &&
304 isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
305 mLimitRingtoneVolume = true;
307 mLimitRingtoneVolume = false;
311 void AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
313 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
315 bool forceVolumeReeval = false;
317 case AudioSystem::FOR_COMMUNICATION:
318 if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
319 config != AudioSystem::FORCE_NONE) {
320 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
323 forceVolumeReeval = true;
324 mForceUse[usage] = config;
326 case AudioSystem::FOR_MEDIA:
327 if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
328 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
329 config != AudioSystem::FORCE_ANALOG_DOCK &&
330 config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE) {
331 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
334 mForceUse[usage] = config;
336 case AudioSystem::FOR_RECORD:
337 if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
338 config != AudioSystem::FORCE_NONE) {
339 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
342 mForceUse[usage] = config;
344 case AudioSystem::FOR_DOCK:
345 if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
346 config != AudioSystem::FORCE_BT_DESK_DOCK &&
347 config != AudioSystem::FORCE_WIRED_ACCESSORY &&
348 config != AudioSystem::FORCE_ANALOG_DOCK &&
349 config != AudioSystem::FORCE_DIGITAL_DOCK) {
350 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
352 forceVolumeReeval = true;
353 mForceUse[usage] = config;
356 ALOGW("setForceUse() invalid usage %d", usage);
360 // check for device and output changes triggered by new force usage
362 checkOutputForAllStrategies();
363 updateDeviceForStrategy();
364 for (size_t i = 0; i < mOutputs.size(); i++) {
365 audio_io_handle_t output = mOutputs.keyAt(i);
366 audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
367 setOutputDevice(output, newDevice, true);
368 if (forceVolumeReeval) {
369 applyStreamVolumes(output, newDevice, 0, true);
373 audio_io_handle_t activeInput = getActiveInput();
374 if (activeInput != 0) {
375 AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
376 audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
377 if ((newDevice != 0) && (newDevice != inputDesc->mDevice)) {
378 ALOGV("setForceUse() changing device from %x to %x for input %d",
379 inputDesc->mDevice, newDevice, activeInput);
380 inputDesc->mDevice = newDevice;
381 AudioParameter param = AudioParameter();
382 param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
383 mpClientInterface->setParameters(activeInput, param.toString());
389 AudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
391 return mForceUse[usage];
394 void AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
396 ALOGV("setSystemProperty() property %s, value %s", property, value);
397 if (strcmp(property, "ro.camera.sound.forced") == 0) {
399 ALOGV("ENFORCED_AUDIBLE cannot be muted");
400 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
402 ALOGV("ENFORCED_AUDIBLE can be muted");
403 mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
408 audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
409 uint32_t samplingRate,
412 AudioSystem::output_flags flags)
414 audio_io_handle_t output = 0;
415 uint32_t latency = 0;
416 routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
417 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
418 ALOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags);
420 #ifdef AUDIO_POLICY_TEST
421 if (mCurOutput != 0) {
422 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d",
423 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
425 if (mTestOutputs[mCurOutput] == 0) {
426 ALOGV("getOutput() opening test output");
427 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
428 outputDesc->mDevice = mTestDevice;
429 outputDesc->mSamplingRate = mTestSamplingRate;
430 outputDesc->mFormat = mTestFormat;
431 outputDesc->mChannels = mTestChannels;
432 outputDesc->mLatency = mTestLatencyMs;
433 outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
434 outputDesc->mRefCount[stream] = 0;
435 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice,
436 &outputDesc->mSamplingRate,
437 &outputDesc->mFormat,
438 &outputDesc->mChannels,
439 &outputDesc->mLatency,
441 if (mTestOutputs[mCurOutput]) {
442 AudioParameter outputCmd = AudioParameter();
443 outputCmd.addInt(String8("set_id"),mCurOutput);
444 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
445 addOutput(mTestOutputs[mCurOutput], outputDesc);
448 return mTestOutputs[mCurOutput];
450 #endif //AUDIO_POLICY_TEST
452 // open a direct output if required by specified parameters
453 if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
455 ALOGV("getOutput() opening direct output device %x", device);
456 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
457 outputDesc->mDevice = device;
458 outputDesc->mSamplingRate = samplingRate;
459 outputDesc->mFormat = format;
460 outputDesc->mChannels = channels;
461 outputDesc->mLatency = 0;
462 outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
463 outputDesc->mRefCount[stream] = 0;
464 outputDesc->mStopTime[stream] = 0;
465 output = mpClientInterface->openOutput((uint32_t *)&outputDesc->mDevice,
466 &outputDesc->mSamplingRate,
467 &outputDesc->mFormat,
468 &outputDesc->mChannels,
469 &outputDesc->mLatency,
472 // only accept an output with the requeted parameters
474 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
475 (format != 0 && format != outputDesc->mFormat) ||
476 (channels != 0 && channels != outputDesc->mChannels)) {
477 ALOGV("getOutput() failed opening direct output: samplingRate %d, format %d, channels %d",
478 samplingRate, format, channels);
480 mpClientInterface->closeOutput(output);
485 addOutput(output, outputDesc);
489 if (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO &&
490 channels != AudioSystem::CHANNEL_OUT_STEREO) {
493 // open a non direct output
495 // get which output is suitable for the specified stream. The actual routing change will happen
496 // when startOutput() will be called
497 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device);
499 output = selectOutput(outputs, flags);
501 ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d, format %d, channels %x, flags %x",
502 stream, samplingRate, format, channels, flags);
504 ALOGV("getOutput() returns output %d", output);
509 audio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
510 AudioSystem::output_flags flags)
512 // select one output among several that provide a path to a particular device or set of
513 // devices (the list was previously build by getOutputsForDevice()).
514 // The priority is as follows:
515 // 1: the output with the highest number of requested policy flags
516 // 2: the primary output
517 // 3: the first output in the list
519 if (outputs.size() == 0) {
522 if (outputs.size() == 1) {
526 int maxCommonFlags = 0;
527 audio_io_handle_t outputFlags = 0;
528 audio_io_handle_t outputPrimary = 0;
530 for (size_t i = 0; i < outputs.size(); i++) {
531 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
532 if (!outputDesc->isDuplicated()) {
533 int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
534 if (commonFlags > maxCommonFlags) {
535 outputFlags = outputs[i];
536 maxCommonFlags = commonFlags;
537 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
539 if (outputDesc->mProfile->mFlags & AUDIO_POLICY_OUTPUT_FLAG_PRIMARY) {
540 outputPrimary = outputs[i];
545 if (outputFlags != 0) {
548 if (outputPrimary != 0) {
549 return outputPrimary;
555 status_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
556 AudioSystem::stream_type stream,
559 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
560 ssize_t index = mOutputs.indexOfKey(output);
562 ALOGW("startOutput() unknow output %d", output);
566 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
568 // increment usage count for this stream on the requested output:
569 // NOTE that the usage count is the same for duplicated output and hardware output which is
570 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
571 outputDesc->changeRefCount(stream, 1);
573 if (outputDesc->mRefCount[stream] == 1) {
574 audio_devices_t prevDevice = outputDesc->device();
575 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
577 // force a device change if any other output is active, is managed by the same and hw
578 // module and has a current device selection that differs from newly selected device.
579 // In this case, the audio HAL must receive the new device selection so that it can
580 // change the device currently selected by the other active output.
582 for (size_t i = 0; i < mOutputs.size(); i++) {
583 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
584 if (output != mOutputs.keyAt(i) &&
585 desc->refCount() != 0 &&
586 outputDesc->sharesHwModuleWith(desc) &&
587 desc->device() != newDevice) {
592 setOutputDevice(output, newDevice, force);
594 // handle special case for sonification while in call
596 handleIncallSonification(stream, true, false);
599 // apply volume rules for current stream and device if necessary
600 checkAndSetVolume(stream,
601 mStreams[stream].getVolumeIndex((audio_devices_t)newDevice),
605 // update the outputs if starting an output with a stream that can affect notification
607 handleNotificationRoutingForStream(stream);
613 status_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
614 AudioSystem::stream_type stream,
617 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
618 ssize_t index = mOutputs.indexOfKey(output);
620 ALOGW("stopOutput() unknow output %d", output);
624 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
626 // handle special case for sonification while in call
628 handleIncallSonification(stream, false, false);
631 if (outputDesc->mRefCount[stream] > 0) {
632 // decrement usage count of this stream on the output
633 outputDesc->changeRefCount(stream, -1);
634 // store time at which the stream was stopped - see isStreamActive()
635 if (outputDesc->mRefCount[stream] == 0) {
636 outputDesc->mStopTime[stream] = systemTime();
637 audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
638 // delay the device switch by twice the latency because stopOutput() is executed when
639 // the track stop() command is received and at that time the audio track buffer can
640 // still contain data that needs to be drained. The latency only covers the audio HAL
641 // and kernel buffers. Also the latency does not always include additional delay in the
642 // audio path (audio DSP, CODEC ...)
643 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
645 // force restoring the device selection on other active outputs if it differs from the
646 // one being selected for this output
647 for (size_t i = 0; i < mOutputs.size(); i++) {
648 audio_io_handle_t curOutput = mOutputs.keyAt(i);
649 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
650 if (curOutput != output &&
651 desc->refCount() != 0 &&
652 outputDesc->sharesHwModuleWith(desc) &&
653 newDevice != desc->device()) {
654 setOutputDevice(curOutput,
655 getNewDevice(curOutput, false /*fromCache*/),
657 outputDesc->mLatency*2);
660 // update the outputs if stopping one with a stream that can affect notification routing
661 handleNotificationRoutingForStream(stream);
665 ALOGW("stopOutput() refcount is already 0 for output %d", output);
666 return INVALID_OPERATION;
670 void AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
672 ALOGV("releaseOutput() %d", output);
673 ssize_t index = mOutputs.indexOfKey(output);
675 ALOGW("releaseOutput() releasing unknown output %d", output);
679 #ifdef AUDIO_POLICY_TEST
680 int testIndex = testOutputIndex(output);
681 if (testIndex != 0) {
682 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
683 if (outputDesc->refCount() == 0) {
684 mpClientInterface->closeOutput(output);
685 delete mOutputs.valueAt(index);
686 mOutputs.removeItem(output);
687 mTestOutputs[testIndex] = 0;
691 #endif //AUDIO_POLICY_TEST
693 if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
694 mpClientInterface->closeOutput(output);
695 delete mOutputs.valueAt(index);
696 mOutputs.removeItem(output);
701 audio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
702 uint32_t samplingRate,
705 AudioSystem::audio_in_acoustics acoustics)
707 audio_io_handle_t input = 0;
708 audio_devices_t device = getDeviceForInputSource(inputSource);
710 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x",
711 inputSource, samplingRate, format, channels, acoustics);
714 ALOGW("getInput() could not find device for inputSource %d", inputSource);
718 // adapt channel selection to input source
719 switch(inputSource) {
720 case AUDIO_SOURCE_VOICE_UPLINK:
721 channels = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
723 case AUDIO_SOURCE_VOICE_DOWNLINK:
724 channels = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
726 case AUDIO_SOURCE_VOICE_CALL:
727 channels = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
733 IOProfile *profile = getInputProfile(device,
737 if (profile == NULL) {
738 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
740 device, samplingRate, format, channels);
744 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
746 inputDesc->mInputSource = inputSource;
747 inputDesc->mDevice = device;
748 inputDesc->mSamplingRate = samplingRate;
749 inputDesc->mFormat = format;
750 inputDesc->mChannels = channels;
751 inputDesc->mAcoustics = acoustics;
752 inputDesc->mRefCount = 0;
753 input = mpClientInterface->openInput((uint32_t *)&inputDesc->mDevice,
754 &inputDesc->mSamplingRate,
756 &inputDesc->mChannels,
757 (audio_in_acoustics_t) inputDesc->mAcoustics);
759 // only accept input with the exact requested set of parameters
761 (samplingRate != inputDesc->mSamplingRate) ||
762 (format != inputDesc->mFormat) ||
763 (channels != inputDesc->mChannels)) {
764 ALOGV("getInput() failed opening input: samplingRate %d, format %d, channels %d",
765 samplingRate, format, channels);
767 mpClientInterface->closeInput(input);
772 mInputs.add(input, inputDesc);
776 status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
778 ALOGV("startInput() input %d", input);
779 ssize_t index = mInputs.indexOfKey(input);
781 ALOGW("startInput() unknow input %d", input);
784 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
786 #ifdef AUDIO_POLICY_TEST
788 #endif //AUDIO_POLICY_TEST
790 // refuse 2 active AudioRecord clients at the same time
791 if (getActiveInput() != 0) {
792 ALOGW("startInput() input %d failed: other input already started", input);
793 return INVALID_OPERATION;
797 AudioParameter param = AudioParameter();
798 param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
800 param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
801 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
803 mpClientInterface->setParameters(input, param.toString());
805 inputDesc->mRefCount = 1;
809 status_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
811 ALOGV("stopInput() input %d", input);
812 ssize_t index = mInputs.indexOfKey(input);
814 ALOGW("stopInput() unknow input %d", input);
817 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
819 if (inputDesc->mRefCount == 0) {
820 ALOGW("stopInput() input %d already stopped", input);
821 return INVALID_OPERATION;
823 AudioParameter param = AudioParameter();
824 param.addInt(String8(AudioParameter::keyRouting), 0);
825 mpClientInterface->setParameters(input, param.toString());
826 inputDesc->mRefCount = 0;
831 void AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
833 ALOGV("releaseInput() %d", input);
834 ssize_t index = mInputs.indexOfKey(input);
836 ALOGW("releaseInput() releasing unknown input %d", input);
839 mpClientInterface->closeInput(input);
840 delete mInputs.valueAt(index);
841 mInputs.removeItem(input);
842 ALOGV("releaseInput() exit");
845 void AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
849 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
850 if (indexMin < 0 || indexMin >= indexMax) {
851 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
854 mStreams[stream].mIndexMin = indexMin;
855 mStreams[stream].mIndexMax = indexMax;
858 status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
860 audio_devices_t device)
863 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
866 if (!audio_is_output_device(device)) {
870 // Force max volume if stream cannot be muted
871 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
873 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
874 stream, device, index);
876 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
877 // clear all device specific values
878 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
879 mStreams[stream].mIndexCur.clear();
881 mStreams[stream].mIndexCur.add(device, index);
883 // compute and apply stream volume on all outputs according to connected device
884 status_t status = NO_ERROR;
885 for (size_t i = 0; i < mOutputs.size(); i++) {
886 audio_devices_t curDevice =
887 getDeviceForVolume((audio_devices_t)mOutputs.valueAt(i)->device());
888 if (device == curDevice) {
889 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
890 if (volStatus != NO_ERROR) {
898 status_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
900 audio_devices_t device)
905 if (!audio_is_output_device(device)) {
908 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
909 // the strategy the stream belongs to.
910 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
911 device = (audio_devices_t)getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
913 device = getDeviceForVolume(device);
915 *index = mStreams[stream].getVolumeIndex(device);
916 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
920 audio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc)
922 ALOGV("getOutputForEffect()");
923 // apply simple rule where global effects are attached to the same output as MUSIC streams
924 return getOutput(AudioSystem::MUSIC);
927 status_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc,
928 audio_io_handle_t io,
933 ssize_t index = mOutputs.indexOfKey(io);
935 index = mInputs.indexOfKey(io);
937 ALOGW("registerEffect() unknown io %d", io);
938 return INVALID_OPERATION;
942 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
943 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
944 desc->name, desc->memoryUsage);
945 return INVALID_OPERATION;
947 mTotalEffectsMemory += desc->memoryUsage;
948 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
949 desc->name, io, strategy, session, id);
950 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
952 EffectDescriptor *pDesc = new EffectDescriptor();
953 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
955 pDesc->mStrategy = (routing_strategy)strategy;
956 pDesc->mSession = session;
957 pDesc->mEnabled = false;
959 mEffects.add(id, pDesc);
964 status_t AudioPolicyManagerBase::unregisterEffect(int id)
966 ssize_t index = mEffects.indexOfKey(id);
968 ALOGW("unregisterEffect() unknown effect ID %d", id);
969 return INVALID_OPERATION;
972 EffectDescriptor *pDesc = mEffects.valueAt(index);
974 setEffectEnabled(pDesc, false);
976 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
977 ALOGW("unregisterEffect() memory %d too big for total %d",
978 pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
979 pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
981 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
982 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
983 pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
985 mEffects.removeItem(id);
991 status_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
993 ssize_t index = mEffects.indexOfKey(id);
995 ALOGW("unregisterEffect() unknown effect ID %d", id);
996 return INVALID_OPERATION;
999 return setEffectEnabled(mEffects.valueAt(index), enabled);
1002 status_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1004 if (enabled == pDesc->mEnabled) {
1005 ALOGV("setEffectEnabled(%s) effect already %s",
1006 enabled?"true":"false", enabled?"enabled":"disabled");
1007 return INVALID_OPERATION;
1011 if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
1012 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1013 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1014 return INVALID_OPERATION;
1016 mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
1017 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1019 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
1020 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1021 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1022 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1024 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
1025 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1027 pDesc->mEnabled = enabled;
1031 bool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1033 nsecs_t sysTime = systemTime();
1034 for (size_t i = 0; i < mOutputs.size(); i++) {
1035 if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1036 ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1044 status_t AudioPolicyManagerBase::dump(int fd)
1046 const size_t SIZE = 256;
1050 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1051 result.append(buffer);
1053 snprintf(buffer, SIZE, " Hardware Output: %d\n", mPrimaryOutput);
1054 result.append(buffer);
1055 snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1056 result.append(buffer);
1057 snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1058 result.append(buffer);
1059 snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1060 result.append(buffer);
1061 snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1062 result.append(buffer);
1063 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1064 result.append(buffer);
1065 snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1066 result.append(buffer);
1067 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1068 result.append(buffer);
1069 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1070 result.append(buffer);
1071 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1072 result.append(buffer);
1073 write(fd, result.string(), result.size());
1075 snprintf(buffer, SIZE, "\nOutput Profiles dump:\n");
1076 write(fd, buffer, strlen(buffer));
1077 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
1078 snprintf(buffer, SIZE, "- Output Profile %d:\n", i + 1);
1079 write(fd, buffer, strlen(buffer));
1080 mOutputProfiles[i]->dump(fd);
1083 snprintf(buffer, SIZE, "\nInput Profiles dump:\n");
1084 write(fd, buffer, strlen(buffer));
1085 for (size_t i = 0; i < mInputProfiles.size(); i++) {
1086 snprintf(buffer, SIZE, "- Input Profile %d:\n", i + 1);
1087 write(fd, buffer, strlen(buffer));
1088 mInputProfiles[i]->dump(fd);
1091 snprintf(buffer, SIZE, "\nOutputs dump:\n");
1092 write(fd, buffer, strlen(buffer));
1093 for (size_t i = 0; i < mOutputs.size(); i++) {
1094 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1095 write(fd, buffer, strlen(buffer));
1096 mOutputs.valueAt(i)->dump(fd);
1099 snprintf(buffer, SIZE, "\nInputs dump:\n");
1100 write(fd, buffer, strlen(buffer));
1101 for (size_t i = 0; i < mInputs.size(); i++) {
1102 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1103 write(fd, buffer, strlen(buffer));
1104 mInputs.valueAt(i)->dump(fd);
1107 snprintf(buffer, SIZE, "\nStreams dump:\n");
1108 write(fd, buffer, strlen(buffer));
1109 snprintf(buffer, SIZE,
1110 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1111 write(fd, buffer, strlen(buffer));
1112 for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1113 snprintf(buffer, SIZE, " %02d ", i);
1114 write(fd, buffer, strlen(buffer));
1115 mStreams[i].dump(fd);
1118 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1119 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1120 write(fd, buffer, strlen(buffer));
1122 snprintf(buffer, SIZE, "Registered effects:\n");
1123 write(fd, buffer, strlen(buffer));
1124 for (size_t i = 0; i < mEffects.size(); i++) {
1125 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1126 write(fd, buffer, strlen(buffer));
1127 mEffects.valueAt(i)->dump(fd);
1134 // ----------------------------------------------------------------------------
1135 // AudioPolicyManagerBase
1136 // ----------------------------------------------------------------------------
1138 AudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1140 #ifdef AUDIO_POLICY_TEST
1142 #endif //AUDIO_POLICY_TEST
1143 mAvailableOutputDevices((audio_devices_t)0),
1144 mPhoneState(AudioSystem::MODE_NORMAL),
1145 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1146 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
1147 mA2dpSuspended(false), mHasA2dp(false)
1149 mpClientInterface = clientInterface;
1151 for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1152 mForceUse[i] = AudioSystem::FORCE_NONE;
1155 initializeVolumeCurves();
1157 mA2dpDeviceAddress = String8("");
1158 mScoDeviceAddress = String8("");
1160 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1161 ALOGE("could not load audio policy configuration file");
1164 // open all output streams needed to access attached devices
1165 for (size_t i = 0; i < mOutputProfiles.size(); i++)
1167 const IOProfile *outProfile = mOutputProfiles[i];
1169 if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
1170 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
1172 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
1173 outProfile->mSupportedDevices);
1174 outputDesc->mSamplingRate = outProfile->mSamplingRates[0];
1175 outputDesc->mFormat = outProfile->mFormats[0];
1176 outputDesc->mChannels = outProfile->mChannelMasks[0];
1177 outputDesc->mFlags = (AudioSystem::output_flags)outProfile->mFlags;
1178 audio_io_handle_t output = mpClientInterface->openOutput(
1179 (uint32_t *)&outputDesc->mDevice,
1180 &outputDesc->mSamplingRate,
1181 &outputDesc->mFormat,
1182 &outputDesc->mChannels,
1183 &outputDesc->mLatency,
1184 outputDesc->mFlags);
1188 mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
1189 (outProfile->mSupportedDevices & mAttachedOutputDevices));
1190 if (outProfile->mFlags & AUDIO_POLICY_OUTPUT_FLAG_PRIMARY) {
1191 mPrimaryOutput = output;
1193 addOutput(output, outputDesc);
1194 setOutputDevice(output,
1195 (audio_devices_t)(mDefaultOutputDevice &
1196 outProfile->mSupportedDevices),
1202 ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1203 "Not output found for attached devices %08x",
1204 (mAttachedOutputDevices & ~mAvailableOutputDevices));
1206 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1208 updateDeviceForStrategy();
1209 #ifdef AUDIO_POLICY_TEST
1210 if (mPrimaryOutput != 0) {
1211 AudioParameter outputCmd = AudioParameter();
1212 outputCmd.addInt(String8("set_id"), 0);
1213 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1215 mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1216 mTestSamplingRate = 44100;
1217 mTestFormat = AudioSystem::PCM_16_BIT;
1218 mTestChannels = AudioSystem::CHANNEL_OUT_STEREO;
1221 mDirectOutput = false;
1222 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1223 mTestOutputs[i] = 0;
1226 const size_t SIZE = 256;
1228 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1229 run(buffer, ANDROID_PRIORITY_AUDIO);
1231 #endif //AUDIO_POLICY_TEST
1234 AudioPolicyManagerBase::~AudioPolicyManagerBase()
1236 #ifdef AUDIO_POLICY_TEST
1238 #endif //AUDIO_POLICY_TEST
1239 for (size_t i = 0; i < mOutputs.size(); i++) {
1240 mpClientInterface->closeOutput(mOutputs.keyAt(i));
1241 delete mOutputs.valueAt(i);
1244 for (size_t i = 0; i < mInputs.size(); i++) {
1245 mpClientInterface->closeInput(mInputs.keyAt(i));
1246 delete mInputs.valueAt(i);
1249 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
1250 delete mOutputProfiles[i];
1252 for (size_t i = 0; i < mInputProfiles.size(); i++) {
1253 delete mInputProfiles[i];
1257 status_t AudioPolicyManagerBase::initCheck()
1259 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1262 #ifdef AUDIO_POLICY_TEST
1263 bool AudioPolicyManagerBase::threadLoop()
1265 ALOGV("entering threadLoop()");
1266 while (!exitPending())
1272 Mutex::Autolock _l(mLock);
1273 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1275 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1276 AudioParameter param = AudioParameter(command);
1278 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1280 ALOGV("Test command %s received", command.string());
1282 if (param.get(String8("target"), target) != NO_ERROR) {
1285 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1286 param.remove(String8("test_cmd_policy_output"));
1287 mCurOutput = valueInt;
1289 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1290 param.remove(String8("test_cmd_policy_direct"));
1291 if (value == "false") {
1292 mDirectOutput = false;
1293 } else if (value == "true") {
1294 mDirectOutput = true;
1297 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1298 param.remove(String8("test_cmd_policy_input"));
1299 mTestInput = valueInt;
1302 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1303 param.remove(String8("test_cmd_policy_format"));
1304 int format = AudioSystem::INVALID_FORMAT;
1305 if (value == "PCM 16 bits") {
1306 format = AudioSystem::PCM_16_BIT;
1307 } else if (value == "PCM 8 bits") {
1308 format = AudioSystem::PCM_8_BIT;
1309 } else if (value == "Compressed MP3") {
1310 format = AudioSystem::MP3;
1312 if (format != AudioSystem::INVALID_FORMAT) {
1313 if (target == "Manager") {
1314 mTestFormat = format;
1315 } else if (mTestOutputs[mCurOutput] != 0) {
1316 AudioParameter outputParam = AudioParameter();
1317 outputParam.addInt(String8("format"), format);
1318 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1322 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1323 param.remove(String8("test_cmd_policy_channels"));
1326 if (value == "Channels Stereo") {
1327 channels = AudioSystem::CHANNEL_OUT_STEREO;
1328 } else if (value == "Channels Mono") {
1329 channels = AudioSystem::CHANNEL_OUT_MONO;
1331 if (channels != 0) {
1332 if (target == "Manager") {
1333 mTestChannels = channels;
1334 } else if (mTestOutputs[mCurOutput] != 0) {
1335 AudioParameter outputParam = AudioParameter();
1336 outputParam.addInt(String8("channels"), channels);
1337 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1341 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1342 param.remove(String8("test_cmd_policy_sampleRate"));
1343 if (valueInt >= 0 && valueInt <= 96000) {
1344 int samplingRate = valueInt;
1345 if (target == "Manager") {
1346 mTestSamplingRate = samplingRate;
1347 } else if (mTestOutputs[mCurOutput] != 0) {
1348 AudioParameter outputParam = AudioParameter();
1349 outputParam.addInt(String8("sampling_rate"), samplingRate);
1350 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1355 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1356 param.remove(String8("test_cmd_policy_reopen"));
1358 mpClientInterface->closeOutput(mPrimaryOutput);
1359 delete mOutputs.valueFor(mPrimaryOutput);
1360 mOutputs.removeItem(mPrimaryOutput);
1362 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1363 outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1364 mPrimaryOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1365 &outputDesc->mSamplingRate,
1366 &outputDesc->mFormat,
1367 &outputDesc->mChannels,
1368 &outputDesc->mLatency,
1369 outputDesc->mFlags);
1370 if (mPrimaryOutput == 0) {
1371 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1372 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1374 AudioParameter outputCmd = AudioParameter();
1375 outputCmd.addInt(String8("set_id"), 0);
1376 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1377 addOutput(mPrimaryOutput, outputDesc);
1382 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1388 void AudioPolicyManagerBase::exit()
1391 AutoMutex _l(mLock);
1393 mWaitWorkCV.signal();
1395 requestExitAndWait();
1398 int AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1400 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1401 if (output == mTestOutputs[i]) return i;
1405 #endif //AUDIO_POLICY_TEST
1409 void AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1411 outputDesc->mId = id;
1412 mOutputs.add(id, outputDesc);
1416 audio_io_handle_t AudioPolicyManagerBase::checkOutputForDevice(
1417 audio_devices_t device,
1418 AudioSystem::device_connection_state state)
1420 audio_io_handle_t output = 0;
1421 AudioOutputDescriptor *outputDesc;
1423 // TODO handle multiple outputs supporting overlapping sets of devices.
1425 if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
1426 // first check if one output already open can be routed to this device
1427 for (size_t i = 0; i < mOutputs.size(); i++) {
1428 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1429 if (outputDesc->mProfile && outputDesc->mProfile->mSupportedDevices & device) {
1430 return mOutputs.keyAt(i);
1433 // then look for one available output that can be routed to this device
1434 const IOProfile *outProfile = NULL;
1435 for (size_t i = 0; i < mOutputProfiles.size(); i++)
1437 if (mOutputProfiles[i]->mSupportedDevices & device) {
1438 outProfile = mOutputProfiles[i];
1442 if (outProfile == NULL) {
1443 ALOGW("No output available for device %04x", device);
1447 ALOGV("opening output for device %08x", device);
1448 outputDesc = new AudioOutputDescriptor(outProfile);
1449 outputDesc->mDevice = device;
1450 output = mpClientInterface->openOutput((uint32_t *)&outputDesc->mDevice,
1451 &outputDesc->mSamplingRate,
1452 &outputDesc->mFormat,
1453 &outputDesc->mChannels,
1454 &outputDesc->mLatency,
1455 outputDesc->mFlags);
1458 audio_io_handle_t duplicatedOutput = 0;
1459 // add output descriptor
1460 addOutput(output, outputDesc);
1461 // set initial stream volume for device
1462 applyStreamVolumes(output, device);
1464 //TODO: configure audio effect output stage here
1466 // open a duplicating output thread for the new output and the primary output
1467 duplicatedOutput = mpClientInterface->openDuplicateOutput(output, mPrimaryOutput);
1468 if (duplicatedOutput != 0) {
1469 // add duplicated output descriptor
1470 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
1471 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
1472 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
1473 dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate;
1474 dupOutputDesc->mFormat = outputDesc->mFormat;
1475 dupOutputDesc->mChannels = outputDesc->mChannels;
1476 dupOutputDesc->mLatency = outputDesc->mLatency;
1477 addOutput(duplicatedOutput, dupOutputDesc);
1478 applyStreamVolumes(duplicatedOutput, device);
1480 ALOGW("getOutput() could not open duplicated output for %d and %d",
1481 mPrimaryOutput, output);
1482 mpClientInterface->closeOutput(output);
1483 mOutputs.removeItem(output);
1488 ALOGW("checkOutputForDevice() could not open output for device %x", device);
1493 // we assume that one given device is supported by zero or one output
1494 // check if one opened output is not needed any more after disconnecting one device
1495 for (size_t i = 0; i < mOutputs.size(); i++) {
1496 outputDesc = mOutputs.valueAt(i);
1497 if (outputDesc->mProfile &&
1498 !(outputDesc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
1499 output = mOutputs.keyAt(i);
1508 void AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1510 ALOGV("closeOutput(%d)", output);
1512 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1513 if (outputDesc == NULL) {
1514 ALOGW("closeOutput() unknown output %d", output);
1518 // look for duplicated outputs connected to the output being removed.
1519 for (size_t i = 0; i < mOutputs.size(); i++) {
1520 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1521 if (dupOutputDesc->isDuplicated() &&
1522 (dupOutputDesc->mOutput1 == outputDesc ||
1523 dupOutputDesc->mOutput2 == outputDesc)) {
1524 AudioOutputDescriptor *outputDesc2;
1525 if (dupOutputDesc->mOutput1 == outputDesc) {
1526 outputDesc2 = dupOutputDesc->mOutput2;
1528 outputDesc2 = dupOutputDesc->mOutput1;
1530 // As all active tracks on duplicated output will be deleted,
1531 // and as they were also referenced on the other output, the reference
1532 // count for their stream type must be adjusted accordingly on
1533 // the other output.
1534 for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1535 int refCount = dupOutputDesc->mRefCount[j];
1536 outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1538 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1539 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1541 mpClientInterface->closeOutput(duplicatedOutput);
1542 delete mOutputs.valueFor(duplicatedOutput);
1543 mOutputs.removeItem(duplicatedOutput);
1547 AudioParameter param;
1548 param.add(String8("closing"), String8("true"));
1549 mpClientInterface->setParameters(output, param.toString());
1551 mpClientInterface->closeOutput(output);
1552 delete mOutputs.valueFor(output);
1553 mOutputs.removeItem(output);
1556 SortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device)
1558 SortedVector<audio_io_handle_t> outputs;
1560 ALOGV("getOutputsForDevice() device %04x", device);
1561 for (size_t i = 0; i < mOutputs.size(); i++) {
1562 ALOGV("output %d isDuplicated=%d device=%04x",
1563 i, mOutputs.valueAt(i)->isDuplicated(), mOutputs.valueAt(i)->supportedDevices());
1564 if ((device & mOutputs.valueAt(i)->supportedDevices()) == device) {
1565 ALOGV("getOutputsForDevice() found output %d", mOutputs.keyAt(i));
1566 outputs.add(mOutputs.keyAt(i));
1572 bool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1573 SortedVector<audio_io_handle_t>& outputs2)
1575 if (outputs1.size() != outputs2.size()) {
1578 for (size_t i = 0; i < outputs1.size(); i++) {
1579 if (outputs1[i] != outputs2[i]) {
1586 void AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1588 SortedVector<audio_io_handle_t> srcOutputs =
1589 getOutputsForDevice(getDeviceForStrategy(strategy, true /*fromCache*/));
1590 SortedVector<audio_io_handle_t> dstOutputs =
1591 getOutputsForDevice(getDeviceForStrategy(strategy, false /*fromCache*/));
1593 if (!vectorsEqual(srcOutputs,dstOutputs)) {
1594 // FIXME dstOutputs[0] happens to be the output to use, what guarantees it is always true?
1595 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1596 strategy, srcOutputs[0], dstOutputs[0]);
1597 // mute strategy while moving tracks from one output to another
1598 for (size_t i = 0; i < srcOutputs.size(); i++) {
1599 setStrategyMute(strategy, true, srcOutputs[i]);
1600 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS);
1603 // Move effects associated to this strategy from previous output to new output
1604 //FIXME: removing this code works for effects applied to a particular session as they will be
1605 // re-connected when the tracks are re-created after being invalidated.
1606 // However we need to define a policy for global effects when more than one output is possible
1607 // for (size_t i = 0; i < mEffects.size(); i++) {
1608 // EffectDescriptor *desc = mEffects.valueAt(i);
1609 // if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE &&
1610 // desc->mStrategy == strategy &&
1611 // desc->mIo == srcOutputs[0]) {
1612 // ALOGV("checkOutputForStrategy() moving effect %d to output %d",
1613 // mEffects.keyAt(i), dstOutputs[0]);
1614 // mpClientInterface->moveEffects(desc->mSession, srcOutputs[0], dstOutputs[0]);
1615 // desc->mIo = dstOutputs[0];
1618 // Move tracks associated to this strategy from previous output to new output
1619 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1620 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
1621 //FIXME see fixme on name change
1622 mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutputs[0]);
1628 void AudioPolicyManagerBase::checkOutputForAllStrategies()
1630 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1631 checkOutputForStrategy(STRATEGY_PHONE);
1632 checkOutputForStrategy(STRATEGY_SONIFICATION);
1633 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1634 checkOutputForStrategy(STRATEGY_MEDIA);
1635 checkOutputForStrategy(STRATEGY_DTMF);
1638 audio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1644 for (size_t i = 0; i < mOutputs.size(); i++) {
1645 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1646 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1647 return mOutputs.keyAt(i);
1654 void AudioPolicyManagerBase::checkA2dpSuspend()
1659 audio_io_handle_t a2dpOutput = getA2dpOutput();
1660 if (a2dpOutput == 0) {
1664 // suspend A2DP output if:
1665 // (NOT already suspended) &&
1666 // ((SCO device is connected &&
1667 // (forced usage for communication || for record is SCO))) ||
1668 // (phone state is ringing || in call)
1670 // restore A2DP output if:
1671 // (Already suspended) &&
1672 // ((SCO device is NOT connected ||
1673 // (forced usage NOT for communication && NOT for record is SCO))) &&
1674 // (phone state is NOT ringing && NOT in call)
1676 if (mA2dpSuspended) {
1677 if (((mScoDeviceAddress == "") ||
1678 ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1679 (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1680 ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1681 (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1683 mpClientInterface->restoreOutput(a2dpOutput);
1684 mA2dpSuspended = false;
1687 if (((mScoDeviceAddress != "") &&
1688 ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1689 (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1690 ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1691 (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1693 mpClientInterface->suspendOutput(a2dpOutput);
1694 mA2dpSuspended = true;
1699 audio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1701 audio_devices_t device = (audio_devices_t)0;
1703 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1704 // check the following by order of priority to request a routing change if necessary:
1705 // 1: the strategy enforced audible is active on the output:
1706 // use device for strategy enforced audible
1707 // 2: we are in call or the strategy phone is active on the output:
1708 // use device for strategy phone
1709 // 3: the strategy sonification is active on the output:
1710 // use device for strategy sonification
1711 // 4: the strategy "respectful" sonification is active on the output:
1712 // use device for strategy "respectful" sonification
1713 // 5: the strategy media is active on the output:
1714 // use device for strategy media
1715 // 6: the strategy DTMF is active on the output:
1716 // use device for strategy DTMF
1717 if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1718 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1719 } else if (isInCall() ||
1720 outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1721 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1722 } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1723 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
1724 } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
1725 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1726 } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1727 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1728 } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1729 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1732 ALOGV("getNewDevice() selected device %x", device);
1736 uint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1737 return (uint32_t)getStrategy(stream);
1740 audio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1741 audio_devices_t devices;
1742 // By checking the range of stream before calling getStrategy, we avoid
1743 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
1744 // and then return STRATEGY_MEDIA, but we want to return the empty set.
1745 if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
1746 devices = (audio_devices_t)0;
1748 AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
1749 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
1754 AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1755 AudioSystem::stream_type stream) {
1756 // stream to strategy mapping
1758 case AudioSystem::VOICE_CALL:
1759 case AudioSystem::BLUETOOTH_SCO:
1760 return STRATEGY_PHONE;
1761 case AudioSystem::RING:
1762 case AudioSystem::ALARM:
1763 return STRATEGY_SONIFICATION;
1764 case AudioSystem::NOTIFICATION:
1765 return STRATEGY_SONIFICATION_RESPECTFUL;
1766 case AudioSystem::DTMF:
1767 return STRATEGY_DTMF;
1769 ALOGE("unknown stream type");
1770 case AudioSystem::SYSTEM:
1771 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1772 // while key clicks are played produces a poor result
1773 case AudioSystem::TTS:
1774 case AudioSystem::MUSIC:
1775 return STRATEGY_MEDIA;
1776 case AudioSystem::ENFORCED_AUDIBLE:
1777 return STRATEGY_ENFORCED_AUDIBLE;
1781 void AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
1783 case AudioSystem::MUSIC:
1784 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1785 updateDeviceForStrategy();
1792 audio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
1795 uint32_t device = 0;
1798 ALOGV("getDeviceForStrategy() from cache strategy %d, device %x",
1799 strategy, mDeviceForStrategy[strategy]);
1800 return mDeviceForStrategy[strategy];
1805 case STRATEGY_SONIFICATION_RESPECTFUL:
1807 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
1808 } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
1809 // while media is playing (or has recently played), use the same device
1810 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
1812 // when media is not playing anymore, fall back on the sonification behavior
1813 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
1820 // when off call, DTMF strategy follows the same rules as MEDIA strategy
1821 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
1824 // when in call, DTMF and PHONE strategies follow the same rules
1827 case STRATEGY_PHONE:
1828 // for phone strategy, we first consider the forced use and then the available devices by order
1830 switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
1831 case AudioSystem::FORCE_BT_SCO:
1832 if (!isInCall() || strategy != STRATEGY_DTMF) {
1833 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1836 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
1838 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
1840 // if SCO device is requested but no SCO device is available, fall back to default case
1843 default: // FORCE_NONE
1844 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1846 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1848 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
1849 if (mHasA2dp && !isInCall() && !mA2dpSuspended) {
1850 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1852 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1855 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1857 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1859 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1861 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1863 device = mDefaultOutputDevice;
1865 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
1869 case AudioSystem::FORCE_SPEAKER:
1870 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1871 // A2DP speaker when forcing to speaker output
1872 if (mHasA2dp && !isInCall() && !mA2dpSuspended) {
1873 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1876 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1878 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1880 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1882 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1884 device = mDefaultOutputDevice;
1886 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
1892 case STRATEGY_SONIFICATION:
1894 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1895 // handleIncallSonification().
1897 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
1902 case STRATEGY_ENFORCED_AUDIBLE:
1903 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
1904 // except when in call where it doesn't default to STRATEGY_PHONE behavior
1906 device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1908 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
1910 // The second device used for sonification is the same as the device used by media strategy
1913 case STRATEGY_MEDIA: {
1914 uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1916 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1918 if (mHasA2dp && (getA2dpOutput() != 0) && !mA2dpSuspended) {
1920 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1923 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1926 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1930 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1933 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1936 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1939 device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1942 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
1943 // STRATEGY_ENFORCED_AUDIBLE, 0 otherwise
1946 device = mDefaultOutputDevice;
1948 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
1953 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1957 ALOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1958 return (audio_devices_t)device;
1961 void AudioPolicyManagerBase::updateDeviceForStrategy()
1963 for (int i = 0; i < NUM_STRATEGIES; i++) {
1964 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
1968 void AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
1971 // mute/unmute strategies using an incompatible device combination
1972 // if muting, wait for the audio in pcm buffer to be drained before proceeding
1973 // if unmuting, unmute only after the specified delay
1975 if (outputDesc->isDuplicated()) {
1979 uint32_t muteWaitMs = 0;
1980 audio_devices_t device = outputDesc->device();
1981 bool shouldMute = (outputDesc->refCount() != 0) &&
1982 (AudioSystem::popCount(device) >= 2);
1984 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
1985 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
1986 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
1987 bool doMute = false;
1989 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
1991 outputDesc->mStrategyMutedByDevice[i] = true;
1992 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
1994 outputDesc->mStrategyMutedByDevice[i] = false;
1997 for (size_t j = 0; j < mOutputs.size(); j++) {
1998 AudioOutputDescriptor *desc = mOutputs.valueAt(j);
1999 if ((desc->supportedDevices() & outputDesc->supportedDevices()) == 0) {
2002 audio_io_handle_t curOutput = mOutputs.keyAt(j);
2003 ALOGV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
2004 mute ? "muting" : "unmuting", i, curDevice, curOutput);
2005 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
2006 if (mute && (desc->strategyRefCount((routing_strategy)i) != 0)) {
2007 if (muteWaitMs < desc->latency()) {
2008 muteWaitMs = desc->latency();
2015 // FIXME: should not need to double latency if volume could be applied immediately by the
2016 // audioflinger mixer. We must account for the delay between now and the next time
2017 // the audioflinger thread for this output will process a buffer (which corresponds to
2018 // one buffer size, usually 1/2 or 1/4 of the latency).
2020 // wait for the PCM output buffers to empty before proceeding with the rest of the command
2021 if (muteWaitMs > delayMs) {
2022 usleep((muteWaitMs - delayMs)*1000);
2026 void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2027 audio_devices_t device,
2031 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2032 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2033 AudioParameter param;
2035 if (outputDesc->isDuplicated()) {
2036 setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2037 setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2040 // filter devices according to output selected
2041 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2043 audio_devices_t prevDevice = outputDesc->mDevice;
2045 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
2048 outputDesc->mDevice = device;
2050 checkDeviceMuteStrategies(outputDesc, delayMs);
2052 // Do not change the routing if:
2053 // - the requested device is 0
2054 // - the requested device is the same as current device and force is not specified.
2055 // Doing this check here allows the caller to call setOutputDevice() without conditions
2056 if ((device == 0 || device == prevDevice) && !force) {
2057 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2061 ALOGV("setOutputDevice() changing device");
2063 param.addInt(String8(AudioParameter::keyRouting), (int)device);
2064 mpClientInterface->setParameters(output, param.toString(), delayMs);
2066 // update stream volumes according to new device
2067 applyStreamVolumes(output, device, delayMs);
2070 AudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
2071 uint32_t samplingRate,
2073 uint32_t channelMask)
2075 IOProfile *inProfile = NULL;
2077 // Choose an input profile based on the requested capture parameters: select the first available
2078 // profile supporting all requested parameters.
2080 for (size_t i = 0; i < mInputProfiles.size(); i++)
2082 IOProfile *profile = mInputProfiles[i];
2084 if ((profile->mSupportedDevices & device) == 0) {
2087 for (j = 0; j < profile->mSamplingRates.size(); j++)
2089 if (profile->mSamplingRates[j] == samplingRate) {
2093 if (j == profile->mSamplingRates.size()) {
2096 for (j = 0; j < profile->mFormats.size(); j++)
2098 if (profile->mFormats[j] == format) {
2102 if (j == profile->mFormats.size()) {
2105 for (j = 0; j < profile->mChannelMasks.size(); j++)
2107 if (profile->mChannelMasks[j] == channelMask) {
2111 if (j == profile->mChannelMasks.size()) {
2115 inProfile = profile;
2118 ALOGW_IF(inProfile == NULL, "getInputProfile() no available input for device %04x"
2119 "samplingRate %d, format %d channel mask %04x",
2127 audio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2129 uint32_t device = 0;
2131 switch(inputSource) {
2132 case AUDIO_SOURCE_DEFAULT:
2133 case AUDIO_SOURCE_MIC:
2134 case AUDIO_SOURCE_VOICE_RECOGNITION:
2135 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2136 if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2137 mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2138 device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2139 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
2140 device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
2141 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BUILTIN_MIC) {
2142 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
2145 case AUDIO_SOURCE_CAMCORDER:
2146 if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BACK_MIC) {
2147 device = AudioSystem::DEVICE_IN_BACK_MIC;
2148 } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BUILTIN_MIC) {
2149 device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
2152 case AUDIO_SOURCE_VOICE_UPLINK:
2153 case AUDIO_SOURCE_VOICE_DOWNLINK:
2154 case AUDIO_SOURCE_VOICE_CALL:
2155 if (mAvailableInputDevices & AudioSystem::DEVICE_IN_VOICE_CALL) {
2156 device = AudioSystem::DEVICE_IN_VOICE_CALL;
2160 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2163 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2164 return (audio_devices_t)device;
2167 audio_io_handle_t AudioPolicyManagerBase::getActiveInput()
2169 for (size_t i = 0; i < mInputs.size(); i++) {
2170 if (mInputs.valueAt(i)->mRefCount > 0) {
2171 return mInputs.keyAt(i);
2178 audio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2181 // this happens when forcing a route update and no track is active on an output.
2182 // In this case the returned category is not important.
2183 device = AUDIO_DEVICE_OUT_SPEAKER;
2184 } else if (AudioSystem::popCount(device) > 1) {
2185 // Multiple device selection is either:
2186 // - speaker + one other device: give priority to speaker in this case.
2187 // - one A2DP device + another device: happens with duplicated output. In this case
2188 // retain the device on the A2DP output as the other must not correspond to an active
2189 // selection if not the speaker.
2190 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2191 device = AUDIO_DEVICE_OUT_SPEAKER;
2193 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2197 ALOGW_IF(AudioSystem::popCount(device) != 1,
2198 "getDeviceForVolume() invalid device combination: %08x",
2204 AudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2206 switch(getDeviceForVolume(device)) {
2207 case AUDIO_DEVICE_OUT_EARPIECE:
2208 return DEVICE_CATEGORY_EARPIECE;
2209 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2210 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2211 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2212 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2213 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2214 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2215 return DEVICE_CATEGORY_HEADSET;
2216 case AUDIO_DEVICE_OUT_SPEAKER:
2217 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2218 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2219 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2221 return DEVICE_CATEGORY_SPEAKER;
2225 float AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2228 device_category deviceCategory = getDeviceCategory(device);
2229 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2231 // the volume index in the UI is relative to the min and max volume indices for this stream type
2232 int nbSteps = 1 + curve[VOLMAX].mIndex -
2233 curve[VOLMIN].mIndex;
2234 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2235 (streamDesc.mIndexMax - streamDesc.mIndexMin);
2237 // find what part of the curve this index volume belongs to, or if it's out of bounds
2239 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
2241 } else if (volIdx < curve[VOLKNEE1].mIndex) {
2243 } else if (volIdx < curve[VOLKNEE2].mIndex) {
2245 } else if (volIdx <= curve[VOLMAX].mIndex) {
2247 } else { // out of bounds
2251 // linear interpolation in the attenuation table in dB
2252 float decibels = curve[segment].mDBAttenuation +
2253 ((float)(volIdx - curve[segment].mIndex)) *
2254 ( (curve[segment+1].mDBAttenuation -
2255 curve[segment].mDBAttenuation) /
2256 ((float)(curve[segment+1].mIndex -
2257 curve[segment].mIndex)) );
2259 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2261 ALOGV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2262 curve[segment].mIndex, volIdx,
2263 curve[segment+1].mIndex,
2264 curve[segment].mDBAttenuation,
2266 curve[segment+1].mDBAttenuation,
2269 return amplification;
2272 const AudioPolicyManagerBase::VolumeCurvePoint
2273 AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2274 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2277 const AudioPolicyManagerBase::VolumeCurvePoint
2278 AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2279 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2282 const AudioPolicyManagerBase::VolumeCurvePoint
2283 AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2284 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2287 const AudioPolicyManagerBase::VolumeCurvePoint
2288 AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2289 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2293 const AudioPolicyManagerBase::VolumeCurvePoint
2294 *AudioPolicyManagerBase::sVolumeProfiles[AudioPolicyManagerBase::NUM_STRATEGIES]
2295 [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2297 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2298 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2299 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
2302 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2303 sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2304 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
2306 { // STRATEGY_SONIFICATION
2307 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2308 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2309 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
2311 { // STRATEGY_SONIFICATION_RESPECTFUL uses same volumes as SONIFICATION
2312 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2313 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2314 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
2317 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2318 sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2319 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
2321 { // STRATEGY_ENFORCED_AUDIBLE
2322 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2323 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2324 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
2328 void AudioPolicyManagerBase::initializeVolumeCurves()
2330 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2331 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2332 mStreams[i].mVolumeCurve[j] =
2333 sVolumeProfiles[getStrategy((AudioSystem::stream_type)i)][j];
2338 float AudioPolicyManagerBase::computeVolume(int stream,
2340 audio_io_handle_t output,
2341 audio_devices_t device)
2344 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2345 StreamDescriptor &streamDesc = mStreams[stream];
2348 device = outputDesc->device();
2351 // if volume is not 0 (not muted), force media volume to max on digital output
2352 if (stream == AudioSystem::MUSIC &&
2353 index != mStreams[stream].mIndexMin &&
2354 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2355 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
2359 volume = volIndexToAmpl(device, streamDesc, index);
2361 // if a headset is connected, apply the following rules to ring tones and notifications
2362 // to avoid sound level bursts in user's ears:
2363 // - always attenuate ring tones and notifications volume by 6dB
2364 // - if music is playing, always limit the volume to current music volume,
2365 // with a minimum threshold at -36dB so that notification is always perceived.
2366 const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2367 if ((device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
2368 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2369 AudioSystem::DEVICE_OUT_WIRED_HEADSET |
2370 AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
2371 ((stream_strategy == STRATEGY_SONIFICATION)
2372 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
2373 || (stream == AudioSystem::SYSTEM)) &&
2374 streamDesc.mCanBeMuted) {
2375 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2376 // when the phone is ringing we must consider that music could have been paused just before
2377 // by the music application and behave as if music was active if the last music track was
2379 if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
2380 float musicVol = computeVolume(AudioSystem::MUSIC,
2381 mStreams[AudioSystem::MUSIC].getVolumeIndex(device),
2384 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2385 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2386 if (volume > minVol) {
2388 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2396 status_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2398 audio_io_handle_t output,
2399 audio_devices_t device,
2404 // do not change actual stream volume if the stream is muted
2405 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
2406 ALOGV("checkAndSetVolume() stream %d muted count %d",
2407 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2411 // do not change in call volume if bluetooth is connected and vice versa
2412 if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2413 (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
2414 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2415 stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2416 return INVALID_OPERATION;
2419 float volume = computeVolume(stream, index, output, device);
2420 // We actually change the volume if:
2421 // - the float value returned by computeVolume() changed
2422 // - the force flag is set
2423 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2425 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
2426 ALOGV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2427 if (stream == AudioSystem::VOICE_CALL ||
2428 stream == AudioSystem::DTMF ||
2429 stream == AudioSystem::BLUETOOTH_SCO) {
2430 // offset value to reflect actual hardware volume that never reaches 0
2431 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2432 volume = 0.01 + 0.99 * volume;
2433 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2435 if (stream == AudioSystem::BLUETOOTH_SCO) {
2436 mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2440 mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2443 if (stream == AudioSystem::VOICE_CALL ||
2444 stream == AudioSystem::BLUETOOTH_SCO) {
2446 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2447 if (stream == AudioSystem::VOICE_CALL) {
2448 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2453 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2454 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2455 mLastVoiceVolume = voiceVolume;
2462 void AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2463 audio_devices_t device,
2467 ALOGV("applyStreamVolumes() for output %d and device %x", output, device);
2469 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2470 checkAndSetVolume(stream,
2471 mStreams[stream].getVolumeIndex(device),
2479 void AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
2481 ALOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2482 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2483 if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
2484 setStreamMute(stream, on, output, delayMs);
2489 void AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
2491 StreamDescriptor &streamDesc = mStreams[stream];
2492 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2493 audio_devices_t device = outputDesc->device();
2495 ALOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
2498 if (outputDesc->mMuteCount[stream] == 0) {
2499 if (streamDesc.mCanBeMuted) {
2500 checkAndSetVolume(stream, 0, output, device, delayMs);
2503 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2504 outputDesc->mMuteCount[stream]++;
2506 if (outputDesc->mMuteCount[stream] == 0) {
2507 ALOGW("setStreamMute() unmuting non muted stream!");
2510 if (--outputDesc->mMuteCount[stream] == 0) {
2511 checkAndSetVolume(stream,
2512 streamDesc.getVolumeIndex((audio_devices_t)device),
2520 void AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2522 // if the stream pertains to sonification strategy and we are in call we must
2523 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2524 // in the device used for phone strategy and play the tone if the selected device does not
2525 // interfere with the device used for phone strategy
2526 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2527 // many times as there are active tracks on the output
2528 const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2529 if ((stream_strategy == STRATEGY_SONIFICATION) ||
2530 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2531 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
2532 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2533 stream, starting, outputDesc->mDevice, stateChange);
2534 if (outputDesc->mRefCount[stream]) {
2537 muteCount = outputDesc->mRefCount[stream];
2539 if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
2540 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2541 for (int i = 0; i < muteCount; i++) {
2542 setStreamMute(stream, starting, mPrimaryOutput);
2545 ALOGV("handleIncallSonification() high visibility");
2546 if (outputDesc->device() &
2547 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
2548 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2549 for (int i = 0; i < muteCount; i++) {
2550 setStreamMute(stream, starting, mPrimaryOutput);
2554 mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2556 mpClientInterface->stopTone();
2563 bool AudioPolicyManagerBase::isInCall()
2565 return isStateInCall(mPhoneState);
2568 bool AudioPolicyManagerBase::isStateInCall(int state) {
2569 return ((state == AudioSystem::MODE_IN_CALL) ||
2570 (state == AudioSystem::MODE_IN_COMMUNICATION));
2573 bool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
2574 uint32_t samplingRate,
2577 AudioSystem::output_flags flags,
2580 return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2581 (format != 0 && !AudioSystem::isLinearPCM(format)));
2584 uint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2586 return MAX_EFFECTS_CPU_LOAD;
2589 uint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2591 return MAX_EFFECTS_MEMORY;
2594 // --- AudioOutputDescriptor class implementation
2596 AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
2597 const IOProfile *profile)
2598 : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
2599 mFlags((AudioSystem::output_flags)0), mDevice((audio_devices_t)0),
2600 mOutput1(0), mOutput2(0), mProfile(profile)
2602 // clear usage count for all stream types
2603 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2605 mCurVolume[i] = -1.0;
2611 audio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2613 if (isDuplicated()) {
2614 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2620 uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
2622 if (isDuplicated()) {
2623 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
2629 bool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
2630 const AudioOutputDescriptor *outputDesc)
2632 if (isDuplicated()) {
2633 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
2634 } else if (outputDesc->isDuplicated()){
2635 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
2637 return strcmp(mProfile->mModuleName, outputDesc->mProfile->mModuleName) == 0;
2641 void AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2643 // forward usage count change to attached outputs
2644 if (isDuplicated()) {
2645 mOutput1->changeRefCount(stream, delta);
2646 mOutput2->changeRefCount(stream, delta);
2648 if ((delta + (int)mRefCount[stream]) < 0) {
2649 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2650 mRefCount[stream] = 0;
2653 mRefCount[stream] += delta;
2654 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2657 uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2659 uint32_t refcount = 0;
2660 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2661 refcount += mRefCount[i];
2666 uint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2668 uint32_t refCount = 0;
2669 for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2670 if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2671 refCount += mRefCount[i];
2677 audio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
2679 if (isDuplicated()) {
2680 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
2682 return mProfile->mSupportedDevices ;
2686 status_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2688 const size_t SIZE = 256;
2692 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2693 result.append(buffer);
2694 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2695 result.append(buffer);
2696 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2697 result.append(buffer);
2698 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2699 result.append(buffer);
2700 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2701 result.append(buffer);
2702 snprintf(buffer, SIZE, " Devices %08x\n", device());
2703 result.append(buffer);
2704 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2705 result.append(buffer);
2706 for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2707 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2708 result.append(buffer);
2710 write(fd, result.string(), result.size());
2715 // --- AudioInputDescriptor class implementation
2717 AudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
2718 : mSamplingRate(0), mFormat(0), mChannels(0),
2719 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice((audio_devices_t)0), mRefCount(0),
2720 mInputSource(0), mProfile(profile)
2724 status_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2726 const size_t SIZE = 256;
2730 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2731 result.append(buffer);
2732 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2733 result.append(buffer);
2734 snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2735 result.append(buffer);
2736 snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2737 result.append(buffer);
2738 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2739 result.append(buffer);
2740 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2741 result.append(buffer);
2742 write(fd, result.string(), result.size());
2747 // --- StreamDescriptor class implementation
2749 AudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
2750 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
2752 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
2755 int AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
2757 device = AudioPolicyManagerBase::getDeviceForVolume(device);
2758 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
2759 if (mIndexCur.indexOfKey(device) < 0) {
2760 device = AUDIO_DEVICE_OUT_DEFAULT;
2762 return mIndexCur.valueFor(device);
2765 void AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
2767 const size_t SIZE = 256;
2771 snprintf(buffer, SIZE, "%s %02d %02d ",
2772 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
2773 result.append(buffer);
2774 for (size_t i = 0; i < mIndexCur.size(); i++) {
2775 snprintf(buffer, SIZE, "%04x : %02d, ",
2777 mIndexCur.valueAt(i));
2778 result.append(buffer);
2780 result.append("\n");
2782 write(fd, result.string(), result.size());
2785 // --- EffectDescriptor class implementation
2787 status_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2789 const size_t SIZE = 256;
2793 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
2794 result.append(buffer);
2795 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2796 result.append(buffer);
2797 snprintf(buffer, SIZE, " Session: %d\n", mSession);
2798 result.append(buffer);
2799 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
2800 result.append(buffer);
2801 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
2802 result.append(buffer);
2803 write(fd, result.string(), result.size());
2808 // --- IOProfile class implementation
2810 AudioPolicyManagerBase::IOProfile::IOProfile(const char *module)
2811 : mFlags((audio_policy_output_flags_t)0),
2812 mModuleName(strndup(module, AUDIO_HARDWARE_MODULE_ID_MAX_LEN))
2816 AudioPolicyManagerBase::IOProfile::~IOProfile()
2821 void AudioPolicyManagerBase::IOProfile::dump(int fd)
2823 const size_t SIZE = 256;
2827 snprintf(buffer, SIZE, " - sampling rates: ");
2828 result.append(buffer);
2829 for (size_t i = 0; i < mSamplingRates.size(); i++) {
2830 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
2831 result.append(buffer);
2832 result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
2835 snprintf(buffer, SIZE, " - channel masks: ");
2836 result.append(buffer);
2837 for (size_t i = 0; i < mChannelMasks.size(); i++) {
2838 snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
2839 result.append(buffer);
2840 result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
2843 snprintf(buffer, SIZE, " - formats: ");
2844 result.append(buffer);
2845 for (size_t i = 0; i < mFormats.size(); i++) {
2846 snprintf(buffer, SIZE, "%d", mFormats[i]);
2847 result.append(buffer);
2848 result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
2851 snprintf(buffer, SIZE, " - devices: %04x\n", mSupportedDevices);
2852 result.append(buffer);
2853 snprintf(buffer, SIZE, " - flags: %04x\n", mFlags);
2854 result.append(buffer);
2855 snprintf(buffer, SIZE, " - hw module: %s\n", mModuleName);
2856 result.append(buffer);
2858 write(fd, result.string(), result.size());
2861 // --- audio_policy.conf file parsing
2863 struct StringToEnum {
2868 #define STRING_TO_ENUM(string) { #string, string }
2869 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
2871 const struct StringToEnum sDeviceNameToEnumTable[] = {
2872 STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
2873 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
2874 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
2875 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
2876 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
2877 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
2878 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
2879 STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
2880 STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
2881 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
2882 STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
2883 STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
2884 STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
2885 STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
2888 const struct StringToEnum sFlagNameToEnumTable[] = {
2889 STRING_TO_ENUM(AUDIO_POLICY_OUTPUT_FLAG_DIRECT),
2890 STRING_TO_ENUM(AUDIO_POLICY_OUTPUT_FLAG_PRIMARY),
2893 const struct StringToEnum sFormatNameToEnumTable[] = {
2894 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
2895 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
2896 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
2897 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
2898 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
2901 const struct StringToEnum sOutChannelsNameToEnumTable[] = {
2902 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
2903 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
2904 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
2905 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
2908 const struct StringToEnum sInChannelsNameToEnumTable[] = {
2909 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
2910 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
2914 uint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
2918 for (size_t i = 0; i < size; i++) {
2919 if (strcmp(table[i].name, name) == 0) {
2920 ALOGV("stringToEnum() found %s", table[i].name);
2921 return table[i].value;
2927 audio_policy_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
2931 // it is OK to cast name to non const here as we are not going to use it after
2932 // strtok() modifies it
2933 char *flagName = strtok(name, "|");
2934 while (flagName != NULL) {
2935 if (strlen(flagName) != 0) {
2936 flag |= stringToEnum(sFlagNameToEnumTable,
2937 ARRAY_SIZE(sFlagNameToEnumTable),
2940 flagName = strtok(NULL, "|");
2942 return (audio_policy_output_flags_t)flag;
2945 audio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
2947 uint32_t device = 0;
2949 char *devName = strtok(name, "|");
2950 while (devName != NULL) {
2951 if (strlen(devName) != 0) {
2952 device |= stringToEnum(sDeviceNameToEnumTable,
2953 ARRAY_SIZE(sDeviceNameToEnumTable),
2956 devName = strtok(NULL, "|");
2958 return (audio_devices_t)device;
2961 void AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
2963 char *str = strtok(name, "|");
2965 while (str != NULL) {
2966 uint32_t rate = atoi(str);
2968 ALOGV("loadSamplingRates() adding rate %d", rate);
2969 profile->mSamplingRates.add(rate);
2971 str = strtok(NULL, "|");
2976 void AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
2978 char *str = strtok(name, "|");
2980 while (str != NULL) {
2981 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
2982 ARRAY_SIZE(sFormatNameToEnumTable),
2985 profile->mFormats.add(format);
2987 str = strtok(NULL, "|");
2992 void AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
2994 const char *str = strtok(name, "|");
2996 ALOGV("loadInChannels() %s", name);
2997 while (str != NULL) {
2998 audio_channel_mask_t channelMask =
2999 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
3000 ARRAY_SIZE(sInChannelsNameToEnumTable),
3002 if (channelMask != 0) {
3003 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
3004 profile->mChannelMasks.add(channelMask);
3006 str = strtok(NULL, "|");
3011 void AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
3013 const char *str = strtok(name, "|");
3014 audio_channel_mask_t channelMask;
3016 while (str != NULL) {
3017 channelMask = stringToEnum(sOutChannelsNameToEnumTable,
3018 ARRAY_SIZE(sOutChannelsNameToEnumTable),
3020 if (channelMask != 0) {
3021 profile->mChannelMasks.add(channelMask);
3023 str = strtok(NULL, "|");
3028 status_t AudioPolicyManagerBase::loadInput(cnode *root, const char *module)
3030 cnode *node = root->first_child;
3032 IOProfile *profile = new IOProfile(module);
3035 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
3036 loadSamplingRates((char *)node->value, profile);
3037 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
3038 loadFormats((char *)node->value, profile);
3039 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
3040 loadInChannels((char *)node->value, profile);
3041 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
3042 profile->mSupportedDevices = parseDeviceNames((char *)node->value);
3046 ALOGW_IF(profile->mSupportedDevices == (audio_devices_t)0,
3047 "loadInput() invalid supported devices");
3048 ALOGW_IF(profile->mChannelMasks.size() == 0,
3049 "loadInput() invalid supported channel masks");
3050 ALOGW_IF(profile->mSamplingRates.size() == 0,
3051 "loadInput() invalid supported sampling rates");
3052 ALOGW_IF(profile->mFormats.size() == 0,
3053 "loadInput() invalid supported formats");
3054 if ((profile->mSupportedDevices != (audio_devices_t)0) &&
3055 (profile->mChannelMasks.size() != 0) &&
3056 (profile->mSamplingRates.size() != 0) &&
3057 (profile->mFormats.size() != 0)) {
3059 ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
3061 mInputProfiles.add(profile);
3069 status_t AudioPolicyManagerBase::loadOutput(cnode *root, const char *module)
3071 cnode *node = root->first_child;
3073 IOProfile *profile = new IOProfile(module);
3076 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
3077 loadSamplingRates((char *)node->value, profile);
3078 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
3079 loadFormats((char *)node->value, profile);
3080 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
3081 loadOutChannels((char *)node->value, profile);
3082 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
3083 profile->mSupportedDevices = parseDeviceNames((char *)node->value);
3084 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
3085 profile->mFlags = parseFlagNames((char *)node->value);
3089 ALOGW_IF(profile->mSupportedDevices == (audio_devices_t)0,
3090 "loadOutput() invalid supported devices");
3091 ALOGW_IF(profile->mChannelMasks.size() == 0,
3092 "loadOutput() invalid supported channel masks");
3093 ALOGW_IF(profile->mSamplingRates.size() == 0,
3094 "loadOutput() invalid supported sampling rates");
3095 ALOGW_IF(profile->mFormats.size() == 0,
3096 "loadOutput() invalid supported formats");
3097 if ((profile->mSupportedDevices != (audio_devices_t)0) &&
3098 (profile->mChannelMasks.size() != 0) &&
3099 (profile->mSamplingRates.size() != 0) &&
3100 (profile->mFormats.size() != 0)) {
3102 ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
3103 profile->mSupportedDevices, profile->mFlags);
3105 mOutputProfiles.add(profile);
3113 void AudioPolicyManagerBase::loadHwModule(cnode *root)
3115 cnode *node = config_find(root, OUTPUTS_TAG);
3116 status_t status = NAME_NOT_FOUND;
3118 if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
3121 node = node->first_child;
3123 ALOGV("loadHwModule() loading output %s", node->name);
3124 status_t tmpStatus = loadOutput(node, root->name);
3125 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
3131 node = config_find(root, INPUTS_TAG);
3133 node = node->first_child;
3135 ALOGV("loadHwModule() loading input %s", node->name);
3136 status_t tmpStatus = loadInput(node, root->name);
3137 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
3143 if (status == NO_ERROR) {
3144 //TODO: load HW module via audioflinger
3148 void AudioPolicyManagerBase::loadHwModules(cnode *root)
3150 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
3155 node = node->first_child;
3157 ALOGV("loadHwModules() loading module %s", node->name);
3163 void AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
3165 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
3169 node = node->first_child;
3171 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
3172 mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3173 ALOGW_IF(mAttachedOutputDevices == 0, "loadGlobalConfig() no attached output devices");
3174 ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
3175 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
3176 mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
3177 ARRAY_SIZE(sDeviceNameToEnumTable),
3178 (char *)node->value);
3179 ALOGW_IF(mDefaultOutputDevice == 0, "loadGlobalConfig() default device not specified");
3180 ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
3181 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
3182 mAvailableInputDevices = parseDeviceNames((char *)node->value);
3183 ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
3189 status_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
3194 data = (char *)load_file(path, NULL);
3198 root = config_node("", "");
3199 config_load(root, data);
3201 loadGlobalConfig(root);
3202 loadHwModules(root);
3212 }; // namespace android