织梦CMS - 轻松建站从此开始!

罗索

LibVLC.DLL之C#接口

jackyhwei 发布于 2011-08-26 20:31 点击:次 
落鹤生:这是一个利用C#来调用libVLC.dll的完整封装类代码。作者是Chris Meadowcroft。非常不错。
TAG:

/*****************************************************************************
* NativeLibVlc.cs: NativeLibVlc class definition
*****************************************************************************
* Copyright (C) 2006 Chris Meadowcroft
*
* Authors: Chris Meadowcroft
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace VLanControl
{
internal enum ObjectType : int
{
VLC_OBJECT_ROOT = (-1),
VLC_OBJECT_VLC = (-2),
VLC_OBJECT_MODULE = (-3),
VLC_OBJECT_INTF = (-4),
VLC_OBJECT_PLAYLIST = (-5),
VLC_OBJECT_ITEM = (-6),
VLC_OBJECT_INPUT = (-7),
VLC_OBJECT_DECODER = (-8),
VLC_OBJECT_VOUT = (-9),
VLC_OBJECT_AOUT = (-10),
VLC_OBJECT_SOUT = (-11),
VLC_OBJECT_HTTPD = (-12),
VLC_OBJECT_PACKETIZER = (-13),
VLC_OBJECT_ENCODER = (-14),
VLC_OBJECT_DIALOGS = (-15),
VLC_OBJECT_VLM = (-16),
VLC_OBJECT_ANNOUNCE = (-17),
VLC_OBJECT_DEMUX = (-18),
VLC_OBJECT_ACCESS = (-19),
VLC_OBJECT_STREAM = (-20),
VLC_OBJECT_OPENGL = (-21),
VLC_OBJECT_FILTER = (-22),
VLC_OBJECT_VOD = (-23),
VLC_OBJECT_SPU = (-24),
VLC_OBJECT_TLS = (-25),
VLC_OBJECT_SD = (-26),
VLC_OBJECT_XML = (-27),
VLC_OBJECT_OSDMENU = (-28),
VLC_OBJECT_STATS = (-29),
VLC_OBJECT_HTTPD_HOST = (-30),

VLC_OBJECT_GENERIC = (-666),
}

internal enum VlcError : int
{
Success = -0,
NoMem = -1,
Thread = -2,
Timeout = -3,

NoMod = -10,

NoObj = -20,
BadObj = -21,

NoVar = -30,
BadVar = -31,

Exit = -255,
Generic = -666,

Exception = -998,
NoInit = -999,
};

internal enum InputState : int
{
INIT_S = 0,
OPENING_S = 1,
BUFFERING_S = 2,
PLAYING_S = 3,
PAUSE_S = 4,
END_S = 5,
ERROR_S = 6,
}

internal class NativeLibVlc : IDisposable
{
#region private vlc enums

enum Mode : int
{
Insert = 0x01,
Replace = 0x02,
Append = 0x04,
Go = 0x08,
CheckInsert = 0x10
};

public const Int32 EndOfPlaylist = -666;

[Flags]
enum ObjectSearchMode : int
{
FIND_PARENT = 0x0001,
FIND_CHILD = 0x0002,
FIND_ANYWHERE = 0x0003,
FIND_STRICT = 0x0010,
}

[Flags]
enum VarFlags : int
{
/** /defgroup var_flags Additive flags
* These flags are added to the type field of the variable. Most as a result of
* a __var_Change() call, but some may be added at creation time
* @{
*/
VLC_VAR_HASCHOICE = 0x0100,
VLC_VAR_HASMIN = 0x0200,
VLC_VAR_HASMAX = 0x0400,
VLC_VAR_HASSTEP = 0x0800,

VLC_VAR_ISLIST = 0x1000,
VLC_VAR_ISCOMMAND = 0x2000,
VLC_VAR_ISCONFIG = 0x2000,

/** Creation flag */
VLC_VAR_DOINHERIT = 0x8000,

/**
* /defgroup var_action Variable actions
* These are the different actions that can be used with __var_Change().
* The parameters given are the meaning of the two last parameters of
* __var_Change() when this action is being used.
* @{
*/

