From d64752a15a5bf0e96c5e27b60b4d7aa76efdaf5d Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 11 Jun 2010 18:57:37 +0000 Subject: [PATCH] WinGui: - Improvements to the IQueue and IEncode interface. This allows for some cleanup of code in frmMain and frmPreview git-svn-id: svn://localhost/HandBrake/trunk@3376 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../EncodeProgressEventArgs.cs | 40 +++ .../HandBrake.ApplicationServices.csproj | 1 + .../Services/Encode.cs | 143 ++++++--- .../Services/Interfaces/IEncode.cs | 5 +- .../Services/Interfaces/IQueue.cs | 38 +-- .../Services/Queue.cs | 39 +++ win/C#/frmMain.cs | 108 +++---- win/C#/frmPreview.Designer.cs | 4 +- win/C#/frmPreview.cs | 344 +++++++++++++-------- 9 files changed, 434 insertions(+), 288 deletions(-) create mode 100644 win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs diff --git a/win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs b/win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs new file mode 100644 index 00000000..8939e11c --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs @@ -0,0 +1,40 @@ +/* EncodeProgressEventArgs.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +namespace HandBrake.ApplicationServices +{ + using System; + + /// + /// Encode Progress Event Args + /// + public class EncodeProgressEventArgs : EventArgs + { + /// + /// Gets or sets PercentComplete. + /// + public float PercentComplete { get; set; } + + /// + /// Gets or sets CurrentFrameRate. + /// + public float CurrentFrameRate { get; set; } + + /// + /// Gets or sets AverageFrameRate. + /// + public float AverageFrameRate { get; set; } + + /// + /// Gets or sets EstimatedTimeLeft. + /// + public TimeSpan EstimatedTimeLeft { get; set; } + + /// + /// Gets or sets Task. + /// + public int Task { get; set; } + } +} diff --git a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index ce6412ff..c2496e94 100644 --- a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -63,6 +63,7 @@ + Form diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs index 38c9b808..d4e971e6 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs @@ -14,6 +14,7 @@ namespace HandBrake.ApplicationServices.Services using HandBrake.ApplicationServices.Functions; using HandBrake.ApplicationServices.Model; + using HandBrake.ApplicationServices.Parsing; using HandBrake.ApplicationServices.Properties; using HandBrake.ApplicationServices.Services.Interfaces; @@ -56,6 +57,29 @@ namespace HandBrake.ApplicationServices.Services /// private int processID; + /* Constructor */ + + /// + /// Initializes a new instance of the class. + /// + public Encode() + { + this.EncodeStarted += Encode_EncodeStarted; + } + + /* Delegates */ + + /// + /// Encode Progess Status + /// + /// + /// The sender. + /// + /// + /// The EncodeProgressEventArgs. + /// + public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e); + /* Event Handlers */ /// @@ -68,12 +92,17 @@ namespace HandBrake.ApplicationServices.Services /// public event EventHandler EncodeEnded; + /// + /// Encode process has progressed + /// + public event EncodeProgessStatus EncodeStatusChanged; + /* Properties */ /// /// Gets or sets The HB Process /// - public Process HbProcess { get; set; } + protected Process HbProcess { get; set; } /// /// Gets a value indicating whether IsEncoding. @@ -183,7 +212,7 @@ namespace HandBrake.ApplicationServices.Services if (HbProcess != null) this.processHandle = HbProcess.MainWindowHandle; // Set the process Handle - + // Start the Log Monitor windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000); @@ -241,6 +270,12 @@ namespace HandBrake.ApplicationServices.Services private void HbProcess_Exited(object sender, EventArgs e) { IsEncoding = false; + + windowTimer.Dispose(); + ReadFile(null); + + if (this.EncodeEnded != null) + this.EncodeEnded(this, new EventArgs()); } /// @@ -324,50 +359,6 @@ namespace HandBrake.ApplicationServices.Services } /// - /// Perform an action after an encode. e.g a shutdown, standby, restart etc. - /// - protected void Finish() - { - if (!IsEncoding) - { - windowTimer.Dispose(); - ReadFile(null); - } - - if (this.EncodeEnded != null) - this.EncodeEnded(this, new EventArgs()); - - // Growl - if (Settings.Default.growlQueue) - GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done."); - - // Do something whent he encode ends. - switch (Settings.Default.CompletionOption) - { - case "Shutdown": - Process.Start("Shutdown", "-s -t 60"); - break; - case "Log Off": - Win32.ExitWindowsEx(0, 0); - break; - case "Suspend": - Application.SetSuspendState(PowerState.Suspend, true, true); - break; - case "Hibernate": - Application.SetSuspendState(PowerState.Hibernate, true, true); - break; - case "Lock System": - Win32.LockWorkStation(); - break; - case "Quit HandBrake": - Application.Exit(); - break; - default: - break; - } - } - - /// /// Add the CLI Query to the Log File. /// /// @@ -550,5 +541,65 @@ namespace HandBrake.ApplicationServices.Services logBuffer.AppendLine(e.Data); } } + + /// + /// Encode Started + /// + /// + /// The sender. + /// + /// + /// The EventArgs. + /// + private void Encode_EncodeStarted(object sender, EventArgs e) + { + Thread monitor = new Thread(EncodeMonitor); + monitor.Start(); + } + + /// + /// Monitor the Job + /// + private void EncodeMonitor() + { + try + { + Parser encode = new Parser(HbProcess.StandardOutput.BaseStream); + encode.OnEncodeProgress += EncodeOnEncodeProgress; + while (!encode.EndOfStream) + encode.ReadEncodeStatus(); + + // Main.ShowExceptiowWindow("Encode Monitor Stopped", "Stopped"); + } + catch (Exception exc) + { + Main.ShowExceptiowWindow("An Unknown Error has occured", exc.ToString()); + } + } + + /// + /// Displays the Encode status in the GUI + /// + /// The sender + /// The current task + /// Number of tasks + /// Percent complete + /// Current encode speed in fps + /// Avg encode speed + /// Time Left + private void EncodeOnEncodeProgress(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float avg, TimeSpan timeRemaining) + { + EncodeProgressEventArgs eventArgs = new EncodeProgressEventArgs + { + AverageFrameRate = avg, + CurrentFrameRate = currentFps, + EstimatedTimeLeft = timeRemaining, + PercentComplete = percentComplete, + Task = currentTask + }; + + if (this.EncodeStatusChanged != null) + this.EncodeStatusChanged(this, eventArgs); + } } } \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs index 3b1a95db..9a0681c2 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs @@ -6,7 +6,6 @@ namespace HandBrake.ApplicationServices.Services.Interfaces { using System; - using System.Diagnostics; /// /// The IEncode Interface @@ -24,9 +23,9 @@ namespace HandBrake.ApplicationServices.Services.Interfaces event EventHandler EncodeEnded; /// - /// Gets or sets The HB Process + /// Encode process has progressed /// - Process HbProcess { get; set; } + event Encode.EncodeProgessStatus EncodeStatusChanged; /// /// Gets a value indicating whether IsEncoding. diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs index a01f13a1..981f3c19 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs @@ -7,14 +7,13 @@ namespace HandBrake.ApplicationServices.Services.Interfaces { using System; using System.Collections.ObjectModel; - using System.Diagnostics; using HandBrake.ApplicationServices.Model; /// /// The IQueue Interface /// - public interface IQueue + public interface IQueue : IEncode { /// /// Fires when the Queue has started @@ -59,11 +58,6 @@ namespace HandBrake.ApplicationServices.Services.Interfaces int Count { get; } /// - /// Gets or sets The HB Process - /// - Process HbProcess { get; set; } - - /// /// Gets a value indicating whether IsEncoding. /// bool IsEncoding { get; } @@ -153,35 +147,5 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// Requests a pause of the encode queue. /// void Pause(); - - /// - /// Fires when a new CLI Job starts - /// - event EventHandler EncodeStarted; - - /// - /// Fires when a CLI job finishes. - /// - event EventHandler EncodeEnded; - - /// - /// Create a preview sample video - /// - /// - /// The CLI Query - /// - void CreatePreviewSample(string query); - - /// - /// Kill the CLI process - /// - void Stop(); - - /// - /// 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 - /// - void SafelyClose(); } } \ 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 226ea68b..0a1eb3c3 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs @@ -8,6 +8,7 @@ namespace HandBrake.ApplicationServices.Services using System; using System.Collections.Generic; using System.Collections.ObjectModel; + using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; @@ -16,6 +17,7 @@ namespace HandBrake.ApplicationServices.Services using HandBrake.ApplicationServices.Functions; using HandBrake.ApplicationServices.Model; + using HandBrake.ApplicationServices.Properties; using HandBrake.ApplicationServices.Services.Interfaces; /// @@ -38,6 +40,7 @@ namespace HandBrake.ApplicationServices.Services /// private int nextJobId; + #region Events /// /// Fires when the Queue has started /// @@ -58,6 +61,7 @@ namespace HandBrake.ApplicationServices.Services /// Fires when the entire encode queue has completed. /// public event EventHandler QueueCompleted; + #endregion #region Properties /// @@ -406,6 +410,41 @@ namespace HandBrake.ApplicationServices.Services Finish(); } + /// + /// Perform an action after an encode. e.g a shutdown, standby, restart etc. + /// + private void Finish() + { + // Growl + if (Settings.Default.growlQueue) + GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done."); + + // Do something whent he encode ends. + switch (Settings.Default.CompletionOption) + { + case "Shutdown": + Process.Start("Shutdown", "-s -t 60"); + break; + case "Log Off": + Win32.ExitWindowsEx(0, 0); + break; + case "Suspend": + Application.SetSuspendState(PowerState.Suspend, true, true); + break; + case "Hibernate": + Application.SetSuspendState(PowerState.Hibernate, true, true); + break; + case "Lock System": + Win32.LockWorkStation(); + break; + case "Quit HandBrake": + Application.Exit(); + break; + default: + break; + } + } + #endregion } } \ No newline at end of file diff --git a/win/C#/frmMain.cs b/win/C#/frmMain.cs index 3a4358bf..d149ec6f 100644 --- a/win/C#/frmMain.cs +++ b/win/C#/frmMain.cs @@ -48,7 +48,6 @@ namespace Handbrake private DVD currentSource; private IScan SourceScan = new ScanService(); private List drives; - private Thread encodeMonitor; // Delegates ********************************************************** private delegate void UpdateWindowHandler(); @@ -343,17 +342,12 @@ namespace Handbrake private void encodeStarted(object sender, EventArgs e) { SetEncodeStarted(); - - // Experimental HBProc Process Monitoring. - if (Properties.Settings.Default.enocdeStatusInGui) - { - encodeMonitor = new Thread(EncodeMonitorThread); - encodeMonitor.Start(); - } + encodeQueue.EncodeStatusChanged += EncodeQueue_EncodeStatusChanged; } private void encodeEnded(object sender, EventArgs e) { + encodeQueue.EncodeStatusChanged -= EncodeQueue_EncodeStatusChanged; SetEncodeFinished(); } @@ -574,7 +568,7 @@ namespace Handbrake private void mnu_UpdateCheck_Click(object sender, EventArgs e) { lbl_updateCheck.Visible = true; - Main.BeginCheckForUpdates(new AsyncCallback(updateCheckDoneMenu), false); + Main.BeginCheckForUpdates(new AsyncCallback(this.UpdateCheckDoneMenu), false); } /// @@ -1038,8 +1032,6 @@ namespace Handbrake !Properties.Settings.Default.showCliForInGuiEncodeStatus) { encodeQueue.Stop(); - if (encodeQueue.HbProcess != null) - encodeQueue.HbProcess.WaitForExit(); } else { @@ -1104,7 +1096,7 @@ namespace Handbrake if (overwrite == DialogResult.Yes) { if (encodeQueue.Count == 0) - encodeQueue.Add(query, getTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); + encodeQueue.Add(query, this.GetTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); queueWindow.SetQueue(); if (encodeQueue.Count > 1) @@ -1154,10 +1146,10 @@ namespace Handbrake "There is already a queue item for this destination path. \n\n If you continue, the encode will be overwritten. Do you wish to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) - encodeQueue.Add(query, getTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); + encodeQueue.Add(query, this.GetTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); } else - encodeQueue.Add(query, getTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); + encodeQueue.Add(query, this.GetTitle(), sourcePath, text_destination.Text, (rtf_query.Text != string.Empty)); lbl_encode.Text = encodeQueue.Count + " encode(s) pending in the queue"; @@ -2255,6 +2247,34 @@ namespace Handbrake } /// + /// Display the Encode Status + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e) + { + if (this.InvokeRequired) + { + this.BeginInvoke(new Encode.EncodeProgessStatus(EncodeQueue_EncodeStatusChanged), new[] { sender, e }); + return; + } + + lbl_encode.Text = + string.Format( + "{0:00.00}%, FPS: {1:000.0}, Avg FPS: {2:000.0}, Time Remaining: {3}", + e.PercentComplete, + e.CurrentFrameRate, + e.AverageFrameRate, + e.EstimatedTimeLeft); + + ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete); + } + + /// /// Set the DVD Drive selection in the "Source" Menu /// private void SetDriveSelectionMenuItem() @@ -2312,7 +2332,7 @@ namespace Handbrake /// /// The title. /// - private int getTitle() + private int GetTitle() { int title = 0; if (drp_dvdtitle.SelectedItem != null) @@ -2330,12 +2350,12 @@ namespace Handbrake /// /// The result. /// - private void updateCheckDoneMenu(IAsyncResult result) + private void UpdateCheckDoneMenu(IAsyncResult result) { // Make sure it's running on the calling thread if (InvokeRequired) { - Invoke(new MethodInvoker(() => updateCheckDoneMenu(result))); + Invoke(new MethodInvoker(() => this.UpdateCheckDoneMenu(result))); return; } UpdateCheckInformation info; @@ -2424,60 +2444,6 @@ namespace Handbrake #endregion - #region In-GUI Encode Status - - /// - /// Starts a new thread to monitor and process the CLI encode status - /// - private void EncodeMonitorThread() - { - try - { - Parser encode = new Parser(encodeQueue.HbProcess.StandardOutput.BaseStream); - encode.OnEncodeProgress += EncodeOnEncodeProgress; - while (!encode.EndOfStream) - encode.ReadEncodeStatus(); - - SetEncodeFinished(); - } - catch (Exception exc) - { - MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - /// - /// Displays the Encode status in the GUI - /// - /// The sender - /// The current task - /// Number of tasks - /// Percent complete - /// Current encode speed in fps - /// Avg encode speed - /// Time Left - private void EncodeOnEncodeProgress(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float av, TimeSpan timeRemaining) - { - if (this.InvokeRequired) - { - this.BeginInvoke( - new EncodeProgressEventHandler(EncodeOnEncodeProgress), - new[] { sender, currentTask, taskCount, percentComplete, currentFps, av, timeRemaining }); - return; - } - lbl_encode.Text = - string.Format( - "{0:00.00}%, FPS: {1:000.0}, Avg FPS: {2:000.0}, Time Remaining: {3}", - percentComplete, - currentFps, - av, - timeRemaining); - - ProgressBarStatus.Value = (int)Math.Round(percentComplete); - } - - #endregion - // This is the END of the road **************************************** } } \ No newline at end of file diff --git a/win/C#/frmPreview.Designer.cs b/win/C#/frmPreview.Designer.cs index 77563035..d37a8f53 100644 --- a/win/C#/frmPreview.Designer.cs +++ b/win/C#/frmPreview.Designer.cs @@ -134,7 +134,7 @@ this.btn_playQT.Name = "btn_playQT"; this.btn_playQT.Size = new System.Drawing.Size(96, 22); this.btn_playQT.Text = "Play with QT"; - this.btn_playQT.Click += new System.EventHandler(this.btn_playQT_Click); + this.btn_playQT.Click += new System.EventHandler(this.PlayQT_Click); // // btn_playVLC // @@ -144,7 +144,7 @@ this.btn_playVLC.Name = "btn_playVLC"; this.btn_playVLC.Size = new System.Drawing.Size(101, 22); this.btn_playVLC.Text = "Play with VLC"; - this.btn_playVLC.Click += new System.EventHandler(this.btn_playVLC_Click); + this.btn_playVLC.Click += new System.EventHandler(this.PlayVLC_Click); // // QTControl // diff --git a/win/C#/frmPreview.cs b/win/C#/frmPreview.cs index 41ceb517..2c9e6c87 100644 --- a/win/C#/frmPreview.cs +++ b/win/C#/frmPreview.cs @@ -13,22 +13,53 @@ namespace Handbrake using System.Windows.Forms; using Functions; - using HandBrake.ApplicationServices.Parsing; using HandBrake.ApplicationServices.Services; + using HandBrake.ApplicationServices.Services.Interfaces; using QTOControlLib; using QTOLibrary; + /// + /// The Preview Window + /// public partial class frmPreview : Form { - private string CurrentlyPlaying = string.Empty; - private readonly frmMain MainWindow; - private Thread Player; - private readonly bool NoQT; - private readonly Queue Process = new Queue(); - private delegate void UpdateUIHandler(); + /// + /// The Main Window + /// + private readonly frmMain mainWindow; + + /// + /// True if QT is not installed + /// + private readonly bool noQT; + + /// + /// The encode queue + /// + private readonly IQueue encodeQueue = new Queue(); + + /// + /// What is currently playing + /// + private string currentlyPlaying = string.Empty; + + /// + /// Play With VLC tracker + /// private bool playWithVLC; + /// + /// A Thread for the video player + /// + private Thread player; + + /// + /// Initializes a new instance of the class. + /// + /// + /// The mw. + /// public frmPreview(frmMain mw) { try @@ -37,9 +68,9 @@ namespace Handbrake } catch (Exception) { - NoQT = true; + this.noQT = true; } - this.MainWindow = mw; + this.mainWindow = mw; cb_preview.SelectedIndex = 0; cb_duration.SelectedIndex = 1; @@ -48,17 +79,121 @@ namespace Handbrake cb_preview.Items.Add(i.ToString()); cb_preview.SelectedIndex = 0; - Process.EncodeStarted += new EventHandler(Process_EncodeStarted); + encodeQueue.EncodeStarted += this.EncodeQueue_EncodeStarted; + encodeQueue.EncodeEnded += this.EncodeQueue_EncodeEnded; } - private void Process_EncodeStarted(object sender, EventArgs e) + + /// + /// Update UI Delegate + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private delegate void UpdateUIHandler(object sender, EventArgs e); + + /// + /// The Open Movie Handler + /// + private delegate void OpenMovieHandler(); + + /// + /// The encode has started + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void EncodeQueue_EncodeStarted(object sender, EventArgs e) { - Thread encodeMon = new Thread(EncodeMonitorThread); - encodeMon.Start(); + encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged; + } + + /// + /// The Enocde has ended + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void EncodeQueue_EncodeEnded(object sender, EventArgs e) + { + encodeQueue.EncodeStatusChanged -= this.EncodeQueue_EncodeStatusChanged; + + try + { + if (this.InvokeRequired) + { + this.BeginInvoke(new UpdateUIHandler(EncodeQueue_EncodeEnded), new[] { sender, e }); + return; + } + + ProgressBarStatus.Visible = false; + lbl_encodeStatus.Visible = false; + + if (!this.noQT) + btn_playQT.Enabled = true; + btn_playVLC.Enabled = true; + + this.Text = this.Text.Replace(" (Encoding)", string.Empty); + + // Get the sample filename + if (this.mainWindow.text_destination.Text != string.Empty) + this.currentlyPlaying = + this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v"). + Replace(".mkv", "_sample.mkv"); + + // Play back in QT or VLC + if (!playWithVLC) + Play(); + else + PlayVLC(); + } + catch (Exception exc) + { + Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString()); + } + } + + /// + /// Encode status has changed + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e) + { + if (this.InvokeRequired) + { + this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueue_EncodeStatusChanged), new[] { sender, e }); + return; + } + + lbl_encodeStatus.Text = e.PercentComplete + "%"; + ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete); } #region Encode Sample - private void btn_playVLC_Click(object sender, EventArgs e) + /// + /// Play with VLC + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void PlayVLC_Click(object sender, EventArgs e) { ProgressBarStatus.Visible = true; ProgressBarStatus.Value = 0; @@ -67,15 +202,15 @@ namespace Handbrake try { - if (!NoQT) + if (!this.noQT) QTControl.URL = string.Empty; - if (File.Exists(CurrentlyPlaying)) - File.Delete(CurrentlyPlaying); + if (File.Exists(this.currentlyPlaying)) + File.Delete(this.currentlyPlaying); } catch (Exception) { - MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", + MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -84,24 +219,33 @@ namespace Handbrake this.Text += " (Encoding)"; int duration; int.TryParse(cb_duration.Text, out duration); - string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text); - ThreadPool.QueueUserWorkItem(ProcMonitor, query); + string query = QueryGenerator.GenerateCliQuery(this.mainWindow, 3, duration, cb_preview.Text); + ThreadPool.QueueUserWorkItem(this.CreatePreview, query); } - private void btn_playQT_Click(object sender, EventArgs e) + /// + /// Encode and Play with QT + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void PlayQT_Click(object sender, EventArgs e) { playWithVLC = false; - if (NoQT) + if (this.noQT) { - MessageBox.Show(this, - "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", + MessageBox.Show(this, + "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } - if (MainWindow.text_destination.Text.Contains(".mkv")) + if (this.mainWindow.text_destination.Text.Contains(".mkv")) { - MessageBox.Show(this, - "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", + MessageBox.Show(this, + "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else @@ -112,13 +256,13 @@ namespace Handbrake try { QTControl.URL = string.Empty; - if (File.Exists(CurrentlyPlaying)) - File.Delete(CurrentlyPlaying); + if (File.Exists(this.currentlyPlaying)) + File.Delete(this.currentlyPlaying); } catch (Exception) { - MessageBox.Show(this, - "Unable to delete previous preview file. You may need to restart the application.", + MessageBox.Show(this, + "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -127,95 +271,34 @@ namespace Handbrake this.Text += " (Encoding)"; int duration; int.TryParse(cb_duration.Text, out duration); - string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text); + string query = QueryGenerator.GenerateCliQuery(this.mainWindow, 3, duration, cb_preview.Text); - ThreadPool.QueueUserWorkItem(ProcMonitor, query); + ThreadPool.QueueUserWorkItem(this.CreatePreview, query); } } - private void ProcMonitor(object state) + /// + /// Create the Preview. + /// + /// + /// The state. + /// + private void CreatePreview(object state) { // Make sure we are not already encoding and if we are then display an error. - if (Process.HbProcess != null) - MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, - MessageBoxIcon.Warning); - else + if (encodeQueue.IsEncoding) { - Process.CreatePreviewSample((string) state); + MessageBox.Show( + this, + "Handbrake is already encoding a video!", + "Status", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); - if (Process.HbProcess != null) - { - Process.HbProcess.WaitForExit(); - Process.HbProcess = null; - } - EncodeCompleted(); - } - } - - private void EncodeMonitorThread() - { - try - { - Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream); - encode.OnEncodeProgress += EncodeOnEncodeProgress; - while (!encode.EndOfStream) - encode.ReadEncodeStatus(); - } - catch (Exception exc) - { - MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining) - { - if (this.InvokeRequired) - { - this.BeginInvoke( - new EncodeProgressEventHandler(EncodeOnEncodeProgress), - new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining }); return; } - lbl_encodeStatus.Text = PercentComplete + "%"; - ProgressBarStatus.Value = (int)Math.Round(PercentComplete); - } - - private void EncodeCompleted() - { - try - { - if (InvokeRequired) - { - BeginInvoke(new UpdateUIHandler(EncodeCompleted)); - return; - } - - ProgressBarStatus.Visible = false; - lbl_encodeStatus.Visible = false; - if (!NoQT) - btn_playQT.Enabled = true; - btn_playVLC.Enabled = true; - - this.Text = this.Text.Replace(" (Encoding)", string.Empty); - - // Get the sample filename - if (MainWindow.text_destination.Text != string.Empty) - CurrentlyPlaying = - MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v"). - Replace(".mkv", "_sample.mkv"); - - // Play back in QT or VLC - if (!playWithVLC) - Play(); - else - PlayVLC(); - } - catch (Exception exc) - { - MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } + encodeQueue.CreatePreviewSample((string)state); } #endregion @@ -227,8 +310,8 @@ namespace Handbrake /// private void Play() { - Player = new Thread(OpenMovie) {IsBackground = true}; - Player.Start(); + this.player = new Thread(OpenMovie) { IsBackground = true }; + this.player.Start(); } /// @@ -237,9 +320,9 @@ namespace Handbrake private void PlayVLC() { // Launch VLC and Play video. - if (CurrentlyPlaying != string.Empty) + if (this.currentlyPlaying != string.Empty) { - if (File.Exists(CurrentlyPlaying)) + if (File.Exists(this.currentlyPlaying)) { // Attempt to find VLC if it doesn't exist in the default set location. string vlcPath; @@ -263,22 +346,22 @@ namespace Handbrake } else { - MessageBox.Show(this, - "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in HandBrake's options is correct. (See: \"Tools Menu > Options > Picture Tab\") ", + MessageBox.Show(this, + "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in HandBrake's options is correct. (See: \"Tools Menu > Options > Picture Tab\") ", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } if (File.Exists(Properties.Settings.Default.VLC_Path)) { - string args = "\"" + CurrentlyPlaying + "\""; + string args = "\"" + this.currentlyPlaying + "\""; ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args); - System.Diagnostics.Process.Start(vlc); + Process.Start(vlc); } } else - MessageBox.Show(this, - "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", + MessageBox.Show(this, + "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } @@ -293,12 +376,12 @@ namespace Handbrake { if (InvokeRequired) { - BeginInvoke(new UpdateUIHandler(OpenMovie)); + BeginInvoke(new OpenMovieHandler(OpenMovie)); return; } - QTControl.URL = CurrentlyPlaying; + QTControl.URL = this.currentlyPlaying; QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true); - QTControl.URL = CurrentlyPlaying; + QTControl.URL = this.currentlyPlaying; QTControl.Show(); this.ClientSize = QTControl.Size; @@ -307,23 +390,26 @@ namespace Handbrake catch (COMException ex) { QTUtils qtu = new QTUtils(); - MessageBox.Show(this, - "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + - "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", - MessageBoxButtons.OK, MessageBoxIcon.Warning); + Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode)); } catch (Exception ex) { - MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, - MessageBoxIcon.Warning); + Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString()); } } #endregion + /// + /// Remove any subscribed events then close. + /// + /// + /// The e. + /// protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { - Process.EncodeStarted -= Process_EncodeStarted; + encodeQueue.EncodeStarted -= this.EncodeQueue_EncodeStarted; + encodeQueue.EncodeEnded -= this.EncodeQueue_EncodeEnded; base.OnClosing(e); } } -- 2.11.0