Skip to content

Commit

Permalink
Bug fix: When the app exits in a ungraceful manner and is unable to d…
Browse files Browse the repository at this point in the history
…ispose of the FFmpeg process, starting a new instance of MediaToolkit will kill all previous instances of FFmpeg.
  • Loading branch information
AydinAdn committed Apr 8, 2016
1 parent b985449 commit dac7f88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 47 deletions.
30 changes: 6 additions & 24 deletions MediaToolkit src/MediaToolkit/EngineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -90,27 +93,6 @@ private void EnsureFFmpegFileExists()
}
}



///-------------------------------------------------------------------------------------------------
/// <summary> Kill FFmpeg processes. </summary>
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);
}
}

///-------------------------------------------------------------------------------------------------
/// <summary> Unpack ffmpeg executable. </summary>
/// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception>
Expand Down
55 changes: 32 additions & 23 deletions MediaToolkit src/MediaToolkit/Util/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(this IEnumerable<T> collection, Action<T> 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);
}
}
}
}

0 comments on commit dac7f88

Please sign in to comment.