Changeset 77aa5b9ebb86cc16a4b57f2f1316ede93b56c188
- Timestamp:
- 21/10/07 13:44:56 (1 year ago)
- git-parent:
- Files:
-
- bindings/cil/Makefile (modified) (2 diffs)
- bindings/cil/exception.cs (modified) (2 diffs)
- bindings/cil/libvlc.cs (modified) (1 diff)
- bindings/cil/marshal.cs (modified) (2 diffs)
- bindings/cil/testvlc.cs (modified) (2 diffs)
- bindings/cil/ustring.cs (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
bindings/cil/Makefile
rcd6d499 r77aa5b9 1 CS = gmcs 1 CS = gmcs -codepage:utf8 2 2 CSFLAGS = 3 3 … … 7 7 8 8 clean: 9 rm -f -- $(TARGETS) *.netmodule 9 rm -f -- $(TARGETS) *.netmodule *~ 10 10 11 VideoLAN.VLC.Control.dll: marshal.cs libvlc.cs exception.cs11 VideoLAN.VLC.Control.dll: marshal.cs ustring.cs exception.cs libvlc.cs 12 12 testvlc.exe: testvlc.cs VideoLAN.VLC.Control.dll 13 13 bindings/cil/exception.cs
rcd6d499 r77aa5b9 23 23 24 24 using System; 25 using System.Runtime.InteropServices; 25 26 26 namespace VideoLAN. VLC27 namespace VideoLAN.LibVLC 27 28 { 28 29 /** 29 * Base class for managedLibVLC exceptions30 * VLCException: managed base class for LibVLC exceptions 30 31 */ 31 public class MediaException : Exception32 public class VLCException : Exception 32 33 { 33 public MediaException ()34 public VLCException () 34 35 { 35 36 } 36 37 37 public MediaException (string message)38 public VLCException (string message) 38 39 : base (message) 39 40 { 40 41 } 41 42 42 public MediaException (string message, Exception inner)43 public VLCException (string message, Exception inner) 43 44 : base (message, inner) 44 45 { … … 46 47 }; 47 48 48 public class PositionKeyNotSupportedException : MediaException 49 /** 50 * libvlc_exception_t: structure for unmanaged LibVLC exceptions 51 */ 52 [StructLayout (LayoutKind.Sequential)] 53 internal sealed class NativeException : IDisposable 49 54 { 50 public PositionKeyNotSupportedException () 55 int raised; 56 int code; 57 IntPtr message; 58 59 [DllImport ("libvlc-control.dll", EntryPoint="libvlc_exception_init")] 60 static extern void Init (NativeException e); 61 [DllImport ("libvlc-control.dll", EntryPoint="libvlc_exception_clear")] 62 static extern void Clear (NativeException e); 63 /*[DllImport ("libvlc-control.dll", 64 EntryPoint="libvlc_exception_raised")] 65 static extern int Raised (NativeException e);*/ 66 [DllImport ("libvlc-control.dll", 67 EntryPoint="libvlc_exception_get_message")] 68 static extern IntPtr GetMessage (NativeException e); 69 70 public NativeException () 51 71 { 72 Init (this); 52 73 } 53 74 54 public PositionKeyNotSupportedException (string message) 55 : base (message) 75 public void Raise () 56 76 { 77 try 78 { 79 string msg = U8String.FromNative (GetMessage (this)); 80 if (msg != null) 81 throw new VLCException (msg); 82 } 83 finally 84 { 85 Clear (this); 86 } 57 87 } 58 88 59 public PositionKeyNotSupportedException (string message, Exception inner) 60 : base (message, inner) 89 public void Dispose () 61 90 { 62 } 63 }; 64 65 public class PositionOriginNotSupportedException : MediaException 66 { 67 public PositionOriginNotSupportedException () 68 { 69 } 70 71 public PositionOriginNotSupportedException (string message) 72 : base (message) 73 { 74 } 75 76 public PositionOriginNotSupportedException (string message, Exception inner) 77 : base (message, inner) 78 { 79 } 80 }; 81 82 public class InvalidPositionException : MediaException 83 { 84 public InvalidPositionException () 85 { 86 } 87 88 public InvalidPositionException (string message) 89 : base (message) 90 { 91 } 92 93 public InvalidPositionException (string message, Exception inner) 94 : base (message, inner) 95 { 96 } 97 }; 98 99 public class PlaylistException : MediaException 100 { 101 public PlaylistException () 102 { 103 } 104 105 public PlaylistException (string message) 106 : base (message) 107 { 108 } 109 110 public PlaylistException (string message, Exception inner) 111 : base (message, inner) 112 { 113 } 114 }; 115 116 public class InternalException : MediaException 117 { 118 public InternalException () 119 { 120 } 121 122 public InternalException (string message) 123 : base (message) 124 { 125 } 126 127 public InternalException (string message, Exception inner) 128 : base (message, inner) 129 { 91 Clear (this); 130 92 } 131 93 }; bindings/cil/libvlc.cs
r4931a92 r77aa5b9 25 25 using System.Runtime.InteropServices; 26 26 27 namespace VideoLAN. VLC27 namespace VideoLAN.LibVLC 28 28 { 29 30 public class MediaControl : IDisposable29 /** Safe handle for unmanaged LibVLC instance pointer */ 30 internal sealed class InstanceHandle : NonNullHandle 31 31 { 32 /** 33 * Possible player status 34 */ 35 enum PlayerStatus 32 private InstanceHandle () 36 33 { 37 PlayingStatus,38 PauseStatus,39 ForwardStatus,40 BackwardStatus,41 InitStatus,42 EndStatus,43 UndefinedStatus,44 };45 46 enum PositionOrigin47 {48 AbsolutePosition,49 RelativePosition,50 ModuloPosition,51 };52 53 enum PositionKey54 {55 ByteCount,56 SampleCount,57 MediaTime,58 };59 60 MediaControlHandle self;61 62 private void CheckDisposed ()63 {64 if (self.IsInvalid)65 throw new ObjectDisposedException ("Media controlled disposed");66 34 } 67 35 68 /** 69 * Creates a MediaControl with a new LibVLC instance 70 */ 71 public MediaControl (string[] args) 36 [DllImport ("libvlc-control.dll", EntryPoint="libvlc_new")] 37 public static extern 38 InstanceHandle Create (int argc, U8String[] argv, NativeException ex); 39 40 [DllImport ("libvlc-control.dll", EntryPoint="libvlc_destroy")] 41 static extern void Destroy (InstanceHandle ptr, NativeException ex); 42 43 protected override bool ReleaseHandle () 72 44 { 73 NativeException e = NativeException.Prepare (); 45 Destroy (this, null); 46 return true; 47 } 48 }; 74 49 50 public class VLCInstance : IDisposable 51 { 52 NativeException ex; 53 InstanceHandle self; 54 55 public VLCInstance (string[] args) 56 { 75 57 U8String[] argv = new U8String[args.Length]; 76 58 for (int i = 0; i < args.Length; i++) 77 59 argv[i] = new U8String (args[i]); 78 60 79 self = MediaControlAPI.New (argv.Length, argv, ref e); 80 e.Consume (); 81 } 82 83 /** 84 * Creates a MediaControl from an existing LibVLC instance 85 */ 86 /*public MediaControl (MediaControl instance) 87 { 88 NativeException e = NativeException.Prepare (); 89 IntPtr libvlc = mediacontrol_get_libvlc_instance (instance.self); 90 91 self = mediacontrol_new_from_instance (libvlc, ref e); 92 e.Consume (); 93 }*/ 94 95 /*public void Play (from) 96 { 97 CheckDisposed (); 98 throw new NotImplementedException (); 99 }*/ 100 101 public void Resume () 102 { 103 CheckDisposed (); 104 NativeException e = NativeException.Prepare (); 105 106 MediaControlAPI.Resume (self, IntPtr.Zero, ref e); 107 e.Consume (); 108 } 109 110 public void Pause () 111 { 112 CheckDisposed (); 113 NativeException e = NativeException.Prepare (); 114 115 MediaControlAPI.Pause (self, IntPtr.Zero, ref e); 116 e.Consume (); 117 } 118 119 public void Stop () 120 { 121 CheckDisposed (); 122 123 NativeException e = NativeException.Prepare (); 124 MediaControlAPI.Stop (self, IntPtr.Zero, ref e); 125 e.Consume (); 126 } 127 128 public void AddItem (string mrl) 129 { 130 CheckDisposed (); 131 132 U8String nmrl = new U8String (mrl); 133 NativeException e = NativeException.Prepare (); 134 MediaControlAPI.PlaylistAddItem (self, nmrl, ref e); 135 e.Consume (); 136 } 137 138 public void Clear () 139 { 140 CheckDisposed (); 141 142 NativeException e = NativeException.Prepare (); 143 MediaControlAPI.PlaylistClear (self, ref e); 144 e.Consume (); 145 } 146 147 public string[] Playlist 148 { 149 get 150 { 151 CheckDisposed (); 152 throw new NotImplementedException (); 153 } 154 } 155 156 /** 157 * Switches to the next playlist item 158 */ 159 public void NextItem () 160 { 161 CheckDisposed (); 162 163 NativeException e = NativeException.Prepare (); 164 MediaControlAPI.PlaylistNextItem (self, ref e); 165 e.Consume (); 166 } 167 168 /** 169 * Normalized audio output volume in percent (must be [0..100]). 170 */ 171 public short SoundVolume 172 { 173 get 174 { 175 CheckDisposed (); 176 177 NativeException e = NativeException.Prepare (); 178 short vol = MediaControlAPI.SoundGetVolume (self, ref e); 179 e.Consume (); 180 return vol; 181 } 182 set 183 { 184 CheckDisposed (); 185 186 if ((value < 0) || (value > 100)) 187 throw new ArgumentOutOfRangeException ("Volume not within [0..100]"); 188 189 NativeException e = NativeException.Prepare (); 190 MediaControlAPI.SoundSetVolume (self, value, ref e); 191 e.Consume (); 192 } 193 } 194 195 /** 196 * Performance speed rate in percent. 197 */ 198 public int Rate 199 { 200 get 201 { 202 CheckDisposed (); 203 204 NativeException e = NativeException.Prepare (); 205 int rate = MediaControlAPI.GetRate (self, ref e); 206 e.Consume (); 207 return rate; 208 } 209 set 210 { 211 CheckDisposed (); 212 213 NativeException e = NativeException.Prepare (); 214 MediaControlAPI.SetRate (self, value, ref e); 215 e.Consume (); 216 } 217 } 218 219 /** 220 * Fullscreen flag. 221 */ 222 public bool Fullscreen 223 { 224 get 225 { 226 CheckDisposed (); 227 228 NativeException e = NativeException.Prepare (); 229 int ret = MediaControlAPI.GetFullscreen (self, ref e); 230 e.Consume (); 231 return ret != 0; 232 } 233 set 234 { 235 CheckDisposed (); 236 237 NativeException e = NativeException.Prepare (); 238 MediaControlAPI.SetFullscreen (self, value ? 1 : 0, ref e); 239 e.Consume (); 240 } 61 ex = new NativeException (); 62 self = InstanceHandle.Create (argv.Length, argv, ex); 63 ex.Raise (); 241 64 } 242 65 243 66 public void Dispose () 244 67 { 245 self.Dispose (); 68 ex.Dispose (); 69 self.Close (); 70 } 71 72 public MediaDescriptor CreateDescriptor (string mrl) 73 { 74 U8String umrl = new U8String (mrl); 75 DescriptorHandle dh = DescriptorHandle.Create (self, umrl, ex); 76 ex.Raise (); 77 78 return new MediaDescriptor (dh); 79 } 80 }; 81 82 /** Safe handle for unmanaged LibVLC media descriptor */ 83 internal sealed class DescriptorHandle : NonNullHandle 84 { 85 private DescriptorHandle () 86 { 87 } 88 89 [DllImport ("libvlc-control.dll", 90 EntryPoint="libvlc_media_descriptor_new")] 91 public static extern 92 DescriptorHandle Create (InstanceHandle inst, U8String mrl, 93 NativeException ex); 94 95 [DllImport ("libvlc-control.dll", 96 EntryPoint="libvlc_media_descriptor_release")] 97 public static extern void Release (DescriptorHandle ptr); 98 99 protected override bool ReleaseHandle () 100 { 101 Release (this); 102 return true; 103 } 104 }; 105 106 public class MediaDescriptor 107 { 108 NativeException ex; 109 DescriptorHandle self; 110 111 internal MediaDescriptor (DescriptorHandle self) 112 { 113 this.self = self; 114 ex = new NativeException (); 246 115 } 247 116 }; bindings/cil/marshal.cs
r4931a92 r77aa5b9 25 25 using System.Runtime.InteropServices; 26 26 27 namespace VideoLAN. VLC27 namespace VideoLAN.LibVLC 28 28 { 29 internal class MediaControlAPI30 {31 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_new")]32 public static extern MediaControlHandle New (int argc, U8String[] argv, ref NativeException e);33 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_exit")]34 public static extern void Exit (IntPtr self);35 36 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_start")]37 public static extern void Start (MediaControlHandle self, IntPtr pos, ref NativeException e);38 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_pause")]39 public static extern void Pause (MediaControlHandle self, IntPtr dummy, ref NativeException e);40 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_resume")]41 public static extern void Resume (MediaControlHandle self, IntPtr dummy, ref NativeException e);42 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_stop")]43 public static extern void Stop (MediaControlHandle self, IntPtr dummy, ref NativeException e);44 45 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_add_item")]46 public static extern void PlaylistAddItem (MediaControlHandle self, U8String mrl, ref NativeException e);47 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_clear")]48 public static extern void PlaylistClear (MediaControlHandle self, ref NativeException e);49 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_get_list")]50 public static extern IntPtr PlaylistGetList (MediaControlHandle self, ref NativeException e);51 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_playlist_next_item")]52 public static extern void PlaylistNextItem (MediaControlHandle self, ref NativeException e);53 54 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_sound_get_volume")]55 public static extern short SoundGetVolume (MediaControlHandle self, ref NativeException e);56 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_sound_set_volume")]57 public static extern void SoundSetVolume (MediaControlHandle self, short volume, ref NativeException e);58 /* SetVisual */59 60 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_get_rate")]61 public static extern short GetRate (MediaControlHandle self, ref NativeException e);62 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_set_rate")]63 public static extern void SetRate (MediaControlHandle self, int rate, ref NativeException e);64 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_get_fullscreen")]65 public static extern int GetFullscreen (MediaControlHandle self, ref NativeException e);66 [DllImport ("libvlc-control.dll", EntryPoint="mediacontrol_set_fullscreen")]67 public static extern void SetFullscreen (MediaControlHandle self, int full, ref NativeException e);68 };69 70 29 /** 71 30 * Abstract safe handle class for non-NULL pointers … … 83 42 get 84 43 { 85 return IsClosed || (handle == IntPtr.Zero); 86 } 87 } 88 }; 89 90 internal sealed class MediaControlHandle : NonNullHandle 91 { 92 private MediaControlHandle () 93 { 94 } 95 96 protected override bool ReleaseHandle () 97 { 98 MediaControlAPI.Exit (handle); 99 return true; 100 } 101 }; 102 103 /** 104 * Wrapper around native UTF-8 nul-terminated character arrays 105 */ 106 [StructLayout (LayoutKind.Sequential)] 107 internal sealed struct U8String 108 { 109 byte[] mb_str; 110 111 public U8String (string value) 112 { 113 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (value); 114 mb_str = new byte[bytes.Length + 1]; 115 Array.Copy (bytes, mb_str, bytes.Length); 116 mb_str[bytes.Length] = 0; 117 } 118 119 public U8String (IntPtr ptr) 120 { 121 if (ptr == IntPtr.Zero) 122 return; 123 124 int i = 0; 125 while (Marshal.ReadByte (ptr, i) != 0) 126 i++; 127 i++; 128 129 mb_str = new byte[i]; 130 Marshal.Copy (ptr, mb_str, 0, i); 131 } 132 133 public override string ToString () 134 { 135 if (mb_str == null) 136 return null; 137 138 byte[] bytes = new byte[mb_str.Length - 1]; 139 Array.Copy (mb_str, bytes, bytes.Length); 140 141 return System.Text.Encoding.UTF8.GetString (bytes); 142 } 143 }; 144 145 146 /** 147 * LibVLC native exception structure 148 */ 149 [StructLayout (LayoutKind.Sequential)] 150 internal sealed struct NativeException 151 { 152 public int code; 153 public IntPtr message; 154 155 public string Message 156 { 157 get 158 { 159 return new U8String (message).ToString (); 160 } 161 } 162 163 [DllImport ("libvlc-control.dll")] 164 static extern void mediacontrol_exception_init (ref NativeException e); 165 [DllImport ("libvlc-control.dll")] 166 static extern void mediacontrol_exception_cleanup (ref NativeException e); 167 168 public static NativeException Prepare () 169 { 170 NativeException e = new NativeException (); 171 mediacontrol_exception_init (ref e); 172 return e; 173 } 174 175 public void Consume () 176 { 177 try 178 { 179 Exception e; 180 string m = Message; 181 182 switch (this.code) 183 { 184 case 0: 185 e = null; 186 break; 187 case 1: 188 e = new PositionKeyNotSupportedException (m); 189 break; 190 case 2: 191 e = new PositionOriginNotSupportedException (m); 192 break; 193 case 3: 194 e = new InvalidPositionException (m); 195 break; 196 case 4: 197 e = new PlaylistException (m); 198 break; 199 case 5: 200 e = new InternalException (m); 201 break; 202 default: 203 e = new MediaException (m); 204 break; 205 } 206 if (e != null) 207 throw e; 208 } 209 finally 210 { 211 mediacontrol_exception_cleanup (ref this); 44 return handle == IntPtr.Zero; 212 45 } 213 46 } bindings/cil/testvlc.cs
r4931a92 r77aa5b9 23 23 24 24 using System; 25 using VideoLAN.LibVLC; 25 26 26 27 namespace VideoLAN.VLC … … 30 31 public static int Main (string[] args) 31 32 { 32 MediaControl mc = new MediaControl (args); 33 34 foreach (string s in args) 35 mc.AddItem (s); 36 37 Console.WriteLine ("Volume : {0}%", mc.SoundVolume); 38 Console.WriteLine ("Rate : {0}%", mc.Rate); 39 Console.WriteLine ("Fullscreen: {0}", mc.Fullscreen); 40 mc.Fullscreen = false; 41 42 /*mc.Play ();*/ 43 Console.ReadLine (); 44 45 mc.Stop (); 46 mc.Clear (); 47 mc.SoundVolume = 100; 48 mc.Rate = 100; 49 50 mc.Dispose (); 33 VLCInstance vlc = new VLCInstance (args); 51 34 return 0; 52 35 }