/**
* Set the minimum value of this variable
* /param p_val The new minimum value
* /param p_val2 Unused
*/
VLC_VAR_SETMIN = 0x0010,
/**
* Set the maximum value of this variable
* /param p_val The new maximum value
* /param p_val2 Unused
*/
VLC_VAR_SETMAX = 0x0011,
VLC_VAR_SETSTEP = 0x0012,
/**
* Set the value of this variable without triggering any callbacks
* /param p_val The new value
* /param p_val2 Unused
*/
VLC_VAR_SETVALUE = 0x0013,

VLC_VAR_SETTEXT = 0x0014,
VLC_VAR_GETTEXT = 0x0015,

VLC_VAR_ADDCHOICE = 0x0020,
VLC_VAR_DELCHOICE = 0x0021,
VLC_VAR_CLEARCHOICES = 0x0022,
VLC_VAR_SETDEFAULT = 0x0023,
VLC_VAR_GETCHOICES = 0x0024,
VLC_VAR_FREECHOICES = 0x0025,
VLC_VAR_GETLIST = 0x0026,
VLC_VAR_FREELIST = 0x0027,
VLC_VAR_CHOICESCOUNT = 0x0028,

VLC_VAR_INHERITVALUE = 0x0030,
VLC_VAR_TRIGGER_CALLBACKS = 0x0035,
}

enum playlist_command : int
{
PLAYLIST_PLAY = 0, /**< No arg. res=can fail*/
PLAYLIST_AUTOPLAY = 1, /**< No arg. res=cant fail*/
PLAYLIST_VIEWPLAY = 2, /**< arg1= int, arg2= playlist_item_t*,*/
/** arg3 = playlist_item_t* , res=can fail */
PLAYLIST_ITEMPLAY = 3, /** <arg1 = playlist_item_t * , res=can fail */
PLAYLIST_PAUSE = 4, /**< No arg res=can fail*/
PLAYLIST_STOP = 5, /**< No arg res=can fail*/
PLAYLIST_SKIP = 6, /**< arg1=int, res=can fail*/
PLAYLIST_GOTO = 7, /**< arg1=int res=can fail */
PLAYLIST_VIEWGOTO = 8 /**< arg1=int res=can fail */
}

enum CONFIG_ITEM : int
{
CONFIG_ITEM_STRING = 0x0010,
CONFIG_ITEM_FILE = 0x0020,
CONFIG_ITEM_MODULE = 0x0030,
CONFIG_ITEM_INTEGER = 0x0040,
CONFIG_ITEM_BOOL = 0x0050,
CONFIG_ITEM_FLOAT = 0x0060,
}

enum input_query_e : int
{
/* input variable "position" */
INPUT_GET_POSITION = 0, /* arg1= double * res= */
INPUT_SET_POSITION, /* arg1= double res=can fail */

/* input variable "length" */
INPUT_GET_LENGTH, /* arg1= int64_t * res=can fail */

/* input variable "time" */
INPUT_GET_TIME, /* arg1= int64_t * res= */
INPUT_SET_TIME, /* arg1= int64_t res=can fail */

/* input variable "rate" (1 is DEFAULT_RATE) */
INPUT_GET_RATE, /* arg1= int * res= */
INPUT_SET_RATE, /* arg1= int res=can fail */

/* input variable "state" */
INPUT_GET_STATE, /* arg1= int * res= */
INPUT_SET_STATE, /* arg1= int res=can fail */

/* input variable "audio-delay" and "sub-delay" */
INPUT_GET_AUDIO_DELAY, /* arg1 = int* res=can fail */
INPUT_SET_AUDIO_DELAY, /* arg1 = int res=can fail */
INPUT_GET_SPU_DELAY, /* arg1 = int* res=can fail */
INPUT_SET_SPU_DELAY, /* arg1 = int res=can fail */

/* Meta datas */
INPUT_ADD_INFO, /* arg1= char* arg2= char* arg3=... res=can fail */
INPUT_GET_INFO, /* arg1= char* arg2= char* arg3= char** res=can fail */
INPUT_DEL_INFO, /* arg1= char* arg2= char* res=can fail */
INPUT_SET_NAME, /* arg1= char* res=can fail */

/* Input config options */
INPUT_ADD_OPTION, /* arg1= char * arg2= char * res=can fail*/

/* Input properties */
INPUT_GET_BYTE_POSITION, /* arg1= int64_t * res= */
INPUT_SET_BYTE_SIZE, /* arg1= int64_t * res= */

/* bookmarks */
INPUT_GET_BOOKMARKS, /* arg1= seekpoint_t *** arg2= int * res=can fail */
INPUT_CLEAR_BOOKMARKS, /* res=can fail */
INPUT_ADD_BOOKMARK, /* arg1= seekpoint_t * res=can fail */
INPUT_CHANGE_BOOKMARK, /* arg1= seekpoint_t * arg2= int * res=can fail */
INPUT_DEL_BOOKMARK, /* arg1= seekpoint_t * res=can fail */
INPUT_SET_BOOKMARK, /* arg1= int res=can fail */

/* On the fly input slave */
INPUT_ADD_SLAVE /* arg1= char * */
}

