OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
1 /*  frmPreview.cs $\r
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
5 \r
6 namespace Handbrake\r
7 {\r
8     using System;\r
9     using System.Diagnostics;\r
10     using System.IO;\r
11     using System.Runtime.InteropServices;\r
12     using System.Threading;\r
13     using System.Windows.Forms;\r
14     using Functions;\r
15 \r
16     using HandBrake.ApplicationServices.Services;\r
17     using HandBrake.ApplicationServices.Services.Interfaces;\r
18 \r
19     using QTOControlLib;\r
20     using QTOLibrary;\r
21 \r
22     /// <summary>\r
23     /// The Preview Window\r
24     /// </summary>\r
25     public partial class frmPreview : Form\r
26     {\r
27         #region Private Variables\r
28         /// <summary>\r
29         /// The Main Window\r
30         /// </summary>\r
31         private readonly frmMain mainWindow;\r
32 \r
33         /// <summary>\r
34         /// True if QT is not installed\r
35         /// </summary>\r
36         private readonly bool noQT;\r
37 \r
38         /// <summary>\r
39         /// The encode queue\r
40         /// </summary>\r
41         private readonly IQueue encodeQueue = new Queue();\r
42 \r
43         /// <summary>\r
44         /// What is currently playing\r
45         /// </summary>\r
46         private string currentlyPlaying = string.Empty;\r
47 \r
48         /// <summary>\r
49         /// Play With VLC tracker\r
50         /// </summary>\r
51         private bool playWithVLC;\r
52 \r
53         /// <summary>\r
54         /// A Thread for the video player\r
55         /// </summary>\r
56         private Thread player;\r
57 \r
58         /// <summary>\r
59         /// Initializes a new instance of the <see cref="frmPreview"/> class.\r
60         /// </summary>\r
61         /// <param name="mw">\r
62         /// The mw.\r
63         /// </param>\r
64         public frmPreview(frmMain mw)\r
65         {\r
66             try\r
67             {\r
68                 InitializeComponent();\r
69             }\r
70             catch (Exception)\r
71             {\r
72                 this.noQT = true;\r
73             }\r
74             this.mainWindow = mw;\r
75             cb_preview.SelectedIndex = 0;\r
76             cb_duration.SelectedIndex = 1;\r
77 \r
78             cb_preview.Items.Clear();\r
79             for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)\r
80                 cb_preview.Items.Add(i.ToString());\r
81             cb_preview.SelectedIndex = 0;\r
82 \r
83             encodeQueue.EncodeStarted += this.EncodeQueue_EncodeStarted;\r
84             encodeQueue.EncodeEnded += this.EncodeQueue_EncodeEnded;\r
85         }\r
86 \r
87         /// <summary>\r
88         /// Update UI Delegate\r
89         /// </summary>\r
90         /// <param name="sender">\r
91         /// The sender.\r
92         /// </param>\r
93         /// <param name="e">\r
94         /// The e.\r
95         /// </param>\r
96         private delegate void UpdateUIHandler(object sender, EventArgs e);\r
97 \r
98         /// <summary>\r
99         /// The Open Movie Handler\r
100         /// </summary>\r
101         private delegate void OpenMovieHandler();\r
102 \r
103         #endregion\r
104 \r
105         /// <summary>\r
106         /// The encode has started\r
107         /// </summary>\r
108         /// <param name="sender">\r
109         /// The sender.\r
110         /// </param>\r
111         /// <param name="e">\r
112         /// The e.\r
113         /// </param>\r
114         private void EncodeQueue_EncodeStarted(object sender, EventArgs e)\r
115         {\r
116             encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged;\r
117         }\r
118 \r
119         /// <summary>\r
120         /// The Enocde has ended\r
121         /// </summary>\r
122         /// <param name="sender">\r
123         /// The sender.\r
124         /// </param>\r
125         /// <param name="e">\r
126         /// The e.\r
127         /// </param>\r
128         private void EncodeQueue_EncodeEnded(object sender, EventArgs e)\r
129         {\r
130             encodeQueue.EncodeStatusChanged -= this.EncodeQueue_EncodeStatusChanged;\r
131 \r
132             try\r
133             {\r
134                 if (this.InvokeRequired)\r
135                 {\r
136                     this.BeginInvoke(new UpdateUIHandler(EncodeQueue_EncodeEnded), new[] { sender, e });\r
137                     return;\r
138                 }\r
139 \r
140                 ProgressBarStatus.Visible = false;\r
141                 lbl_encodeStatus.Visible = false;\r
142 \r
143                 if (!this.noQT)\r
144                     btn_playQT.Enabled = true;\r
145                 btn_playVLC.Enabled = true;\r
146 \r
147                 this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
148 \r
149                 // Get the sample filename\r
150                 if (this.mainWindow.text_destination.Text != string.Empty)\r
151                     this.currentlyPlaying =\r
152                         this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
153                             Replace(".mkv", "_sample.mkv");\r
154 \r
155                 // Play back in QT or VLC\r
156                 if (!playWithVLC)\r
157                     Play();\r
158                 else\r
159                     PlayVLC();\r
160             }\r
161             catch (Exception exc)\r
162             {\r
163                 Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());\r
164             }\r
165         }\r
166 \r
167         /// <summary>\r
168         /// Encode status has changed\r
169         /// </summary>\r
170         /// <param name="sender">\r
171         /// The sender.\r
172         /// </param>\r
173         /// <param name="e">\r
174         /// The e.\r
175         /// </param>\r
176         private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)\r
177         {\r
178             if (this.InvokeRequired)\r
179             {\r
180                 this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueue_EncodeStatusChanged), new[] { sender, e });\r
181                 return;\r
182             }\r
183 \r
184             lbl_encodeStatus.Text = e.PercentComplete + "%";\r
185             ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);\r
186         }\r
187 \r
188         #region Encode Sample\r
189 \r
190         /// <summary>\r
191         /// Play with VLC\r
192         /// </summary>\r
193         /// <param name="sender">\r
194         /// The sender.\r
195         /// </param>\r
196         /// <param name="e">\r
197         /// The e.\r
198         /// </param>\r
199         private void PlayVLC_Click(object sender, EventArgs e)\r
200         {\r
201             ProgressBarStatus.Visible = true;\r
202             ProgressBarStatus.Value = 0;\r
203             lbl_encodeStatus.Visible = true;\r
204             playWithVLC = true;\r
205 \r
206             try\r
207             {\r
208                 if (!this.noQT)\r
209                     QTControl.URL = string.Empty;\r
210 \r
211                 if (File.Exists(this.currentlyPlaying))\r
212                     File.Delete(this.currentlyPlaying);\r
213             }\r
214             catch (Exception)\r
215             {\r
216                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",\r
217                                 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
218             }\r
219 \r
220             btn_playQT.Enabled = false;\r
221             btn_playVLC.Enabled = false;\r
222             this.Text += " (Encoding)";\r
223             int duration;\r
224             int.TryParse(cb_duration.Text, out duration);\r
225             string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
226             ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
227         }\r
228 \r
229         /// <summary>\r
230         /// Encode and Play with QT\r
231         /// </summary>\r
232         /// <param name="sender">\r
233         /// The sender.\r
234         /// </param>\r
235         /// <param name="e">\r
236         /// The e.\r
237         /// </param>\r
238         private void PlayQT_Click(object sender, EventArgs e)\r
239         {\r
240             playWithVLC = false;\r
241             if (this.noQT)\r
242             {\r
243                 MessageBox.Show(this,\r
244                                 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",\r
245                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
246                 return;\r
247             }\r
248             if (this.mainWindow.text_destination.Text.Contains(".mkv"))\r
249             {\r
250                 MessageBox.Show(this,\r
251                                 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",\r
252                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
253             }\r
254             else\r
255             {\r
256                 ProgressBarStatus.Visible = true;\r
257                 ProgressBarStatus.Value = 0;\r
258                 lbl_encodeStatus.Visible = true;\r
259                 try\r
260                 {\r
261                     QTControl.URL = string.Empty;\r
262                     if (File.Exists(this.currentlyPlaying))\r
263                         File.Delete(this.currentlyPlaying);\r
264                 }\r
265                 catch (Exception)\r
266                 {\r
267                     MessageBox.Show(this,\r
268                                     "Unable to delete previous preview file. You may need to restart the application.",\r
269                                     "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
270                 }\r
271 \r
272                 btn_playQT.Enabled = false;\r
273                 btn_playVLC.Enabled = false;\r
274                 this.Text += " (Encoding)";\r
275                 int duration;\r
276                 int.TryParse(cb_duration.Text, out duration);\r
277                 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
278 \r
279                 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
280             }\r
281         }\r
282 \r
283         /// <summary>\r
284         /// Create the Preview.\r
285         /// </summary>\r
286         /// <param name="state">\r
287         /// The state.\r
288         /// </param>\r
289         private void CreatePreview(object state)\r
290         {\r
291             // Make sure we are not already encoding and if we are then display an error.\r
292             if (encodeQueue.IsEncoding)\r
293             {\r
294                 MessageBox.Show(\r
295                     this,\r
296                     "Handbrake is already encoding a video!",\r
297                     "Status",\r
298                     MessageBoxButtons.OK,\r
299                     MessageBoxIcon.Warning);\r
300 \r
301                 return;\r
302             }\r
303 \r
304             encodeQueue.CreatePreviewSample((string)state);\r
305         }\r
306 \r
307         #endregion\r
308 \r
309         #region Playback\r
310 \r
311         /// <summary>\r
312         /// Play the video back in the QuickTime control\r
313         /// </summary>\r
314         private void Play()\r
315         {\r
316             this.player = new Thread(OpenMovie) { IsBackground = true };\r
317             this.player.Start();\r
318         }\r
319 \r
320         /// <summary>\r
321         /// Play the video back in an external VLC Player\r
322         /// </summary>\r
323         private void PlayVLC()\r
324         {\r
325             // Launch VLC and Play video.\r
326             if (this.currentlyPlaying != string.Empty)\r
327             {\r
328                 if (File.Exists(this.currentlyPlaying))\r
329                 {\r
330                     // Attempt to find VLC if it doesn't exist in the default set location.\r
331                     string vlcPath;\r
332 \r
333                     if (8 == IntPtr.Size ||\r
334                         (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
335                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
336                     else\r
337                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
338 \r
339                     vlcPath = vlcPath != null\r
340                                   ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
341                                   : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
342 \r
343                     if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
344                     {\r
345                         if (File.Exists(vlcPath))\r
346                         {\r
347                             Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
348                             Properties.Settings.Default.Save(); // Save this new path if it does\r
349                         }\r
350                         else\r
351                         {\r
352                             MessageBox.Show(this,\r
353                                             "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
354                                             "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
355                         }\r
356                     }\r
357 \r
358                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
359                     {\r
360                         string args = "\"" + this.currentlyPlaying + "\"";\r
361                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
362                         Process.Start(vlc);\r
363                     }\r
364                 }\r
365                 else\r
366                     MessageBox.Show(this,\r
367                                     "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",\r
368                                     "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
369             }\r
370         }\r
371 \r
372         /// <summary>\r
373         /// QT control - Open the file\r
374         /// </summary>\r
375         [STAThread]\r
376         private void OpenMovie()\r
377         {\r
378             try\r
379             {\r
380                 if (InvokeRequired)\r
381                 {\r
382                     BeginInvoke(new OpenMovieHandler(OpenMovie));\r
383                     return;\r
384                 }\r
385                 QTControl.URL = this.currentlyPlaying;\r
386                 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
387                 QTControl.URL = this.currentlyPlaying;\r
388                 QTControl.Show();\r
389 \r
390                 this.ClientSize = QTControl.Size;\r
391                 this.Height += 25;\r
392             }\r
393             catch (COMException ex)\r
394             {\r
395                 QTUtils qtu = new QTUtils();\r
396                 Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));\r
397             }\r
398             catch (Exception ex)\r
399             {\r
400                 Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());\r
401             }\r
402         }\r
403 \r
404         #endregion\r
405 \r
406         /// <summary>\r
407         /// Remove any subscribed events then close.\r
408         /// </summary>\r
409         /// <param name="e">\r
410         /// The e.\r
411         /// </param>\r
412         protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
413         {\r
414             encodeQueue.EncodeStarted -= this.EncodeQueue_EncodeStarted;\r
415             encodeQueue.EncodeEnded -= this.EncodeQueue_EncodeEnded;\r
416             base.OnClosing(e);\r
417         }\r
418     }\r
419 }