]> git.sesse.net Git - vlc/commitdiff
Add support for allocating media_descriptors and factorize some code (generics yum...
authorRémi Denis-Courmont <rem@videolan.org>
Sun, 21 Oct 2007 12:18:19 +0000 (12:18 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Sun, 21 Oct 2007 12:18:19 +0000 (12:18 +0000)
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... ?

bindings/cil/exception.cs
bindings/cil/libvlc.cs
bindings/cil/marshal.cs
bindings/cil/testvlc.cs

index b1acf92c534716d7acce68855987c6a57196836e..1f72909fd5223909b68348bfcea01b10cb8533a3 100644 (file)
@@ -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;
index 9866c825767b40578787cd4a0ef47873cf50d075..0bd4cebf2ec527476ca8e375115d071a3c1a1675 100644 (file)
@@ -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 ();
         }
     };
 };
index 05920073c277b4fba0a10cea278aabd0c7a84c8f..c210283d04e34b0678db206a9d0d104afcb98429 100644 (file)
@@ -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 ();
+        }
+    };
 };
index 29645944a23b1a11a7c8923e467c69484663fc60..4fa204b55414c8e8bb5c8817775933892d734900 100644 (file)
 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;
         }
     };