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.Services
\r
9 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
17 using Timer = System.Threading.Timer;
\r
20 /// Class which handles the CLI
\r
24 /* Private Variables */
\r
34 private StringBuilder logBuffer;
\r
37 /// The line number thats been read to in the log file
\r
39 private int logFilePosition;
\r
42 /// A Timer for this window
\r
44 private Timer windowTimer;
\r
47 /// Gets The Process Handle
\r
49 private IntPtr processHandle;
\r
52 /// Gets the Process ID
\r
54 private int processID;
\r
56 /* Event Handlers */
\r
59 /// Fires when a new CLI Job starts
\r
61 public event EventHandler EncodeStarted;
\r
64 /// Fires when a CLI job finishes.
\r
66 public event EventHandler EncodeEnded;
\r
71 /// Gets or sets The HB Process
\r
73 public Process HbProcess { get; set; }
\r
76 /// Gets a value indicating whether IsEncoding.
\r
78 public bool IsEncoding { get; private set; }
\r
80 /* Public Methods */
\r
83 /// Gets ActivityLog.
\r
85 public string ActivityLog
\r
89 if (logBuffer == null)
\r
95 return logBuffer != null ? logBuffer.ToString() : string.Empty;
\r
100 /// Create a preview sample video
\r
102 /// <param name="query">
\r
105 public void CreatePreviewSample(string query)
\r
107 this.Run(new Job { Query = query });
\r
111 /// Kill the CLI process
\r
115 if (this.HbProcess != null)
\r
116 this.HbProcess.Kill();
\r
118 Process[] list = Process.GetProcessesByName("HandBrakeCLI");
\r
119 foreach (Process process in list)
\r
122 if (this.EncodeEnded != null)
\r
123 this.EncodeEnded(this, new EventArgs());
\r
127 /// Attempt to Safely kill a DirectRun() CLI
\r
128 /// NOTE: This will not work with a MinGW CLI
\r
129 /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
\r
131 public void SafelyClose()
\r
133 if ((int)this.processHandle == 0)
\r
136 // Allow the CLI to exit cleanly
\r
137 Win32.SetForegroundWindow((int)this.processHandle);
\r
138 SendKeys.Send("^C");
\r
141 // HbProcess.StandardInput.AutoFlush = true;
\r
142 // HbProcess.StandardInput.WriteLine("^C");
\r
146 /// Execute a HandBrakeCLI process.
\r
148 /// <param name="encJob">
\r
151 protected void Run(Job encJob)
\r
158 if (this.EncodeStarted != null)
\r
159 this.EncodeStarted(this, new EventArgs());
\r
163 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
166 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs",
\r
167 "last_encode_log.txt");
\r
168 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query,
\r
170 var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
172 if (Settings.Default.enocdeStatusInGui)
\r
174 cliStart.RedirectStandardOutput = true;
\r
175 cliStart.UseShellExecute = false;
\r
176 if (!Settings.Default.showCliForInGuiEncodeStatus)
\r
177 cliStart.CreateNoWindow = true;
\r
179 if (Settings.Default.cli_minimized)
\r
180 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\r
182 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
\r
183 Process startProcess = Process.Start(cliStart);
\r
185 this.processID = Main.GetCliProcess(before);
\r
187 // Fire the Encode Started Event
\r
188 if (this.EncodeStarted != null)
\r
189 this.EncodeStarted(this, new EventArgs());
\r
191 if (startProcess != null)
\r
192 this.processHandle = startProcess.MainWindowHandle; // Set the process Handle
\r
194 // Start the Log Monitor
\r
195 windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000);
\r
197 // Set the process Priority
\r
198 if (this.processID != -1)
\r
200 HbProcess = Process.GetProcessById(this.processID);
\r
201 HbProcess.EnableRaisingEvents = true;
\r
202 HbProcess.Exited += new EventHandler(HbProcess_Exited);
\r
205 if (HbProcess != null)
\r
206 switch (Settings.Default.processPriority)
\r
209 HbProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
212 HbProcess.PriorityClass = ProcessPriorityClass.High;
\r
214 case "Above Normal":
\r
215 HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
218 HbProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
221 HbProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
224 HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
228 catch (Exception exc)
\r
231 "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()\n\n" +
\r
234 MessageBoxButtons.OK,
\r
235 MessageBoxIcon.Error);
\r
240 /// The HandBrakeCLI process has exited.
\r
242 /// <param name="sender">
\r
245 /// <param name="e">
\r
248 private void HbProcess_Exited(object sender, EventArgs e)
\r
250 IsEncoding = false;
\r
254 /// Function to run the CLI directly rather than via CMD
\r
255 /// TODO: Code to handle the Log data has yet to be written.
\r
256 /// TODO: Code to handle the % / ETA info has to be written.
\r
258 /// <param name="query">
\r
261 protected void DirectRun(string query)
\r
265 if (this.EncodeStarted != null)
\r
266 this.EncodeStarted(this, new EventArgs());
\r
273 string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");
\r
274 HbProcess = new Process
\r
278 FileName = handbrakeCLIPath,
\r
280 UseShellExecute = false,
\r
281 RedirectStandardOutput = true,
\r
282 RedirectStandardError = true,
\r
283 RedirectStandardInput = true,
\r
284 CreateNoWindow = false,
\r
285 WindowStyle = ProcessWindowStyle.Minimized
\r
289 // Setup event handlers for rediected data
\r
290 HbProcess.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);
\r
291 HbProcess.OutputDataReceived += new DataReceivedEventHandler(HbProcOutputDataReceived);
\r
293 // Start the process
\r
296 // Setup the asynchronous reading of stdin and stderr
\r
297 HbProcess.BeginErrorReadLine();
\r
298 HbProcess.BeginOutputReadLine();
\r
300 // Set the Process Priority);
\r
301 switch (Settings.Default.processPriority)
\r
304 HbProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
307 HbProcess.PriorityClass = ProcessPriorityClass.High;
\r
309 case "Above Normal":
\r
310 HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
313 HbProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
316 HbProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
319 HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
323 // Set the class items
\r
324 this.processID = HbProcess.Id;
\r
325 this.processHandle = HbProcess.Handle;
\r
327 catch (Exception exc)
\r
329 Console.WriteLine(exc);
\r
334 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
336 protected void Finish()
\r
340 windowTimer.Dispose();
\r
344 if (this.EncodeEnded != null)
\r
345 this.EncodeEnded(this, new EventArgs());
\r
348 if (Settings.Default.growlQueue)
\r
349 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
\r
351 // Do something whent he encode ends.
\r
352 switch (Settings.Default.CompletionOption)
\r
355 Process.Start("Shutdown", "-s -t 60");
\r
358 Win32.ExitWindowsEx(0, 0);
\r
361 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
364 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
366 case "Lock System":
\r
367 Win32.LockWorkStation();
\r
369 case "Quit HandBrake":
\r
370 Application.Exit();
\r
378 /// Add the CLI Query to the Log File.
\r
380 /// <param name="encJob">
\r
381 /// The Encode Job Object
\r
383 protected void AddCLIQueryToLog(Job encJob)
\r
387 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
388 "\\HandBrake\\logs";
\r
389 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
391 var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
\r
392 string log = reader.ReadToEnd();
\r
395 var writer = new StreamWriter(File.Create(logPath));
\r
397 writer.WriteLine("### CLI Query: " + encJob.Query);
\r
398 writer.WriteLine("### User Query: " + encJob.CustomQuery);
\r
399 writer.WriteLine("#########################################");
\r
400 writer.WriteLine(log);
\r
411 /// Save a copy of the log to the users desired location or a default location
\r
412 /// if this feature is enabled in options.
\r
414 /// <param name="destination">
\r
415 /// The Destination File Path
\r
417 protected void CopyLog(string destination)
\r
421 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
422 "\\HandBrake\\logs";
\r
423 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\r
425 string encodeDestinationPath = Path.GetDirectoryName(destination);
\r
426 string destinationFile = Path.GetFileName(destination);
\r
427 string encodeLogFile = destinationFile + " " +
\r
428 DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
\r
430 // Make sure the log directory exists.
\r
431 if (!Directory.Exists(logDir))
\r
432 Directory.CreateDirectory(logDir);
\r
434 // Copy the Log to HandBrakes log folder in the users applciation data folder.
\r
435 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
\r
437 // Save a copy of the log file in the same location as the enocde.
\r
438 if (Settings.Default.saveLogWithVideo)
\r
439 File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
\r
441 // Save a copy of the log file to a user specified location
\r
442 if (Directory.Exists(Settings.Default.saveLogPath))
\r
443 if (Settings.Default.saveLogPath != String.Empty && Settings.Default.saveLogToSpecifiedPath)
\r
444 File.Copy(tempLogFile, Path.Combine(Settings.Default.saveLogPath, encodeLogFile));
\r
446 catch (Exception exc)
\r
449 "Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc,
\r
451 MessageBoxButtons.OK,
\r
452 MessageBoxIcon.Error);
\r
457 /// Read the log file
\r
459 /// <param name="n">
\r
462 private void ReadFile(object n)
\r
466 // 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
467 // we'll need to make a copy of it.
\r
468 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
469 "\\HandBrake\\logs";
\r
470 string logFile = Path.Combine(logDir, "last_encode_log.txt");
\r
471 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
475 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
476 if (File.Exists(logFile2))
\r
477 File.Delete(logFile2);
\r
479 // Copy the log file.
\r
480 if (File.Exists(logFile))
\r
481 File.Copy(logFile, logFile2, true);
\r
488 // Put the Query and User Generated Query Flag on the log.
\r
489 if (logFilePosition == 0 && job.Query != null)
\r
491 logBuffer.AppendLine("### CLI Query: " + job.Query);
\r
492 logBuffer.AppendLine("### User Query: " + job.CustomQuery);
\r
493 logBuffer.AppendLine("#########################################");
\r
496 // Start the Reader
\r
497 // Only use text which continues on from the last read line
\r
498 StreamReader sr = new StreamReader(logFile2);
\r
501 while ((line = sr.ReadLine()) != null)
\r
503 if (i > logFilePosition)
\r
505 logBuffer.AppendLine(line);
\r
521 /// Reset the Log Reader
\r
523 private void ResetLogReader()
\r
525 logFilePosition = 0;
\r
526 logBuffer = new StringBuilder();
\r
530 /// Recieve the Standard Error information and process it
\r
532 /// <param name="sender">
\r
533 /// The Sender Object
\r
535 /// <param name="e">
\r
536 /// DataReceived EventArgs
\r
538 private void HbProcErrorDataReceived(object sender, DataReceivedEventArgs e)
\r
540 if (!String.IsNullOrEmpty(e.Data))
\r
543 logBuffer.AppendLine(e.Data);
\r
548 /// Standard Input Data Recieved from the CLI
\r
550 /// <param name="sender">
\r
551 /// The Sender Object
\r
553 /// <param name="e">
\r
554 /// DataReceived EventArgs
\r
556 private void HbProcOutputDataReceived(object sender, DataReceivedEventArgs e)
\r
558 if (!String.IsNullOrEmpty(e.Data))
\r
561 logBuffer.AppendLine(e.Data);
\r