[chronojump] webcam code is abstract and current code is at inherited webcamMplayer class



commit ce293dc07820d6f2ad16844756b6f81870e91361
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Jul 27 18:48:42 2018 +0200

    webcam code is abstract and current code is at inherited webcamMplayer class

 po/POTFILES.in        |   1 +
 src/Makefile.am       |   3 +-
 src/constants.cs      |   4 -
 src/gui/chronojump.cs |   4 +-
 src/gui/event.cs      |   2 +-
 src/gui/person.cs     |   2 +-
 src/webcam.cs         | 225 +++-------------------------------------
 src/webcamMplayer.cs  | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 295 insertions(+), 222 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 2aa6fa80..a25f1352 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -155,3 +155,4 @@ src/treeViewPerson.cs
 src/treeViewPulse.cs
 src/treeViewReactionTime.cs
 src/treeViewRun.cs
+src/webcamMplayer.cs
diff --git a/src/Makefile.am b/src/Makefile.am
index 46f6aff2..a0aa7f45 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -215,7 +215,8 @@ SOURCES = \
        oldCodeNeedToDBConvert/sqlite/person.cs\
        oldCodeNeedToDBConvert/sqlite/personSession.cs\
        oldCodeNeedToDBConvert/sqlite/session.cs\
-       webcam.cs
+       webcam.cs\
+       webcamMplayer.cs
 
 RESOURCES = \
        ../glade/app1.glade \
