1 /* EncodeAndQueueHandler.cs $
\r
3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr/>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Collections.Generic;
\r
9 using System.Collections.ObjectModel;
\r
10 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
14 using System.Xml.Serialization;
\r
15 using Handbrake.Functions;
\r
17 namespace Handbrake.EncodeQueue
\r
19 public class EncodeAndQueueHandler
\r
21 private static XmlSerializer serializer = new XmlSerializer(typeof(List<Job>));
\r
22 private List<Job> queue = new List<Job>();
\r
23 private int nextJobId;
\r
25 #region Event Handlers
\r
27 /// Fires when an encode job has been started.
\r
29 public event EventHandler NewJobStarted;
\r
32 /// Fires when a pause to the encode queue has been requested.
\r
34 public event EventHandler QueuePauseRequested;
\r
37 /// Fires when an encode job has been completed.
\r
39 public event EventHandler CurrentJobCompleted;
\r
42 /// Fires when the entire encode queue has completed.
\r
44 public event EventHandler QueueCompleted;
\r
49 /// Gets and removes the next job in the queue.
\r
51 /// <returns>The job that was removed from the queue.</returns>
\r
52 private Job GetNextJob()
\r
56 RemoveJob(0); // Remove the item which we are about to pass out.
\r
58 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
64 /// Gets the current state of the encode queue.
\r
66 public ReadOnlyCollection<Job> CurrentQueue
\r
68 get { return queue.AsReadOnly(); }
\r
72 /// Gets the number of items in the queue.
\r
76 get { return queue.Count; }
\r
81 /// Adds an item to the queue.
\r
83 /// <param name="query">The query that will be passed to the HandBrake CLI.</param>
\r
84 /// <param name="source">The location of the source video.</param>
\r
85 /// <param name="destination">The location where the encoded video will be.</param>
\r
86 public void AddJob(string query, string source, string destination)
\r
88 Job newJob = new Job { Id = nextJobId++, Query = query, Source = source, Destination = destination };
\r
91 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
95 /// Removes an item from the queue.
\r
97 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
98 public void RemoveJob(int index)
\r
100 queue.RemoveAt(index);
\r
101 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
105 /// Moves an item up one position in the queue.
\r
107 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
108 public void MoveUp(int index)
\r
112 Job item = queue[index];
\r
114 queue.RemoveAt(index);
\r
115 queue.Insert((index - 1), item);
\r
118 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
122 /// Moves an item down one position in the queue.
\r
124 /// <param name="index">The zero-based location of the job in the queue.</param>
\r
125 public void MoveDown(int index)
\r
127 if (index < queue.Count - 1)
\r
129 Job item = queue[index];
\r
131 queue.RemoveAt(index);
\r
132 queue.Insert((index + 1), item);
\r
135 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
139 /// Writes the current state of the queue to a file.
\r
141 /// <param name="file">The location of the file to write the queue to.</param>
\r
142 public void WriteQueueStateToFile(string file)
\r
144 string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
\r
148 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
\r
150 serializer.Serialize(strm, queue);
\r
157 // Any Errors will be out of diskspace/permissions problems.
\r
158 // Don't report them as they'll annoy the user.
\r
163 /// Writes the current state of the queue in the form of a batch (.bat) file.
\r
165 /// <param name="file">The location of the file to write the batch file to.</param>
\r
166 public void WriteBatchScriptToFile(string file)
\r
168 string queries = "";
\r
169 foreach (Job queue_item in queue)
\r
171 string q_item = queue_item.Query;
\r
172 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;
\r
174 if (queries == string.Empty)
\r
175 queries = queries + fullQuery;
\r
177 queries = queries + " && " + fullQuery;
\r
179 string strCmdLine = queries;
\r
185 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
186 // Close the stream
\r
187 using (StreamWriter line = new StreamWriter(file))
\r
189 line.WriteLine(strCmdLine);
\r
192 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
196 MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
203 /// Reads a serialized XML file that represents a queue of encoding jobs.
\r
205 /// <param name="file">The location of the file to read the queue from.</param>
\r
206 public void LoadQueueFromFile(string file)
\r
208 string tempPath = file == "hb_queue_recovery.xml" ? Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml") : file;
\r
210 if (File.Exists(tempPath))
\r
212 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
214 if (strm.Length != 0)
\r
216 List<Job> list = serializer.Deserialize(strm) as List<Job>;
\r
219 foreach (Job item in list)
\r
222 if (file != "hb_queue_recovery.xml")
\r
223 WriteQueueStateToFile("hb_queue_recovery.xml");
\r
230 /// Checks the current queue for an existing instance of the specified destination.
\r
232 /// <param name="destination">The destination of the encode.</param>
\r
233 /// <returns>Whether or not the supplied destination is already in the queue.</returns>
\r
234 public bool CheckForDestinationDuplicate(string destination)
\r
236 foreach (Job checkItem in queue)
\r
238 if (checkItem.Destination.Contains(destination.Replace("\\\\", "\\")))
\r
250 /// Gets the last encode that was processed.
\r
252 /// <returns></returns>
\r
253 public Job LastEncode { get; set; }
\r
258 public Boolean PauseRequested { get; private set; }
\r
261 /// Starts encoding the first job in the queue and continues encoding until all jobs
\r
262 /// have been encoded.
\r
264 public void StartEncodeQueue()
\r
266 if (this.Count != 0)
\r
268 if (PauseRequested)
\r
269 PauseRequested = false;
\r
272 PauseRequested = false;
\r
275 Thread theQueue = new Thread(startProcess) { IsBackground = true };
\r
278 catch (Exception exc)
\r
280 MessageBox.Show(exc.ToString());
\r
287 /// Requests a pause of the encode queue.
\r
289 public void RequestPause()
\r
291 PauseRequested = true;
\r
293 if (QueuePauseRequested != null)
\r
294 QueuePauseRequested(this, new EventArgs());
\r
298 /// Stops the current job.
\r
300 public void EndEncodeJob()
\r
305 private void startProcess(object state)
\r
307 // Run through each item on the queue
\r
308 while (this.Count != 0)
\r
310 string query = GetNextJob().Query;
\r
311 WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file
\r
315 if (NewJobStarted != null)
\r
316 NewJobStarted(this, new EventArgs());
\r
318 hbProcess.WaitForExit();
\r
320 addCLIQueryToLog(query);
\r
321 copyLog(LastEncode.Destination);
\r
324 hbProcess.Dispose();
\r
326 isEncoding = false;
\r
328 if (CurrentJobCompleted != null)
\r
329 CurrentJobCompleted(this, new EventArgs());
\r
331 while (PauseRequested) // Need to find a better way of doing this.
\r
333 Thread.Sleep(5000);
\r
337 if (QueueCompleted != null)
\r
338 QueueCompleted(this, new EventArgs());
\r
340 // After the encode is done, we may want to shutdown, suspend etc.
\r
341 afterEncodeAction();
\r
346 #region CLI and Log Handling
\r
347 public Process hbProcess { get; set; }
\r
348 public int processID { get; set; }
\r
349 public IntPtr processHandle { get; set; }
\r
350 public String currentQuery { get; set; }
\r
351 public Boolean isEncoding { get; set; }
\r
354 /// Execute a HandBrakeCLI process.
\r
356 /// <param name="query">The CLI Query</param>
\r
357 public void runCli(string query)
\r
361 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
362 string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");
\r
363 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);
\r
364 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
366 if (Properties.Settings.Default.enocdeStatusInGui)
\r
368 cliStart.RedirectStandardOutput = true;
\r
369 cliStart.UseShellExecute = false;
\r
371 if (Properties.Settings.Default.cli_minimized)
\r
372 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\r
374 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
\r
375 hbProcess = Process.Start(cliStart);
\r
376 processID = Main.getCliProcess(before);
\r
378 currentQuery = query;
\r
379 if (hbProcess != null)
\r
380 processHandle = hbProcess.MainWindowHandle; // Set the process Handle
\r
382 // Set the process Priority
\r
383 Process hbCliProcess = null;
\r
384 if (processID != -1)
\r
385 hbCliProcess = Process.GetProcessById(processID);
\r
387 if (hbCliProcess != null)
\r
388 switch (Properties.Settings.Default.processPriority)
\r
391 hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
394 hbCliProcess.PriorityClass = ProcessPriorityClass.High;
\r
396 case "Above Normal":
\r
397 hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
400 hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
403 hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
406 hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
410 catch (Exception exc)
\r
412 MessageBox.Show("It would appear that HandBrakeCLI has not started correctly. You should take a look at the Activity log as it may indicate the reason why.\n\n Detailed Error Information: error occured in runCli()\n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
418 /// Kill the CLI process
\r
420 public void closeCLI()
\r
423 isEncoding = false;
\r
427 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
429 public void afterEncodeAction()
\r
431 isEncoding = false;
\r
432 currentQuery = String.Empty;
\r
433 // Do something whent he encode ends.
\r
434 switch (Properties.Settings.Default.CompletionOption)
\r
437 Process.Start("Shutdown", "-s -t 60");
\r
440 Win32.ExitWindowsEx(0, 0);
\r
443 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
446 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
448 case "Lock System":
\r
449 Win32.LockWorkStation();
\r
451 case "Quit HandBrake":
\r
452 Application.Exit();
\r
460 /// Append the CLI query to the start of the log file.
\r
462 /// <param name="query"></param>
\r
463 public void addCLIQueryToLog(string query)
\r
465 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
466 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
468 StreamReader reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
\r
469 String log = reader.ReadToEnd();
\r
472 StreamWriter writer = new StreamWriter(File.Create(logPath));
\r
474 writer.Write("### CLI Query: " + query + "\n\n");
\r
475 writer.Write("#########################################\n\n");
\r
476 writer.WriteLine(log);
\r
482 /// Save a copy of the log to the users desired location or a default location
\r
483 /// if this feature is enabled in options.
\r
485 /// <param name="destination"></param>
\r
486 public void copyLog(string destination)
\r
490 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
491 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\r
493 string encodeDestinationPath = Path.GetDirectoryName(destination);
\r
494 String destinationFile = Path.GetFileName(destination);
\r
495 string encodeLogFile = destinationFile + " " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
\r
497 // Make sure the log directory exists.
\r
498 if (!Directory.Exists(logDir))
\r
499 Directory.CreateDirectory(logDir);
\r
501 // Copy the Log to HandBrakes log folder in the users applciation data folder.
\r
502 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
\r
504 // Save a copy of the log file in the same location as the enocde.
\r
505 if (Properties.Settings.Default.saveLogWithVideo)
\r
506 File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
\r
508 // Save a copy of the log file to a user specified location
\r
509 if (Directory.Exists(Properties.Settings.Default.saveLogPath))
\r
510 if (Properties.Settings.Default.saveLogPath != String.Empty && Properties.Settings.Default.saveLogToSpecifiedPath)
\r
511 File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.saveLogPath, encodeLogFile));
\r
513 catch (Exception exc)
\r
515 MessageBox.Show("Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc, "Error",
\r
516 MessageBoxButtons.OK, MessageBoxIcon.Error);
\r