diff options
Diffstat (limited to 'win/CS/HandBrakeWPF')
-rw-r--r-- | win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs | 18 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Instance/RemoteInstance.cs | 115 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs | 2 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/LogService.cs | 87 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/UserSettingService.cs | 2 |
5 files changed, 94 insertions, 130 deletions
diff --git a/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs b/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs index 914d7916f..f24ae527b 100644 --- a/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs +++ b/win/CS/HandBrakeWPF/Instance/HandBrakeInstanceManager.cs @@ -12,11 +12,13 @@ namespace HandBrakeWPF.Instance using System; using System.Runtime.CompilerServices; + using HandBrake.Interop.Interop; using HandBrake.Interop.Interop.Interfaces; using HandBrake.Interop.Model; using HandBrakeWPF.Factories; + using HandBrakeWPF.Services.Logging.Interfaces; /// <summary> /// The HandBrake Instance manager. @@ -36,19 +38,7 @@ namespace HandBrakeWPF.Instance HandBrakeUtils.EnsureGlobalInit(noHardwareMode); } - /// <summary> - /// The get encode instance. - /// </summary> - /// <param name="verbosity"> - /// The verbosity. - /// </param> - /// <param name="configuration"> - /// The configuration. - /// </param> - /// <returns> - /// The <see cref="IHandBrakeInstance"/>. - /// </returns> - public static IEncodeInstance GetEncodeInstance(int verbosity, HBConfiguration configuration) + public static IEncodeInstance GetEncodeInstance(int verbosity, HBConfiguration configuration, ILog logService) { if (!HandBrakeUtils.IsInitialised()) { @@ -65,7 +55,7 @@ namespace HandBrakeWPF.Instance if (configuration.RemoteServiceEnabled) { - newInstance = new RemoteInstance(configuration.RemoteServicePort); + newInstance = new RemoteInstance(configuration, logService); } else { diff --git a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs index 36221da4d..dc52d0885 100644 --- a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs +++ b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs @@ -11,22 +11,22 @@ namespace HandBrakeWPF.Instance { using System; - using System.Collections.Generic; using System.Diagnostics; - using System.Linq; - using System.Net.Http; - using System.Net.Http.Headers; - using System.Runtime.CompilerServices; - using System.Text; + using System.IO; using System.Threading.Tasks; using System.Timers; + using System.Windows.Media.Animation; + using HandBrake.Interop.Interop.EventArgs; using HandBrake.Interop.Interop.Interfaces; using HandBrake.Interop.Interop.Json.Encode; using HandBrake.Interop.Interop.Json.State; + using HandBrake.Interop.Model; + using HandBrake.Worker.Routing.Commands; using HandBrakeWPF.Instance.Model; + using HandBrakeWPF.Services.Logging.Interfaces; using HandBrakeWPF.Utilities; using Newtonsoft.Json; @@ -42,14 +42,21 @@ namespace HandBrakeWPF.Instance public class RemoteInstance : HttpRequestBase, IEncodeInstance, IDisposable { - private const double EncodePollIntervalMs = 1000; + private readonly HBConfiguration configuration; + + private readonly ILog logService; + + private const double EncodePollIntervalMs = 250; private Process workerProcess; private Timer encodePollTimer; + private int retryCount = 0; - public RemoteInstance(int port) + public RemoteInstance(HBConfiguration configuration, ILog logService) { - this.port = port; + this.configuration = configuration; + this.logService = logService; + this.port = configuration.RemoteServicePort; this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); } @@ -71,8 +78,20 @@ namespace HandBrakeWPF.Instance public async void StartEncode(JsonEncodeObject jobToStart) { + InitCommand initCommand = new InitCommand + { + EnableDiskLogging = true, + AllowDisconnectedWorker = false, + DisableLibDvdNav = this.configuration.IsDvdNavDisabled, + EnableHardwareAcceleration = true, + LogDirectory = DirectoryUtilities.GetLogDirectory(), + LogVerbosity = configuration.Verbosity + }; + + initCommand.LogFile = Path.Combine(initCommand.LogDirectory, string.Format("activity_log.worker.{0}.txt", GeneralUtilities.ProcessId)); + JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; - string job = JsonConvert.SerializeObject(jobToStart, Formatting.None, settings); + string job = JsonConvert.SerializeObject(new EncodeCommand { InitialiseCommand = initCommand, EncodeJob = jobToStart }, Formatting.None, settings); await this.MakeHttpJsonPostRequest("StartEncode", job); @@ -82,7 +101,6 @@ namespace HandBrakeWPF.Instance public async void StopEncode() { await this.MakeHttpGetRequest("StopEncode"); - this.StopPollingProgress(); } public JsonState GetEncodeProgress() @@ -103,34 +121,49 @@ namespace HandBrakeWPF.Instance public void Initialize(int verbosityLvl, bool noHardwareMode) { - this.StartServer(verbosityLvl, noHardwareMode); + this.StartServer(); } public void Dispose() { this.client?.Dispose(); this.workerProcess?.Dispose(); - this.StopEncode(); - this.StopServer(); } - private void StartServer(int verbosityLvl, bool noHardwareMode) + private async void StartServer() { if (this.workerProcess == null || this.workerProcess.HasExited) { - this.workerProcess = new Process(); - this.workerProcess.StartInfo = - new ProcessStartInfo("HandBrake.Worker.exe", string.Format("--port={0} --verbosity={1}", this.port, verbosityLvl)) - { - WindowStyle = ProcessWindowStyle.Normal - }; - - this.workerProcess.Start(); - this.workerProcess.Exited += this.WorkerProcess_Exited; - Debug.WriteLine("Worker Process Started. PID = {0}", this.workerProcess.Id); + workerProcess = new Process + { + StartInfo = + { + FileName = "HandBrake.Worker.exe", + Arguments = string.Format(" --port={0}", this.port), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + } + }; + workerProcess.Exited += this.WorkerProcess_Exited; + workerProcess.OutputDataReceived += this.WorkerProcess_OutputDataReceived; + workerProcess.ErrorDataReceived += this.WorkerProcess_OutputDataReceived; + + workerProcess.Start(); + workerProcess.BeginOutputReadLine(); + workerProcess.BeginErrorReadLine(); + + + this.logService.LogMessage(string.Format("Worker Process started with Process ID: {0}", this.workerProcess.Id)); } } + private void WorkerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) + { + this.logService.LogMessage(e.Data); + } + private void MonitorEncodeProgress() { this.encodePollTimer = new Timer(); @@ -152,13 +185,13 @@ namespace HandBrakeWPF.Instance private void StopPollingProgress() { - this.PollEncodeProgress(); // Get the final progress state. + this.PollEncodeProgress(); // Get the last progress state. this.encodePollTimer?.Stop(); } private void WorkerProcess_Exited(object sender, EventArgs e) { - Debug.WriteLine("Worker Process has exited"); + this.logService.LogMessage("Worker Process Existed!"); } private void StopServer() @@ -174,18 +207,29 @@ namespace HandBrakeWPF.Instance ServerResponse response = null; try { + if (this.retryCount > 5) + { + this.EncodeCompleted?.Invoke(sender: this, e: new EncodeCompletedEventArgs(true)); + + this.encodePollTimer?.Stop(); + + this.workerProcess?.Kill(); + + return; + } + response = await this.MakeHttpGetRequest("PollEncodeProgress"); + + this.retryCount = 0; // Reset } catch (Exception e) { - if (this.encodePollTimer != null) - { - this.encodePollTimer.Stop(); - } + retryCount = this.retryCount + 1; } if (response == null || !response.WasSuccessful) { + retryCount = this.retryCount + 1; return; } @@ -215,8 +259,15 @@ namespace HandBrakeWPF.Instance else if (taskState != null && taskState == TaskState.WorkDone) { this.encodePollTimer.Stop(); - + + this.EncodeCompleted?.Invoke(sender: this, e: new EncodeCompletedEventArgs(state.WorkDone.Error != 0)); + this.workerProcess?.Kill(); + } + else if (taskState == TaskState.Idle) + { + this.encodePollTimer.Stop(); this.EncodeCompleted?.Invoke(sender: this, e: new EncodeCompletedEventArgs(state.WorkDone.Error != 0)); + this.workerProcess?.Kill(); } } } diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs index 417d77855..481c8cebd 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs @@ -108,7 +108,7 @@ namespace HandBrakeWPF.Services.Encode this.TimedLogMessage(string.Format("base preset: {0}", basePresetName)); } - this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity, configuration) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity, configuration); + this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity, configuration) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity, configuration, this.log); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; diff --git a/win/CS/HandBrakeWPF/Services/Logging/LogService.cs b/win/CS/HandBrakeWPF/Services/Logging/LogService.cs index c11b764b0..738cb2b3a 100644 --- a/win/CS/HandBrakeWPF/Services/Logging/LogService.cs +++ b/win/CS/HandBrakeWPF/Services/Logging/LogService.cs @@ -22,8 +22,10 @@ namespace HandBrakeWPF.Services.Logging using HandBrake.Interop.Interop; using HandBrake.Interop.Interop.EventArgs; + using HandBrake.Worker.Logging.Interfaces; using HandBrake.Worker.Logging.Models; + using HandBrakeWPF.Instance; using HandBrakeWPF.Instance.Model; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Services.Logging.Model; @@ -34,7 +36,7 @@ namespace HandBrakeWPF.Services.Logging using ILog = Interfaces.ILog; using LogEventArgs = EventArgs.LogEventArgs; - public class LogService : HttpRequestBase, ILog + public class LogService : ILog { // TODO List. // Maybe make the event weak? @@ -52,23 +54,11 @@ namespace HandBrakeWPF.Services.Logging private bool isDiskLoggingEnabled; private StreamWriter fileWriter; private string logHeader; - private Timer remoteLogPollTimer; - private int remoteIndex = 0; - private bool isRemotePollingEnabled = false; public LogService(IUserSettingService userSettingService) { HandBrakeUtils.MessageLogged += this.HandBrakeUtils_MessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeUtils_ErrorLogged; - - if (userSettingService.GetUserSetting<bool>(UserSettingConstants.RemoteServiceEnabled)) - { - this.ActivateRemoteLogPolling(); - this.isRemotePollingEnabled = true; - - this.port = userSettingService.GetUserSetting<int>(UserSettingConstants.RemoteServicePort); - this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); - } } public event EventHandler<LogEventArgs> MessageLogged; @@ -175,21 +165,6 @@ namespace HandBrakeWPF.Services.Logging public async void Reset() { - //if (this.isRemotePollingEnabled) - //{ - // try - // { - // await this.MakeHttpGetRequest("ResetLogging"); - // } - // catch (Exception e) - // { - // if (this.remoteLogPollTimer != null) - // { - // this.remoteLogPollTimer.Stop(); - // } - // } - //} - lock (this.lockObject) { this.logMessages.Clear(); @@ -359,61 +334,9 @@ namespace HandBrakeWPF.Services.Logging this.LogMessage(e.Message); } - private void ActivateRemoteLogPolling() - { - this.remoteLogPollTimer = new Timer(); - this.remoteLogPollTimer.Interval = 1000; - - this.remoteLogPollTimer.Elapsed += (o, e) => - { - try - { - this.PollRemoteLog(); - } - catch (Exception exc) - { - Debug.WriteLine(exc); - } - }; - this.remoteLogPollTimer.Start(); - } - - private async void PollRemoteLog() + void ILogHandler.ShutdownFileWriter() { - ServerResponse response = null; - try - { - int nextIndex = this.remoteIndex + 1; - string json = JsonConvert.SerializeObject(nextIndex, Formatting.Indented, this.jsonNetSettings); - - response = await this.MakeHttpJsonPostRequest("GetLogMessagesFromIndex", json); - } - catch (Exception e) - { - Debug.WriteLine("No Endpoint"); - } - - if (response == null || !response.WasSuccessful) - { - return; - } - - string statusJson = response.JsonResponse; - - List<LogMessage> messages = null; - if (!string.IsNullOrEmpty(statusJson)) - { - messages = JsonConvert.DeserializeObject<List<LogMessage>>(statusJson, this.jsonNetSettings); - } - - if (messages != null) - { - foreach (var item in messages) - { - this.LogMessage(item.Content); - this.remoteIndex = item.MessageIndex; - } - } + throw new NotImplementedException(); } } } diff --git a/win/CS/HandBrakeWPF/Services/UserSettingService.cs b/win/CS/HandBrakeWPF/Services/UserSettingService.cs index 7e7b49e4a..4c181541a 100644 --- a/win/CS/HandBrakeWPF/Services/UserSettingService.cs +++ b/win/CS/HandBrakeWPF/Services/UserSettingService.cs @@ -305,7 +305,7 @@ namespace HandBrakeWPF.Services // Experimental
defaults.Add(UserSettingConstants.RemoteServiceEnabled, false);
- defaults.Add(UserSettingConstants.RemoteServicePort, true);
+ defaults.Add(UserSettingConstants.RemoteServicePort, 8037);
// Misc
defaults.Add(UserSettingConstants.ShowPresetPanel, false);
|