1、架構架構
2、MediaServer初始化app
全部的media服務都在進程mediaserver裏。其代碼在framework/base/media/mediaserver/main_mediaserver.cpp中。初始化過程以下。函數
1. 先初始化AudioFlingerspa
其初始化經過AudioFlinger的父類BindService建立惟一的AudioFlinger實例。線程
2. 而後初始化MediaPlayerService和CameraServicerest
3. 最後初始化AudioPolicyServicecode
3、 AudioFlinger分析server
AudioFlinger負責管理每一個音軌AudioTrack及RecordTrack,主音量控制,每種聲音流的屬性設置,設備控制,音效控制。blog
4、 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?)
5、 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;
6、 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