Skip to content

Commit fa53a1a

Browse files
committed
Fix - Dropping mencoder bin on disposing even when async encoding is running
1 parent b8c23b7 commit fa53a1a

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

MencoderSharp/Mencoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public bool EncodeToMp4(string source, string destination)
2828
/// </summary>
2929
/// <param name="source">The source.</param>
3030
/// <param name="destination">The destination.</param>
31-
/// <param name="videoParameter">The video parameter.</param>
32-
/// <param name="audioParameter">The audio parameter.</param>
31+
/// <param name="videoParameter">The video parameter e.g. "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:global_header:threads=auto"</param>
32+
/// <param name="audioParameter">The audio parameter e.g. "-oac mp3lame"</param>
3333
/// <returns>True if task finishes without error</returns>
3434
public bool Mencode(string source, string destination, string videoParameter, string audioParameter)
3535
{

MencoderSharp/MencoderBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Reflection;
45
using System.Runtime.InteropServices;
6+
using System.Threading.Tasks;
57

68
namespace MencoderSharp
79
{
@@ -116,6 +118,14 @@ protected virtual void Dispose(bool disposing)
116118

117119
if (!customMencoderLocation && File.Exists(PathToExternalMencoderBin))
118120
{
121+
foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(PathToExternalMencoderBin)))
122+
{
123+
if (!process.HasExited)
124+
{
125+
process.Kill();
126+
Task.Delay(5).Wait();
127+
}
128+
}
119129
File.Delete(PathToExternalMencoderBin);
120130
}
121131

MencoderSharpTest/MencoderSharpIntegrationTest.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Threading.Tasks;
45
using Xunit;
@@ -43,6 +44,22 @@ public void ShouldStartEncodeAsync()
4344
AssertVeraPdfGotDisposed();
4445
}
4546

47+
[Fact]
48+
public void ShouldDisposeWithoutExceptionWhileEncodeAsync()
49+
{
50+
using (var mencoderAsync = new MencoderSharp.MencoderAsync())
51+
{
52+
mencoderAsync.Finished += Mencoder_Finished;
53+
mencoderAsync.ProgressChanged += Mencoder_Progress;
54+
mencoderAsync.StartEncodeAsync("./TestFiles/HelloWorld.avi", "./TestOutput.mp4");
55+
while (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(mencoderAsync.PathToExternalMencoderBin)).Length == 0)
56+
{
57+
Task.Delay(50).Wait();
58+
}
59+
}
60+
AssertVeraPdfGotDisposed();
61+
}
62+
4663
[Fact]
4764
public void ShouldStartEncodeAsyncAndCancle()
4865
{
@@ -75,20 +92,21 @@ public void ShouldStartEncodeAsyncAndCancle()
7592
[Fact]
7693
public void ShouldStartEncodeSync()
7794
{
78-
asyncTaskRunning = false;
79-
using (var mencoderAsync = new MencoderSharp.MencoderAsync())
95+
using (var mencoderSync = new MencoderSharp.Mencoder())
8096
{
81-
mencoderAsync.Finished += Mencoder_Finished;
82-
mencoderAsync.ProgressChanged += Mencoder_Progress;
83-
mencoderAsync.StartEncodeAsync("./TestFiles/small.mp4", "./SmallTestOutput.mp4");
84-
pathToExternalMencoderBin = mencoderAsync.PathToExternalMencoderBin;
85-
asyncTaskRunning = true;
86-
while (asyncTaskRunning)
87-
{
88-
Task.Delay(1000);
89-
}
90-
Assert.InRange(progress, 1, 100);
91-
Assert.True(mencoderAsync.Result.ExecutionWasSuccessfull, mencoderAsync.Result.StandardError);
97+
Assert.True(mencoderSync.Mencode("./TestFiles/small.mp4", "./SmallTestOutput.mp4", "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:global_header:threads=auto", "-oac mp3lame"));
98+
pathToExternalMencoderBin = mencoderSync.PathToExternalMencoderBin;
99+
}
100+
AssertVeraPdfGotDisposed();
101+
}
102+
103+
[Fact]
104+
public void ShouldDetectEncodeSyncFail()
105+
{
106+
using (var mencoderSync = new MencoderSharp.Mencoder())
107+
{
108+
Assert.False(mencoderSync.Mencode("./TestFiles/DoesNotExist", "./SmallTestOutput.mp4", "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:global_header:threads=auto", "-oac mp3lame"));
109+
pathToExternalMencoderBin = mencoderSync.PathToExternalMencoderBin;
92110
}
93111
AssertVeraPdfGotDisposed();
94112
}

0 commit comments

Comments
 (0)