OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
index e00bcd9..14080bd 100644 (file)
-using System;\r
-using System.Windows.Forms;\r
-using System.Threading;\r
-using System.Diagnostics;\r
-using System.Runtime.InteropServices;\r
-using QTOLibrary;\r
+/*  frmPreview.cs $\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr/>.\r
+    It may be used under the terms of the GNU General Public License. */\r
 \r
 namespace Handbrake\r
 {\r
+    using System;\r
+    using System.Diagnostics;\r
+    using System.IO;\r
+    using System.Runtime.InteropServices;\r
+    using System.Threading;\r
+    using System.Windows.Forms;\r
+    using Functions;\r
+    using QTOControlLib;\r
+    using QTOLibrary;\r
+    using Services;\r
+    using Parsing;\r
+\r
     public partial class frmPreview : Form\r
     {\r
-\r
-        QueryGenerator hb_common_func = new QueryGenerator();\r
-        Functions.Encode process = new Functions.Encode();\r
+        private string CurrentlyPlaying = string.Empty;\r
+        private readonly frmMain MainWindow;\r
+        private Thread Player;\r
+        private readonly bool NoQT;\r
+        private readonly Queue Process = new Queue();\r
         private delegate void UpdateUIHandler();\r
-        String currently_playing = "";\r
-        frmMain mainWindow;\r
-        private Process hbProc;\r
-        private Thread player;\r
+        private bool playWithVLC;\r
 \r
         public frmPreview(frmMain mw)\r
         {\r
-            InitializeComponent();\r
-            this.mainWindow = mw;\r
+            try\r
+            {\r
+                InitializeComponent();\r
+            }\r
+            catch (Exception)\r
+            {\r
+                NoQT = true;\r
+            }\r
+            this.MainWindow = mw;\r
             cb_preview.SelectedIndex = 0;\r
             cb_duration.SelectedIndex = 1;\r
-        }\r
 \r
-        private void play()\r
+            cb_preview.Items.Clear();\r
+            for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)\r
+                cb_preview.Items.Add(i.ToString());\r
+            cb_preview.SelectedIndex = 0;\r
+\r
+            Process.EncodeStarted += new EventHandler(Process_EncodeStarted);\r
+        }\r
+        private void Process_EncodeStarted(object sender, EventArgs e)\r
         {\r
-            player = new Thread(OpenMovie);\r
-            player.IsBackground = true;\r
-            player.Start();\r
+            Thread encodeMon = new Thread(EncodeMonitorThread);\r
+            encodeMon.Start();\r
         }\r
 \r
