[chronojump] webcam getDevices refactored



commit 1a2314cdd4ac60f2c27f7b8696b41ffae47303f9
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Mar 29 15:59:23 2019 +0100

    webcam getDevices refactored

 src/Makefile.am            |   1 +
 src/utilMultimedia.cs      |  31 ++------
 src/webcamFfmpeg.cs        |  99 ------------------------
 src/webcamFfmpegDevices.cs | 183 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 190 insertions(+), 124 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index f67c964d..a0ef601a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -219,6 +219,7 @@ SOURCES = \
        oldCodeNeedToDBConvert/sqlite/session.cs\
        webcam.cs\
        webcamFfmpeg.cs\
+       webcamFfmpegDevices.cs\
        webcamMplayer.cs
 
 RESOURCES = \
diff --git a/src/utilMultimedia.cs b/src/utilMultimedia.cs
index c3ef5d37..25e06160 100644
--- a/src/utilMultimedia.cs
+++ b/src/utilMultimedia.cs
@@ -50,38 +50,19 @@ public class UtilMultimedia
                return devicesStr;
                */
 
+               WebcamFfmpegGetDevices w;
 
-               //on Linux search for video0, video1, ...
                if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
-                       return GetVideoDevicesLinux();
+                       w = new WebcamFfmpegGetDevicesLinux();
                else if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.WINDOWS)
-                       return GetVideoDevicesWindows();
+                       w = new WebcamFfmpegGetDevicesWindows();
                else
-                       return new List<string>();
-       }
+                       //return new List<string>();
+                       w = new WebcamFfmpegGetDevicesMac();
 
-       public static List<string> GetVideoDevicesLinux ()
-       {
-               List<string> list = new List<string>();
-               string prefix = "/dev/";
-               var dir = new DirectoryInfo(prefix);
-               foreach(var file in dir.EnumerateFiles("video*"))
-                       /*
-                        * return 0, 1, ...
-                        if( file.Name.Length > 5 &&                            //there's something more than 
"video", like "video0" or "video1", ...
-                        char.IsNumber(file.Name, 5) )          //and it's a number
-                        list.Add(Convert.ToInt32(file.Name[5]));                       //0 or 1, or ...
-                        */
-                       //return "/dev/video0", "/dev/video1", ...
-                       list.Add(prefix + file.Name);
-
-               return list;
+               return w.GetDevices();
        }
 
