Android音频架构概览
FROM:http://www.bobbog.com/archives/147
============================
一、架构图
二、MediaServer初始化
所有的media服务都在进程mediaserver里。其代码在framework/base/media/mediaserver/main_mediaserver.cpp中。初始化过程如下。
1. 先初始化AudioFlinger
其初始化通过AudioFlinger的父类BindService创建唯一的AudioFlinger实例。
2. 然后初始化MediaPlayerService和CameraService
3. 最后初始化AudioPolicyService
三、 AudioFlinger分析
AudioFlinger负责管理每个音轨AudioTrack及RecordTrack,主音量控制,每种声音流的属性设置,设备控制,音效控制。
四、 AudioPolicyService分析
AudioPolicyService用于声音策略管理
客户端代码在framework/base/media/libmedia/AudioSystem.cpp中,其实该客户端只是监测AudioPolicyService是否死亡(binderDied)。真正的客户端为BpAudioPolicyService类,在framework/base/media/libmedia/IAudioPolicyService.cpp中。
接口
adjustVolume adjustSuggestedStreamVolume adjustStreamVolume setStreamVolume setStreamSolo setStreamMute getStreamVolume getStreamMaxVolume setRingerMode getRingerMode setVibrateSetting getVibrateSetting shouldVibrate setMode getMode playSoundEffect playSoundEffectVolume loadSoundEffects unloadSoundEffects reloadAudioSettings setSpeakerphoneOn isSpeakerphoneOn setBluetoothScoOn isBluetoothScoOn requestAudioFocus abandonAudioFocus unregisterAudioFocusClient registerMediaButtonEventReceiver unregisterMediaButtonEventReceiver startBluetoothSco stopBluetoothSco
服务端代码在framework/base/services/audioflinger/AudioPolicyService.cpp中,实现了AudioPolicyService本地代码及服务内部使用的AudioPolicyClientInterface接口,该接口被AudioPolicyManager调用。
openOutput openDuplicateOutput closeOutput suspendOutput restoreOutput openInput loseInput setStreamVolume setStreamOutput setParameters getParameters startTone stopTone setVoiceVolume moveEffects
AudioPolicyService会启动两个AudioCommandThread类的线程mTonePlaybackThread和mAudioCommandThread,然后构造AudioPolicyManager的实例mpPolicyManager。基本上所有的服务接口都会在检查权限后调用mpPolicyManager的相应实现。
mTonePlaybackThread用户播放通话中的通知音
mAudioCommandThread用于设置AudioFlinger的一些设置(为什么使用AudioCommandThread,然后调用AudioSystem中接口控制AudioFlinger?)
五、 AudioManager分析
1. 音频焦点转
requestAudioFocus 请求焦点 abandonAudioFocus 放弃焦点
requestAudioFocus的第一参数是Listener,可以是下面枚举的后三个值
第二个参数是当前流类型
最后一个参数可以是以下的前三个值
AUDIOFOCUS_GAIN 1 AUDIOFOCUS_GAIN_TRANSIENT 2 AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3; AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN; AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT; AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
六、 AudioPolicyManager分析
流类型stream_type(在 AudioSystem中定义)
TYPE VALUE STRATEGY
DEFAULT(->MUSIC) -1 STRATEGY_MEDIA(0)
VOICE_CALL 0 STRATEGY_PHONE(1)
SYSTEM 1 STRATEGY_MEDIA(0)
RING 2 STRATEGY_SONIFICATION(2)
MUSIC 3 STRATEGY_MEDIA(0)
ALARM 4 STRATEGY_SONIFICATION(2)
NOTIFICATION 5 STRATEGY_SONIFICATION(2)
BLUETOOTH_SCO 6 STRATEGY_PHONE(1)
// Sounds that cannot be muted by user and must be routed to speaker
ENFORCED_AUDIBLE 7 STRATEGY_SONIFICATION(2)
DTMF 8 STRATEGY_DTMF(3)
TTS 9 STRATEGY_MEDIA(0)
HDMI 10 MV的自定义类型 STRATEGY_MEDIA(0)
FM 11 MV的自定义类型 STRATEGY_MEDIA(0)
音频状态audio_mode(在 AudioSystem中定义)
enum audio_mode { MODE_INVALID = -2, MODE_CURRENT = -1, MODE_NORMAL = 0, MODE_RINGTONE, MODE_IN_CALL, MODE_IN_COMMUNICATION, NUM_MODES // not a valid entry, denotes end-of-list };
策略类型routing_strategy:
STRATEGY_MEDIA 0 STRATEGY_PHONE 1 STRATEGY_SONIFICATION 2 STRATEGY_DTMF 3
设备类型audio_devices:
enum audio_devices { // output devices DEVICE_OUT_EARPIECE = 0x1, DEVICE_OUT_SPEAKER = 0x2, DEVICE_OUT_WIRED_HEADSET = 0x4, DEVICE_OUT_WIRED_HEADPHONE = 0x8, DEVICE_OUT_BLUETOOTH_SCO = 0x10, DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20, DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40, DEVICE_OUT_BLUETOOTH_A2DP = 0x80, DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100, DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200, DEVICE_OUT_AUX_DIGITAL = 0x400, DEVICE_OUT_DEFAULT = 0x8000, DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE | DEVICE_OUT_SPEAKER | DEVICE_OUT_WIRED_HEADSET | DEVICE_OUT_WIRED_HEADPHONE | DEVICE_OUT_BLUETOOTH_SCO | DEVICE_OUT_BLUETOOTH_SCO_HEADSET | DEVICE_OUT_BLUETOOTH_SCO_CARKIT | DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER | DEVICE_OUT_AUX_DIGITAL | DEVICE_OUT_DEFAULT), DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), // input devices DEVICE_IN_COMMUNICATION = 0x10000, DEVICE_IN_AMBIENT = 0x20000, DEVICE_IN_BUILTIN_MIC = 0x40000, DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x80000, DEVICE_IN_WIRED_HEADSET = 0x100000, DEVICE_IN_AUX_DIGITAL = 0x200000, DEVICE_IN_VOICE_CALL = 0x400000, DEVICE_IN_BACK_MIC = 0x800000, DEVICE_IN_DEFAULT = 0x80000000, DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION | DEVICE_IN_AMBIENT | DEVICE_IN_BUILTIN_MIC | DEVICE_IN_BLUETOOTH_SCO_HEADSET | DEVICE_IN_WIRED_HEADSET | DEVICE_IN_AUX_DIGITAL | DEVICE_IN_VOICE_CALL | DEVICE_IN_BACK_MIC | DEVICE_IN_DEFAULT) };
七、其它:
1.音量控制
initStreamVolume
setStreamVolumeIndex
setPhoneState
2.播放控制
入口函数
startOutput
getStrategy
getDeviceForStrategy
setOutputDevice
applyStreamVolume
checkAndSetVolume
===================================END===============================