带着以下几个问题,我们着手分析:Android系统在播放音乐的时候,如果插入耳机,系统会发生什么变化?会产生哪些逻辑?音频是如何从扬声器切换到耳机的?插入3.5mm的耳机孔和usb耳机又有什么不同?
1. 插拔检测
1.1 3.5mm耳机
3.5mm耳机插拔检测的逻辑在WiredAccessoryManager.java中,它由SystemServer启动。里面实现了两种检测机制供厂商选择。
- 通过input event, 插拔时会产生一个keycode, 由InputFlinger传上来。使用getevent命令可捕获查看。
- 通过UEvent监听/sys/class/switch/xxx/state节点变化。
不管是哪种方式,最终都会走到相同逻辑处理。
WiredAccessoryManager.java
- updateStateLocked
- updateLocked
- setDevicesState
- setDeviceStateLocked
1.2 USB耳机
USB耳机插拔检测逻辑在UsbHostManager.java中
WiredAccessoryManager.java
- monitorUsbHostBus
- usbDeviceAdded
UsbAlsaManager.java
- usbDeviceAdded
- selectAlsaDevice
UsbAlsaDevice.java
- start
- updateWiredDeviceConnectionState
2. 通知Audio系统有新设备插入
通过AudioManager调用setWiredDeviceConnectionState
mAudioManager.setWiredDeviceConnectionState(outDevice, state, "", headsetName);
第一个参数代表设备,这里是AudioManager.DEVICE_OUT_WIRED_HEADSET
第二个参数代表插拔状态,插入是1,拔出是0。
第三个参数是设备address。
第四个是耳机名称。
然后再经过一系列调用,最终会走到AudioPolicyManager.cpp
AudioManager.java->AudioService.java->AudioDeviceBroker.java->AudioDeviceInventory.java
- setWiredDeviceConnectionState
- postSetWiredDeviceConnectionState
- onSetWiredDeviceConnectionState
- handleDeviceConnection
- onSetWiredDeviceConnectionState
- postSetWiredDeviceConnectionState
AudioSystem.cpp->AudioPolicyInterfaceImpl.cpp->AudioPolicyManager.cpp
- setDeviceConnectionState
- setDeviceConnectionStateInt
- checkOutputsForDevice
- openOutputWithProfileAndDevice
- checkForDeviceAndOutputChanges
- checkOutputForAllStrategies
- checkOutputForAttributes
- connectAudioSource
- createAudioPatchInternal
- connectAudioSource
- checkOutputsForDevice
checkOutputsForDevice:根据音频设备和输入输出类型(在这里是耳机和输出)查询支持这种设备的流,包括目前已经打开的和未打开但配置文件中配置的流。
openOutputWithProfileAndDevice:打开之前未打开但配置文件中查询到的output流,进一步调用SwAudioOutputDescriptor的open函数,最后会调用到AudioFlinger的openOutput。如果module没有被加载,会打开该module并创建playbackThread, 如果已经打开了,会查找对应的playBackThread,如果没找到也会创建。
checkForDeviceAndOutputChanges:判断是否需要迁移,如果srcOutputs、dstOutputs不相同, 表示需要迁移。如果迁移的话,还需要把对应的Track设置为invalidate状态,AudioTrack.cpp在写AudioTrack时发现它是invalidate状态, 就会重新创建新的Track。
createAudioPatchInternal:创建AudioPatch,将原来的speaker路由到新的headset。
3. 3.5mm耳机与usb耳机的区别
3.5mm耳机:这种设备一般是配置在primary module中,与内置扬声器属于同一个module,因此并不会重新打开module,playbackThread也会延用之前的thread。
在高通的设备上,开机部分以及开机后插入3.5mm耳机:
//开机
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x6
1212 1212 I AudioFlinger: openOutput_l() created mixer output: ID 13 thread 0x7253ce99a0
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x4
1212 1214 E audio_hw_primary: adev_open_output_stream: Primary output is already opened
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x8
1212 1212 I AudioFlinger: openOutput_l() created mixer output: ID 29 thread 0x7253c0a9a0
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x4001
1212 1212 I AudioFlinger: openOutput_l() created mmap playback thread: ID 37 thread 0x71388d6010
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 8000, Format 0x000003, Channels 0x1, flags 0x1
1212 1212 I AudioFlinger: openOutput_l() created direct output: ID 45 thread 0x71688d5010
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 8000, Format 0x1000000, Channels 0x1, flags 0x31
1212 1212 I AudioFlinger: openOutput_l() created offload output: ID 53 thread 0x71688d5010
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_TELEPHONY_TX, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0
1212 1212 I AudioFlinger: openOutput_l() created mixer output: ID 61 thread 0x72535ba9a0
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_SPEAKER, @:, SamplingRate 8000, Format 0x000001, Channels 0x1, flags 0x8001
1212 1212 I AudioFlinger: openOutput_l() created direct output: ID 69 thread 0x71688d5010
1212 1212 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_TELEPHONY_TX, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x10000
1212 1212 I AudioFlinger: openOutput_l() created mixer output: ID 77 thread 0x72502309a0
//插入耳机
1212 5103 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_WIRED_HEADPHONE, @:, SamplingRate 8000, Format 0x000003, Channels 0x1, flags 0x1
1212 5103 I AudioFlinger: openOutput_l() created direct output: ID 85 thread 0x71688d5010
1212 5103 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_WIRED_HEADPHONE, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x4
1145 1214 E audio_hw_primary: adev_open_output_stream: Primary output is already opened
1212 5103 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_WIRED_HEADPHONE, @:, SamplingRate 8000, Format 0x000001, Channels 0x1, flags 0x8001
1212 5103 I AudioFlinger: openOutput_l() created direct output: ID 101 thread 0x71688de030
1212 5103 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_WIRED_HEADPHONE, @:, SamplingRate 8000, Format 0x1000000, Channels 0x1, flags 0x31
1212 5103 I AudioFlinger: openOutput_l() created offload output: ID 109 thread 0x71688e2840
1212 5103 I AudioFlinger: openOutput() this 0x70c88cc130, module 10 Device AUDIO_DEVICE_OUT_WIRED_HEADPHONE, @:, SamplingRate 48000, Format 0x000001, Channels 0x3, flags 0x4001
1212 5103 I AudioFlinger: openOutput_l() created mmap playback thread: ID 117 thread 0x71388dbb30
可以看到插入耳机时并没有创建MixerThread。那么AudioTrack在播放音乐时必然会延用了之前创建的MixerThread。
USB耳机:usb耳机配置在另外一个module中,因此会重新打开这个module,并查找配置中对应的输入输出流。