// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// We have multiple implementations of IEncode. This is a wrapper class for the GUI so that the
// implementation used is controllable via user settings.
// Over time, this class will go away when the LibHB and process isolation code matures.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services
{
using System;
using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Isolation;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Services.Encode;
using HandBrake.ApplicationServices.Services.Encode.EventArgs;
using HandBrake.ApplicationServices.Services.Encode.Interfaces;
using HandBrakeWPF.Services.Interfaces;
///
/// We have multiple implementations of Iencode. This is a wrapper class for the GUI so that the
/// implementation used is controllable via user settings.
/// Over time, this class will go away when the LibHB and process isolation code matures.
///
public class EncodeServiceWrapper : IEncodeServiceWrapper
{
#region Constants and Fields
///
/// The encode service.
///
private readonly IEncode encodeService;
#endregion
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// The user setting service.
///
public EncodeServiceWrapper(IUserSettingService userSettingService)
{
var useLibHb = AppArguments.UseLibHb ? AppArguments.UseLibHb : userSettingService.GetUserSetting(UserSettingConstants.UseLibHb);
var useProcessIsolation =
userSettingService.GetUserSetting(UserSettingConstants.EnableProcessIsolation);
var port = userSettingService.GetUserSetting(UserSettingConstants.ServerPort);
if (useLibHb)
{
try
{
if (useProcessIsolation)
{
this.encodeService = new IsolatedEncodeService(port);
}
else
{
this.encodeService = new LibEncode();
}
}
catch (Exception exc)
{
// Try to recover from errors.
throw new GeneralApplicationException(
"Unable to initialise LibHB or Background worker service",
"Falling back to using HandBrakeCLI.exe. Setting has been reset",
exc);
}
}
else
{
this.encodeService = new EncodeService();
}
this.encodeService.EncodeCompleted += this.EncodeServiceEncodeCompleted;
this.encodeService.EncodeStarted += this.EncodeServiceEncodeStarted;
this.encodeService.EncodeStatusChanged += this.EncodeServiceEncodeStatusChanged;
}
#endregion
#region Events
///
/// The encode completed.
///
public event EncodeCompletedStatus EncodeCompleted;
///
/// The encode started.
///
public event EventHandler EncodeStarted;
///
/// The encode status changed.
///
public event EncodeProgessStatus EncodeStatusChanged;
#endregion
#region Properties
///
/// Gets ActivityLog.
///
public string ActivityLog
{
get
{
return this.encodeService.ActivityLog;
}
}
///
/// Gets the log index.
///
public int LogIndex
{
get
{
return this.encodeService.LogIndex;
}
}
///
/// Gets a value indicating whether can pause.
///
public bool CanPause
{
get
{
return this.encodeService.CanPause;
}
}
///
/// Gets a value indicating whether is pasued.
///
public bool IsPasued
{
get
{
return this.encodeService.IsPasued;
}
}
///
/// Gets a value indicating whether IsEncoding.
///
public bool IsEncoding
{
get
{
return this.encodeService.IsEncoding;
}
}
#endregion
#region Implemented Interfaces
#region IEncode
///
/// Copy the log file to the desired destinations
///
///
/// The destination.
///
///
/// The configuration.
///
public void ProcessLogs(string destination, HBConfiguration configuration)
{
this.encodeService.ProcessLogs(destination, configuration);
}
///
/// Shutdown the service.
///
public void Shutdown()
{
this.encodeService.Shutdown();
this.encodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted;
this.encodeService.EncodeStarted -= this.EncodeServiceEncodeStarted;
this.encodeService.EncodeStatusChanged -= this.EncodeServiceEncodeStatusChanged;
}
///
/// Start with a LibHb EncodeJob Object
///
///
/// The job.
///
public void Start(QueueTask job)
{
this.encodeService.Start(job);
}
///
/// The pause.
///
public void Pause()
{
this.encodeService.Pause();
}
///
/// The resume.
///
public void Resume()
{
this.encodeService.Resume();
}
///
/// Kill the CLI process
///
public void Stop()
{
this.encodeService.Stop();
}
#endregion
#endregion
#region Methods
///
/// The encode service_ encode completed.
///
///
/// The sender.
///
///
/// The EncodeCompletedEventArgs.
///
private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
{
if (EncodeCompleted != null)
{
this.EncodeCompleted(sender, e);
}
}
///
/// The encode service_ encode started.
///
///
/// The sender.
///
///
/// The EventArgs
///
private void EncodeServiceEncodeStarted(object sender, EventArgs e)
{
if (EncodeStarted != null)
{
this.EncodeStarted(sender, e);
}
}
///
/// The encode service_ encode status changed.
///
///
/// The sender.
///
///
/// The EncodeProgressEventArgs.
///
private void EncodeServiceEncodeStatusChanged(object sender, EncodeProgressEventArgs e)
{
if (EncodeStatusChanged != null)
{
this.EncodeStatusChanged(sender, e);
}
}
#endregion
}
}