Motion source code parsing

Source: Internet
Author: User

I haven't updated my blog for a long time. This is not good. After work, I still need to constantly learn new technologies, external technologies, and so on ......

Motion is an open-source project based on the Linux platform. Its function is to detect image motion in video streams, similar to motion detection. The entire project has more than 30 files, which are very small. It is said that axis, the Network Camera's big-name manufacturer, also uses this algorithm to process it. I also need to take a nap ~!

The program entry is the main function in motion. C.

/** <Br/> * main entry point of motion. launches all the motion threads and contains <br/> * The logic for starting up, restarting and cleaning up everything. <br/> * parameters: <br/> * argc-size of argv <br/> * argv-command-line options <br/> * returns: motion exit status = 0 always <br/> */<br/> int main (INT argc, char ** argv) <br/>{< br/> int I; <Br/> pthread_attr_t thread_attr; <br/> pthread_t thread_id; <br/>/* setup signals and do some initialization. 1 In the call to <br/> * 'Motion _ startup' means that motion will become a daemon if so has been <br/> * requested, and argc and argc are necessary for reading the command <br/> * line options. <br/> */<br/> struct sigaction sig_handler_action; <br/> struct sigaction sigchild_action; <br /> Setup_signals (& sig_handler_action, & sigchild_action); <br/> motion_startup (1, argc, argv ); <br/> # ifdef have_ffmpeg <br/>/* FFMPEG Initialization is only med if FFMPEG support was found <br/> * and not disabled during the configure phase. <br/> */<br/> ffmpeg_init (); <br/> # endif/* have_ffmpeg */<br/>/* In setup mode, motion is very communicative towards the user, which <br/> * allows The user to experiment with the config parameters in order to <br/> * optimize motion detection and stuff. <br/> */<br/> If (cnt_list [0]-> Conf. setup_mode) <br/> motion_log (-1, 0, "motion running in setup mode. "); <br/>/* Create and a thread attribute for the threads we spawn later on. <br/> * pthread_create_detached means to create threads detached, I. e. <br/> * their termination cannot be sync Hronized through 'pthread _ join '. <br/> */<br/> pthread_attr_init (& thread_attr); <br/> pthread_attr_setdetachstate (& thread_attr, pthread_create_detached ); <br/>/* Create the TLS key for thread number. */<br/> pthread_key_create (& tls_key_threadnr, null); <br/> do {<br/> If (restart) {<br/>/* handle the restart situation. currently the approach is to <br/> * cleanup everything, and then initial Ize everything again <br/> * (including re-reading the config file (s )). <br/> */<br/> motion_shutdown (); <br/> restart = 0;/* only one reset for now */<br/> motion_log (log_info, 0, "motion restarted"); <br/> # ifndef without_v4l <br/> sleep (5, 0 ); // maybe some cameras needs less time <br/> # endif <br/> motion_startup (0, argc, argv ); /* 0 = skip daemon init */<br/>}</P> <p>/* Start the motion threa DS. first 'cnt _ list' item is global if 'thread' <br/> * option is used, so start at 1 then and 0 otherwise. <br/> */<br/> for (I = cnt_list [1]! = NULL? 1: 0; cnt_list [I]; I ++) {<br/>/* If I is 0 it means no thread files and we then set the thread number to 1 */<br/> cnt_list [I]-> threadnr = I? I: 1; <br/> If (strcmp (cnt_list [I]-> conf_filename, "") <br/> motion_log (log_info, 0, "thread % d is from % s", cnt_list [I]-> threadnr, cnt_list [I]-> conf_filename ); <br/> If (cnt_list [0]-> Conf. setup_mode) {<br/> motion_log (-1, 0, "thread % d is device: % s input % d", cnt_list [I]-> threadnr, <br/> cnt_list [I]-> Conf. netcam_url? Cnt_list [I]-> Conf. netcam_url: cnt_list [I]-> Conf. video_device, <br/> cnt_list [I]-> Conf. netcam_url? -1: cnt_list [I]-> Conf. input <br/>); <br/>}< br/> If (cnt_list [0]-> Conf. setup_mode) <br/> motion_log (log_err, 0, "webcam port % d", cnt_list [I]-> Conf. webcam_port); <br/> start_motion_thread (cnt_list [I], & thread_attr); <br/>}< br/>/* Create a thread for the control interface if requested. create it <br/> * detached and with 'Motion _ web_control 'as the thread function. <br/> */<br/> If (cnt_list [0]-> Conf. control_port) <br/> pthread_create (& thread_id, & thread_attr, & motion_web_control, cnt_list); <br/> If (cnt_list [0]-> Conf. setup_mode) <br/> motion_log (-1, 0, "waiting for threads to finish, PID: % d", getpid ()); <br/>/* crude way of waiting for all threads to finish-check the thread <br/> * counter (because we cannot do join on the detached threads ). <br/> */<br/> while (1) {<br/> slee P (1, 0); <br/>/* calculate how many threads runnig or wants to run <br/> * If zero and we want to finish, break out <br/> */<br/> int motion_threads_running = 0; <br/> for (I = (cnt_list [1]! = NULL? 1: 0); cnt_list [I]; I ++) {<br/> If (cnt_list [I]-> running | cnt_list [I]-> restart) <br/> motion_threads_running ++; <br/>}< br/> If (motion_threads_running = 0) & finish) | <br/> (motion_threads_running = 0) & (threads_running = 0) {<br/> If (debug_level> = camera_debug) {<br/> motion_log (log_info, 0, "debug-1 threads_running % d motion_threads_running % d, finish % d", <br/> threads _ Running, motion_threads_running, finish); <br/>}< br/> break; <br/>}< br/> for (I = (cnt_list [1]! = NULL? 1: 0); cnt_list [I]; I ++) {<br/>/* Check if threads wants to be restarted */<br/> If ((! Cnt_list [I]-> running) & (cnt_list [I]-> restart) {<br/> motion_log (log_info, 0, "motion thread % d restart ", cnt_list [I]-> threadnr); <br/> start_motion_thread (cnt_list [I], & thread_attr ); <br/>}< br/> If (cnt_list [I]-> watchdog> watchdog_off) {<br/> cnt_list [I]-> watchdog --; <br/> If (cnt_list [I]-> watchdog = 0) {<br/> motion_log (log_err, 0, "thread % d-watchdog timeout, trying to do a graceful Restart ", <br/> cnt_list [I]-> threadnr); <br/> cnt_list [I]-> finish = 1; <br/>}< br/> If (cnt_list [I]-> watchdog =-60) {<br/> motion_log (log_err, 0, "thread % d-watchdog timeout, did not restart graceful," <br/> "killing it! ", Cnt_list [I]-> threadnr); <br/> pthread_cancel (cnt_list [I]-> thread_id); <br/> pthread_mutex_lock (& global_lock ); <br/> threads_running --; <br/> pthread_mutex_unlock (& global_lock); <br/> motion_cleanup (cnt_list [I]); <br/> cnt_list [I]-> running = 0; <br/> cnt_list [I]-> finish = 0; <br/>}< br/> If (debug_level> = camera_debug) {<br/> motion_log (log_info, 0, "debug-2 threads_running % d motion_threads_running % d, finish % d", <br/> threads_running, motion_threads_running, finish ); <br/>}< br/>/* reset end main loop flag */<br/> finish = 0; <br/> If (cnt_list [0]-> Conf. setup_mode) <br/> motion_log (log_debug, 0, "Threads finished"); <br/>/* rest for a while if we're re supposed to restart. */<br/> If (restart) <br/> sleep (2, 0); <br/>}while (restart ); /* loop if we're supposed to restart */<br/> // be sure that HTTP control exits fine <br/> cnt_list [0]-> finish = 1; <br/> sleep (1, 0); <br/> motion_log (log_info, 0, "motion terminating"); <br/>/* perform final cleanup. */<br/> pthread_key_delete (tls_key_threadnr); <br/> abort (& thread_attr); <br/> pthread_mutex_destroy (& global_lock); <br/> motion_shutdown (); <br/> return 0; <br/>}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.