Platform: Rk3329
OS: Android 10.0
Kernel: v4.19.111
问题现象
在开发完音频驱动 rk817 后,上层APP调用通道切换接口 mAudioManager.setSpeakerphoneOn(), 发现无效,
一直未切换成功,通道始终在麦克风上。
问题排查
开始先从 setSpeakerphoneOn 入手,跟踪
frameworks/base/services/core/java/com/android/server/audio/AudioService.java
frameworks/base/services/core/java/com/android/server/audio/AudioDeviceBroker.java
日志打印 framework 层是ok的,向jni传递通道数据正常。
system_process D/cck: on=true eventSource=setSpeakerphoneOn(true) from u/pid:1000/2841
system_process D/cck: mForcedUseForComm=1
system_process I/cck: time=847700
system_process D/cck: time=847700
system_process E/cck: MSG_IIL_SET_FORCE_USE arr
system_process D/cck: useCase=0 config=1
system_process D/cck: eventSource=setSpeakerphoneOn(true) from u/pid:1000/2841
system_process I/cck: time=852780
system_process D/cck: time=852780
system_process D/cck: on=false eventSource=setSpeakerphoneOn(false) from u/pid:1000/2841
system_process D/cck: mForcedUseForComm=0
system_process I/cck: time=852782
system_process D/cck: time=852782
system_process E/cck: MSG_IIL_SET_FORCE_USE arr
system_process D/cck: useCase=0 config=0
system_process D/cck: eventSource=setSpeakerphoneOn(false) from u/pid:1000/2841
向下寻找到hal层,hardware/rockchip/audio/tinyalsa_hal/
日志打印如下
听筒
start_output_stream:873 out = 0xf75a8000,device = 0x2,outputs[OUTPUT_HDMI_MULTI] = 0x0
AudioHardwareTiny: out->Device : 0x2
alsa_route: route_pcm_card_open(card 0, route 0)
D/alsa_route: route_info->sound_card 0, route_info->devices 0
D/alsa_route: route_pcm_card_open() INCALL_OFF_ROUTE 26
D/alsa_route: route_set_controls() set route 0
D/AudioHardwareTiny: start_output_stream:968, out = 0xf75a8000
D/alsa_route: route_set_controls() set route 24
免提
start_output_stream:873 out = 0xf75a8000,device = 0x2,outputs[OUTPUT_HDMI_MULTI] = 0x0
/AudioHardwareTiny: out->Device : 0x2
alsa_route: route_pcm_card_open(card 0, route 24)
D/alsa_route: route_info->sound_card 0, route_info->devices 0
D/alsa_route: route_pcm_card_open() INCALL_OFF_ROUTE 26
D/alsa_route: route_set_controls() set route 24
D/AudioHardwareTiny: start_output_stream:968, out = 0xf75a8000
耳机
start_output_stream:873 out = 0xf75a8000,device = 0x4,outputs[OUTPUT_HDMI_MULTI] = 0x0
AudioHardwareTiny: out->Device : 0x4
alsa_route: route_pcm_card_open(card 0, route 14)
D/alsa_route: route_info->sound_card 0, route_info->devices 0
D/alsa_route: route_pcm_card_open() INCALL_OFF_ROUTE 26
D/alsa_route: route_set_controls() set route 14
D/AudioHardwareTiny: start_output_stream:968, out = 0xf75a8000
发现耳机 device 类型是正确的,听筒和免提切换时 device 并未改变,alsa route 打印 只找到 set route 0 和 route 24
AUDIO_DEVICE_OUT_EARPIECE = 0x1, // 听筒
AUDIO_DEVICE_OUT_SPEAKER = 0x2, // 扬声器
AUDIO_DEVICE_OUT_WIRED_HEADSET = 0x4, // 线控耳机
据此推测节点配置不对,pull 出设备中 vendor/etc/audio_policy_configuration.xml 查看,其中并
不包含 AUDIO_DEVICE_OUT_EARPIECE 节点,破案了
解决方案
device\rockchip\common\audio_policy_configuration.xml
<modules> <module name="primary" halVersion="2.0"> <attachedDevices> + <item>Earpiece</item> <item>Speaker</item> <devicePorts> + <devicePort tagName="Earpiece" type="AUDIO_DEVICE_OUT_EARPIECE" role="sink"> + </devicePort> <devicePort tagName="Speaker" type="AUDIO_DEVICE_OUT_SPEAKER" role="sink"> </devicePort> <routes> + <route type="mix" sink="Earpiece" + sources="primary output"/> <route type="mix" sink="Speaker" sources="primary output"/>
hardware\libhardware_legacy\audio\audio_policy.conf
# as well as the output device selected by default. # Devices are designated by a string that corresponds to the enum in audio.h global_configuration { - attached_output_devices AUDIO_DEVICE_OUT_SPEAKER + attached_output_devices AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_SPEAKER default_output_device AUDIO_DEVICE_OUT_SPEAKER attached_input_devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_REMOTE_SUBMIX }
hardware\rockchip\audio\tinyalsa_hal\audio_hw.c
unsigned getOutputRouteFromDevice(uint32_t device) { /*if (mMode != AudioSystem::MODE_RINGTONE && mMode != AudioSystem::MODE_NORMAL) return PLAYBACK_OFF_ROUTE; */ ALOGD("%s:%d, device = 0x%x",__FUNCTION__,__LINE__, device); switch (device) { + case AUDIO_DEVICE_OUT_EARPIECE: + return EARPIECE_NORMAL_ROUTE; case AUDIO_DEVICE_OUT_SPEAKER: return SPEAKER_NORMAL_ROUTE; static int start_output_stream(struct stream_out *out) { struct audio_device *adev = out->dev; int ret = 0; int card = (int)SND_OUT_SOUND_CARD_UNKNOWN; int device = 0; if (out->device & ( + AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | AUDIO_DEVICE_OUT_ALL_SCO)) { card = adev->dev_out[SND_OUT_SOUND_CARD_SPEAKER].card;