3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Text.RegularExpressions;
\r
10 using System.Globalization;
\r
12 namespace Handbrake.Parsing
\r
15 /// A delegate to handle custom events regarding data being parsed from the buffer
\r
17 /// <param name="Sender">The object which raised this delegate</param>
\r
18 /// <param name="Data">The data parsed from the stream</param>
\r
19 public delegate void DataReadEventHandler(object Sender, string Data);
\r
22 /// A delegate to handle events regarding progress during DVD scanning
\r
24 /// <param name="Sender">The object who's raising the event</param>
\r
25 /// <param name="CurrentTitle">The title number currently being processed</param>
\r
26 /// <param name="TitleCount">The total number of titiles to be processed</param>
\r
27 public delegate void ScanProgressEventHandler(object Sender, int CurrentTitle, int TitleCount);
\r
30 /// A delegate to handle encode progress updates // EXPERIMENTAL
\r
32 /// <param name="Sender">The object which raised the event</param>
\r
33 /// <param name="CurrentTask">The current task being processed from the queue</param>
\r
34 /// <param name="TaskCount">The total number of tasks in queue</param>
\r
35 /// <param name="PercentComplete">The percentage this task is complete</param>
\r
36 /// <param name="CurrentFps">The current encoding fps</param>
\r
37 /// <param name="AverageFps">The average encoding fps for this task</param>
\r
38 /// <param name="TimeRemaining">The estimated time remaining for this task to complete</param>
\r
39 public delegate void EncodeProgressEventHandler(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining);
\r
43 /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process
\r
45 internal class Parser : StreamReader
\r
47 private string m_buffer;
\r
49 /// The output from the CLI process
\r
51 public string Buffer
\r
60 /// Raised upon a new line being read from stdout/stderr
\r
62 public event DataReadEventHandler OnReadLine;
\r
65 /// Raised upon the entire stdout/stderr stream being read in a single call
\r
67 public event DataReadEventHandler OnReadToEnd;
\r
70 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
72 public event ScanProgressEventHandler OnScanProgress;
\r
74 #region Experimetnal Code
\r
76 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
78 public event EncodeProgressEventHandler OnEncodeProgress;
\r
82 /// Default constructor for this object
\r
84 /// <param name="baseStream">The stream to parse from</param>
\r
85 public Parser(Stream baseStream) : base(baseStream)
\r
87 m_buffer = string.Empty;
\r
90 public override string ReadLine()
\r
92 string tmp = base.ReadLine();
\r
95 Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");
\r
96 if (OnReadLine != null)
\r
97 OnReadLine(this, tmp);
\r
99 if (m.Success && OnScanProgress != null)
\r
100 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
\r
105 public override string ReadToEnd()
\r
107 string tmp = base.ReadToEnd();
\r
110 if (OnReadToEnd != null)
\r
111 OnReadToEnd(this, tmp);
\r
117 /// Pase the CLI status output (from standard output)
\r
119 public void readEncodeStatus()
\r
121 CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
\r
122 string tmp = base.ReadLine();
\r
124 Match m = Regex.Match(tmp, @"^Encoding: task ([0-9]*) of ([0-9]*), ([0-9]*\.[0-9]*) %( \(([0-9]*\.[0-9]*) fps, avg ([0-9]*\.[0-9]*) fps, ETA ([0-9]{2})h([0-9]{2})m([0-9]{2})s\))?");
\r
125 if (m.Success && OnEncodeProgress != null)
\r
127 int currentTask = int.Parse(m.Groups[1].Value);
\r
128 int totalTasks = int.Parse(m.Groups[2].Value);
\r
129 float percent = float.Parse(m.Groups[3].Value, culture);
\r
130 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);
\r
131 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);
\r
132 TimeSpan remaining = TimeSpan.Zero;
\r
133 if (m.Groups[7].Value != string.Empty)
\r
135 remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);
\r
137 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);
\r