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

Start rewriting the CIL bindings

parent 866faad6
......@@ -7,6 +7,8 @@ SOURCES_dll = \
ustring.cs \
exception.cs \
marshal.cs \
media.cs \
player.cs \
libvlc.cs
VideoLAN.LibVLC.dll: $(SOURCES_dll)
......
/*
* libvlc.cs - libvlc CIL bindings
*
* $Id$
/**
* @file exception.cs
* @brief LibVLC exceptions
* @ingroup API
*/
/**********************************************************************
* Copyright (C) 2007 Rémi Denis-Courmont. *
* Copyright (C) 2007-2009 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
......@@ -27,7 +27,8 @@ using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* VLCException: managed base class for LibVLC exceptions
* @brief VLCException: base class for LibVLC exceptions
* @ingroup API
*/
public class VLCException : Exception
{
......@@ -58,6 +59,10 @@ namespace VideoLAN.LibVLC
}
};
/**
* @section Internals
*/
/**
* libvlc_exception_t: structure for unmanaged LibVLC exceptions
*/
......
/**
* @file libvlc.cs
* @brief libvlc CIL bindings
* @brief Bindings to LibVLC for the .NET Common Intermediate Language
* @ingroup API
*
* $Id$
* @defgroup API Managed interface to LibVLC
* This is the primary class library for .NET applications
* to embed and control LibVLC.
*
* @defgroup Internals LibVLC internals
* This covers internal marshalling functions to use the native LibVLC.
* Only VLC developpers should need to read this section.
*/
/**********************************************************************
* Copyright (C) 2007 Rémi Denis-Courmont. *
* Copyright (C) 2007-2009 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
......@@ -23,327 +30,156 @@
**********************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* The VLC class is used to create LibVLC Instance objects.
* The VLC class has only one static method and cannot be instanciated.
*
* @code
* string[] argv = new string[]{ "-vvv", "-I", "dummy" };
*
* Instance vlc = VLC.CreateInstance (argv);
* @endcode
* @brief InstanceHandle: unmanaged LibVLC instance pointer
* @ingroup Internals
*/
public sealed class VLC
internal sealed class InstanceHandle : NonNullHandle
{
/**
* Loads native LibVLC and creates a LibVLC instance.
*
* @param args VLC command line parameters for the LibVLC Instance.
*
* @return a new LibVLC Instance
*/
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 ();
[DllImport ("libvlc.dll", EntryPoint="libvlc_get_version")]
public static extern IntPtr GetVersion ();
return new Instance (h);
}
};
[DllImport ("libvlc.dll", EntryPoint="libvlc_get_compiler")]
public static extern IntPtr GetCompiler ();
/**
* Safe handle for unmanaged LibVLC instance pointer.
*/
public sealed class InstanceHandle : NonNullHandle
{
private InstanceHandle ()
{
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_get_changeset")]
public static extern IntPtr GetChangeSet ();
[DllImport ("libvlc.dll", EntryPoint="libvlc_new")]
internal static extern
InstanceHandle Create (int argc, U8String[] argv, NativeException ex);
public static extern
InstanceHandle Create (int argc, U8String[] argv,
NativeException ex);
/*[DllImport ("libvlc.dll", EntryPoint="libvlc_retain")]
public static extern void Hold (InstanceHandle h,
NativeException ex);*/
[DllImport ("libvlc.dll", EntryPoint="libvlc_release")]
static extern void Destroy (IntPtr ptr, NativeException ex);
private static extern void Release (IntPtr h,
NativeException ex);
/**
* System.Runtime.InteropServices.SafeHandle::ReleaseHandle.
*/
protected override bool ReleaseHandle ()
[DllImport ("libvlc.dll", EntryPoint="libvlc_add_intf")]
public static extern void AddInterface (InstanceHandle h,
U8String name,
NativeException ex);
[DllImport ("libvlc.dll", EntryPoint="libvlc_wait")]
public static extern void Run (InstanceHandle h);
[DllImport ("libvlc.dll", EntryPoint="libvlc_get_vlc_instance")]
public static extern NonNullHandle GetVLC (InstanceHandle h);
protected override void Destroy ()
{
Destroy (handle, null);
return true;
Release (handle, null);
}
};
/**
* LibVLC Instance provides basic media player features from VLC,
* such as play/pause/stop and flat playlist management.
* @brief VLC: VLC media player instance
* @ingroup API
*
* The VLC class provides represent a run-time instance of a media player.
* An instance can spawn multiple independent medias, however
* configuration settings, message logging, etc are common to all medias
* from the same instance.
*/
public class Instance : BaseObject<InstanceHandle>
public class VLC : BaseObject
{
Dictionary<int, PlaylistItem> items;
internal Instance (InstanceHandle self) : base (self)
{
items = new Dictionary<int, PlaylistItem> ();
}
/**
* Creates a MediaDescriptor.
* @param mrl Media Resource Locator (file path or URL)
* @return create MediaDescriptor object.
*/
public MediaDescriptor CreateDescriptor (string mrl)
{
U8String umrl = new U8String (mrl);
DescriptorHandle dh = DescriptorHandle.Create (self, umrl, ex);
ex.Raise ();
return new MediaDescriptor (dh);
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_loop")]
static extern void PlaylistLoop (InstanceHandle self, bool b,
NativeException ex);
/** Sets the playlist loop flag. */
public bool Loop
{
set
{
PlaylistLoop (self, value, ex);
ex.Raise ();
}
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_play")]
static extern void PlaylistPlay (InstanceHandle self, int id, int optc,
U8String[] optv, NativeException ex);
/** Plays the next playlist item (if not already playing). */
public void Play ()
{
PlaylistPlay (self, -1, 0, new U8String[0], ex);
ex.Raise ();
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_pause")]
static extern void PlaylistPause (InstanceHandle self,
NativeException ex);
/** Toggles pause (starts playing if stopped, pauses if playing). */
public void TogglePause ()
{
PlaylistPause (self, ex);
ex.Raise ();
}
[DllImport ("libvlc.dll",
EntryPoint="libvlc_playlist_isplaying")]
static extern int PlaylistIsPlaying (InstanceHandle self,
NativeException ex);
/** Whether the playlist is running, or paused/stopped. */
public bool IsPlaying
internal InstanceHandle Handle
{
get
{
int ret = PlaylistIsPlaying (self, ex);
ex.Raise ();
return ret != 0;
return handle as InstanceHandle;
}
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_stop")]
static extern void PlaylistStop (InstanceHandle self,
NativeException ex);
/** Stops playing. */
public void Stop ()
{
PlaylistStop (self, ex);
ex.Raise ();
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_next")]
static extern void PlaylistNext (InstanceHandle self,
NativeException ex);
/** Switches to next playlist item, and starts playing it. */
public void Next ()
{
PlaylistNext (self, ex);
ex.Raise ();
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_prev")]
static extern void PlaylistPrev (InstanceHandle self,
NativeException ex);
/** Switches to previous playlist item, and starts playing it. */
public void Prev ()
{
PlaylistPrev (self, ex);
ex.Raise ();
}
[DllImport ("libvlc.dll", EntryPoint="libvlc_playlist_clear")]
static extern void PlaylistClear (InstanceHandle self,
NativeException ex);
/** Clears the whole playlist. */
public void Clear ()
/**
* Loads the native LibVLC and creates a LibVLC instance.
*
* @param args VLC command line parameters for the LibVLC Instance.
*/
public VLC (string[] args)
{
PlaylistClear (self, ex);
ex.Raise ();
U8String[] argv = new U8String[args.Length];
for (int i = 0; i < args.Length; i++)
argv[i] = new U8String (args[i]);
foreach (PlaylistItem item in items.Values)
item.Close ();
items.Clear ();
handle = InstanceHandle.Create (argv.Length, argv, ex);
Raise ();
}
[DllImport ("libvlc.dll",
EntryPoint="libvlc_playlist_add_extended")]
static extern int PlaylistAdd (InstanceHandle self, U8String uri,
U8String name, int optc,
U8String[] optv, NativeException e);
/**
* Appends an item to the playlist, with options.
* @param mrl Media Resource Locator (file name or URL)
* @param name playlist item user-visible name
* @param opts item options (see LibVLC documentation for details)
* @return created playlist item.
* Starts a VLC interface plugin.
*
* @param name name of the interface plugin (e.g. "http", "qt4", ...)
*/
public PlaylistItem Add (string mrl, string name, string[] opts)
public void AddInterface (string name)
{
U8String umrl = new U8String (mrl);
U8String uname = new U8String (name);
U8String[] optv = new U8String[opts.Length];
for (int i = 0; i < opts.Length; i++)
optv[i] = new U8String (opts[i]);
int id = PlaylistAdd (self, umrl, uname, optv.Length, optv, ex);
ex.Raise ();
PlaylistItem item = new PlaylistItem (id);
items.Add (id, item);
return item;
InstanceHandle.AddInterface (Handle, uname, ex);
Raise ();
}
/**
* Appends an item with options.
* @param mrl Media Resource Locator (file name or URL)
* @param opts item options (see LibVLC documentation for details)
* @return created playlist item.
* Waits until VLC instance exits. This can happen if a fatal error
* occurs (e.g. cannot parse the arguments), if the user has quit
* through an interface, or if the special vlc://quit item was played.
*/
public PlaylistItem Add (string mrl, string[] opts)
public void Run ()
{
return Add (mrl, null, opts);
InstanceHandle.Run (Handle);
}
/**
* Appends an item to the playlist.
* @param mrl Media Resource Locator (file name or URL)
* @param name playlist item user-visible name
* @return created playlist item.
* The human-readable LibVLC version number.
*/
public PlaylistItem Add (string mrl, string name)
public static string Version
{
return Add (mrl, name, new string[0]);
get
{
return U8String.FromNative (InstanceHandle.GetVersion ());
}
}
/**
* Appends an item to the playlist.
* @param mrl Media Resource Locator (file name or URL)
* @return created playlist item.
* The human-readable LibVLC C compiler infos.
*/
public PlaylistItem Add (string mrl)
public static string Compiler
{
return Add (mrl, null, new string[0]);
get
{
return U8String.FromNative (InstanceHandle.GetCompiler ());
}
}
[DllImport ("libvlc.dll",
EntryPoint="libvlc_playlist_delete_item")]
static extern int PlaylistDelete (InstanceHandle self, int id,
NativeException e);
/**
* Removes an item from the playlist.
* @param item playlist item (as obtained from Add())
* The unique commit identifier from the LibVLC source control system,
* or "exported" if unknown.
*/
public void Delete (PlaylistItem item)
{
int id = item.Id;
PlaylistDelete (self, id, ex);
ex.Raise ();
item.Close ();
items.Remove (id);
}
};
/**
* A playlist item.
*/
public class PlaylistItem
{
int id;
bool deleted;
internal PlaylistItem (int id)
{
this.id = id;
this.deleted = false;
}
internal void Close ()
{
deleted = true;
}
internal int Id
public static string ChangeSet
{
get
{
if (deleted)
throw new ObjectDisposedException ("Playlist item deleted");
return id;
return U8String.FromNative (InstanceHandle.GetChangeSet ());
}
}
};
/** Safe handle for unmanaged LibVLC media descriptor */
public sealed class DescriptorHandle : NonNullHandle
{
private DescriptorHandle ()
{
}
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_new")]
public static extern
DescriptorHandle Create (InstanceHandle inst, U8String mrl,
NativeException ex);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_release")]
public static extern void Release (IntPtr ptr);
protected override bool ReleaseHandle ()
{
Release (handle);
return true;
}
};
/**
* Media descriptor. Not implemented yet.
*/
public class MediaDescriptor : BaseObject<DescriptorHandle>
{
internal MediaDescriptor (DescriptorHandle self) : base (self)
/**
* The unmanaged VLC-internal instance object.
* Do not use this unless you really know what you are doing.
*/
public SafeHandle Object
{
get
{
return InstanceHandle.GetVLC (Handle);
}
}
};
};
/**
* @file marshal.cs
* @brief LibVLC marshalling utilities
*
* $Id$
* @brief Common LibVLC objects marshalling utilities
* @ingroup Internals
*/
/**********************************************************************
* Copyright (C) 2007 Rémi Denis-Courmont. *
* Copyright (C) 2007-2009 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
......@@ -28,17 +27,21 @@ using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* Abstract safe handle class for non-NULL pointers
* (Microsoft.* namespace has a similar class,
* but lets stick to System.*).
* @brief NonNullHandle: abstract safe handle class for non-NULL pointers
* @ingroup Internals
* Microsoft.* namespace has a similar class. However we want to use the
* System.* namespace only.
*/
public abstract class NonNullHandle : SafeHandle
internal abstract class NonNullHandle : SafeHandle
{
protected NonNullHandle ()
: base (IntPtr.Zero, true)
{
}
/**
* System.Runtime.InteropServices.SafeHandle::IsInvalid.
*/
public override bool IsInvalid
{
get
......@@ -46,26 +49,49 @@ namespace VideoLAN.LibVLC
return handle == IntPtr.Zero;
}
}
protected abstract void Destroy ();
/**
* System.Runtime.InteropServices.SafeHandle::ReleaseHandle.
*/
protected override bool ReleaseHandle ()
{
Destroy ();
return true;
}
};
/**
* Generic class for managed wrapper around a native safe handle.
* @brief BaseObject: generic wrapper around a safe handle.
* @ingroup Internals
* This is the baseline for all managed LibVLC objects which wrap
* an unmanaged LibVLC pointer.
*/
public class BaseObject<HandleT> : IDisposable where HandleT : SafeHandle
public class BaseObject : IDisposable
{
protected NativeException ex;
protected HandleT self;
protected NativeException ex; /**< buffer for LibVLC exceptions */
protected SafeHandle handle; /**< wrapped safe handle */
internal BaseObject (HandleT self)
internal BaseObject ()
{
this.self = self;
ex = new NativeException ();
this.handle = null;
}
protected void Raise ()
{
ex.Raise ();
}
/**
* IDisposable::Dispose.
*/
public void Dispose ()
{
ex.Dispose ();
self.Close ();
handle.Close ();
}
};
};
/**
* @file media.cs
* @brief Media descriptor class
* @ingroup API
*/
/**********************************************************************
* Copyright (C) 2007-2009 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
**********************************************************************/
using System;
//using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* @brief MediaHandle: unmanaged LibVLC media pointer
* @ingroup Internals
*/
internal sealed class MediaHandle : NonNullHandle
{
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_new")]
public static extern
MediaHandle Create (InstanceHandle inst, U8String mrl,
NativeException ex);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_release")]
private static extern void Release (IntPtr ptr);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_add_option")]
public static extern void AddOption (MediaHandle ptr, U8String options,
NativeException ex);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_add_option_untrusted")]
public static extern void AddUntrustedOption (MediaHandle ptr,
U8String options,
NativeException ex);
protected override void Destroy ()
{
Release (handle);
}
};
/**
* @brief Media: a source media
* Use this class to extract meta-informations from a media.
*/
public class Media : BaseObject
{
internal MediaHandle Handle
{
get
{
return handle as MediaHandle;
}
}
/**
* Creates a Media object.
*
* @param instance VLC instance
* @param mrl Media Resource Locator (file path or URL)
*/
public Media (VLC instance, string mrl)
{
U8String umrl = new U8String (mrl);
handle = MediaHandle.Create (instance.Handle, umrl, ex);
Raise ();
}
public void AddOptions (string options, bool trusted)
{
U8String uopts = new U8String (options);
if (trusted)
MediaHandle.AddOption (Handle, uopts, ex);
else
MediaHandle.AddUntrustedOption (Handle, uopts, ex);
Raise ();
}
};
};
/**
* @file player.cs
* @brief Media player class
* @ingroup API
*
* @defgroup API Managed interface to LibVLC
* This is the primary class library for .NET applications
* to embed and control LibVLC.
*
* @defgroup Internals LibVLC internals
* This covers internal marshalling functions to use the native LibVLC.
* Only VLC developpers should need to read this section.
*/
/**********************************************************************
* Copyright (C) 2009 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
**********************************************************************/
using System;
using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* @brief MediaPlayerHandle: unmanaged LibVLC media player pointer
* @ingroup Internals
*/
internal sealed class MediaPlayerHandle : NonNullHandle
{
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_player_new")]
internal static extern
MediaPlayerHandle Create (InstanceHandle inst, NativeException ex);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_player_new_from_media")]
internal static extern
MediaPlayerHandle Create (MediaHandle media, NativeException ex);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_player_release")]
internal static extern void Release (IntPtr ptr);
[DllImport ("libvlc.dll",
EntryPoint="libvlc_media_player_set_media")]
internal static extern
MediaPlayerHandle SetMedia (MediaPlayerHandle player,
MediaHandle media,
NativeException ex);
protected override void Destroy ()
{
Release (handle);
}
};
/**
* @brief MediaPlayer: a simple media player
* Use this class to play a media.
*/
public class MediaPlayer : BaseObject
{
internal MediaPlayerHandle Handle
{
get
{
return handle as MediaPlayerHandle;
}
}
Media media; /**< Active media */
/**
* The Media object that the MediaPlayer is using,
* or null if there is none.
*/
public Media Media
{
get
{
return media;
}
set
{
MediaHandle mh = (value != null) ? value.Handle : null;
MediaPlayerHandle.SetMedia (Handle, mh, null);
media = value;
}
}
/**
* Creates an empty MediaPlayer object.
* An input media will be needed before this media player can be used.
*
* @param instance VLC instance
*/
public MediaPlayer (VLC instance)
{
this.media = null;
handle = MediaPlayerHandle.Create (instance.Handle, ex);
ex.Raise ();
}
/**
* Creates a MediaPlayer object from a Media object.
* This allows playing the specified media.
*
* @param media media object
*/
public MediaPlayer (Media media)
{
this.media = media;
handle = MediaPlayerHandle.Create (media.Handle, ex);
ex.Raise ();
}
};
};
/*
* ustring.cs - Managed LibVLC string
*
* $Id$
/**
* @file ustring.cs
* @brief Managed LibVLC strings
* @ingroup Internals
*/
/**********************************************************************
......@@ -27,13 +27,20 @@ using System.Runtime.InteropServices;
namespace VideoLAN.LibVLC
{
/**
* Managed class for UTF-8 nul-terminated character arrays
* @brief U8String: Native UTF-8 characters array
* @ingroup Internals
* This supports conversion between native UTF-8 nul-terminated characters
* arrays (as used by the native LibVLC) and managed strings.
*/
[StructLayout (LayoutKind.Sequential)]
public struct U8String
{
public byte[] mb_str;
public byte[] mb_str; /**< nul-terminated UTF-8 bytes array */
/**
* Creates an UTF-8 characters array from a .NET string.
* @param value string to convert
*/
public U8String (string value)
{
mb_str = null;
......@@ -46,7 +53,7 @@ namespace VideoLAN.LibVLC
mb_str[bytes.Length] = 0;
}
public U8String (IntPtr ptr)
private U8String (IntPtr ptr)
{
mb_str = null;
if (ptr == IntPtr.Zero)
......@@ -61,6 +68,9 @@ namespace VideoLAN.LibVLC
Marshal.Copy (ptr, mb_str, 0, i);
}
/**
* Object::ToString.
*/
public override string ToString ()
{
if (mb_str == null)
......@@ -72,6 +82,10 @@ namespace VideoLAN.LibVLC
return System.Text.Encoding.UTF8.GetString (bytes);
}
/**
* Converts a pointer to a nul-terminated UTF-8 characters array into
* a managed string.
*/
public static string FromNative (IntPtr ptr)
{
return new U8String (ptr).ToString ();
......
......@@ -31,28 +31,20 @@ namespace VideoLAN.LibVLC.Test
public static int Main (string[] args)
{
string[] argv = new string[]{
"-vvv", "-I", "dummy", "--plugin-path=../../modules"
"-v", "-I", "dummy", "--plugin-path=../../modules"
};
Instance vlc = VLC.CreateInstance (argv);
MediaDescriptor md = vlc.CreateDescriptor ("/dev/null");
md.Dispose ();
Console.WriteLine("Running on VLC {0} ({1})", VLC.Version,
VLC.ChangeSet);
Console.WriteLine("Compiled with {0}", VLC.Compiler);
PlaylistItem item = null;
VLC vlc = new VLC (argv);
Media m = new Media (vlc, "/dev/null");
foreach (string s in args)
item = vlc.Add (s);
vlc.AddInterface ("qt4");
vlc.Run ();
vlc.Loop = false;
vlc.TogglePause ();
Console.ReadLine ();
vlc.Stop ();
if (item != null)
vlc.Delete (item);
vlc.Clear ();
Console.ReadLine ();
m.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