motion源代码分析

来源:互联网 发布:mac dock 切换屏幕 编辑:程序博客网 时间:2024/05/17 03:42

转自:http://blog.csdn.net/sakaue/article/details/22816475

楔子

前几天研究了如何将ffmpeg编入motion,并实现录像功能。现在研究下motion的工作流程。


几个主要模块

motion.c主程序,视频采集编码主循环ffmpeg.c一个代理模块,封装了ffmpeg的方法,根据v4l获取的数据编码录像video.2.c提供video4linux功能video_common.c封装video.2.c中功能,为motion.c提供支持vebhttpd.c向客户端提供控制功能webcam.c向客户端提供实时图像服务(浏览器刷图片)


正文

照例从main函数进入。在这里,main起了两个线程,一个是集视频采集录像放送于一体的motion_loop:

[cpp] view plain copy
  1. /* Start the motion threads. First 'cnt_list' item is global if 'thread' 
  2.  * option is used, so start at 1 then and 0 otherwise. 
  3.  */  
  4. for (i = cnt_list[1] != NULL ? 1 : 0; cnt_list[i]; i++) {  
  5.     /* If i is 0 it means no thread files and we then set the thread number to 1 */  
  6.     cnt_list[i]->threadnr = i ? i : 1;  
  7.   
  8.     if (strcmp(cnt_list[i]->conf_filename,"") )  
  9.         motion_log(LOG_INFO, 0, "Thread %d is from %s", cnt_list[i]->threadnr, cnt_list[i]->conf_filename );  
  10.   
  11.         if (cnt_list[0]->conf.setup_mode) {  
  12.             motion_log(-1, 0, "Thread %d is device: %s input %d", cnt_list[i]->threadnr,  
  13.                        cnt_list[i]->conf.netcam_url ? cnt_list[i]->conf.netcam_url : cnt_list[i]->conf.video_device,  
  14.                        cnt_list[i]->conf.netcam_url ? -1 : cnt_list[i]->conf.input  
  15.                        );  
  16.         }  
  17.   
  18.         if (cnt_list[0]->conf.setup_mode)  
  19.             motion_log(LOG_ERR, 0, "Webcam port %d", cnt_list[i]->conf.webcam_port);  
  20.   
  21.         start_motion_thread(cnt_list[i], &thread_attr); //在这里启动线程函数motion_loop  
  22. }  
一个是用于web控制的motion_web_control:

[cpp] view plain copy
  1. /* Create a thread for the control interface if requested. Create it 
  2.  * detached and with 'motion_web_control' as the thread function. 
  3.  */  
  4. if (cnt_list[0]->conf.control_port)  
  5.     pthread_create(&thread_id, &thread_attr, &motion_web_control, cnt_list); //启动线程函数motion_web_control  

而main中的迭代主要负责一些统计任务。我们主要关心motion的核心——motion_loop。

刷照片的效果实在太挫了,让我们看看如何打开ffmpeg录像的配置。这里需要修改motion-dist.conf中的两个选项(采用默认值则不会录像):

[cpp] view plain copy
  1. # Use ffmpeg to encode a timelapse movie   
  2. # Default value 0 = off - else save frame every Nth second  
  3. ffmpeg_timelapse 1  
  4.   
  5.   
  6. # Enables and defines variable bitrate for the ffmpeg encoder.  
  7. # ffmpeg_bps is ignored if variable bitrate is enabled.  
  8. # Valid values: 0 (default) = fixed bitrate defined by ffmpeg_bps,  
  9. # or the range 2 - 31 where 2 means best quality and 31 is worst.  
  10. ffmpeg_variable_bitrate 1  
第一个选项是设置每一秒保存帧(帧的数量由配置文件中的framerate决定 ),第二个选项设置ffmpeg编码的比特率。下面还有个选项:
[cpp] view plain copy
  1. # Codec to used by ffmpeg for the video compression.  
  2. # Timelapse mpegs are always made in mpeg1 format independent from this option.  
  3. # Supported formats are: mpeg1 (ffmpeg-0.4.8 only), mpeg4 (default), and msmpeg4.  
  4. # mpeg1 - gives you files with extension .mpg  
  5. # mpeg4 or msmpeg4 - gives you files with extension .avi  
  6. # msmpeg4 is recommended for use with Windows Media Player because  
  7. # it requires no installation of codec on the Windows client.  
  8. # swf - gives you a flash film with extension .swf  
  9. # flv - gives you a flash video with extension .flv  
  10. # ffv1 - FF video codec 1 for Lossless Encoding ( experimental )  
  11. # mov - QuickTime ( testing )  
  12. ffmpeg_video_codec mpeg4  

