From 6dde7dab7f8585d98cdbeba4b8408f0d94c15663 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sun, 9 Dec 2018 21:15:40 +0000 Subject: [PATCH] WinGui: Some further tidyup of HandBrake.Interop. Remove the instance manager. The consumers should manage this themselves. --- .../HandBrake.Interop.csproj | 1 - .../Interop/HandBrakeEncoderHelpers.cs | 32 +--- .../Interop/HandBrakeInstance.cs | 154 ++++-------------- .../Interop/HandBrakeInstanceManager.cs | 116 ------------- .../Instance/HandBrakeInstanceManager.cs | 55 +++++++ .../HandBrakeWPF/Services/Encode/LibEncode.cs | 2 +- win/CS/HandBrakeWPF/Services/Scan/LibScan.cs | 1 + 7 files changed, 92 insertions(+), 269 deletions(-) delete mode 100644 win/CS/HandBrake.Interop/Interop/HandBrakeInstanceManager.cs diff --git a/win/CS/HandBrake.Interop/HandBrake.Interop.csproj b/win/CS/HandBrake.Interop/HandBrake.Interop.csproj index 3571728ea..d7cc5090a 100644 --- a/win/CS/HandBrake.Interop/HandBrake.Interop.csproj +++ b/win/CS/HandBrake.Interop/HandBrake.Interop.csproj @@ -58,7 +58,6 @@ - diff --git a/win/CS/HandBrake.Interop/Interop/HandBrakeEncoderHelpers.cs b/win/CS/HandBrake.Interop/Interop/HandBrakeEncoderHelpers.cs index f684d7367..1bdb849d5 100644 --- a/win/CS/HandBrake.Interop/Interop/HandBrakeEncoderHelpers.cs +++ b/win/CS/HandBrake.Interop/Interop/HandBrakeEncoderHelpers.cs @@ -9,6 +9,7 @@ namespace HandBrake.Interop.Interop { + using System; using System.Collections.Generic; using System.Linq; @@ -22,39 +23,12 @@ namespace HandBrake.Interop.Interop /// public static class HandBrakeEncoderHelpers { - /// - /// The audio encoders. - /// private static List audioEncoders; - - /// - /// The video encoders. - /// private static List videoEncoders; - - /// - /// Video framerates in pts. - /// private static List videoFramerates; - - /// - /// List of HandBrake mixdowns. - /// private static List mixdowns; - - /// - /// List of HandBrake containers. - /// private static List containers; - - /// - /// The audio bitrates. - /// private static List audioBitrates; - - /// - /// Audio sample rates in Hz. - /// private static List audioSampleRates; /// @@ -432,9 +406,9 @@ namespace HandBrake.Interop.Interop /// /// True if DRC can be applied to the track with the given encoder. /// - public static bool CanApplyDrc(int trackNumber, HBAudioEncoder encoder, int title) + public static bool CanApplyDrc(IntPtr handle, int trackNumber, HBAudioEncoder encoder, int title) { - return HBFunctions.hb_audio_can_apply_drc2(HandBrakeInstanceManager.LastScanHandle, title, trackNumber, encoder.Id) > 0; + return HBFunctions.hb_audio_can_apply_drc2(handle, title, trackNumber, encoder.Id) > 0; } /// diff --git a/win/CS/HandBrake.Interop/Interop/HandBrakeInstance.cs b/win/CS/HandBrake.Interop/Interop/HandBrakeInstance.cs index c6aa42fe3..6e7103bd6 100644 --- a/win/CS/HandBrake.Interop/Interop/HandBrakeInstance.cs +++ b/win/CS/HandBrake.Interop/Interop/HandBrakeInstance.cs @@ -30,59 +30,12 @@ namespace HandBrake.Interop.Interop using Newtonsoft.Json; - /// - /// A wrapper for a HandBrake instance. - /// public class HandBrakeInstance : IHandBrakeInstance, IDisposable { - /// - /// The number of MS between status polls when scanning. - /// private const double ScanPollIntervalMs = 250; - - /// - /// The number of MS between status polls when encoding. - /// private const double EncodePollIntervalMs = 250; - - /// - /// The native handle to the HandBrake instance. - /// - private IntPtr hbHandle; - - /// - /// The number of previews created during scan. - /// - private int previewCount; - - /// - /// The timer to poll for scan status. - /// private Timer scanPollTimer; - - /// - /// The timer to poll for encode status. - /// private Timer encodePollTimer; - - /// - /// The list of titles on this instance. - /// - private JsonScanObject titles; - - /// - /// The raw JSON for the titles list. - /// - private string titlesJson; - - /// - /// The index of the default title. - /// - private int featureTitle; - - /// - /// A value indicating whether this object has been disposed or not. - /// private bool disposed; /// @@ -90,7 +43,7 @@ namespace HandBrake.Interop.Interop /// ~HandBrakeInstance() { - if (this.hbHandle != IntPtr.Zero) + if (this.Handle != IntPtr.Zero) { this.Dispose(false); } @@ -119,80 +72,37 @@ namespace HandBrake.Interop.Interop /// /// Gets the handle. /// - internal IntPtr Handle - { - get - { - return this.hbHandle; - } - } + internal IntPtr Handle { get; private set; } /// /// Gets the number of previews created during scan. /// - public int PreviewCount - { - get - { - return this.previewCount; - } - } + public int PreviewCount { get; private set; } /// /// Gets the list of titles on this instance. /// - public JsonScanObject Titles - { - get - { - return this.titles; - } - } + public JsonScanObject Titles { get; private set; } /// /// Gets the raw JSON for the list of titles on this instance. /// - public string TitlesJson - { - get - { - return this.titlesJson; - } - } + public string TitlesJson { get; private set; } /// /// Gets the index of the default title. /// - public int FeatureTitle - { - get - { - return this.featureTitle; - } - } + public int FeatureTitle { get; private set; } /// /// Gets the HandBrake version string. /// - public string Version - { - get - { - var versionPtr = HBFunctions.hb_get_version(this.hbHandle); - return Marshal.PtrToStringAnsi(versionPtr); - } - } + public string Version => Marshal.PtrToStringAnsi(HBFunctions.hb_get_version(this.Handle)); /// /// Gets the HandBrake build number. /// - public int Build - { - get - { - return HBFunctions.hb_get_build(this.hbHandle); - } - } + public int Build => HBFunctions.hb_get_build(this.Handle); /// /// Initializes this instance. @@ -205,7 +115,7 @@ namespace HandBrake.Interop.Interop HandBrakeUtils.EnsureGlobalInit(); HandBrakeUtils.RegisterLogger(); - this.hbHandle = HBFunctions.hb_init(verbosity, update_check: 0); + this.Handle = HBFunctions.hb_init(verbosity, update_check: 0); } /// @@ -225,10 +135,10 @@ namespace HandBrake.Interop.Interop /// public void StartScan(string path, int previewCount, TimeSpan minDuration, int titleIndex) { - this.previewCount = previewCount; + this.PreviewCount = previewCount; IntPtr pathPtr = InteropUtilities.ToUtf8PtrFromString(path); - HBFunctions.hb_scan(this.hbHandle, pathPtr, titleIndex, previewCount, 1, (ulong)(minDuration.TotalSeconds * 90000)); + HBFunctions.hb_scan(this.Handle, pathPtr, titleIndex, previewCount, 1, (ulong)(minDuration.TotalSeconds * 90000)); Marshal.FreeHGlobal(pathPtr); this.scanPollTimer = new Timer(); @@ -256,7 +166,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public void StopScan() { - HBFunctions.hb_scan_stop(this.hbHandle); + HBFunctions.hb_scan_stop(this.Handle); } /// @@ -303,7 +213,7 @@ namespace HandBrake.Interop.Interop }; // Fetch the image data from LibHb - IntPtr resultingImageStuct = HBFunctions.hb_get_preview2(this.hbHandle, settings.TitleNumber, previewNumber, ref uiGeometry, deinterlace ? 1 : 0); + IntPtr resultingImageStuct = HBFunctions.hb_get_preview2(this.Handle, settings.TitleNumber, previewNumber, ref uiGeometry, deinterlace ? 1 : 0); hb_image_s image = InteropUtilities.ToStructureFromPtr(resultingImageStuct); // Copy the filled image buffer to a managed array. @@ -334,7 +244,7 @@ namespace HandBrake.Interop.Interop /// True if DRC can be applied to the track with the given encoder. public bool CanApplyDrc(int trackNumber, HBAudioEncoder encoder, int title) { - return HBFunctions.hb_audio_can_apply_drc2(this.hbHandle, title, trackNumber, encoder.Id) > 0; + return HBFunctions.hb_audio_can_apply_drc2(this.Handle, title, trackNumber, encoder.Id) > 0; } /// @@ -362,8 +272,8 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public void StartEncode(string encodeJson) { - HBFunctions.hb_add_json(this.hbHandle, InteropUtilities.ToUtf8PtrFromString(encodeJson)); - HBFunctions.hb_start(this.hbHandle); + HBFunctions.hb_add_json(this.Handle, InteropUtilities.ToUtf8PtrFromString(encodeJson)); + HBFunctions.hb_start(this.Handle); this.encodePollTimer = new Timer(); this.encodePollTimer.Interval = EncodePollIntervalMs; @@ -388,7 +298,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public void PauseEncode() { - HBFunctions.hb_pause(this.hbHandle); + HBFunctions.hb_pause(this.Handle); } /// @@ -397,7 +307,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public void ResumeEncode() { - HBFunctions.hb_resume(this.hbHandle); + HBFunctions.hb_resume(this.Handle); } /// @@ -406,20 +316,20 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public void StopEncode() { - HBFunctions.hb_stop(this.hbHandle); + HBFunctions.hb_stop(this.Handle); // Also remove all jobs from the queue (in case we stopped a 2-pass encode) var currentJobs = new List(); - int jobs = HBFunctions.hb_count(this.hbHandle); + int jobs = HBFunctions.hb_count(this.Handle); for (int i = 0; i < jobs; i++) { - currentJobs.Add(HBFunctions.hb_job(this.hbHandle, 0)); + currentJobs.Add(HBFunctions.hb_job(this.Handle, 0)); } foreach (IntPtr job in currentJobs) { - HBFunctions.hb_rem(this.hbHandle, job); + HBFunctions.hb_rem(this.Handle, job); } } @@ -432,7 +342,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] public JsonState GetEncodeProgress() { - IntPtr json = HBFunctions.hb_get_state_json(this.hbHandle); + IntPtr json = HBFunctions.hb_get_state_json(this.Handle); string statusJson = Marshal.PtrToStringAnsi(json); JsonState state = JsonConvert.DeserializeObject(statusJson); @@ -479,7 +389,7 @@ namespace HandBrake.Interop.Interop // Free unmanaged objects. IntPtr handlePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); - Marshal.WriteIntPtr(handlePtr, this.hbHandle); + Marshal.WriteIntPtr(handlePtr, this.Handle); HBFunctions.hb_close(handlePtr); Marshal.FreeHGlobal(handlePtr); @@ -492,7 +402,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] private void PollScanProgress() { - IntPtr json = HBFunctions.hb_get_state_json(this.hbHandle); + IntPtr json = HBFunctions.hb_get_state_json(this.Handle); string statusJson = Marshal.PtrToStringAnsi(json); JsonState state = null; if (!string.IsNullOrEmpty(statusJson)) @@ -513,15 +423,15 @@ namespace HandBrake.Interop.Interop { this.scanPollTimer.Stop(); - var jsonMsg = HBFunctions.hb_get_title_set_json(this.hbHandle); - this.titlesJson = InteropUtilities.ToStringFromUtf8Ptr(jsonMsg); + var jsonMsg = HBFunctions.hb_get_title_set_json(this.Handle); + this.TitlesJson = InteropUtilities.ToStringFromUtf8Ptr(jsonMsg); - if (!string.IsNullOrEmpty(this.titlesJson)) + if (!string.IsNullOrEmpty(this.TitlesJson)) { - this.titles = JsonConvert.DeserializeObject(this.titlesJson); - if (this.titles != null) + this.Titles = JsonConvert.DeserializeObject(this.TitlesJson); + if (this.Titles != null) { - this.featureTitle = this.titles.MainFeature; + this.FeatureTitle = this.Titles.MainFeature; } } @@ -538,7 +448,7 @@ namespace HandBrake.Interop.Interop [HandleProcessCorruptedStateExceptions] private void PollEncodeProgress() { - IntPtr json = HBFunctions.hb_get_state_json(this.hbHandle); + IntPtr json = HBFunctions.hb_get_state_json(this.Handle); string statusJson = Marshal.PtrToStringAnsi(json); JsonState state = JsonConvert.DeserializeObject(statusJson); diff --git a/win/CS/HandBrake.Interop/Interop/HandBrakeInstanceManager.cs b/win/CS/HandBrake.Interop/Interop/HandBrakeInstanceManager.cs deleted file mode 100644 index 76855c5d1..000000000 --- a/win/CS/HandBrake.Interop/Interop/HandBrakeInstanceManager.cs +++ /dev/null @@ -1,116 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// -// -// The hand brake instance manager. -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrake.Interop.Interop -{ - using System; - - using HandBrake.Interop.Interop.Interfaces; - using HandBrake.Interop.Model; - - /// - /// The HandBrake Instance manager. - /// Only supports scanning right now. - /// - public static class HandBrakeInstanceManager - { - private static HandBrakeInstance scanInstance; - private static HandBrakeInstance previewInstance; - - /// - /// Initializes static members of the class. - /// - static HandBrakeInstanceManager() - { - } - - /// - /// The init. - /// - public static void Init() - { - // Nothing to do. Triggers static constructor. - } - - /// - /// Gets the scanInstance. - /// - /// - /// The verbosity. - /// - /// - /// The . - /// - public static IHandBrakeInstance GetScanInstance(int verbosity) - { - if (scanInstance != null) - { - scanInstance.Dispose(); - scanInstance = null; - } - - HandBrakeInstance newInstance = new HandBrakeInstance(); - newInstance.Initialize(verbosity); - scanInstance = newInstance; - - return scanInstance; - } - - /// - /// The get encode instance. - /// - /// - /// The verbosity. - /// - /// - /// The configuration. - /// - /// - /// The . - /// - public static IHandBrakeInstance GetPreviewInstance(int verbosity, HBConfiguration configuration) - { - if (previewInstance != null) - { - previewInstance.Dispose(); - previewInstance = null; - } - - HandBrakeInstance newInstance = new HandBrakeInstance(); - newInstance.Initialize(verbosity); - previewInstance = newInstance; - - HandBrakeUtils.SetDvdNav(!configuration.IsDvdNavDisabled); - - return previewInstance; - } - - /// - /// Gets the last scan scan instance. - /// - internal static IHandBrakeInstance LastScanScanInstance - { - get - { - return scanInstance; - } - } - - /// - /// Gets the handle. - /// - internal static IntPtr LastScanHandle - { - get - { - return scanInstance.Handle; - } - } - } -} diff --git a/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs b/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs index 3fcf00e8f..d841dfab5 100644 --- a/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs +++ b/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs @@ -20,6 +20,8 @@ namespace HandBrakeWPF.Instance public static class HandBrakeInstanceManager { private static IEncodeInstance encodeInstance; + private static HandBrakeInstance scanInstance; + private static HandBrakeInstance previewInstance; /// /// Initializes static members of the class. @@ -74,5 +76,58 @@ namespace HandBrakeWPF.Instance return encodeInstance; } + + /// + /// Gets the scanInstance. + /// + /// + /// The verbosity. + /// + /// + /// The . + /// + public static IHandBrakeInstance GetScanInstance(int verbosity) + { + if (scanInstance != null) + { + scanInstance.Dispose(); + scanInstance = null; + } + + HandBrakeInstance newInstance = new HandBrakeInstance(); + newInstance.Initialize(verbosity); + scanInstance = newInstance; + + return scanInstance; + } + + /// + /// The get encode instance. + /// + /// + /// The verbosity. + /// + /// + /// The configuration. + /// + /// + /// The . + /// + public static IHandBrakeInstance GetPreviewInstance(int verbosity, HBConfiguration configuration) + { + if (previewInstance != null) + { + previewInstance.Dispose(); + previewInstance = null; + } + + HandBrakeInstance newInstance = new HandBrakeInstance(); + newInstance.Initialize(verbosity); + previewInstance = newInstance; + + HandBrakeUtils.SetDvdNav(!configuration.IsDvdNavDisabled); + + return previewInstance; + } } } diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs index 5c37dc4dd..382dd7cf9 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs @@ -80,7 +80,7 @@ namespace HandBrakeWPF.Services.Encode this.log.Reset(); // Reset so we have a clean log for the start of the encode. this.ServiceLogMessage("Starting Encode ..."); - this.instance = task.IsPreviewEncode ? HandBrake.Interop.Interop.HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity, configuration) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity, configuration); + this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity, configuration) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity, configuration); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; diff --git a/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs b/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs index e3148866a..40823f3f7 100644 --- a/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs +++ b/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs @@ -23,6 +23,7 @@ namespace HandBrakeWPF.Services.Scan using HandBrake.Interop.Interop.Model.Preview; using HandBrake.Interop.Model; + using HandBrakeWPF.Instance; using HandBrakeWPF.Services.Encode.Model; using HandBrakeWPF.Services.Encode.Model.Models; using HandBrakeWPF.Services.Scan.EventArgs; -- 2.40.0