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
7 namespace Handbrake.Parsing
\r
10 using System.Globalization;
\r
13 using System.Text.RegularExpressions;
\r
16 /// A delegate to handle custom events regarding data being parsed from the buffer
\r
18 /// <param name="Sender">The object which raised this delegate</param>
\r
19 /// <param name="Data">The data parsed from the stream</param>
\r
20 public delegate void DataReadEventHandler(object Sender, string Data);
\r
23 /// A delegate to handle events regarding progress during DVD scanning
\r
25 /// <param name="Sender">The object who's raising the event</param>
\r
26 /// <param name="CurrentTitle">The title number currently being processed</param>
\r
27 /// <param name="TitleCount">The total number of titiles to be processed</param>
\r
28 public delegate void ScanProgressEventHandler(object Sender, int CurrentTitle, int TitleCount);
\r
31 /// A delegate to handle encode progress updates // EXPERIMENTAL
\r
33 /// <param name="Sender">The object which raised the event</param>
\r
34 /// <param name="CurrentTask">The current task being processed from the queue</param>
\r
35 /// <param name="TaskCount">The total number of tasks in queue</param>
\r
36 /// <param name="PercentComplete">The percentage this task is complete</param>
\r
37 /// <param name="CurrentFps">The current encoding fps</param>
\r
38 /// <param name="AverageFps">The average encoding fps for this task</param>
\r
39 /// <param name="TimeRemaining">The estimated time remaining for this task to complete</param>
\r
40 public delegate void EncodeProgressEventHandler(
\r
41 object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps,
\r
42 TimeSpan TimeRemaining);
\r
46 /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process
\r
48 internal class Parser : StreamReader
\r
50 private StringBuilder _buffer = new StringBuilder(string.Empty);
\r
53 /// The output from the CLI process
\r
55 public string Buffer
\r
57 get { return _buffer.ToString(); }
\r
61 /// Raised upon a new line being read from stdout/stderr
\r
63 public event DataReadEventHandler OnReadLine;
\r
66 /// Raised upon the entire stdout/stderr stream being read in a single call
\r
68 public event DataReadEventHandler OnReadToEnd;
\r
71 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
73 public event ScanProgressEventHandler OnScanProgress;
\r
75 #region Experimetnal Code
\r
78 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
80 public event EncodeProgressEventHandler OnEncodeProgress;
\r
85 /// Initializes a new instance of the <see cref="Parser"/> class.
\r
86 /// Default constructor for this object
\r
88 /// <param name="baseStream">
\r
89 /// The stream to parse from
\r
91 public Parser(Stream baseStream)
\r
96 public override string ReadLine()
\r
98 string tmp = base.ReadLine();
\r
100 _buffer.Append(tmp + Environment.NewLine);
\r
103 if (tmp.Contains("Scanning title"))
\r
104 m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");
\r
106 if (OnReadLine != null)
\r
107 OnReadLine(this, tmp);
\r
110 if (m.Success && OnScanProgress != null)
\r
111 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
\r
116 public override string ReadToEnd()
\r
118 string tmp = base.ReadToEnd();
\r
120 _buffer.Append(tmp + Environment.NewLine);
\r
122 if (OnReadToEnd != null)
\r
123 OnReadToEnd(this, tmp);
\r
129 /// Pase the CLI status output (from standard output)
\r
131 public void readEncodeStatus()
\r
133 CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
\r
134 string tmp = base.ReadLine();
\r
136 Match m = Regex.Match(tmp,
\r
137 @"^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
138 if (m.Success && OnEncodeProgress != null)
\r
140 int currentTask = int.Parse(m.Groups[1].Value);
\r
141 int totalTasks = int.Parse(m.Groups[2].Value);
\r
142 float percent = float.Parse(m.Groups[3].Value, culture);
\r
143 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);
\r
144 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);
\r
145 TimeSpan remaining = TimeSpan.Zero;
\r
146 if (m.Groups[7].Value != string.Empty)
\r
148 remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);
\r
150 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);
\r