mjpg-streamer项目源码分析

mjpg-streamer项目源码分析  

2013-09-09 10:41:05|  分类: GT2440 |举报 |字号 订阅

2012-11-06 10:04  397人阅读  评论(2)  收藏  举报

        前一段时间自己买了个开发板(GT2440的),可是我没有够相应的买cmos摄像头,可是又想做下国嵌的usb视频采集和传输的哪个项目,没办法,只好网上找找相关的项目,最终发现了mjpg-streamer这个开源项目。看了blog们的文章,有种激动,于是自己问同学借了个usb摄像头,试了试,挺好使的,而且处理速度上也挺好的,就开始想了解这个项目是怎么工作了(研究了好几天哦)。

下面是我个人在这学习mjpg-streamer时的一些看法,不足之处,还请大家多多指出:

首先,mjpg-streamer目录由以下目录组成:

.deps 

doc 

mjpeg-client //这个目录包含我们使用的客服端

mjpg-streamer //这个是usb摄像头采集和传输的目录

mjpg-streamer-experiment

udp-client

uvc-streamer.

这里我们只分析下mjpg-streamer目录,下面看下上面的mjpg-streamer目录的组成:


plugins目录:主要是一些usb摄像头的数据采集和传输的功能子函数

scripts目录貌似没啥用

www目录主要是在使用浏览器浏览时,可以增加html界面上一些功能。

mjpg-streamer.c文件夹,主要实现命令参数的解析及调用相关线程运行功能子函数