#endregion

#region private vlc structs
[StructLayout(LayoutKind.Sequential)]
struct libvlc_exception_t
{
public Int32 b_raised;
public IntPtr psz_message;

public void Init()
{
libvlc_exception_init(out this);
}

public bool WasExceptionRaised()
{
if(0 != libvlc_exception_raised(ref this))
{
libvlc_exception_clear(ref this);
return true;
}
return false;
}
}

[StructLayout(LayoutKind.Sequential)]
struct libvlc_instance_t
{
public IntPtr p_vlc;
public IntPtr p_playlist;
public IntPtr p_vlm;
public Int32 i_vlc_id;

public libvlc_instance_t(IntPtr vlc, IntPtr playlist, int vlcHandle)
{
this.p_vlc = vlc;
this.p_playlist = playlist;
this.p_vlm = IntPtr.Zero;
this.i_vlc_id = vlcHandle;
}
}

[StructLayout(LayoutKind.Sequential)]
struct vlc_list_t
{
public Int32 i_count;
public IntPtr p_values;
public IntPtr pi_types;
}

[StructLayout( LayoutKind.Explicit )]
struct vlc_value_t
{
[FieldOffset( 0 )] public Int32 i_int;
[FieldOffset( 0 )] public Int32 b_bool;
[FieldOffset( 0 )][MarshalAs(UnmanagedType.R4)] public float f_float;
[FieldOffset( 0 )] public IntPtr psz_string;
[FieldOffset( 0 )] public IntPtr p_address;
[FieldOffset( 0 )] public IntPtr p_object;
[FieldOffset( 0 )] public IntPtr p_list;
[FieldOffset( 0 )][MarshalAs(UnmanagedType.I8)] public Int64 i_time;

[FieldOffset( 0 )] public IntPtr psz_name;
[FieldOffset( 4 )] public Int32 i_object_id;
}

[StructLayout(LayoutKind.Sequential)]
struct module_config_t
{
public CONFIG_ITEM i_type;
}

#endregion

#region vlc api interop
const int AOUT_VOLUME_MAX = 1024;
const int VOLUME_MAX = 200;
const int DEFAULT_CHAN = 1;
const String Playlist_Current = "item-change";
const String Now_Playing = "Now Playing";
const String Meta_information = "Meta-information";
const String Meta_title = "meta-title";
const String Meta_author = "meta-author";
const String Meta_artist = "meta-artist";
const String Meta_genre = "meta-genre";
const String Meta_description = "meta-description";
const String Meta_url = "meta-url";

[DllImport("libvlc")]
static extern int VLC_Create();
[DllImport("libvlc")]
static extern VlcError VLC_Init(int iVLC, int Argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)]string[] Argv);
[DllImport("libvlc")]
static extern string VLC_Version();
[DllImport("libvlc")]
static extern VlcError VLC_CleanUp(int iVLC);
[DllImport("libvlc")]
static extern VlcError VLC_Destroy(int iVLC);
[DllImport("libvlc")]
static extern string VLC_Error(int i_err);

[DllImport("libvlc")]
static extern IntPtr vlc_current_object(int i_object);
[DllImport("libvlc")]
static extern IntPtr __vlc_object_find(IntPtr p_vlc, ObjectType objectType, ObjectSearchMode mode);
[DllImport("libvlc")]
static extern void __vlc_object_release(IntPtr p_vlc);
[DllImport("libvlc")]
static extern VlcError __var_Set(IntPtr p_vlc, String name, vlc_value_t value);
[DllImport("libvlc")]
static extern VlcError __var_Get(IntPtr p_this, String name, ref vlc_value_t value);
[DllImport("libvlc")]
static extern VlcError __var_Change(IntPtr p_this, String name, VarFlags varFlags,
ref vlc_value_t value, ref vlc_value_t value2);

