summaryrefslogtreecommitdiffstats
path: root/win/C#/HandBrake.ApplicationServices/Services
diff options
context:
space:
mode:
authorsr55 <[email protected]>2010-07-13 22:02:00 +0000
committersr55 <[email protected]>2010-07-13 22:02:00 +0000
commit07de1ce8ff81af998b294103206bdeb4d5591efd (patch)
treed7a86034f5a0a4a85cb719d5fcf5a8148bec8386 /win/C#/HandBrake.ApplicationServices/Services
parentd484afb3733d33d42392b915a195769aee2a0ba5 (diff)
WinGui:
- Major rework of the Encode Service. This allows previews to be generated while an encode is running. I've left in a bunch of debug code for the moment so it may be a bit noisy at times if something goes a bit wrong. Will remove later when the code is known to work. - The windows 7 Encode Progress Bar on the Task bar is working again. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3436 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/C#/HandBrake.ApplicationServices/Services')
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Encode.cs446
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Queue.cs3
2 files changed, 197 insertions, 252 deletions
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
index 1731d4851..79b82a2ea 100644
--- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
+++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
@@ -25,7 +25,7 @@ namespace HandBrake.ApplicationServices.Services
/// </summary>
public class Encode : IEncode
{
- /* Private Variables */
+ #region Private Variables
/// <summary>
/// An Encode Job
@@ -38,14 +38,9 @@ namespace HandBrake.ApplicationServices.Services
private StringBuilder logBuffer;
/// <summary>
- /// The line number thats been read to in the log file
+ /// The Log file writer
/// </summary>
- private int logFilePosition;
-
- /// <summary>
- /// A Timer for this window
- /// </summary>
- private Timer windowTimer;
+ private StreamWriter fileWriter;
/// <summary>
/// Gets The Process Handle
@@ -62,6 +57,8 @@ namespace HandBrake.ApplicationServices.Services
/// </summary>
private Win7 windowsSeven = new Win7();
+ #endregion
+
/* Constructor */
/// <summary>
@@ -72,7 +69,7 @@ namespace HandBrake.ApplicationServices.Services
this.EncodeStarted += Encode_EncodeStarted;
}
- /* Delegates */
+ #region Delegates and Event Handlers
/// <summary>
/// Encode Progess Status
@@ -101,6 +98,7 @@ namespace HandBrake.ApplicationServices.Services
/// Encode process has progressed
/// </summary>
public event EncodeProgessStatus EncodeStatusChanged;
+ #endregion
/* Properties */
@@ -114,8 +112,6 @@ namespace HandBrake.ApplicationServices.Services
/// </summary>
public bool IsEncoding { get; private set; }
- /* Public Methods */
-
/// <summary>
/// Gets ActivityLog.
/// </summary>
@@ -123,16 +119,17 @@ namespace HandBrake.ApplicationServices.Services
{
get
{
- if (logBuffer == null)
+ if (this.IsEncoding == false)
{
- ResetLogReader();
- ReadFile(null);
+ ReadFile(); // Read the last log file back in if it exists
}
- return logBuffer != null ? logBuffer.ToString() : string.Empty;
+ return string.IsNullOrEmpty(this.logBuffer.ToString()) ? "No log data available..." : this.logBuffer.ToString();
}
}
+ /* Public Methods */
+
/// <summary>
/// Create a preview sample video
/// </summary>
@@ -141,42 +138,7 @@ namespace HandBrake.ApplicationServices.Services
/// </param>
public void CreatePreviewSample(string query)
{
- this.Run(new Job { Query = query }, true);
- }
-
- /// <summary>
- /// Kill the CLI process
- /// </summary>
- public void Stop()
- {
- if (this.HbProcess != null)
- this.HbProcess.Kill();
-
- Process[] list = Process.GetProcessesByName("HandBrakeCLI");
- foreach (Process process in list)
- process.Kill();
-
- if (this.EncodeEnded != null)
- this.EncodeEnded(this, new EventArgs());
- }
-
- /// <summary>
- /// Attempt to Safely kill a DirectRun() CLI
- /// NOTE: This will not work with a MinGW CLI
- /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
- /// </summary>
- public void SafelyClose()
- {
- if ((int)this.processHandle == 0)
- return;
-
- // Allow the CLI to exit cleanly
- Win32.SetForegroundWindow((int)this.processHandle);
- SendKeys.Send("^C");
- SendKeys.Flush();
-
- // HbProcess.StandardInput.AutoFlush = true;
- // HbProcess.StandardInput.WriteLine("^C");
+ this.Run(new Job { Query = query }, false);
}
/// <summary>
@@ -185,77 +147,73 @@ namespace HandBrake.ApplicationServices.Services
/// <param name="encJob">
/// The enc Job.
/// </param>
- /// <param name="requireStandardOuput">
- /// Set to True to show no window and force standard output redirect
+ /// <param name="enableLogging">
+ /// Enable Logging. When Disabled we onlt parse Standard Ouput for progress info. Standard Error log data is ignored.
/// </param>
- protected void Run(Job encJob, bool requireStandardOuput)
+ protected void Run(Job encJob, bool enableLogging)
{
this.job = encJob;
try
{
- if (Properties.Settings.Default.preventSleep)
+ IsEncoding = true;
+
+ if (enableLogging)
+ SetupLogging(encJob);
+
+ if (Settings.Default.preventSleep)
{
Win32.PreventSleep();
}
- ResetLogReader();
- IsEncoding = true;
-
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
- string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");
- string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query, logPath);
- var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
+ ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, encJob.Query)
+ {
+ RedirectStandardOutput = true,
+ RedirectStandardError = enableLogging ? true : false,
+ UseShellExecute = false,
+ CreateNoWindow = !Settings.Default.showCliForInGuiEncodeStatus ? true : false
+ };
+
+ this.HbProcess = Process.Start(cliStart);
- if (Settings.Default.enocdeStatusInGui || requireStandardOuput)
+ if (enableLogging)
{
- cliStart.RedirectStandardOutput = true;
- cliStart.UseShellExecute = false;
- if (!Settings.Default.showCliForInGuiEncodeStatus || requireStandardOuput)
- cliStart.CreateNoWindow = true;
+ this.HbProcess.ErrorDataReceived += HbProcErrorDataReceived;
+ this.HbProcess.BeginErrorReadLine();
}
- if (Settings.Default.cli_minimized)
- cliStart.WindowStyle = ProcessWindowStyle.Minimized;
- Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
- HbProcess = Process.Start(cliStart);
- this.processID = Main.GetCliProcess(before);
-
- this.processHandle = HbProcess.MainWindowHandle; // Set the process Handle
-
- // Start the Log Monitor
- windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000);
+ this.processID = HbProcess.Id;
+ this.processHandle = HbProcess.MainWindowHandle;
// Set the process Priority
- Process hbCliProcess = null;
if (this.processID != -1)
{
- hbCliProcess = Process.GetProcessById(this.processID);
- hbCliProcess.EnableRaisingEvents = true;
- hbCliProcess.Exited += new EventHandler(HbProcess_Exited);
+ this.HbProcess.EnableRaisingEvents = true;
+ this.HbProcess.Exited += HbProcess_Exited;
}
- if (hbCliProcess != null)
- switch (Settings.Default.processPriority)
- {
- case "Realtime":
- hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
- break;
- case "High":
- hbCliProcess.PriorityClass = ProcessPriorityClass.High;
- break;
- case "Above Normal":
- hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
- break;
- case "Normal":
- hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
- break;
- case "Low":
- hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
- break;
- default:
- hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
- break;
- }
+ // Set the Process Priority
+ switch (Settings.Default.processPriority)
+ {
+ case "Realtime":
+ this.HbProcess.PriorityClass = ProcessPriorityClass.RealTime;
+ break;
+ case "High":
+ this.HbProcess.PriorityClass = ProcessPriorityClass.High;
+ break;
+ case "Above Normal":
+ this.HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
+ break;
+ case "Normal":
+ this.HbProcess.PriorityClass = ProcessPriorityClass.Normal;
+ break;
+ case "Low":
+ this.HbProcess.PriorityClass = ProcessPriorityClass.Idle;
+ break;
+ default:
+ this.HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
+ break;
+ }
// Fire the Encode Started Event
if (this.EncodeStarted != null)
@@ -263,150 +221,94 @@ namespace HandBrake.ApplicationServices.Services
}
catch (Exception exc)
{
- Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly. You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()", exc.ToString());
+ Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly."+
+ "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()",
+ exc.ToString());
}
}
/// <summary>
- /// The HandBrakeCLI process has exited.
+ /// Kill the CLI process
/// </summary>
- /// <param name="sender">
- /// The sender.
- /// </param>
- /// <param name="e">
- /// The EventArgs.
- /// </param>
- private void HbProcess_Exited(object sender, EventArgs e)
+ public void Stop()
{
- IsEncoding = false;
-
- windowTimer.Dispose();
- ReadFile(null);
-
- if (this.EncodeEnded != null)
- this.EncodeEnded(this, new EventArgs());
-
- if (windowsSeven.IsWindowsSeven)
+ try
{
- windowsSeven.SetTaskBarProgressToNoProgress();
- }
+ if (this.HbProcess != null) this.HbProcess.Kill();
- if (Properties.Settings.Default.preventSleep)
+ Process[] list = Process.GetProcessesByName("HandBrakeCLI");
+ foreach (Process process in list)
+ process.Kill();
+ }
+ catch (Exception exc)
{
- Win32.AllowSleep();
+ Main.ShowExceptiowWindow("Unable to stop HandBrakeCLI. It may not be running.", exc.ToString());
}
+
+ if (this.EncodeEnded != null)
+ this.EncodeEnded(this, new EventArgs());
}
/// <summary>
- /// Function to run the CLI directly rather than via CMD
- /// TODO: Code to handle the Log data has yet to be written.
- /// TODO: Code to handle the % / ETA info has to be written.
+ /// Attempt to Safely kill a DirectRun() CLI
+ /// NOTE: This will not work with a MinGW CLI
+ /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
/// </summary>
- /// <param name="query">
- /// The query.
- /// </param>
- protected void DirectRun(string query)
+ public void SafelyClose()
{
- try
- {
- if (this.EncodeStarted != null)
- this.EncodeStarted(this, new EventArgs());
-
- IsEncoding = true;
+ if ((int)this.processHandle == 0)
+ return;
- ResetLogReader();
-
- // Setup the job
- string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");
- HbProcess = new Process
- {
- StartInfo =
- {
- FileName = handbrakeCLIPath,
- Arguments = query,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- RedirectStandardInput = true,
- CreateNoWindow = false,
- WindowStyle = ProcessWindowStyle.Minimized
- }
- };
-
- // Setup event handlers for rediected data
- HbProcess.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);
- HbProcess.OutputDataReceived += new DataReceivedEventHandler(HbProcOutputDataReceived);
-
- // Start the process
- HbProcess.Start();
-
- // Setup the asynchronous reading of stdin and stderr
- HbProcess.BeginErrorReadLine();
- HbProcess.BeginOutputReadLine();
-
- // Set the Process Priority);
- switch (Settings.Default.processPriority)
- {
- case "Realtime":
- HbProcess.PriorityClass = ProcessPriorityClass.RealTime;
- break;
- case "High":
- HbProcess.PriorityClass = ProcessPriorityClass.High;
- break;
- case "Above Normal":
- HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
- break;
- case "Normal":
- HbProcess.PriorityClass = ProcessPriorityClass.Normal;
- break;
- case "Low":
- HbProcess.PriorityClass = ProcessPriorityClass.Idle;
- break;
- default:
- HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
- break;
- }
+ // Allow the CLI to exit cleanly
+ Win32.SetForegroundWindow((int)this.processHandle);
+ SendKeys.Send("^C");
+ SendKeys.Flush();
- // Set the class items
- this.processID = HbProcess.Id;
- this.processHandle = HbProcess.Handle;
- }
- catch (Exception exc)
+ if (HbProcess != null)
{
- Console.WriteLine(exc);
+ HbProcess.StandardInput.AutoFlush = true;
+ HbProcess.StandardInput.WriteLine("^C");
}
}
+ /* Helpers */
/// <summary>
/// Add the CLI Query to the Log File.
/// </summary>
/// <param name="encJob">
/// The Encode Job Object
/// </param>
- protected void AddCLIQueryToLog(Job encJob)
+ private static string CreateCliLogHeader(Job encJob)
{
try
{
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\logs";
- string logPath = Path.Combine(logDir, "last_encode_log.txt");
+ //string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
+ // "\\HandBrake\\logs";
+ //string logPath = Path.Combine(logDir, "last_encode_log.txt");
+
+ //var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
+ //string log = reader.ReadToEnd();
+ //reader.Close();
+
+ //var writer = new StreamWriter(File.Create(logPath));
- var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
- string log = reader.ReadToEnd();
- reader.Close();
- var writer = new StreamWriter(File.Create(logPath));
+ //writer.WriteLine("### CLI Query: " + encJob.Query);
+ //writer.WriteLine("### User Query: " + encJob.CustomQuery);
+ //writer.WriteLine("#########################################");
+ //writer.WriteLine(log);
+ //writer.Flush();
+ //writer.Close();
+ StringBuilder logHeader = new StringBuilder();
+ logHeader.AppendLine("### CLI Query: " + encJob.Query);
+ logHeader.AppendLine("### User Query: " + encJob.CustomQuery);
+ logHeader.AppendLine("#########################################");
- writer.WriteLine("### CLI Query: " + encJob.Query);
- writer.WriteLine("### User Query: " + encJob.CustomQuery);
- writer.WriteLine("#########################################");
- writer.WriteLine(log);
- writer.Flush();
- writer.Close();
+ return logHeader.ToString();
}
catch (Exception)
{
- return;
+ return string.Empty;
}
}
@@ -453,21 +355,61 @@ namespace HandBrake.ApplicationServices.Services
}
/// <summary>
+ /// The HandBrakeCLI process has exited.
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EventArgs.
+ /// </param>
+ private void HbProcess_Exited(object sender, EventArgs e)
+ {
+ IsEncoding = false;
+
+ // ReadFile(null);
+
+ if (this.EncodeEnded != null)
+ this.EncodeEnded(this, new EventArgs());
+
+ if (windowsSeven.IsWindowsSeven)
+ {
+ windowsSeven.SetTaskBarProgressToNoProgress();
+ }
+
+ if (Properties.Settings.Default.preventSleep)
+ {
+ Win32.AllowSleep();
+ }
+
+ try
+ {
+ if (fileWriter != null)
+ fileWriter.Close();
+ }
+ catch(Exception exc)
+ {
+ Main.ShowExceptiowWindow("Unable to close the log file wrtier", exc.ToString());
+ }
+ }
+
+ /// <summary>
/// Read the log file
/// </summary>
/// <param name="n">
/// The object.
/// </param>
- private void ReadFile(object n)
+ private void ReadFile()
{
+ logBuffer = new StringBuilder();
lock (logBuffer)
{
// last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),
// we'll need to make a copy of it.
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\logs";
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string logFile = Path.Combine(logDir, "last_encode_log.txt");
string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
+ int logFilePosition = 0;
try
{
@@ -479,19 +421,8 @@ namespace HandBrake.ApplicationServices.Services
if (File.Exists(logFile))
File.Copy(logFile, logFile2, true);
else
- {
- ResetLogReader();
return;
- }
-
- // Put the Query and User Generated Query Flag on the log.
- if (logFilePosition == 0 && job.Query != null)
- {
- logBuffer.AppendLine("### CLI Query: " + job.Query);
- logBuffer.AppendLine("### User Query: " + job.CustomQuery);
- logBuffer.AppendLine("#########################################");
- }
-
+
// Start the Reader
// Only use text which continues on from the last read line
StreamReader sr = new StreamReader(logFile2);
@@ -509,42 +440,45 @@ namespace HandBrake.ApplicationServices.Services
sr.Close();
sr.Dispose();
}
- catch (Exception)
+ catch (Exception exc)
{
- ResetLogReader();
+ Main.ShowExceptiowWindow("Unable to read log file", exc.ToString());
}
}
}
/// <summary>
- /// Reset the Log Reader
+ /// Setup the logging.
/// </summary>
- private void ResetLogReader()
+ private void SetupLogging(Job encodeJob)
{
- logFilePosition = 0;
- logBuffer = new StringBuilder();
- }
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
+ string logFile = Path.Combine(logDir, "last_encode_log.txt");
+ string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
- /// <summary>
- /// Recieve the Standard Error information and process it
- /// </summary>
- /// <param name="sender">
- /// The Sender Object
- /// </param>
- /// <param name="e">
- /// DataReceived EventArgs
- /// </param>
- private void HbProcErrorDataReceived(object sender, DataReceivedEventArgs e)
- {
- if (!String.IsNullOrEmpty(e.Data))
+ try
{
- lock (logBuffer)
- logBuffer.AppendLine(e.Data);
+ logBuffer = new StringBuilder();
+
+ // Clear the current Encode Logs
+ if (File.Exists(logFile)) File.Delete(logFile);
+ if (File.Exists(logFile2)) File.Delete(logFile2);
+
+ fileWriter = new StreamWriter(logFile) { AutoFlush = true };
+
+ fileWriter.WriteLine(CreateCliLogHeader(encodeJob));
+ logBuffer.AppendLine(CreateCliLogHeader(encodeJob));
+ }
+ catch (Exception exc)
+ {
+ if (fileWriter != null)
+ fileWriter.Close();
+ Main.ShowExceptiowWindow("Error", exc.ToString());
}
}
/// <summary>
- /// Standard Input Data Recieved from the CLI
+ /// Recieve the Standard Error information and process it
/// </summary>
/// <param name="sender">
/// The Sender Object
@@ -552,15 +486,27 @@ namespace HandBrake.ApplicationServices.Services
/// <param name="e">
/// DataReceived EventArgs
/// </param>
- private void HbProcOutputDataReceived(object sender, DataReceivedEventArgs e)
+ private void HbProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
{
lock (logBuffer)
logBuffer.AppendLine(e.Data);
+
+ try
+ {
+ if (fileWriter != null)
+ fileWriter.WriteLine(e.Data);
+ }
+ catch (Exception exc)
+ {
+ Main.ShowExceptiowWindow("Unable to write log data...", exc.ToString());
+ }
}
}
+ #region Encode Status from Standard Output
+
/// <summary>
/// Encode Started
/// </summary>
@@ -587,8 +533,6 @@ namespace HandBrake.ApplicationServices.Services
encode.OnEncodeProgress += EncodeOnEncodeProgress;
while (!encode.EndOfStream)
encode.ReadEncodeStatus();
-
- // Main.ShowExceptiowWindow("Encode Monitor Stopped", "Stopped");
}
catch (Exception exc)
{
@@ -629,5 +573,7 @@ namespace HandBrake.ApplicationServices.Services
windowsSeven.SetTaskBarProgress(percent);
}
}
+
+ #endregion
}
} \ No newline at end of file
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs
index 5b6904f64..da32d2503 100644
--- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs
+++ b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs
@@ -380,7 +380,7 @@ namespace HandBrake.ApplicationServices.Services
Job encJob = this.GetNextJob();
this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
- Run(encJob, false);
+ Run(encJob, true);
if (HbProcess == null)
{
@@ -388,7 +388,6 @@ namespace HandBrake.ApplicationServices.Services
}
HbProcess.WaitForExit();
- AddCLIQueryToLog(encJob);
this.CopyLog(this.LastEncode.Destination);
HbProcess.Close();