在上一篇中Android Framework源码解读 - Audio - SoundTrigger(1) - AIDL,提到startRecognition如何到达HAL层(本例中使用的唤醒词检测驱动来自厂商,HAL层的代码实现也是厂商提供),请看下图:
相关代码位于./frameworks/av/soundtrigger/ISoundTrigger.cpp 、./frameworks/av/services/soundtrigger/SoundTriggerHwService.cpp
共4步:① BpSoundTrigger::transact(START_RECOGNITION)到达 ② BnSoundTrigger::onTransaction(START_RECOGNITION),再到具体实现类的③ ModuleClient::startRecognition,再由④ Module::startRecognition -》mHalInterface->startRecognition走到Hal层的SoundTriggerHalHidl::startRecognition,代码位于./frameworks/av/services/soundtrigger/SoundTriggerHalHidl.cpp
这里调用了sp<ISoundTriggerHw> soundtrigger = getService(); 从而得到了BpSoundTriggerHw对象,其接口定义在:./hardware/interfaces/soundtrigger/2.0/ISoundTriggerHw.hal 编译时会由工具生成中间文件:SoundTriggerHwAll.cpp。需要注意的是getService()的返回值被做了一个castFrom转换,最终返回的是 BpHwSoundTriggerHw对象,然后通过BpHwSoundTriggerHw::startRecognition -> _hidl_startRecognition方法调用::IInterface::asBinder(_hidl_this)->transact(5 /* startRecognition */, _hidl_data, &_hid