标签:
mscommon.c:
void ms_init(){
….
/* register builtin MSFilter‘s */ //注册所有内置filter
for (i=0;ms_filter_descs[i]!=NULL;i++){
ms_filter_register(ms_filter_descs[i]);
}
。。。
********************************
alldesc.h
其中ms_filter_descs如下:
数组中filter的desc没有顺序之分
MSFilterDesc * ms_filter_descs[]={
35 &ms_alaw_dec_desc,
36 &ms_alaw_enc_desc,
37 &ms_ulaw_dec_desc,
38 &ms_ulaw_enc_desc,
39 &ms_file_player_desc,
40 &ms_rtp_send_desc,
41 &ms_rtp_recv_desc,
42 &ms_dtmf_gen_desc,
43 &ms_file_rec_desc,
44 &ms_speex_dec_desc,
45 &ms_speex_enc_desc,
46 &ms_gsm_dec_desc,
47 &ms_gsm_enc_desc,
48 &ms_speex_ec_desc,
49 &ms_tee_desc,
50 &ms_conf_desc,
51 //&alsa_write_desc,
52 //&alsa_read_desc,
53 //&oss_read_desc,
54 //&oss_write_desc,
55 //&ms_arts_read_desc,
56 //&ms_arts_write_desc,
57 //&ms_v4l_desc,
58 //&ms_sdl_out_desc,
59 //&ms_h263_enc_desc,
60 //&ms_h263_dec_desc,
61 &ms_join_desc,
62 #ifndef DISABLE_RESAMPLE
63 &ms_resample_desc,
64 #endif
65 &ms_volume_desc,
66 &ms_ice_desc,
67 &ms_equalizer_desc,
68 NULL
69 };
******************
在ms_filter_register(ms_filter_descs[i]);后有个非常重要的方法:
ms_message("Registering all soundcard handlers");
cm=ms_snd_card_manager_get(); //获取声卡管理器 MSSndCardManager
for (i=0;ms_snd_card_descs[i]!=NULL;i++){
ms_snd_card_manager_register_desc(cm,ms_snd_card_descs[i]); //通过声卡描述数组,来注册一系列的声卡
}
*****************下面所有的分析就是针对ms_snd_card_manager_register_desc方法*********************
注册声卡,通过声卡描述来注册声卡到声卡管理器 MSSndCardManager
下面来看看ms_snd_card_manager_register_desc方法(mssndcard.c 113行)
void ms_snd_card_manager_register_desc(MSSndCardManager *m, MSSndCardDesc *desc){
m->descs=ms_list_append(m->descs,desc);
card_detect(m,desc);
}
继续->
static void card_detect(MSSndCardManager *m, MSSndCardDesc *desc){
if (desc->detect!=NULL)
desc->detect(m);
}
detect是结构体_MSSndCardDesc的一个指针函数。请看->mssndcard.h 101行:
struct _MSSndCardDesc{
const char *driver_type;
MSSndCardDetectFunc detect;
MSSndCardInitFunc init;
MSSndCardSetLevelFunc set_level;
MSSndCardGetLevelFunc get_level;
MSSndCardSetCaptureFunc set_capture;
MSSndCardSetControlFunc set_control;
MSSndCardGetControlFunc get_control;
MSSndCardCreateReaderFunc create_reader;
MSSndCardCreateWriterFunc create_writer;
MSSndCardUninitFunc uninit;
MSSndCardDuplicateFunc duplicate;
MSSndCardUnloadFunc unload;
};
在88行有: typedef void (*MSSndCardDetectFunc)(MSSndCardManager *obj);
在mscommon.c 中定义了ms_snd_card_descs ,请看->
static MSSndCardDesc * ms_snd_card_descs[]={
#ifdef __ALSA_ENABLED__
&alsa_card_desc,
#endif
#ifdef HAVE_SYS_SOUNDCARD_H
&oss_card_desc,
#endif
#ifdef __ARTS_ENABLED__
&arts_card_desc,
#endif
#ifdef WIN32
&winsnd_card_desc,
#endif
#ifdef __DIRECTSOUND_ENABLED__
&winsndds_card_desc,
#endif
#ifdef __PORTAUDIO_ENABLED__
&pasnd_card_desc,
#endif
#ifdef __MACSND_ENABLED__
&ca_card_desc,
#endif
#ifdef __PULSEAUDIO_ENABLED__
&pulse_card_desc,
#endif
#if TARGET_OS_IPHONE
&au_card_desc,
#endif
#ifdef __MAC_AQ_ENABLED__
&aq_card_desc,
#endif
#ifdef ANDROID
&msandroid_sound_card_desc,
#endif
NULL
};
在line482查到了android 的声卡描述 (注意这个是个全局变量)
#ifdef ANDROID
extern MSSndCardDesc msandroid_sound_card_desc;
#endif
在msandroid.cpp中找到了msandroid_sound_card_desc的赋值操作。
MSSndCardDesc msandroid_sound_card_desc = {
/*.driver_type=*/"ANDROID SND",
/*.detect=*/msandroid_sound_detect,
/*.init=*/msandroid_sound_init,
/*.set_level=*/msandroid_sound_set_level,
/*.get_level=*/msandroid_sound_get_level,
/*.set_capture=*/msandroid_sound_set_source,
/*.set_control=*/NULL,
/*.get_control=*/NULL,
/*.create_reader=*/msandroid_sound_read_new,
/*.create_writer=*/msandroid_sound_write_new,
/*.uninit=*/msandroid_sound_uninit,
/*.duplicate=*/msandroid_sound_duplicate
};
好了,android的声卡描述终于找到了,现在回过头来看看mssndcard.c 中的
static void card_detect(MSSndCardManager *m, MSSndCardDesc *desc){
if (desc->detect!=NULL)
desc->detect(m);
}
在 msandroid_sound_card_desc 中 detect的值为:msandroid_sound_detect
那么在msandroid.cpp中找到这个方法:
void msandroid_sound_detect(MSSndCardManager *m){
ms_debug("msandroid_sound_detect");
MSSndCard *card=msandroid_sound_card_new();
ms_snd_card_manager_add_card(m,card);
}
先看msandroid_sound_card_new();
MSSndCard *msandroid_sound_card_new(){
MSSndCard *card=ms_snd_card_new(&msandroid_sound_card_desc);
card->name=ms_strdup("Android Sound card");
return card;
}
在mssndcard.c中查看ms_snd_card_new方法:
MSSndCard * ms_snd_card_new(MSSndCardDesc *desc){
return ms_snd_card_new_with_name(desc,NULL);
}
MSSndCard * ms_snd_card_new_with_name(MSSndCardDesc *desc,const char* name) {
MSSndCard *obj=(MSSndCard *)ms_new(MSSndCard,1);
obj->desc=desc;
obj->name=name?ms_strdup(name):NULL;
obj->data=NULL;
obj->id=NULL;
obj->capabilities=MS_SND_CARD_CAP_CAPTURE|MS_SND_CARD_CAP_PLAYBACK;
if (desc->init!=NULL)
desc->init(obj);
return obj;
}
结果发现最终还是调到了msandroid.cpp中的init方法:
可惜的是可惜的是!!!!!msandroid.cpp 中msandroid_sound_card_desc的init值为 msandroid_sound_init,它的方法体
是空的!!!
void msandroid_sound_init(MSSndCard *card){
}
傻眼了!!!!,虽然init是空的,但是呢,毕竟一个MSSndCard被创建出来了,在方法ms_snd_card_new_with_name中,
而且这个MSSndCard中初始化了desc,name,capabilities的值。只是没有init而已!!
好吧,先停一停,回到msandroid.cpp中的
void msandroid_sound_detect(MSSndCardManager *m){
ms_debug("msandroid_sound_detect");
MSSndCard *card=msandroid_sound_card_new();
ms_snd_card_manager_add_card(m,card);
}
从一路跟踪msandroid_sound_card_new下来,发现card是有值了,现在就把这个声卡添加到声卡管理器中:
在mssndcard.c 103 ->
void ms_snd_card_manager_add_card(MSSndCardManager *m, MSSndCard *c){
ms_message("Card ‘%s‘ added",ms_snd_card_get_string_id(c));
m->cards=ms_list_append(m->cards,c);
}
*****************ms_init方法中的 ms_snd_card_manager_register_desc方法 分析技术*********************
标签:
原文地址:http://www.cnblogs.com/samaritan/p/4235289.html