From 19f3dc6e8f6f8ee25ef99526276d400753aaa0a7 Mon Sep 17 00:00:00 2001 From: Scott Date: Sat, 31 Oct 2015 17:35:56 +0000 Subject: Allow the preview encode to run at the same time as an actual encode. --- .../Interop/HandBrakeInstance.cs | 2 +- .../Interop/HandBrakeInstanceManager.cs | 25 ++++++++++++++++++++++ win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs | 18 ++++++---------- .../Services/Encode/Interfaces/IEncode.cs | 11 ---------- win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs | 22 +++++++++++-------- 5 files changed, 46 insertions(+), 32 deletions(-) (limited to 'win/CS') diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs index a862dc1e6..ca1a8367b 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs @@ -43,7 +43,7 @@ namespace HandBrake.ApplicationServices.Interop /// /// A wrapper for a HandBrake instance. /// - public class HandBrakeInstance : IHandBrakeInstance, IDisposable + public class HandBrakeInstance : IHandBrakeInstance, IDisposable { /// /// The number of MS between status polls when scanning. diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstanceManager.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstanceManager.cs index a74513f4a..5ceb70bdc 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstanceManager.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstanceManager.cs @@ -21,6 +21,7 @@ namespace HandBrake.ApplicationServices.Interop { private static HandBrakeInstance scanInstance; private static HandBrakeInstance encodeInstance; + private static HandBrakeInstance previewInstance; private static HandBrakeInstance masterInstance; /// @@ -88,6 +89,30 @@ namespace HandBrake.ApplicationServices.Interop return encodeInstance; } + /// + /// The get encode instance. + /// + /// + /// The verbosity. + /// + /// + /// The . + /// + public static IHandBrakeInstance GetPreviewInstance(int verbosity) + { + if (previewInstance != null) + { + previewInstance.Dispose(); + previewInstance = null; + } + + HandBrakeInstance newInstance = new HandBrakeInstance(); + newInstance.Initialize(verbosity); + previewInstance = newInstance; + + return previewInstance; + } + /// /// Gets the master instance. /// diff --git a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs index 4179ed3c7..9ae0398ee 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs @@ -11,6 +11,7 @@ namespace HandBrakeWPF.Services.Encode { using System; using System.Diagnostics; + using System.Globalization; using System.IO; using System.Text; @@ -188,18 +189,19 @@ namespace HandBrakeWPF.Services.Encode /// /// The configuration. /// - public void ProcessLogs(string destination, HBConfiguration configuration) + public void ProcessLogs(string destination, bool isPreview, HBConfiguration configuration) { try { string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId)); + + string tempLogFile = Path.Combine(logDir, isPreview ? $"preview_encode_log{GeneralUtilities.ProcessId}.txt" : string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId)); string encodeDestinationPath = Path.GetDirectoryName(destination); string destinationFile = Path.GetFileName(destination); string encodeLogFile = destinationFile + " " + - DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt"; + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt"; // Make sure the log directory exists. if (!Directory.Exists(logDir)) @@ -232,12 +234,11 @@ namespace HandBrakeWPF.Services.Encode /// /// Setup the logging. /// - protected void SetupLogging() + protected void SetupLogging(bool isPreviewEncode) { this.ShutdownFileWriter(); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId)); - string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.ProcessId)); + string logFile = Path.Combine(logDir, isPreviewEncode ? $"preview_last_encode_log{GeneralUtilities.ProcessId}.txt" : $"last_encode_log{GeneralUtilities.ProcessId}.txt"); try { @@ -251,11 +252,6 @@ namespace HandBrakeWPF.Services.Encode File.Delete(logFile); } - if (File.Exists(logFile2)) - { - File.Delete(logFile2); - } - lock (FileWriterLock) { this.fileWriter = new StreamWriter(logFile) { AutoFlush = true }; diff --git a/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs index 1e81ed5ed..691df1500 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs @@ -104,16 +104,5 @@ namespace HandBrakeWPF.Services.Encode.Interfaces /// Kill the process /// void Stop(); - - /// - /// Copy the log file to the desired destinations - /// - /// - /// The destination. - /// - /// - /// The configuration. - /// - void ProcessLogs(string destination, HBConfiguration configuration); } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs index caf1eff1d..111c91c6e 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs @@ -17,6 +17,7 @@ namespace HandBrakeWPF.Services.Encode using HandBrake.ApplicationServices.Interop.Interfaces; using HandBrake.ApplicationServices.Model; + using HandBrakeWPF.Exceptions; using HandBrakeWPF.Services.Encode.Factories; using EncodeTask = HandBrakeWPF.Services.Encode.Model.EncodeTask; @@ -34,6 +35,7 @@ namespace HandBrakeWPF.Services.Encode private DateTime startTime; private EncodeTask currentTask; private HBConfiguration currentConfiguration; + private bool isPreviewInstance; #endregion @@ -55,6 +57,12 @@ namespace HandBrakeWPF.Services.Encode { try { + // Sanity Checking and Setup + if (this.IsEncoding) + { + throw new GeneralApplicationException("HandBrake is already encoding a file.", "Please stop the current encode. If the problem persists, please restart HandBrake.", null); + } + // Setup this.startTime = DateTime.Now; this.currentTask = task; @@ -64,18 +72,14 @@ namespace HandBrakeWPF.Services.Encode // Setup the HandBrake Instance HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; - this.instance = HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); + this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); + this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; - // Sanity Checking and Setup - if (this.IsEncoding) - { - throw new Exception("HandBrake is already encoding."); - } - this.IsEncoding = true; - this.SetupLogging(); + this.isPreviewInstance = task.IsPreviewEncode; + this.SetupLogging(task.IsPreviewEncode); // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); @@ -246,7 +250,7 @@ namespace HandBrakeWPF.Services.Encode HandBrakeUtils.ErrorLogged -= this.HandBrakeInstanceErrorLogged; // Handling Log Data - this.ProcessLogs(this.currentTask.Destination, this.currentConfiguration); + this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration); // Cleanup this.ShutdownFileWriter(); -- cgit v1.2.3