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 ...@@ -50,7 +50,7 @@ namespace VideoLAN.LibVLC
* libvlc_exception_t: structure for unmanaged LibVLC exceptions * libvlc_exception_t: structure for unmanaged LibVLC exceptions
*/ */
[StructLayout (LayoutKind.Sequential)] [StructLayout (LayoutKind.Sequential)]
internal sealed class NativeException : IDisposable public sealed class NativeException : IDisposable
{ {
int raised; int raised;
int code; int code;
......
...@@ -26,8 +26,25 @@ using System.Runtime.InteropServices; ...@@ -26,8 +26,25 @@ using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC 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 */ /** Safe handle for unmanaged LibVLC instance pointer */
internal sealed class InstanceHandle : NonNullHandle public sealed class InstanceHandle : NonNullHandle
{ {
private InstanceHandle () private InstanceHandle ()
{ {
...@@ -38,35 +55,19 @@ namespace VideoLAN.LibVLC ...@@ -38,35 +55,19 @@ namespace VideoLAN.LibVLC
InstanceHandle Create (int argc, U8String[] argv, NativeException ex); InstanceHandle Create (int argc, U8String[] argv, NativeException ex);
[DllImport ("libvlc-control.dll", EntryPoint="libvlc_destroy")] [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 () protected override bool ReleaseHandle ()
{ {
Destroy (this, null); Destroy (handle, null);
return true; return true;
} }
}; };
public class VLCInstance : IDisposable public class Instance : BaseObject<InstanceHandle>
{ {
NativeException ex; internal Instance (InstanceHandle self) : base (self)
InstanceHandle self;
public VLCInstance (string[] args)
{ {
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) public MediaDescriptor CreateDescriptor (string mrl)
...@@ -80,7 +81,7 @@ namespace VideoLAN.LibVLC ...@@ -80,7 +81,7 @@ namespace VideoLAN.LibVLC
}; };
/** Safe handle for unmanaged LibVLC media descriptor */ /** Safe handle for unmanaged LibVLC media descriptor */
internal sealed class DescriptorHandle : NonNullHandle public sealed class DescriptorHandle : NonNullHandle
{ {
private DescriptorHandle () private DescriptorHandle ()
{ {
...@@ -94,24 +95,19 @@ namespace VideoLAN.LibVLC ...@@ -94,24 +95,19 @@ namespace VideoLAN.LibVLC
[DllImport ("libvlc-control.dll", [DllImport ("libvlc-control.dll",
EntryPoint="libvlc_media_descriptor_release")] EntryPoint="libvlc_media_descriptor_release")]
public static extern void Release (DescriptorHandle ptr); public static extern void Release (IntPtr ptr);
protected override bool ReleaseHandle () protected override bool ReleaseHandle ()
{ {
Release (this); Release (handle);
return true; return true;
} }
}; };
public class MediaDescriptor public class MediaDescriptor : BaseObject<DescriptorHandle>
{ {
NativeException ex; internal MediaDescriptor (DescriptorHandle self) : base (self)
DescriptorHandle self;
internal MediaDescriptor (DescriptorHandle self)
{ {
this.self = self;
ex = new NativeException ();
} }
}; };
}; };
...@@ -30,7 +30,7 @@ namespace VideoLAN.LibVLC ...@@ -30,7 +30,7 @@ namespace VideoLAN.LibVLC
* Abstract safe handle class for non-NULL pointers * Abstract safe handle class for non-NULL pointers
* (Microsoft.* namespace has a similar class, but lets stick to System.*) * (Microsoft.* namespace has a similar class, but lets stick to System.*)
*/ */
internal abstract class NonNullHandle : SafeHandle public abstract class NonNullHandle : SafeHandle
{ {
protected NonNullHandle () protected NonNullHandle ()
: base (IntPtr.Zero, true) : base (IntPtr.Zero, true)
...@@ -45,4 +45,22 @@ namespace VideoLAN.LibVLC ...@@ -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 @@ ...@@ -24,13 +24,19 @@
using System; using System;
using VideoLAN.LibVLC; using VideoLAN.LibVLC;
namespace VideoLAN.VLC namespace VideoLAN.LibVLC.Test
{ {
public sealed class Test public sealed class Test
{ {
public static int Main (string[] args) 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; 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