diff --git a/MediaToolkit src/MediaToolkit/EngineBase.cs b/MediaToolkit src/MediaToolkit/EngineBase.cs index 527b4a0..8402b96 100644 --- a/MediaToolkit src/MediaToolkit/EngineBase.cs +++ b/MediaToolkit src/MediaToolkit/EngineBase.cs @@ -1,144 +1,145 @@ namespace MediaToolkit { - using System; - using System.Configuration; - using System.Diagnostics; - using System.IO; - using System.IO.Compression; - using System.Reflection; - using System.Threading; - - using MediaToolkit.Properties; - using MediaToolkit.Util; - - public class EngineBase : IDisposable - { - private bool isDisposed; - - /// Used for locking the FFmpeg process to one thread. - private const string LockName = "MediaToolkit.Engine.LockName"; - - private const string DefaultFFmpegFilePath = @"/MediaToolkit/ffmpeg.exe"; - - /// Full pathname of the FFmpeg file. - protected readonly string FFmpegFilePath; - - /// The Mutex. - protected readonly Mutex Mutex; - - /// The ffmpeg process. - protected Process FFmpegProcess; - - - protected EngineBase() - : this(ConfigurationManager.AppSettings["mediaToolkit.ffmpeg.path"]) - { - } - - ///------------------------------------------------------------------------------------------------- - /// - /// Initializes FFmpeg.exe; Ensuring that there is a copy - /// in the clients temp folder & isn't in use by another process. - /// - protected EngineBase(string ffMpegPath) - { - this.Mutex = new Mutex(false, LockName); - this.isDisposed = false; - - if (ffMpegPath.IsNullOrWhiteSpace()) - { - ffMpegPath = DefaultFFmpegFilePath; - } - - this.FFmpegFilePath = ffMpegPath; - - this.EnsureDirectoryExists (); - this.EnsureFFmpegFileExists(); - this.EnsureFFmpegIsNotUsed (); - } - - private void EnsureFFmpegIsNotUsed() - { - try - { - this.Mutex.WaitOne(); - Process.GetProcessesByName(Resources.FFmpegProcessName) - .ForEach(process => - { - process.Kill(); - process.WaitForExit(); - }); - } - finally - { - this.Mutex.ReleaseMutex(); - } - } - - private void EnsureDirectoryExists() - { - string directory = Path.GetDirectoryName(this.FFmpegFilePath) ?? Directory.GetCurrentDirectory(); ; - - if (!Directory.Exists(directory)) - { - Directory.CreateDirectory(directory); - } - } - - private void EnsureFFmpegFileExists() - { - if (!File.Exists(this.FFmpegFilePath)) - { - UnpackFFmpegExecutable(this.FFmpegFilePath); - } - } - - ///------------------------------------------------------------------------------------------------- - /// Unpack ffmpeg executable. - /// Thrown when an exception error condition occurs. - private static void UnpackFFmpegExecutable(string path) - { - Stream compressedFFmpegStream = Assembly.GetExecutingAssembly() - .GetManifestResourceStream(Resources.FFmpegManifestResourceName); - - if (compressedFFmpegStream == null) - { - throw new Exception(Resources.Exceptions_Null_FFmpeg_Gzip_Stream); - } - - using (FileStream fileStream = new FileStream(path, FileMode.Create)) - using (GZipStream compressedStream = new GZipStream(compressedFFmpegStream, CompressionMode.Decompress)) - { - compressedStream.CopyTo(fileStream); - } - } - - - - ///------------------------------------------------------------------------------------------------- - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting - /// unmanaged resources. - /// - /// Aydin Aydin, 30/03/2015. - public virtual void Dispose() - { - this.Dispose(true); - } - - private void Dispose(bool disposing) - { - if (!disposing || this.isDisposed) - { - return; - } - - if(FFmpegProcess != null) - { - this.FFmpegProcess.Dispose(); - } - this.FFmpegProcess = null; - this.isDisposed = true; - } - } + using System; + using System.Configuration; + using System.Diagnostics; + using System.IO; + using System.IO.Compression; + using System.Reflection; + using System.Threading; + + using MediaToolkit.Properties; + using MediaToolkit.Util; + + public class EngineBase : IDisposable + { + private bool isDisposed; + + /// Used for locking the FFmpeg process to one thread. + private const string LockName = "MediaToolkit.Engine.LockName"; + + private const string DefaultFFmpegFilePath = @"/MediaToolkit/ffmpeg.exe"; + + /// Full pathname of the FFmpeg file. + protected readonly string FFmpegFilePath; + + /// The Mutex. + protected readonly Mutex Mutex; + + /// The ffmpeg process. + protected Process FFmpegProcess; + + + protected EngineBase() + : this(ConfigurationManager.AppSettings["mediaToolkit.ffmpeg.path"]) + { + } + + ///------------------------------------------------------------------------------------------------- + /// + /// Initializes FFmpeg.exe; Ensuring that there is a copy + /// in the clients temp folder & isn't in use by another process. + /// + protected EngineBase(string ffMpegPath) + { + var lockName = $"{LockName}-{ConfigurationManager.AppSettings["mediaToolkit.application.name"]}"; + this.Mutex = new Mutex(false, lockName); + this.isDisposed = false; + + if (ffMpegPath.IsNullOrWhiteSpace()) + { + ffMpegPath = DefaultFFmpegFilePath; + } + + this.FFmpegFilePath = ffMpegPath; + + this.EnsureDirectoryExists(); + this.EnsureFFmpegFileExists(); + this.EnsureFFmpegIsNotUsed(); + } + + private void EnsureFFmpegIsNotUsed() + { + try + { + this.Mutex.WaitOne(); + Process.GetProcessesByName(Resources.FFmpegProcessName) + .ForEach(process => + { + process.Kill(); + process.WaitForExit(); + }); + } + finally + { + this.Mutex.ReleaseMutex(); + } + } + + private void EnsureDirectoryExists() + { + string directory = Path.GetDirectoryName(this.FFmpegFilePath) ?? Directory.GetCurrentDirectory(); ; + + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + } + + private void EnsureFFmpegFileExists() + { + if (!File.Exists(this.FFmpegFilePath)) + { + UnpackFFmpegExecutable(this.FFmpegFilePath); + } + } + + ///------------------------------------------------------------------------------------------------- + /// Unpack ffmpeg executable. + /// Thrown when an exception error condition occurs. + private static void UnpackFFmpegExecutable(string path) + { + Stream compressedFFmpegStream = Assembly.GetExecutingAssembly() + .GetManifestResourceStream(Resources.FFmpegManifestResourceName); + + if (compressedFFmpegStream == null) + { + throw new Exception(Resources.Exceptions_Null_FFmpeg_Gzip_Stream); + } + + using (FileStream fileStream = new FileStream(path, FileMode.Create)) + using (GZipStream compressedStream = new GZipStream(compressedFFmpegStream, CompressionMode.Decompress)) + { + compressedStream.CopyTo(fileStream); + } + } + + + + ///------------------------------------------------------------------------------------------------- + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting + /// unmanaged resources. + /// + /// Aydin Aydin, 30/03/2015. + public virtual void Dispose() + { + this.Dispose(true); + } + + private void Dispose(bool disposing) + { + if (!disposing || this.isDisposed) + { + return; + } + + if (FFmpegProcess != null) + { + this.FFmpegProcess.Dispose(); + } + this.FFmpegProcess = null; + this.isDisposed = true; + } + } }