From dac7f88b4fe8ec3347408167c84e97b1da4c3468 Mon Sep 17 00:00:00 2001 From: Aydin Date: Fri, 8 Apr 2016 03:57:12 +0100 Subject: [PATCH] Bug fix: When the app exits in a ungraceful manner and is unable to dispose of the FFmpeg process, starting a new instance of MediaToolkit will kill all previous instances of FFmpeg. --- MediaToolkit src/MediaToolkit/EngineBase.cs | 30 ++-------- .../MediaToolkit/Util/Extensions.cs | 55 +++++++++++-------- 2 files changed, 38 insertions(+), 47 deletions(-) diff --git a/MediaToolkit src/MediaToolkit/EngineBase.cs b/MediaToolkit src/MediaToolkit/EngineBase.cs index e37357e..527b4a0 100644 --- a/MediaToolkit src/MediaToolkit/EngineBase.cs +++ b/MediaToolkit src/MediaToolkit/EngineBase.cs @@ -59,12 +59,15 @@ protected EngineBase(string ffMpegPath) private void EnsureFFmpegIsNotUsed() { - if (!Document.IsLocked(this.FFmpegFilePath)) return; - try { this.Mutex.WaitOne(); - KillFFmpegProcesses(); + Process.GetProcessesByName(Resources.FFmpegProcessName) + .ForEach(process => + { + process.Kill(); + process.WaitForExit(); + }); } finally { @@ -90,27 +93,6 @@ private void EnsureFFmpegFileExists() } } - - - ///------------------------------------------------------------------------------------------------- - /// Kill FFmpeg processes. - private static void KillFFmpegProcesses() - { - Process[] ffmpegProcesses = Process.GetProcessesByName(Resources.FFmpegProcessName); - if (ffmpegProcesses.Length <= 0) - { - return; - } - - foreach (Process process in ffmpegProcesses) - { - // pew pew pew... - process.Kill(); - // let it die... - Thread.Sleep(200); - } - } - ///------------------------------------------------------------------------------------------------- /// Unpack ffmpeg executable. /// Thrown when an exception error condition occurs. diff --git a/MediaToolkit src/MediaToolkit/Util/Extensions.cs b/MediaToolkit src/MediaToolkit/Util/Extensions.cs index 708622c..33ff975 100644 --- a/MediaToolkit src/MediaToolkit/Util/Extensions.cs +++ b/MediaToolkit src/MediaToolkit/Util/Extensions.cs @@ -1,56 +1,65 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; namespace System.Runtime.CompilerServices { - public class ExtensionAttribute : Attribute { } + public class ExtensionAttribute : Attribute + { + } } + namespace MediaToolkit.Util { public static class Extensions { + private const int BUFF_SIZE = 16*1024; + + internal static void CopyTo(this Stream input, Stream output) + { + byte[] buffer = new byte[Extensions.BUFF_SIZE]; + int bytesRead; + + while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, bytesRead); } + } + public static string FormatInvariant(this string value, params object[] args) { try { - return value == null ? string.Empty : string.Format(CultureInfo.InvariantCulture, value, args); + return value == null + ? string.Empty + : string.Format(CultureInfo.InvariantCulture, value, args); } - catch (FormatException ex) - { + catch (FormatException ex) { return value; } } internal static bool IsNullOrWhiteSpace(this string value) { - return String.IsNullOrEmpty(value) || value.Trim().Length == 0; + return string.IsNullOrEmpty(value) || value.Trim() + .Length == 0; } - const int BUFF_SIZE = 16 * 1024; - internal static void CopyTo(this Stream input, Stream output) + internal static string Remove(this Enum enumerable, string text) { - byte[] buffer = new byte[BUFF_SIZE]; - int bytesRead; - - while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) - { - output.Write(buffer, 0, bytesRead); - } + return enumerable.ToString() + .Replace(text, ""); } + internal static string ToLower(this Enum enumerable) { - return enumerable.ToString().ToLowerInvariant(); + return enumerable.ToString() + .ToLowerInvariant(); } - internal static string Remove(this Enum enumerable, string text) + public static void ForEach(this IEnumerable collection, Action action) { - return enumerable.ToString().Replace(text, ""); - } + if (action == null) throw new ArgumentNullException("action"); - //internal static bool IsNullOrWhiteSpace(this string text) - //{ - // return string.IsNullOrWhiteSpace(text); - //} + foreach (T t in collection) action(t); + } } -} +} \ No newline at end of file