Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.VisualBasic;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.ComponentModel;
- using System.Globalization;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace libMpv
- {
- public static class moduleMpv
- {
- public static clsMpvDynamicLib Mpv_DLL = new clsMpvDynamicLib();
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = false)]
- static internal IntPtr LoadLibrary(string dllToLoad)
- {
- }
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = false)]
- static internal IntPtr GetProcAddress(IntPtr hModule, string procedureName)
- {
- }
- static internal object GetDllType(IntPtr intptrAssembly, Type type, string name)
- {
- IntPtr address = GetProcAddress(intptrAssembly, name);
- if (address != IntPtr.Zero) {
- return Marshal.GetDelegateForFunctionPointer(address, type);
- }
- return null;
- }
- public enum enumMpv_Format
- {
- MPV_FORMAT_NONE = 0,
- MPV_FORMAT_STRING = 1,
- MPV_FORMAT_OSD_STRING = 2,
- MPV_FORMAT_FLAG = 3,
- MPV_FORMAT_INT64 = 4,
- MPV_FORMAT_DOUBLE = 5,
- MPV_FORMAT_NODE = 6,
- MPV_FORMAT_NODE_ARRAY = 7,
- MPV_FORMAT_NODE_MAP = 8,
- MPV_FORMAT_BYTE_ARRAY = 9
- }
- public enum enumMpv_Event
- {
- MPV_EVENT_NONE = 0,
- MPV_EVENT_SHUTDOWN = 1,
- MPV_EVENT_LOG_MESSAGE = 2,
- MPV_EVENT_GET_PROPERTY_REPLY = 3,
- MPV_EVENT_SET_PROPERTY_REPLY = 4,
- MPV_EVENT_COMMAND_REPLY = 5,
- MPV_EVENT_START_FILE = 6,
- MPV_EVENT_END_FILE = 7,
- MPV_EVENT_FILE_LOADED = 8,
- MPV_EVENT_TRACKS_CHANGED = 9,
- MPV_EVENT_TRACK_SWITCHED = 10,
- MPV_EVENT_IDLE = 11,
- MPV_EVENT_PAUSE = 12,
- MPV_EVENT_UNPAUSE = 13,
- MPV_EVENT_TICK = 14,
- MPV_EVENT_SCRIPT_INPUT_DISPATCH = 15,
- MPV_EVENT_CLIENT_MESSAGE = 16,
- MPV_EVENT_VIDEO_RECONFIG = 17,
- MPV_EVENT_AUDIO_RECONFIG = 18,
- MPV_EVENT_METADATA_UPDATE = 19,
- MPV_EVENT_SEEK = 20,
- MPV_EVENT_PLAYBACK_RESTART = 21,
- MPV_EVENT_PROPERTY_CHANGE = 22,
- MPV_EVENT_CHAPTER_CHANGE = 23,
- MPV_EVENT_QUEUE_OVERFLOW = 24
- }
- public enum enumMpv_ReasonEOF
- {
- MPV_END_FILE_REASON_EOF = 0,
- MPV_END_FILE_REASON_STOP = 2,
- MPV_END_FILE_REASON_QUIT = 3,
- MPV_END_FILE_REASON_ERROR = 4,
- MPV_END_FILE_REASON_REDIRECT = 5
- }
- public enum enumMpv_Error
- {
- MPV_ERROR_SUCCESS = 0,
- MPV_ERROR_EVENT_QUEUE_FULL = -1,
- MPV_ERROR_NOMEM = -2,
- MPV_ERROR_UNINITIALIZED = -3,
- MPV_ERROR_INVALID_PARAMETER = -4,
- MPV_ERROR_OPTION_NOT_FOUND = -5,
- MPV_ERROR_OPTION_FORMAT = -6,
- MPV_ERROR_OPTION_ERROR = -7,
- MPV_ERROR_PROPERTY_NOT_FOUND = -8,
- MPV_ERROR_PROPERTY_FORMAT = -9,
- MPV_ERROR_PROPERTY_UNAVAILABLE = -10,
- MPV_ERROR_PROPERTY_ERROR = -11,
- MPV_ERROR_COMMAND = -12,
- MPV_ERROR_LOADING_FAILED = -13,
- MPV_ERROR_AO_INIT_FAILED = -14,
- MPV_ERROR_VO_INIT_FAILED = -15,
- MPV_ERROR_NOTHING_TO_PLAY = -16,
- MPV_ERROR_UNKNOWN_FORMAT = -17,
- MPV_ERROR_UNSUPPORTED = -18,
- MPV_ERROR_NOT_IMPLEMENTED = -19,
- MPV_ERROR_GENERIC = -20
- }
- public enum enumMpv_LogLevel
- {
- MPV_LOG_LEVEL_NONE = 0,
- MPV_LOG_LEVEL_FATAL = 10,
- MPV_LOG_LEVEL_ERROR = 20,
- MPV_LOG_LEVEL_WARN = 30,
- MPV_LOG_LEVEL_INFO = 40,
- MPV_LOG_LEVEL_V = 50,
- MPV_LOG_LEVEL_DEBUG = 60,
- MPV_LOG_LEVEL_TRACE = 70
- }
- [StructLayout(LayoutKind.Explicit)]
- internal struct _structMpv_Node
- {
- [FieldOffset(0)]
- public double Dbl;
- [FieldOffset(0)]
- public long Int64;
- [FieldOffset(0)]
- public bool Flag;
- [FieldOffset(0)]
- public IntPtr Ptr;
- [FieldOffset(8)]
- public enumMpv_Format Format;
- }
- internal struct _structMpv_Node_List
- {
- public int Num;
- public IntPtr Values;
- public IntPtr Keys;
- }
- internal struct _structMpv_ByteArray
- {
- public IntPtr pData;
- public int Size;
- }
- internal struct _structMpv_Event_LogMessage
- {
- public string Prefix;
- public string Level;
- public string Text;
- }
- internal struct _structMpv_Event_EOF
- {
- public enumMpv_ReasonEOF ReasonID;
- public enumMpv_Error ErrorID;
- }
- internal struct _structMpv_Event_Property
- {
- public string Name;
- public enumMpv_Format Format;
- public IntPtr ptrData;
- }
- internal struct _structMpv_Event
- {
- public enumMpv_Event EventID;
- public enumMpv_Error ErrorID;
- public long reply_userdata;
- public IntPtr ptrData;
- }
- public struct structMpv_Node
- {
- public enumMpv_Format FormatID;
- public object Data;
- public structMpv_Node(object obj)
- {
- Type typ = obj.GetType();
- switch (typ) {
- case typeof(bool):
- FormatID = enumMpv_Format.MPV_FORMAT_FLAG;
- break;
- case typeof(long):
- FormatID = enumMpv_Format.MPV_FORMAT_INT64;
- break;
- case typeof(double):
- FormatID = enumMpv_Format.MPV_FORMAT_DOUBLE;
- break;
- case typeof(string):
- FormatID = enumMpv_Format.MPV_FORMAT_STRING;
- break;
- case typeof(byte[]):
- FormatID = enumMpv_Format.MPV_FORMAT_BYTE_ARRAY;
- break;
- case typeof(structMpv_Node):
- FormatID = enumMpv_Format.MPV_FORMAT_NODE;
- break;
- case typeof(structMpv_Node[]):
- FormatID = enumMpv_Format.MPV_FORMAT_NODE_ARRAY;
- break;
- case typeof(Dictionary<string, structMpv_Node>):
- FormatID = enumMpv_Format.MPV_FORMAT_NODE_MAP;
- break;
- }
- Data = obj;
- }
- internal structMpv_Node(_structMpv_Node @ref)
- {
- FormatID = @ref.Format;
- switch (FormatID) {
- case enumMpv_Format.MPV_FORMAT_FLAG:
- Data = @ref.Flag;
- break;
- case enumMpv_Format.MPV_FORMAT_INT64:
- Data = @ref.Int64;
- break;
- case enumMpv_Format.MPV_FORMAT_DOUBLE:
- Data = @ref.Dbl;
- break;
- case enumMpv_Format.MPV_FORMAT_STRING:
- Data = Marshal.PtrToStringAnsi(@ref.Ptr);
- break;
- case enumMpv_Format.MPV_FORMAT_OSD_STRING:
- Data = Marshal.PtrToStringAnsi(@ref.Ptr);
- break;
- case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
- _structMpv_ByteArray ptrAry = Marshal.PtrToStructure<_structMpv_ByteArray>(@ref.Ptr);
- byte[] aryRet = new byte[ptrAry.Size + 1];
- Marshal.PtrToStructure<byte[]>(ptrAry.pData, aryRet);
- Data = aryRet;
- break;
- case enumMpv_Format.MPV_FORMAT_NODE:
- Data = new structMpv_Node(Marshal.PtrToStructure<_structMpv_Node>(@ref.Ptr));
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
- _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(@ref.Ptr);
- structMpv_Node[] retLst = new structMpv_Node[ptrLst.Num];
- for (int i = 0; i <= ptrLst.Num - 1; i++) {
- IntPtr ptrNewItm = moduleMpv.GetPtrFromAry(ptrLst.Values, i);
- _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
- retLst(i) = new structMpv_Node(objNewItm);
- }
- Data = retLst;
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_MAP:
- _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(@ref.Ptr);
- Dictionary<string, structMpv_Node> retMap = new Dictionary<string, structMpv_Node>();
- for (int i = 0; i <= ptrLst.Num - 1; i++) {
- IntPtr ptrNewKey = moduleMpv.GetPtrFromAry(ptrLst.Keys, i);
- string strNewKey = Marshal.PtrToStringAnsi(ptrNewKey);
- IntPtr ptrNewItm = moduleMpv.GetPtrFromAry(ptrLst.Values, i);
- _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
- retMap.Add(strNewKey, new structMpv_Node(objNewItm));
- }
- Data = retMap;
- break;
- }
- }
- public string Format {
- get { return moduleMpv.GetMpvEnum(FormatID); }
- }
- internal _structMpv_Node ToNative()
- {
- _structMpv_Node structRet = new _structMpv_Node();
- structRet.Format = FormatID;
- switch (FormatID) {
- case enumMpv_Format.MPV_FORMAT_FLAG:
- structRet.Flag = Convert.ToBoolean(Data);
- break;
- case enumMpv_Format.MPV_FORMAT_INT64:
- structRet.Int64 = Convert.ToInt64(Data);
- break;
- case enumMpv_Format.MPV_FORMAT_DOUBLE:
- structRet.Dbl = Convert.ToDouble(Data);
- break;
- case enumMpv_Format.MPV_FORMAT_STRING:
- structRet.Ptr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel({ Data.ToString }, ref null);
- break;
- case enumMpv_Format.MPV_FORMAT_OSD_STRING:
- structRet.Ptr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel({ Data.ToString }, ref null);
- break;
- case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
- byte[] dataTmp = (byte[])Data;
- _structMpv_ByteArray aryRet = new _structMpv_ByteArray();
- aryRet.Size = dataTmp.Length;
- aryRet.pData = Marshal.AllocHGlobal(Marshal.SizeOf<byte> * aryRet.Size);
- Marshal.Copy(dataTmp, 0, aryRet.pData, aryRet.Size);
- structRet.Ptr = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_ByteArray>);
- Marshal.StructureToPtr<_structMpv_ByteArray>(aryRet, structRet.Ptr, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE:
- _structMpv_Node objRet = ((structMpv_Node)Data).ToNative();
- structRet.Ptr = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node>);
- Marshal.StructureToPtr<_structMpv_Node>(objRet, structRet.Ptr, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
- structMpv_Node[] lstTmp = (structMpv_Node[])Data;
- _structMpv_Node_List lstRet = new _structMpv_Node_List();
- lstRet.Num = lstTmp.Length;
- lstRet.Values = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node> * lstRet.Num);
- for (int i = 0; i <= lstTmp.Length - 1; i++) {
- _structMpv_Node tmpItm = lstTmp(i).ToNative();
- Marshal.StructureToPtr<_structMpv_Node>(tmpItm, moduleMpv.GetPtrFromAry(lstRet.Values, i), true);
- }
- Marshal.StructureToPtr<_structMpv_Node_List>(lstRet, structRet.Ptr, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_MAP:
- Dictionary<string, structMpv_Node> mapTmp = (Dictionary<string, structMpv_Node>)Data;
- _structMpv_Node_List mapRet = new _structMpv_Node_List();
- mapRet.Num = mapTmp.Count;
- mapRet.Keys = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel(mapTmp.Keys.ToArray, ref null);
- for (int i = 0; i <= mapTmp.Count - 1; i++) {
- structMpv_Node tmpNode = null;
- mapTmp.TryGetValue(mapTmp.Keys(i), tmpNode);
- _structMpv_Node tmpItm = tmpNode.ToNative();
- Marshal.StructureToPtr<_structMpv_Node>(tmpItm, moduleMpv.GetPtrFromAry(mapRet.Values, i), true);
- }
- Marshal.StructureToPtr<_structMpv_Node_List>(mapRet, structRet.Ptr, true);
- break;
- }
- return structRet;
- }
- public override string ToString()
- {
- string strRet = "<" + Format + ">";
- if ((Data != null)) {
- if (Data.GetType.IsArray) {
- Array aryTmp = (Array)Data;
- strRet += " [Length=" + aryTmp.Length.ToString + "] {";
- for (int i = 0; i <= aryTmp.Length - 1; i++) {
- strRet += " " + aryTmp.GetValue(i).ToString + ";";
- }
- strRet += "}";
- } else if ((Data as Dictionary<string, structMpv_Node> != null)) {
- List<KeyValuePair<string, structMpv_Node>> mapTmp = (Data as Dictionary<string, structMpv_Node>).ToList();
- strRet += " [Length=" + mapTmp.Count.ToString + "] {";
- for (int i = 0; i <= mapTmp.Count - 1; i++) {
- strRet += " " + mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + ";";
- }
- strRet += "}";
- } else {
- strRet += " " + Data.ToString;
- }
- }
- return strRet;
- }
- }
- public struct structMpv_Event
- {
- public enumMpv_Event EventID;
- public enumMpv_Error ErrorID;
- public long UserDataReply;
- public string DataName;
- public object Data;
- internal structMpv_Event(_structMpv_Event @ref)
- {
- EventID = @ref.EventID;
- ErrorID = @ref.ErrorID;
- UserDataReply = @ref.reply_userdata;
- UpdateFromDataPointer(@ref.ptrData);
- }
- public string EventName {
- get { return moduleMpv.Mpv_DLL.mpvGetEventNameString(EventID); }
- }
- public string ErrorName {
- get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
- }
- internal void UpdateFromDataPointer(IntPtr ptrData)
- {
- if (ptrData != IntPtr.Zero) {
- switch (EventID) {
- case enumMpv_Event.MPV_EVENT_LOG_MESSAGE:
- _structMpv_Event_LogMessage pEvent = Marshal.PtrToStructure<_structMpv_Event_LogMessage>(ptrData);
- DataName = pEvent.Prefix;
- Data = pEvent.Text;
- break;
- case enumMpv_Event.MPV_EVENT_PROPERTY_CHANGE:
- _structMpv_Event_Property pEvent = Marshal.PtrToStructure<_structMpv_Event_Property>(ptrData);
- DataName = pEvent.Name;
- Data = moduleMpv.GetMpvObjectFromFormat(pEvent.ptrData, pEvent.Format);
- break;
- case enumMpv_Event.MPV_EVENT_END_FILE:
- _structMpv_Event_EOF pEvent = Marshal.PtrToStructure<_structMpv_Event_EOF>(ptrData);
- DataName = moduleMpv.GetMpvEnum(pEvent.ReasonID);
- ErrorID = pEvent.ErrorID;
- break;
- }
- }
- }
- }
- public class MpvEventArgs : EventArgs
- {
- public enumMpv_Event EventID;
- public enumMpv_Error ErrorID;
- public string DataName;
- public object Data;
- public MpvEventArgs(structMpv_Event @ref)
- {
- EventID = @ref.EventID;
- ErrorID = @ref.ErrorID;
- DataName = @ref.DataName;
- Data = @ref.Data;
- }
- public string EventName {
- get { return moduleMpv.Mpv_DLL.mpvGetEventNameString(EventID); }
- }
- public string ErrorName {
- get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
- }
- }
- public class MpvPropertyChangeEventArgs : EventArgs
- {
- public string PropertyName;
- public object NewValue;
- public MpvPropertyChangeEventArgs()
- {
- PropertyName = "";
- NewValue = null;
- }
- public MpvPropertyChangeEventArgs(structMpv_Event @ref)
- {
- PropertyName = @ref.DataName;
- NewValue = @ref.Data;
- }
- public MpvPropertyChangeEventArgs(string strPropertyName, object objNewValue)
- {
- PropertyName = strPropertyName;
- NewValue = objNewValue;
- }
- }
- public class MpvEndOfFileEventArgs : EventArgs
- {
- public string Reason;
- public enumMpv_Error ErrorID;
- public MpvEndOfFileEventArgs()
- {
- Reason = "";
- ErrorID = 0;
- }
- public MpvEndOfFileEventArgs(enumMpv_ReasonEOF intReason, enumMpv_Error intError)
- {
- Reason = moduleMpv.GetMpvEnum(intReason);
- ErrorID = intError;
- }
- public MpvEndOfFileEventArgs(structMpv_Event @ref)
- {
- Reason = @ref.DataName;
- ErrorID = @ref.ErrorID;
- }
- public enumMpv_ReasonEOF ReasonID {
- get {
- enumMpv_ReasonEOF intRet = default(enumMpv_ReasonEOF);
- Enum.TryParse(Reason, true, intRet);
- return intRet;
- }
- }
- public string ErrorName {
- get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
- }
- }
- public struct structCache
- {
- private IntPtr _ptrHandle;
- public structCache(IntPtr mpvHandle)
- {
- _ptrHandle = mpvHandle;
- }
- public long Size {
- get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-size", enumMpv_Format.MPV_FORMAT_INT64)); }
- set { moduleMpv.Mpv_DLL.mpvSetProperty(_ptrHandle, "cache-size", enumMpv_Format.MPV_FORMAT_INT64, value); }
- }
- public double Percent {
- get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
- }
- public long SizeUsed {
- get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-used", enumMpv_Format.MPV_FORMAT_INT64)); }
- }
- public long SizeFree {
- get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-free", enumMpv_Format.MPV_FORMAT_INT64)); }
- }
- public double Position {
- get {
- double dblRet = 0;
- double dblCacheSecs = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "demuxer-cache-time", enumMpv_Format.MPV_FORMAT_DOUBLE));
- double dblLength = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "duration", enumMpv_Format.MPV_FORMAT_DOUBLE));
- double dblPos = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "time-pos", enumMpv_Format.MPV_FORMAT_DOUBLE));
- long intFileSize = Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "file-size", enumMpv_Format.MPV_FORMAT_INT64));
- double dblFileSize = Convert.ToDouble(intFileSize) / 1024;
- double dblSizePassed = (dblPos / dblLength) * dblFileSize;
- dblRet = (dblLength) * (SizeUsed / (dblFileSize - dblSizePassed)) + dblCacheSecs;
- if ((dblRet > dblLength))
- dblRet = dblLength;
- return dblRet;
- }
- }
- public override string ToString()
- {
- return SizeUsed.ToString("#,###") + " of " + Size.ToString("#,###") + " KiB Used. BufferEnd At " + Position.ToString("0.0#");
- }
- }
- public class clsMpvDynamicLib : IDisposable
- {
- private IntPtr _libMpvDll;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate IntPtr delegateMpvCreate();
- public delegateMpvCreate mpvCreate;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvInitialize(IntPtr mpvHandle);
- public delegateMpvInitialize mpvInitialize;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvCommand(IntPtr mpvHandle, IntPtr strings);
- public delegateMpvCommand mpvCommand;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvTerminateDestroy(IntPtr mpvHandle);
- public delegateMpvTerminateDestroy mpvTerminateDestroy;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate IntPtr delegateMpvWaitEvent(IntPtr mpvHandle, double timeout);
- public delegateMpvWaitEvent mpvWaitEvent;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvSetOption(IntPtr mpvHandle, byte[] name, enumMpv_Format format, ref IntPtr data);
- public delegateMpvSetOption mpvSetOption;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvSetOptionString(IntPtr mpvHandle, byte[] name, byte[] value);
- public delegateMpvSetOptionString mpvSetOptionString;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate int delegateMpvGetPropertystring(IntPtr mpvHandle, byte[] name, ref IntPtr data);
- private delegateMpvGetPropertystring _mpvGetPropertyString;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate int delegateMpvSetPropertystring(IntPtr mpvHandle, byte[] name, byte[] value);
- private delegateMpvSetPropertystring _mpvSetPropertyString;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate int delegateMpvGetProperty(IntPtr mpvHandle, byte[] name, enumMpv_Format format, IntPtr data);
- private delegateMpvGetProperty _mpvGetProperty;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate int delegateMpvSetProperty(IntPtr mpvHandle, byte[] name, enumMpv_Format format, IntPtr data);
- private delegateMpvSetProperty _mpvSetProperty;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvRequestLogMessages(IntPtr mpvHandle, byte[] strLevel);
- public delegateMpvRequestLogMessages mpvRequestLogMessages;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvRequestEvent(IntPtr mpvHandle, enumMpv_Event intEvent, int intEnable);
- public delegateMpvRequestEvent mpvRequestEvent;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvObserveProperty(IntPtr mpvHandle, int intUserGroup, byte[] name, enumMpv_Format format);
- public delegateMpvObserveProperty mpvObserveProperty;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int delegateMpvUnobserveProperty(IntPtr mpvHandle, int intUserGroup);
- public delegateMpvUnobserveProperty mpvUnobserveProperty;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void delegateMpvFree(IntPtr data);
- public delegateMpvFree mpvFree;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate string delegateMpvErrorString(enumMpv_Error intError);
- public delegateMpvErrorString mpvGetErrorString;
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate IntPtr delegateMpvEventNameString(enumMpv_Event intEvent);
- private delegateMpvEventNameString _mpvGetEventNameString;
- public clsMpvDynamicLib()
- {
- _libMpvDll = moduleMpv.LoadLibrary(Application.StartupPath + "\\mpv-1.dll");
- // The dll is included in the DEV builds by lachs0r: https://mpv.srsfckn.biz/
- mpvCreate = (delegateMpvCreate)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvCreate), "mpv_create");
- mpvInitialize = (delegateMpvInitialize)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvInitialize), "mpv_initialize");
- mpvTerminateDestroy = (delegateMpvTerminateDestroy)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvTerminateDestroy), "mpv_terminate_destroy");
- mpvCommand = (delegateMpvCommand)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvCommand), "mpv_command");
- mpvSetOption = (delegateMpvSetOption)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetOption), "mpv_set_option");
- mpvSetOptionString = (delegateMpvSetOptionString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetOptionString), "mpv_set_option_string");
- _mpvGetPropertyString = (delegateMpvGetPropertystring)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvGetPropertystring), "mpv_get_property_string");
- _mpvSetPropertyString = (delegateMpvSetPropertystring)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetPropertystring), "mpv_set_property_string");
- _mpvGetProperty = (delegateMpvGetProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvGetProperty), "mpv_get_property");
- _mpvSetProperty = (delegateMpvSetProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetProperty), "mpv_set_property");
- mpvWaitEvent = (delegateMpvWaitEvent)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvWaitEvent), "mpv_wait_event");
- mpvRequestLogMessages = (delegateMpvRequestLogMessages)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvRequestLogMessages), "mpv_request_log_messages");
- mpvRequestEvent = (delegateMpvRequestEvent)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvRequestEvent), "mpv_request_event");
- mpvObserveProperty = (delegateMpvObserveProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvObserveProperty), "mpv_observe_property");
- mpvUnobserveProperty = (delegateMpvUnobserveProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvUnobserveProperty), "mpv_unobserve_property");
- mpvFree = (delegateMpvFree)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvFree), "mpv_free");
- mpvGetErrorString = (delegateMpvErrorString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvErrorString), "mpv_error_string");
- _mpvGetEventNameString = (delegateMpvEventNameString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvEventNameString), "mpv_event_name");
- }
- private bool disposedValue;
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue) {
- if (disposing) {
- _libMpvDll = IntPtr.Zero;
- mpvCreate = null;
- mpvInitialize = null;
- mpvTerminateDestroy = null;
- mpvCommand = null;
- mpvSetOption = null;
- mpvSetOptionString = null;
- _mpvGetPropertyString = null;
- _mpvSetPropertyString = null;
- _mpvGetProperty = null;
- _mpvSetProperty = null;
- mpvWaitEvent = null;
- mpvRequestLogMessages = null;
- mpvRequestEvent = null;
- mpvObserveProperty = null;
- mpvUnobserveProperty = null;
- mpvFree = null;
- mpvGetErrorString = null;
- _mpvGetEventNameString = null;
- }
- }
- disposedValue = true;
- }
- public void Dispose()
- {
- Dispose(true);
- }
- public object mpvGetProperty(IntPtr _ptrHandle, string strPropertyName, enumMpv_Format intFormat)
- {
- object objRet = null;
- if (_ptrHandle != IntPtr.Zero) {
- IntPtr lpBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(moduleMpv.GetMpvTypeFromFormat(intFormat)));
- int intNum = _mpvGetProperty(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), intFormat, lpBuffer);
- if (lpBuffer != IntPtr.Zero)
- objRet = moduleMpv.GetMpvObjectFromFormat(lpBuffer, intFormat);
- Marshal.FreeHGlobal(lpBuffer);
- }
- return objRet;
- }
- public void mpvSetProperty(IntPtr _ptrHandle, string strPropertyName, enumMpv_Format intFormat, object obj)
- {
- if (_ptrHandle == IntPtr.Zero) {
- return;
- }
- _mpvSetProperty(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), intFormat, moduleMpv.GetMpvValueFromObject(obj, intFormat));
- }
- public string mpvGetPropertyString(IntPtr _ptrHandle, string strPropertyName)
- {
- string strRet = "";
- if (_ptrHandle != IntPtr.Zero) {
- IntPtr lpBuffer = IntPtr.Zero;
- _mpvGetPropertyString(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), lpBuffer);
- strRet = Marshal.PtrToStringAnsi(lpBuffer);
- mpvFree(lpBuffer);
- }
- return strRet;
- }
- public void mpvSetPropertyString(IntPtr _ptrHandle, string strPropertyName, string str)
- {
- if (_ptrHandle == IntPtr.Zero) {
- return;
- }
- _mpvSetPropertyString(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), moduleMpv.GetUtf8Bytes(str));
- }
- public string mpvGetEventNameString(enumMpv_Event intEvent)
- {
- return Marshal.PtrToStringAnsi(_mpvGetEventNameString(intEvent));
- }
- }
- static internal IntPtr GetPtrFromAry<Typ>(IntPtr AryPtr, int Itm)
- {
- return IntPtr.Add(AryPtr, Itm * Marshal.SizeOf<Typ>);
- }
- static internal object GetMpvObjectFromFormat(IntPtr ptrData, enumMpv_Format format)
- {
- object objRet = null;
- if (ptrData != IntPtr.Zero) {
- switch (format) {
- case enumMpv_Format.MPV_FORMAT_FLAG:
- objRet = ((Marshal.PtrToStructure<int>(ptrData)) == 1);
- break;
- case enumMpv_Format.MPV_FORMAT_INT64:
- objRet = Marshal.PtrToStructure<long>(ptrData);
- break;
- case enumMpv_Format.MPV_FORMAT_DOUBLE:
- objRet = Marshal.PtrToStructure<double>(ptrData);
- break;
- case enumMpv_Format.MPV_FORMAT_STRING:
- objRet = Marshal.PtrToStringAnsi(ptrData);
- break;
- case enumMpv_Format.MPV_FORMAT_OSD_STRING:
- objRet = Marshal.PtrToStringAnsi(ptrData);
- break;
- case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
- _structMpv_ByteArray ptrAry = Marshal.PtrToStructure<_structMpv_ByteArray>(ptrData);
- byte[] aryRet = new byte[ptrAry.Size + 1];
- Marshal.PtrToStructure<byte[]>(ptrAry.pData, aryRet);
- objRet = aryRet;
- break;
- case enumMpv_Format.MPV_FORMAT_NODE:
- _structMpv_Node ptrNode = Marshal.PtrToStructure<_structMpv_Node>(ptrData);
- objRet = new structMpv_Node(ptrNode);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
- _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(ptrData);
- structMpv_Node[] retLst = new structMpv_Node[ptrLst.Num];
- for (int i = 0; i <= ptrLst.Num - 1; i++) {
- IntPtr ptrNewItm = GetPtrFromAry<_structMpv_Node>(ptrLst.Values, i);
- _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
- retLst(i) = new structMpv_Node(objNewItm);
- }
- objRet = retLst;
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_MAP:
- _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(ptrData);
- Dictionary<string, structMpv_Node> retMap = new Dictionary<string, structMpv_Node>();
- for (int i = 0; i <= ptrLst.Num - 1; i++) {
- IntPtr ptrNewKey = GetPtrFromAry<string>(ptrLst.Keys, i);
- string strNewKey = Marshal.PtrToStringAnsi(ptrNewKey);
- IntPtr ptrNewItm = GetPtrFromAry<_structMpv_Node>(ptrLst.Values, i);
- _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
- retMap.Add(strNewKey, new structMpv_Node(objNewItm));
- }
- objRet = retMap;
- break;
- }
- }
- return objRet;
- }
- static internal IntPtr GetMpvValueFromObject(object objData, enumMpv_Format format)
- {
- IntPtr ptrRet = IntPtr.Zero;
- if ((objData != null)) {
- switch (format) {
- case enumMpv_Format.MPV_FORMAT_FLAG:
- ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<int>);
- int intRet = Convert.ToInt32((Convert.ToBoolean(objData) ? 1 : 0));
- Marshal.Copy({ intRet }, 0, ptrRet, 1);
- break;
- case enumMpv_Format.MPV_FORMAT_INT64:
- ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<long>);
- long intRet = Convert.ToInt64(objData);
- Marshal.Copy({ intRet }, 0, ptrRet, 1);
- break;
- case enumMpv_Format.MPV_FORMAT_DOUBLE:
- ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<double>);
- double dblRet = Convert.ToDouble(objData);
- Marshal.Copy({ dblRet }, 0, ptrRet, 1);
- break;
- case enumMpv_Format.MPV_FORMAT_STRING:
- ptrRet = AllocateUtf8IntPtrArrayWithSentinel({ objData.ToString }, ref null);
- break;
- case enumMpv_Format.MPV_FORMAT_OSD_STRING:
- ptrRet = AllocateUtf8IntPtrArrayWithSentinel({ objData.ToString }, ref null);
- break;
- case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
- byte[] aryByt = (byte[])objData;
- _structMpv_ByteArray structRet = new _structMpv_ByteArray();
- structRet.Size = aryByt.Length;
- structRet.pData = Marshal.AllocHGlobal(Marshal.SizeOf<byte> * structRet.Size);
- Marshal.Copy(aryByt, 0, structRet.pData, structRet.Size);
- ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_ByteArray>);
- Marshal.StructureToPtr<_structMpv_ByteArray>(structRet, ptrRet, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE:
- _structMpv_Node structRet = ((structMpv_Node)objData).ToNative();
- ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node>);
- Marshal.StructureToPtr<_structMpv_Node>(structRet, ptrRet, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
- structMpv_Node[] lstTmp = (structMpv_Node[])objData;
- _structMpv_Node_List lstRet = new _structMpv_Node_List();
- lstRet.Num = lstTmp.Length;
- lstRet.Values = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node> * lstRet.Num);
- for (int i = 0; i <= lstTmp.Length - 1; i++) {
- _structMpv_Node tmpItm = lstTmp(i).ToNative();
- Marshal.StructureToPtr<_structMpv_Node>(tmpItm, GetPtrFromAry<_structMpv_Node>(lstRet.Values, i), true);
- }
- Marshal.StructureToPtr<_structMpv_Node_List>(lstRet, ptrRet, true);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_MAP:
- Dictionary<string, structMpv_Node> mapTmp = (Dictionary<string, structMpv_Node>)objData;
- _structMpv_Node_List mapRet = new _structMpv_Node_List();
- mapRet.Num = mapTmp.Count;
- mapRet.Keys = AllocateUtf8IntPtrArrayWithSentinel(mapTmp.Keys.ToArray, ref null);
- for (int i = 0; i <= mapTmp.Count - 1; i++) {
- structMpv_Node tmpNode = null;
- mapTmp.TryGetValue(mapTmp.Keys(i), tmpNode);
- _structMpv_Node tmpItm = tmpNode.ToNative();
- Marshal.StructureToPtr<_structMpv_Node>(tmpItm, GetPtrFromAry<_structMpv_Node>(mapRet.Values, i), true);
- }
- Marshal.StructureToPtr<_structMpv_Node_List>(mapRet, ptrRet, true);
- break;
- }
- }
- return ptrRet;
- }
- public static Type GetMpvTypeFromFormat(enumMpv_Format format)
- {
- Type typRet = null;
- switch (format) {
- case enumMpv_Format.MPV_FORMAT_FLAG:
- typRet = typeof(int);
- break;
- case enumMpv_Format.MPV_FORMAT_INT64:
- typRet = typeof(long);
- break;
- case enumMpv_Format.MPV_FORMAT_DOUBLE:
- typRet = typeof(double);
- break;
- case enumMpv_Format.MPV_FORMAT_STRING:
- typRet = typeof(string);
- break;
- case enumMpv_Format.MPV_FORMAT_OSD_STRING:
- typRet = typeof(string);
- break;
- case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
- typRet = typeof(_structMpv_ByteArray);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE:
- typRet = typeof(_structMpv_Node);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
- typRet = typeof(_structMpv_Node_List);
- break;
- case enumMpv_Format.MPV_FORMAT_NODE_MAP:
- typRet = typeof(_structMpv_Node_List);
- break;
- }
- return typRet;
- }
- public static string GetMpvEnum<Typ>(Typ intValue)
- {
- return Enum.GetName(intValue.GetType(), intValue);
- }
- public static string[] GetMpvEnumNames<Typ>()
- {
- return Enum.GetNames(typeof(Typ));
- }
- public static object[] GetMpvEnumValues<Typ>()
- {
- return Enum.GetNames(typeof(Typ));
- }
- public static Dictionary<string, object> GetMpvEnums<Typ>()
- {
- Dictionary<string, object> dictRet = new Dictionary<string, object>();
- foreach (object enumItm in Enum.GetValues(typeof(Typ))) {
- dictRet.Add(Enum.GetName(typeof(Typ), enumItm), enumItm);
- }
- return dictRet;
- }
- public static byte[] GetUtf8Bytes(string s)
- {
- return Encoding.UTF8.GetBytes(s + Convert.ToString(Constants.vbNullChar));
- }
- public static IntPtr AllocateUtf8IntPtrArrayWithSentinel(string[] arr, ref IntPtr[] byteArrayPointers)
- {
- int numberOfStrings = arr.Length + 1;
- // add extra element for extra null pointer last (sentinel)
- byteArrayPointers = new IntPtr[numberOfStrings];
- IntPtr rootPointer = Marshal.AllocCoTaskMem(IntPtr.Size * numberOfStrings);
- for (int index = 0; index <= arr.Length - 1; index++) {
- dynamic bytes = GetUtf8Bytes(arr(index));
- IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
- Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);
- byteArrayPointers(index) = unmanagedPointer;
- }
- Marshal.Copy(byteArrayPointers, 0, rootPointer, numberOfStrings);
- return rootPointer;
- }
- public static string Str_AryChk(object obj)
- {
- string strRet = "";
- if ((obj != null)) {
- if (obj.GetType.IsArray) {
- Array aryTmp = (Array)obj;
- strRet += "[Length=" + aryTmp.Length.ToString + "] {";
- for (int i = 0; i <= aryTmp.Length - 1; i++) {
- strRet += aryTmp.GetValue(i).ToString + (i < aryTmp.Length - 1 ? "; " : "").ToString;
- }
- strRet += "}";
- } else if ((obj as Dictionary<string, structMpv_Node> != null)) {
- List<KeyValuePair<string, structMpv_Node>> mapTmp = (obj as Dictionary<string, structMpv_Node>).ToList();
- strRet += "[Length=" + mapTmp.Count.ToString + "] {";
- for (int i = 0; i <= mapTmp.Count - 1; i++) {
- strRet += mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + (i < mapTmp.Count - 1 ? "; " : "").ToString;
- }
- strRet += "}";
- } else if ((obj as Dictionary<string, object> != null)) {
- List<KeyValuePair<string, object>> mapTmp = (obj as Dictionary<string, object>).ToList();
- strRet += "[Length=" + mapTmp.Count.ToString + "] {";
- for (int i = 0; i <= mapTmp.Count - 1; i++) {
- strRet += mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + (i < mapTmp.Count - 1 ? "; " : "").ToString;
- }
- strRet += "}";
- } else {
- strRet += " " + obj.ToString;
- }
- }
- return strRet;
- }
- }
- public class clsMpv : IDisposable
- {
- private IntPtr _mpvHandle;
- private structCache _cache;
- public event MpvEventEventHandler MpvEvent;
- public delegate void MpvEventEventHandler(object sender, MpvEventArgs e);
- public event MpvPropertyChangeEventHandler MpvPropertyChange;
- public delegate void MpvPropertyChangeEventHandler(object sender, MpvPropertyChangeEventArgs e);
- public event MpvEndOfFileEventHandler MpvEndOfFile;
- public delegate void MpvEndOfFileEventHandler(object sender, MpvEndOfFileEventArgs e);
- public clsMpv(IntPtr hWnd)
- {
- long windowId = hWnd.ToInt64();
- _mpvHandle = moduleMpv.Mpv_DLL.mpvCreate.Invoke();
- moduleMpv.Mpv_DLL.mpvInitialize.Invoke(_mpvHandle);
- _cache = new structCache(_mpvHandle);
- moduleMpv.Mpv_DLL.mpvRequestLogMessages(_mpvHandle, moduleMpv.GetUtf8Bytes("v"));
- moduleMpv.Mpv_DLL.mpvRequestEvent(_mpvHandle, enumMpv_Event.MPV_EVENT_SEEK, 1);
- moduleMpv.Mpv_DLL.mpvRequestEvent(_mpvHandle, enumMpv_Event.MPV_EVENT_CLIENT_MESSAGE, 1);
- moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("ytdl"), moduleMpv.GetUtf8Bytes("yes"));
- moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("msg-level"), moduleMpv.GetUtf8Bytes("all=trace"));
- moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("keepaspect"), moduleMpv.GetUtf8Bytes("no"));
- moduleMpv.Mpv_DLL.mpvSetOption(_mpvHandle, moduleMpv.GetUtf8Bytes("wid"), enumMpv_Format.MPV_FORMAT_INT64, hWnd);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("duration"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("time-pos"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-size"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-free"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-used"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-buffering-state"), enumMpv_Format.MPV_FORMAT_INT64);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("demuxer-cache-time"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("volume"), enumMpv_Format.MPV_FORMAT_DOUBLE);
- Task.Run(new Action(threadEventLoop));
- }
- public IntPtr Handle {
- get { return _mpvHandle; }
- }
- private void threadEventLoop()
- {
- while (!disposedValue) {
- try {
- if (_mpvHandle != IntPtr.Zero) {
- IntPtr lpBuffer = moduleMpv.Mpv_DLL.mpvWaitEvent(_mpvHandle, -1);
- _structMpv_Event pEvent = Marshal.PtrToStructure<_structMpv_Event>(lpBuffer);
- structMpv_Event objEvent = new structMpv_Event(pEvent);
- lpBuffer = IntPtr.Zero;
- pEvent = null;
- if (MpvEvent != null) {
- MpvEvent(this, new MpvEventArgs(objEvent));
- }
- if (objEvent.EventID == enumMpv_Event.MPV_EVENT_PROPERTY_CHANGE) {
- if (MpvPropertyChange != null) {
- MpvPropertyChange(this, new MpvPropertyChangeEventArgs(objEvent));
- }
- }
- if (objEvent.EventID == enumMpv_Event.MPV_EVENT_END_FILE) {
- if (MpvEndOfFile != null) {
- MpvEndOfFile(this, new MpvEndOfFileEventArgs(objEvent));
- }
- }
- }
- Application.DoEvents();
- } catch (Exception ex) {
- }
- }
- }
- public void LoadFile(string strURi, double dblStartPos)
- {
- if (_mpvHandle == IntPtr.Zero) {
- return;
- }
- DoMpvCommand("loadfile", strURi, "replace", "start=" + (dblStartPos).ToString);
- }
- public void Pause()
- {
- moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, true);
- }
- public void Play()
- {
- moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, false);
- }
- public void Stop()
- {
- if (_mpvHandle == IntPtr.Zero) {
- return;
- }
- DoMpvCommand("stop");
- }
- public bool IsPaused {
- get { return Convert.ToBoolean(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG)); }
- set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, value); }
- }
- public double TimePosition {
- get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "time-pos", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
- set {
- if (_mpvHandle != IntPtr.Zero) {
- DoMpvCommand("seek", value.ToString(CultureInfo.InvariantCulture), "absolute");
- }
- }
- }
- public double TimeLength {
- get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "duration", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
- }
- public double Volume {
- get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "volume", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
- set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "volume", enumMpv_Format.MPV_FORMAT_DOUBLE, value); }
- }
- public structCache Cache {
- get { return _cache; }
- }
- public object PropertyValue {
- get { return moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, strPropertyName, intFormat); }
- set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, strPropertyName, intFormat, value); }
- }
- public object PropertyValue {
- get { return ((structMpv_Node)moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, strPropertyName, enumMpv_Format.MPV_FORMAT_NODE)).Data; }
- set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, strPropertyName, enumMpv_Format.MPV_FORMAT_NODE, new structMpv_Node(value)); }
- }
- public void DoMpvCommand(params string[] args)
- {
- IntPtr[] byteArrayPointers = new IntPtr[];
- dynamic mainPtr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel(args, ref byteArrayPointers);
- moduleMpv.Mpv_DLL.mpvCommand(_mpvHandle, mainPtr);
- foreach (IntPtr ptr in byteArrayPointers) {
- Marshal.FreeHGlobal(ptr);
- }
- Marshal.FreeHGlobal(mainPtr);
- }
- private bool disposedValue;
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue) {
- if (disposing) {
- if (_mpvHandle != IntPtr.Zero) {
- moduleMpv.Mpv_DLL.mpvUnobserveProperty(_mpvHandle, 0);
- moduleMpv.Mpv_DLL.mpvTerminateDestroy(_mpvHandle);
- _mpvHandle = IntPtr.Zero;
- }
- }
- }
- disposedValue = true;
- }
- public void Dispose()
- {
- Dispose(true);
- }
- }
- }
- //=======================================================
- //Service provided by Telerik (www.telerik.com)
- //Conversion powered by NRefactory.
- //Twitter: @telerik
- //Facebook: facebook.com/telerik
- //=======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement