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