summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.ApplicationServices
diff options
context:
space:
mode:
authorsr55 <[email protected]>2011-05-29 16:19:19 +0000
committersr55 <[email protected]>2011-05-29 16:19:19 +0000
commit6521211f3657d5f4dc424ec871d2e0458bb6b287 (patch)
tree6fd7053103fe8e4419c7ed6ebce639c61c11fb6a /win/CS/HandBrake.ApplicationServices
parent23ec3a0d4ad11c01645b6778420fb64913164d45 (diff)
WinGui: Refactored the Encode Service to move any reusable code into a base class. Added a new service which will eventually work with libhb to do encodes. Currently this just contains placeholders which throw NotImplementedExceptions.
Removed the Total Elapsed Time on the Queue window since it doesn't work. This will be re-implemented later. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4009 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/CS/HandBrake.ApplicationServices')
-rw-r--r--win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj2
-rw-r--r--win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs341
-rw-r--r--win/CS/HandBrake.ApplicationServices/Services/Encode.cs264
-rw-r--r--win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs204
4 files changed, 578 insertions, 233 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
index 94f87478c..2140890cb 100644
--- a/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
+++ b/win/CS/HandBrake.ApplicationServices/HandBrake.ApplicationServices.csproj
@@ -139,6 +139,7 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
+ <Compile Include="Services\Base\EncodeBase.cs" />
<Compile Include="Services\Encode.cs" />
<Compile Include="Services\Interfaces\IEncode.cs" />
<Compile Include="Services\Interfaces\IQueueManager.cs" />
@@ -146,6 +147,7 @@
<Compile Include="Services\Interfaces\IScan.cs" />
<Compile Include="Services\Interfaces\IPresetService.cs" />
<Compile Include="Services\Interfaces\IUserSettingService.cs" />
+ <Compile Include="Services\LibEncode.cs" />
<Compile Include="Services\LibScan.cs" />
<Compile Include="Services\PresetService.cs" />
<Compile Include="Services\QueueManager.cs" />
diff --git a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs
new file mode 100644
index 000000000..ecaafeeb2
--- /dev/null
+++ b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs
@@ -0,0 +1,341 @@
+/* EncodeBase.cs $
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr>.
+ It may be used under the terms of the GNU General Public License. */
+
+namespace HandBrake.ApplicationServices.Services.Base
+{
+ using System;
+ using System.IO;
+ using System.Text;
+ using System.Threading;
+
+ using HandBrake.ApplicationServices.EventArgs;
+ using HandBrake.ApplicationServices.Functions;
+ using HandBrake.ApplicationServices.Model;
+ using HandBrake.ApplicationServices.Services.Interfaces;
+ using HandBrake.ApplicationServices.Utilities;
+
+ /// <summary>
+ /// A Base Class for the Encode Services.
+ /// </summary>
+ public class EncodeBase
+ {
+ #region Private Variables
+
+ /// <summary>
+ /// A Lock for the filewriter
+ /// </summary>
+ private static readonly object fileWriterLock = new object();
+
+ /// <summary>
+ /// Windows 7 API Pack wrapper
+ /// </summary>
+ private Win7 windowsSeven = new Win7();
+
+ /// <summary>
+ /// The Log Buffer
+ /// </summary>
+ private StringBuilder logBuffer;
+
+ /// <summary>
+ /// The Log file writer
+ /// </summary>
+ private StreamWriter fileWriter;
+
+ /// <summary>
+ /// The Log File Header
+ /// </summary>
+ private StringBuilder header = GeneralUtilities.CreateCliLogHeader(null);
+
+ #endregion
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="EncodeBase"/> class.
+ /// </summary>
+ public EncodeBase()
+ {
+ this.logBuffer = new StringBuilder();
+ }
+
+ #region Events
+
+ /// <summary>
+ /// Fires when a new CLI QueueTask starts
+ /// </summary>
+ public event EventHandler EncodeStarted;
+
+ /// <summary>
+ /// Fires when a CLI QueueTask finishes.
+ /// </summary>
+ public event EncodeCompletedStatus EncodeCompleted;
+
+ /// <summary>
+ /// Encode process has progressed
+ /// </summary>
+ public event EncodeProgessStatus EncodeStatusChanged;
+
+ #endregion
+
+ #region Properties
+ /// <summary>
+ /// Gets or sets a value indicating whether IsEncoding.
+ /// </summary>
+ public bool IsEncoding { get; protected set; }
+
+ /// <summary>
+ /// Gets ActivityLog.
+ /// </summary>
+ public string ActivityLog
+ {
+ get
+ {
+ return string.IsNullOrEmpty(this.logBuffer.ToString()) ? this.header + "No log data available..." : this.header + this.logBuffer.ToString();
+ }
+ }
+
+ /// <summary>
+ /// Gets LogBuffer.
+ /// </summary>
+ public StringBuilder LogBuffer
+ {
+ get
+ {
+ return this.logBuffer;
+ }
+ }
+
+ /// <summary>
+ /// Gets WindowsSeven.
+ /// </summary>
+ public Win7 WindowsSeven
+ {
+ get
+ {
+ return this.windowsSeven;
+ }
+ }
+
+ #endregion
+
+ #region Invoke Events
+
+ /// <summary>
+ /// Invoke the Encode Status Changed Event.
+ /// </summary>
+ /// <param name="e">
+ /// The EncodeProgressEventArgs.
+ /// </param>
+ public void Invoke_encodeStatusChanged(EncodeProgressEventArgs e)
+ {
+ EncodeProgessStatus handler = this.EncodeStatusChanged;
+ if (handler != null)
+ {
+ handler(this, e);
+ }
+ }
+
+ /// <summary>
+ /// Invoke the Encode Completed Event
+ /// </summary>
+ /// <param name="e">
+ /// The EncodeCompletedEventArgs.
+ /// </param>
+ public void Invoke_encodeCompleted(EncodeCompletedEventArgs e)
+ {
+ EncodeCompletedStatus handler = this.EncodeCompleted;
+ if (handler != null)
+ {
+ handler(this, e);
+ }
+ }
+
+ /// <summary>
+ /// Invoke the Encode Started Event
+ /// </summary>
+ /// <param name="e">
+ /// The EventArgs.
+ /// </param>
+ public void Invoke_encodeStarted(EventArgs e)
+ {
+ EventHandler handler = this.EncodeStarted;
+ if (handler != null)
+ {
+ handler(this, e);
+ }
+ }
+
+ #endregion
+
+ #region Methods
+
+ /// <summary>
+ /// A Stop Method to be implemeneted.
+ /// </summary>
+ /// <param name="exc">
+ /// The Exception that occured that required a STOP action.
+ /// </param>
+ public virtual void Stop(Exception exc)
+ {
+ // Do Nothing
+ }
+
+ /// <summary>
+ /// Save a copy of the log to the users desired location or a default location
+ /// if this feature is enabled in options.
+ /// </summary>
+ /// <param name="destination">
+ /// The Destination File Path
+ /// </param>
+ public void ProcessLogs(string destination)
+ {
+ try
+ {
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
+ "\\HandBrake\\logs";
+ string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
+
+ string encodeDestinationPath = Path.GetDirectoryName(destination);
+ string destinationFile = Path.GetFileName(destination);
+ string encodeLogFile = destinationFile + " " +
+ DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
+
+ // Make sure the log directory exists.
+ if (!Directory.Exists(logDir))
+ {
+ Directory.CreateDirectory(logDir);
+ }
+
+ // Copy the Log to HandBrakes log folder in the users applciation data folder.
+ File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
+
+ // Save a copy of the log file in the same location as the enocde.
+ if (Properties.Settings.Default.SaveLogWithVideo)
+ {
+ File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
+ }
+
+ // Save a copy of the log file to a user specified location
+ if (Directory.Exists(Properties.Settings.Default.SaveLogCopyDirectory) &&
+ Properties.Settings.Default.SaveLogToCopyDirectory)
+ {
+ File.Copy(
+ tempLogFile, Path.Combine(Properties.Settings.Default.SaveLogCopyDirectory, encodeLogFile));
+ }
+ }
+ catch (Exception)
+ {
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)
+ }
+ }
+
+ /// <summary>
+ /// Setup the logging.
+ /// </summary>
+ /// <param name="encodeQueueTask">
+ /// The encode QueueTask.
+ /// </param>
+ protected void SetupLogging(QueueTask encodeQueueTask)
+ {
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
+ string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
+ string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.GetInstanceCount));
+
+ try
+ {
+ this.logBuffer = new StringBuilder();
+
+ // Clear the current Encode Logs
+ if (File.Exists(logFile))
+ {
+ File.Delete(logFile);
+ }
+
+ if (File.Exists(logFile2))
+ {
+ File.Delete(logFile2);
+ }
+
+ this.fileWriter = new StreamWriter(logFile) { AutoFlush = true };
+ this.fileWriter.WriteLine(GeneralUtilities.CreateCliLogHeader(encodeQueueTask));
+ }
+ catch (Exception)
+ {
+ if (this.fileWriter != null)
+ {
+ this.fileWriter.Close();
+ this.fileWriter.Dispose();
+ }
+
+ throw;
+ }
+ }
+
+ /// <summary>
+ /// Process an Incomming Log Message.
+ /// </summary>
+ /// <param name="message">
+ /// The message.
+ /// </param>
+ protected void ProcessLogMessage(string message)
+ {
+ if (!String.IsNullOrEmpty(message))
+ {
+ try
+ {
+ lock (this.LogBuffer)
+ {
+ this.LogBuffer.AppendLine(message);
+ }
+
+ lock (fileWriterLock)
+ {
+ if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite)
+ {
+ this.fileWriter.WriteLine(message);
+
+ // If the logging grows past 100MB, kill the encode and stop.
+ if (this.fileWriter.BaseStream.Length > 100000000)
+ {
+ this.Stop(
+ new Exception(
+ "The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode." +
+ "Please check the encode log for an indication of what the problem is."));
+ }
+ }
+ }
+ }
+ catch (Exception exc)
+ {
+ // Do Nothing.
+ }
+ }
+ }
+
+ /// <summary>
+ /// Shutdown and Dispose of the File Writer.
+ /// </summary>
+ protected void ShutdownFileWriter()
+ {
+ try
+ {
+ lock (fileWriterLock)
+ {
+ if (this.fileWriter != null)
+ {
+ this.fileWriter.Close();
+ this.fileWriter.Dispose();
+ }
+
+ this.fileWriter = null;
+ }
+ }
+ catch (Exception)
+ {
+ // This exception doesn't warrent user interaction, but it should be logged (TODO)
+ }
+ }
+
+ #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 468f4597a..12f1e2760 100644
--- a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs
+++ b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs
@@ -6,39 +6,26 @@
namespace HandBrake.ApplicationServices.Services
{
using System;
- using System.ComponentModel;
using System.Diagnostics;
using System.IO;
- using System.Text;
using System.Threading;
using System.Windows.Forms;
using HandBrake.ApplicationServices.EventArgs;
- using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Functions;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Parsing;
+ using HandBrake.ApplicationServices.Services.Base;
using HandBrake.ApplicationServices.Services.Interfaces;
- using HandBrake.ApplicationServices.Utilities;
/// <summary>
/// Class which handles the CLI
/// </summary>
- public class Encode : IEncode
+ public class Encode : EncodeBase, IEncode
{
#region Private Variables
/// <summary>
- /// The Log Buffer
- /// </summary>
- private StringBuilder logBuffer;
-
- /// <summary>
- /// The Log file writer
- /// </summary>
- private StreamWriter fileWriter;
-
- /// <summary>
/// Gets The Process Handle
/// </summary>
private IntPtr processHandle;
@@ -49,21 +36,6 @@ namespace HandBrake.ApplicationServices.Services
private int processId;
/// <summary>
- /// Windows 7 API Pack wrapper
- /// </summary>
- private Win7 windowsSeven = new Win7();
-
- /// <summary>
- /// A Lock for the filewriter
- /// </summary>
- static readonly object fileWriterLock = new object();
-
- /// <summary>
- /// The Log File Header
- /// </summary>
- StringBuilder header = GeneralUtilities.CreateCliLogHeader(null);
-
- /// <summary>
/// The Start time of the current Encode;
/// </summary>
private DateTime startTime;
@@ -76,53 +48,12 @@ namespace HandBrake.ApplicationServices.Services
public Encode()
{
this.EncodeStarted += this.EncodeEncodeStarted;
- this.logBuffer = new StringBuilder();
GrowlCommunicator.Register();
}
- #region Delegates and Event Handlers
-
- /// <summary>
- /// Fires when a new CLI QueueTask starts
- /// </summary>
- public event EventHandler EncodeStarted;
-
- /// <summary>
- /// Fires when a CLI QueueTask finishes.
- /// </summary>
- public event EncodeCompletedStatus EncodeCompleted;
-
- /// <summary>
- /// Encode process has progressed
- /// </summary>
- public event EncodeProgessStatus EncodeStatusChanged;
-
- /// <summary>
- /// An event for a failed encode.
- /// </summary>
- public event EventHandler EncodeFailed;
-
- #endregion
-
#region Properties
/// <summary>
- /// Gets a value indicating whether IsEncoding.
- /// </summary>
- public bool IsEncoding { get; private set; }
-
- /// <summary>
- /// Gets ActivityLog.
- /// </summary>
- public string ActivityLog
- {
- get
- {
- return string.IsNullOrEmpty(this.logBuffer.ToString()) ? header + "No log data available..." : header + this.logBuffer.ToString();
- }
- }
-
- /// <summary>
/// Gets or sets The HB Process
/// </summary>
protected Process HbProcess { get; set; }
@@ -241,17 +172,11 @@ namespace HandBrake.ApplicationServices.Services
}
// Fire the Encode Started Event
- if (this.EncodeStarted != null)
- {
- this.EncodeStarted(this, new EventArgs());
- }
+ this.Invoke_encodeStarted(EventArgs.Empty);
}
catch (Exception exc)
{
- if (this.EncodeCompleted != null)
- {
- this.EncodeCompleted(this, new EncodeCompletedEventArgs(false, exc, "An Error occured when trying to encode this source. "));
- }
+ this.Invoke_encodeCompleted(new EncodeCompletedEventArgs(false, exc, "An Error occured when trying to encode this source. "));
}
}
@@ -270,7 +195,7 @@ namespace HandBrake.ApplicationServices.Services
/// The Exception that has occured.
/// This will get bubbled up through the EncodeCompletedEventArgs
/// </param>
- public void Stop(Exception exc)
+ public override void Stop(Exception exc)
{
try
{
@@ -284,24 +209,10 @@ namespace HandBrake.ApplicationServices.Services
// No need to report anything to the user. If it fails, it's probably already stopped.
}
-
- if (exc == null)
- {
- if (this.EncodeCompleted != null)
- {
- this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
- }
- }
- else
- {
- if (this.EncodeCompleted != null)
- {
- this.EncodeCompleted(
- this,
- new EncodeCompletedEventArgs(
- false, exc, "An Unknown Error has occured when trying to Stop this encode."));
- }
- }
+ this.Invoke_encodeCompleted(
+ exc == null
+ ? new EncodeCompletedEventArgs(true, null, string.Empty)
+ : new EncodeCompletedEventArgs(false, exc, "An Unknown Error has occured when trying to Stop this encode."));
}
/// <summary>
@@ -326,47 +237,6 @@ namespace HandBrake.ApplicationServices.Services
//}*/
}
- /// <summary>
- /// Save a copy of the log to the users desired location or a default location
- /// if this feature is enabled in options.
- /// </summary>
- /// <param name="destination">
- /// The Destination File Path
- /// </param>
- public void ProcessLogs(string destination)
- {
- try
- {
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\logs";
- string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
-
- string encodeDestinationPath = Path.GetDirectoryName(destination);
- string destinationFile = Path.GetFileName(destination);
- string encodeLogFile = destinationFile + " " +
- DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
-
- // Make sure the log directory exists.
- if (!Directory.Exists(logDir))
- Directory.CreateDirectory(logDir);
-
- // Copy the Log to HandBrakes log folder in the users applciation data folder.
- File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
-
- // Save a copy of the log file in the same location as the enocde.
- if (Properties.Settings.Default.SaveLogWithVideo)
- File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
-
- // Save a copy of the log file to a user specified location
- if (Directory.Exists(Properties.Settings.Default.SaveLogCopyDirectory) && Properties.Settings.Default.SaveLogToCopyDirectory)
- File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.SaveLogCopyDirectory, encodeLogFile));
- }
- catch (Exception exc)
- {
- // This exception doesn't warrent user interaction, but it should be logged (TODO)
- }
- }
-
#endregion
#region Private Helper Methods
@@ -382,10 +252,10 @@ namespace HandBrake.ApplicationServices.Services
/// </param>
private void HbProcessExited(object sender, EventArgs e)
{
- IsEncoding = false;
- if (windowsSeven.IsWindowsSeven)
+ this.IsEncoding = false;
+ if (this.WindowsSeven.IsWindowsSeven)
{
- windowsSeven.SetTaskBarProgressToNoProgress();
+ this.WindowsSeven.SetTaskBarProgressToNoProgress();
}
if (Properties.Settings.Default.PreventSleep)
@@ -395,66 +265,20 @@ namespace HandBrake.ApplicationServices.Services
try
{
- lock (fileWriterLock)
- {
- // This is just a quick hack to ensure that we are done processing the logging data.
- // Logging data comes in after the exit event has processed sometimes. We should really impliment ISyncronizingInvoke
- // and set the SyncObject on the process. I think this may resolve this properly.
- // For now, just wait 2.5 seconds to let any trailing log messages come in and be processed.
- Thread.Sleep(2500);
-
- this.HbProcess.CancelErrorRead();
-
- if (fileWriter != null)
- {
- fileWriter.Close();
- fileWriter.Dispose();
- }
-
- fileWriter = null;
- }
+ // This is just a quick hack to ensure that we are done processing the logging data.
+ // Logging data comes in after the exit event has processed sometimes. We should really impliment ISyncronizingInvoke
+ // and set the SyncObject on the process. I think this may resolve this properly.
+ // For now, just wait 2.5 seconds to let any trailing log messages come in and be processed.
+ Thread.Sleep(2500);
+ this.HbProcess.CancelErrorRead();
+ this.ShutdownFileWriter();
}
catch (Exception exc)
{
// This exception doesn't warrent user interaction, but it should be logged (TODO)
}
- if (this.EncodeCompleted != null)
- this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
- }
-
- /// <summary>
- /// Setup the logging.
- /// </summary>
- /// <param name="encodeQueueTask">
- /// The encode QueueTask.
- /// </param>
- private void SetupLogging(QueueTask encodeQueueTask)
- {
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
- string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount));
- string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.GetInstanceCount));
-
- try
- {
- logBuffer = new StringBuilder();
-
- // Clear the current Encode Logs
- if (File.Exists(logFile)) File.Delete(logFile);
- if (File.Exists(logFile2)) File.Delete(logFile2);
-
- fileWriter = new StreamWriter(logFile) { AutoFlush = true };
- fileWriter.WriteLine(GeneralUtilities.CreateCliLogHeader(encodeQueueTask));
- }
- catch (Exception)
- {
- if (fileWriter != null)
- {
- fileWriter.Close();
- fileWriter.Dispose();
- }
- throw;
- }
+ this.Invoke_encodeCompleted(new EncodeCompletedEventArgs(true, null, string.Empty));
}
/// <summary>
@@ -470,32 +294,7 @@ namespace HandBrake.ApplicationServices.Services
{
if (!String.IsNullOrEmpty(e.Data))
{
- try
- {
- lock (logBuffer)
- logBuffer.AppendLine(e.Data);
-
- lock (fileWriterLock)
- {
- if (fileWriter != null && fileWriter.BaseStream.CanWrite)
- {
- fileWriter.WriteLine(e.Data);
-
- // If the logging grows past 100MB, kill the encode and stop.
- if (fileWriter.BaseStream.Length > 100000000)
- {
- this.Stop(
- new Exception(
- "The encode has been stopped. The log file has grown to over 100MB which indicates a serious problem has occured with the encode." +
- "Please check the encode log for an indication of what the problem is."));
- }
- }
- }
- }
- catch (Exception exc)
- {
- // Do Nothing.
- }
+ this.ProcessLogMessage(e.Data);
}
}
@@ -510,7 +309,7 @@ namespace HandBrake.ApplicationServices.Services
/// </param>
private void EncodeEncodeStarted(object sender, EventArgs e)
{
- Thread monitor = new Thread(EncodeMonitor);
+ Thread monitor = new Thread(this.EncodeMonitor);
monitor.Start();
}
@@ -521,14 +320,16 @@ namespace HandBrake.ApplicationServices.Services
{
try
{
- Parser encode = new Parser(HbProcess.StandardOutput.BaseStream);
- encode.OnEncodeProgress += EncodeOnEncodeProgress;
+ Parser encode = new Parser(this.HbProcess.StandardOutput.BaseStream);
+ encode.OnEncodeProgress += this.EncodeOnEncodeProgress;
while (!encode.EndOfStream)
+ {
encode.ReadEncodeStatus();
+ }
}
- catch (Exception exc)
+ catch (Exception)
{
- EncodeOnEncodeProgress(null, 0, 0, 0, 0, 0, "Unknown, status not available..");
+ this.EncodeOnEncodeProgress(null, 0, 0, 0, 0, 0, "Unknown, status not available..");
}
}
@@ -555,17 +356,14 @@ namespace HandBrake.ApplicationServices.Services
ElapsedTime = DateTime.Now - this.startTime,
};
- if (this.EncodeStatusChanged != null)
- {
- this.EncodeStatusChanged(this, eventArgs);
- }
+ this.Invoke_encodeStatusChanged(eventArgs);
- if (this.windowsSeven.IsWindowsSeven)
+ if (this.WindowsSeven.IsWindowsSeven)
{
int percent;
int.TryParse(Math.Round(percentComplete).ToString(), out percent);
- this.windowsSeven.SetTaskBarProgress(percent);
+ this.WindowsSeven.SetTaskBarProgress(percent);
}
}
diff --git a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs
new file mode 100644
index 000000000..0cacea2ac
--- /dev/null
+++ b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs
@@ -0,0 +1,204 @@
+/* LibEncode.cs $
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr>.
+ It may be used under the terms of the GNU General Public License. */
+
+namespace HandBrake.ApplicationServices.Services
+{
+ using System;
+ using System.Text;
+
+ using HandBrake.ApplicationServices.Functions;
+ using HandBrake.ApplicationServices.Model;
+ using HandBrake.ApplicationServices.Services.Base;
+ using HandBrake.ApplicationServices.Services.Interfaces;
+ using HandBrake.Interop;
+
+ using EncodeCompletedEventArgs = HandBrake.ApplicationServices.EventArgs.EncodeCompletedEventArgs;
+ using EncodeProgressEventArgs = HandBrake.ApplicationServices.EventArgs.EncodeProgressEventArgs;
+
+ /// <summary>
+ /// TODO: Update summary.
+ /// </summary>
+ public class LibEncode : EncodeBase, IEncode
+ {
+ #region Private Variables
+
+ /// <summary>
+ /// Lock for the log file
+ /// </summary>
+ private static readonly object logLock = new object();
+
+ /// <summary>
+ /// The Start time of the current Encode;
+ /// </summary>
+ private DateTime startTime;
+
+ /// <summary>
+ /// An Instance of the HandBrake Interop Library
+ /// </summary>
+ private HandBrakeInstance instance;
+
+ #endregion
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="LibEncode"/> class.
+ /// </summary>
+ public LibEncode()
+ {
+ // Setup the HandBrake Instance
+ this.instance = new HandBrakeInstance();
+ this.instance.EncodeCompleted += this.InstanceEncodeCompleted;
+ this.instance.EncodeProgress += this.InstanceEncodeProgress;
+
+ HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged;
+ HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged;
+
+ GrowlCommunicator.Register();
+ }
+
+ /// <summary>
+ /// Start with a LibHb EncodeJob Object
+ /// </summary>
+ /// <param name="job">
+ /// The job.
+ /// </param>
+ /// <param name="enableLogging">
+ /// The enable Logging.
+ /// </param>
+ public void Start(QueueTask job, bool enableLogging)
+ {
+ throw new NotImplementedException("This will be implemented later.");
+ this.startTime = DateTime.Now;
+ }
+
+ /// <summary>
+ /// Kill the CLI process
+ /// </summary>
+ public void Stop()
+ {
+ this.Stop(null);
+ }
+
+ /// <summary>
+ /// Kill the CLI process
+ /// </summary>
+ /// <param name="exc">
+ /// The Exception that has occured.
+ /// This will get bubbled up through the EncodeCompletedEventArgs
+ /// </param>
+ public override void Stop(Exception exc)
+ {
+ this.instance.StopEncode();
+
+ this.Invoke_encodeCompleted(
+ exc == null
+ ? new EncodeCompletedEventArgs(true, null, string.Empty)
+ : new EncodeCompletedEventArgs(false, exc, "An Error has occured."));
+ }
+
+ /// <summary>
+ /// 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
+ /// </summary>
+ public void SafelyStop()
+ {
+ throw new NotImplementedException("This Method is not used in the LibEncode service. You should use the Stop() method instead! ");
+ }
+
+ #region HandBrakeInstance Event Handlers.
+ /// <summary>
+ /// Log a message
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The MessageLoggedEventArgs.
+ /// </param>
+ private void HandBrakeInstanceErrorLogged(object sender, MessageLoggedEventArgs e)
+ {
+ lock (logLock)
+ {
+ this.LogBuffer.AppendLine(e.Message);
+ }
+ }
+
+ /// <summary>
+ /// Log a message
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The MessageLoggedEventArgs.
+ /// </param>
+ private void HandBrakeInstanceMessageLogged(object sender, MessageLoggedEventArgs e)
+ {
+ lock (logLock)
+ {
+ this.LogBuffer.AppendLine(e.Message);
+ }
+ }
+
+ /// <summary>
+ /// Encode Progress Event Handler
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The Interop.EncodeProgressEventArgs.
+ /// </param>
+ private void InstanceEncodeProgress(object sender, Interop.EncodeProgressEventArgs e)
+ {
+ EncodeProgressEventArgs args = new EncodeProgressEventArgs
+ {
+ AverageFrameRate = e.AverageFrameRate,
+ CurrentFrameRate = e.CurrentFrameRate,
+ EstimatedTimeLeft = e.EstimatedTimeLeft,
+ PercentComplete = e.FractionComplete,
+ Task = e.Pass,
+ ElapsedTime = DateTime.Now - this.startTime,
+ };
+
+ this.Invoke_encodeStatusChanged(args);
+
+ if (this.WindowsSeven.IsWindowsSeven)
+ {
+ int percent;
+ int.TryParse(Math.Round(e.FractionComplete).ToString(), out percent);
+
+ this.WindowsSeven.SetTaskBarProgress(percent);
+ }
+ }
+
+ /// <summary>
+ /// Encode Completed Event Handler
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The e.
+ /// </param>
+ private void InstanceEncodeCompleted(object sender, Interop.EncodeCompletedEventArgs e)
+ {
+ this.IsEncoding = false;
+
+ this.Invoke_encodeCompleted(new EncodeCompletedEventArgs(e.Error, null, string.Empty));
+
+ if (this.WindowsSeven.IsWindowsSeven)
+ {
+ this.WindowsSeven.SetTaskBarProgressToNoProgress();
+ }
+
+ if (Properties.Settings.Default.PreventSleep)
+ {
+ Win32.AllowSleep();
+ }
+ }
+ #endregion
+ }
+}