public static IEncodeInstance GetEncodeInstance(int verbosity, HBConfiguration configuration, ILog logService, IUserSettingService userSettingService, IPortService portService) { lock (ProcessingLock) { if (!HandBrakeUtils.IsInitialised()) { throw new Exception("Please call Init before Using!"); } IEncodeInstance newInstance; if (userSettingService.GetUserSetting <bool>(UserSettingConstants.ProcessIsolationEnabled) && Portable.IsProcessIsolationEnabled()) { newInstance = new RemoteInstance(logService, userSettingService, portService); } else { if (encodeInstance != null && !encodeInstance.IsRemoteInstance) { encodeInstance.Dispose(); encodeInstance = null; } newInstance = new HandBrakeInstance(); HandBrakeUtils.SetDvdNav( !userSettingService.GetUserSetting <bool>(UserSettingConstants.DisableLibDvdNav)); encodeInstance = newInstance; } newInstance.Initialize(verbosity, noHardware); return(newInstance); } }
public void Start(EncodeTask task, string basePresetName) { try { // Sanity Checking and Setup if (this.IsEncoding) { throw new GeneralApplicationException(Resources.Queue_AlreadyEncoding, Resources.Queue_AlreadyEncodingSolution, null); } // Setup this.startTime = DateTime.Now; this.currentTask = task; this.isPreviewInstance = task.IsPreviewEncode; if (this.userSettingService.GetUserSetting <bool>(UserSettingConstants.ProcessIsolationEnabled) && Portable.IsProcessIsolationEnabled()) { this.InitRemoteLogging(task.Destination); } else { this.encodeLogService = this.logInstanceManager.ApplicationLogInstance; this.encodeLogService.Reset(); } if (this.instance != null) { // Cleanup try { this.instance.EncodeCompleted -= this.InstanceEncodeCompleted; this.instance.EncodeProgress -= this.InstanceEncodeProgress; this.instance.Dispose(); this.instance = null; } catch (Exception exc) { this.ServiceLogMessage("Failed to cleanup previous instance: " + exc); } } this.ServiceLogMessage("Starting Encode ..."); if (!string.IsNullOrEmpty(basePresetName)) { this.TimedLogMessage(string.Format("base preset: {0}", basePresetName)); } int verbosity = this.userSettingService.GetUserSetting <int>(UserSettingConstants.Verbosity); // Prevent port stealing if multiple jobs start at the same time. lock (this.portLock) { this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(verbosity, this.userSettingService) : HandBrakeInstanceManager.GetEncodeInstance(verbosity, this.encodeLogService, this.userSettingService, this.portService); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; this.IsEncoding = true; // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); // Get an EncodeJob object for the Interop Library JsonEncodeObject work = this.encodeTaskFactory.Create(task); this.instance.StartEncode(work); } // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); } catch (Exception exc) { this.IsEncoding = false; this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", this.currentTask.Source, this.currentTask.Destination, null, 0, 3)); } }
private string ProcessLogs(string destination) { try { string logDir = DirectoryUtilities.GetLogDirectory(); string filename = this.encodeLogService.FileName; string logContent = this.encodeLogService.GetFullLog(); // Make sure the log directory exists. if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } // Copy the Log to HandBrakes log folder in the users application data folder. // Only needed for process isolation mode. Worker will handle it's own logging. bool processIsolationEnabled = this.userSettingService.GetUserSetting <bool>(UserSettingConstants.ProcessIsolationEnabled) && Portable.IsProcessIsolationEnabled(); if (!processIsolationEnabled) { string logType = this.isPreviewInstance ? "preview" : "encode"; string destinationFile = Path.GetFileNameWithoutExtension(destination); string logFileName = string.Format("{0}_{1}_{2}.txt", destinationFile, logType, DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", ".").Replace(":", "-")); this.WriteFile(logContent, Path.Combine(logDir, logFileName)); filename = logFileName; } // Save a copy of the log file in the same location as the encode. if (this.userSettingService.GetUserSetting <bool>(UserSettingConstants.SaveLogWithVideo)) { this.WriteFile(logContent, Path.Combine(Path.GetDirectoryName(destination), filename)); } // Save a copy of the log file to a user specified location if (Directory.Exists(this.userSettingService.GetUserSetting <string>(UserSettingConstants.SaveLogCopyDirectory)) && this.userSettingService.GetUserSetting <bool>(UserSettingConstants.SaveLogToCopyDirectory)) { this.WriteFile(logContent, Path.Combine(this.userSettingService.GetUserSetting <string>(UserSettingConstants.SaveLogCopyDirectory), filename)); } return(Path.Combine(logDir, filename)); } catch (Exception exc) { Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged } return(null); }