OSDN Git Service

e2022ca2b4e7f7bb2eac675a41f71256997a5d63
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
1 using System;\r
2 using System.Windows.Forms;\r
3 using System.Threading;\r
4 using System.Diagnostics;\r
5 using System.Runtime.InteropServices;\r
6 using System.IO;\r
7 using QTOLibrary;\r
8 \r
9 namespace Handbrake\r
10 {\r
11     public partial class frmPreview : Form\r
12     {\r
13 \r
14         QueryGenerator hb_common_func = new QueryGenerator();\r
15         Functions.Encode process = new Functions.Encode();\r
16         private delegate void UpdateUIHandler();\r
17         String currently_playing = "";\r
18         readonly frmMain mainWindow;\r
19         private Process hbProc;\r
20         private Thread player;\r
21         private Boolean noQT;\r
22 \r
23         public frmPreview(frmMain mw)\r
24         {\r
25             try\r
26             {\r
27                 InitializeComponent();\r
28             }\r
29             catch (Exception exc)\r
30             {\r
31                 MessageBox.Show(mw, "It would appear QuickTime 7 is not installed. QuickTime preview functionality will be disabled! \n\n Debug Info:\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
32                 btn_playQT.Enabled = false;\r
33                 noQT = true;\r
34             }\r
35             this.mainWindow = mw;\r
36             cb_preview.SelectedIndex = 0;\r
37             cb_duration.SelectedIndex = 1;\r
38         }\r
39  \r
40         #region Encode Sample\r
41         private void btn_playVLC_Click(object sender, EventArgs e)\r
42         {\r
43             btn_playQT.Enabled = false;\r
44             btn_playVLC.Enabled = false;\r
45             lbl_status.Text = "Encoding Sample for (VLC) ...";\r
46             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
47             ThreadPool.QueueUserWorkItem(procMonitor, query);  \r
48         }\r
49         private void btn_playQT_Click(object sender, EventArgs e)\r
50         {\r
51             btn_playQT.Enabled = false;\r
52             btn_playVLC.Enabled = false;\r
53             lbl_status.Text = "Encoding Sample for (QT) ...";\r
54             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
55             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
56         }\r
57         private void procMonitor(object state)\r
58         {\r
59             // Make sure we are not already encoding and if we are then display an error.\r
60             if (hbProc != null)\r
61                 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
62             else\r
63             {\r
64                 hbProc = process.runCli(this, (string)state);\r
65                 hbProc.WaitForExit();\r
66                 hbProc = null;\r
67                 encodeCompleted();\r
68             }\r
69         }\r
70         private void encodeCompleted()\r
71         {\r
72             try\r
73             {\r
74                 if (InvokeRequired)\r
75                 {\r
76                     BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
77                     return;\r
78                 }\r
79                 if (!noQT)\r
80                     btn_playQT.Enabled = true;\r
81                 btn_playVLC.Enabled = true;\r
82 \r
83                 // Decide which player to use.\r
84                 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
85 \r
86                 lbl_status.Text = "Loading Clip ...";\r
87 \r
88                 // Get the sample filename\r
89                 if (mainWindow.text_destination.Text != "")\r
90                     currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm");\r
91 \r
92                 // Play back in QT or VLC\r
93                 if (playerSelection == "QT")\r
94                     play();\r
95                 else \r
96                     playVLC();\r
97                 \r
98                 lbl_status.Text = "";\r
99             }\r
100             catch (Exception exc)\r
101             {\r
102                 MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
103             }\r
104         }\r
105         #endregion\r
106 \r
107         #region Playback\r
108 \r
109         /// <summary>\r
110         /// Play the video back in the QuickTime control\r
111         /// </summary>\r
112         private void play()\r
113         {\r
114             player = new Thread(OpenMovie) { IsBackground = true };\r
115             player.Start();\r
116         }\r
117 \r
118         /// <summary>\r
119         /// Play the video back in an external VLC player\r
120         /// </summary>\r
121         private void playVLC()\r
122         {\r
123             // Launch VLC and play video.\r
124             if (currently_playing != "")\r
125             {\r
126                 if (File.Exists(currently_playing))\r
127                 {\r
128                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
129                     {\r
130                         String args = "\"" + currently_playing + "\"";\r
131                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
132                         Process.Start(vlc);\r
133                         lbl_status.Text = "VLC will now launch.";\r
134                     }\r
135                     else\r
136                         MessageBox.Show(this, "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in the program options is correct.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
137                 }\r
138                 else\r
139                     MessageBox.Show(this, "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
140             }\r
141         }\r
142 \r
143         /// <summary>\r
144         /// QT control - Open the file\r
145         /// </summary>\r
146         [STAThread]\r
147         private void OpenMovie()\r
148         {\r
149             try\r
150             {\r
151                 if (InvokeRequired)\r
152                 {\r
153                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
154                     return;\r
155                 }\r
156                 QTControl.URL = currently_playing;\r
157                 QTControl.Width = QTControl.Movie.Width;\r
158                 QTControl.Height = QTControl.Movie.Height;\r
159                 // The initial control size is 64,64. If we do not reload the clip here\r
160                 // it'll scale the video from 64,64. \r
161                 // Unsure why as it correctly resizes the control to the movies actual size.\r
162                 QTControl.URL = currently_playing;\r
163                 QTControl.SetScale(0);\r
164                 QTControl.Show();\r
165 \r
166                 this.Width = QTControl.Width + 5;\r
167                 this.Height = QTControl.Height + 90;\r
168             }\r
169             catch (COMException ex)\r
170             {\r
171                 QTUtils qtu = new QTUtils();\r
172                 MessageBox.Show(this, "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
173             }\r
174             catch (Exception ex)\r
175             {\r
176                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
177             }\r
178         }\r
179         #endregion\r
180     }\r
181 }\r