From cbe5f09e69c95df3be3b38de06c2b7bd6b5bd949 Mon Sep 17 00:00:00 2001 From: sr55 Date: Wed, 22 Aug 2012 20:11:18 +0000 Subject: WinGui: Initial Work to wire up Encode Process Isolation. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4914 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../EventArgs/EncodeCompletedEventArgs.cs | 5 + .../EventArgs/EncodeProgressEventArgs.cs | 9 + .../Services/Base/EncodeBase.cs | 28 ++- .../Services/Encode.cs | 13 +- .../Services/Interfaces/IHbServiceCallback.cs | 24 ++ .../Services/Interfaces/IQueueProcessor.cs | 9 + .../Services/Interfaces/IServerService.cs | 52 +++- .../Services/LibEncode.cs | 10 +- .../Services/QueueProcessor.cs | 13 +- .../Services/ServerService.cs | 267 ++++++++++++++++----- .../Utilities/QueryGeneratorUtility.cs | 29 ++- .../Utilities/QueryParserUtility.cs | 1 - win/CS/HandBrakeWPF/HandBrakeWPF.csproj | 2 + .../Isolation/BackgroundServiceConnector.cs | 134 +++++++---- .../Isolation/Interfaces/IIsolatedEncodeService.cs | 24 ++ .../Isolation/IsolatedEncodeService.cs | 189 +++++++++++++++ .../HandBrakeWPF/Isolation/IsolatedScanService.cs | 13 +- win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 30 ++- 18 files changed, 698 insertions(+), 154 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Isolation/Interfaces/IIsolatedEncodeService.cs create mode 100644 win/CS/HandBrakeWPF/Isolation/IsolatedEncodeService.cs (limited to 'win/CS') diff --git a/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeCompletedEventArgs.cs b/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeCompletedEventArgs.cs index 12f3af2e9..44a66cac7 100644 --- a/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeCompletedEventArgs.cs +++ b/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeCompletedEventArgs.cs @@ -10,10 +10,12 @@ namespace HandBrake.ApplicationServices.EventArgs { using System; + using System.Runtime.Serialization; /// /// Encode Progress Event Args /// + [DataContractAttribute] public class EncodeCompletedEventArgs : EventArgs { /// @@ -38,16 +40,19 @@ namespace HandBrake.ApplicationServices.EventArgs /// /// Gets or sets a value indicating whether Successful. /// + [DataMember] public bool Successful { get; set; } /// /// Gets or sets Exception. /// + [DataMember] public Exception Exception { get; set; } /// /// Gets or sets ErrorInformation. /// + [DataMember] public string ErrorInformation { get; set; } } } diff --git a/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeProgressEventArgs.cs b/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeProgressEventArgs.cs index f4c7e9446..37cb76f25 100644 --- a/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeProgressEventArgs.cs +++ b/win/CS/HandBrake.ApplicationServices/EventArgs/EncodeProgressEventArgs.cs @@ -10,45 +10,54 @@ namespace HandBrake.ApplicationServices.EventArgs { using System; + using System.Runtime.Serialization; /// /// Encode Progress Event Args /// + [DataContractAttribute] public class EncodeProgressEventArgs : EventArgs { /// /// Gets or sets PercentComplete. /// + [DataMember] public float PercentComplete { get; set; } /// /// Gets or sets CurrentFrameRate. /// + [DataMember] public float CurrentFrameRate { get; set; } /// /// Gets or sets AverageFrameRate. /// + [DataMember] public float AverageFrameRate { get; set; } /// /// Gets or sets EstimatedTimeLeft. /// + [DataMember] public TimeSpan EstimatedTimeLeft { get; set; } /// /// Gets or sets Task. /// + [DataMember] public int Task { get; set; } /// /// Gets or sets TaskCount. /// + [DataMember] public int TaskCount { get; set; } /// /// Gets or sets ElapsedTime. /// + [DataMember] public TimeSpan ElapsedTime { get; set; } } } diff --git a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs index e20215bcf..36b9002bc 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs @@ -13,8 +13,6 @@ namespace HandBrake.ApplicationServices.Services.Base using System.IO; using System.Text; - using Caliburn.Micro; - using HandBrake.ApplicationServices.EventArgs; using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Model; @@ -31,17 +29,22 @@ namespace HandBrake.ApplicationServices.Services.Base /// /// A Lock for the filewriter /// - private static readonly object fileWriterLock = new object(); + private static readonly object FileWriterLock = new object(); /// /// The User Setting Service /// - private IUserSettingService userSettingService = IoC.Get(); + private readonly IUserSettingService userSettingService; /// /// Windows 7 API Pack wrapper /// - private Win7 windowsSeven = new Win7(); + private readonly Win7 windowsSeven = new Win7(); + + /// + /// The Log File Header + /// + private readonly StringBuilder header = GeneralUtilities.CreateCliLogHeader(); /// /// The Log Buffer @@ -53,18 +56,17 @@ namespace HandBrake.ApplicationServices.Services.Base /// private StreamWriter fileWriter; - /// - /// The Log File Header - /// - private StringBuilder header = GeneralUtilities.CreateCliLogHeader(); - #endregion /// /// Initializes a new instance of the class. /// - public EncodeBase() + /// + /// The user Setting Service. + /// + public EncodeBase(IUserSettingService userSettingService) { + this.userSettingService = userSettingService; this.logBuffer = new StringBuilder(); } @@ -310,7 +312,7 @@ namespace HandBrake.ApplicationServices.Services.Base this.LogBuffer.AppendLine(message); } - lock (fileWriterLock) + lock (FileWriterLock) { if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite) { @@ -341,7 +343,7 @@ namespace HandBrake.ApplicationServices.Services.Base { try { - lock (fileWriterLock) + lock (FileWriterLock) { if (this.fileWriter != null) { diff --git a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs index f0f1b656b..98827a6e3 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs @@ -15,8 +15,6 @@ namespace HandBrake.ApplicationServices.Services using System.Threading; using System.Windows.Forms; - using Caliburn.Micro; - using HandBrake.ApplicationServices.EventArgs; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Services.Base; @@ -35,7 +33,7 @@ namespace HandBrake.ApplicationServices.Services /// /// The User Setting Service /// - private IUserSettingService userSettingService = IoC.Get(); + private readonly IUserSettingService userSettingService; /// /// Gets The Process Handle @@ -62,8 +60,13 @@ namespace HandBrake.ApplicationServices.Services /// /// Initializes a new instance of the class. /// - public Encode() + /// + /// The user Setting Service. + /// + public Encode(IUserSettingService userSettingService) + : base(userSettingService) { + this.userSettingService = userSettingService; this.EncodeStarted += this.EncodeEncodeStarted; GrowlCommunicator.Register(); } @@ -116,7 +119,7 @@ namespace HandBrake.ApplicationServices.Services if (this.userSettingService.GetUserSetting(ASUserSettingConstants.PreventSleep)) { - Win32.PreventSleep(); + // Win32.PreventSleep(); } // Make sure the path exists, attempt to create it if it doesn't diff --git a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IHbServiceCallback.cs b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IHbServiceCallback.cs index afb7ca5c3..1d10e3062 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IHbServiceCallback.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IHbServiceCallback.cs @@ -42,5 +42,29 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// [OperationContract(IsOneWay = true)] void ScanStartedCallback(); + + /// + /// The encode progress callback. + /// + /// + /// The event Args. + /// + [OperationContract(IsOneWay = true)] + void EncodeProgressCallback(EncodeProgressEventArgs eventArgs); + + /// + /// The encode completed callback. + /// + /// + /// The event Args. + /// + [OperationContract(IsOneWay = true)] + void EncodeCompletedCallback(EncodeCompletedEventArgs eventArgs); + + /// + /// The encode started callback. + /// + [OperationContract(IsOneWay = true)] + void EncodeStartedCallback(); } } diff --git a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs index 22cf365b0..b695bab49 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IQueueProcessor.cs @@ -56,5 +56,14 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// Requests a pause of the encode queue. /// void Pause(); + + /// + /// Swap encode service. + /// Temp method until Castle is hooked up. + /// + /// + /// The service. + /// + void SwapEncodeService(IEncode service); } } \ No newline at end of file diff --git a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IServerService.cs b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IServerService.cs index 22156e6fc..49eedffa5 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IServerService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IServerService.cs @@ -12,6 +12,7 @@ namespace HandBrake.ApplicationServices.Services.Interfaces using System.Runtime.Serialization; using System.ServiceModel; + using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Parsing; /// @@ -24,7 +25,13 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// Gets the activity log. /// [DataMember] - string ActivityLog { get; } + string ScanActivityLog { get; } + + /// + /// Gets the activity log. + /// + [DataMember] + string EncodeActivityLog { get; } /// /// Gets the souce data. @@ -38,8 +45,20 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// /// Gets a value indicating whether is scanning. /// - [DataMember] - bool IsScanning { get; } + bool IsScanning + { + [OperationContract] + get; + } + + /// + /// Gets a value indicating whether is encoding. + /// + bool IsEncoding + { + [OperationContract] + get; + } /// /// Start the WCF Service @@ -66,6 +85,33 @@ namespace HandBrake.ApplicationServices.Services.Interfaces [OperationContract] void ScanSource(string path, int title, int previewCount); + /// + /// Start and Encode + /// + /// + /// The job. + /// + /// + /// The enable logging. + /// + [OperationContract] + void StartEncode(QueueTask job, bool enableLogging); + + /// + /// The process encode logs. + /// + /// + /// The destination. + /// + [OperationContract] + void ProcessEncodeLogs(string destination); + + /// + /// Stop and Encode + /// + [OperationContract] + void StopEncode(); + /// /// Stop the scan. /// diff --git a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs index 086305f4e..3ae06bbfd 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs @@ -40,7 +40,7 @@ namespace HandBrake.ApplicationServices.Services /// /// The User Setting Service /// - private IUserSettingService userSettingService = IoC.Get(); + private readonly IUserSettingService userSettingService; /// /// The Start time of the current Encode; @@ -62,8 +62,14 @@ namespace HandBrake.ApplicationServices.Services /// /// Initializes a new instance of the class. /// - public LibEncode() + /// + /// The user Setting Service. + /// + public LibEncode(IUserSettingService userSettingService) + : base(userSettingService) { + this.userSettingService = userSettingService; + // Setup the HandBrake Instance this.instance = IoC.Get(); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; diff --git a/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs b/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs index cd1ff226d..f90aa0ef4 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/QueueProcessor.cs @@ -11,7 +11,6 @@ namespace HandBrake.ApplicationServices.Services { using System; using System.Diagnostics; - using System.IO; using System.Windows.Forms; using Caliburn.Micro; @@ -180,6 +179,18 @@ namespace HandBrake.ApplicationServices.Services this.IsProcessing = false; } + /// + /// Swap encode service. + /// Temp method until Castle is hooked up. + /// + /// + /// The service. + /// + public void SwapEncodeService(IEncode service) + { + this.EncodeService = service; + } + /// /// After an encode is complete, move onto the next job. /// diff --git a/win/CS/HandBrake.ApplicationServices/Services/ServerService.cs b/win/CS/HandBrake.ApplicationServices/Services/ServerService.cs index 025b7c432..9984ade21 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/ServerService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/ServerService.cs @@ -16,13 +16,15 @@ namespace HandBrake.ApplicationServices.Services using System.Windows; using HandBrake.ApplicationServices.EventArgs; + using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Parsing; using HandBrake.ApplicationServices.Services.Interfaces; /// /// HandBrake WCF Service /// - [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true, ConcurrencyMode = ConcurrencyMode.Single)] + [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true, + ConcurrencyMode = ConcurrencyMode.Single)] public class ServerService : IServerService { #region Constants and Fields @@ -32,6 +34,11 @@ namespace HandBrake.ApplicationServices.Services /// private static readonly List Subscribers = new List(); + /// + /// The encode service. + /// + private static IEncode encodeService; + /// /// The scan service. /// @@ -44,45 +51,47 @@ namespace HandBrake.ApplicationServices.Services #endregion - #region Implemented Interfaces - - #region IServerService + #region Properties /// - /// The scan source. + /// Gets the activity log. /// - /// - /// The path. - /// - /// - /// The title. - /// - /// - /// The preview Count. - /// - public void ScanSource(string path, int title, int previewCount) + [DataMember] + public string ActivityLog { - Console.WriteLine("Starting Source Scan for: " + path); - scanService.ScanStared += this.ScanStaredHandler; - scanService.ScanStatusChanged += this.ScanStatusChangedHandler; - scanService.ScanCompleted += this.ScanCompletedHandler; - - scanService.Scan(path, title, previewCount, null); + get + { + return scanService.ActivityLog; + } } /// /// Gets the activity log. /// + public string EncodeActivityLog { get; private set; } + + /// + /// Gets a value indicating whether is encoding. + /// + public bool IsEncoding { get; private set; } + + /// + /// Gets a value indicating whether is scanning. + /// [DataMember] - public string ActivityLog + public bool IsScanning { get { - return scanService.ActivityLog; - + return scanService.IsScanning; } } + /// + /// Gets the activity log. + /// + public string ScanActivityLog { get; private set; } + /// /// Gets the souce data. /// @@ -95,16 +104,43 @@ namespace HandBrake.ApplicationServices.Services } } + #endregion + + #region Implemented Interfaces + + #region IServerService + /// - /// Gets a value indicating whether is scanning. + /// The process encode logs. /// - [DataMember] - public bool IsScanning + /// + /// The destination. + /// + public void ProcessEncodeLogs(string destination) { - get - { - return scanService.IsScanning; - } + encodeService.ProcessLogs(destination); + } + + /// + /// The scan source. + /// + /// + /// The path. + /// + /// + /// The title. + /// + /// + /// The preview Count. + /// + public void ScanSource(string path, int title, int previewCount) + { + Console.WriteLine("Starting Source Scan for: " + path); + scanService.ScanStared += this.ScanStaredHandler; + scanService.ScanStatusChanged += this.ScanStatusChangedHandler; + scanService.ScanCompleted += this.ScanCompletedHandler; + + scanService.Scan(path, title, previewCount, null); } /// @@ -121,11 +157,30 @@ namespace HandBrake.ApplicationServices.Services Console.WriteLine("Service Started"); // Setup the services we are going to use. - scanService = new ScanService(new UserSettingService()); + scanService = new ScanService(new UserSettingService()); // TODO this needs wired up with castle + encodeService = new Encode(new UserSettingService()); Console.ReadLine(); } } + /// + /// Start and Encode + /// + /// + /// The job. + /// + /// + /// The enable logging. + /// + public void StartEncode(QueueTask job, bool enableLogging) + { + Console.WriteLine("Starting Source Encode for: " + job.Task.Source); + encodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted; + encodeService.EncodeStarted += this.encodeService_EncodeStarted; + encodeService.EncodeStatusChanged += this.encodeService_EncodeStatusChanged; + encodeService.Start(job, enableLogging); + } + /// /// Stop this service /// @@ -138,6 +193,14 @@ namespace HandBrake.ApplicationServices.Services } } + /// + /// Stop and Encode + /// + public void StopEncode() + { + encodeService.Stop(); + } + /// /// Stop the scan. /// @@ -222,17 +285,17 @@ namespace HandBrake.ApplicationServices.Services { Subscribers.ForEach( delegate(IHbServiceCallback callback) - { - if (((ICommunicationObject)callback).State == CommunicationState.Opened) { - Console.WriteLine("Scan Completed Callback"); - callback.ScanCompletedCallback(e); - } - else - { - Subscribers.Remove(callback); - } - }); + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Scan Completed Callback"); + callback.ScanCompletedCallback(e); + } + else + { + Subscribers.Remove(callback); + } + }); scanService.ScanStared -= this.ScanStaredHandler; scanService.ScanStatusChanged -= this.ScanStatusChangedHandler; @@ -252,17 +315,17 @@ namespace HandBrake.ApplicationServices.Services { Subscribers.ForEach( delegate(IHbServiceCallback callback) - { - if (((ICommunicationObject)callback).State == CommunicationState.Opened) - { - Console.WriteLine("Scan Started Callback"); - callback.ScanStartedCallback(); - } - else { - Subscribers.Remove(callback); - } - }); + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Scan Started Callback"); + callback.ScanStartedCallback(); + } + else + { + Subscribers.Remove(callback); + } + }); } /// @@ -278,17 +341,99 @@ namespace HandBrake.ApplicationServices.Services { Subscribers.ForEach( delegate(IHbServiceCallback callback) - { - if (((ICommunicationObject)callback).State == CommunicationState.Opened) { - Console.WriteLine("Scan Changed Callback"); - callback.ScanProgressCallback(e); - } - else + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Scan Changed Callback"); + callback.ScanProgressCallback(e); + } + else + { + Subscribers.Remove(callback); + } + }); + } + + /// + /// The encode service_ encode completed. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e) + { + encodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted; + encodeService.EncodeStarted -= this.encodeService_EncodeStarted; + encodeService.EncodeStatusChanged -= this.encodeService_EncodeStatusChanged; + + Subscribers.ForEach( + delegate(IHbServiceCallback callback) { - Subscribers.Remove(callback); - } - }); + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Encode Completed Callback"); + callback.EncodeCompletedCallback(e); + } + else + { + Subscribers.Remove(callback); + } + }); + } + + /// + /// The encode service_ encode started. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void encodeService_EncodeStarted(object sender, EventArgs e) + { + Subscribers.ForEach( + delegate(IHbServiceCallback callback) + { + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Encode Started Callback"); + callback.EncodeStartedCallback(); + } + else + { + Subscribers.Remove(callback); + } + }); + } + + /// + /// The encode service_ encode status changed. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void encodeService_EncodeStatusChanged(object sender, EncodeProgressEventArgs e) + { + Subscribers.ForEach( + delegate(IHbServiceCallback callback) + { + if (((ICommunicationObject)callback).State == CommunicationState.Opened) + { + Console.WriteLine("Encode Status Callback"); + callback.EncodeProgressCallback(e); + } + else + { + Subscribers.Remove(callback); + } + }); } #endregion diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/QueryGeneratorUtility.cs b/win/CS/HandBrake.ApplicationServices/Utilities/QueryGeneratorUtility.cs index 4b50eef1a..b28de2264 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/QueryGeneratorUtility.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/QueryGeneratorUtility.cs @@ -19,6 +19,7 @@ namespace HandBrake.ApplicationServices.Utilities using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Model.Encoding; + using HandBrake.ApplicationServices.Services; using HandBrake.ApplicationServices.Services.Interfaces; using HandBrake.Interop.Model.Encoding; using HandBrake.Interop.Model.Encoding.x264; @@ -31,7 +32,7 @@ namespace HandBrake.ApplicationServices.Utilities /// /// Backing field for the user settings service. /// - private static readonly IUserSettingService UserSettingService = IoC.Get(); + private static IUserSettingService UserSettingService; /// /// Generate a CLI Query for an EncodeTask Model object @@ -44,6 +45,19 @@ namespace HandBrake.ApplicationServices.Utilities /// public static string GenerateQuery(EncodeTask task) { + // TODO Remove this quick hack + if (UserSettingService == null) + { + try + { + UserSettingService = IoC.Get(); + } + catch (Exception exc) + { + UserSettingService = new UserSettingService(); + } + } + string query = string.Empty; query += SourceQuery(task, null, null); query += DestinationQuery(task); @@ -69,6 +83,19 @@ namespace HandBrake.ApplicationServices.Utilities /// public static string GeneratePreviewQuery(EncodeTask task, int duration, string startAtPreview) { + // TODO Remove this quick hack + if (UserSettingService == null) + { + try + { + UserSettingService = IoC.Get(); + } + catch (Exception exc) + { + UserSettingService = new UserSettingService(); + } + } + string query = string.Empty; query += SourceQuery(task, duration, startAtPreview); query += DestinationQuery(task); diff --git a/win/CS/HandBrake.ApplicationServices/Utilities/QueryParserUtility.cs b/win/CS/HandBrake.ApplicationServices/Utilities/QueryParserUtility.cs index e16274fe6..c13511580 100644 --- a/win/CS/HandBrake.ApplicationServices/Utilities/QueryParserUtility.cs +++ b/win/CS/HandBrake.ApplicationServices/Utilities/QueryParserUtility.cs @@ -10,7 +10,6 @@ namespace HandBrake.ApplicationServices.Utilities { using System; - using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index d560cc961..d3682caeb 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -130,6 +130,8 @@ + + diff --git a/win/CS/HandBrakeWPF/Isolation/BackgroundServiceConnector.cs b/win/CS/HandBrakeWPF/Isolation/BackgroundServiceConnector.cs index b6198da46..0d08f2dca 100644 --- a/win/CS/HandBrakeWPF/Isolation/BackgroundServiceConnector.cs +++ b/win/CS/HandBrakeWPF/Isolation/BackgroundServiceConnector.cs @@ -28,23 +28,23 @@ namespace HandBrakeWPF.Isolation /// public class BackgroundServiceConnector : IHbServiceCallback, IDisposable { + #region Constants and Fields + /// /// The error service. /// private readonly IErrorService errorService; - #region Constants and Fields - /// /// Gets or sets the pipe factory. /// DuplexChannelFactory is necessary for Callbacks. /// - private DuplexChannelFactory pipeFactory; + private static DuplexChannelFactory pipeFactory; /// /// The background process. /// - private Process backgroundProcess; + private static Process backgroundProcess; #endregion @@ -73,7 +73,7 @@ namespace HandBrakeWPF.Isolation #endregion - #region Public Methods + #region Public Server Management Methods /// /// The can connect. @@ -91,19 +91,24 @@ namespace HandBrakeWPF.Isolation /// public void Connect() { + if (backgroundProcess == null) + { + // backgroundProcess = Process.Start("HandBrake.Server.exe"); + } + ThreadPool.QueueUserWorkItem(delegate { try { - this.pipeFactory = new DuplexChannelFactory( + pipeFactory = new DuplexChannelFactory( new InstanceContext(this), new NetTcpBinding(), new EndpointAddress("net.tcp://127.0.0.1:8000/IHbService")); // Connect and Subscribe to the Server - this.Service = this.pipeFactory.CreateChannel(); - this.Service.Subscribe(); - this.IsConnected = true; + Service = pipeFactory.CreateChannel(); + Service.Subscribe(); + IsConnected = true; } catch (Exception exc) { @@ -121,7 +126,7 @@ namespace HandBrakeWPF.Isolation { try { - this.Service.Unsubscribe(); + Service.Unsubscribe(); } catch (Exception exc) { @@ -130,33 +135,56 @@ namespace HandBrakeWPF.Isolation } } - /// - /// The scan source. - /// - /// - /// The path. - /// - /// - /// The title. - /// - /// - /// The preview count. - /// - public void ScanSource(string path, int title, int previewCount) - { - ThreadPool.QueueUserWorkItem(delegate { this.Service.ScanSource(path, title, previewCount); }); - } + #endregion - /// - /// The start server. - /// - public void StartServer() - { - if (this.backgroundProcess == null) - { - this.backgroundProcess = Process.Start("HandBrake.Server.exe"); - } - } + #region Public Service Methods + + ///// + ///// The scan source. + ///// + ///// + ///// The path. + ///// + ///// + ///// The title. + ///// + ///// + ///// The preview count. + ///// + //public void ScanSource(string path, int title, int previewCount) + //{ + // ThreadPool.QueueUserWorkItem(delegate { this.Service.ScanSource(path, title, previewCount); }); + //} + + ///// + ///// The stop scan. + ///// + //public void StopScan() + //{ + // ThreadPool.QueueUserWorkItem(delegate { this.Service.StopScan(); }); + //} + + ///// + ///// Start an Encode + ///// + ///// + ///// The job. + ///// + ///// + ///// The enable logging. + ///// + //public void StartEncode(QueueTask job, bool enableLogging) + //{ + // ThreadPool.QueueUserWorkItem(delegate { this.Service.StartEncode(job, enableLogging); }); + //} + + ///// + ///// Stop an Encode + ///// + //public void StopEncode() + //{ + // ThreadPool.QueueUserWorkItem(delegate { this.Service.StopEncode(); }); + //} #endregion @@ -169,7 +197,7 @@ namespace HandBrakeWPF.Isolation /// public void Dispose() { - this.Service.Unsubscribe(); + Service.Unsubscribe(); } #endregion @@ -203,23 +231,35 @@ namespace HandBrakeWPF.Isolation { } - #endregion - - #endregion - - #region Implementation of IHbServiceCallback + /// + /// The encode progress callback. + /// + /// + /// The event Args. + /// + public virtual void EncodeProgressCallback(EncodeProgressEventArgs eventArgs) + { + } /// - /// The test. + /// The encode completed callback. /// - /// - /// The message. + /// + /// The event Args. /// - public void Test(string message) + public virtual void EncodeCompletedCallback(EncodeCompletedEventArgs eventArgs) { - Console.WriteLine(message); } + /// + /// The encode started callback. + /// + public virtual void EncodeStartedCallback() + { + } + + #endregion + #endregion } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Isolation/Interfaces/IIsolatedEncodeService.cs b/win/CS/HandBrakeWPF/Isolation/Interfaces/IIsolatedEncodeService.cs new file mode 100644 index 000000000..fb9912ccf --- /dev/null +++ b/win/CS/HandBrakeWPF/Isolation/Interfaces/IIsolatedEncodeService.cs @@ -0,0 +1,24 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// The Isolated Encode Service interface. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Isolation.Interfaces +{ + using HandBrake.ApplicationServices.Services.Interfaces; + + /// + /// The Isolated Encode Service interface. + /// + public interface IIsolatedEncodeService : IEncode + { + /// + /// The disconnect. + /// + void Disconnect(); + } +} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Isolation/IsolatedEncodeService.cs b/win/CS/HandBrakeWPF/Isolation/IsolatedEncodeService.cs new file mode 100644 index 000000000..3c5363c28 --- /dev/null +++ b/win/CS/HandBrakeWPF/Isolation/IsolatedEncodeService.cs @@ -0,0 +1,189 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Isolated Scan Service +// This is an implementation of the IEncode implementation that runs scans on a seperate process +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Isolation +{ + using System; + using System.Threading; + + using HandBrake.ApplicationServices.EventArgs; + using HandBrake.ApplicationServices.Model; + using HandBrake.ApplicationServices.Services.Interfaces; + + using HandBrakeWPF.Isolation.Interfaces; + using HandBrakeWPF.Services.Interfaces; + + /// + /// Isolated Scan Service. + /// This is an implementation of the IEncode implementation that runs scans on a seperate process + /// + public class IsolatedEncodeService : BackgroundServiceConnector, IIsolatedEncodeService + { + #region Constructors and Destructors + + /// + /// Initializes a new instance of the class. + /// + /// + /// The error Service. + /// + public IsolatedEncodeService(IErrorService errorService) + : base(errorService) + { + try + { + if (this.CanConnect()) + { + this.Connect(); + } + } + catch (Exception exception) + { + errorService.ShowError( + "Unable to connect to scan worker process.", "Try restarting HandBrake", exception); + } + } + + #endregion + + #region Events + + /// + /// The encode completed. + /// + public event EncodeCompletedStatus EncodeCompleted; + + /// + /// The encode started. + /// + public event EventHandler EncodeStarted; + + /// + /// The encode status changed. + /// + public event EncodeProgessStatus EncodeStatusChanged; + + #endregion + + #region Properties + + /// + /// Gets ActivityLog. + /// + public string ActivityLog + { + get + { + return Service.EncodeActivityLog; + } + } + + /// + /// Gets a value indicating whether IsEncoding. + /// + public bool IsEncoding + { + get + { + return Service.IsEncoding; + } + } + + #endregion + + #region Public Methods + + /// + /// The encode completed callback. + /// + /// + /// The event args. + /// + public override void EncodeCompletedCallback(EncodeCompletedEventArgs eventArgs) + { + if (this.EncodeCompleted != null) + { + ThreadPool.QueueUserWorkItem(delegate { this.EncodeCompleted(this, eventArgs); }); + } + + base.EncodeCompletedCallback(eventArgs); + } + + /// + /// The encode progress callback. + /// + /// + /// The event args. + /// + public override void EncodeProgressCallback(EncodeProgressEventArgs eventArgs) + { + if (this.EncodeStatusChanged != null) + { + ThreadPool.QueueUserWorkItem(delegate { this.EncodeStatusChanged(this, eventArgs); }); + } + + base.EncodeProgressCallback(eventArgs); + } + + #endregion + + #region Implemented Interfaces + + #region IEncode + + /// + /// Copy the log file to the desired destinations + /// + /// + /// The destination. + /// + public void ProcessLogs(string destination) + { + ThreadPool.QueueUserWorkItem(delegate { Service.ProcessEncodeLogs(destination); }); + } + + /// + /// Attempt to Safely kill a DirectRun() CLI + /// NOTE: This will not work with a MinGW CLI + /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html + /// + public void SafelyStop() + { + ThreadPool.QueueUserWorkItem(delegate { Service.StopEncode(); }); + } + + /// + /// Start with a LibHb EncodeJob Object + /// + /// + /// The job. + /// + /// + /// The enable Logging. + /// + public void Start(QueueTask job, bool enableLogging) + { + ThreadPool.QueueUserWorkItem( + delegate { Service.StartEncode(job, enableLogging); }); + } + + /// + /// Kill the CLI process + /// + public void Stop() + { + ThreadPool.QueueUserWorkItem(delegate { Service.StopEncode(); }); + } + + #endregion + + #endregion + } +} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Isolation/IsolatedScanService.cs b/win/CS/HandBrakeWPF/Isolation/IsolatedScanService.cs index 01153d2c6..608b00c21 100644 --- a/win/CS/HandBrakeWPF/Isolation/IsolatedScanService.cs +++ b/win/CS/HandBrakeWPF/Isolation/IsolatedScanService.cs @@ -66,7 +66,6 @@ namespace HandBrakeWPF.Isolation { if (this.CanConnect()) { - this.StartServer(); this.Connect(); } } @@ -86,7 +85,7 @@ namespace HandBrakeWPF.Isolation { get { - return this.Service.ActivityLog; + return Service.ScanActivityLog; } } @@ -97,7 +96,7 @@ namespace HandBrakeWPF.Isolation { get { - return this.Service.IsScanning; + return Service.IsScanning; } } @@ -108,7 +107,7 @@ namespace HandBrakeWPF.Isolation { get { - return this.Service.SouceData; + return Service.SouceData; } } @@ -180,7 +179,7 @@ namespace HandBrakeWPF.Isolation /// public void DebugScanLog(string path) { - throw new NotImplementedException(); + throw new NotImplementedException("Not available in process isolation mode!"); } /// @@ -202,7 +201,7 @@ namespace HandBrakeWPF.Isolation public void Scan(string sourcePath, int title, int previewCount, Action postAction) { this.postScanAction = postAction; - this.Service.ScanSource(sourcePath, title, previewCount); + Service.ScanSource(sourcePath, title, previewCount); } /// @@ -210,7 +209,7 @@ namespace HandBrakeWPF.Isolation /// public void Stop() { - throw new NotImplementedException(); + Service.StopScan(); } #endregion diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index 3fc83ddac..5f65b26ce 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -47,16 +47,6 @@ namespace HandBrakeWPF.ViewModels { #region Private Variables and Services - /// - /// The Source Scan Service. - /// - private IScan scanService; - - /// - /// The Encode Service - /// - private readonly IEncode encodeService; - /// /// The Encode Service /// @@ -92,6 +82,16 @@ namespace HandBrakeWPF.ViewModels /// private readonly IUserSettingService userSettingService; + /// + /// The Source Scan Service. + /// + private IScan scanService; + + /// + /// The Encode Service + /// + private IEncode encodeService; + /// /// HandBrakes Main Window Title /// @@ -1147,18 +1147,22 @@ namespace HandBrakeWPF.ViewModels /// public void TestIsolationServices() { - // Unhook the old service + // Unhook the old services this.scanService.ScanStared -= this.ScanStared; this.scanService.ScanCompleted -= this.ScanCompleted; this.scanService.ScanStatusChanged -= this.ScanStatusChanged; + this.queueProcessor.EncodeService.EncodeStatusChanged -= this.EncodeStatusChanged; - // Replace the Service + // Replace the Services this.scanService = new IsolatedScanService(this.errorService); + this.encodeService = new IsolatedEncodeService(this.errorService); + this.queueProcessor.SwapEncodeService(this.encodeService); - // Add Event Hooks + // Add the new Event Hooks this.scanService.ScanStared += this.ScanStared; this.scanService.ScanCompleted += this.ScanCompleted; this.scanService.ScanStatusChanged += this.ScanStatusChanged; + this.queueProcessor.EncodeService.EncodeStatusChanged += this.EncodeStatusChanged; } #endregion -- cgit v1.2.3