From e6f26f89f2d084a2adb40e4c41e46de5c7e5d2c5 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sun, 29 May 2011 17:29:57 +0000 Subject: WinGui: Some additional work on the libhb encode service to move it closer to a working service. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4010 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../Converters/EnumToDescConverter.cs | 37 ++++- .../Extensions/StringExtensions.cs | 3 + .../Functions/InteropModelCreator.cs | 166 +++++++++++++++++++++ .../Functions/PlistHelper.cs | 1 - .../HandBrake.ApplicationServices.csproj | 1 + .../Services/Base/EncodeBase.cs | 27 ++++ .../Services/Encode.cs | 14 +- .../Services/LibEncode.cs | 95 +++++++++++- 8 files changed, 323 insertions(+), 21 deletions(-) create mode 100644 win/CS/HandBrake.ApplicationServices/Functions/InteropModelCreator.cs diff --git a/win/CS/HandBrake.ApplicationServices/Converters/EnumToDescConverter.cs b/win/CS/HandBrake.ApplicationServices/Converters/EnumToDescConverter.cs index a03a3f071..d66f27a7e 100644 --- a/win/CS/HandBrake.ApplicationServices/Converters/EnumToDescConverter.cs +++ b/win/CS/HandBrake.ApplicationServices/Converters/EnumToDescConverter.cs @@ -1,17 +1,50 @@ -namespace HandBrake.ApplicationServices.Converters +/* EnumToDescConveter.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + + +namespace HandBrake.ApplicationServices.Converters { - using System.ComponentModel; using System; + using System.ComponentModel; using HandBrake.ApplicationServices.Functions; + /// + /// Enum to Description Converter + /// public class EnumToDescConveter : EnumConverter { + /// + /// Initializes a new instance of the class. + /// + /// + /// The type. + /// public EnumToDescConveter(Type type) : base(type) { } + /// + /// Convert To an Object. + /// + /// + /// The context. + /// + /// + /// The culture. + /// + /// + /// The value. + /// + /// + /// The destination type. + /// + /// + /// The Enum Object + /// public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, diff --git a/win/CS/HandBrake.ApplicationServices/Extensions/StringExtensions.cs b/win/CS/HandBrake.ApplicationServices/Extensions/StringExtensions.cs index 42f40c13b..d420e68c3 100644 --- a/win/CS/HandBrake.ApplicationServices/Extensions/StringExtensions.cs +++ b/win/CS/HandBrake.ApplicationServices/Extensions/StringExtensions.cs @@ -7,6 +7,9 @@ namespace HandBrake.ApplicationServices.Extensions { using System.Text; + /// + /// String Extensions + /// public static class StringExtensions { /// diff --git a/win/CS/HandBrake.ApplicationServices/Functions/InteropModelCreator.cs b/win/CS/HandBrake.ApplicationServices/Functions/InteropModelCreator.cs new file mode 100644 index 000000000..639cf34b2 --- /dev/null +++ b/win/CS/HandBrake.ApplicationServices/Functions/InteropModelCreator.cs @@ -0,0 +1,166 @@ +/* InteropModelCreator.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +namespace HandBrake.ApplicationServices.Functions +{ + using System.Collections.Generic; + + using HandBrake.ApplicationServices.Model; + using HandBrake.ApplicationServices.Model.Encoding; + using HandBrake.Interop; + + using Cropping = HandBrake.Interop.Cropping; + + /// + /// A Utility Class to Convert a + /// + public class InteropModelCreator + { + /* + * TODO: This conversion class needs to be finished off before libencode will work. + */ + + + + /// + /// Get an EncodeJob model for a LibHB Encode. + /// + /// + /// The task. + /// + /// + /// An Interop.EncodeJob model. + /// + public static EncodeJob GetEncodeJob(QueueTask task) + { + // Sanity Checking + if (task == null || task.Task == null) + { + return null; + } + + // The current Job Configuration + EncodeTask work = task.Task; + + // Which will be converted to this EncodeJob Model. + EncodeJob job = new EncodeJob(); + EncodingProfile profile = new EncodingProfile(); + job.EncodingProfile = profile; + + + switch (work.Anamorphic) + { + case Model.Encoding.Anamorphic.Custom: + profile.Anamorphic = Interop.Anamorphic.Custom; + break; + case Model.Encoding.Anamorphic.Strict: + profile.Anamorphic = Interop.Anamorphic.Strict; + break; + case Model.Encoding.Anamorphic.Loose: + profile.Anamorphic = Interop.Anamorphic.Loose; + break; + case Model.Encoding.Anamorphic.None: + profile.Anamorphic = Interop.Anamorphic.None; + break; + } + + + profile.AudioEncodings = new List(); + foreach (AudioTrack track in work.AudioTracks) + { + AudioEncoding newTrack = new AudioEncoding + { + Bitrate = track.Bitrate, + Drc = track.DRC, + Gain = track.Gain, + //Encoder = track.Encoder, + // InputNumber = track.Track, + //Mixdown = track.MixDown, + //SampleRateRaw = track.SampleRate + }; + + profile.AudioEncodings.Add(newTrack); + } + + profile.Cropping = new Cropping + { + Top = work.Cropping.Top, + Bottom = work.Cropping.Bottom, + Left = work.Cropping.Left, + Right = work.Cropping.Right + }; + + profile.CustomCropping = true; + profile.CustomDecomb = work.CustomDecomb; + profile.CustomDeinterlace = work.CustomDeinterlace; + profile.CustomDenoise = work.CustomDenoise; + profile.CustomDetelecine = work.CustomDetelecine; + profile.Deblock = work.Deblock; + //profile.Decomb = work.Decomb; + //profile.Deinterlace = work.Deinterlace; + //profile.Denoise = work.Denoise; + //profile.Detelecine = work.Detelecine; + //profile.DisplayWidth = work.DisplayWidth; + profile.Framerate = work.Framerate.HasValue ? work.Framerate.Value : 0; + profile.Grayscale = work.Grayscale; + profile.Height = work.Height.HasValue ? work.Height.Value : 0; + profile.IPod5GSupport = work.IPod5GSupport; + profile.IncludeChapterMarkers = work.IncludeChapterMarkers; + profile.KeepDisplayAspect = work.KeepDisplayAspect; + profile.LargeFile = work.LargeFile; + profile.MaxHeight = work.MaxHeight.HasValue ? work.MaxHeight.Value : 0; + profile.MaxWidth = work.MaxWidth.HasValue ? work.MaxWidth.Value : 0; + profile.Modulus = work.Modulus.HasValue ? work.Modulus.Value : 16; + profile.Optimize = work.OptimizeMP4; + //profile.OutputFormat = work.OutputFormat; + profile.PeakFramerate = work.FramerateMode == FramerateMode.PFR; + profile.PixelAspectX = work.PixelAspectX; + profile.PixelAspectY = work.PixelAspectY; + //profile.PreferredExtension = work.OutputFormat; + profile.Quality = work.Quality.HasValue ? work.Quality.Value : 0; + profile.UseDisplayWidth = true; + profile.VideoBitrate = work.VideoBitrate.HasValue ? work.VideoBitrate.Value : 0; + //profile.VideoEncodeRateType = work.VideoEncodeRateType; + //profile.VideoEncoder = work.VideoEncoder; + profile.Width = work.Width.HasValue ? work.Width.Value : 0; + profile.X264Options = work.AdvancedEncoderOptions; + + if (work.PointToPointMode == PointToPointMode.Chapters) + { + job.ChapterStart = work.StartPoint; + job.ChapterEnd = work.EndPoint; + } + + job.Angle = work.Angle; + job.EncodingProfile = profile; + if (work.PointToPointMode == PointToPointMode.Frames) + { + job.FramesEnd = work.EndPoint; + job.FramesStart = work.StartPoint; + } + + job.OutputPath = work.Destination; + //job.RangeType = work.PointToPointMode; + + if (work.PointToPointMode == PointToPointMode.Seconds) + { + job.SecondsEnd = work.EndPoint; + job.SecondsStart = work.StartPoint; + } + + job.SourcePath = work.Source; + // job.SourceType = work.Type; + job.Title = work.Title; + job.Subtitles = new Subtitles { SourceSubtitles = new List(), SrtSubtitles = new List() }; + foreach (SubtitleTrack track in work.SubtitleTracks) + { + // TODO + } + + + return job; + } + } +} diff --git a/win/CS/HandBrake.ApplicationServices/Functions/PlistHelper.cs b/win/CS/HandBrake.ApplicationServices/Functions/PlistHelper.cs index fab40a27c..8ce092dad 100644 --- a/win/CS/HandBrake.ApplicationServices/Functions/PlistHelper.cs +++ b/win/CS/HandBrake.ApplicationServices/Functions/PlistHelper.cs @@ -3,7 +3,6 @@ Homepage: . It may be used under the terms of the GNU General Public License. */ - namespace HandBrake.ApplicationServices.Functions { using System; diff --git a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj index 2140890cb..b88becd39 100644 --- a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj +++ b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj @@ -94,6 +94,7 @@ + diff --git a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs index ecaafeeb2..ca9558175 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs @@ -336,6 +336,33 @@ namespace HandBrake.ApplicationServices.Services.Base } } + /// + /// Verify the Encode Destination path exists and if not, create it. + /// + /// + /// The task. + /// + /// + /// If the creation fails, an exception is thrown. + /// + protected void VerifyEncodeDestinationPath(QueueTask task) + { + // Make sure the path exists, attempt to create it if it doesn't + string path = Directory.GetParent(task.Destination).ToString(); + if (!Directory.Exists(path)) + { + try + { + Directory.CreateDirectory(path); + } + catch (Exception) + { + throw new Exception( + "Unable to create directory for the encoded output. Please verify the drive and path is correct."); + } + } + } + #endregion } } \ No newline at end of file diff --git a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs index 12f1e2760..26708434f 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs @@ -103,19 +103,7 @@ namespace HandBrake.ApplicationServices.Services } // Make sure the path exists, attempt to create it if it doesn't - string path = Directory.GetParent(queueTask.Destination).ToString(); - if (!Directory.Exists(path)) - { - try - { - Directory.CreateDirectory(path); - } - catch (Exception) - { - throw new Exception( - "Unable to create directory for the encoded output. Please verify the drive and path is correct."); - } - } + this.VerifyEncodeDestinationPath(queueTask); string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, queueTask.Query) diff --git a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs index 0cacea2ac..d8a951d51 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs @@ -6,7 +6,9 @@ namespace HandBrake.ApplicationServices.Services { using System; + using System.Diagnostics; using System.Text; + using System.Threading; using HandBrake.ApplicationServices.Functions; using HandBrake.ApplicationServices.Model; @@ -39,6 +41,11 @@ namespace HandBrake.ApplicationServices.Services /// private HandBrakeInstance instance; + /// + /// A flag to indicate if logging is enabled or not. + /// + private bool loggingEnabled; + #endregion /// @@ -68,8 +75,80 @@ namespace HandBrake.ApplicationServices.Services /// public void Start(QueueTask job, bool enableLogging) { - throw new NotImplementedException("This will be implemented later."); + throw new NotImplementedException("This Method has not been completed yet"); + this.startTime = DateTime.Now; + this.loggingEnabled = enableLogging; + + try + { + // Sanity Checking and Setup + if (this.IsEncoding) + { + throw new Exception("HandBrake is already encodeing."); + } + + this.IsEncoding = true; + + // Get an EncodeJob object for the Interop Library + EncodeJob encodeJob = InteropModelCreator.GetEncodeJob(job); + + // Enable logging if required. + if (enableLogging) + { + try + { + this.SetupLogging(job); + } + catch (Exception) + { + this.IsEncoding = false; + throw; + } + } + + // Prvent the system from sleeping if the user asks + if (Properties.Settings.Default.PreventSleep) + { + Win32.PreventSleep(); + } + + // Verify the Destination Path Exists, and if not, create it. + this.VerifyEncodeDestinationPath(job); + + // Start the Encode + this.instance.StartEncode(encodeJob); + + // Set the Process Priority + switch (Properties.Settings.Default.ProcessPriority) + { + case "Realtime": + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; + break; + case "High": + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; + break; + case "Above Normal": + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; + break; + case "Normal": + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; + break; + case "Low": + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; + break; + default: + Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; + break; + } + + // Fire the Encode Started Event + this.Invoke_encodeStarted(EventArgs.Empty); + } + catch (Exception exc) + { + this.Invoke_encodeCompleted(new EncodeCompletedEventArgs(false, exc, "An Error has occured in EncodeService.Run()")); + } } /// @@ -119,9 +198,12 @@ namespace HandBrake.ApplicationServices.Services /// private void HandBrakeInstanceErrorLogged(object sender, MessageLoggedEventArgs e) { - lock (logLock) + if (this.loggingEnabled) { - this.LogBuffer.AppendLine(e.Message); + lock (logLock) + { + this.LogBuffer.AppendLine(e.Message); + } } } @@ -136,9 +218,12 @@ namespace HandBrake.ApplicationServices.Services /// private void HandBrakeInstanceMessageLogged(object sender, MessageLoggedEventArgs e) { - lock (logLock) + if (this.loggingEnabled) { - this.LogBuffer.AppendLine(e.Message); + lock (logLock) + { + this.LogBuffer.AppendLine(e.Message); + } } } -- cgit v1.2.3