diff --git a/src/constants.cs b/src/constants.cs
index 5c91806d..ca899d8b 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -540,10 +540,6 @@ public class Constants
        public static string FindDriverNeed = Catalog.GetString("Chronopic driver has to be installed.");
        public static string FindDriverWindows = Catalog.GetString("If you have problems connecting with 
Chronopic, ensure you have the <b>driver</b> installed at 'Windows Start Menu / Chronojump / Install 
Chronopic driver'."); 
        public static string FindDriverOthers = Catalog.GetString("Check Chronojump software website.");
-       public static string MplayerNotInstalled = string.Format(Catalog.GetString("Error. {0} is not 
installed."), "mplayer");
-       public static string FfmpegNotInstalled = string.Format(Catalog.GetString("Error. {0} is not 
installed."), "ffmpeg");
-       public static string MplayerClosed = string.Format(Catalog.GetString("Error. {0} has been closed."), 
"mplayer");
-       public static string MplayerCannotSave = string.Format(Catalog.GetString("Error. {0} cannot save 
video."), "mplayer");
        public static string VideoNothingCaptured = Catalog.GetString("Error. Nothing has been captured.");
 
 //     public static System.Media.SystemSound SoundCanStart = System.Media.SystemSounds.Question; 
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 306a0faf..23063935 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -4269,7 +4269,7 @@ public partial class ChronoJumpWindow
                //w = new Webcam(preferences.videoDevice);
                LogB.Information("wRS at gui chronojump.cs 0, videoDevice: " + videoDevice);
 
-               w = new Webcam(videoDevice);
+               w = new WebcamMplayer (videoDevice);
                Webcam.Result result = w.CapturePrepare (Webcam.CaptureTypes.VIDEO);
 
                LogB.Information("wRS at gui chronojump.cs 1, videoDevice: " + videoDevice);
@@ -6086,7 +6086,7 @@ LogB.Debug("mc finished 5");
        //Not used on encoder   
        private bool playVideo(string fileName, bool play) 
        {
-               webcam = new Webcam();
+               webcam = new WebcamMplayer ();
                Webcam.Result result = webcam.Play(fileName);
 
                /*
diff --git a/src/gui/event.cs b/src/gui/event.cs
index 6473d4a3..6909a126 100644
--- a/src/gui/event.cs
+++ b/src/gui/event.cs
@@ -315,7 +315,7 @@ public class EditEventWindow
                {
                        LogB.Information("Exists and clicked " + videoFileName);
 
-                       Webcam webcam = new Webcam();
+                       Webcam webcam = new WebcamMplayer ();
                        Webcam.Result result = webcam.Play(videoFileName);
                }
        }
diff --git a/src/gui/person.cs b/src/gui/person.cs
index da0cfd0b..06f310fd 100644
--- a/src/gui/person.cs
+++ b/src/gui/person.cs
@@ -1081,7 +1081,7 @@ public class PersonAddModifyWindow
                }
 
                // B) start if it's not running
-               webcam = new Webcam(videoDevice);
+               webcam = new WebcamMplayer (videoDevice);
                Webcam.Result result = webcam.CapturePrepare (Webcam.CaptureTypes.PHOTO);
                if (! result.success)
                {
diff --git a/src/webcam.cs b/src/webcam.cs
index 49df2db6..f1d11dc5 100644
--- a/src/webcam.cs
+++ b/src/webcam.cs
@@ -24,13 +24,12 @@ using System;
 using System.IO;
 
 //todo separate in different classes (inherited)
-class Webcam
+public abstract class Webcam
 {
        public bool Running;
 
-       private Process process;
-       private StreamWriter streamWriter;
-       private string videoDevice;
+       protected Process process;
+       protected string videoDevice;
 
        // Result struct holds the output, error and success operations. It's used to pass
        // errors from different layers (e.g. executing Python scripts) to the UI layer
@@ -61,6 +60,7 @@ class Webcam
        /*
         * constructor for play
         */
+
        public Webcam()
        {
        }
@@ -71,228 +71,27 @@ class Webcam
 
        public enum CaptureTypes { PHOTO, VIDEO }
 
-       public Result CapturePrepare (CaptureTypes captureType)
-       {
-               if(process != null)
-                       return new Result (false, "");
-
-               string tempFile = Util.GetMplayerPhotoTempFileNamePost(videoDeviceToFilename());
-               Util.FileDelete(tempFile);
-
-               string executable = "mplayer";
-               List<string> parameters = new List<string>();
-               //-noborder -nosound -tv 
driver=v4l2:gain=1:width=400:height=400:device=/dev/video0:fps=10:outfmt=rgb16 tv:// -vf 
screenshot=/tmp/chronojump-last-photo
-               //parameters.Insert (0, "-noborder"); //on X11 can be: title "Chronojump"". -noborder makes 
no accept 's', or 'q'
-
-               int i = 0;
-               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
-               {
-                       parameters.Insert (i ++, "-title"); //on X11 can be: title "Chronojump"". -noborder 
makes no accept 's', or 'q'
-                       if(captureType == CaptureTypes.PHOTO)
-                               parameters.Insert (i ++, "Chronojump snapshot");
-                       else //if(captureType == CaptureTypes.VIDEO)
-                               parameters.Insert (i ++, "Chronojump video record");
-               } else
-                       parameters.Insert (i ++, "-noborder");
-
-               parameters.Insert (i ++, "-nosound");
-               parameters.Insert (i ++, "-tv");
-               parameters.Insert (i ++, "driver=v4l2:gain=1:width=400:height=400:device=" + videoDevice + 
":fps=10:outfmt=rgb16");
-               parameters.Insert (i ++, "tv://");
-               parameters.Insert (i ++, "-vf");
-               parameters.Insert (i ++, "screenshot=" + 
Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()));
-
-               process = new Process();
-               bool success = ExecuteProcess.RunAtBackground (process, executable, parameters, true); 
//redirectInput
-               if(! success)
-               {
-                       streamWriter = null;
-                       process = null;
-                       return new Result (false, "", Constants.MplayerNotInstalled);
-               }
-
-               /*
-                * experimental double camera start
-                */
-               /*
-               List<string> parametersB = parameters;
-               parametersB[4] = 
"driver=v4l2:gain=1:width=400:height=400:device=/dev/video1:fps=10:outfmt=rgb16";
-               parametersB[7] = "screenshot=/tmp/b/chronojump-last-photo";
-               Process processB = new Process();
-               ExecuteProcess.RunAtBackground (processB, executable, parametersB, true); //redirectInput
-               */
-               /*
-                * experimental double camera end
-                */
-
-
-               streamWriter = process.StandardInput;
-
-               Running = true;
-               return new Result (true, "");
-       }
-
-       public Result Play(string filename)
-       {
-               if(process != null || filename == "")
-                       return new Result (false, "");
-
-               string executable = "mplayer";
-               List<string> parameters = new List<string>();
-               parameters.Insert (0, filename);
-               //parameters.Insert (0, "-noborder"); //on X11 can be: title "Chronojump"". -noborder makes 
no accept 's', or 'q'
-               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
-               {
-                       parameters.Insert (0, "-title"); //on X11 can be: title "Chronojump"". -noborder 
makes no accept 's', or 'q'
-                       parameters.Insert (1, "Chronojump video");
-               } else
-                       parameters.Insert (0, "-noborder");
-
+       public abstract Result CapturePrepare (CaptureTypes captureType);
 
-               process = new Process();
-               bool success = ExecuteProcess.RunAtBackground (process, executable, parameters, false);
-               if(! success)
-               {
-                       process = null;
-                       return new Result (false, "", Constants.MplayerNotInstalled);
-               }
+       public abstract Result Play(string filename);
 
-               Running = true;
-               return new Result (true, "");
-       }
-
-
-       public bool Snapshot()
-       {
-               if(process == null || streamWriter == null)
-                       return false;
-
-               bool exitAtFirstSnapshot = true;
-
-               if(! snapshotDo())
-                       return false;
-
-               if(exitAtFirstSnapshot)
-               {
-                       streamWriter.Flush();
-                       System.Threading.Thread.Sleep(100);
-                       ExitCamera();
-               }
-               return true;
-       }
-
-       public Result VideoCaptureStart()
-       {
-               if(process == null || streamWriter == null)
-                       return new Result (false, "", Constants.MplayerClosed);
+       public abstract bool Snapshot();
 
-               if(! recordStartOrEndDo())
-                       return new Result (false, "", Constants.MplayerCannotSave);
-
-               return new Result (true, "");
-       }
+       public abstract Result VideoCaptureStart();
 
        //short process, to do end capture (good if there's more than one camera to end capture all at same 
time)
-       public Result VideoCaptureEnd()
-       {
-               if(process == null || streamWriter == null)
-                       return new Result (false, "", Constants.MplayerClosed);
-
-               //System.Threading.Thread.Sleep(2000); //TODO: play with this to see if cut better video at 
end
-               if(! recordStartOrEndDo())
-                       return new Result (false, "", Constants.MplayerCannotSave);
-
-               return new Result (true, "");
-       }
-
-       public Result ExitAndFinish (int sessionID, Constants.TestTypes testType, int testID)
-       {
-               ExitCamera();
-
-               if(! findIfThereAreImagesToConvert())
-                       return new Result (false, "", Constants.VideoNothingCaptured);
+       public abstract Result VideoCaptureEnd();
 
-               //Convert video to the name and format expected
-               if(! convertImagesToVideo())
-                       return new Result (false, "", Constants.FfmpegNotInstalled);
+       public abstract Result ExitAndFinish (int sessionID, Constants.TestTypes testType, int testID);
 
-               //Copy the video to expected place
-               if (! Util.CopyTempVideo(sessionID, testType, testID))
-                       return new Result (false, "", Constants.FileCopyProblem);
-
-               //Delete temp photos and video
-               Util.DeleteTempPhotosAndVideo(videoDeviceToFilename());
-
-               return new Result (true, "");
-       }
-
-       public void ExitCamera()
-       {
-               try {
-                       streamWriter.Write('q');
-                       streamWriter.Flush();
-               } catch {
-                       //maybe Mplayer window has been closed by user
-                       process = null;
-                       Running = false;
-                       return;
-               }
-               System.Threading.Thread.Sleep(100);
-
-               streamWriter = null;
-               process = null;
-               Running = false;
-       }
+       public abstract void ExitCamera();
 
        /*
         * private methods
         */
 
-       private bool snapshotDo()
-       {
-               try {
-                       streamWriter.Write('s');
-               } catch {
-                       //maybe Mplayer window has been closed by user
-                       return false;
-               }
-               return true;
-       }
-
-       private bool recordStartOrEndDo()
-       {
-               try {
-                       streamWriter.Write('S');
-               } catch {
-                       //maybe Mplayer window has been closed by user
-                       return false;
-               }
-               return true;
-       }
-
-       private bool findIfThereAreImagesToConvert()
-       {
-               return (File.Exists(Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()) + 
"0001.png"));
-       }
-
-       private bool convertImagesToVideo()
-       {
-               string executable = "ffmpeg";
-               List<string> parameters = new List<string>();
-               //ffmpeg -framerate 20 -y -i chronojump-last-photo%04d.png output.mp4
-               parameters.Insert (0, "-framerate");
-               parameters.Insert (1, "20");
-               parameters.Insert (2, "-y"); //force overwrite without asking
-               parameters.Insert (3, "-i"); //input files
-               parameters.Insert (4, Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()) + 
"%04d.png");
-               parameters.Insert (5, Util.GetVideoTempFileName());
-
-               ExecuteProcess.Result execute_result = ExecuteProcess.run (executable, parameters);
-               return execute_result.success;
-       }
-
        // convert /dev/video0 to _dev_video0
-       private string videoDeviceToFilename()
+       protected string videoDeviceToFilename()
        {
                return Util.ChangeChars(videoDevice, "/", "_");
        }
diff --git a/src/webcamMplayer.cs b/src/webcamMplayer.cs
new file mode 100644
index 00000000..029e37a9
--- /dev/null
+++ b/src/webcamMplayer.cs
@@ -0,0 +1,276 @@
+//Remember to add to POTFILES.in
+
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or   
+ * (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  Copyright (C) 2018   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System.Collections.Generic; //List
+using System.Diagnostics;
+using System;
+using System.IO;
+using Mono.Unix;
+
+public class WebcamMplayer : Webcam
+{
+       //messages
+       private static string programMplayerNotInstalled =
+               string.Format(Catalog.GetString("Error. {0} is not installed."), "mplayer");
+       private static string programFfmpegNotInstalled =
+               string.Format(Catalog.GetString("Error. {0} is not installed."), "ffmpeg");
+       private static string programMplayerClosed =
+               string.Format(Catalog.GetString("Error. {0} has been closed."), "mplayer");
+       private static string programMplayerCannotSave =
+               string.Format(Catalog.GetString("Error. {0} cannot save video."), "mplayer");
+       
+       private StreamWriter streamWriter;
+
+       public WebcamMplayer (string videoDevice)
+       {
+               this.videoDevice = videoDevice;
+               Running = false;
+       }
+
+       /*
+        * constructor for play
+        */
+
+       public WebcamMplayer ()
+       {
+       }
+
+       public override Result CapturePrepare (CaptureTypes captureType)
+       {
+               if(process != null)
+                       return new Result (false, "");
+
+               string tempFile = Util.GetMplayerPhotoTempFileNamePost(videoDeviceToFilename());
+               Util.FileDelete(tempFile);
+
+               string executable = "mplayer";
+               List<string> parameters = new List<string>();
+               //-noborder -nosound -tv 
driver=v4l2:gain=1:width=400:height=400:device=/dev/video0:fps=10:outfmt=rgb16 tv:// -vf 
screenshot=/tmp/chronojump-last-photo
+               //parameters.Insert (0, "-noborder"); //on X11 can be: title "Chronojump"". -noborder makes 
no accept 's', or 'q'
+
+               int i = 0;
+               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
+               {
+                       parameters.Insert (i ++, "-title"); //on X11 can be: title "Chronojump"". -noborder 
makes no accept 's', or 'q'
+                       if(captureType == CaptureTypes.PHOTO)
+                               parameters.Insert (i ++, "Chronojump snapshot");
+                       else //if(captureType == CaptureTypes.VIDEO)
+                               parameters.Insert (i ++, "Chronojump video record");
+               } else
+                       parameters.Insert (i ++, "-noborder");
+
+               parameters.Insert (i ++, "-nosound");
+               parameters.Insert (i ++, "-tv");
+               parameters.Insert (i ++, "driver=v4l2:gain=1:width=400:height=400:device=" + videoDevice + 
":fps=10:outfmt=rgb16");
+               parameters.Insert (i ++, "tv://");
+               parameters.Insert (i ++, "-vf");
+               parameters.Insert (i ++, "screenshot=" + 
Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()));
+
+               process = new Process();
+               bool success = ExecuteProcess.RunAtBackground (process, executable, parameters, true); 
//redirectInput
+               if(! success)
+               {
+                       streamWriter = null;
+                       process = null;
+                       return new Result (false, "", programMplayerNotInstalled);
+               }
+
+               /*
+                * experimental double camera start
+                */
+               /*
+                  List<string> parametersB = parameters;
+                  parametersB[4] = 
"driver=v4l2:gain=1:width=400:height=400:device=/dev/video1:fps=10:outfmt=rgb16";
+                  parametersB[7] = "screenshot=/tmp/b/chronojump-last-photo";
+                  Process processB = new Process();
+                  ExecuteProcess.RunAtBackground (processB, executable, parametersB, true); //redirectInput
+                  */
+               /*
+                * experimental double camera end
+                */
+
+
+               streamWriter = process.StandardInput;
+
+               Running = true;
+               return new Result (true, "");
+       }
+
+       public override Result Play(string filename)
+       {
+               if(process != null || filename == "")
+                       return new Result (false, "");
+
+               string executable = "mplayer";
+               List<string> parameters = new List<string>();
+               parameters.Insert (0, filename);
+               //parameters.Insert (0, "-noborder"); //on X11 can be: title "Chronojump"". -noborder makes 
no accept 's', or 'q'
+               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
+               {
+                       parameters.Insert (0, "-title"); //on X11 can be: title "Chronojump"". -noborder 
makes no accept 's', or 'q'
+                       parameters.Insert (1, "Chronojump video");
+               } else
+                       parameters.Insert (0, "-noborder");
+
+
+               process = new Process();
+               bool success = ExecuteProcess.RunAtBackground (process, executable, parameters, false);
+               if(! success)
+               {
+                       process = null;
+                       return new Result (false, "", programMplayerNotInstalled);
+               }
+
+               Running = true;
+               return new Result (true, "");
+       }
+
+       public override bool Snapshot()
+       {
+               if(process == null || streamWriter == null)
+                       return false;
+
+               bool exitAtFirstSnapshot = true;
+
+               if(! snapshotDo())
+                       return false;
+
+               if(exitAtFirstSnapshot)
+               {
+                       streamWriter.Flush();
+                       System.Threading.Thread.Sleep(100);
+                       ExitCamera();
+               }
+               return true;
+       }
+
+       public override Result VideoCaptureStart()
+       {
+               if(process == null || streamWriter == null)
+                       return new Result (false, "", programMplayerClosed);
+
+               if(! recordStartOrEndDo())
+                       return new Result (false, "", programMplayerCannotSave);
+
+               return new Result (true, "");
+       }
+
+       public override Result VideoCaptureEnd()
+       {
+               if(process == null || streamWriter == null)
+                       return new Result (false, "", programMplayerClosed);
+
+               //System.Threading.Thread.Sleep(2000); //TODO: play with this to see if cut better video at 
end
+               if(! recordStartOrEndDo())
+                       return new Result (false, "", programMplayerCannotSave);
+
+               return new Result (true, "");
+       }
+
+
+       public override Result ExitAndFinish (int sessionID, Constants.TestTypes testType, int testID)
+       {
+               ExitCamera();
+
+               if(! findIfThereAreImagesToConvert())
+                       return new Result (false, "", Constants.VideoNothingCaptured);
+
+               //Convert video to the name and format expected
+               if(! convertImagesToVideo())
+                       return new Result (false, "", programFfmpegNotInstalled);
+
+               //Copy the video to expected place
+               if (! Util.CopyTempVideo(sessionID, testType, testID))
+                       return new Result (false, "", Constants.FileCopyProblem);
+
+               //Delete temp photos and video
+               Util.DeleteTempPhotosAndVideo(videoDeviceToFilename());
+
+               return new Result (true, "");
+       }
+
+       public override void ExitCamera()
+       {
+               try {
+                       streamWriter.Write('q');
+                       streamWriter.Flush();
+               } catch {
+                       //maybe Mplayer window has been closed by user
+                       process = null;
+                       Running = false;
+                       return;
+               }
+               System.Threading.Thread.Sleep(100);
+
+               streamWriter = null;
+               process = null;
+               Running = false;
+       }
+
+       /*
+        * private methods
+        */
+
+       private bool snapshotDo()
+       {
+               try {
+                       streamWriter.Write('s');
+               } catch {
+                       //maybe Mplayer window has been closed by user
+                       return false;
+               }
+               return true;
+       }
+
+       private bool recordStartOrEndDo()
+       {
+               try {
+                       streamWriter.Write('S');
+               } catch {
+                       //maybe Mplayer window has been closed by user
+                       return false;
+               }
+               return true;
+       }
+
+       private bool findIfThereAreImagesToConvert()
+       {
+               return (File.Exists(Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()) + 
"0001.png"));
+       }
+
+       private bool convertImagesToVideo()
+       {
+               string executable = "ffmpeg";
+               List<string> parameters = new List<string>();
+               //ffmpeg -framerate 20 -y -i chronojump-last-photo%04d.png output.mp4
+               parameters.Insert (0, "-framerate");
+               parameters.Insert (1, "20");
+               parameters.Insert (2, "-y"); //force overwrite without asking
+               parameters.Insert (3, "-i"); //input files
+               parameters.Insert (4, Util.GetMplayerPhotoTempFileNamePre(videoDeviceToFilename()) + 
"%04d.png");
+               parameters.Insert (5, Util.GetVideoTempFileName());
+
+               ExecuteProcess.Result execute_result = ExecuteProcess.run (executable, parameters);
+               return execute_result.success;
+       }
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]