[chronojump] webcamSupportedModes with sort resolution and framerate (WIP)



commit 47194b8c32ca1462314cc072100647b13f598fb4
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Jun 25 18:31:33 2019 +0200

    webcamSupportedModes with sort resolution and framerate (WIP)

 src/webcamFfmpegSupportedModes.cs | 165 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 163 insertions(+), 2 deletions(-)
---
diff --git a/src/webcamFfmpegSupportedModes.cs b/src/webcamFfmpegSupportedModes.cs
index fc071612..8583026d 100644
--- a/src/webcamFfmpegSupportedModes.cs
+++ b/src/webcamFfmpegSupportedModes.cs
@@ -86,6 +86,9 @@ public class WebcamFfmpegSupportedModesLinux : WebcamFfmpegSupportedModes
                                );
 
                bool foundAtLeastOne = false;
+
+               WebcamSupportedModesList wsmList = new WebcamSupportedModesList();
+               WebcamSupportedMode currentMode = null;
                foreach(string l in lines)
                {
                        LogB.Information("line: " + l);
@@ -96,19 +99,35 @@ public class WebcamFfmpegSupportedModesLinux : WebcamFfmpegSupportedModes
                                continue;
                        }
 
-                       if(l.Contains("Size: Discrete") && matchResolution(l) != "")
-                               parsedAll += "\n" + matchResolution(l) + ": ";
+                       string resolutionStr = matchResolution(l);
+                       if(l.Contains("Size: Discrete") && resolutionStr != "")
+                       {
+                               parsedAll += "\n" + resolutionStr + ": ";
+
+                               if(wsmList.ModeExist(resolutionStr))
+                                       currentMode = wsmList.GetMode(resolutionStr);
+                               else {
+                                       currentMode = new WebcamSupportedMode(resolutionStr);
+                                       wsmList.Add(currentMode);
+                               }
+                       }
 
                        if(l.Contains("Interval: Discrete") && l.Contains("fps") && matchFPS(l) != "")
                        {
                                parsedAll += "\t" + matchFPS(l);
                                foundAtLeastOne = true;
+
+                               if(currentMode != null)
+                                       currentMode.AddFramerate(matchFPS(l));
                        }
                }
 
                if(! foundAtLeastOne)
                        return "Not found any mode supported for your camera.";
 
+               wsmList.Sort();
+               LogB.Information("printing list:\n" + wsmList.ToString());
+
                return parsedAll;
        }
 
@@ -365,3 +384,145 @@ public class WebcamFfmpegSupportedModesMac : WebcamFfmpegSupportedModes
 [avfoundation @ 0x7f849a8be800]   1280x720@[14.999993 14.999993]fps
 0: Input/output error";
 }
+
+public class WebcamSupportedModesList
+{
+       List<WebcamSupportedMode> l;
+       public WebcamSupportedModesList ()
+       {
+               l = new List<WebcamSupportedMode>();
+       }
+
+       public bool ModeExist (string resolutionStr)
+       {
+               WebcamSupportedMode wsmNew = new WebcamSupportedMode(resolutionStr);
+               foreach (WebcamSupportedMode wsm in l)
+                       if(wsm.ResolutionWidth == wsmNew.ResolutionWidth &&
+                                       wsm.ResolutionHeight == wsmNew.ResolutionHeight)
+                               return true;
+
+               return false;
+       }
+
+       //used if ModeExist()
+       public WebcamSupportedMode GetMode (string resolutionStr)
+       {
+               WebcamSupportedMode wsmNew = new WebcamSupportedMode(resolutionStr);
+               foreach (WebcamSupportedMode wsm in l)
+                       if(wsm.ResolutionWidth == wsmNew.ResolutionWidth &&
+                                       wsm.ResolutionHeight == wsmNew.ResolutionHeight)
+                               return wsm;
+
+               return null;
+       }
+
+       public void Add (WebcamSupportedMode wsm)
+       {
+               l.Add(wsm);
+       }
+
+       public void Sort()
+       {
+               WebcamSupportedModeSort wsms = new WebcamSupportedModeSort();
+               l.Sort(wsms);
+       }
+
+       public override string ToString()
+       {
+               string str = "";
+               foreach(WebcamSupportedMode wsm in l)
+                       str += wsm.ToString() + "\n";
+
+               return str;
+       }
+
+}
+
+//https://www.geeksforgeeks.org/how-to-sort-list-in-c-sharp-set-1/
+//example 2
+public class WebcamSupportedModeSort : IComparer<WebcamSupportedMode>
+{
+       public int Compare(WebcamSupportedMode x, WebcamSupportedMode y)
+       {
+               if(x == null || y == null)
+                       return 0;
+
+               return (x.Size).CompareTo(y.Size);
+       }
+}
+public class WebcamSupportedModeSortFramerates : IComparer<string>
+{
+       public int Compare(string x, string y)
+       {
+               if(x == null || y == null)
+                       return 0;
+
+               return 
Convert.ToDouble(Util.ChangeDecimalSeparator(x)).CompareTo(Convert.ToDouble(Util.ChangeDecimalSeparator(y)));
+       }
+}
+
+public class WebcamSupportedMode
+{
+       int resolutionWidth;
+       int resolutionHeight;
+       List<string> framerates;
+
+       public WebcamSupportedMode (string resolutionStr)
+       {
+               string [] strFull = resolutionStr.Split(new char[] {'x'});
+               if(strFull.Length == 2)
+               {
+                       this.resolutionWidth = Convert.ToInt32(strFull[0]);
+                       this.resolutionHeight = Convert.ToInt32(strFull[1]);
+               }
+               framerates = new List<string>();
+       }
+
+       public void AddFramerate (string f)
+       {
+               framerates.Add(f);
+       }
+
+       public override string ToString()
+       {
+               string str = string.Format("{0}x{1}: ", resolutionWidth, resolutionHeight);
+
+               /*
+                * unsorted:
+               foreach (string framerate in framerates)
+                       str += string.Format("\t{0}", framerate);
+               */
+
+               //"\nSorting:";
+               WebcamSupportedModeSortFramerates wsmsf = new WebcamSupportedModeSortFramerates();
+               framerates.Sort(wsmsf);
+               foreach (string framerate in framerates)
+                       str += string.Format("\t{0}", framerate);
+
+               return str;
+       }
+
+       public int CompareTo( WebcamSupportedMode that )
+       {
+               if ( that == null ) return 1;
+               if ( this.Size > that.Size) return 1;
+               if ( this.Size < that.Size) return -1;
+               return 0;
+       }
+
+       public int ResolutionWidth
+       {
+               get { return resolutionWidth; }
+       }
+       public int ResolutionHeight
+       {
+               get { return resolutionHeight; }
+       }
+
+       public int Size
+       {
+               get { return resolutionWidth * resolutionHeight; }
+       }
+
+       ~WebcamSupportedMode() {}
+}


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