Commit 103ad604 authored by Martin Finkel's avatar Martin Finkel

Add MediaPlayer and LibVLC overrides and tests

parent cc0619de
......@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LibVLCSharp.Shared;
using NUnit.Framework;
......@@ -9,7 +10,7 @@ using NUnit.Framework;
namespace LibVLCSharp.Tests
{
[TestFixture]
public class InstanceTests : BaseSetup
public class LibVLCTests : BaseSetup
{
[Test]
public void DisposeInstanceNativeRelease()
......@@ -140,5 +141,25 @@ namespace LibVLCSharp.Tests
var logs = File.ReadAllText(path);
Assert.True(logs.StartsWith("VLC media player"));
}
[Test]
public void DisposeLibVLC()
{
var libvlc = new LibVLC();
libvlc.SetLog((data, logLevel, logContext, format, args) => { });
libvlc.SetDialogHandlers((title, text) => Task.CompletedTask,
(dialog, title, text, defaultUsername, askStore, token) => Task.CompletedTask,
(dialog, title, text, type, cancelText, firstActionText, secondActonText, token) => Task.CompletedTask,
(dialog, title, text, indeterminate, position, cancelText, token) => Task.CompletedTask,
(dialog, position, text) => Task.CompletedTask);
Assert.IsTrue(libvlc.DialogHandlersSet);
libvlc.Dispose();
Assert.AreEqual(IntPtr.Zero, libvlc.NativeReference);
Assert.IsFalse(libvlc.DialogHandlersSet);
}
}
}
\ No newline at end of file
......@@ -156,5 +156,20 @@ namespace LibVLCSharp.Tests
callCountRegisterOne++;
Debug.WriteLine($"Mp_Playing called with {callCountRegisterOne}");
}
[Test]
public async Task DisposeMediaPlayer()
{
var libvlc = new LibVLC();
var mp = new MediaPlayer(libvlc);
mp.Play(new Media(libvlc, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", Media.FromType.FromLocation));
await Task.Delay(1000);
mp.Dispose();
Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
}
}
}
......@@ -240,14 +240,25 @@ namespace LibVLCSharp.Shared
NativeToManagedMap[NativeReference] = this;
}
public override void Dispose()
protected override void Dispose(bool disposing)
{
if(_logCallback != null)
if (IsDisposed || NativeReference == IntPtr.Zero)
return;
if (disposing)
{
UnsetLog();
UnsetDialogHandlers();
base.Dispose();
UnsetDialogHandlers();
}
base.Dispose(disposing);
}
~LibVLC()
{
Dispose(false);
}
public static bool operator ==(LibVLC obj1, LibVLC obj2)
{
return obj1?.NativeReference == obj2?.NativeReference;
......@@ -328,9 +339,13 @@ namespace LibVLCSharp.Shared
/// </remarks>
public void UnsetLog()
{
if (_logCallback == null) return;
Native.LibVLCLogUnset(NativeReference);
if(!CloseLogFile())
throw new VLCException("Could not close log file");
_logCallback = null;
}
public void UnsetDialogHandlers()
......
......@@ -1995,6 +1995,32 @@ namespace LibVLCSharp.Shared
}
#endregion
protected override void Dispose(bool disposing)
{
if (IsDisposed || NativeReference == IntPtr.Zero)
return;
if(disposing)
{
if(IsPlaying)
{
Stop();
}
if(Media != null)
{
Media.Dispose();
}
}
base.Dispose(disposing);
}
~MediaPlayer()
{
Dispose(false);
}
}
/// <summary>Description for titles</summary>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment