summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr55 <[email protected]>2010-06-11 18:57:37 +0000
committersr55 <[email protected]>2010-06-11 18:57:37 +0000
commit4ecb7b2c297ba7e87c0a9ab8e0768d45fc4f0810 (patch)
tree0bcc32549872c771f0cf21c71a7b1da31513f153
parent20c52cb94e7cb9db92a58a8aca3f92715d073f98 (diff)
WinGui:
- Improvements to the IQueue and IEncode interface. This allows for some cleanup of code in frmMain and frmPreview git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3376 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs40
-rw-r--r--win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj1
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Encode.cs143
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs5
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs38
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Queue.cs39
-rw-r--r--win/C#/frmMain.cs108
-rw-r--r--win/C#/frmPreview.Designer.cs4
-rw-r--r--win/C#/frmPreview.cs344
9 files changed, 434 insertions, 288 deletions
diff --git a/win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs b/win/C#/HandBrake.ApplicationServices/EncodeProgressEventArgs.cs
new file mode 100644
index 000000000..8939e11c2
--- /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: <http://handbrake.fr>.
+ It may be used under the terms of the GNU General Public License. */
+
+namespace HandBrake.ApplicationServices
+{
+ using System;
+
+ /// <summary>
+ /// Encode Progress Event Args
+ /// </summary>
+ public class EncodeProgressEventArgs : EventArgs
+ {
+ /// <summary>
+ /// Gets or sets PercentComplete.
+ /// </summary>
+ public float PercentComplete { get; set; }
+
+ /// <summary>
+ /// Gets or sets CurrentFrameRate.
+ /// </summary>
+ public float CurrentFrameRate { get; set; }
+
+ /// <summary>
+ /// Gets or sets AverageFrameRate.
+ /// </summary>
+ public float AverageFrameRate { get; set; }
+
+ /// <summary>
+ /// Gets or sets EstimatedTimeLeft.
+ /// </summary>
+ public TimeSpan EstimatedTimeLeft { get; set; }
+
+ /// <summary>
+ /// Gets or sets Task.
+ /// </summary>
+ public int Task { get; set; }
+ }
+}
diff --git a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
index ce6412ffa..c2496e948 100644
--- a/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
+++ b/win/C#/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
@@ -63,6 +63,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="EncodeProgressEventArgs.cs" />
<Compile Include="frmExceptionWindow.cs">
<SubType>Form</SubType>
</Compile>
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
index 38c9b8086..d4e971e63 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
/// </summary>
private int processID;
+ /* Constructor */
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Encode"/> class.
+ /// </summary>
+ public Encode()
+ {
+ this.EncodeStarted += Encode_EncodeStarted;
+ }
+
+ /* Delegates */
+
+ /// <summary>
+ /// Encode Progess Status
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EncodeProgressEventArgs.
+ /// </param>
+ public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);
+
/* Event Handlers */
/// <summary>
@@ -68,12 +92,17 @@ namespace HandBrake.ApplicationServices.Services
/// </summary>
public event EventHandler EncodeEnded;
+ /// <summary>
+ /// Encode process has progressed
+ /// </summary>
+ public event EncodeProgessStatus EncodeStatusChanged;
+
/* Properties */
/// <summary>
/// Gets or sets The HB Process
/// </summary>
- public Process HbProcess { get; set; }
+ protected Process HbProcess { get; set; }
/// <summary>
/// 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());
}
/// <summary>
@@ -324,50 +359,6 @@ namespace HandBrake.ApplicationServices.Services
}
/// <summary>
- /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
- /// </summary>
- 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;
- }
- }
-
- /// <summary>
/// Add the CLI Query to the Log File.
/// </summary>
/// <param name="encJob">
@@ -550,5 +541,65 @@ namespace HandBrake.ApplicationServices.Services
logBuffer.AppendLine(e.Data);
}
}
+
+ /// <summary>
+ /// Encode Started
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EventArgs.
+ /// </param>
+ private void Encode_EncodeStarted(object sender, EventArgs e)
+ {
+ Thread monitor = new Thread(EncodeMonitor);
+ monitor.Start();
+ }
+
+ /// <summary>
+ /// Monitor the Job
+ /// </summary>
+ 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());
+ }
+ }
+
+ /// <summary>
+ /// Displays the Encode status in the GUI
+ /// </summary>
+ /// <param name="sender">The sender</param>
+ /// <param name="currentTask">The current task</param>
+ /// <param name="taskCount">Number of tasks</param>
+ /// <param name="percentComplete">Percent complete</param>
+ /// <param name="currentFps">Current encode speed in fps</param>
+ /// <param name="avg">Avg encode speed</param>
+ /// <param name="timeRemaining">Time Left</param>
+ 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 3b1a95dba..9a0681c2c 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;
/// <summary>
/// The IEncode Interface
@@ -24,9 +23,9 @@ namespace HandBrake.ApplicationServices.Services.Interfaces
event EventHandler EncodeEnded;
/// <summary>
- /// Gets or sets The HB Process
+ /// Encode process has progressed
/// </summary>
- Process HbProcess { get; set; }
+ event Encode.EncodeProgessStatus EncodeStatusChanged;
/// <summary>
/// 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 a01f13a1e..981f3c19e 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;
/// <summary>
/// The IQueue Interface
/// </summary>
- public interface IQueue
+ public interface IQueue : IEncode
{
/// <summary>
/// Fires when the Queue has started
@@ -59,11 +58,6 @@ namespace HandBrake.ApplicationServices.Services.Interfaces
int Count { get; }
/// <summary>
- /// Gets or sets The HB Process
- /// </summary>
- Process HbProcess { get; set; }
-
- /// <summary>
/// Gets a value indicating whether IsEncoding.
/// </summary>
bool IsEncoding { get; }
@@ -153,35 +147,5 @@ namespace HandBrake.ApplicationServices.Services.Interfaces
/// Requests a pause of the encode queue.
/// </summary>
void Pause();
-
- /// <summary>
- /// Fires when a new CLI Job starts
- /// </summary>
- event EventHandler EncodeStarted;
-
- /// <summary>
- /// Fires when a CLI job finishes.
- /// </summary>
- event EventHandler EncodeEnded;
-
- /// <summary>
- /// Create a preview sample video
- /// </summary>
- /// <param name="query">
- /// The CLI Query
- /// </param>
- void CreatePreviewSample(string query);
-
- /// <summary>
- /// Kill the CLI process
- /// </summary>
- void Stop();
-
- /// <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>
- 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 226ea68b1..0a1eb3c3d 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;
/// <summary>
@@ -38,6 +40,7 @@ namespace HandBrake.ApplicationServices.Services
/// </summary>
private int nextJobId;
+ #region Events
/// <summary>
/// Fires when the Queue has started
/// </summary>
@@ -58,6 +61,7 @@ namespace HandBrake.ApplicationServices.Services
/// Fires when the entire encode queue has completed.
/// </summary>
public event EventHandler QueueCompleted;
+ #endregion
#region Properties
/// <summary>
@@ -406,6 +410,41 @@ namespace HandBrake.ApplicationServices.Services
Finish();
}
+ /// <summary>
+ /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
+ /// </summary>
+ 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 3a4358bf7..d149ec6f7 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<DriveInformation> 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);
}
/// <summary>
@@ -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
}
/// <summary>
+ /// Display the Encode Status
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ 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);
+ }
+
+ /// <summary>
/// Set the DVD Drive selection in the "Source" Menu
/// </summary>
private void SetDriveSelectionMenuItem()
@@ -2312,7 +2332,7 @@ namespace Handbrake
/// <returns>
/// The title.
/// </returns>
- private int getTitle()
+ private int GetTitle()
{
int title = 0;
if (drp_dvdtitle.SelectedItem != null)
@@ -2330,12 +2350,12 @@ namespace Handbrake
/// <param name="result">
/// The result.
/// </param>
- 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
-
- /// <summary>
- /// Starts a new thread to monitor and process the CLI encode status
- /// </summary>
- 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);
- }
- }
-
- /// <summary>
- /// Displays the Encode status in the GUI
- /// </summary>
- /// <param name="sender">The sender</param>
- /// <param name="currentTask">The current task</param>
- /// <param name="taskCount">Number of tasks</param>
- /// <param name="percentComplete">Percent complete</param>
- /// <param name="currentFps">Current encode speed in fps</param>
- /// <param name="av">Avg encode speed</param>
- /// <param name="timeRemaining">Time Left</param>
- 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 775630358..d37a8f53e 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 41ceb5172..2c9e6c87d 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;
+ /// <summary>
+ /// The Preview Window
+ /// </summary>
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();
+ /// <summary>
+ /// The Main Window
+ /// </summary>
+ private readonly frmMain mainWindow;
+
+ /// <summary>
+ /// True if QT is not installed
+ /// </summary>
+ private readonly bool noQT;
+
+ /// <summary>
+ /// The encode queue
+ /// </summary>
+ private readonly IQueue encodeQueue = new Queue();
+
+ /// <summary>
+ /// What is currently playing
+ /// </summary>
+ private string currentlyPlaying = string.Empty;
+
+ /// <summary>
+ /// Play With VLC tracker
+ /// </summary>
private bool playWithVLC;
+ /// <summary>
+ /// A Thread for the video player
+ /// </summary>
+ private Thread player;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="frmPreview"/> class.
+ /// </summary>
+ /// <param name="mw">
+ /// The mw.
+ /// </param>
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)
+
+ /// <summary>
+ /// Update UI Delegate
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private delegate void UpdateUIHandler(object sender, EventArgs e);
+
+ /// <summary>
+ /// The Open Movie Handler
+ /// </summary>
+ private delegate void OpenMovieHandler();
+
+ /// <summary>
+ /// The encode has started
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private void EncodeQueue_EncodeStarted(object sender, EventArgs e)
{
- Thread encodeMon = new Thread(EncodeMonitorThread);
- encodeMon.Start();
+ encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged;
+ }
+
+ /// <summary>
+ /// The Enocde has ended
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ 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());
+ }
+ }
+
+ /// <summary>
+ /// Encode status has changed
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ 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)
+ /// <summary>
+ /// Play with VLC
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ 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)
+ /// <summary>
+ /// Encode and Play with QT
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ 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)
+ /// <summary>
+ /// Create the Preview.
+ /// </summary>
+ /// <param name="state">
+ /// The state.
+ /// </param>
+ 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
/// </summary>
private void Play()
{
- Player = new Thread(OpenMovie) {IsBackground = true};
- Player.Start();
+ this.player = new Thread(OpenMovie) { IsBackground = true };
+ this.player.Start();
}
/// <summary>
@@ -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
+ /// <summary>
+ /// Remove any subscribed events then close.
+ /// </summary>
+ /// <param name="e">
+ /// The e.
+ /// </param>
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);
}
}