2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace HandBrake.ApplicationServices.Services
\r
9 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
15 using HandBrake.ApplicationServices.Functions;
\r
16 using HandBrake.ApplicationServices.Model;
\r
17 using HandBrake.ApplicationServices.Parsing;
\r
18 using HandBrake.ApplicationServices.Properties;
\r
19 using HandBrake.ApplicationServices.Services.Interfaces;
\r
21 using Timer = System.Threading.Timer;
\r
24 /// Class which handles the CLI
\r
26 public class Encode : IEncode
\r
28 /* Private Variables */
\r
38 private StringBuilder logBuffer;
\r
41 /// The line number thats been read to in the log file
\r
43 private int logFilePosition;
\r
46 /// A Timer for this window
\r
48 private Timer windowTimer;
\r
51 /// Gets The Process Handle
\r
53 private IntPtr processHandle;
\r
56 /// Gets the Process ID
\r
58 private int processID;
\r
61 /// Windows 7 API Pack wrapper
\r
63 private Win7 windowsSeven = new Win7();
\r
68 /// Initializes a new instance of the <see cref="Encode"/> class.
\r
72 this.EncodeStarted += Encode_EncodeStarted;
\r
78 /// Encode Progess Status
\r
80 /// <param name="sender">
\r
83 /// <param name="e">
\r
84 /// The EncodeProgressEventArgs.
\r
86 public delegate void EncodeProgessStatus(object sender, EncodeProgressEventArgs e);
\r
88 /* Event Handlers */
\r
91 /// Fires when a new CLI Job starts
\r
93 public event EventHandler EncodeStarted;
\r
96 /// Fires when a CLI job finishes.
\r
98 public event EventHandler EncodeEnded;
\r
101 /// Encode process has progressed
\r
103 public event EncodeProgessStatus EncodeStatusChanged;
\r
108 /// Gets or sets The HB Process
\r
110 protected Process HbProcess { get; set; }
\r
113 /// Gets a value indicating whether IsEncoding.
\r
115 public bool IsEncoding { get; private set; }
\r
117 /* Public Methods */
\r
120 /// Gets ActivityLog.
\r
122 public string ActivityLog
\r
126 if (logBuffer == null)
\r
132 return logBuffer != null ? logBuffer.ToString() : string.Empty;
\r
137 /// Create a preview sample video
\r
139 /// <param name="query">
\r
142 public void CreatePreviewSample(string query)
\r
144 this.Run(new Job { Query = query }, true);
\r
148 /// Kill the CLI process
\r
152 if (this.HbProcess != null)
\r
153 this.HbProcess.Kill();
\r
155 Process[] list = Process.GetProcessesByName("HandBrakeCLI");
\r
156 foreach (Process process in list)
\r
159 if (this.EncodeEnded != null)
\r
160 this.EncodeEnded(this, new EventArgs());
\r
164 /// Attempt to Safely kill a DirectRun() CLI
\r
165 /// NOTE: This will not work with a MinGW CLI
\r
166 /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
\r
168 public void SafelyClose()
\r
170 if ((int)this.processHandle == 0)
\r
173 // Allow the CLI to exit cleanly
\r
174 Win32.SetForegroundWindow((int)this.processHandle);
\r
175 SendKeys.Send("^C");
\r
178 // HbProcess.StandardInput.AutoFlush = true;
\r
179 // HbProcess.StandardInput.WriteLine("^C");
\r
183 /// Execute a HandBrakeCLI process.
\r
185 /// <param name="encJob">
\r
188 /// <param name="requireStandardOuput">
\r
189 /// Set to True to show no window and force standard output redirect
\r
191 protected void Run(Job encJob, bool requireStandardOuput)
\r
199 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
200 string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");
\r
201 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query, logPath);
\r
202 var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
204 if (Settings.Default.enocdeStatusInGui || requireStandardOuput)
\r
206 cliStart.RedirectStandardOutput = true;
\r
207 cliStart.UseShellExecute = false;
\r
208 if (!Settings.Default.showCliForInGuiEncodeStatus || requireStandardOuput)
\r
209 cliStart.CreateNoWindow = true;
\r
211 if (Settings.Default.cli_minimized)
\r
212 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\r
214 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
\r
215 HbProcess = Process.Start(cliStart);
\r
216 this.processID = Main.GetCliProcess(before);
\r
218 if (HbProcess != null)
\r
219 this.processHandle = HbProcess.MainWindowHandle; // Set the process Handle
\r
221 // Start the Log Monitor
\r
222 windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000);
\r
224 // Set the process Priority
\r
225 Process hbCliProcess = null;
\r
226 if (this.processID != -1)
\r
228 hbCliProcess = Process.GetProcessById(this.processID);
\r
229 hbCliProcess.EnableRaisingEvents = true;
\r
230 hbCliProcess.Exited += new EventHandler(HbProcess_Exited);
\r
233 if (hbCliProcess != null)
\r
234 switch (Settings.Default.processPriority)
\r
237 hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
240 hbCliProcess.PriorityClass = ProcessPriorityClass.High;
\r
242 case "Above Normal":
\r
243 hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
246 hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
249 hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
252 hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
256 // Fire the Encode Started Event
\r
257 if (this.EncodeStarted != null)
\r
258 this.EncodeStarted(this, new EventArgs());
\r
260 catch (Exception exc)
\r
262 Main.ShowExceptiowWindow("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\nDetailed Error Information: error occured in runCli()", exc.ToString());
\r
267 /// The HandBrakeCLI process has exited.
\r
269 /// <param name="sender">
\r
272 /// <param name="e">
\r
275 private void HbProcess_Exited(object sender, EventArgs e)
\r
277 IsEncoding = false;
\r
279 windowTimer.Dispose();
\r
282 if (this.EncodeEnded != null)
\r
283 this.EncodeEnded(this, new EventArgs());
\r
285 if (windowsSeven.IsWindowsSeven)
\r
287 windowsSeven.SetTaskBarProgressToNoProgress();
\r
292 /// Function to run the CLI directly rather than via CMD
\r
293 /// TODO: Code to handle the Log data has yet to be written.
\r
294 /// TODO: Code to handle the % / ETA info has to be written.
\r
296 /// <param name="query">
\r
299 protected void DirectRun(string query)
\r
303 if (this.EncodeStarted != null)
\r
304 this.EncodeStarted(this, new EventArgs());
\r
311 string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");
\r
312 HbProcess = new Process
\r
316 FileName = handbrakeCLIPath,
\r
318 UseShellExecute = false,
\r
319 RedirectStandardOutput = true,
\r
320 RedirectStandardError = true,
\r
321 RedirectStandardInput = true,
\r
322 CreateNoWindow = false,
\r
323 WindowStyle = ProcessWindowStyle.Minimized
\r
327 // Setup event handlers for rediected data
\r
328 HbProcess.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);
\r
329 HbProcess.OutputDataReceived += new DataReceivedEventHandler(HbProcOutputDataReceived);
\r
331 // Start the process
\r
334 // Setup the asynchronous reading of stdin and stderr
\r
335 HbProcess.BeginErrorReadLine();
\r
336 HbProcess.BeginOutputReadLine();
\r
338 // Set the Process Priority);
\r
339 switch (Settings.Default.processPriority)
\r
342 HbProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
345 HbProcess.PriorityClass = ProcessPriorityClass.High;
\r
347 case "Above Normal":
\r
348 HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
351 HbProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
354 HbProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
357 HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
361 // Set the class items
\r
362 this.processID = HbProcess.Id;
\r
363 this.processHandle = HbProcess.Handle;
\r
365 catch (Exception exc)
\r
367 Console.WriteLine(exc);
\r
372 /// Add the CLI Query to the Log File.
\r
374 /// <param name="encJob">
\r
375 /// The Encode Job Object
\r
377 protected void AddCLIQueryToLog(Job encJob)
\r
381 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
382 "\\HandBrake\\logs";
\r
383 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
385 var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
\r
386 string log = reader.ReadToEnd();
\r
389 var writer = new StreamWriter(File.Create(logPath));
\r
391 writer.WriteLine("### CLI Query: " + encJob.Query);
\r
392 writer.WriteLine("### User Query: " + encJob.CustomQuery);
\r
393 writer.WriteLine("#########################################");
\r
394 writer.WriteLine(log);
\r
405 /// Save a copy of the log to the users desired location or a default location
\r
406 /// if this feature is enabled in options.
\r
408 /// <param name="destination">
\r
409 /// The Destination File Path
\r
411 protected void CopyLog(string destination)
\r
415 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
416 "\\HandBrake\\logs";
\r
417 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\r
419 string encodeDestinationPath = Path.GetDirectoryName(destination);
\r
420 string destinationFile = Path.GetFileName(destination);
\r
421 string encodeLogFile = destinationFile + " " +
\r
422 DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
\r
424 // Make sure the log directory exists.
\r
425 if (!Directory.Exists(logDir))
\r
426 Directory.CreateDirectory(logDir);
\r
428 // Copy the Log to HandBrakes log folder in the users applciation data folder.
\r
429 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
\r
431 // Save a copy of the log file in the same location as the enocde.
\r
432 if (Settings.Default.saveLogWithVideo)
\r
433 File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
\r
435 // Save a copy of the log file to a user specified location
\r
436 if (Directory.Exists(Settings.Default.saveLogPath))
\r
437 if (Settings.Default.saveLogPath != String.Empty && Settings.Default.saveLogToSpecifiedPath)
\r
438 File.Copy(tempLogFile, Path.Combine(Settings.Default.saveLogPath, encodeLogFile));
\r
440 catch (Exception exc)
\r
442 Main.ShowExceptiowWindow("Unable to make a copy of the log file", exc.ToString());
\r
447 /// Read the log file
\r
449 /// <param name="n">
\r
452 private void ReadFile(object n)
\r
456 // last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),
\r
457 // we'll need to make a copy of it.
\r
458 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
459 "\\HandBrake\\logs";
\r
460 string logFile = Path.Combine(logDir, "last_encode_log.txt");
\r
461 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
465 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
466 if (File.Exists(logFile2))
\r
467 File.Delete(logFile2);
\r
469 // Copy the log file.
\r
470 if (File.Exists(logFile))
\r
471 File.Copy(logFile, logFile2, true);
\r
478 // Put the Query and User Generated Query Flag on the log.
\r
479 if (logFilePosition == 0 && job.Query != null)
\r
481 logBuffer.AppendLine("### CLI Query: " + job.Query);
\r
482 logBuffer.AppendLine("### User Query: " + job.CustomQuery);
\r
483 logBuffer.AppendLine("#########################################");
\r
486 // Start the Reader
\r
487 // Only use text which continues on from the last read line
\r
488 StreamReader sr = new StreamReader(logFile2);
\r
491 while ((line = sr.ReadLine()) != null)
\r
493 if (i > logFilePosition)
\r
495 logBuffer.AppendLine(line);
\r
511 /// Reset the Log Reader
\r
513 private void ResetLogReader()
\r
515 logFilePosition = 0;
\r
516 logBuffer = new StringBuilder();
\r
520 /// Recieve the Standard Error information and process it
\r
522 /// <param name="sender">
\r
523 /// The Sender Object
\r
525 /// <param name="e">
\r
526 /// DataReceived EventArgs
\r
528 private void HbProcErrorDataReceived(object sender, DataReceivedEventArgs e)
\r
530 if (!String.IsNullOrEmpty(e.Data))
\r
533 logBuffer.AppendLine(e.Data);
\r
538 /// Standard Input Data Recieved from the CLI
\r
540 /// <param name="sender">
\r
541 /// The Sender Object
\r
543 /// <param name="e">
\r
544 /// DataReceived EventArgs
\r
546 private void HbProcOutputDataReceived(object sender, DataReceivedEventArgs e)
\r
548 if (!String.IsNullOrEmpty(e.Data))
\r
551 logBuffer.AppendLine(e.Data);
\r
558 /// <param name="sender">
\r
561 /// <param name="e">
\r
564 private void Encode_EncodeStarted(object sender, EventArgs e)
\r
566 Thread monitor = new Thread(EncodeMonitor);
\r
571 /// Monitor the Job
\r
573 private void EncodeMonitor()
\r
577 Parser encode = new Parser(HbProcess.StandardOutput.BaseStream);
\r
578 encode.OnEncodeProgress += EncodeOnEncodeProgress;
\r
579 while (!encode.EndOfStream)
\r
580 encode.ReadEncodeStatus();
\r
582 // Main.ShowExceptiowWindow("Encode Monitor Stopped", "Stopped");
\r
584 catch (Exception exc)
\r
586 Main.ShowExceptiowWindow("An Unknown Error has occured", exc.ToString());
\r
591 /// Displays the Encode status in the GUI
\r
593 /// <param name="sender">The sender</param>
\r
594 /// <param name="currentTask">The current task</param>
\r
595 /// <param name="taskCount">Number of tasks</param>
\r
596 /// <param name="percentComplete">Percent complete</param>
\r
597 /// <param name="currentFps">Current encode speed in fps</param>
\r
598 /// <param name="avg">Avg encode speed</param>
\r
599 /// <param name="timeRemaining">Time Left</param>
\r
600 private void EncodeOnEncodeProgress(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float avg, TimeSpan timeRemaining)
\r
602 EncodeProgressEventArgs eventArgs = new EncodeProgressEventArgs
\r
604 AverageFrameRate = avg,
\r
605 CurrentFrameRate = currentFps,
\r
606 EstimatedTimeLeft = timeRemaining,
\r
607 PercentComplete = percentComplete,
\r
608 Task = currentTask,
\r
609 TaskCount = taskCount
\r
612 if (this.EncodeStatusChanged != null)
\r
613 this.EncodeStatusChanged(this, eventArgs);
\r
615 if (windowsSeven.IsWindowsSeven)
\r
618 int.TryParse(Math.Round(percentComplete).ToString(), out percent);
\r
620 windowsSeven.SetTaskBarProgress(percent);
\r