Commit 77015ab4 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Add support for allocating media_descriptors and factorize some code (generics yum yum!).

Destroying the instance object while a descriptor is still alive will crash(!).
I wonder why libvlc_instance_t is not reference counted as the other handler types... ?
parent 256878bc
......@@ -50,7 +50,7 @@ namespace VideoLAN.LibVLC
* libvlc_exception_t: structure for unmanaged LibVLC exceptions
*/
[StructLayout (LayoutKind.Sequential)]
internal sealed class NativeException : IDisposable
public sealed class NativeException : IDisposable
{
int raised;
int code;
......
......@@ -26,8 +26,25 @@ using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
public sealed class VLC
{
public static Instance CreateInstance (string[] args)
{
U8String[] argv = new U8String[args.Length];
for (int i = 0; i < args.Length; i++)
argv[i] = new U8String (args[i]);
NativeException ex = new NativeException ();
InstanceHandle h = InstanceHandle.Create (argv.Length, argv, ex);
ex.Raise ();
return new Instance (h);
}
};
/** Safe handle for unmanaged LibVLC instance pointer */
internal sealed class InstanceHandle : NonNullHandle
public sealed class InstanceHandle : NonNullHandle
{
private InstanceHandle ()
{
......@@ -38,35 +55,19 @@ namespace VideoLAN.LibVLC
InstanceHandle Create (int argc, U8String[] argv, NativeException ex);
[DllImport ("libvlc-control.dll", EntryPoint="libvlc_destroy")]
static extern void Destroy (InstanceHandle ptr, NativeException ex);
static extern void Destroy (IntPtr ptr, NativeException ex);
protected override bool ReleaseHandle ()
{
Destroy (this, null);
Destroy (handle, null);
return true;
}
};
public class VLCInstance : IDisposable
public class Instance : BaseObject<InstanceHandle>
{
NativeException ex;
InstanceHandle self;
public VLCInstance (string[] args)
internal Instance (InstanceHandle self) : base (self)
{
U8String[] argv = new U8String[args.Length];
for (int i = 0; i < args.Length; i++)
argv[i] = new U8String (args[i]);
ex = new NativeException ();
self = InstanceHandle.Create (argv.Length, argv, ex);
ex.Raise ();
}
public void Dispose ()
{
ex.Dispose ();
self.Close ();
}
public MediaDescriptor CreateDescriptor (string mrl)
......@@ -80,7 +81,7 @@ namespace VideoLAN.LibVLC
};
/** Safe handle for unmanaged LibVLC media descriptor */
internal sealed class DescriptorHandle : NonNullHandle
public sealed class DescriptorHandle : NonNullHandle
{
private DescriptorHandle ()
{
......@@ -94,24 +95,19 @@ namespace VideoLAN.LibVLC
[DllImport ("libvlc-control.dll",
EntryPoint="libvlc_media_descriptor_release")]
public static extern void Release (DescriptorHandle ptr);
public static extern void Release (IntPtr ptr);
protected override bool ReleaseHandle ()
{
Release (this);
Release (handle);
return true;
}
};
public class MediaDescriptor
public class MediaDescriptor : BaseObject<DescriptorHandle>
{
NativeException ex;
DescriptorHandle self;
internal MediaDescriptor (DescriptorHandle self)
internal MediaDescriptor (DescriptorHandle self) : base (self)
{
this.self = self;
ex = new NativeException ();
}
};
};
......@@ -30,7 +30,7 @@ namespace VideoLAN.LibVLC
* Abstract safe handle class for non-NULL pointers
* (Microsoft.* namespace has a similar class, but lets stick to System.*)
*/
internal abstract class NonNullHandle : SafeHandle
public abstract class NonNullHandle : SafeHandle
{
protected NonNullHandle ()
: base (IntPtr.Zero, true)
......@@ -45,4 +45,22 @@ namespace VideoLAN.LibVLC
}
}
};
public class BaseObject<HandleT> : IDisposable where HandleT : SafeHandle
{
protected NativeException ex;
protected HandleT self;
internal BaseObject (HandleT self)
{
this.self = self;
ex = new NativeException ();
}
public void Dispose ()
{
ex.Dispose ();
self.Close ();
}
};
};
......@@ -24,13 +24,19 @@
using System;
using VideoLAN.LibVLC;
namespace VideoLAN.VLC
namespace VideoLAN.LibVLC.Test
{
public sealed class Test
{
public static int Main (string[] args)
{
VLCInstance vlc = new VLCInstance (args);
string[] argv = new string[3]{ "-vvv", "-I", "dummy" };
Instance vlc = VLC.CreateInstance (argv);
MediaDescriptor md = vlc.CreateDescriptor (args[0]);
md.Dispose ();
vlc.Dispose ();
return 0;
}
};
......
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