using System.Windows.Forms;\r
using Functions;\r
\r
- using HandBrake.ApplicationServices.Parsing;\r
using HandBrake.ApplicationServices.Services;\r
+ using HandBrake.ApplicationServices.Services.Interfaces;\r
\r
using QTOControlLib;\r
using QTOLibrary;\r
\r
+ /// <summary>\r
+ /// The Preview Window\r
+ /// </summary>\r
public partial class frmPreview : Form\r
{\r
- private string CurrentlyPlaying = string.Empty;\r
- private readonly frmMain MainWindow;\r
- private Thread Player;\r
- private readonly bool NoQT;\r
- private readonly Queue Process = new Queue();\r
- private delegate void UpdateUIHandler();\r
+ #region Private Variables\r
+ /// <summary>\r
+ /// The Main Window\r
+ /// </summary>\r
+ private readonly frmMain mainWindow;\r
+\r
+ /// <summary>\r
+ /// True if QT is not installed\r
+ /// </summary>\r
+ private readonly bool noQT;\r
+\r
+ /// <summary>\r
+ /// The encode queue\r
+ /// </summary>\r
+ private readonly IQueue encodeQueue = new Queue();\r
+\r
+ /// <summary>\r
+ /// What is currently playing\r
+ /// </summary>\r
+ private string currentlyPlaying = string.Empty;\r
+\r
+ /// <summary>\r
+ /// Play With VLC tracker\r
+ /// </summary>\r
private bool playWithVLC;\r
\r
+ /// <summary>\r
+ /// A Thread for the video player\r
+ /// </summary>\r
+ private Thread player;\r
+\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="frmPreview"/> class.\r
+ /// </summary>\r
+ /// <param name="mw">\r
+ /// The mw.\r
+ /// </param>\r
public frmPreview(frmMain mw)\r
{\r
try\r
}\r
catch (Exception)\r
{\r
- NoQT = true;\r
+ this.noQT = true;\r
}\r
- this.MainWindow = mw;\r
+ this.mainWindow = mw;\r
cb_preview.SelectedIndex = 0;\r
cb_duration.SelectedIndex = 1;\r
\r
cb_preview.Items.Add(i.ToString());\r
cb_preview.SelectedIndex = 0;\r
\r
- Process.EncodeStarted += new EventHandler(Process_EncodeStarted);\r
+ encodeQueue.EncodeStarted += this.EncodeQueue_EncodeStarted;\r
+ encodeQueue.EncodeEnded += this.EncodeQueue_EncodeEnded;\r
+ }\r
+\r
+ /// <summary>\r
+ /// Update UI Delegate\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private delegate void UpdateUIHandler(object sender, EventArgs e);\r
+\r
+ /// <summary>\r
+ /// The Open Movie Handler\r
+ /// </summary>\r
+ private delegate void OpenMovieHandler();\r
+\r
+ #endregion\r
+\r
+ /// <summary>\r
+ /// The encode has started\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void EncodeQueue_EncodeStarted(object sender, EventArgs e)\r
+ {\r
+ encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged;\r
}\r
- private void Process_EncodeStarted(object sender, EventArgs e)\r
+\r
+ /// <summary>\r
+ /// The Enocde has ended\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void EncodeQueue_EncodeEnded(object sender, EventArgs e)\r
{\r
- Thread encodeMon = new Thread(EncodeMonitorThread);\r
- encodeMon.Start();\r
+ encodeQueue.EncodeStatusChanged -= this.EncodeQueue_EncodeStatusChanged;\r
+\r
+ try\r
+ {\r
+ if (this.InvokeRequired)\r
+ {\r
+ this.BeginInvoke(new UpdateUIHandler(EncodeQueue_EncodeEnded), new[] { sender, e });\r
+ return;\r
+ }\r
+\r
+ ProgressBarStatus.Visible = false;\r
+ lbl_encodeStatus.Visible = false;\r
+\r
+ if (!this.noQT)\r
+ btn_playQT.Enabled = true;\r
+ btn_playVLC.Enabled = true;\r
+\r
+ this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
+\r
+ // Get the sample filename\r
+ if (this.mainWindow.text_destination.Text != string.Empty)\r
+ this.currentlyPlaying =\r
+ this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
+ Replace(".mkv", "_sample.mkv");\r
+\r
+ // Play back in QT or VLC\r
+ if (!playWithVLC)\r
+ Play();\r
+ else\r
+ PlayVLC();\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Encode status has changed\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)\r
+ {\r
+ if (this.InvokeRequired)\r
+ {\r
+ this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueue_EncodeStatusChanged), new[] { sender, e });\r
+ return;\r
+ }\r
+\r
+ lbl_encodeStatus.Text = e.PercentComplete + "%";\r
+ ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);\r
}\r
\r
#region Encode Sample\r
\r
- private void btn_playVLC_Click(object sender, EventArgs e)\r
+ /// <summary>\r
+ /// Play with VLC\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void PlayVLC_Click(object sender, EventArgs e)\r
{\r
ProgressBarStatus.Visible = true;\r
ProgressBarStatus.Value = 0;\r
\r
try\r
{\r
- if (!NoQT)\r
+ if (!this.noQT)\r
QTControl.URL = string.Empty;\r
\r
- if (File.Exists(CurrentlyPlaying))\r
- File.Delete(CurrentlyPlaying);\r
+ if (File.Exists(this.currentlyPlaying))\r
+ File.Delete(this.currentlyPlaying);\r
}\r
catch (Exception)\r
{\r
- MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", \r
+ MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",\r
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
}\r
\r
this.Text += " (Encoding)";\r
int duration;\r
int.TryParse(cb_duration.Text, out duration);\r
- string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
- ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
+ string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
+ ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
}\r
\r
- private void btn_playQT_Click(object sender, EventArgs e)\r
+ /// <summary>\r
+ /// Encode and Play with QT\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void PlayQT_Click(object sender, EventArgs e)\r
{\r
playWithVLC = false;\r
- if (NoQT)\r
+ if (this.noQT)\r
{\r
- MessageBox.Show(this, \r
- "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", \r
+ MessageBox.Show(this,\r
+ "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",\r
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
return;\r
}\r
- if (MainWindow.text_destination.Text.Contains(".mkv"))\r
+ if (this.mainWindow.text_destination.Text.Contains(".mkv"))\r
{\r
- MessageBox.Show(this, \r
- "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", \r
+ MessageBox.Show(this,\r
+ "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",\r
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
}\r
else\r
try\r
{\r
QTControl.URL = string.Empty;\r
- if (File.Exists(CurrentlyPlaying))\r
- File.Delete(CurrentlyPlaying);\r
+ if (File.Exists(this.currentlyPlaying))\r
+ File.Delete(this.currentlyPlaying);\r
}\r
catch (Exception)\r
{\r
- MessageBox.Show(this, \r
- "Unable to delete previous preview file. You may need to restart the application.", \r
+ MessageBox.Show(this,\r
+ "Unable to delete previous preview file. You may need to restart the application.",\r
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
}\r
\r
this.Text += " (Encoding)";\r
int duration;\r
int.TryParse(cb_duration.Text, out duration);\r
- string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
+ string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
\r
- ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
+ ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
}\r
}\r
\r
- private void ProcMonitor(object state)\r
+ /// <summary>\r
+ /// Create the Preview.\r
+ /// </summary>\r
+ /// <param name="state">\r
+ /// The state.\r
+ /// </param>\r
+ private void CreatePreview(object state)\r
{\r
// Make sure we are not already encoding and if we are then display an error.\r
- if (Process.HbProcess != null)\r
- MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, \r
- MessageBoxIcon.Warning);\r
- else\r
- {\r
- Process.CreatePreviewSample((string) state);\r
-\r
- if (Process.HbProcess != null)\r
- {\r
- Process.HbProcess.WaitForExit();\r
- Process.HbProcess = null;\r
- }\r
- EncodeCompleted();\r
- }\r
- }\r
-\r
- private void EncodeMonitorThread()\r
- {\r
- try\r
+ if (encodeQueue.IsEncoding)\r
{\r
- Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);\r
- encode.OnEncodeProgress += EncodeOnEncodeProgress;\r
- while (!encode.EndOfStream)\r
- encode.ReadEncodeStatus();\r
- }\r
- catch (Exception exc)\r
- {\r
- MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
- }\r
- }\r
+ MessageBox.Show(\r
+ this,\r
+ "Handbrake is already encoding a video!",\r
+ "Status",\r
+ MessageBoxButtons.OK,\r
+ MessageBoxIcon.Warning);\r
\r
- private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)\r
- {\r
- if (this.InvokeRequired)\r
- {\r
- this.BeginInvoke(\r
- new EncodeProgressEventHandler(EncodeOnEncodeProgress),\r
- new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });\r
return;\r
}\r
- lbl_encodeStatus.Text = PercentComplete + "%";\r
- ProgressBarStatus.Value = (int)Math.Round(PercentComplete);\r
- }\r
-\r
- private void EncodeCompleted()\r
- {\r
- try\r
- {\r
- if (InvokeRequired)\r
- {\r
- BeginInvoke(new UpdateUIHandler(EncodeCompleted));\r
- return;\r
- }\r
-\r
- ProgressBarStatus.Visible = false;\r
- lbl_encodeStatus.Visible = false;\r
\r
- if (!NoQT)\r
- btn_playQT.Enabled = true;\r
- btn_playVLC.Enabled = true;\r
-\r
- this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
-\r
- // Get the sample filename\r
- if (MainWindow.text_destination.Text != string.Empty)\r
- CurrentlyPlaying =\r
- MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
- Replace(".mkv", "_sample.mkv");\r
-\r
- // Play back in QT or VLC\r
- if (!playWithVLC)\r
- Play();\r
- else\r
- PlayVLC();\r
- }\r
- catch (Exception exc)\r
- {\r
- MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, \r
- MessageBoxIcon.Error);\r
- }\r
+ encodeQueue.CreatePreviewSample((string)state);\r
}\r
\r
#endregion\r
/// </summary>\r
private void Play()\r
{\r
- Player = new Thread(OpenMovie) {IsBackground = true};\r
- Player.Start();\r
+ this.player = new Thread(OpenMovie) { IsBackground = true };\r
+ this.player.Start();\r
}\r
\r
/// <summary>\r
private void PlayVLC()\r
{\r
// Launch VLC and Play video.\r
- if (CurrentlyPlaying != string.Empty)\r
+ if (this.currentlyPlaying != string.Empty)\r
{\r
- if (File.Exists(CurrentlyPlaying))\r
+ if (File.Exists(this.currentlyPlaying))\r
{\r
// Attempt to find VLC if it doesn't exist in the default set location.\r
string vlcPath;\r
}\r
else\r
{\r
- MessageBox.Show(this, \r
- "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\") ", \r
+ MessageBox.Show(this,\r
+ "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\") ",\r
"VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
}\r
}\r
\r
if (File.Exists(Properties.Settings.Default.VLC_Path))\r
{\r
- string args = "\"" + CurrentlyPlaying + "\"";\r
+ string args = "\"" + this.currentlyPlaying + "\"";\r
ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
- System.Diagnostics.Process.Start(vlc);\r
+ Process.Start(vlc);\r
}\r
}\r
else\r
- MessageBox.Show(this, \r
- "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", \r
+ MessageBox.Show(this,\r
+ "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",\r
"VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
}\r
}\r
{\r
if (InvokeRequired)\r
{\r
- BeginInvoke(new UpdateUIHandler(OpenMovie));\r
+ BeginInvoke(new OpenMovieHandler(OpenMovie));\r
return;\r
}\r
- QTControl.URL = CurrentlyPlaying;\r
+ QTControl.URL = this.currentlyPlaying;\r
QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
- QTControl.URL = CurrentlyPlaying;\r
+ QTControl.URL = this.currentlyPlaying;\r
QTControl.Show();\r
\r
this.ClientSize = QTControl.Size;\r
catch (COMException ex)\r
{\r
QTUtils qtu = new QTUtils();\r
- MessageBox.Show(this, \r
- "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +\r
- "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", \r
- MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+ Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));\r
}\r
catch (Exception ex)\r
{\r
- MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, \r
- MessageBoxIcon.Warning);\r
+ Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());\r
}\r
}\r
\r
#endregion\r
\r
+ /// <summary>\r
+ /// Remove any subscribed events then close.\r
+ /// </summary>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
{\r
- Process.EncodeStarted -= Process_EncodeStarted;\r
+ encodeQueue.EncodeStarted -= this.EncodeQueue_EncodeStarted;\r
+ encodeQueue.EncodeEnded -= this.EncodeQueue_EncodeEnded;\r
base.OnClosing(e);\r
}\r
}\r