2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace Handbrake.Parsing
\r
9 using System.Globalization;
\r
12 using System.Text.RegularExpressions;
\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
48 /// The Buffer StringBuilder
\r
50 private StringBuilder buffer = new StringBuilder(string.Empty);
\r
53 /// Initializes a new instance of the <see cref="Parser"/> class.
\r
54 /// Default constructor for this object
\r
56 /// <param name="baseStream">
\r
57 /// The stream to parse from
\r
59 public Parser(Stream baseStream) : base(baseStream)
\r
64 /// Raised upon a new line being read from stdout/stderr
\r
66 public event DataReadEventHandler OnReadLine;
\r
69 /// Raised upon the entire stdout/stderr stream being read in a single call
\r
71 public event DataReadEventHandler OnReadToEnd;
\r
74 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
76 public event ScanProgressEventHandler OnScanProgress;
\r
79 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
81 public event EncodeProgressEventHandler OnEncodeProgress;
\r
84 /// Gets the buffer of data that came from the CLI standard input/error
\r
86 public string Buffer
\r
88 get { return buffer.ToString(); }
\r
92 /// Read a line from standard in/err
\r
97 public override string ReadLine()
\r
99 string tmp = base.ReadLine();
\r
101 buffer.Append(tmp + Environment.NewLine);
\r
104 if (tmp.Contains("Scanning title"))
\r
105 m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");
\r
107 if (OnReadLine != null)
\r
108 OnReadLine(this, tmp);
\r
111 if (m.Success && OnScanProgress != null)
\r
112 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
\r
118 /// Read to the end of the input stream
\r
121 /// A string of the input data
\r
123 public override string ReadToEnd()
\r
125 string tmp = base.ReadToEnd();
\r
127 buffer.Append(tmp + Environment.NewLine);
\r
129 if (OnReadToEnd != null)
\r
130 OnReadToEnd(this, tmp);
\r
136 /// Pase the CLI status output (from standard output)
\r
138 public void ReadEncodeStatus()
\r
140 CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
\r
141 string tmp = base.ReadLine();
\r
143 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
144 if (m.Success && OnEncodeProgress != null)
\r
146 int currentTask = int.Parse(m.Groups[1].Value);
\r
147 int totalTasks = int.Parse(m.Groups[2].Value);
\r
148 float percent = float.Parse(m.Groups[3].Value, culture);
\r
149 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);
\r
150 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);
\r
151 TimeSpan remaining = TimeSpan.Zero;
\r
152 if (m.Groups[7].Value != string.Empty)
\r
154 remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);
\r
156 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);
\r