- 浏览: 432215 次
文章分类
最新评论
ANDROID音频系统散记之四:4.0音频系统HAL初探
一、代码模块位置
1、AudioFlinger
[plain]
view plaincopy
- frameworks/base/services/audioflinger/
- +--Android.mk
- +--AudioBufferProvider.h
- +--AudioFlinger.cpp
- +--AudioFlinger.h
- +--AudioMixer.cpp
- +--AudioMixer.h
- +--AudioPolicyService.cpp
- +--AudioPolicyService.h
- +--AudioResampler.cpp
- +--AudioResamplerCubic.cpp
- +--AudioResamplerCubic.h
- +--AudioResampler.h
- +--AudioResamplerSinc.cpp
- +--AudioResamplerSinc.h
两种Audio Hardware HAL接口定义:
1/ legacy:hardware/libhardware_legacy/include/hardware_legacy/AudioHardwareInterface.h
2/ 非legacy:hardware/libhardware/include/hardware/audio.h
2、audio_hw
[plain]
view plaincopy
- hardware/libhardware_legacy/audio/
- +--A2dpAudioInterface.cpp
- +--A2dpAudioInterface.h
- +--Android.mk
- +--AudioDumpInterface.cpp
- +--AudioDumpInterface.h
- +--AudioHardwareGeneric.cpp
- +--AudioHardwareGeneric.h
- +--AudioHardwareInterface.cpp
- +--AudioHardwareStub.cpp
- +--AudioHardwareStub.h
- +--audio_hw_hal.cpp
- +--AudioPolicyCompatClient.cpp
- +--AudioPolicyCompatClient.h
- +--audio_policy_hal.cpp
- +--AudioPolicyManagerBase.cpp
- +--AudioPolicyManagerDefault.cpp
- +--AudioPolicyManagerDefault.h
事实上legacy也要封装成非legacy中的audio.h,确切的说需要一个联系legacy interface和not legacy interface的中间层,这里的audio_hw_hal.cpp就充当这样的一个角色了。因此,我们其实也可以把2.3之前的alsa_sound这一套东西也搬过来。
[plain]
view plaincopy
- hardware/libhardware/modules/audio/
- +--Android.mk
- +--audio_hw.c
- +--audio_policy.c
[plain]
view plaincopy
- device/samsung/tuna/audio/
- +--Android.mk
- +--audio_hw.c
- +--ril_interface.c
- +--ril_interface.h
3、tinyalsa
[plain]
view plaincopy
- external/tinyalsa/
- +--Android.mk
- +--include
- |+--tinyalsa
- |+--asoundlib.h
- +--mixer.c##类alsa-lib的control,作用音频部件开关、音量调节等
- +--pcm.c##类alsa-lib的pcm,作用音频pcm数据回放录制
- +--README
- +--tinycap.c##类alsa_arecord
- +--tinymix.c##类alsa_amixer
- +--tinyplay.c##类alsa_aplay
这其实是历史的必然了,alsa-lib太过复杂繁琐了,我看得也很不爽;更重要的商业上面的考虑,必须移除被GNU GPL授权证所约束的部份,alsa-lib并不是个例。
注意:上面的hardware/libhardware_legacy/audio/、hardware/libhardware/modules/audio/、device/samsung/tuna/audio/是同层的。之一是legacy audio,用于兼容2.2时代的alsa_sound;之二是stub audio接口;之三是Samsung Tuna的音频抽象层实现。调用层次:AudioFlinger -> audio_hw -> tinyalsa。
注意:上面的hardware/libhardware_legacy/audio/、hardware/libhardware/modules/audio/、device/samsung/tuna/audio/是同层的。之一是legacy audio,用于兼容2.2时代的alsa_sound;之二是stub audio接口;之三是Samsung Tuna的音频抽象层实现。调用层次:AudioFlinger -> audio_hw -> tinyalsa。
二、Audio Hardware HAL加载
1、AudioFlinger
[cpp]
view plaincopy
- //加载audiohardwarehal
- staticintload_audio_interface(constchar*if_name,consthw_module_t**mod,
- audio_hw_device_t**dev)
- {
- intrc;
- //根据classid和if_name找到指定的动态库并加载,这里加载的是音频动态库,如libaudio.primary.tuna.so
- rc=hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID,if_name,mod);
- if(rc)
- gotoout;
- //加载好的动态库模块必有个open方法,调用open方法打开音频设备模块
- rc=audio_hw_device_open(*mod,dev);
- LOGE_IF(rc,"couldn'topenaudiohwdevicein%s.%s(%s)",
- AUDIO_HARDWARE_MODULE_ID,if_name,strerror(-rc));
- if(rc)
- gotoout;
- return0;
- out:
- *mod=NULL;
- *dev=NULL;
- returnrc;
- }
- //音频设备接口,hw_get_module_by_class需要根据这些字符串找到相关的音频模块库
- staticconstchar*audio_interfaces[]={
- "primary",//主音频设备,一般为本机codec
- "a2dp",//a2dp设备,蓝牙高保真音频
- "usb",//usb-audio设备,这个东东我2.3就考虑要实现了,现在终于支持了
- };
- #defineARRAY_SIZE(x)(sizeof((x))/sizeof(((x)[0])))
- //----------------------------------------------------------------------------
- AudioFlinger::AudioFlinger()
- :BnAudioFlinger(),
- mPrimaryHardwareDev(0),mMasterVolume(1.0f),mMasterMute(false),mNextUniqueId(1),
- mBtNrecIsOff(false)
- {
- }
- voidAudioFlinger::onFirstRef()
- {
- intrc=0;
- Mutex::Autolock_l(mLock);
- /*TODO:moveallthisworkintoanInit()function*/
- mHardwareStatus=AUDIO_HW_IDLE;
- //打开audio_interfaces数组定义的所有音频设备
- for(size_ti=0;i<ARRAY_SIZE(audio_interfaces);i++){
- consthw_module_t*mod;
- audio_hw_device_t*dev;
- rc=load_audio_interface(audio_interfaces[i],&mod,&dev);
- if(rc)
- continue;
- LOGI("Loaded%saudiointerfacefrom%s(%s)",audio_interfaces[i],
- mod->name,mod->id);
- mAudioHwDevs.push(dev);//mAudioHwDevs是一个Vector,存储已打开的audiohwdevices
- if(!mPrimaryHardwareDev){
- mPrimaryHardwareDev=dev;
- LOGI("Using'%s'(%s.%s)astheprimaryaudiointerface",
- mod->name,mod->id,audio_interfaces[i]);
- }
- }
- mHardwareStatus=AUDIO_HW_INIT;
- if(!mPrimaryHardwareDev||mAudioHwDevs.size()==0){
- LOGE("Primaryaudiointerfacenotfound");
- return;
- }
- //对audiohwdevices进行一些初始化,如mode、mastervolume的设置
- for(size_ti=0;i<mAudioHwDevs.size();i++){
- audio_hw_device_t*dev=mAudioHwDevs[i];
- mHardwareStatus=AUDIO_HW_INIT;
- rc=dev->init_check(dev);
- if(rc==0){
- AutoMutexlock(mHardwareLock);
- mMode=AUDIO_MODE_NORMAL;
- mHardwareStatus=AUDIO_HW_SET_MODE;
- dev->set_mode(dev,mMode);
- mHardwareStatus=AUDIO_HW_SET_MASTER_VOLUME;
- dev->set_master_volume(dev,1.0f);
- mHardwareStatus=AUDIO_HW_IDLE;
- }
- }
- }
以上对AudioFlinger进行的分析,主要是通过hw_get_module_by_class()找到模块接口名字if_name相匹配的模块库,加载,然后audio_hw_device_open()调用模块的open方法,完成音频设备模块的初始化。
留意AudioFlinger的构造函数只有简单的私有变量的初始化操作了,把音频设备初始化放到onFirstRef(),Android终于改进了这一点,好的设计根本不应该把可能会失败的操作放到构造函数中。onFirstRef是RefBase类的一个虚函数,在构造sp的时候就会被调用。因此,在构造sp<AudioFlinger>的时候就会触发onFirstRef方法,从而完成音频设备模块初始化。
2、hw_get_module_by_class
我们接下来看看hw_get_module_by_class,实现在hardware/libhardware/ hardware.c中,它作用加载指定名字的模块库(.so文件),这个应该是用于加载所有硬件设备相关的库文件,并不只是音频设备。
[cpp]
view plaincopy
- inthw_get_module_by_class(constchar*class_id,constchar*inst,
- conststructhw_module_t**module)
- {
- intstatus;
- inti;
- conststructhw_module_t*hmi=NULL;
- charprop[PATH_MAX];
- charpath[PATH_MAX];
- charname[PATH_MAX];
- if(inst)
- snprintf(name,PATH_MAX,"%s.%s",class_id,inst);
- else
- strlcpy(name,class_id,PATH_MAX);
- //这里我们以音频库为例,AudioFlinger调用到这个函数时,
- //class_id=AUDIO_HARDWARE_MODULE_ID="audio",inst="primary"(或"a2dp"或"usb")
- //那么此时name="audio.primary"
- /*
- *Herewerelyonthefactthatcallingdlopenmultipletimeson
- *thesame.sowillsimplyincrementarefcount(andnotload
- *anewcopyofthelibrary).
- *Wealsoassumethatdlopen()isthread-safe.
- */
- /*Loopthroughtheconfigurationvariantslookingforamodule*/
- for(i=0;i<HAL_VARIANT_KEYS_COUNT+1;i++){
- if(i<HAL_VARIANT_KEYS_COUNT){
- //通过property_get找到厂家标记如"ro.product.board=tuna",这时prop="tuna"
- if(property_get(variant_keys[i],prop,NULL)==0){
- continue;
- }
- snprintf(path,sizeof(path),"%s/%s.%s.so",
- HAL_LIBRARY_PATH2,name,prop);//#defineHAL_LIBRARY_PATH2"/vendor/lib/hw"
- if(access(path,R_OK)==0)break;
- snprintf(path,sizeof(path),"%s/%s.%s.so",
- HAL_LIBRARY_PATH1,name,prop);//#defineHAL_LIBRARY_PATH1"/system/lib/hw"
- if(access(path,R_OK)==0)break;
- }else{
- snprintf(path,sizeof(path),"%s/%s.default.so",//如没有指定的库文件,则加载default.so,即stub-device
- HAL_LIBRARY_PATH1,name);
- if(access(path,R_OK)==0)break;
- }
- }
- //到这里,完成一个模块库的完整路径名称,如path="/system/lib/hw/audio.primary.tuna.so"
- //如何生成audio.primary.tuna.so?请看相关的Android.mk文件,其中有定义LOCAL_MODULE:=audio.primary.tuna
- status=-ENOENT;
- if(i<HAL_VARIANT_KEYS_COUNT+1){
- /*loadthemodule,ifthisfails,we'redoomed,andweshouldnottry
- *toloadadifferentvariant.*/
- status=load(class_id,path,module);//加载模块库
- }
- returnstatus;
- }
[cpp]
view plaincopy
- /**
- *EveryhardwaremodulemusthaveadatastructurenamedHAL_MODULE_INFO_SYM
- *andthefieldsofthisdatastructuremustbeginwithhw_module_t
- *followedbymodulespecificinformation.
- */
- typedefstructhw_module_t{
- /**tagmustbeinitializedtoHARDWARE_MODULE_TAG*/
- uint32_ttag;
- /**majorversionnumberforthemodule*/
- uint16_tversion_major;
- /**minorversionnumberofthemodule*/
- uint16_tversion_minor;
- /**Identifierofmodule*/
- constchar*id;
- /**Nameofthismodule*/
- constchar*name;
- /**Author/owner/implementorofthemodule*/
- constchar*author;
- /**Modulesmethods*/
- structhw_module_methods_t*methods;
- /**module'sdso*/
- void*dso;
- /**paddingto128bytes,reservedforfutureuse*/
- uint32_treserved[32-7];
- }hw_module_t;
- typedefstructhw_module_methods_t{
- /**Openaspecificdevice*/
- int(*open)(conststructhw_module_t*module,constchar*id,
- structhw_device_t**device);
- }hw_module_methods_t;
例如,在audio_hw.c中,它是这样定义的:
[cpp]
view plaincopy
- staticstructhw_module_methods_thal_module_methods={
- .open=adev_open,
- };
- structaudio_moduleHAL_MODULE_INFO_SYM={
- .common={
- .tag=HARDWARE_MODULE_TAG,
- .version_major=1,
- .version_minor=0,
- .id=AUDIO_HARDWARE_MODULE_ID,
- .name="TunaaudioHWHAL",
- .author="TheAndroidOpenSourceProject",
- .methods=&hal_module_methods,
- },
- };
3、audio_hw
好了,经过一番周折,又dlopen又dlsym的,终于进入我们的audio_hw。这部分没什么好说的,按照hardware/libhardware/include/hardware/audio.h定义的接口实现就行了。这些接口全扔到一个结构体里面的,这样做的好处是:不必用大量的dlsym来获取各个接口函数的地址,只需找到这个结构体即可,从易用性和可扩充性来说,都是首选方式。
接口定义如下:
[cpp]
view plaincopy
- structaudio_hw_device{
- structhw_device_tcommon;
- /**
- *usedbyaudioflingertoenumeratewhatdevicesaresupportedby
- *eachaudio_hw_deviceimplementation.
- *
- *Returnvalueisabitmaskof1ormorevaluesofaudio_devices_t
- */
- uint32_t(*get_supported_devices)(conststructaudio_hw_device*dev);
- /**
- *checktoseeiftheaudiohardwareinterfacehasbeeninitialized.
- *returns0onsuccess,-ENODEVonfailure.
- */
- int(*init_check)(conststructaudio_hw_device*dev);
- /**settheaudiovolumeofavoicecall.Rangeisbetween0.0and1.0*/
- int(*set_voice_volume)(structaudio_hw_device*dev,floatvolume);
- /**
- *settheaudiovolumeforallaudioactivitiesotherthanvoicecall.
- *Rangebetween0.0and1.0.Ifanyvalueotherthan0isreturned,
- *thesoftwaremixerwillemulatethiscapability.
- */
- int(*set_master_volume)(structaudio_hw_device*dev,floatvolume);
- /**
- *setModeiscalledwhentheaudiomodechanges.AUDIO_MODE_NORMALmode
- *isforstandardaudioplayback,AUDIO_MODE_RINGTONEwhenaringtoneis
- *playing,andAUDIO_MODE_IN_CALLwhenacallisinprogress.
- */
- int(*set_mode)(structaudio_hw_device*dev,intmode);
- /*micmute*/
- int(*set_mic_mute)(structaudio_hw_device*dev,boolstate);
- int(*get_mic_mute)(conststructaudio_hw_device*dev,bool*state);
- /*set/getglobalaudioparameters*/
- int(*set_parameters)(structaudio_hw_device*dev,constchar*kv_pairs);
- /*
- *Returnsapointertoaheapallocatedstring.Thecallerisresponsible
- *forfreeingthememoryforit.
- */
- char*(*get_parameters)(conststructaudio_hw_device*dev,
- constchar*keys);
- /*Returnsaudioinputbuffersizeaccordingtoparameterspassedor
- *0ifoneoftheparametersisnotsupported
- */
- size_t(*get_input_buffer_size)(conststructaudio_hw_device*dev,
- uint32_tsample_rate,intformat,
- intchannel_count);
- /**Thismethodcreatesandopenstheaudiohardwareoutputstream*/
- int(*open_output_stream)(structaudio_hw_device*dev,uint32_tdevices,
- int*format,uint32_t*channels,
- uint32_t*sample_rate,
- structaudio_stream_out**out);
- void(*close_output_stream)(structaudio_hw_device*dev,
- structaudio_stream_out*out);
- /**Thismethodcreatesandopenstheaudiohardwareinputstream*/
- int(*open_input_stream)(structaudio_hw_device*dev,uint32_tdevices,
- int*format,uint32_t*channels,
- uint32_t*sample_rate,
- audio_in_acoustics_tacoustics,
- structaudio_stream_in**stream_in);
- void(*close_input_stream)(structaudio_hw_device*dev,
- structaudio_stream_in*in);
- /**Thismethoddumpsthestateoftheaudiohardware*/
- int(*dump)(conststructaudio_hw_device*dev,intfd);
- };
- typedefstructaudio_hw_deviceaudio_hw_device_t;
三、Audio Hardware HAL的legacy实现
之前提到两种Audio Hardware HAL接口定义:
1/ legacy:hardware/libhardware_legacy/include/hardware_legacy/AudioHardwareInterface.h
2/ 非legacy:hardware/libhardware/include/hardware/audio.h
前者是2.3及之前的音频设备接口定义,后者是4.0的接口定义。
1/ legacy:hardware/libhardware_legacy/include/hardware_legacy/AudioHardwareInterface.h
2/ 非legacy:hardware/libhardware/include/hardware/audio.h
前者是2.3及之前的音频设备接口定义,后者是4.0的接口定义。
为了兼容以前的设计,4.0实现一个中间层:hardware/libhardware_legacy/audio/audio_hw_hal.cpp,结构与其他的audio_hw.c大同小异,差别在于open方法:
[cpp]
view plaincopy
- staticintlegacy_adev_open(consthw_module_t*module,constchar*name,
- hw_device_t**device)
- {
- ......
- ladev->hwif=createAudioHardware();
- if(!ladev->hwif){
- ret=-EIO;
- gotoerr_create_audio_hw;
- }
- ......
- }
因此老一套的alsa-lib、alsa-utils和alsa_sound也可以照搬过来,这里的文件被编译成静态库的,因此你需要修改alsa_sound里面的Android.mk文件,链接这个静态库。还有alsa_sound的命名空间原来是“android”,现在需要改成“android_audio_legacy”。
四、a2dp Audio HAL的实现
external/Bluetooth/bluez/audio/android_audio_hw.c
大致与上面提到的audio_hw.c类似,因为都是基于audio.h定义的接口来实现的。
如果需要编译这个库,须在BoardConfig.mk里定义:
BOARD_HAVE_BLUETOOTH := true
开始还提到现在支持3种audio设备了,分别是primary、a2dp和usb。目前剩下usb audio hal我没有找到,不知是否需要自己去实现?其实alsa-driver都支持大部分的usb-audio设备了,因此上层也可调用tinyalsa的接口,就像samsung tuna的audio_hw.c那样。
五、音质改进???
--to be continued…
相关推荐
北京散记(四)作文.doc
湘行散记 名著导读.doc
mtk散记1到3.docmtk散记1到3.doc mtk散记1到3.docmtk散记1到3.doc mtk散记1到3.docmtk散记1到3.doc
形散而神聚。因为是散文,所以没有框架结构的限制,作者写起来轻松,读者读起来方便,数千字成一文,每篇文章都是一个独立主题的探讨,又都紧密围绕ERP项目实施这一主题。
散记 关于 演示Android应用程序,用于。 谷歌认证 资源 。 用法 在preferences.xml将此值替换为自己的类似物: [APP_ID] Facebook身份验证 资源 用法 在preferences.xml将此值替换为自己的类似物: [APP_ID] ...
鄂教版七年级上语文:第17课《牧鹅散记》同步练习精选.doc
OBLOG 浮生散记
湘行散记名著导读.doc
北京散记(一)作文.doc
北京散记(十二)作文.doc
北京散记(十)作文.doc
北京散记(三)作文.doc
北京散记(六)作文.doc
北京散记(八)作文.doc
Maven游乐场一般信息什么时候怎样散记推荐链接:
巴隆国际猎场散记[规整].pdf
《湘行散记》读后感.doc
【北戴河散记写景美文】-北戴河美文.docx