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         private void EncodeCompleted()\r
182         {\r
183             try\r
184             {\r
185                 if (InvokeRequired)\r
186                 {\r
187                     BeginInvoke(new UpdateUIHandler(EncodeCompleted));\r
188                     return;\r
189                 }\r
190 \r
191                 ProgressBarStatus.Visible = false;\r
192                 lbl_encodeStatus.Visible = false;\r
193 \r
194                 if (!NoQT)\r
195                     btn_playQT.Enabled = true;\r
196                 btn_playVLC.Enabled = true;\r
197 \r
198                 this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
199 \r
200                 // Get the sample filename\r
201                 if (MainWindow.text_destination.Text != string.Empty)\r
202                     CurrentlyPlaying =\r
203                         MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
204                             Replace(".mkv", "_sample.mkv");\r
205 \r
206                 // Play back in QT or VLC\r
207                 if (!playWithVLC)\r
208                     Play();\r
209                 else\r
210                     PlayVLC();\r
211             }\r
212             catch (Exception exc)\r
213             {\r
214                 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, \r
215                                 MessageBoxIcon.Error);\r
216             }\r
217         }\r
218 \r
219         #endregion\r
220 \r
221         #region Playback\r
222 \r
223         /// <summary>\r
224         /// Play the video back in the QuickTime control\r
225         /// </summary>\r
226         private void Play()\r
227         {\r
228             Player = new Thread(OpenMovie) {IsBackground = true};\r
229             Player.Start();\r
230         }\r
231 \r
232         /// <summary>\r
233         /// Play the video back in an external VLC Player\r
234         /// </summary>\r
235         private void PlayVLC()\r
236         {\r
237             // Launch VLC and Play video.\r
238             if (CurrentlyPlaying != string.Empty)\r
239             {\r
240                 if (File.Exists(CurrentlyPlaying))\r
241                 {\r
242                     // Attempt to find VLC if it doesn't exist in the default set location.\r
243                     string vlcPath;\r
244 \r
245                     if (8 == IntPtr.Size ||\r
246                         (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
247                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
248                     else\r
249                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
250 \r
251                     vlcPath = vlcPath != null\r
252                                   ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
253                                   : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
254 \r
255                     if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
256                     {\r
257                         if (File.Exists(vlcPath))\r
258                         {\r
259                             Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
260                             Properties.Settings.Default.Save(); // Save this new path if it does\r
261                         }\r
262                         else\r
263                         {\r
264                             MessageBox.Show(this, \r
265                                             "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
266                                             "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
267                         }\r
268                     }\r
269 \r
270                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
271                     {\r
272                         string args = "\"" + CurrentlyPlaying + "\"";\r
273                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
274                         System.Diagnostics.Process.Start(vlc);\r
275                     }\r
276                 }\r
277                 else\r
278                     MessageBox.Show(this, \r
279                                     "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", \r
280                                     "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
281             }\r
282         }\r
283 \r
284         /// <summary>\r
285         /// QT control - Open the file\r
286         /// </summary>\r
287         [STAThread]\r
288         private void OpenMovie()\r
289         {\r
290             try\r
291             {\r
292                 if (InvokeRequired)\r
293                 {\r
294                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
295                     return;\r
296                 }\r
297                 QTControl.URL = CurrentlyPlaying;\r
298                 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
299                 QTControl.URL = CurrentlyPlaying;\r
300                 QTControl.Show();\r
301 \r
302                 this.ClientSize = QTControl.Size;\r
303                 this.Height += 25;\r
304             }\r
305             catch (COMException ex)\r
306             {\r
307                 QTUtils qtu = new QTUtils();\r
308                 MessageBox.Show(this, \r
309                                 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +\r
310                                 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", \r
311                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
312             }\r
313             catch (Exception ex)\r
314             {\r
315                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, \r
316                                 MessageBoxIcon.Warning);\r
317             }\r
318         }\r
319 \r
320         #endregion\r
321 \r
322         protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
323         {\r
324             Process.EncodeStarted -= Process_EncodeStarted;\r
325             base.OnClosing(e);\r
326         }\r
327     }\r
328 }