-       public static List<string> GetVideoDevicesWindows ()
-       {
-               return WebcamFfmpegGetDevicesWindows.GetDevices();
-       }
 
        /*
         * IMAGES
diff --git a/src/webcamFfmpeg.cs b/src/webcamFfmpeg.cs
index 0f3519d7..c14e86ca 100644
--- a/src/webcamFfmpeg.cs
+++ b/src/webcamFfmpeg.cs
@@ -403,102 +403,3 @@ public class WebcamFfmpeg : Webcam
        }
 
 }
-
-
-public static class WebcamFfmpegGetDevicesWindows
-{
-       public static List<string> GetDevices()
-       {
-               string executable = "ffmpeg";
-               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.WINDOWS)
-                       executable = System.IO.Path.Combine(Util.GetPrefixDir(), "bin/ffmpeg.exe");
-
-               List<string> parameters = createParameters();
-
-               ExecuteProcess.Result execute_result = ExecuteProcess.run (executable, parameters, true, 
true);
-
-               LogB.Information("---- stdout: ----");
-               LogB.Information(execute_result.stdout);
-               LogB.Information("---- stderr: ----");
-               LogB.Information(execute_result.stderr);
-               LogB.Information("-----------------");
-
-               if(! execute_result.success)
-               {
-                       LogB.Information("WebcamFfmpegGetDevicesWindows error: " + execute_result.stderr);
-
-                       /*
-                        * on Windows the -i dummy produces an error, so stderr exists and success is false
-                        * stdout has the list of devices and stderr also
-                        * check if in stdout there's the: "DirectShow video devices" string and if not 
exists, really we have an error
-                        */
-                       if(execute_result.stdout != null && execute_result.stdout != "" &&
-                                       execute_result.stdout.Contains("DirectShow video devices"))
-                       {
-                               LogB.Information("Calling parse with stdout");
-                               return parse(execute_result.stdout);
-                       }
-
-                       if(execute_result.stderr != null && execute_result.stderr != "" &&
-                                       execute_result.stderr.Contains("DirectShow video devices"))
-                       {
-                               LogB.Information("Calling parse with stderr");
-                               return parse(execute_result.stderr);
-                       }
-
-                       return new List<string>();
-               }
-               else
-                       return parse(execute_result.stdout);
-       }
-
-       private static List<string> createParameters()
-       {
-               //ffmpeg -list_devices true -f dshow -i dummy
-               List<string> parameters = new List<string>();
-
-               int i = 0;
-               parameters.Insert (i ++, "-list_devices");
-               parameters.Insert (i ++, "true");
-               parameters.Insert (i ++, "-f");
-               parameters.Insert (i ++, "dshow");
-               parameters.Insert (i ++, "-i");
-               parameters.Insert (i ++, "dummy");
-
-               return parameters;
-       }
-
-       private static List<string> parse(string devicesOutput)
-       {
-               LogB.Information("Called parse");
-
-               /*
-                * break the big string in \n strings
-                * https://stackoverflow.com/a/1547483
-                */
-               string[] lines = devicesOutput.Split(
-                               new[] { Environment.NewLine },
-                               StringSplitOptions.None
-                               );
-
-               List<string> parsedList = new List<string>();
-               foreach(string l in lines)
-               {
-                       LogB.Information("line: " + l);
-                       foreach(Match match in Regex.Matches(l, "\"([^\"]*)\""))
-                       {
-                               //remove quotes from the match (at beginning and end) to add it in SQL
-                               string s = match.ToString().Substring(1, match.ToString().Length -2);
-
-                               LogB.Information("add match: " + s);
-                               parsedList.Add(s);
-                       }
-
-                       //after the list of video devices comes the list of audio devices, skip it
-                       if(l.Contains("DirectShow audio devices"))
-                               break;
-               }
-
-               return parsedList;
-       }
-}
diff --git a/src/webcamFfmpegDevices.cs b/src/webcamFfmpegDevices.cs
new file mode 100644
index 00000000..56162d2d
--- /dev/null
+++ b/src/webcamFfmpegDevices.cs
@@ -0,0 +1,183 @@
+/*
+ * 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) 2019   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System.Collections.Generic; //List
+using System.Diagnostics;
+using System;
+using System.IO;
+using System.Text.RegularExpressions; //Regex
+
+
+public abstract class WebcamFfmpegGetDevices
+{
+       public abstract List<string> GetDevices();
+
+       protected abstract List<string> createParameters();
+}
+
+public class WebcamFfmpegGetDevicesLinux : WebcamFfmpegGetDevices
+{
+       public WebcamFfmpegGetDevicesLinux()
+       {
+       }
+
+       public override List<string> GetDevices()
+       {
+               List<string> list = new List<string>();
+               string prefix = "/dev/";
+               var dir = new DirectoryInfo(prefix);
+               foreach(var file in dir.EnumerateFiles("video*"))
+                       /*
+                        * return 0, 1, ...
+                        if( file.Name.Length > 5 &&                            //there's something more than 
"video", like "video0" or "video1", ...
+                        char.IsNumber(file.Name, 5) )          //and it's a number
+                        list.Add(Convert.ToInt32(file.Name[5]));                       //0 or 1, or ...
+                        */
+                       //return "/dev/video0", "/dev/video1", ...
+                       list.Add(prefix + file.Name);
+
+               return list;
+       }
+
+       protected override List<string> createParameters()
+       {
+               return new List<string>();
+       }
+}
+
+
+public class WebcamFfmpegGetDevicesWindows : WebcamFfmpegGetDevices
+{
+       public WebcamFfmpegGetDevicesWindows()
+       {
+       }
+
+       public override List<string> GetDevices()
+       {
+               string executable = "ffmpeg";
+               if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.WINDOWS)
+                       executable = System.IO.Path.Combine(Util.GetPrefixDir(), "bin/ffmpeg.exe");
+
+               List<string> parameters = createParameters();
+
+               ExecuteProcess.Result execute_result = ExecuteProcess.run (executable, parameters, true, 
true);
+
+               LogB.Information("---- stdout: ----");
+               LogB.Information(execute_result.stdout);
+               LogB.Information("---- stderr: ----");
+               LogB.Information(execute_result.stderr);
+               LogB.Information("-----------------");
+
+               if(! execute_result.success)
+               {
+                       LogB.Information("WebcamFfmpegGetDevicesWindows error: " + execute_result.stderr);
+
+                       /*
+                        * on Windows the -i dummy produces an error, so stderr exists and success is false
+                        * stdout has the list of devices and stderr also
+                        * check if in stdout there's the: "DirectShow video devices" string and if not 
exists, really we have an error
+                        */
+                       if(execute_result.stdout != null && execute_result.stdout != "" &&
+                                       execute_result.stdout.Contains("DirectShow video devices"))
+                       {
+                               LogB.Information("Calling parse with stdout");
+                               return parse(execute_result.stdout);
+                       }
+
+                       if(execute_result.stderr != null && execute_result.stderr != "" &&
+                                       execute_result.stderr.Contains("DirectShow video devices"))
+                       {
+                               LogB.Information("Calling parse with stderr");
+                               return parse(execute_result.stderr);
+                       }
+
+                       return new List<string>();
+               }
+               else
+                       return parse(execute_result.stdout);
+       }
+
+       protected override List<string> createParameters()
+       {
+               //ffmpeg -list_devices true -f dshow -i dummy
+               List<string> parameters = new List<string>();
+
+               int i = 0;
+               parameters.Insert (i ++, "-list_devices");
+               parameters.Insert (i ++, "true");
+               parameters.Insert (i ++, "-f");
+               parameters.Insert (i ++, "dshow");
+               parameters.Insert (i ++, "-i");
+               parameters.Insert (i ++, "dummy");
+
+               return parameters;
+       }
+
+       private List<string> parse(string devicesOutput)
+       {
+               LogB.Information("Called parse");
+
+               /*
+                * break the big string in \n strings
+                * https://stackoverflow.com/a/1547483
+                */
+               string[] lines = devicesOutput.Split(
+                               new[] { Environment.NewLine },
+                               StringSplitOptions.None
+                               );
+
+               List<string> parsedList = new List<string>();
+               foreach(string l in lines)
+               {
+                       LogB.Information("line: " + l);
+                       foreach(Match match in Regex.Matches(l, "\"([^\"]*)\""))
+                       {
+                               //remove quotes from the match (at beginning and end) to add it in SQL
+                               string s = match.ToString().Substring(1, match.ToString().Length -2);
+
+                               LogB.Information("add match: " + s);
+                               parsedList.Add(s);
+                       }
+
+                       //after the list of video devices comes the list of audio devices, skip it
+                       if(l.Contains("DirectShow audio devices"))
+                               break;
+               }
+
+               return parsedList;
+       }
+}
+
+public class WebcamFfmpegGetDevicesMac : WebcamFfmpegGetDevices
+{
+       public WebcamFfmpegGetDevicesMac()
+       {
+       }
+
+       public override List<string> GetDevices()
+       {
+               return new List<string>();
+       }
+
+       protected override List<string> createParameters()
+       {
+               return new List<string>();
+       }
+}


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