下面我们看下mjpg-streamer.c中的源代码:

  1. int main(int argc, char *argv[])  
  2. {  
  3.   char *input  = "input_uvc.so --resolution 640x480 --fps 5 --device /dev/video0";  
  4.   char *output[MAX_OUTPUT_PLUGINS];//10  
  5.   int daemon=0, i;  
  6.   size_t tmp=0;  
  7.   
  8.   output[0] = "output_http.so --port 8080";  
  9.   global.outcnt = 0;  
  10.   
  11.   global.control = control;  
  12.   
  13.   /* parameter parsing */  
  14.   while(1) {  
  15.     int option_index = 0c=0;  
  16.     static struct option long_options[] = \  
  17.     {  
  18.       {"h", no_argument, 0, 0},  
  19.       {"help", no_argument, 0, 0},  
  20.       {"i", required_argument, 0, 0},  
  21.       {"input", required_argument, 0, 0},  
  22.       {"o", required_argument, 0, 0},  
  23.       {"output", required_argument, 0, 0},  
  24.       {"v", no_argument, 0, 0},  
  25.       {"version", no_argument, 0, 0},  
  26.       {"b", no_argument, 0, 0},  
  27.       {"background", no_argument, 0, 0},  
  28.       {0, 0, 0, 0}  
  29.     };  
  30.   
  31.     c = getopt_long_only(argc, argv, "", long_options, &option_index);  

这里主要遇到一个功能函数getopt_long_only(),该函数功能主要是解析命令行选项,也就是将*input中的-h -i -o -v -b的参数解析出来,该函数具体如何使用,可参照 getopt_long_only .
  1.   switch (option_index) {  
  2.     /* h, help */  
  3.     case 0:  
  4.     case 1:  
  5.       help(argv[0]);  
  6.       return 0;  
  7.       break;  
  8.   
  9.     /* i, input */  
  10.     case 2:  
  11.     case 3:  
  12.       input = strdup(optarg);  
  13.       break;  
  14.   
  15.     /* o, output */  
  16.     case 4:  
  17.     case 5:  
  18.       output[global.outcnt++] = strdup(optarg);  
  19.       break;  
  20.   
  21.     /* v, version */  
  22.     case 6:  
  23.     case 7:  
  24.       printf("MJPG Streamer Version: %s\n" \  
  25.              "Compilation Date.....: %s\n" \  
  26.              "Compilation Time.....: %s\n", SOURCE_VERSION, __DATE__, __TIME__);  
  27.       return 0;  
  28.       break;  
  29.   
  30.     /* b, background */  
  31.     case 8:  
  32.     case 9:  
  33.       daemon=1;  
  34.       break;  
  35.   
  36.     default:  
  37.       help(argv[0]);  
  38.       return 0;  
  39.   }  
  40. }  

option_index保存的是上面的long_options中的坐标值,strdup(optarg)用来获取相应的标记后的参数,例如当main函数中传入的命令为./mjpg_streamer -i "./input_uvc.so"时,

程序将执行到case3:中,然后将input="./input_uvc.so".

至此,前面就帮我们将输入的mjpg-streamer命令读解析好了。

下面开始调用相应的函数:

  1.   /* open input plugin */  
  2.   tmp = (size_t)(strchr(input, ' ')-input);  
  3.   global.in.plugin = (tmp > 0)?strndup(input, tmp):strdup(input);  
  4.   global.in.handle = dlopen(global.in.plugin, RTLD_LAZY);  
  5.   if ( !global.in.handle ) {  
  6.     LOG("ERROR: could not find input plugin\n");  
  7.     LOG("       Perhaps you want to adjust the search path with:\n");  
  8.     LOG("       # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");  
  9.     LOG("       dlopen: %s\n", dlerror() );  
  10.     closelog();  
  11.     exit(EXIT_FAILURE);  
  12.   }  
  13.   global.in.init = dlsym(global.in.handle, "input_init");  
  14.   if ( global.in.init == NULL ) {  
  15.     LOG("%s\n", dlerror());  
  16.     exit(EXIT_FAILURE);  
  17.   }  
  18.   global.in.stop = dlsym(global.in.handle, "input_stop");  
  19.   if ( global.in.stop == NULL ) {  
  20.     LOG("%s\n", dlerror());  
  21.     exit(EXIT_FAILURE);  
  22.   }  
  23.   global.in.run = dlsym(global.in.handle, "input_run");  
  24.   if ( global.in.run == NULL ) {  
  25.     LOG("%s\n", dlerror());  
  26.     exit(EXIT_FAILURE);  
  27.   }  

dlopen()函数负责打开*.so插件,dlsym()负责调用插件中的相关函数。目前意思已经很明显了,当输入./mjpg_streamer -i "./input_uvc.so"时,系统将会调用input_uvc.so中的input_init函数,对输入设备进行初始化。我们打开plugins\input_uvc\input_uvc.c看下是否有input_init()函数,确实存在,这说明我前面的理解没错。

既然输入已经初始化了,那输出呢?不急,我们继续看下下面的代码:

  1. /* open output plugin */  
  2.   for (i=0; i<global.outcnt; i++) {  
  3.     tmp = (size_t)(strchr(output[i], ' ')-output[i]);  
  4.     global.out[i].plugin = (tmp > 0)?strndup(output[i], tmp):strdup(output[i]);  
  5.     global.out[i].handle = dlopen(global.out[i].plugin, RTLD_LAZY);  
  6.     if ( !global.out[i].handle ) {  
  7.       LOG("ERROR: could not find output plugin %s\n", global.out[i].plugin);  
  8.       LOG("       Perhaps you want to adjust the search path with:\n");  
  9.       LOG("       # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");  
  10.       LOG("       dlopen: %s\n", dlerror() );  
  11.       closelog();  
  12.       exit(EXIT_FAILURE);  
  13.     }  
  14.     global.out[i].init = dlsym(global.out[i].handle, "output_init");  
  15.     if ( global.out[i].init == NULL ) {  
  16.       LOG("%s\n", dlerror());  
  17.       exit(EXIT_FAILURE);  
  18.     }  
  19.     global.out[i].stop = dlsym(global.out[i].handle, "output_stop");  
  20.     if ( global.out[i].stop == NULL ) {  
  21.       LOG("%s\n", dlerror());  
  22.       exit(EXIT_FAILURE);  
  23.     }  
  24.     if ( global.out[i].stop == NULL ) {  
  25.       LOG("%s\n", dlerror());  
  26.       exit(EXIT_FAILURE);  
  27.     }  
  28.     global.out[i].run = dlsym(global.out[i].handle, "output_run");  
  29.     if ( global.out[i].run == NULL ) {  
  30.       LOG("%s\n", dlerror());  
  31.       exit(EXIT_FAILURE);  
  32.     }  
这段代码就不分析了,相信大家已经很明白了。

嗯,输入和输出设备都准备,下面该干啥了,不用说肯定干活了吗。俗话说“养军千日用在一时”:

  1. if ( global.in.run() ) {  
  2.     LOG("can not run input plugin\n");  
  3.     closelog();  
  4.     return 1;  
  5.   }  
  6.   
  7. for(i=0; i<global.outcnt; i++) {  
  8.     syslog(LOG_INFO, "starting output plugin: %s (ID: %02d)", global.out[i].plugin, global.out[i].param.id);  
  9.     global.out[i].run(global.out[i].param.id);  
  10. }  
mjpg-streamer就这样结束了,貌似还真不过瘾,下面我们到input_uvc.c和output_http.c文件看下:

input_uvc.c(plugins\input_uvc\)

  1. int input_init(input_parameter *param)   
这个函数貌似挺长的,其实代码也就只是实现对usb摄像头的格式、帧、请求buf,队列buf等的一些设置,主要实现也是在函数init_videoIn(videoIn, dev, width, height, fps, format, 1) 中,有兴趣的话,大家可以去看看。
  1. int input_stop(void) {  
  2.   DBG("will cancel input thread\n");  
  3.   pthread_cancel(cam);  
  4.   
  5.   return 0;  
  6. }  

输入停止,貌似更加简单,只要将线程取下cancel就ok,那么run肯定是crete threads了。
  1. int input_run(void) {  
  2.   pglobal->buf = malloc(videoIn->framesizeIn);  
  3.   if (pglobal->buf == NULL) {  
  4.     fprintf(stderr, "could not allocate memory\n");  
  5.     exit(EXIT_FAILURE);  
  6.   }  
  7.   
  8.   pthread_create(&cam, 0, cam_thread, NULL);  
  9.   pthread_detach(cam);  
  10.   
  11.   return 0;  
  12. }  

猜对了,可惜没分加。下面那些代码基本也没用到,这就不细说了。

output_http.c(plugins\output_http\)

刚开始也肯定是对其初始化,然后stop,最后run。其实我挺喜欢这个项目的编写者,处处为咱们考虑。

  1. int output_init(output_parameter *param) {  
  2.   servers[param->id].id = param->id;  
  3.   servers[param->id].pglobal = param->global;  
  4.   servers[param->id].conf.port = port;  
  5.   servers[param->id].conf.credentials = credentials;  
  6.   servers[param->id].conf.www_folder = www_folder;  
  7.   servers[param->id].conf.nocommands = nocommands;  
  8. }  
  9. int output_stop(int id) {  
  10.   
  11.   DBG("will cancel server thread #%02d\n", id);  
  12.   pthread_cancel(servers[id].threadID);  
  13.   
  14.   return 0;  
  15. }  
  16.   
  17. int output_run(int id) {  
  18.   DBG("launching server thread #%02d\n", id);  
  19.   
  20.   /* create thread and pass context to thread function */  
  21.   pthread_create(&(servers[id].threadID), NULL, server_thread, &(servers[id]));  
  22.   pthread_detach(servers[id].threadID);  
  23.   
  24.   return 0;  
  25. }  

我想这个项目分析的差不多了,至于一些小的方面,我不依依细说了,最后送大家一句古语“  师傅领进门,修行靠个人 ”,并赠上这个项目的框架图。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值