summaryrefslogtreecommitdiffstats
path: root/win/C#/EncodeQueue
diff options
context:
space:
mode:
Diffstat (limited to 'win/C#/EncodeQueue')
-rw-r--r--win/C#/EncodeQueue/Encode.cs5
-rw-r--r--win/C#/EncodeQueue/Job.cs18
-rw-r--r--win/C#/EncodeQueue/QueueHandler.cs277
3 files changed, 159 insertions, 141 deletions
diff --git a/win/C#/EncodeQueue/Encode.cs b/win/C#/EncodeQueue/Encode.cs
index 248624064..83ebc986b 100644
--- a/win/C#/EncodeQueue/Encode.cs
+++ b/win/C#/EncodeQueue/Encode.cs
@@ -38,7 +38,6 @@ namespace Handbrake.EncodeQueue
{
cliStart.RedirectStandardOutput = true;
cliStart.UseShellExecute = false;
-
}
if (Properties.Settings.Default.cli_minimized)
cliStart.WindowStyle = ProcessWindowStyle.Minimized;
@@ -127,7 +126,7 @@ namespace Handbrake.EncodeQueue
}
}
- /// <summary>
+ /// <summar>
/// Append the CLI query to the start of the log file.
/// </summary>
/// <param name="query"></param>
@@ -136,7 +135,7 @@ namespace Handbrake.EncodeQueue
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string logPath = Path.Combine(logDir, "last_encode_log.txt");
- StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read));
+ StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
String log = reader.ReadToEnd();
reader.Close();
diff --git a/win/C#/EncodeQueue/Job.cs b/win/C#/EncodeQueue/Job.cs
index 291ef400d..e912fd65a 100644
--- a/win/C#/EncodeQueue/Job.cs
+++ b/win/C#/EncodeQueue/Job.cs
@@ -6,26 +6,34 @@
namespace Handbrake.EncodeQueue
{
- public class Job
+ public struct Job
{
/// <summary>
- /// Get or Set the job id.
+ /// Gets or sets the job ID.
/// </summary>
public int Id { get; set; }
/// <summary>
- /// Get or Set the query string.
+ /// Gets or sets the query string.
/// </summary>
public string Query { get; set; }
/// <summary>
- /// Get or set the source file of encoding
+ /// Gets or sets the source file of encoding.
/// </summary>
public string Source { get; set; }
/// <summary>
- /// Get or set the destination for the file to be encoded.
+ /// Gets or sets the destination for the file to be encoded.
/// </summary>
public string Destination { get; set; }
+
+ /// <summary>
+ /// Gets whether or not this instance is empty.
+ /// </summary>
+ public bool IsEmpty
+ {
+ get { return Id == 0 && string.IsNullOrEmpty(Query) && string.IsNullOrEmpty(Source) && string.IsNullOrEmpty(Destination); }
+ }
}
} \ No newline at end of file
diff --git a/win/C#/EncodeQueue/QueueHandler.cs b/win/C#/EncodeQueue/QueueHandler.cs
index 6670e9e89..d183af0c5 100644
--- a/win/C#/EncodeQueue/QueueHandler.cs
+++ b/win/C#/EncodeQueue/QueueHandler.cs
@@ -6,87 +6,112 @@
using System;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.IO;
+using System.Threading;
using System.Windows.Forms;
using System.Xml.Serialization;
-using System.Threading;
namespace Handbrake.EncodeQueue
{
+ /// <summary>
+ /// Provides a handler for encoding jobs and a queue of those jobs.
+ /// </summary>
public class QueueHandler
{
public Encode encodeHandler = new Encode();
- private static XmlSerializer ser = new XmlSerializer(typeof(List<Job>));
- List<Job> queue = new List<Job>();
- int id; // Unique identifer number for each job
+ private static XmlSerializer serializer = new XmlSerializer(typeof(List<Job>));
+ private List<Job> queue = new List<Job>();
+ private int nextJobId;
- #region Queue Handling
- public List<Job> getQueue()
+ /// <summary>
+ /// Gets the number of items in the queue.
+ /// </summary>
+ public int Count
{
- return queue;
+ get { return queue.Count; }
}
/// <summary>
- /// Get's the next CLI query for encoding
+ /// Gets the last encode that was processed.
+ /// </summary>
+ /// <returns></returns>
+ public Job LastEncode { get; set; }
+
+ /// <summary>
+ /// Gets the current state of the encode queue.
/// </summary>
- /// <returns>String</returns>
- private Job getNextJobForEncoding()
+ public ReadOnlyCollection<Job> CurrentQueue
{
- Job job = queue[0];
- lastQueueItem = job;
- remove(0); // Remove the item which we are about to pass out.
+ get { return queue.AsReadOnly(); }
+ }
- updateQueueRecoveryFile("hb_queue_recovery.xml");
+ /// <summary>
+ /// Fires when an encode job has been started.
+ /// </summary>
+ public event EventHandler NewJobStarted;
- return job;
- }
+ /// <summary>
+ /// Fires when a pause to the encode queue has been requested.
+ /// </summary>
+ public event EventHandler QueuePauseRequested;
/// <summary>
- /// Get the last query that was returned by getNextItemForEncoding()
+ /// Fires when an encode job has been completed.
/// </summary>
- /// <returns></returns>
- public Job lastQueueItem { get; set; }
+ public event EventHandler CurrentJobCompleted;
/// <summary>
- /// Add's a new item to the queue
+ /// Fires when the entire encode queue has completed.
/// </summary>
- /// <param name="query">String</param>
- /// <param name="source"></param>
- /// <param name="destination"></param>
- public void add(string query, string source, string destination)
+ public event EventHandler QueueCompleted;
+
+ #region Queue Handling
+
+ /// <summary>
+ /// Gets and removes the next job in the queue.
+ /// </summary>
+ /// <returns>The job that was removed from the queue.</returns>
+ private Job GetNextJob()
{
- Job newJob = new Job { Id = id, Query = query, Source = source, Destination = destination };
- id++;
+ Job job = queue[0];
+ LastEncode = job;
+ RemoveJob(0); // Remove the item which we are about to pass out.
- queue.Add(newJob);
- updateQueueRecoveryFile("hb_queue_recovery.xml");
+ WriteQueueStateToFile("hb_queue_recovery.xml");
+
+ return job;
}
/// <summary>
- /// Removes an item from the queue.
+ /// Adds an item to the queue.
/// </summary>
- /// <param name="index">Index</param>
- /// <returns>Bolean true if successful</returns>
- public void remove(int index)
+ /// <param name="query">The query that will be passed to the HandBrake CLI.</param>
+ /// <param name="source">The location of the source video.</param>
+ /// <param name="destination">The location where the encoded video will be.</param>
+ public void AddJob(string query, string source, string destination)
{
- queue.RemoveAt(index);
- updateQueueRecoveryFile("hb_queue_recovery.xml");
+ Job newJob = new Job { Id = nextJobId++, Query = query, Source = source, Destination = destination };
+
+ queue.Add(newJob);
+ WriteQueueStateToFile("hb_queue_recovery.xml");
}
/// <summary>
- /// Returns how many items are in the queue
+ /// Removes an item from the queue.
/// </summary>
- /// <returns>Int</returns>
- public int count()
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ public void RemoveJob(int index)
{
- return queue.Count;
+ queue.RemoveAt(index);
+ WriteQueueStateToFile("hb_queue_recovery.xml");
}
/// <summary>
- /// Move an item with an index x, up in the queue
+ /// Moves an item up one position in the queue.
/// </summary>
- /// <param name="index">Int</param>
- public void moveUp(int index)
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ public void MoveUp(int index)
{
if (index > 0)
{
@@ -95,14 +120,15 @@ namespace Handbrake.EncodeQueue
queue.RemoveAt(index);
queue.Insert((index - 1), item);
}
- updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
+
+ WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
}
/// <summary>
- /// Move an item with an index x, down in the queue
+ /// Moves an item down one position in the queue.
/// </summary>
- /// <param name="index">Int</param>
- public void moveDown(int index)
+ /// <param name="index">The zero-based location of the job in the queue.</param>
+ public void MoveDown(int index)
{
if (index < queue.Count - 1)
{
@@ -111,14 +137,15 @@ namespace Handbrake.EncodeQueue
queue.RemoveAt(index);
queue.Insert((index + 1), item);
}
- updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
+
+ WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
}
/// <summary>
- /// Writes the current queue to disk. hb_queue_recovery.xml
- /// This function is called after getNextItemForEncoding()
+ /// Writes the current state of the queue to a file.
/// </summary>
- public void updateQueueRecoveryFile(string file)
+ /// <param name="file">The location of the file to write the queue to.</param>
+ public void WriteQueueStateToFile(string file)
{
string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
@@ -126,7 +153,7 @@ namespace Handbrake.EncodeQueue
{
using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
{
- ser.Serialize(strm, queue);
+ serializer.Serialize(strm, queue);
strm.Close();
strm.Dispose();
}
@@ -139,10 +166,10 @@ namespace Handbrake.EncodeQueue
}
/// <summary>
- /// Writes the current queue to disk to the location specified in file
+ /// Writes the current state of the queue in the form of a batch (.bat) file.
/// </summary>
- /// <param name="file"></param>
- public void writeBatchScript(string file)
+ /// <param name="file">The location of the file to write the batch file to.</param>
+ public void WriteBatchScriptToFile(string file)
{
string queries = "";
foreach (Job queue_item in queue)
@@ -163,9 +190,10 @@ namespace Handbrake.EncodeQueue
{
// Create a StreamWriter and open the file, Write the batch file query to the file and
// Close the stream
- StreamWriter line = new StreamWriter(file);
- line.WriteLine(strCmdLine);
- line.Close();
+ using (StreamWriter line = new StreamWriter(file))
+ {
+ line.WriteLine(strCmdLine);
+ }
MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
@@ -178,9 +206,10 @@ namespace Handbrake.EncodeQueue
}
/// <summary>
- /// Recover the queue from hb_queue_recovery.xml
+ /// Reads a serialized XML file that represents a queue of encoding jobs.
/// </summary>
- public void recoverQueue(string file)
+ /// <param name="file">The location of the file to read the queue from.</param>
+ public void LoadQueueFromFile(string file)
{
string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
@@ -190,50 +219,55 @@ namespace Handbrake.EncodeQueue
{
if (strm.Length != 0)
{
- List<Job> list = ser.Deserialize(strm) as List<Job>;
+ List<Job> list = serializer.Deserialize(strm) as List<Job>;
if (list != null)
foreach (Job item in list)
queue.Add(item);
if (file != "hb_queue_recovery.xml")
- updateQueueRecoveryFile("hb_queue_recovery.xml");
+ WriteQueueStateToFile("hb_queue_recovery.xml");
}
}
}
}
/// <summary>
- /// Check to see if a destination path is already on the queue
+ /// Checks the current queue for an existing instance of the specified destination.
/// </summary>
- /// <param name="destination">Destination path</param>
- /// <returns>Boolean True/False. True = Path Exists</returns>
- public Boolean checkDestinationPath(string destination)
+ /// <param name="destination">The destination of the encode.</param>
+ /// <returns>Whether or not the supplied destination is already in the queue.</returns>
+ public bool CheckForDestinationDuplicate(string destination)
{
foreach (Job checkItem in queue)
{
if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
return true;
}
+
return false;
}
+
#endregion
#region Encoding
- public Boolean isEncodeStarted { get; private set; }
- public Boolean isPaused { get; private set; }
- public Boolean isEncoding { get; private set; }
+ public bool PauseRequested { get; private set; }
+ public bool IsEncoding { get; private set; }
- public void startEncode()
+ /// <summary>
+ /// Starts encoding the first job in the queue and continues encoding until all jobs
+ /// have been encoded.
+ /// </summary>
+ public void StartEncodeQueue()
{
- if (this.count() != 0)
+ if (this.Count != 0)
{
- if (isPaused)
- isPaused = false;
+ if (PauseRequested)
+ PauseRequested = false;
else
{
- isPaused = false;
+ PauseRequested = false;
try
{
Thread theQueue = new Thread(startProcess) { IsBackground = true };
@@ -246,86 +280,63 @@ namespace Handbrake.EncodeQueue
}
}
}
- public void pauseEncodeQueue()
+
+ /// <summary>
+ /// Requests a pause of the encode queue.
+ /// </summary>
+ public void RequestPause()
{
- isPaused = true;
- EncodePaused(null);
+ PauseRequested = true;
+
+ if (QueuePauseRequested != null)
+ QueuePauseRequested(this, new EventArgs());
}
- public void endEncode()
+
+ /// <summary>
+ /// Stops the current job.
+ /// </summary>
+ public void EndEncodeJob()
{
encodeHandler.closeCLI();
}
private void startProcess(object state)
{
- try
+ // Run through each item on the queue
+ while (this.Count != 0)
{
- // Run through each item on the queue
- while (this.count() != 0)
- {
- string query = getNextJobForEncoding().Query;
- updateQueueRecoveryFile("hb_queue_recovery.xml"); // Update the queue recovery file
+ string query = GetNextJob().Query;
+ WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
- encodeHandler.runCli(query);
- EncodeStarted(null);
- encodeHandler.hbProcess.WaitForExit();
+ encodeHandler.runCli(query);
- encodeHandler.addCLIQueryToLog(query);
- encodeHandler.copyLog(lastQueueItem.Destination);
+ if (NewJobStarted != null)
+ NewJobStarted(this, new EventArgs());
- encodeHandler.hbProcess.Close();
- encodeHandler.hbProcess.Dispose();
- EncodeFinished(null);
+ encodeHandler.hbProcess.WaitForExit();
- while (isPaused) // Need to find a better way of doing this.
- {
- Thread.Sleep(5000);
- }
- }
- EncodeQueueFinished(null);
+ encodeHandler.addCLIQueryToLog(query);
+ encodeHandler.copyLog(LastEncode.Destination);
- // After the encode is done, we may want to shutdown, suspend etc.
- encodeHandler.afterEncodeAction();
- }
- catch (Exception exc)
- {
- throw new Exception(exc.ToString());
- }
- }
- #endregion
+ encodeHandler.hbProcess.Close();
+ encodeHandler.hbProcess.Dispose();
- #region Events
- public event EventHandler OnEncodeStart;
- public event EventHandler OnPaused;
- public event EventHandler OnEncodeEnded;
- public event EventHandler OnQueueFinished;
+ if (CurrentJobCompleted != null)
+ CurrentJobCompleted(this, new EventArgs());
- // Invoke the Changed event; called whenever encodestatus changes:
- protected virtual void EncodeStarted(EventArgs e)
- {
- if (OnEncodeStart != null)
- OnEncodeStart(this, e);
+ while (PauseRequested) // Need to find a better way of doing this.
+ {
+ Thread.Sleep(5000);
+ }
+ }
- isEncoding = true;
- }
- protected virtual void EncodePaused(EventArgs e)
- {
- if (OnPaused != null)
- OnPaused(this, e);
- }
- protected virtual void EncodeFinished(EventArgs e)
- {
- if (OnEncodeEnded != null)
- OnEncodeEnded(this, e);
+ if (QueueCompleted != null)
+ QueueCompleted(this, new EventArgs());
- isEncoding = false;
+ // After the encode is done, we may want to shutdown, suspend etc.
+ encodeHandler.afterEncodeAction();
}
- protected virtual void EncodeQueueFinished(EventArgs e)
- {
- if (OnQueueFinished != null)
- OnQueueFinished(this, e);
- }
- #endregion
+ #endregion
}
} \ No newline at end of file