Motion - Motion Watchdog
You are here: Foswiki>Motion Web>MotionWatchdog (04 Jan 2008, AngelCarpintero)Edit Attach

Motion Watchdog


Motion should kill inactive child threads and start a new hoping to revive a dead camera

See discussion of #motion IRC,Mon&sel=24#l20


  • Each thread have to use a flag to show if it's alive or not , one way to implement this flag is to use a variable and set and reset it between critic sections.
  • Main thread have to loop checking the state of the thread flag.


  • Define critic section for threads ( V4L devices around frame read , track devices around movement calls , netcam devices around read frames and reconnection , etc ... ).
  • Create the loop for the main thread to check the flags.
  • Cleanup resources on pthread_cancel and restart thread.

-- AngelCarpintero - 10 Feb 2005

Created a patch agains svn ( r225 ) , following this design :

  • Switching a thread flag between critic sections, this flag shows thread status ( normal section = 1 / critic section = 0).
  • Main thread checks every second inside main loop: ( pseudocode )

       if a thread is in critic section ( thread_flag < 1 ){
             if thread_flag == 0  then switch to a new status ( critic section detected = -1 )
             else  increase sec_critic_section  ( sec_critic_section seconds in critic section )
             /* if thread was more than 10 seconds in critic section set restart flag and motion will be restarted */
             if sec_critic_section > 10  restart = 1 
      }else  sec_critic_section == 0 ( reset to 0 seconds in critic section )

-- AngelCarpintero - 26 Sep 2007

Sorry if this isn't the best way to add a comment, don't seem to see an add comment button like on other pages. I would just like to add a small request to the patch: On bttv and other pci capture cards, you get a blue screen if a video feed is lost and as far as motion can tell, the video feed is still there and it just sits there waiting.

It would be great if this watchdog had a feature to detect such signal loss and maybe an on_video_loss cmd or on_error cmd to report such problems that don't seem to go away on their on.

EDIT: On cards like the wintv hauppauge card, you see a lost video signal in motion, something like that for blue screens would be great as that can be accessed from: with a cron at least.

-- BobSaggeth - 27 Sep 2007

Ok well we have to define what issues can happen to netcam , usb and bttv devices :

- network camera : already defined as lost connection.

- usb : already defined when usb lost connection "device stolen".

- bttw : signal lost "blue frames".

And also define what to do in each case :

- restart motion

- wait until reconnection

- trigger a new event

-- AngelCarpintero - 29 Sep 2007

I think in all cases of video loss a special event should be triggered, I think it will really be worth designating a new event option for this.

-- BobSaggeth - 29 Sep 2007

Since other people were also asking for a dead-camera event, I think I will support this idea. I sounds like a useful option. In the context of a surveillance system, it can help to make the whole system more robust.

-- JoergWeber - 29 Sep 2007

Discussion and Comments

Why define this critical section? Isn't it enought to just have a watchdog counter in the cnt struct. This counter is set to 10 in the motion_loop (and init).
In the "main" thread do something like:
for (i = 1; cnt_list[i]; i++) {
  if (cnt[i]->watchdog > 0) {
  } else {
    restart = 1
The only thing to be noticed here is that the watchdog variable needs to be a type that is written (set and decreased) in a single (or mutal) instruction in the cpu.

-- DagErlandsson - 12 Oct 2007

Yes Dag , your approach is easier than mine wink Let me change the patch.

-- AngelCarpintero - 14 Oct 2007

Added second version of patch following Dag approach.

-- AngelCarpintero - 15 Oct 2007

Any update on this? -- I'm currently forced to do wget to check if the USB camera is still responding, then kill and restart motion frown, sad smile

This isn't normal, but the camera sometimes disappears briefly (like once a week or 2 weeks) when there is a lot of interference (it's a 10m cable) and motion just sits there instead of trying to reopen the usb device.

-- RomanGaufman - 25 Nov 2007

-- AngelCarpintero - 04 Jan 2008
I Attachment Action Size Date Who Comment
watchdog-patch.diffdiff watchdog-patch.diff manage 5 K 12 Oct 2007 - 13:37 AngelCarpintero Proof of concept + signal lost detection
watchdog-patch2.diffdiff watchdog-patch2.diff manage 2 K 15 Oct 2007 - 00:41 AngelCarpintero Dag approach
Topic revision: r14 - 04 Jan 2008, AngelCarpintero
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Please do not email Kenneth for support questions (read why). Use the Support Requests page or join the Mailing List.
This website only use harmless session cookies. See Cookie Policy for details. By using this website you accept the use of these cookies.