motion源代码分析

2024-08-31 06:58
文章标签 分析 源代码 motion

本文主要是介绍motion源代码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自: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将图像加入录像。

 

 

更多Linux资料及视频教程点击这里

这篇关于motion源代码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123213

相关文章

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C