2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.Diagnostics;
\r
11 using System.Runtime.InteropServices;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
16 using HandBrake.ApplicationServices.Services;
\r
17 using HandBrake.ApplicationServices.Services.Interfaces;
\r
19 using QTOControlLib;
\r
23 /// The Preview Window
\r
25 public partial class frmPreview : Form
\r
27 #region Private Variables
\r
32 private readonly frmMain mainWindow;
\r
35 /// True if QT is not installed
\r
37 private readonly bool noQt;
\r
40 /// The encode queue
\r
42 private readonly IQueue encodeQueue = new Queue();
\r
45 /// What is currently playing
\r
47 private string currentlyPlaying = string.Empty;
\r
50 /// Play With VLC tracker
\r
52 private bool playWithVlc;
\r
55 /// A Thread for the video player
\r
57 private Thread player;
\r
62 /// Initializes a new instance of the <see cref="frmPreview"/> class.
\r
64 /// <param name="mw">
\r
67 public frmPreview(frmMain mw)
\r
71 InitializeComponent();
\r
78 this.mainWindow = mw;
\r
80 cb_preview.SelectedIndex = 0;
\r
81 cb_duration.SelectedIndex = 1;
\r
83 cb_preview.Items.Clear();
\r
84 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
86 cb_preview.Items.Add(i.ToString());
\r
89 cb_preview.SelectedIndex = 0;
\r
91 encodeQueue.EncodeStarted += this.EncodeQueueEncodeStarted;
\r
92 encodeQueue.EncodeEnded += this.EncodeQueueEncodeEnded;
\r
97 /// Update UI Delegate
\r
99 /// <param name="sender">
\r
102 /// <param name="e">
\r
105 private delegate void UpdateUiHandler(object sender, EventArgs e);
\r
108 /// The Open Movie Handler
\r
110 private delegate void OpenMovieHandler();
\r
113 #region Event Handlers
\r
115 /// The encode has started
\r
117 /// <param name="sender">
\r
120 /// <param name="e">
\r
123 private void EncodeQueueEncodeStarted(object sender, EventArgs e)
\r
125 encodeQueue.EncodeStatusChanged += this.EncodeQueueEncodeStatusChanged;
\r
129 /// The Enocde has ended
\r
131 /// <param name="sender">
\r
134 /// <param name="e">
\r
137 private void EncodeQueueEncodeEnded(object sender, EventArgs e)
\r
139 encodeQueue.EncodeStatusChanged -= this.EncodeQueueEncodeStatusChanged;
\r
143 if (this.InvokeRequired)
\r
145 this.BeginInvoke(new UpdateUiHandler(EncodeQueueEncodeEnded), new[] { sender, e });
\r
149 ProgressBarStatus.Visible = false;
\r
150 lbl_encodeStatus.Visible = false;
\r
153 btn_playQT.Enabled = true;
\r
154 btn_playVLC.Enabled = true;
\r
156 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
158 // Get the sample filename
\r
159 if (this.mainWindow.text_destination.Text != string.Empty)
\r
160 this.currentlyPlaying =
\r
161 this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
162 Replace(".mkv", "_sample.mkv");
\r
164 // Play back in QT or VLC
\r
170 catch (Exception exc)
\r
172 Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());
\r
177 /// Encode status has changed
\r
179 /// <param name="sender">
\r
182 /// <param name="e">
\r
185 private void EncodeQueueEncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)
\r
187 if (this.InvokeRequired)
\r
189 this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueueEncodeStatusChanged), new[] { sender, e });
\r
193 lbl_encodeStatus.Text = e.PercentComplete + "%";
\r
194 ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);
\r
198 #region Encode Sample
\r
203 /// <param name="sender">
\r
206 /// <param name="e">
\r
209 private void PlayVlcClick(object sender, EventArgs e)
\r
211 ProgressBarStatus.Visible = true;
\r
212 ProgressBarStatus.Value = 0;
\r
213 lbl_encodeStatus.Visible = true;
\r
214 playWithVlc = true;
\r
215 this.panel1.Visible = false;
\r
220 QTControl.URL = string.Empty;
\r
222 if (File.Exists(this.currentlyPlaying))
\r
223 File.Delete(this.currentlyPlaying);
\r
227 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
228 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
231 btn_playQT.Enabled = false;
\r
232 btn_playVLC.Enabled = false;
\r
233 this.Text += " (Encoding)";
\r
235 int.TryParse(cb_duration.Text, out duration);
\r
236 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
237 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
241 /// Encode and Play with QT
\r
243 /// <param name="sender">
\r
246 /// <param name="e">
\r
249 private void PlayQtClick(object sender, EventArgs e)
\r
251 playWithVlc = false;
\r
252 this.panel1.Visible = true;
\r
255 MessageBox.Show(this,
\r
256 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
257 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
260 if (this.mainWindow.text_destination.Text.Contains(".mkv"))
\r
262 MessageBox.Show(this,
\r
263 "The QuickTime Control does not support MKV files, It is recommended you use the VLC option instead.",
\r
264 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
268 ProgressBarStatus.Visible = true;
\r
269 ProgressBarStatus.Value = 0;
\r
270 lbl_encodeStatus.Visible = true;
\r
273 QTControl.URL = string.Empty;
\r
274 if (File.Exists(this.currentlyPlaying))
\r
275 File.Delete(this.currentlyPlaying);
\r
279 MessageBox.Show(this,
\r
280 "Unable to delete previous preview file. You may need to restart the application.",
\r
281 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
284 btn_playQT.Enabled = false;
\r
285 btn_playVLC.Enabled = false;
\r
286 this.Text += " (Encoding)";
\r
288 int.TryParse(cb_duration.Text, out duration);
\r
289 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
291 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
296 /// Create the Preview.
\r
298 /// <param name="state">
\r
301 private void CreatePreview(object state)
\r
303 // Make sure we are not already encoding and if we are then display an error.
\r
304 if (encodeQueue.IsEncoding)
\r
308 "Handbrake is already encoding a video!",
\r
310 MessageBoxButtons.OK,
\r
311 MessageBoxIcon.Warning);
\r
316 encodeQueue.CreatePreviewSample((string)state);
\r
324 /// Play the video back in the QuickTime control
\r
326 private void Play()
\r
328 this.player = new Thread(OpenMovie) { IsBackground = true };
\r
329 this.player.Start();
\r
333 /// Play the video back in an external VLC Player
\r
335 private void PlayVlc()
\r
337 // Launch VLC and Play video.
\r
338 if (this.currentlyPlaying != string.Empty)
\r
340 if (File.Exists(this.currentlyPlaying))
\r
342 // Attempt to find VLC if it doesn't exist in the default set location.
\r
345 if (8 == IntPtr.Size ||
\r
346 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
347 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
349 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
351 vlcPath = vlcPath != null
\r
352 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
353 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
355 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
357 if (File.Exists(vlcPath))
\r
359 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
360 Properties.Settings.Default.Save(); // Save this new path if it does
\r
364 MessageBox.Show(this,
\r
365 "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
366 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
370 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
372 string args = "\"" + this.currentlyPlaying + "\"";
\r
373 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
374 Process.Start(vlc);
\r
378 MessageBox.Show(this,
\r
379 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
380 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
385 /// QT control - Open the file
\r
388 private void OpenMovie()
\r
392 if (InvokeRequired)
\r
394 BeginInvoke(new OpenMovieHandler(OpenMovie));
\r
397 QTControl.URL = this.currentlyPlaying;
\r
398 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
399 QTControl.URL = this.currentlyPlaying;
\r
402 this.ClientSize = QTControl.Size;
\r
405 catch (COMException ex)
\r
407 QTUtils qtu = new QTUtils();
\r
408 Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));
\r
410 catch (Exception ex)
\r
412 Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());
\r
419 /// Remove any subscribed events then close.
\r
421 /// <param name="e">
\r
424 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
426 encodeQueue.EncodeStarted -= this.EncodeQueueEncodeStarted;
\r
427 encodeQueue.EncodeEnded -= this.EncodeQueueEncodeEnded;
\r