-        [STAThread]\r
-        private void OpenMovie()\r
+        #region Encode Sample\r
+\r
+        private void btn_playVLC_Click(object sender, EventArgs e)\r
         {\r
+            ProgressBarStatus.Visible = true;\r
+            ProgressBarStatus.Value = 0;\r
+            lbl_encodeStatus.Visible = true;\r
+            playWithVLC = true;\r
+\r
             try\r
             {\r
-                if (this.InvokeRequired)\r
-                {\r
-                    this.BeginInvoke(new UpdateUIHandler(OpenMovie));\r
-                    return;\r
-                }\r
-                QTControl.URL = currently_playing;\r
-                QTControl.Width = QTControl.Movie.Width;\r
-                QTControl.Height = QTControl.Movie.Height;\r
-                // The initial control size is 64,64. If we do not reload the clip here\r
-                // it'll scale the video from 64,64. \r
-                // Unsure why as it correctly resizes the control to the movies actual size.\r
-                QTControl.URL = currently_playing; \r
-                QTControl.SetScale(0);\r
-                QTControl.Show();\r
+                if (!NoQT)\r
+                    QTControl.URL = string.Empty;\r
 \r
-                this.Width = QTControl.Width + 5;\r
-                this.Height = QTControl.Height + 90;\r
+                if (File.Exists(CurrentlyPlaying))\r
+                    File.Delete(CurrentlyPlaying);\r
             }\r
-            catch (COMException ex)\r
-            {\r
-                QTUtils qtu = new QTUtils();\r
-                MessageBox.Show("Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode).ToString());\r
-            }\r
-            catch (Exception ex)\r
+            catch (Exception)\r
             {\r
-                MessageBox.Show("Unable to open movie:\n\n" + ex.ToString());\r
+                MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", \r
+                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
+\r
+            btn_playQT.Enabled = false;\r
+            btn_playVLC.Enabled = false;\r
+            this.Text += " (Encoding)";\r
+            int duration;\r
+            int.TryParse(cb_duration.Text, out duration);\r
+            string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
+            ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
         }\r
\r
-        #region Encode Sample\r
-        private void btn_encode_Click(object sender, EventArgs e)\r
+\r
+        private void btn_playQT_Click(object sender, EventArgs e)\r
         {\r
-            btn_encode.Enabled = false;\r
-            lbl_encode.Text = "Encoding Sample ...";\r
-            String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
-            ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+            playWithVLC = false;\r
+            if (NoQT)\r
+            {\r
+                MessageBox.Show(this, \r
+                                "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", \r
+                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                return;\r
+            }\r
+            if (MainWindow.text_destination.Text.Contains(".mkv"))\r
+            {\r
+                MessageBox.Show(this, \r
+                                "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", \r
+                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            else\r
+            {\r
+                ProgressBarStatus.Visible = true;\r
+                ProgressBarStatus.Value = 0;\r
+                lbl_encodeStatus.Visible = true;\r
+                try\r
+                {\r
+                    QTControl.URL = string.Empty;\r
+                    if (File.Exists(CurrentlyPlaying))\r
+                        File.Delete(CurrentlyPlaying);\r
+                }\r
+                catch (Exception)\r
+                {\r
+                    MessageBox.Show(this, \r
+                                    "Unable to delete previous preview file. You may need to restart the application.", \r
+                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                }\r
+\r
+                btn_playQT.Enabled = false;\r
+                btn_playVLC.Enabled = false;\r
+                this.Text += " (Encoding)";\r
+                int duration;\r
+                int.TryParse(cb_duration.Text, out duration);\r
+                string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
+\r
+                ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
+            }\r
         }\r
-        private void procMonitor(object state)\r
+\r
+        private void ProcMonitor(object state)\r
         {\r
             // Make sure we are not already encoding and if we are then display an error.\r
-            if (hbProc != null)\r
-                MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            if (Process.HbProcess != null)\r
+                MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, \r
+                                MessageBoxIcon.Warning);\r
             else\r
             {\r
-                hbProc = process.runCli(this, (string)state);\r
-                hbProc.WaitForExit();\r
-                hbProc = null;\r
-                encodeCompleted();\r
+                Process.CreatePreviewSample((string) state);\r
+\r
+                if (Process.HbProcess != null)\r
+                {\r
+                    Process.HbProcess.WaitForExit();\r
+                    Process.HbProcess = null;\r
+                }\r
+                EncodeCompleted();\r
             }\r
         }\r
-        private void encodeCompleted()\r
+\r
+        private void EncodeMonitorThread()\r
         {\r
             try\r
             {\r
-                if (this.InvokeRequired)\r
+                Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);\r
+                encode.OnEncodeProgress += EncodeOnEncodeProgress;\r
+                while (!encode.EndOfStream)\r
+                    encode.ReadEncodeStatus();\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
+        }\r
+\r
+        private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)\r
+        {\r
+            if (this.InvokeRequired)\r
+            {\r
+                this.BeginInvoke(\r
+                    new EncodeProgressEventHandler(EncodeOnEncodeProgress),\r
+                    new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });\r
+                return;\r
+            }\r
+            lbl_encodeStatus.Text = PercentComplete + "%";\r
+            ProgressBarStatus.Value = (int)Math.Round(PercentComplete);\r
+        }\r
+\r
+        private void EncodeCompleted()\r
+        {\r
+            try\r
+            {\r
+                if (InvokeRequired)\r
                 {\r
-                    this.BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
+                    BeginInvoke(new UpdateUIHandler(EncodeCompleted));\r
                     return;\r
                 }\r
-                btn_encode.Enabled = true;\r
-                lbl_encode.Text = "Loading Clip ...";\r
 \r
-                if (mainWindow.text_destination.Text != "")\r
-                    currently_playing = mainWindow.text_destination.Text.Replace(".m", "_sample.m").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm");\r
+                ProgressBarStatus.Visible = false;\r
+                lbl_encodeStatus.Visible = false;\r
+\r
+                if (!NoQT)\r
+                    btn_playQT.Enabled = true;\r
+                btn_playVLC.Enabled = true;\r
+\r
+                this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
 \r
-                play();\r
-                lbl_encode.Text = "";\r
+                // Get the sample filename\r
+                if (MainWindow.text_destination.Text != string.Empty)\r
+                    CurrentlyPlaying =\r
+                        MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
+                            Replace(".mkv", "_sample.mkv");\r
+\r
+                // Play back in QT or VLC\r
+                if (!playWithVLC)\r
+                    Play();\r
+                else\r
+                    PlayVLC();\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("frmPreview.cs encodeCompleted " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, \r
+                                MessageBoxIcon.Error);\r
             }\r
         }\r
+\r
         #endregion\r
 \r
+        #region Playback\r
+\r
+        /// <summary>\r
+        /// Play the video back in the QuickTime control\r
+        /// </summary>\r
+        private void Play()\r
+        {\r
+            Player = new Thread(OpenMovie) {IsBackground = true};\r
+            Player.Start();\r
+        }\r
+\r
+        /// <summary>\r
+        /// Play the video back in an external VLC Player\r
+        /// </summary>\r
+        private void PlayVLC()\r
+        {\r
+            // Launch VLC and Play video.\r
+            if (CurrentlyPlaying != string.Empty)\r
+            {\r
+                if (File.Exists(CurrentlyPlaying))\r
+                {\r
+                    // Attempt to find VLC if it doesn't exist in the default set location.\r
+                    string vlcPath;\r
+\r
+                    if (8 == IntPtr.Size ||\r
+                        (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
+                        vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
+                    else\r
+                        vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
+\r
+                    vlcPath = vlcPath != null\r
+                                  ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
+                                  : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
+\r
+                    if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
+                    {\r
+                        if (File.Exists(vlcPath))\r
+                        {\r
+                            Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
+                            Properties.Settings.Default.Save(); // Save this new path if it does\r
+                        }\r
+                        else\r
+                        {\r
+                            MessageBox.Show(this, \r
+                                            "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
+                                            "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                        }\r
+                    }\r
+\r
+                    if (File.Exists(Properties.Settings.Default.VLC_Path))\r
+                    {\r
+                        string args = "\"" + CurrentlyPlaying + "\"";\r
+                        ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
+                        System.Diagnostics.Process.Start(vlc);\r
+                    }\r
+                }\r
+                else\r
+                    MessageBox.Show(this, \r
+                                    "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", \r
+                                    "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
+        /// QT control - Open the file\r
+        /// </summary>\r
+        [STAThread]\r
+        private void OpenMovie()\r
+        {\r
+            try\r
+            {\r
+                if (InvokeRequired)\r
+                {\r
+                    BeginInvoke(new UpdateUIHandler(OpenMovie));\r
+                    return;\r
+                }\r
+                QTControl.URL = CurrentlyPlaying;\r
+                QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
+                QTControl.URL = CurrentlyPlaying;\r
+                QTControl.Show();\r
+\r
+                this.ClientSize = QTControl.Size;\r
+                this.Height += 25;\r
+            }\r
+            catch (COMException ex)\r
+            {\r
+                QTUtils qtu = new QTUtils();\r
+                MessageBox.Show(this, \r
+                                "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +\r
+                                "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", \r
+                                MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, \r
+                                MessageBoxIcon.Warning);\r
+            }\r
+        }\r
+\r
+        #endregion\r
+\r
+        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
+        {\r
+            Process.EncodeStarted -= Process_EncodeStarted;\r
+            base.OnClosing(e);\r
+        }\r
     }\r
 }
\ No newline at end of file