AudioPolicy的音频输出分析
AudioPolicy是什么
手机本身有听筒和扬声器作为音频输出,手机本身可能有底部(双)Mic、顶部Mic、背部Mic作为音频输入。
手机可能连接有线耳机、多个蓝牙耳机、多个WiFi音频外设,或者车载设备、VR设备、投屏设备等。
AudioPolicy提供了一个音频输入、输出管理的中心,当然它还有一些其他的作用。
分析AudioPolicy的方法
最直接的方法还是去看Google的官网文档,在Android Developer和Androidxref上都能看到相应的文档。
本文使用Log的方法来分析AudioPolicy的一些内容,抓Log的命令是:adb shell dumpsys media.audio_policy
。
这里以某一款小米手机为例。
Audio输出设备
当前可用的输出设备
在没有接入任何外设的情况下,可用的输出设备有:听筒、扬声器、Telephony TX(猜测应该是通话相关,因为采样率只支持8kHz和16kHz,是典型的通话配置参数),以及它们支持的采样率、声道数和文件格式。
这里可以看出,小米的扬声器输出频率是48kHz,Telephony TX则是8kHz和16kHz。
Config source: /vendor/etc/audio/audio_policy_configuration.xml
- Available output devices:
Device 1:
- id: 2
- tag name: Earpiece
- type: AUDIO_DEVICE_OUT_EARPIECE
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:48000
- channel masks:0x0010
Device 2:
- id: 3
- tag name: Speaker
- type: AUDIO_DEVICE_OUT_SPEAKER
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:48000
- channel masks:0x0003
Device 3:
- id: 10
- tag name: Telephony Tx
- type: AUDIO_DEVICE_OUT_TELEPHONY_TX
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 16000
- channel masks:0x0001, 0x0003
同理,下面还有当前可用的音频输入设备。
- Available input devices:
Device 1:
- id: 18
- tag name: Built-In Mic
- type: AUDIO_DEVICE_IN_BUILTIN_MIC
- address: bottom
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
- channel masks:0x000c, 0x0010, 0x0030
Profile 1:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
- channel masks:0x000c, 0x0010, 0x0030, 0x17000c, 0x80000007, 0x8000000f, 0x8000003f
HW Modules硬件模块
小米手机定义了4个HW Module模块,分别是:Primary、BT A2DP、USB、Remote Submix。
//中间省略了很多
HW Modules dump:
- HW Module 1:
- name: primary
……
- HW Module 2:
- name: a2dp
……
- HW Module 3:
- name: usb
- HW Module 4:
- name: r_submix
这4个HW Module分别有着输入和输出设备,当然A2DP除外,BT A2DP协议只支持音频输出,不支持音频输入。蓝牙的输入输出协议是SCO协议。
Primary Module支持的Outputs
这款小米手机的Primary Module支持10种Outputs(其他安卓手机可能不是这些),分别是:
- Primary output
- raw
- deep buffer
- mmap no irq out
- hifi playback
- compress passthrough
- direct pcm
- compressed offload
- voice tx
- voip rx
- in call music uplink
outputs:
output 0:
- name: primary output
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_24_BIT_PACKED
- sampling rates:48000
- channel masks:0x0003
……
output 1:
- name: raw
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:48000
- channel masks:0x0003
……
output 2:
- name: deep_buffer
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_24_BIT_PACKED
- sampling rates:48000
- channel masks:0x0003
……
output 10:
- name: incall_music_uplink
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 16000, 48000
- channel masks:0x0003
A2DP、USB、Remote Submix支持的Outputs
这三个Module支持的Outputs没有Primary那么复杂,每个Module仅对应一个Output,分别是:
- A2DP:A2DP output;
- USB:usb_accessory output;
- Remote Submix:r_submix output。
HW Module 2:
- name: a2dp
- handle: 18
- version: 2.0
- outputs:
output 0:
- name: a2dp output
- Profiles:
Profile 0:[dynamic format][dynamic channels][dynamic rates]
HW Module 3:
- name: usb
- handle: 26
- version: 2.0
- outputs:
output 0:
- name: usb_accessory output
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:44100
- channel masks:0x0003
HW Module 4:
- name: r_submix
- handle: 34
- version: 2.0
- outputs:
output 0:
- name: r_submix output
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:48000
- channel masks:0x0003
接下来以HW Primary Module的Direct PCM Output为例,来看一下具体的信息。
Profile配置
首先是Profile,也就是支持的配置。
Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
- channel masks:0x0001, 0x0003, 0x000b, 0x0033, 0x0037, 0x003f, 0x013f, 0x063f
Profile 1:
- format: AUDIO_FORMAT_PCM_8_24_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000, 352800, 384000
- channel masks:0x0001, 0x0003, 0x000b, 0x0033, 0x0037, 0x003f, 0x013f, 0x063f
Profile 2:
- format: AUDIO_FORMAT_PCM_24_BIT_PACKED
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000, 352800, 384000
- channel masks:0x0001, 0x0003, 0x000b, 0x0033, 0x0037, 0x003f, 0x013f, 0x063f
Profile 3:
- format: AUDIO_FORMAT_PCM_32_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000, 352800, 384000
- channel masks:0x0001, 0x0003, 0x000b, 0x0033, 0x0037, 0x003f, 0x013f, 0x063f
Direct PCM支持4组配置,对应着不同的音频格式,包括:音频的位深度、采样率信息、声道信息。
Flag输出标志
flags: 0x0001 (AUDIO_OUTPUT_FLAG_DIRECT)
这里仅对应着一个Flag Direct,在其他的Outputs里,还有其他的Flag,包括:
- audio output flag compress offload
- audio output flag non blocking
- audio output flag incall music
- audio output flag fast
- audio output flag primary
- audio output flag deep buffer
- audio output flag raw
- ……
支持的设备
每个Output支持的设备是不同的,Direct PCM支持的设备比较多,有15种;有些Output支持的设备就比较少,比如:incall music就支持一种设备。
Supported devices:
Device 1:
- id: 2
- tag name: Earpiece
- type: AUDIO_DEVICE_OUT_EARPIECE
Device 2:
- id: 3
- tag name: Speaker
- type: AUDIO_DEVICE_OUT_SPEAKER
Device 3:
- tag name: Wired Headset
- type: AUDIO_DEVICE_OUT_WIRED_HEADSET
Device 4:
- tag name: Wired Headphones
- type: AUDIO_DEVICE_OUT_WIRED_HEADPHONE
Device 5:
- tag name: BT SCO
- type: AUDIO_DEVICE_OUT_BLUETOOTH_SCO
Device 6:
- tag name: BT SCO Headset
- type: AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET
Device 7:
- tag name: BT SCO Car Kit
- type: AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT
Device 8:
- tag name: BT A2DP Out
- type: AUDIO_DEVICE_OUT_BLUETOOTH_A2DP
Device 9:
- tag name: BT A2DP Headphones
- type: AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES
Device 10:
- tag name: BT A2DP Speaker
- type: AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER
Device 11:
- tag name: HDMI
- type: AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_HDMI
Device 12:
- tag name: USB Device Out
- type: AUDIO_DEVICE_OUT_USB_DEVICE
Device 13:
- tag name: Line
- type: AUDIO_DEVICE_OUT_LINE
Device 14:
- tag name: Proxy
- type: AUDIO_DEVICE_OUT_PROXY
Device 15:
- tag name: USB Headset Out
- type: AUDIO_DEVICE_OUT_USB_HEADSET
//incall music uplink就支持1种设备
output 10:
- name: incall_music_uplink
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 16000, 48000
- channel masks:0x0003
- flags: 0x80000000 (AUDIO_OUTPUT_FLAG_INCALL_MUSIC)
- Supported devices:
Device 1:
- id: 10
- tag name: Telephony Tx
- type: AUDIO_DEVICE_OUT_TELEPHONY_TX
Audio输入设备
HW Primary Module的音频输入设备
与分析输出设备类似,从log中可以看出,HW Primary Module支持的inputs有:
- primary input
- surround sound
- VoIP tx
- usb surround sound
- record 24
- voice rx
- ……
Input的详细信息
这里能看到Input的Profile配置、支持的文件格式、支持的采样率、支持的麦克风设备等。
input 1:
- name: surround_sound
- Profiles:
Profile 0:
- format: AUDIO_FORMAT_PCM_16_BIT
- sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
- channel masks:0x000c, 0x0010, 0x0030, 0x17000c, 0x80000007, 0x8000000f, 0x8000003f
- flags: 0x0000 (AUDIO_INPUT_FLAG_NONE)
- Supported devices:
Device 1:
- id: 18
- tag name: Built-In Mic
- type: AUDIO_DEVICE_IN_BUILTIN_MIC
- address: bottom
Device 2:
- id: 19
- tag name: Built-In Back Mic
- type: AUDIO_DEVICE_IN_BACK_MIC
- address: back
音频路由Audio Route
Audio Route信息有三组内容,分别是:Type、Sink、Source。
Source:对应着output或者input;
Sink:对应着输出或者输入设备;
Type:所有的都是Mix(这个不太懂,回头再研究一下)。
Audio Routes (25):
Route 1:
- Type: Mix
- Sink: Earpiece
- Sources:
primary output
raw
deep_buffer
direct_pcm
compressed_offload
voip_rx
mmap_no_irq_out
……
Route 5:
- Type: Mix
- Sink: Line
- Sources:
primary output
raw
deep_buffer
direct_pcm
compressed_offload
voip_rx
mmap_no_irq_out
……
分析到这里,AudioPolicy的音频输出结构应该能稍微理出一些头绪了。
下一篇: 关于实时消息推送系统的架构之浅见