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     using QTOControlLib;\r
16     using QTOLibrary;\r
17     using Services;\r
18     using Parsing;\r
19 \r
20     public partial class frmPreview : Form\r
21     {\r
22         private string CurrentlyPlaying = string.Empty;\r
23         private readonly frmMain MainWindow;\r
24         private Thread Player;\r
25         private readonly bool NoQT;\r
26         private readonly Queue Process = new Queue();\r
27         private delegate void UpdateUIHandler();\r
28         private bool playWithVLC;\r
29 \r
30         public frmPreview(frmMain mw)\r
31         {\r
32             try\r
33             {\r
34                 InitializeComponent();\r
35             }\r
36             catch (Exception)\r
37             {\r
38                 NoQT = true;\r
39             }\r
40             this.MainWindow = mw;\r
41             cb_preview.SelectedIndex = 0;\r
42             cb_duration.SelectedIndex = 1;\r
43 \r
44             cb_preview.Items.Clear();\r
45             for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)\r
46                 cb_preview.Items.Add(i.ToString());\r
47             cb_preview.SelectedIndex = 0;\r
48 \r
49             Process.EncodeStarted += new EventHandler(Process_EncodeStarted);\r
50         }\r
51         private void Process_EncodeStarted(object sender, EventArgs e)\r
52         {\r
53             Thread encodeMon = new Thread(EncodeMonitorThread);\r
54             encodeMon.Start();\r
55         }\r
56 \r
57         #region Encode Sample\r
58 \r
59         private void btn_playVLC_Click(object sender, EventArgs e)\r
60         {\r
61             ProgressBarStatus.Visible = true;\r
62             ProgressBarStatus.Value = 0;\r
63             lbl_encodeStatus.Visible = true;\r
64             playWithVLC = true;\r
65 \r
66             try\r
67             {\r
68                 if (!NoQT)\r
69                     QTControl.URL = string.Empty;\r
70 \r
71                 if (File.Exists(CurrentlyPlaying))\r
72                     File.Delete(CurrentlyPlaying);\r
73             }\r
74             catch (Exception)\r
75             {\r
76                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", \r
77                                 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
78             }\r
79 \r
80             btn_playQT.Enabled = false;\r
81             btn_playVLC.Enabled = false;\r
82             this.Text += " (Encoding)";\r
83             int duration;\r
84             int.TryParse(cb_duration.Text, out duration);\r
85             string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
86             ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
87         }\r
88 \r
89         private void btn_playQT_Click(object sender, EventArgs e)\r
90         {\r
91             playWithVLC = false;\r
92             if (NoQT)\r
93             {\r
94                 MessageBox.Show(this, \r
95                                 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", \r
96                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
97                 return;\r
98             }\r
99             if (MainWindow.text_destination.Text.Contains(".mkv"))\r
100             {\r
101                 MessageBox.Show(this, \r
102                                 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", \r
103                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
104             }\r
105             else\r
106             {\r
107                 ProgressBarStatus.Visible = true;\r
108                 ProgressBarStatus.Value = 0;\r
109                 lbl_encodeStatus.Visible = true;\r
110                 try\r
111                 {\r
112                     QTControl.URL = string.Empty;\r
113                     if (File.Exists(CurrentlyPlaying))\r
114                         File.Delete(CurrentlyPlaying);\r
115                 }\r
116                 catch (Exception)\r
117                 {\r
118                     MessageBox.Show(this, \r
119                                     "Unable to delete previous preview file. You may need to restart the application.", \r
120                                     "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
121                 }\r
122 \r
123                 btn_playQT.Enabled = false;\r
124                 btn_playVLC.Enabled = false;\r
125                 this.Text += " (Encoding)";\r
126                 int duration;\r
127                 int.TryParse(cb_duration.Text, out duration);\r
128                 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
129 \r
130                 ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
131             }\r
132         }\r
133 \r
134         private void ProcMonitor(object state)\r
135         {\r
136             // Make sure we are not already encoding and if we are then display an error.\r
137             if (Process.HbProcess != null)\r
138                 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, \r
139                                 MessageBoxIcon.Warning);\r
140             else\r
141             {\r
142                 Process.CreatePreviewSample((string) state);\r
143 \r
144                 if (Process.HbProcess != null)\r
145                 {\r
146                     Process.HbProcess.WaitForExit();\r
147                     Process.HbProcess = null;\r
148                 }\r
149                 EncodeCompleted();\r
150             }\r
151         }\r
152 \r
153         private void EncodeMonitorThread()\r
154         {\r
155             try\r
156             {\r
157                 Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);\r
158                 encode.OnEncodeProgress += EncodeOnEncodeProgress;\r
159                 while (!encode.EndOfStream)\r
160                     encode.ReadEncodeStatus();\r
161             }\r
162             catch (Exception exc)\r
163             {\r
164                 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
165             }\r
166         }\r
167 \r
168         private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)\r
169         {\r
170             if (this.InvokeRequired)\r
171             {\r
172                 this.BeginInvoke(\r
173                     new EncodeProgressEventHandler(EncodeOnEncodeProgress),\r
174                     new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });\r
175                 return;\r
176             }\r
177             lbl_encodeStatus.Text = PercentComplete + "%";\r
178             ProgressBarStatus.Value = (int)Math.Round(PercentComplete);\r
179         }\r
180 \r
181 \r
182         private void EncodeCompleted()\r
183         {\r
184             try\r
185             {\r
186                 if (InvokeRequired)\r
187                 {\r
188                     BeginInvoke(new UpdateUIHandler(EncodeCompleted));\r
189                     return;\r
190                 }\r
191 \r
192                 ProgressBarStatus.Visible = false;\r
193                 lbl_encodeStatus.Visible = false;\r
194 \r
195                 if (!NoQT)\r
196                     btn_playQT.Enabled = true;\r
197                 btn_playVLC.Enabled = true;\r
198 \r
199                 this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
200 \r
201                 // Get the sample filename\r
202                 if (MainWindow.text_destination.Text != string.Empty)\r
203                     CurrentlyPlaying =\r
204                         MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
205                             Replace(".mkv", "_sample.mkv");\r
206 \r
207                 // Play back in QT or VLC\r
208                 if (!playWithVLC)\r
209                     Play();\r
210                 else\r
211                     PlayVLC();\r
212             }\r
213             catch (Exception exc)\r
214             {\r
215                 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, \r
216                                 MessageBoxIcon.Error);\r
217             }\r
218         }\r
219 \r
220         #endregion\r
221 \r
222         #region Playback\r
223 \r
224         /// <summary>\r
225         /// Play the video back in the QuickTime control\r
226         /// </summary>\r
227         private void Play()\r
228         {\r
229             Player = new Thread(OpenMovie) {IsBackground = true};\r
230             Player.Start();\r
231         }\r
232 \r
233         /// <summary>\r
234         /// Play the video back in an external VLC Player\r
235         /// </summary>\r
236         private void PlayVLC()\r
237         {\r
238             // Launch VLC and Play video.\r
239             if (CurrentlyPlaying != string.Empty)\r
240             {\r
241                 if (File.Exists(CurrentlyPlaying))\r
242                 {\r
243                     // Attempt to find VLC if it doesn't exist in the default set location.\r
244                     string vlcPath;\r
245 \r
246                     if (8 == IntPtr.Size ||\r
247                         (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
248                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
249                     else\r
250                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
251 \r
252                     vlcPath = vlcPath != null\r
253                                   ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
254                                   : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
255 \r
256                     if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
257                     {\r
258                         if (File.Exists(vlcPath))\r
259                         {\r
260                             Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
261                             Properties.Settings.Default.Save(); // Save this new path if it does\r
262                         }\r
263                         else\r
264                         {\r
265                             MessageBox.Show(this, \r
266                                             "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
267                                             "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
268                         }\r
269                     }\r
270 \r
271                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
272                     {\r
273                         string args = "\"" + CurrentlyPlaying + "\"";\r
274                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
275                         System.Diagnostics.Process.Start(vlc);\r
276                     }\r
277                 }\r
278                 else\r
279                     MessageBox.Show(this, \r
280                                     "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", \r
281                                     "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
282             }\r
283         }\r
284 \r
285         /// <summary>\r
286         /// QT control - Open the file\r
287         /// </summary>\r
288         [STAThread]\r
289         private void OpenMovie()\r
290         {\r
291             try\r
292             {\r
293                 if (InvokeRequired)\r
294                 {\r
295                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
296                     return;\r
297                 }\r
298                 QTControl.URL = CurrentlyPlaying;\r
299                 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
300                 QTControl.URL = CurrentlyPlaying;\r
301                 QTControl.Show();\r
302 \r
303                 this.ClientSize = QTControl.Size;\r
304                 this.Height += 25;\r
305             }\r
306             catch (COMException ex)\r
307             {\r
308                 QTUtils qtu = new QTUtils();\r
309                 MessageBox.Show(this, \r
310                                 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +\r
311                                 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", \r
312                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
313             }\r
314             catch (Exception ex)\r
315             {\r
316                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, \r
317                                 MessageBoxIcon.Warning);\r
318             }\r
319         }\r
320 \r
321         #endregion\r
322 \r
323         protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
324         {\r
325             Process.EncodeStarted -= Process_EncodeStarted;\r
326             base.OnClosing(e);\r
327         }\r
328     }\r
329 }