Changeset 77015ab46b9aff1d25ac3ca3c064893549fd4137
- Timestamp:
- 10/21/07 14:18:19
(10 months ago)
- Author:
- Rémi Denis-Courmont <rem@videolan.org>
- git-committer:
- Rémi Denis-Courmont <rem@videolan.org> 1192969099 +0000
- git-parent:
[256878bc4b10572511d4c96ac5ad1774482fcc56]
- git-author:
- Rémi Denis-Courmont <rem@videolan.org> 1192969099 +0000
- Message:
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... ?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r77aa5b9 |
r77015ab |
|
| 51 | 51 | */ |
|---|
| 52 | 52 | [StructLayout (LayoutKind.Sequential)] |
|---|
| 53 | | internal sealed class NativeException : IDisposable |
|---|
| | 53 | public sealed class NativeException : IDisposable |
|---|
| 54 | 54 | { |
|---|
| 55 | 55 | int raised; |
|---|
| r77aa5b9 |
r77015ab |
|
| 27 | 27 | namespace VideoLAN.LibVLC |
|---|
| 28 | 28 | { |
|---|
| | 29 | public sealed class VLC |
|---|
| | 30 | { |
|---|
| | 31 | public static Instance CreateInstance (string[] args) |
|---|
| | 32 | { |
|---|
| | 33 | U8String[] argv = new U8String[args.Length]; |
|---|
| | 34 | for (int i = 0; i < args.Length; i++) |
|---|
| | 35 | argv[i] = new U8String (args[i]); |
|---|
| | 36 | |
|---|
| | 37 | NativeException ex = new NativeException (); |
|---|
| | 38 | |
|---|
| | 39 | InstanceHandle h = InstanceHandle.Create (argv.Length, argv, ex); |
|---|
| | 40 | ex.Raise (); |
|---|
| | 41 | |
|---|
| | 42 | return new Instance (h); |
|---|
| | 43 | } |
|---|
| | 44 | }; |
|---|
| | 45 | |
|---|
| 29 | 46 | /** Safe handle for unmanaged LibVLC instance pointer */ |
|---|
| 30 | | internal sealed class InstanceHandle : NonNullHandle |
|---|
| | 47 | public sealed class InstanceHandle : NonNullHandle |
|---|
| 31 | 48 | { |
|---|
| 32 | 49 | private InstanceHandle () |
|---|
| … | … | |
| 39 | 56 | |
|---|
| 40 | 57 | [DllImport ("libvlc-control.dll", EntryPoint="libvlc_destroy")] |
|---|
| 41 | | static extern void Destroy (InstanceHandle ptr, NativeException ex); |
|---|
| | 58 | static extern void Destroy (IntPtr ptr, NativeException ex); |
|---|
| 42 | 59 | |
|---|
| 43 | 60 | protected override bool ReleaseHandle () |
|---|
| 44 | 61 | { |
|---|
| 45 | | Destroy (this, null); |
|---|
| | 62 | Destroy (handle, null); |
|---|
| 46 | 63 | return true; |
|---|
| 47 | 64 | } |
|---|
| 48 | 65 | }; |
|---|
| 49 | 66 | |
|---|
| 50 | | public class VLCInstance : IDisposable |
|---|
| | 67 | public class Instance : BaseObject<InstanceHandle> |
|---|
| 51 | 68 | { |
|---|
| 52 | | NativeException ex; |
|---|
| 53 | | InstanceHandle self; |
|---|
| 54 | | |
|---|
| 55 | | public VLCInstance (string[] args) |
|---|
| | 69 | internal Instance (InstanceHandle self) : base (self) |
|---|
| 56 | 70 | { |
|---|
| 57 | | U8String[] argv = new U8String[args.Length]; |
|---|
| 58 | | for (int i = 0; i < args.Length; i++) |
|---|
| 59 | | argv[i] = new U8String (args[i]); |
|---|
| 60 | | |
|---|
| 61 | | ex = new NativeException (); |
|---|
| 62 | | self = InstanceHandle.Create (argv.Length, argv, ex); |
|---|
| 63 | | ex.Raise (); |
|---|
| 64 | | } |
|---|
| 65 | | |
|---|
| 66 | | public void Dispose () |
|---|
| 67 | | { |
|---|
| 68 | | ex.Dispose (); |
|---|
| 69 | | self.Close (); |
|---|
| 70 | 71 | } |
|---|
| 71 | 72 | |
|---|
| … | … | |
| 81 | 82 | |
|---|
| 82 | 83 | /** Safe handle for unmanaged LibVLC media descriptor */ |
|---|
| 83 | | internal sealed class DescriptorHandle : NonNullHandle |
|---|
| | 84 | public sealed class DescriptorHandle : NonNullHandle |
|---|
| 84 | 85 | { |
|---|
| 85 | 86 | private DescriptorHandle () |
|---|
| … | … | |
| 95 | 96 | [DllImport ("libvlc-control.dll", |
|---|
| 96 | 97 | EntryPoint="libvlc_media_descriptor_release")] |
|---|
| 97 | | public static extern void Release (DescriptorHandle ptr); |
|---|
| | 98 | public static extern void Release (IntPtr ptr); |
|---|
| 98 | 99 | |
|---|
| 99 | 100 | protected override bool ReleaseHandle () |
|---|
| 100 | 101 | { |
|---|
| 101 | | Release (this); |
|---|
| | 102 | Release (handle); |
|---|
| 102 | 103 | return true; |
|---|
| 103 | 104 | } |
|---|
| 104 | 105 | }; |
|---|
| 105 | 106 | |
|---|
| 106 | | public class MediaDescriptor |
|---|
| | 107 | public class MediaDescriptor : BaseObject<DescriptorHandle> |
|---|
| 107 | 108 | { |
|---|
| 108 | | NativeException ex; |
|---|
| 109 | | DescriptorHandle self; |
|---|
| 110 | | |
|---|
| 111 | | internal MediaDescriptor (DescriptorHandle self) |
|---|
| | 109 | internal MediaDescriptor (DescriptorHandle self) : base (self) |
|---|
| 112 | 110 | { |
|---|
| 113 | | this.self = self; |
|---|
| 114 | | ex = new NativeException (); |
|---|
| 115 | 111 | } |
|---|
| 116 | 112 | }; |
|---|
| r77aa5b9 |
r77015ab |
|
| 31 | 31 | * (Microsoft.* namespace has a similar class, but lets stick to System.*) |
|---|
| 32 | 32 | */ |
|---|
| 33 | | internal abstract class NonNullHandle : SafeHandle |
|---|
| | 33 | public abstract class NonNullHandle : SafeHandle |
|---|
| 34 | 34 | { |
|---|
| 35 | 35 | protected NonNullHandle () |
|---|
| … | … | |
| 46 | 46 | } |
|---|
| 47 | 47 | }; |
|---|
| | 48 | |
|---|
| | 49 | public class BaseObject<HandleT> : IDisposable where HandleT : SafeHandle |
|---|
| | 50 | { |
|---|
| | 51 | protected NativeException ex; |
|---|
| | 52 | protected HandleT self; |
|---|
| | 53 | |
|---|
| | 54 | internal BaseObject (HandleT self) |
|---|
| | 55 | { |
|---|
| | 56 | this.self = self; |
|---|
| | 57 | ex = new NativeException (); |
|---|
| | 58 | } |
|---|
| | 59 | |
|---|
| | 60 | public void Dispose () |
|---|
| | 61 | { |
|---|
| | 62 | ex.Dispose (); |
|---|
| | 63 | self.Close (); |
|---|
| | 64 | } |
|---|
| | 65 | }; |
|---|
| 48 | 66 | }; |
|---|
| r77aa5b9 |
r77015ab |
|
| 25 | 25 | using VideoLAN.LibVLC; |
|---|
| 26 | 26 | |
|---|
| 27 | | namespace VideoLAN.VLC |
|---|
| | 27 | namespace VideoLAN.LibVLC.Test |
|---|
| 28 | 28 | { |
|---|
| 29 | 29 | public sealed class Test |
|---|
| … | … | |
| 31 | 31 | public static int Main (string[] args) |
|---|
| 32 | 32 | { |
|---|
| 33 | | VLCInstance vlc = new VLCInstance (args); |
|---|
| | 33 | string[] argv = new string[3]{ "-vvv", "-I", "dummy" }; |
|---|
| | 34 | |
|---|
| | 35 | Instance vlc = VLC.CreateInstance (argv); |
|---|
| | 36 | MediaDescriptor md = vlc.CreateDescriptor (args[0]); |
|---|
| | 37 | |
|---|
| | 38 | md.Dispose (); |
|---|
| | 39 | vlc.Dispose (); |
|---|
| 34 | 40 | return 0; |
|---|
| 35 | 41 | } |
|---|