Android Audio 分析
2013-08-06 08:55 Rudrj2 阅读(5126) 评论(0) 编辑 收藏 举报一、架构
二、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中。
接口
1 adjustVolume 2 adjustSuggestedStreamVolume 3 adjustStreamVolume 4 setStreamVolume 5 setStreamSolo 6 setStreamMute 7 getStreamVolume 8 getStreamMaxVolume 9 setRingerMode 10 getRingerMode 11 setVibrateSetting 12 getVibrateSetting 13 shouldVibrate 14 setMode 15 getMode 16 playSoundEffect 17 playSoundEffectVolume 18 loadSoundEffects 19 unloadSoundEffects 20 reloadAudioSettings 21 setSpeakerphoneOn 22 isSpeakerphoneOn 23 setBluetoothScoOn 24 isBluetoothScoOn 25 requestAudioFocus 26 abandonAudioFocus 27 unregisterAudioFocusClient 28 registerMediaButtonEventReceiver 29 unregisterMediaButtonEventReceiver 30 startBluetoothSco 31 stopBluetoothSco
服务端代码在framework/base/services/audioflinger/AudioPolicyService.cpp中,实现了AudioPolicyService本地代码及服务内部使用的AudioPolicyClientInterface接口,该接口被AudioPolicyManager调用。
1 openOutput 2 openDuplicateOutput 3 closeOutput 4 suspendOutput 5 restoreOutput 6 openInput 7 loseInput 8 setStreamVolume 9 setStreamOutput 10 setParameters 11 getParameters 12 startTone 13 stopTone 14 setVoiceVolume 15 moveEffects
AudioPolicyService会启动两个AudioCommandThread类的线程mTonePlaybackThread和mAudioCommandThread,然后构造AudioPolicyManager的实例mpPolicyManager。基本上所有的服务接口都会在检查权限后调用mpPolicyManager的相应实现。
mTonePlaybackThread用户播放通话中的通知音
mAudioCommandThread用于设置AudioFlinger的一些设置(为什么使用AudioCommandThread,然后调用AudioSystem中接口控制AudioFlinger?)
五、 AudioManager分析
1. 音频焦点转
1 requestAudioFocus 请求焦点 2 abandonAudioFocus 放弃焦点
requestAudioFocus的第一参数是Listener,可以是下面枚举的后三个值
第二个参数是当前流类型
最后一个参数可以是以下的前三个值
1 AUDIOFOCUS_GAIN 1 2 AUDIOFOCUS_GAIN_TRANSIENT 2 3 AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3; 4 AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN; 5 AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT; 6 AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = 7 -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中定义)
1 enum audio_mode { 2 MODE_INVALID = -2, 3 MODE_CURRENT = -1, 4 MODE_NORMAL = 0, 5 MODE_RINGTONE, 6 MODE_IN_CALL, 7 MODE_IN_COMMUNICATION, 8 NUM_MODES // not a valid entry, denotes end-of-list 9 };
策略类型routing_strategy:
STRATEGY_MEDIA 0 STRATEGY_PHONE 1 STRATEGY_SONIFICATION 2 STRATEGY_DTMF 3
设备类型audio_devices:
1 enum audio_devices { 2 // output devices 3 DEVICE_OUT_EARPIECE = 0x1, 4 DEVICE_OUT_SPEAKER = 0x2, 5 DEVICE_OUT_WIRED_HEADSET = 0x4, 6 DEVICE_OUT_WIRED_HEADPHONE = 0x8, 7 DEVICE_OUT_BLUETOOTH_SCO = 0x10, 8 DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20, 9 DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40, 10 DEVICE_OUT_BLUETOOTH_A2DP = 0x80, 11 DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100, 12 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200, 13 DEVICE_OUT_AUX_DIGITAL = 0x400, 14 DEVICE_OUT_DEFAULT = 0x8000, 15 DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE | DEVICE_OUT_SPEAKER | DEVICE_OUT_WIRED_HEADSET | 16 DEVICE_OUT_WIRED_HEADPHONE | DEVICE_OUT_BLUETOOTH_SCO | DEVICE_OUT_BLUETOOTH_SCO_HEADSET | 17 DEVICE_OUT_BLUETOOTH_SCO_CARKIT | DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 18 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER | DEVICE_OUT_AUX_DIGITAL | DEVICE_OUT_DEFAULT), 19 DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 20 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), 21 22 // input devices 23 DEVICE_IN_COMMUNICATION = 0x10000, 24 DEVICE_IN_AMBIENT = 0x20000, 25 DEVICE_IN_BUILTIN_MIC = 0x40000, 26 DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x80000, 27 DEVICE_IN_WIRED_HEADSET = 0x100000, 28 DEVICE_IN_AUX_DIGITAL = 0x200000, 29 DEVICE_IN_VOICE_CALL = 0x400000, 30 DEVICE_IN_BACK_MIC = 0x800000, 31 DEVICE_IN_DEFAULT = 0x80000000, 32 33 DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION | DEVICE_IN_AMBIENT | DEVICE_IN_BUILTIN_MIC | 34 DEVICE_IN_BLUETOOTH_SCO_HEADSET | DEVICE_IN_WIRED_HEADSET | DEVICE_IN_AUX_DIGITAL | 35 DEVICE_IN_VOICE_CALL | DEVICE_IN_BACK_MIC | DEVICE_IN_DEFAULT) 36 };
流程分析:
1.音量控制
initStreamVolume
setStreamVolumeIndex
setPhoneState
2.播放控制
入口函数
1 startOutput 2 3 getStrategy 4 5 getDeviceForStrategy 6 7 setOutputDevice 8 9 applyStreamVolume 10 11 checkAndSetVolume