// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// A delegate to handle custom events regarding data being parsed from the buffer
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.ApplicationServices.Parsing
{
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
///
/// A delegate to handle custom events regarding data being parsed from the buffer
///
/// The object which raised this delegate
/// The data parsed from the stream
public delegate void DataReadEventHandler(object sender, string data);
///
/// A delegate to handle events regarding progress during DVD scanning
///
/// The object who's raising the event
/// The title number currently being processed
/// The total number of titiles to be processed
public delegate void ScanProgressEventHandler(object sender, int currentTitle, int titleCount);
///
/// A delegate to handle encode progress updates // EXPERIMENTAL
///
/// The object which raised the event
/// The current task being processed from the queue
/// The total number of tasks in queue
/// The percentage this task is complete
/// The current encoding fps
/// The average encoding fps for this task
/// The estimated time remaining for this task to complete
public delegate void EncodeProgressEventHandler(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float averageFps, string timeRemaining);
///
/// A simple wrapper around a StreamReader to keep track of the entire output from a cli process
///
public class Parser : StreamReader
{
///
/// The Buffer StringBuilder
///
private readonly StringBuilder buffer = new StringBuilder(string.Empty);
///
/// Initializes a new instance of the class.
/// Default constructor for this object
///
///
/// The stream to parse from
///
public Parser(Stream baseStream) : base(baseStream, Encoding.Default)
{
}
///
/// Raised upon a new line being read from stdout/stderr
///
public event DataReadEventHandler OnReadLine;
///
/// Raised upon the entire stdout/stderr stream being read in a single call
///
public event DataReadEventHandler OnReadToEnd;
///
/// Raised upon the catching of a "Scanning title # of #..." in the stream
///
public event ScanProgressEventHandler OnScanProgress;
///
/// Raised upon the catching of a "Scanning title # of #..." in the stream
///
public event EncodeProgressEventHandler OnEncodeProgress;
///
/// Gets the buffer of data that came from the CLI standard input/error
///
public StringBuilder Buffer
{
get { return buffer; }
}
///
/// Read a line from standard in/err
///
///
/// The read line
///
public override string ReadLine()
{
string tmp = base.ReadLine();
buffer.Append(tmp + Environment.NewLine);
Match m = null;
if (tmp.Contains("Scanning title"))
m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");
if (OnReadLine != null)
OnReadLine(this, tmp);
if (m != null)
if (m.Success && OnScanProgress != null)
OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
return tmp;
}
///
/// Read to the end of the input stream
///
///
/// A string of the input data
///
public override string ReadToEnd()
{
string tmp = base.ReadToEnd();
buffer.Append(tmp + Environment.NewLine);
if (OnReadToEnd != null)
OnReadToEnd(this, tmp);
return tmp;
}
///
/// Pase the CLI status output (from standard output)
///
public void ReadEncodeStatus()
{
string tmp = base.ReadLine();
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\))?");
if (m.Success && OnEncodeProgress != null)
{
int currentTask = int.Parse(m.Groups[1].Value);
int totalTasks = int.Parse(m.Groups[2].Value);
float percent = float.Parse(m.Groups[3].Value, CultureInfo.InvariantCulture);
float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, CultureInfo.InvariantCulture);
float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, CultureInfo.InvariantCulture);
string remaining = string.Empty;
if (m.Groups[7].Value != string.Empty)
{
remaining = m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value;
}
if (string.IsNullOrEmpty(remaining))
{
remaining = "Calculating ...";
}
OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);
}
}
}
}