using Newtonsoft.Json;
- /// <summary>
- /// A wrapper for a HandBrake instance.
- /// </summary>
public class HandBrakeInstance : IHandBrakeInstance, IDisposable
{
- /// <summary>
- /// The number of MS between status polls when scanning.
- /// </summary>
private const double ScanPollIntervalMs = 250;
-
- /// <summary>
- /// The number of MS between status polls when encoding.
- /// </summary>
private const double EncodePollIntervalMs = 250;
-
- /// <summary>
- /// The native handle to the HandBrake instance.
- /// </summary>
- private IntPtr hbHandle;
-
- /// <summary>
- /// The number of previews created during scan.
- /// </summary>
- private int previewCount;
-
- /// <summary>
- /// The timer to poll for scan status.
- /// </summary>
private Timer scanPollTimer;
-
- /// <summary>
- /// The timer to poll for encode status.
- /// </summary>
private Timer encodePollTimer;
-
- /// <summary>
- /// The list of titles on this instance.
- /// </summary>
- private JsonScanObject titles;
-
- /// <summary>
- /// The raw JSON for the titles list.
- /// </summary>
- private string titlesJson;
-
- /// <summary>
- /// The index of the default title.
- /// </summary>
- private int featureTitle;
-
- /// <summary>
- /// A value indicating whether this object has been disposed or not.
- /// </summary>
private bool disposed;
/// <summary>
/// </summary>
~HandBrakeInstance()
{
- if (this.hbHandle != IntPtr.Zero)
+ if (this.Handle != IntPtr.Zero)
{
this.Dispose(false);
}
/// <summary>
/// Gets the handle.
/// </summary>
- internal IntPtr Handle
- {
- get
- {
- return this.hbHandle;
- }
- }
+ internal IntPtr Handle { get; private set; }
/// <summary>
/// Gets the number of previews created during scan.
/// </summary>
- public int PreviewCount
- {
- get
- {
- return this.previewCount;
- }
- }
+ public int PreviewCount { get; private set; }
/// <summary>
/// Gets the list of titles on this instance.
/// </summary>
- public JsonScanObject Titles
- {
- get
- {
- return this.titles;
- }
- }
+ public JsonScanObject Titles { get; private set; }
/// <summary>
/// Gets the raw JSON for the list of titles on this instance.
/// </summary>
- public string TitlesJson
- {
- get
- {
- return this.titlesJson;
- }
- }
+ public string TitlesJson { get; private set; }
/// <summary>
/// Gets the index of the default title.
/// </summary>
- public int FeatureTitle
- {
- get
- {
- return this.featureTitle;
- }
- }
+ public int FeatureTitle { get; private set; }
/// <summary>
/// Gets the HandBrake version string.
/// </summary>
- 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));
/// <summary>
/// Gets the HandBrake build number.
/// </summary>
- public int Build
- {
- get
- {
- return HBFunctions.hb_get_build(this.hbHandle);
- }
- }
+ public int Build => HBFunctions.hb_get_build(this.Handle);
/// <summary>
/// Initializes this instance.
HandBrakeUtils.EnsureGlobalInit();
HandBrakeUtils.RegisterLogger();
- this.hbHandle = HBFunctions.hb_init(verbosity, update_check: 0);
+ this.Handle = HBFunctions.hb_init(verbosity, update_check: 0);
}
/// <summary>
/// </param>
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();
[HandleProcessCorruptedStateExceptions]
public void StopScan()
{
- HBFunctions.hb_scan_stop(this.hbHandle);
+ HBFunctions.hb_scan_stop(this.Handle);
}
/// <summary>
};
// 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<hb_image_s>(resultingImageStuct);
// Copy the filled image buffer to a managed array.
/// <returns>True if DRC can be applied to the track with the given encoder.</returns>
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;
}
/// <summary>
[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;
[HandleProcessCorruptedStateExceptions]
public void PauseEncode()
{
- HBFunctions.hb_pause(this.hbHandle);
+ HBFunctions.hb_pause(this.Handle);
}
/// <summary>
[HandleProcessCorruptedStateExceptions]
public void ResumeEncode()
{
- HBFunctions.hb_resume(this.hbHandle);
+ HBFunctions.hb_resume(this.Handle);
}
/// <summary>
[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<IntPtr>();
- 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);
}
}
[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<JsonState>(statusJson);
// 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);
[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))
{
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<JsonScanObject>(this.titlesJson);
- if (this.titles != null)
+ this.Titles = JsonConvert.DeserializeObject<JsonScanObject>(this.TitlesJson);
+ if (this.Titles != null)
{
- this.featureTitle = this.titles.MainFeature;
+ this.FeatureTitle = this.Titles.MainFeature;
}
}
[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<JsonState>(statusJson);
+++ /dev/null
-// --------------------------------------------------------------------------------------------------------------------
-// <copyright file="HandBrakeInstanceManager.cs" company="HandBrake Project (http://handbrake.fr)">
-// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
-// </copyright>
-// <summary>
-// The hand brake instance manager.
-// </summary>
-// --------------------------------------------------------------------------------------------------------------------
-
-namespace HandBrake.Interop.Interop
-{
- using System;
-
- using HandBrake.Interop.Interop.Interfaces;
- using HandBrake.Interop.Model;
-
- /// <summary>
- /// The HandBrake Instance manager.
- /// Only supports scanning right now.
- /// </summary>
- public static class HandBrakeInstanceManager
- {
- private static HandBrakeInstance scanInstance;
- private static HandBrakeInstance previewInstance;
-
- /// <summary>
- /// Initializes static members of the <see cref="HandBrakeInstanceManager"/> class.
- /// </summary>
- static HandBrakeInstanceManager()
- {
- }
-
- /// <summary>
- /// The init.
- /// </summary>
- public static void Init()
- {
- // Nothing to do. Triggers static constructor.
- }
-
- /// <summary>
- /// Gets the scanInstance.
- /// </summary>
- /// <param name="verbosity">
- /// The verbosity.
- /// </param>
- /// <returns>
- /// The <see cref="IHandBrakeInstance"/>.
- /// </returns>
- public static IHandBrakeInstance GetScanInstance(int verbosity)
- {
- if (scanInstance != null)
- {
- scanInstance.Dispose();
- scanInstance = null;
- }
-
- HandBrakeInstance newInstance = new HandBrakeInstance();
- newInstance.Initialize(verbosity);
- scanInstance = newInstance;
-
- return scanInstance;
- }
-
- /// <summary>
- /// The get encode instance.
- /// </summary>
- /// <param name="verbosity">
- /// The verbosity.
- /// </param>
- /// <param name="configuration">
- /// The configuration.
- /// </param>
- /// <returns>
- /// The <see cref="IHandBrakeInstance"/>.
- /// </returns>
- 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;
- }
-
- /// <summary>
- /// Gets the last scan scan instance.
- /// </summary>
- internal static IHandBrakeInstance LastScanScanInstance
- {
- get
- {
- return scanInstance;
- }
- }
-
- /// <summary>
- /// Gets the handle.
- /// </summary>
- internal static IntPtr LastScanHandle
- {
- get
- {
- return scanInstance.Handle;
- }
- }
- }
-}