这个其实该不该无所谓,因为编码器的格式在里面是写死的,稍后会提到。motion的录像在motion_loop(motion.c:1698)当中:

[cpp] view plain copy
  1. #ifdef HAVE_FFMPEG  
  2.   
  3.   
  4.   
  5.         if (cnt->conf.timelapse) {  
  6.   
  7.             /* Check to see if we should start a new timelapse file. We start one when 
  8.              * we are on the first shot, and and the seconds are zero. We must use the seconds 
  9.              * to prevent the timelapse file from getting reset multiple times during the minute. 
  10.              */  
  11.             if (cnt->current_image->timestamp_tm.tm_min == 0 &&  
  12.                 (time_current_frame % 60 < time_last_frame % 60) &&  
  13.                 cnt->shots == 0) {  
  14.   
  15.                 if (strcasecmp(cnt->conf.timelapse_mode,"manual") == 0) {  
  16.                     ;/* No action */  
  17.   
  18.                 /* If we are daily, raise timelapseend event at midnight */  
  19.                 } else if (strcasecmp(cnt->conf.timelapse_mode, "daily") == 0) {  //根据配置文件中ffmpeg_timelapse_mode的选则执行相应代码  
  20.                     if (cnt->current_image->timestamp_tm.tm_hour == 0)  
  21.                         event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL,   
  22.                               &cnt->current_image->timestamp_tm);  
  23.   
  24.                 /* handle the hourly case */  
  25.                 } else if (strcasecmp(cnt->conf.timelapse_mode, "hourly") == 0) {  
  26.                     event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL,   
  27.                           &cnt->current_image->timestamp_tm);  
  28.   
  29.                 /* If we are weekly-sunday, raise timelapseend event at midnight on sunday */  
  30.                 } else if (strcasecmp(cnt->conf.timelapse_mode, "weekly-sunday") == 0) {  
  31.                     if (cnt->current_image->timestamp_tm.tm_wday == 0 && cnt->current_image->timestamp_tm.tm_hour == 0)  
  32.                         event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tm);  
  33.   
  34.                 /* If we are weekly-monday, raise timelapseend event at midnight on monday */      
  35.                 } else if (strcasecmp(cnt->conf.timelapse_mode, "weekly-monday") == 0) {  
  36.                     if (cnt->current_image->timestamp_tm.tm_wday == 1 && cnt->current_image->timestamp_tm.tm_hour == 0)  
  37.                         event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tm);  
  38.   
  39.                 /* If we are monthly, raise timelapseend event at midnight on first day of month */      
  40.                 } else if (strcasecmp(cnt->conf.timelapse_mode, "monthly") == 0) {  
  41.                     if (cnt->current_image->timestamp_tm.tm_mday == 1 && cnt->current_image->timestamp_tm.tm_hour == 0)  
  42.                         event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tm);  
  43.   
  44.                 /* If invalid we report in syslog once and continue in manual mode */      
  45.                 } else {  
  46.                     motion_log(LOG_ERR, 0, "Invalid timelapse_mode argument '%s'",  
  47.                                cnt->conf.timelapse_mode);  
  48.                     motion_log(LOG_ERR, 0, "Defaulting to manual timelapse mode");  
  49.                     conf_cmdparse(&cnt, (char *)"ffmpeg_timelapse_mode",(char *)"manual");  
  50.                 }  
  51.             }  
  52.   
  53.             /* If ffmpeg timelapse is enabled and time since epoch MOD ffmpeg_timelaps = 0 
  54.              * add a timelapse frame to the timelapse mpeg. 
  55.              */  
  56.             if (cnt->shots == 0 &&  
  57.                 time_current_frame % cnt->conf.timelapse <= time_last_frame % cnt->conf.timelapse)  
  58.                 event(cnt, EVENT_TIMELAPSE, cnt->current_image->image, NULL, NULL,   
  59.                       &cnt->current_image->timestamp_tm);     // Line:1757 在这里创建并保存录制的视频  
  60.         } else if (cnt->ffmpeg_timelapse) {  
  61.         /* if timelapse mpeg is in progress but conf.timelapse is zero then close timelapse file 
  62.          * This is an important feature that allows manual roll-over of timelapse file using the http 
  63.          * remote control via a cron job. 
  64.          */  
  65.             event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, cnt->currenttime_tm);   
  66.         }  
  67.   
  68. #endif /* HAVE_FFMPEG */  
