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;
\r
14 using System.Windows.Forms;
\r
17 using HandBrake.ApplicationServices.Model;
\r
18 using HandBrake.ApplicationServices.Services;
\r
19 using HandBrake.ApplicationServices.Services.Interfaces;
\r
21 using QTOControlLib;
\r
24 using MessageBox = System.Windows.Forms.MessageBox;
\r
27 /// The Preview Window
\r
29 public partial class frmPreview : Form
\r
31 #region Private Variables
\r
36 private readonly frmMain mainWindow;
\r
39 /// True if QT is not installed
\r
41 private readonly bool noQt;
\r
44 /// The encode queue
\r
46 private readonly IEncode encodeQueue = new Encode();
\r
49 /// What is currently playing
\r
51 private string currentlyPlaying = string.Empty;
\r
54 /// Play With VLC tracker
\r
56 private bool playWithVlc;
\r
59 /// A Thread for the video player
\r
61 private Thread player;
\r
66 /// Initializes a new instance of the <see cref="frmPreview"/> class.
\r
68 /// <param name="mw">
\r
71 public frmPreview(frmMain mw)
\r
75 InitializeComponent();
\r
81 int borderWidth = (this.Width - this.ClientSize.Width) / 2;
\r
82 int titlebarAndBorder = this.Height - this.ClientSize.Height;
\r
84 this.Height = toolBar.Height + titlebarAndBorder + 1;
\r
85 btn_playQT.Enabled = false;
\r
86 btn_playQT.Visible = false;
\r
89 this.mainWindow = mw;
\r
91 cb_preview.SelectedIndex = 0;
\r
92 cb_duration.SelectedIndex = 1;
\r
94 cb_preview.Items.Clear();
\r
95 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
97 cb_preview.Items.Add(i.ToString());
\r
100 cb_preview.SelectedIndex = 0;
\r
102 encodeQueue.EncodeStarted += this.EncodeQueueEncodeStarted;
\r
103 encodeQueue.EncodeCompleted += this.EncodeQueueEncodeEnded;
\r
108 /// Update UI Delegate
\r
110 /// <param name="sender">
\r
113 /// <param name="e">
\r
116 private delegate void UpdateUiHandler(object sender, EventArgs e);
\r
119 /// The Open Movie Handler
\r
121 private delegate void OpenMovieHandler();
\r
124 #region Event Handlers
\r
126 /// The encode has started
\r
128 /// <param name="sender">
\r
131 /// <param name="e">
\r
134 private void EncodeQueueEncodeStarted(object sender, EventArgs e)
\r
136 encodeQueue.EncodeStatusChanged += this.EncodeQueueEncodeStatusChanged;
\r
140 /// The Enocde has ended
\r
142 /// <param name="sender">
\r
145 /// <param name="e">
\r
148 private void EncodeQueueEncodeEnded(object sender, EventArgs e)
\r
150 encodeQueue.EncodeStatusChanged -= this.EncodeQueueEncodeStatusChanged;
\r
154 if (this.InvokeRequired)
\r
156 this.BeginInvoke(new UpdateUiHandler(EncodeQueueEncodeEnded), new[] { sender, e });
\r
160 ProgressBarStatus.Visible = false;
\r
161 lbl_encodeStatus.Visible = false;
\r
164 btn_playQT.Enabled = true;
\r
165 btn_playVLC.Enabled = true;
\r
167 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
169 // Get the sample filename
\r
170 if (this.mainWindow.text_destination.Text != string.Empty)
\r
171 this.currentlyPlaying =
\r
172 this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
173 Replace(".mkv", "_sample.mkv");
\r
175 // Play back in QT or VLC
\r
181 catch (Exception exc)
\r
183 Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());
\r
188 /// Encode status has changed
\r
190 /// <param name="sender">
\r
193 /// <param name="e">
\r
196 private void EncodeQueueEncodeStatusChanged(object sender, HandBrake.ApplicationServices.EventArgs.EncodeProgressEventArgs e)
\r
198 if (this.InvokeRequired)
\r
200 this.BeginInvoke(new EncodeProgessStatus(this.EncodeQueueEncodeStatusChanged), new[] { sender, e });
\r
204 lbl_encodeStatus.Text = e.PercentComplete + "%";
\r
205 ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);
\r
209 #region Encode Sample
\r
214 /// <param name="sender">
\r
217 /// <param name="e">
\r
220 private void PlayVlcClick(object sender, EventArgs e)
\r
222 ProgressBarStatus.Visible = true;
\r
223 ProgressBarStatus.Value = 0;
\r
224 lbl_encodeStatus.Visible = true;
\r
225 playWithVlc = true;
\r
226 this.panel1.Visible = false;
\r
231 QTControl.URL = string.Empty;
\r
233 if (File.Exists(this.currentlyPlaying))
\r
234 File.Delete(this.currentlyPlaying);
\r
238 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
239 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
242 btn_playQT.Enabled = false;
\r
243 btn_playVLC.Enabled = false;
\r
244 this.Text += " (Encoding)";
\r
246 int.TryParse(cb_duration.Text, out duration);
\r
247 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
248 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
252 /// Encode and Play with QT
\r
254 /// <param name="sender">
\r
257 /// <param name="e">
\r
260 private void PlayQtClick(object sender, EventArgs e)
\r
262 playWithVlc = false;
\r
263 this.panel1.Visible = true;
\r
266 MessageBox.Show(this,
\r
267 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
268 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
271 if (this.mainWindow.text_destination.Text.Contains(".mkv"))
\r
273 MessageBox.Show(this,
\r
274 "The QuickTime Control does not support MKV files, It is recommended you use the VLC option instead.",
\r
275 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
279 ProgressBarStatus.Visible = true;
\r
280 ProgressBarStatus.Value = 0;
\r
281 lbl_encodeStatus.Visible = true;
\r
284 QTControl.URL = string.Empty;
\r
285 if (File.Exists(this.currentlyPlaying))
\r
286 File.Delete(this.currentlyPlaying);
\r
290 MessageBox.Show(this,
\r
291 "Unable to delete previous preview file. You may need to restart the application.",
\r
292 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
295 btn_playQT.Enabled = false;
\r
296 btn_playVLC.Enabled = false;
\r
297 this.Text += " (Encoding)";
\r
299 int.TryParse(cb_duration.Text, out duration);
\r
300 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
302 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
307 /// Create the Preview.
\r
309 /// <param name="state">
\r
312 private void CreatePreview(object state)
\r
314 // Make sure we are not already encoding and if we are then display an error.
\r
315 if (encodeQueue.IsEncoding)
\r
319 "Handbrake is already encoding a video!",
\r
321 MessageBoxButtons.OK,
\r
322 MessageBoxIcon.Warning);
\r
327 QueueTask task = new QueueTask((string)state);
\r
328 encodeQueue.Start(task, false);
\r
336 /// Play the video back in the QuickTime control
\r
338 private void Play()
\r
340 this.player = new Thread(OpenMovie) { IsBackground = true };
\r
341 this.player.Start();
\r
345 /// Play the video back in an external VLC Player
\r
347 private void PlayVlc()
\r
349 // Launch VLC and Play video.
\r
350 if (this.currentlyPlaying != string.Empty)
\r
352 if (File.Exists(this.currentlyPlaying))
\r
354 // Attempt to find VLC if it doesn't exist in the default set location.
\r
357 if (8 == IntPtr.Size ||
\r
358 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
359 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
361 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
363 vlcPath = vlcPath != null
\r
364 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
365 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
367 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
369 if (File.Exists(vlcPath))
\r
371 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
372 Properties.Settings.Default.Save(); // Save this new path if it does
\r
376 MessageBox.Show(this,
\r
377 "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
378 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
382 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
384 string args = "\"" + this.currentlyPlaying + "\"";
\r
385 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
386 Process.Start(vlc);
\r
390 MessageBox.Show(this,
\r
391 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
392 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
397 /// QT control - Open the file
\r
400 private void OpenMovie()
\r
404 if (InvokeRequired)
\r
406 BeginInvoke(new OpenMovieHandler(OpenMovie));
\r
409 QTControl.URL = this.currentlyPlaying;
\r
410 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
411 QTControl.URL = this.currentlyPlaying;
\r
414 this.ClientSize = QTControl.Size;
\r
415 this.Height += toolBar.Height;
\r
417 catch (COMException ex)
\r
419 QTUtils qtu = new QTUtils();
\r
420 Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));
\r
422 catch (Exception ex)
\r
424 Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());
\r
431 /// Remove any subscribed events then close.
\r
433 /// <param name="e">
\r
436 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
438 encodeQueue.EncodeStarted -= this.EncodeQueueEncodeStarted;
\r
439 encodeQueue.EncodeCompleted -= this.EncodeQueueEncodeEnded;
\r