[DllImport("libvlc")]
static extern VlcError __aout_VolumeGet(IntPtr p_vlc, ref Int16 volume);
[DllImport("libvlc")]
static extern VlcError __aout_VolumeSet(IntPtr p_vlc, Int16 volume);
[DllImport("libvlc")]
static extern VlcError __aout_VolumeMute(IntPtr p_vlc, IntPtr alwaysNull);

[DllImport("libvlc")]
static extern void libvlc_exception_init(out libvlc_exception_t p_exception);
[DllImport("libvlc")]
static extern Int32 libvlc_exception_raised(ref libvlc_exception_t p_exception);
[DllImport("libvlc")]
static extern void libvlc_exception_clear(ref libvlc_exception_t p_exception);

[DllImport("libvlc")]
static extern void libvlc_playlist_play(ref libvlc_instance_t libvlc, Int32 id,
Int32 optionsCount, IntPtr optionsAlwaysNull, ref libvlc_exception_t p_exception);
[DllImport("libvlc")]
static extern IntPtr libvlc_playlist_get_input(ref libvlc_instance_t libvlc, ref libvlc_exception_t p_exception);

[DllImport("libvlc")]
static extern int VLC_PlaylistIndex(int vlcObject);
[DllImport("libvlc")]
static extern int VLC_PlaylistNumberOfItems(int vlcObject);

[DllImport("libvlc")]
static extern void libvlc_input_free(IntPtr p_input);

[DllImport("libvlc")]
static extern int libvlc_video_get_width(IntPtr p_input, ref libvlc_exception_t p_exception);
[DllImport("libvlc")]
static extern int libvlc_video_get_height(IntPtr p_input, ref libvlc_exception_t p_exception);

[DllImport("libvlc", CallingConvention=CallingConvention.Cdecl)]
static extern VlcError playlist_LockControl(IntPtr p_playlist, playlist_command i_query);
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
static extern VlcError playlist_LockControl(IntPtr p_playlist, playlist_command i_query, Int32 arg1);
[DllImport("libvlc")]
static extern VlcError playlist_Clear(IntPtr p_playlist);
[DllImport("libvlc")]
static extern VlcError playlist_AddExt(IntPtr p_playlist, String mrl, String mrlDuplicate,
Mode mode, Int32 pos, Int64 mtime_t,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string[] Options,
int OptionsCount);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int VarChangedCallback(IntPtr vlc, String variable, vlc_value_t old_val,
vlc_value_t new_val, IntPtr param);

[DllImport("libvlc")]
static extern int __var_AddCallback(IntPtr vlc, String variable, VarChangedCallback cb,
IntPtr param);
[DllImport("libvlc")]
static extern int __var_DelCallback(IntPtr vlc, String variable, VarChangedCallback cb,
IntPtr param);
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
static extern VlcError input_Control(IntPtr input_thread_t, input_query_e i_query, String category, String name,
ref IntPtr result);

[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
static extern void __vout_OSDMessage(IntPtr p_input, int i_channel, String message);
[DllImport("libvlc")]
static extern IntPtr config_FindConfig(IntPtr vlc, String name);
[DllImport("libvlc")]
static extern void __config_PutInt(IntPtr vlc, String name, int value);
[DllImport("libvlc")]
static extern void __config_PutFloat(IntPtr vlc, String name, float value);
[DllImport("libvlc")]
static extern void __config_PutPsz(IntPtr vlc, String name, String value);

[DllImport("libvlc")]
static extern int __config_GetInt(IntPtr vlc, String name);
[DllImport("libvlc")]
static extern float __config_GetFloat(IntPtr vlc, String name);
[DllImport("libvlc")]
static extern String __config_GetPsz(IntPtr vlc, String name);

#endregion

static NativeLibVlc()
{
NativeLibVlc.vlcInstallDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);
//NativeLibVlc.vlcInstallDirectory = Environment.CurrentDirectory;
//NativeLibVlc.vlcInstallDirectory = QueryVlcInstallPath();
}

public NativeLibVlc()
{
}

#region IDisposable
public void Dispose()
{
if(vlcHandle != -1)
{
try
{
if(this.gch.IsAllocated)
{
UnhookPlaylistChanges();
}
VideoOutput = null;
VLC_CleanUp(this.vlcHandle);
VLC_Destroy(this.vlcHandle);
}
catch { }
}
vlcHandle = -1;
}
#endregion