执行录制视频的方法在1757行,根据EVENT_TIMELAPSE,它对应的方法叫event_ffmpeg_timelapse

[cpp] view plain copy
  1. // event.c:673  
  2.   
  3. {  
  4. EVENT_TIMELAPSE,  
  5. event_ffmpeg_timelapse  
  6. },  
[cpp] view plain copy
  1. // event.c:461  
  2.   
  3. static void event_ffmpeg_timelapse(struct context *cnt,  
  4.             int type ATTRIBUTE_UNUSED, unsigned char *img,  
  5.             char *dummy1 ATTRIBUTE_UNUSED, void *dummy2 ATTRIBUTE_UNUSED,  
  6.             struct tm *currenttime_tm)  
  7. {  
  8.     int width = cnt->imgs.width;  
  9.     int height = cnt->imgs.height;  
  10.     unsigned char *convbuf, *y, *u, *v;  
  11.   
  12.     if (!cnt->ffmpeg_timelapse) {  //只在第一次创建时执行  
  13.         char tmp[PATH_MAX];  
  14.         const char *timepath;  
  15.   
  16.         /* conf.timepath would normally be defined but if someone deleted it by control interface 
  17.            it is better to revert to the default than fail */  
  18.         if (cnt->conf.timepath)  
  19.             timepath = cnt->conf.timepath;  
  20.         else  
  21.             timepath = DEF_TIMEPATH;  
  22.           
  23.         mystrftime(cnt, tmp, sizeof(tmp), timepath, currenttime_tm, NULL, 0);  
  24.           
  25.         /* PATH_MAX - 4 to allow for .mpg to be appended without overflow */  
  26.         snprintf(cnt->timelapsefilename, PATH_MAX - 4, "%s/%s", cnt->conf.filepath, tmp);  
  27.           
  28.         if (cnt->imgs.type == VIDEO_PALETTE_GREY) {  
  29.             convbuf = mymalloc((width * height) / 2);  
  30.             y = img;  
  31.             u = convbuf;  
  32.             v = convbuf+(width * height) / 4;  
  33.             grey2yuv420p(u, v, width, height);  
  34.         } else {  
  35.             convbuf = NULL;  
  36.             y = img;  
  37.             u = img + width * height;  
  38.             v = u + (width * height) / 4;  
  39.         }  
  40.           
  41.         if ((cnt->ffmpeg_timelapse =  
  42.              ffmpeg_open((char *)TIMELAPSE_CODEC, cnt->timelapsefilename, y, u, v,   //#define TIMELAPSE_CODEC "mpeg1_tl"  
  43.                          cnt->imgs.width, cnt->imgs.height, 24, cnt->conf.ffmpeg_bps,  
  44.                          cnt->conf.ffmpeg_vbr)) == NULL) {   // 创建录像文件  
  45.             motion_log(LOG_ERR, 1, "ffopen_open error creating (timelapse) file [%s]", cnt->timelapsefilename);  
  46.             cnt->finish = 1;  
  47.             return;  
  48.         }  
  49.           
  50.         cnt->ffmpeg_timelapse->udata = convbuf;  
  51.         event(cnt, EVENT_FILECREATE, NULL, cnt->timelapsefilename, (void *)FTYPE_MPEG_TIMELAPSE, NULL);  
  52.     }  
  53.       
  54.     y = img;  
  55.       
  56.     if (cnt->imgs.type == VIDEO_PALETTE_GREY)  
  57.         u = cnt->ffmpeg_timelapse->udata;  
  58.     else  
  59.         u = img + width * height;  
  60.       
  61.     v = u + (width * height) / 4;  
  62.     ffmpeg_put_other_image(cnt->ffmpeg_timelapse, y, u, v);  //put图像进录像  
  63.       
  64. }  
其中Line:500,如果是第一次进入,则执行ffmpeg_open:

[cpp] view plain copy
  1. ffmpeg_open((char *)TIMELAPSE_CODEC,                                // #define TIMELAPSE_CODEC "mpeg1_tl"  
  2.                      cnt->timelapsefilename,                                    // 录像文件名  
  3.                       y, u, v,//#define TIMELAPSE_CODEC "mpeg1_tl"   
  4.                       cnt->imgs.width, cnt->imgs.height,                   // 录像的宽高  
  5.                       24,                                                                 // fps   
  6.                       cnt->conf.ffmpeg_bps,                                    // bps  
  7.                       cnt->conf.ffmpeg_vbr)) == NULL)  
如果不是第一次进入,则会直接执行ffmpeg_put_other_image将图像加入录像。

阅读全文
0 0