#region internal Vlc interop helper classes
internal class VlcObject : IDisposable
{
IntPtr vlc = IntPtr.Zero;
IntPtr subObject = IntPtr.Zero;
bool isDisposed;

public VlcObject(int vlcHandle, ObjectType objectType)
{
vlc = vlc_current_object(vlcHandle);
if(IntPtr.Zero != vlc)
{
if(objectType == ObjectType.VLC_OBJECT_VLC)
{
subObject = vlc;
}
else
{
subObject = __vlc_object_find(vlc, objectType, ObjectSearchMode.FIND_CHILD);
}
}
}

public IntPtr Vlc { get { return this.vlc; } }
public IntPtr SubObject { get { return this.subObject; } }

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if(!this.isDisposed)
{
this.isDisposed = true;
if((IntPtr.Zero != subObject) && (subObject != vlc))
{
__vlc_object_release(subObject);
}
if(IntPtr.Zero != vlc)
{
__vlc_object_release(vlc);
}
}
}

protected bool IsDisposed { get { return this.isDisposed; } }

~VlcObject()
{
Dispose(false);
}
}

private class VlcPlaylistObject : VlcObject
{
public libvlc_instance_t libvlc;
public libvlc_exception_t exception;

public VlcPlaylistObject(int vlcHandle)
: base(vlcHandle, ObjectType.VLC_OBJECT_PLAYLIST)
{
if(this.SubObject != IntPtr.Zero)
{
this.libvlc = new libvlc_instance_t(this.Vlc, this.SubObject, vlcHandle);
this.exception.Init();
}
}
}
#endregion

#region public Vlc interop helper functions
public VlcObject OpenVlcObject(ObjectType objectType)
{
return new VlcObject(this.vlcHandle, objectType);
}

public int GetVlcObjectInt(ObjectType objectType, String propertyName, int errorReturn)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
vlc_value_t intValue = new vlc_value_t();
if((vobj.SubObject != IntPtr.Zero) &&
(VlcError.Success == __var_Get(vobj.SubObject, propertyName, ref intValue)))
{
return intValue.i_int;
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return errorReturn;
}

public VlcError SetVlcObjectInt(ObjectType objectType, String propertyName, int value)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
if(vobj.SubObject != IntPtr.Zero)
{
vlc_value_t intValue = new vlc_value_t();
intValue.i_int = value;
return __var_Set(vobj.SubObject, propertyName, intValue);
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return VlcError.NoObj;
}

public long GetVlcObjectLong(ObjectType objectType, String propertyName, long errorReturn)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
vlc_value_t longValue = new vlc_value_t();
if((vobj.SubObject != IntPtr.Zero) &&
(VlcError.Success == __var_Get(vobj.SubObject, propertyName, ref longValue)))
{
return longValue.i_time;
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return errorReturn;
}

public VlcError SetVlcObjectLong(ObjectType objectType, String propertyName, long value)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
if(vobj.SubObject != IntPtr.Zero)
{
vlc_value_t longValue = new vlc_value_t();
longValue.i_time = value;
return __var_Set(vobj.SubObject, propertyName, longValue);
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return VlcError.NoObj;
}

public float GetVlcObjectFloat(ObjectType objectType, String propertyName, float errorReturn)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
vlc_value_t floatValue = new vlc_value_t();
if((vobj.SubObject != IntPtr.Zero) &&
(VlcError.Success == __var_Get(vobj.SubObject, propertyName, ref floatValue)))
{
return floatValue.f_float;
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return errorReturn;
}

public VlcError SetVlcObjectFloat(ObjectType objectType, String propertyName, float value)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
if(vobj.SubObject != IntPtr.Zero)
{
vlc_value_t floatValue = new vlc_value_t();
floatValue.f_float = value;
return __var_Set(vobj.SubObject, propertyName, floatValue);
}
}
}
catch(Exception ex)
{
this.lastErrorMessage = ex.Message;
}
return VlcError.NoObj;
}

public String GetVlcObjectString(ObjectType objectType, String propertyName, String errorReturn)
{
try
{
using(VlcObject vobj = new VlcObject(this.vlcHandle, objectType))
{
vlc_value_t stringValue = new vlc_value_t();
if((vobj.SubObject != IntPtr.Zero) &&
(VlcError.Success == __var_Get(vobj.SubObject, propertyName, ref stringValue))) (Chris Meadowcroft)

本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201108/14910.html]
本文出处:网络 作者:Chris Meadowcroft
顶一下
(5)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容