Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 41.73 KB | None | 0 0
  1.  
  2. using Microsoft.VisualBasic;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.ComponentModel;
  9. using System.Globalization;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. namespace libMpv
  13. {
  14.     public static class moduleMpv
  15.     {
  16.  
  17.         public static clsMpvDynamicLib Mpv_DLL = new clsMpvDynamicLib();
  18.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = false)]
  19.         static internal IntPtr LoadLibrary(string dllToLoad)
  20.         {
  21.         }
  22.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = false)]
  23.         static internal IntPtr GetProcAddress(IntPtr hModule, string procedureName)
  24.         {
  25.         }
  26.         static internal object GetDllType(IntPtr intptrAssembly, Type type, string name)
  27.         {
  28.             IntPtr address = GetProcAddress(intptrAssembly, name);
  29.             if (address != IntPtr.Zero) {
  30.                 return Marshal.GetDelegateForFunctionPointer(address, type);
  31.             }
  32.             return null;
  33.         }
  34.  
  35.         public enum enumMpv_Format
  36.         {
  37.             MPV_FORMAT_NONE = 0,
  38.             MPV_FORMAT_STRING = 1,
  39.             MPV_FORMAT_OSD_STRING = 2,
  40.             MPV_FORMAT_FLAG = 3,
  41.             MPV_FORMAT_INT64 = 4,
  42.             MPV_FORMAT_DOUBLE = 5,
  43.             MPV_FORMAT_NODE = 6,
  44.             MPV_FORMAT_NODE_ARRAY = 7,
  45.             MPV_FORMAT_NODE_MAP = 8,
  46.             MPV_FORMAT_BYTE_ARRAY = 9
  47.         }
  48.         public enum enumMpv_Event
  49.         {
  50.             MPV_EVENT_NONE = 0,
  51.             MPV_EVENT_SHUTDOWN = 1,
  52.             MPV_EVENT_LOG_MESSAGE = 2,
  53.             MPV_EVENT_GET_PROPERTY_REPLY = 3,
  54.             MPV_EVENT_SET_PROPERTY_REPLY = 4,
  55.             MPV_EVENT_COMMAND_REPLY = 5,
  56.             MPV_EVENT_START_FILE = 6,
  57.             MPV_EVENT_END_FILE = 7,
  58.             MPV_EVENT_FILE_LOADED = 8,
  59.             MPV_EVENT_TRACKS_CHANGED = 9,
  60.             MPV_EVENT_TRACK_SWITCHED = 10,
  61.             MPV_EVENT_IDLE = 11,
  62.             MPV_EVENT_PAUSE = 12,
  63.             MPV_EVENT_UNPAUSE = 13,
  64.             MPV_EVENT_TICK = 14,
  65.             MPV_EVENT_SCRIPT_INPUT_DISPATCH = 15,
  66.             MPV_EVENT_CLIENT_MESSAGE = 16,
  67.             MPV_EVENT_VIDEO_RECONFIG = 17,
  68.             MPV_EVENT_AUDIO_RECONFIG = 18,
  69.             MPV_EVENT_METADATA_UPDATE = 19,
  70.             MPV_EVENT_SEEK = 20,
  71.             MPV_EVENT_PLAYBACK_RESTART = 21,
  72.             MPV_EVENT_PROPERTY_CHANGE = 22,
  73.             MPV_EVENT_CHAPTER_CHANGE = 23,
  74.             MPV_EVENT_QUEUE_OVERFLOW = 24
  75.         }
  76.         public enum enumMpv_ReasonEOF
  77.         {
  78.             MPV_END_FILE_REASON_EOF = 0,
  79.             MPV_END_FILE_REASON_STOP = 2,
  80.             MPV_END_FILE_REASON_QUIT = 3,
  81.             MPV_END_FILE_REASON_ERROR = 4,
  82.             MPV_END_FILE_REASON_REDIRECT = 5
  83.         }
  84.         public enum enumMpv_Error
  85.         {
  86.             MPV_ERROR_SUCCESS = 0,
  87.             MPV_ERROR_EVENT_QUEUE_FULL = -1,
  88.             MPV_ERROR_NOMEM = -2,
  89.             MPV_ERROR_UNINITIALIZED = -3,
  90.             MPV_ERROR_INVALID_PARAMETER = -4,
  91.             MPV_ERROR_OPTION_NOT_FOUND = -5,
  92.             MPV_ERROR_OPTION_FORMAT = -6,
  93.             MPV_ERROR_OPTION_ERROR = -7,
  94.             MPV_ERROR_PROPERTY_NOT_FOUND = -8,
  95.             MPV_ERROR_PROPERTY_FORMAT = -9,
  96.             MPV_ERROR_PROPERTY_UNAVAILABLE = -10,
  97.             MPV_ERROR_PROPERTY_ERROR = -11,
  98.             MPV_ERROR_COMMAND = -12,
  99.             MPV_ERROR_LOADING_FAILED = -13,
  100.             MPV_ERROR_AO_INIT_FAILED = -14,
  101.             MPV_ERROR_VO_INIT_FAILED = -15,
  102.             MPV_ERROR_NOTHING_TO_PLAY = -16,
  103.             MPV_ERROR_UNKNOWN_FORMAT = -17,
  104.             MPV_ERROR_UNSUPPORTED = -18,
  105.             MPV_ERROR_NOT_IMPLEMENTED = -19,
  106.             MPV_ERROR_GENERIC = -20
  107.         }
  108.         public enum enumMpv_LogLevel
  109.         {
  110.             MPV_LOG_LEVEL_NONE = 0,
  111.             MPV_LOG_LEVEL_FATAL = 10,
  112.             MPV_LOG_LEVEL_ERROR = 20,
  113.             MPV_LOG_LEVEL_WARN = 30,
  114.             MPV_LOG_LEVEL_INFO = 40,
  115.             MPV_LOG_LEVEL_V = 50,
  116.             MPV_LOG_LEVEL_DEBUG = 60,
  117.             MPV_LOG_LEVEL_TRACE = 70
  118.         }
  119.  
  120.         [StructLayout(LayoutKind.Explicit)]
  121.         internal struct _structMpv_Node
  122.         {
  123.             [FieldOffset(0)]
  124.             public double Dbl;
  125.             [FieldOffset(0)]
  126.             public long Int64;
  127.             [FieldOffset(0)]
  128.             public bool Flag;
  129.             [FieldOffset(0)]
  130.             public IntPtr Ptr;
  131.             [FieldOffset(8)]
  132.             public enumMpv_Format Format;
  133.         }
  134.         internal struct _structMpv_Node_List
  135.         {
  136.             public int Num;
  137.             public IntPtr Values;
  138.             public IntPtr Keys;
  139.         }
  140.         internal struct _structMpv_ByteArray
  141.         {
  142.             public IntPtr pData;
  143.             public int Size;
  144.         }
  145.         internal struct _structMpv_Event_LogMessage
  146.         {
  147.             public string Prefix;
  148.             public string Level;
  149.             public string Text;
  150.         }
  151.         internal struct _structMpv_Event_EOF
  152.         {
  153.             public enumMpv_ReasonEOF ReasonID;
  154.             public enumMpv_Error ErrorID;
  155.         }
  156.         internal struct _structMpv_Event_Property
  157.         {
  158.             public string Name;
  159.             public enumMpv_Format Format;
  160.             public IntPtr ptrData;
  161.         }
  162.         internal struct _structMpv_Event
  163.         {
  164.             public enumMpv_Event EventID;
  165.             public enumMpv_Error ErrorID;
  166.             public long reply_userdata;
  167.             public IntPtr ptrData;
  168.         }
  169.  
  170.         public struct structMpv_Node
  171.         {
  172.             public enumMpv_Format FormatID;
  173.             public object Data;
  174.             public structMpv_Node(object obj)
  175.             {
  176.                 Type typ = obj.GetType();
  177.                 switch (typ) {
  178.                     case typeof(bool):
  179.                         FormatID = enumMpv_Format.MPV_FORMAT_FLAG;
  180.                         break;
  181.                     case typeof(long):
  182.                         FormatID = enumMpv_Format.MPV_FORMAT_INT64;
  183.                         break;
  184.                     case typeof(double):
  185.                         FormatID = enumMpv_Format.MPV_FORMAT_DOUBLE;
  186.                         break;
  187.                     case typeof(string):
  188.                         FormatID = enumMpv_Format.MPV_FORMAT_STRING;
  189.                         break;
  190.                     case typeof(byte[]):
  191.                         FormatID = enumMpv_Format.MPV_FORMAT_BYTE_ARRAY;
  192.                         break;
  193.                     case typeof(structMpv_Node):
  194.                         FormatID = enumMpv_Format.MPV_FORMAT_NODE;
  195.                         break;
  196.                     case typeof(structMpv_Node[]):
  197.                         FormatID = enumMpv_Format.MPV_FORMAT_NODE_ARRAY;
  198.                         break;
  199.                     case typeof(Dictionary<string, structMpv_Node>):
  200.                         FormatID = enumMpv_Format.MPV_FORMAT_NODE_MAP;
  201.                         break;
  202.                 }
  203.                 Data = obj;
  204.             }
  205.             internal structMpv_Node(_structMpv_Node @ref)
  206.             {
  207.                 FormatID = @ref.Format;
  208.                 switch (FormatID) {
  209.                     case enumMpv_Format.MPV_FORMAT_FLAG:
  210.                         Data = @ref.Flag;
  211.                         break;
  212.                     case enumMpv_Format.MPV_FORMAT_INT64:
  213.                         Data = @ref.Int64;
  214.                         break;
  215.                     case enumMpv_Format.MPV_FORMAT_DOUBLE:
  216.                         Data = @ref.Dbl;
  217.                         break;
  218.                     case enumMpv_Format.MPV_FORMAT_STRING:
  219.                         Data = Marshal.PtrToStringAnsi(@ref.Ptr);
  220.                         break;
  221.                     case enumMpv_Format.MPV_FORMAT_OSD_STRING:
  222.                         Data = Marshal.PtrToStringAnsi(@ref.Ptr);
  223.                         break;
  224.                     case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
  225.                         _structMpv_ByteArray ptrAry = Marshal.PtrToStructure<_structMpv_ByteArray>(@ref.Ptr);
  226.                         byte[] aryRet = new byte[ptrAry.Size + 1];
  227.                         Marshal.PtrToStructure<byte[]>(ptrAry.pData, aryRet);
  228.                         Data = aryRet;
  229.                         break;
  230.                     case enumMpv_Format.MPV_FORMAT_NODE:
  231.                         Data = new structMpv_Node(Marshal.PtrToStructure<_structMpv_Node>(@ref.Ptr));
  232.                         break;
  233.                     case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
  234.                         _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(@ref.Ptr);
  235.                         structMpv_Node[] retLst = new structMpv_Node[ptrLst.Num];
  236.                         for (int i = 0; i <= ptrLst.Num - 1; i++) {
  237.                             IntPtr ptrNewItm = moduleMpv.GetPtrFromAry(ptrLst.Values, i);
  238.                             _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
  239.                             retLst(i) = new structMpv_Node(objNewItm);
  240.                         }
  241.  
  242.                         Data = retLst;
  243.                         break;
  244.                     case enumMpv_Format.MPV_FORMAT_NODE_MAP:
  245.                         _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(@ref.Ptr);
  246.                         Dictionary<string, structMpv_Node> retMap = new Dictionary<string, structMpv_Node>();
  247.                         for (int i = 0; i <= ptrLst.Num - 1; i++) {
  248.                             IntPtr ptrNewKey = moduleMpv.GetPtrFromAry(ptrLst.Keys, i);
  249.                             string strNewKey = Marshal.PtrToStringAnsi(ptrNewKey);
  250.                             IntPtr ptrNewItm = moduleMpv.GetPtrFromAry(ptrLst.Values, i);
  251.                             _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
  252.                             retMap.Add(strNewKey, new structMpv_Node(objNewItm));
  253.                         }
  254.  
  255.                         Data = retMap;
  256.                         break;
  257.                 }
  258.             }
  259.             public string Format {
  260.                 get { return moduleMpv.GetMpvEnum(FormatID); }
  261.             }
  262.             internal _structMpv_Node ToNative()
  263.             {
  264.                 _structMpv_Node structRet = new _structMpv_Node();
  265.                 structRet.Format = FormatID;
  266.                 switch (FormatID) {
  267.                     case enumMpv_Format.MPV_FORMAT_FLAG:
  268.                         structRet.Flag = Convert.ToBoolean(Data);
  269.                         break;
  270.                     case enumMpv_Format.MPV_FORMAT_INT64:
  271.                         structRet.Int64 = Convert.ToInt64(Data);
  272.                         break;
  273.                     case enumMpv_Format.MPV_FORMAT_DOUBLE:
  274.                         structRet.Dbl = Convert.ToDouble(Data);
  275.                         break;
  276.                     case enumMpv_Format.MPV_FORMAT_STRING:
  277.                         structRet.Ptr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel({ Data.ToString }, ref null);
  278.                         break;
  279.                     case enumMpv_Format.MPV_FORMAT_OSD_STRING:
  280.                         structRet.Ptr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel({ Data.ToString }, ref null);
  281.                         break;
  282.                     case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
  283.                         byte[] dataTmp = (byte[])Data;
  284.                         _structMpv_ByteArray aryRet = new _structMpv_ByteArray();
  285.                         aryRet.Size = dataTmp.Length;
  286.                         aryRet.pData = Marshal.AllocHGlobal(Marshal.SizeOf<byte> * aryRet.Size);
  287.                         Marshal.Copy(dataTmp, 0, aryRet.pData, aryRet.Size);
  288.                         structRet.Ptr = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_ByteArray>);
  289.                         Marshal.StructureToPtr<_structMpv_ByteArray>(aryRet, structRet.Ptr, true);
  290.                         break;
  291.                     case enumMpv_Format.MPV_FORMAT_NODE:
  292.                         _structMpv_Node objRet = ((structMpv_Node)Data).ToNative();
  293.                         structRet.Ptr = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node>);
  294.                         Marshal.StructureToPtr<_structMpv_Node>(objRet, structRet.Ptr, true);
  295.                         break;
  296.                     case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
  297.                         structMpv_Node[] lstTmp = (structMpv_Node[])Data;
  298.                         _structMpv_Node_List lstRet = new _structMpv_Node_List();
  299.                         lstRet.Num = lstTmp.Length;
  300.                         lstRet.Values = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node> * lstRet.Num);
  301.                         for (int i = 0; i <= lstTmp.Length - 1; i++) {
  302.                             _structMpv_Node tmpItm = lstTmp(i).ToNative();
  303.                             Marshal.StructureToPtr<_structMpv_Node>(tmpItm, moduleMpv.GetPtrFromAry(lstRet.Values, i), true);
  304.                         }
  305.  
  306.                         Marshal.StructureToPtr<_structMpv_Node_List>(lstRet, structRet.Ptr, true);
  307.                         break;
  308.                     case enumMpv_Format.MPV_FORMAT_NODE_MAP:
  309.                         Dictionary<string, structMpv_Node> mapTmp = (Dictionary<string, structMpv_Node>)Data;
  310.                         _structMpv_Node_List mapRet = new _structMpv_Node_List();
  311.                         mapRet.Num = mapTmp.Count;
  312.                         mapRet.Keys = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel(mapTmp.Keys.ToArray, ref null);
  313.                         for (int i = 0; i <= mapTmp.Count - 1; i++) {
  314.                             structMpv_Node tmpNode = null;
  315.                             mapTmp.TryGetValue(mapTmp.Keys(i), tmpNode);
  316.                             _structMpv_Node tmpItm = tmpNode.ToNative();
  317.                             Marshal.StructureToPtr<_structMpv_Node>(tmpItm, moduleMpv.GetPtrFromAry(mapRet.Values, i), true);
  318.                         }
  319.  
  320.                         Marshal.StructureToPtr<_structMpv_Node_List>(mapRet, structRet.Ptr, true);
  321.                         break;
  322.                 }
  323.                 return structRet;
  324.             }
  325.             public override string ToString()
  326.             {
  327.                 string strRet = "<" + Format + ">";
  328.                 if ((Data != null)) {
  329.                     if (Data.GetType.IsArray) {
  330.                         Array aryTmp = (Array)Data;
  331.                         strRet += " [Length=" + aryTmp.Length.ToString + "] {";
  332.                         for (int i = 0; i <= aryTmp.Length - 1; i++) {
  333.                             strRet += " " + aryTmp.GetValue(i).ToString + ";";
  334.                         }
  335.                         strRet += "}";
  336.                     } else if ((Data as Dictionary<string, structMpv_Node> != null)) {
  337.                         List<KeyValuePair<string, structMpv_Node>> mapTmp = (Data as Dictionary<string, structMpv_Node>).ToList();
  338.                         strRet += " [Length=" + mapTmp.Count.ToString + "] {";
  339.                         for (int i = 0; i <= mapTmp.Count - 1; i++) {
  340.                             strRet += " " + mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + ";";
  341.                         }
  342.                         strRet += "}";
  343.                     } else {
  344.                         strRet += " " + Data.ToString;
  345.                     }
  346.                 }
  347.                 return strRet;
  348.             }
  349.         }
  350.         public struct structMpv_Event
  351.         {
  352.             public enumMpv_Event EventID;
  353.             public enumMpv_Error ErrorID;
  354.             public long UserDataReply;
  355.             public string DataName;
  356.             public object Data;
  357.             internal structMpv_Event(_structMpv_Event @ref)
  358.             {
  359.                 EventID = @ref.EventID;
  360.                 ErrorID = @ref.ErrorID;
  361.                 UserDataReply = @ref.reply_userdata;
  362.                 UpdateFromDataPointer(@ref.ptrData);
  363.             }
  364.             public string EventName {
  365.                 get { return moduleMpv.Mpv_DLL.mpvGetEventNameString(EventID); }
  366.             }
  367.             public string ErrorName {
  368.                 get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
  369.             }
  370.             internal void UpdateFromDataPointer(IntPtr ptrData)
  371.             {
  372.                 if (ptrData != IntPtr.Zero) {
  373.                     switch (EventID) {
  374.                         case enumMpv_Event.MPV_EVENT_LOG_MESSAGE:
  375.                             _structMpv_Event_LogMessage pEvent = Marshal.PtrToStructure<_structMpv_Event_LogMessage>(ptrData);
  376.                             DataName = pEvent.Prefix;
  377.                             Data = pEvent.Text;
  378.                             break;
  379.                         case enumMpv_Event.MPV_EVENT_PROPERTY_CHANGE:
  380.                             _structMpv_Event_Property pEvent = Marshal.PtrToStructure<_structMpv_Event_Property>(ptrData);
  381.                             DataName = pEvent.Name;
  382.                             Data = moduleMpv.GetMpvObjectFromFormat(pEvent.ptrData, pEvent.Format);
  383.                             break;
  384.                         case enumMpv_Event.MPV_EVENT_END_FILE:
  385.                             _structMpv_Event_EOF pEvent = Marshal.PtrToStructure<_structMpv_Event_EOF>(ptrData);
  386.                             DataName = moduleMpv.GetMpvEnum(pEvent.ReasonID);
  387.                             ErrorID = pEvent.ErrorID;
  388.                             break;
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.         public class MpvEventArgs : EventArgs
  394.         {
  395.             public enumMpv_Event EventID;
  396.             public enumMpv_Error ErrorID;
  397.             public string DataName;
  398.             public object Data;
  399.             public MpvEventArgs(structMpv_Event @ref)
  400.             {
  401.                 EventID = @ref.EventID;
  402.                 ErrorID = @ref.ErrorID;
  403.                 DataName = @ref.DataName;
  404.                 Data = @ref.Data;
  405.             }
  406.             public string EventName {
  407.                 get { return moduleMpv.Mpv_DLL.mpvGetEventNameString(EventID); }
  408.             }
  409.             public string ErrorName {
  410.                 get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
  411.             }
  412.         }
  413.         public class MpvPropertyChangeEventArgs : EventArgs
  414.         {
  415.             public string PropertyName;
  416.             public object NewValue;
  417.             public MpvPropertyChangeEventArgs()
  418.             {
  419.                 PropertyName = "";
  420.                 NewValue = null;
  421.             }
  422.             public MpvPropertyChangeEventArgs(structMpv_Event @ref)
  423.             {
  424.                 PropertyName = @ref.DataName;
  425.                 NewValue = @ref.Data;
  426.             }
  427.             public MpvPropertyChangeEventArgs(string strPropertyName, object objNewValue)
  428.             {
  429.                 PropertyName = strPropertyName;
  430.                 NewValue = objNewValue;
  431.             }
  432.         }
  433.         public class MpvEndOfFileEventArgs : EventArgs
  434.         {
  435.             public string Reason;
  436.             public enumMpv_Error ErrorID;
  437.             public MpvEndOfFileEventArgs()
  438.             {
  439.                 Reason = "";
  440.                 ErrorID = 0;
  441.             }
  442.             public MpvEndOfFileEventArgs(enumMpv_ReasonEOF intReason, enumMpv_Error intError)
  443.             {
  444.                 Reason = moduleMpv.GetMpvEnum(intReason);
  445.                 ErrorID = intError;
  446.             }
  447.             public MpvEndOfFileEventArgs(structMpv_Event @ref)
  448.             {
  449.                 Reason = @ref.DataName;
  450.                 ErrorID = @ref.ErrorID;
  451.             }
  452.             public enumMpv_ReasonEOF ReasonID {
  453.                 get {
  454.                     enumMpv_ReasonEOF intRet = default(enumMpv_ReasonEOF);
  455.                     Enum.TryParse(Reason, true, intRet);
  456.                     return intRet;
  457.                 }
  458.             }
  459.             public string ErrorName {
  460.                 get { return moduleMpv.Mpv_DLL.mpvGetErrorString(ErrorID); }
  461.             }
  462.         }
  463.         public struct structCache
  464.         {
  465.             private IntPtr _ptrHandle;
  466.             public structCache(IntPtr mpvHandle)
  467.             {
  468.                 _ptrHandle = mpvHandle;
  469.             }
  470.             public long Size {
  471.                 get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-size", enumMpv_Format.MPV_FORMAT_INT64)); }
  472.                 set { moduleMpv.Mpv_DLL.mpvSetProperty(_ptrHandle, "cache-size", enumMpv_Format.MPV_FORMAT_INT64, value); }
  473.             }
  474.             public double Percent {
  475.                 get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
  476.             }
  477.             public long SizeUsed {
  478.                 get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-used", enumMpv_Format.MPV_FORMAT_INT64)); }
  479.             }
  480.             public long SizeFree {
  481.                 get { return Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "cache-free", enumMpv_Format.MPV_FORMAT_INT64)); }
  482.             }
  483.             public double Position {
  484.                 get {
  485.                     double dblRet = 0;
  486.                     double dblCacheSecs = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "demuxer-cache-time", enumMpv_Format.MPV_FORMAT_DOUBLE));
  487.                     double dblLength = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "duration", enumMpv_Format.MPV_FORMAT_DOUBLE));
  488.                     double dblPos = Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "time-pos", enumMpv_Format.MPV_FORMAT_DOUBLE));
  489.                     long intFileSize = Convert.ToInt64(moduleMpv.Mpv_DLL.mpvGetProperty(_ptrHandle, "file-size", enumMpv_Format.MPV_FORMAT_INT64));
  490.                     double dblFileSize = Convert.ToDouble(intFileSize) / 1024;
  491.                     double dblSizePassed = (dblPos / dblLength) * dblFileSize;
  492.                     dblRet = (dblLength) * (SizeUsed / (dblFileSize - dblSizePassed)) + dblCacheSecs;
  493.                     if ((dblRet > dblLength))
  494.                         dblRet = dblLength;
  495.                     return dblRet;
  496.                 }
  497.             }
  498.             public override string ToString()
  499.             {
  500.                 return SizeUsed.ToString("#,###") + " of " + Size.ToString("#,###") + " KiB Used. BufferEnd At " + Position.ToString("0.0#");
  501.             }
  502.         }
  503.         public class clsMpvDynamicLib : IDisposable
  504.         {
  505.  
  506.  
  507.             private IntPtr _libMpvDll;
  508.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  509.             public delegate IntPtr delegateMpvCreate();
  510.  
  511.             public delegateMpvCreate mpvCreate;
  512.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  513.             public delegate int delegateMpvInitialize(IntPtr mpvHandle);
  514.  
  515.             public delegateMpvInitialize mpvInitialize;
  516.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  517.             public delegate int delegateMpvCommand(IntPtr mpvHandle, IntPtr strings);
  518.  
  519.             public delegateMpvCommand mpvCommand;
  520.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  521.             public delegate int delegateMpvTerminateDestroy(IntPtr mpvHandle);
  522.  
  523.             public delegateMpvTerminateDestroy mpvTerminateDestroy;
  524.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  525.             public delegate IntPtr delegateMpvWaitEvent(IntPtr mpvHandle, double timeout);
  526.  
  527.             public delegateMpvWaitEvent mpvWaitEvent;
  528.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  529.             public delegate int delegateMpvSetOption(IntPtr mpvHandle, byte[] name, enumMpv_Format format, ref IntPtr data);
  530.  
  531.             public delegateMpvSetOption mpvSetOption;
  532.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  533.             public delegate int delegateMpvSetOptionString(IntPtr mpvHandle, byte[] name, byte[] value);
  534.  
  535.             public delegateMpvSetOptionString mpvSetOptionString;
  536.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  537.             private delegate int delegateMpvGetPropertystring(IntPtr mpvHandle, byte[] name, ref IntPtr data);
  538.             private delegateMpvGetPropertystring _mpvGetPropertyString;
  539.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  540.             private delegate int delegateMpvSetPropertystring(IntPtr mpvHandle, byte[] name, byte[] value);
  541.  
  542.             private delegateMpvSetPropertystring _mpvSetPropertyString;
  543.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  544.             private delegate int delegateMpvGetProperty(IntPtr mpvHandle, byte[] name, enumMpv_Format format, IntPtr data);
  545.             private delegateMpvGetProperty _mpvGetProperty;
  546.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  547.             private delegate int delegateMpvSetProperty(IntPtr mpvHandle, byte[] name, enumMpv_Format format, IntPtr data);
  548.  
  549.             private delegateMpvSetProperty _mpvSetProperty;
  550.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  551.             public delegate int delegateMpvRequestLogMessages(IntPtr mpvHandle, byte[] strLevel);
  552.             public delegateMpvRequestLogMessages mpvRequestLogMessages;
  553.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  554.             public delegate int delegateMpvRequestEvent(IntPtr mpvHandle, enumMpv_Event intEvent, int intEnable);
  555.             public delegateMpvRequestEvent mpvRequestEvent;
  556.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  557.             public delegate int delegateMpvObserveProperty(IntPtr mpvHandle, int intUserGroup, byte[] name, enumMpv_Format format);
  558.             public delegateMpvObserveProperty mpvObserveProperty;
  559.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  560.             public delegate int delegateMpvUnobserveProperty(IntPtr mpvHandle, int intUserGroup);
  561.  
  562.             public delegateMpvUnobserveProperty mpvUnobserveProperty;
  563.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  564.             public delegate void delegateMpvFree(IntPtr data);
  565.  
  566.             public delegateMpvFree mpvFree;
  567.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  568.             public delegate string delegateMpvErrorString(enumMpv_Error intError);
  569.  
  570.             public delegateMpvErrorString mpvGetErrorString;
  571.             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  572.             private delegate IntPtr delegateMpvEventNameString(enumMpv_Event intEvent);
  573.  
  574.             private delegateMpvEventNameString _mpvGetEventNameString;
  575.             public clsMpvDynamicLib()
  576.             {
  577.                 _libMpvDll = moduleMpv.LoadLibrary(Application.StartupPath + "\\mpv-1.dll");
  578.                 // The dll is included in the DEV builds by lachs0r: https://mpv.srsfckn.biz/
  579.                 mpvCreate = (delegateMpvCreate)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvCreate), "mpv_create");
  580.                 mpvInitialize = (delegateMpvInitialize)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvInitialize), "mpv_initialize");
  581.                 mpvTerminateDestroy = (delegateMpvTerminateDestroy)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvTerminateDestroy), "mpv_terminate_destroy");
  582.                 mpvCommand = (delegateMpvCommand)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvCommand), "mpv_command");
  583.                 mpvSetOption = (delegateMpvSetOption)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetOption), "mpv_set_option");
  584.                 mpvSetOptionString = (delegateMpvSetOptionString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetOptionString), "mpv_set_option_string");
  585.                 _mpvGetPropertyString = (delegateMpvGetPropertystring)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvGetPropertystring), "mpv_get_property_string");
  586.                 _mpvSetPropertyString = (delegateMpvSetPropertystring)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetPropertystring), "mpv_set_property_string");
  587.                 _mpvGetProperty = (delegateMpvGetProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvGetProperty), "mpv_get_property");
  588.                 _mpvSetProperty = (delegateMpvSetProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvSetProperty), "mpv_set_property");
  589.                 mpvWaitEvent = (delegateMpvWaitEvent)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvWaitEvent), "mpv_wait_event");
  590.                 mpvRequestLogMessages = (delegateMpvRequestLogMessages)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvRequestLogMessages), "mpv_request_log_messages");
  591.                 mpvRequestEvent = (delegateMpvRequestEvent)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvRequestEvent), "mpv_request_event");
  592.                 mpvObserveProperty = (delegateMpvObserveProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvObserveProperty), "mpv_observe_property");
  593.                 mpvUnobserveProperty = (delegateMpvUnobserveProperty)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvUnobserveProperty), "mpv_unobserve_property");
  594.                 mpvFree = (delegateMpvFree)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvFree), "mpv_free");
  595.                 mpvGetErrorString = (delegateMpvErrorString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvErrorString), "mpv_error_string");
  596.                 _mpvGetEventNameString = (delegateMpvEventNameString)moduleMpv.GetDllType(_libMpvDll, typeof(delegateMpvEventNameString), "mpv_event_name");
  597.             }
  598.             private bool disposedValue;
  599.             protected virtual void Dispose(bool disposing)
  600.             {
  601.                 if (!disposedValue) {
  602.                     if (disposing) {
  603.                         _libMpvDll = IntPtr.Zero;
  604.                         mpvCreate = null;
  605.                         mpvInitialize = null;
  606.                         mpvTerminateDestroy = null;
  607.                         mpvCommand = null;
  608.                         mpvSetOption = null;
  609.                         mpvSetOptionString = null;
  610.                         _mpvGetPropertyString = null;
  611.                         _mpvSetPropertyString = null;
  612.                         _mpvGetProperty = null;
  613.                         _mpvSetProperty = null;
  614.                         mpvWaitEvent = null;
  615.                         mpvRequestLogMessages = null;
  616.                         mpvRequestEvent = null;
  617.                         mpvObserveProperty = null;
  618.                         mpvUnobserveProperty = null;
  619.                         mpvFree = null;
  620.                         mpvGetErrorString = null;
  621.                         _mpvGetEventNameString = null;
  622.                     }
  623.                 }
  624.                 disposedValue = true;
  625.             }
  626.             public void Dispose()
  627.             {
  628.                 Dispose(true);
  629.             }
  630.  
  631.             public object mpvGetProperty(IntPtr _ptrHandle, string strPropertyName, enumMpv_Format intFormat)
  632.             {
  633.                 object objRet = null;
  634.                 if (_ptrHandle != IntPtr.Zero) {
  635.                     IntPtr lpBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(moduleMpv.GetMpvTypeFromFormat(intFormat)));
  636.                     int intNum = _mpvGetProperty(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), intFormat, lpBuffer);
  637.                     if (lpBuffer != IntPtr.Zero)
  638.                         objRet = moduleMpv.GetMpvObjectFromFormat(lpBuffer, intFormat);
  639.                     Marshal.FreeHGlobal(lpBuffer);
  640.                 }
  641.                 return objRet;
  642.             }
  643.             public void mpvSetProperty(IntPtr _ptrHandle, string strPropertyName, enumMpv_Format intFormat, object obj)
  644.             {
  645.                 if (_ptrHandle == IntPtr.Zero) {
  646.                     return;
  647.                 }
  648.                 _mpvSetProperty(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), intFormat, moduleMpv.GetMpvValueFromObject(obj, intFormat));
  649.             }
  650.             public string mpvGetPropertyString(IntPtr _ptrHandle, string strPropertyName)
  651.             {
  652.                 string strRet = "";
  653.                 if (_ptrHandle != IntPtr.Zero) {
  654.                     IntPtr lpBuffer = IntPtr.Zero;
  655.                     _mpvGetPropertyString(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), lpBuffer);
  656.                     strRet = Marshal.PtrToStringAnsi(lpBuffer);
  657.                     mpvFree(lpBuffer);
  658.                 }
  659.                 return strRet;
  660.             }
  661.             public void mpvSetPropertyString(IntPtr _ptrHandle, string strPropertyName, string str)
  662.             {
  663.                 if (_ptrHandle == IntPtr.Zero) {
  664.                     return;
  665.                 }
  666.                 _mpvSetPropertyString(_ptrHandle, moduleMpv.GetUtf8Bytes(strPropertyName), moduleMpv.GetUtf8Bytes(str));
  667.             }
  668.             public string mpvGetEventNameString(enumMpv_Event intEvent)
  669.             {
  670.                 return Marshal.PtrToStringAnsi(_mpvGetEventNameString(intEvent));
  671.             }
  672.         }
  673.  
  674.         static internal IntPtr GetPtrFromAry<Typ>(IntPtr AryPtr, int Itm)
  675.         {
  676.             return IntPtr.Add(AryPtr, Itm * Marshal.SizeOf<Typ>);
  677.         }
  678.         static internal object GetMpvObjectFromFormat(IntPtr ptrData, enumMpv_Format format)
  679.         {
  680.             object objRet = null;
  681.             if (ptrData != IntPtr.Zero) {
  682.                 switch (format) {
  683.                     case enumMpv_Format.MPV_FORMAT_FLAG:
  684.                         objRet = ((Marshal.PtrToStructure<int>(ptrData)) == 1);
  685.                         break;
  686.                     case enumMpv_Format.MPV_FORMAT_INT64:
  687.                         objRet = Marshal.PtrToStructure<long>(ptrData);
  688.                         break;
  689.                     case enumMpv_Format.MPV_FORMAT_DOUBLE:
  690.                         objRet = Marshal.PtrToStructure<double>(ptrData);
  691.                         break;
  692.                     case enumMpv_Format.MPV_FORMAT_STRING:
  693.                         objRet = Marshal.PtrToStringAnsi(ptrData);
  694.                         break;
  695.                     case enumMpv_Format.MPV_FORMAT_OSD_STRING:
  696.                         objRet = Marshal.PtrToStringAnsi(ptrData);
  697.                         break;
  698.                     case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
  699.                         _structMpv_ByteArray ptrAry = Marshal.PtrToStructure<_structMpv_ByteArray>(ptrData);
  700.                         byte[] aryRet = new byte[ptrAry.Size + 1];
  701.                         Marshal.PtrToStructure<byte[]>(ptrAry.pData, aryRet);
  702.                         objRet = aryRet;
  703.                         break;
  704.                     case enumMpv_Format.MPV_FORMAT_NODE:
  705.                         _structMpv_Node ptrNode = Marshal.PtrToStructure<_structMpv_Node>(ptrData);
  706.                         objRet = new structMpv_Node(ptrNode);
  707.                         break;
  708.                     case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
  709.                         _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(ptrData);
  710.                         structMpv_Node[] retLst = new structMpv_Node[ptrLst.Num];
  711.                         for (int i = 0; i <= ptrLst.Num - 1; i++) {
  712.                             IntPtr ptrNewItm = GetPtrFromAry<_structMpv_Node>(ptrLst.Values, i);
  713.                             _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
  714.                             retLst(i) = new structMpv_Node(objNewItm);
  715.                         }
  716.  
  717.                         objRet = retLst;
  718.                         break;
  719.                     case enumMpv_Format.MPV_FORMAT_NODE_MAP:
  720.                         _structMpv_Node_List ptrLst = Marshal.PtrToStructure<_structMpv_Node_List>(ptrData);
  721.                         Dictionary<string, structMpv_Node> retMap = new Dictionary<string, structMpv_Node>();
  722.                         for (int i = 0; i <= ptrLst.Num - 1; i++) {
  723.                             IntPtr ptrNewKey = GetPtrFromAry<string>(ptrLst.Keys, i);
  724.                             string strNewKey = Marshal.PtrToStringAnsi(ptrNewKey);
  725.                             IntPtr ptrNewItm = GetPtrFromAry<_structMpv_Node>(ptrLst.Values, i);
  726.                             _structMpv_Node objNewItm = Marshal.PtrToStructure<_structMpv_Node>(ptrNewItm);
  727.                             retMap.Add(strNewKey, new structMpv_Node(objNewItm));
  728.                         }
  729.  
  730.                         objRet = retMap;
  731.                         break;
  732.                 }
  733.             }
  734.             return objRet;
  735.         }
  736.         static internal IntPtr GetMpvValueFromObject(object objData, enumMpv_Format format)
  737.         {
  738.             IntPtr ptrRet = IntPtr.Zero;
  739.             if ((objData != null)) {
  740.                 switch (format) {
  741.                     case enumMpv_Format.MPV_FORMAT_FLAG:
  742.                         ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<int>);
  743.                         int intRet = Convert.ToInt32((Convert.ToBoolean(objData) ? 1 : 0));
  744.                         Marshal.Copy({ intRet }, 0, ptrRet, 1);
  745.                         break;
  746.                     case enumMpv_Format.MPV_FORMAT_INT64:
  747.                         ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<long>);
  748.                         long intRet = Convert.ToInt64(objData);
  749.                         Marshal.Copy({ intRet }, 0, ptrRet, 1);
  750.                         break;
  751.                     case enumMpv_Format.MPV_FORMAT_DOUBLE:
  752.                         ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<double>);
  753.                         double dblRet = Convert.ToDouble(objData);
  754.                         Marshal.Copy({ dblRet }, 0, ptrRet, 1);
  755.                         break;
  756.                     case enumMpv_Format.MPV_FORMAT_STRING:
  757.                         ptrRet = AllocateUtf8IntPtrArrayWithSentinel({ objData.ToString }, ref null);
  758.                         break;
  759.                     case enumMpv_Format.MPV_FORMAT_OSD_STRING:
  760.                         ptrRet = AllocateUtf8IntPtrArrayWithSentinel({ objData.ToString }, ref null);
  761.                         break;
  762.                     case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
  763.                         byte[] aryByt = (byte[])objData;
  764.                         _structMpv_ByteArray structRet = new _structMpv_ByteArray();
  765.                         structRet.Size = aryByt.Length;
  766.                         structRet.pData = Marshal.AllocHGlobal(Marshal.SizeOf<byte> * structRet.Size);
  767.                         Marshal.Copy(aryByt, 0, structRet.pData, structRet.Size);
  768.                         ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_ByteArray>);
  769.                         Marshal.StructureToPtr<_structMpv_ByteArray>(structRet, ptrRet, true);
  770.                         break;
  771.                     case enumMpv_Format.MPV_FORMAT_NODE:
  772.                         _structMpv_Node structRet = ((structMpv_Node)objData).ToNative();
  773.                         ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node>);
  774.                         Marshal.StructureToPtr<_structMpv_Node>(structRet, ptrRet, true);
  775.                         break;
  776.                     case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
  777.                         structMpv_Node[] lstTmp = (structMpv_Node[])objData;
  778.                         _structMpv_Node_List lstRet = new _structMpv_Node_List();
  779.                         lstRet.Num = lstTmp.Length;
  780.                         lstRet.Values = Marshal.AllocHGlobal(Marshal.SizeOf<_structMpv_Node> * lstRet.Num);
  781.                         for (int i = 0; i <= lstTmp.Length - 1; i++) {
  782.                             _structMpv_Node tmpItm = lstTmp(i).ToNative();
  783.                             Marshal.StructureToPtr<_structMpv_Node>(tmpItm, GetPtrFromAry<_structMpv_Node>(lstRet.Values, i), true);
  784.                         }
  785.  
  786.                         Marshal.StructureToPtr<_structMpv_Node_List>(lstRet, ptrRet, true);
  787.                         break;
  788.                     case enumMpv_Format.MPV_FORMAT_NODE_MAP:
  789.                         Dictionary<string, structMpv_Node> mapTmp = (Dictionary<string, structMpv_Node>)objData;
  790.                         _structMpv_Node_List mapRet = new _structMpv_Node_List();
  791.                         mapRet.Num = mapTmp.Count;
  792.                         mapRet.Keys = AllocateUtf8IntPtrArrayWithSentinel(mapTmp.Keys.ToArray, ref null);
  793.                         for (int i = 0; i <= mapTmp.Count - 1; i++) {
  794.                             structMpv_Node tmpNode = null;
  795.                             mapTmp.TryGetValue(mapTmp.Keys(i), tmpNode);
  796.                             _structMpv_Node tmpItm = tmpNode.ToNative();
  797.                             Marshal.StructureToPtr<_structMpv_Node>(tmpItm, GetPtrFromAry<_structMpv_Node>(mapRet.Values, i), true);
  798.                         }
  799.  
  800.                         Marshal.StructureToPtr<_structMpv_Node_List>(mapRet, ptrRet, true);
  801.                         break;
  802.                 }
  803.             }
  804.             return ptrRet;
  805.         }
  806.  
  807.         public static Type GetMpvTypeFromFormat(enumMpv_Format format)
  808.         {
  809.             Type typRet = null;
  810.             switch (format) {
  811.                 case enumMpv_Format.MPV_FORMAT_FLAG:
  812.                     typRet = typeof(int);
  813.                     break;
  814.                 case enumMpv_Format.MPV_FORMAT_INT64:
  815.                     typRet = typeof(long);
  816.                     break;
  817.                 case enumMpv_Format.MPV_FORMAT_DOUBLE:
  818.                     typRet = typeof(double);
  819.                     break;
  820.                 case enumMpv_Format.MPV_FORMAT_STRING:
  821.                     typRet = typeof(string);
  822.                     break;
  823.                 case enumMpv_Format.MPV_FORMAT_OSD_STRING:
  824.                     typRet = typeof(string);
  825.                     break;
  826.                 case enumMpv_Format.MPV_FORMAT_BYTE_ARRAY:
  827.                     typRet = typeof(_structMpv_ByteArray);
  828.                     break;
  829.                 case enumMpv_Format.MPV_FORMAT_NODE:
  830.                     typRet = typeof(_structMpv_Node);
  831.                     break;
  832.                 case enumMpv_Format.MPV_FORMAT_NODE_ARRAY:
  833.                     typRet = typeof(_structMpv_Node_List);
  834.                     break;
  835.                 case enumMpv_Format.MPV_FORMAT_NODE_MAP:
  836.                     typRet = typeof(_structMpv_Node_List);
  837.                     break;
  838.             }
  839.             return typRet;
  840.         }
  841.         public static string GetMpvEnum<Typ>(Typ intValue)
  842.         {
  843.             return Enum.GetName(intValue.GetType(), intValue);
  844.         }
  845.         public static string[] GetMpvEnumNames<Typ>()
  846.         {
  847.             return Enum.GetNames(typeof(Typ));
  848.         }
  849.         public static object[] GetMpvEnumValues<Typ>()
  850.         {
  851.             return Enum.GetNames(typeof(Typ));
  852.         }
  853.         public static Dictionary<string, object> GetMpvEnums<Typ>()
  854.         {
  855.             Dictionary<string, object> dictRet = new Dictionary<string, object>();
  856.             foreach (object enumItm in Enum.GetValues(typeof(Typ))) {
  857.                 dictRet.Add(Enum.GetName(typeof(Typ), enumItm), enumItm);
  858.             }
  859.             return dictRet;
  860.         }
  861.         public static byte[] GetUtf8Bytes(string s)
  862.         {
  863.             return Encoding.UTF8.GetBytes(s + Convert.ToString(Constants.vbNullChar));
  864.         }
  865.         public static IntPtr AllocateUtf8IntPtrArrayWithSentinel(string[] arr, ref IntPtr[] byteArrayPointers)
  866.         {
  867.             int numberOfStrings = arr.Length + 1;
  868.             // add extra element for extra null pointer last (sentinel)
  869.             byteArrayPointers = new IntPtr[numberOfStrings];
  870.             IntPtr rootPointer = Marshal.AllocCoTaskMem(IntPtr.Size * numberOfStrings);
  871.             for (int index = 0; index <= arr.Length - 1; index++) {
  872.                 dynamic bytes = GetUtf8Bytes(arr(index));
  873.                 IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
  874.                 Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);
  875.                 byteArrayPointers(index) = unmanagedPointer;
  876.             }
  877.             Marshal.Copy(byteArrayPointers, 0, rootPointer, numberOfStrings);
  878.             return rootPointer;
  879.         }
  880.         public static string Str_AryChk(object obj)
  881.         {
  882.             string strRet = "";
  883.             if ((obj != null)) {
  884.                 if (obj.GetType.IsArray) {
  885.                     Array aryTmp = (Array)obj;
  886.                     strRet += "[Length=" + aryTmp.Length.ToString + "] {";
  887.                     for (int i = 0; i <= aryTmp.Length - 1; i++) {
  888.                         strRet += aryTmp.GetValue(i).ToString + (i < aryTmp.Length - 1 ? "; " : "").ToString;
  889.                     }
  890.                     strRet += "}";
  891.                 } else if ((obj as Dictionary<string, structMpv_Node> != null)) {
  892.                     List<KeyValuePair<string, structMpv_Node>> mapTmp = (obj as Dictionary<string, structMpv_Node>).ToList();
  893.                     strRet += "[Length=" + mapTmp.Count.ToString + "] {";
  894.                     for (int i = 0; i <= mapTmp.Count - 1; i++) {
  895.                         strRet += mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + (i < mapTmp.Count - 1 ? "; " : "").ToString;
  896.                     }
  897.                     strRet += "}";
  898.                 } else if ((obj as Dictionary<string, object> != null)) {
  899.                     List<KeyValuePair<string, object>> mapTmp = (obj as Dictionary<string, object>).ToList();
  900.                     strRet += "[Length=" + mapTmp.Count.ToString + "] {";
  901.                     for (int i = 0; i <= mapTmp.Count - 1; i++) {
  902.                         strRet += mapTmp(i).Key + " = " + mapTmp(i).Value.ToString + (i < mapTmp.Count - 1 ? "; " : "").ToString;
  903.                     }
  904.                     strRet += "}";
  905.                 } else {
  906.                     strRet += " " + obj.ToString;
  907.                 }
  908.             }
  909.             return strRet;
  910.         }
  911.     }
  912.  
  913.     public class clsMpv : IDisposable
  914.     {
  915.         private IntPtr _mpvHandle;
  916.         private structCache _cache;
  917.         public event MpvEventEventHandler MpvEvent;
  918.         public delegate void MpvEventEventHandler(object sender, MpvEventArgs e);
  919.         public event MpvPropertyChangeEventHandler MpvPropertyChange;
  920.         public delegate void MpvPropertyChangeEventHandler(object sender, MpvPropertyChangeEventArgs e);
  921.         public event MpvEndOfFileEventHandler MpvEndOfFile;
  922.         public delegate void MpvEndOfFileEventHandler(object sender, MpvEndOfFileEventArgs e);
  923.         public clsMpv(IntPtr hWnd)
  924.         {
  925.             long windowId = hWnd.ToInt64();
  926.             _mpvHandle = moduleMpv.Mpv_DLL.mpvCreate.Invoke();
  927.             moduleMpv.Mpv_DLL.mpvInitialize.Invoke(_mpvHandle);
  928.  
  929.             _cache = new structCache(_mpvHandle);
  930.  
  931.             moduleMpv.Mpv_DLL.mpvRequestLogMessages(_mpvHandle, moduleMpv.GetUtf8Bytes("v"));
  932.             moduleMpv.Mpv_DLL.mpvRequestEvent(_mpvHandle, enumMpv_Event.MPV_EVENT_SEEK, 1);
  933.             moduleMpv.Mpv_DLL.mpvRequestEvent(_mpvHandle, enumMpv_Event.MPV_EVENT_CLIENT_MESSAGE, 1);
  934.             moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("ytdl"), moduleMpv.GetUtf8Bytes("yes"));
  935.             moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("msg-level"), moduleMpv.GetUtf8Bytes("all=trace"));
  936.             moduleMpv.Mpv_DLL.mpvSetOptionString(_mpvHandle, moduleMpv.GetUtf8Bytes("keepaspect"), moduleMpv.GetUtf8Bytes("no"));
  937.  
  938.             moduleMpv.Mpv_DLL.mpvSetOption(_mpvHandle, moduleMpv.GetUtf8Bytes("wid"), enumMpv_Format.MPV_FORMAT_INT64, hWnd);
  939.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("duration"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  940.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("time-pos"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  941.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-size"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  942.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-free"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  943.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-used"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  944.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("cache-buffering-state"), enumMpv_Format.MPV_FORMAT_INT64);
  945.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("demuxer-cache-time"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  946.             moduleMpv.Mpv_DLL.mpvObserveProperty(_mpvHandle, 0, moduleMpv.GetUtf8Bytes("volume"), enumMpv_Format.MPV_FORMAT_DOUBLE);
  947.             Task.Run(new Action(threadEventLoop));
  948.         }
  949.         public IntPtr Handle {
  950.             get { return _mpvHandle; }
  951.         }
  952.         private void threadEventLoop()
  953.         {
  954.             while (!disposedValue) {
  955.                 try {
  956.                     if (_mpvHandle != IntPtr.Zero) {
  957.                         IntPtr lpBuffer = moduleMpv.Mpv_DLL.mpvWaitEvent(_mpvHandle, -1);
  958.                         _structMpv_Event pEvent = Marshal.PtrToStructure<_structMpv_Event>(lpBuffer);
  959.                         structMpv_Event objEvent = new structMpv_Event(pEvent);
  960.                         lpBuffer = IntPtr.Zero;
  961.                         pEvent = null;
  962.                         if (MpvEvent != null) {
  963.                             MpvEvent(this, new MpvEventArgs(objEvent));
  964.                         }
  965.                         if (objEvent.EventID == enumMpv_Event.MPV_EVENT_PROPERTY_CHANGE) {
  966.                             if (MpvPropertyChange != null) {
  967.                                 MpvPropertyChange(this, new MpvPropertyChangeEventArgs(objEvent));
  968.                             }
  969.                         }
  970.                         if (objEvent.EventID == enumMpv_Event.MPV_EVENT_END_FILE) {
  971.                             if (MpvEndOfFile != null) {
  972.                                 MpvEndOfFile(this, new MpvEndOfFileEventArgs(objEvent));
  973.                             }
  974.                         }
  975.                     }
  976.                     Application.DoEvents();
  977.  
  978.                 } catch (Exception ex) {
  979.                 }
  980.             }
  981.         }
  982.         public void LoadFile(string strURi, double dblStartPos)
  983.         {
  984.             if (_mpvHandle == IntPtr.Zero) {
  985.                 return;
  986.             }
  987.             DoMpvCommand("loadfile", strURi, "replace", "start=" + (dblStartPos).ToString);
  988.         }
  989.         public void Pause()
  990.         {
  991.             moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, true);
  992.         }
  993.         public void Play()
  994.         {
  995.             moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, false);
  996.         }
  997.         public void Stop()
  998.         {
  999.             if (_mpvHandle == IntPtr.Zero) {
  1000.                 return;
  1001.             }
  1002.             DoMpvCommand("stop");
  1003.         }
  1004.         public bool IsPaused {
  1005.             get { return Convert.ToBoolean(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG)); }
  1006.             set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "pause", enumMpv_Format.MPV_FORMAT_FLAG, value); }
  1007.         }
  1008.         public double TimePosition {
  1009.             get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "time-pos", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
  1010.             set {
  1011.                 if (_mpvHandle != IntPtr.Zero) {
  1012.                     DoMpvCommand("seek", value.ToString(CultureInfo.InvariantCulture), "absolute");
  1013.                 }
  1014.             }
  1015.         }
  1016.         public double TimeLength {
  1017.             get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "duration", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
  1018.         }
  1019.         public double Volume {
  1020.             get { return Convert.ToDouble(moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, "volume", enumMpv_Format.MPV_FORMAT_DOUBLE)); }
  1021.             set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, "volume", enumMpv_Format.MPV_FORMAT_DOUBLE, value); }
  1022.         }
  1023.         public structCache Cache {
  1024.             get { return _cache; }
  1025.         }
  1026.         public object PropertyValue {
  1027.             get { return moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, strPropertyName, intFormat); }
  1028.             set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, strPropertyName, intFormat, value); }
  1029.         }
  1030.         public object PropertyValue {
  1031.             get { return ((structMpv_Node)moduleMpv.Mpv_DLL.mpvGetProperty(_mpvHandle, strPropertyName, enumMpv_Format.MPV_FORMAT_NODE)).Data; }
  1032.             set { moduleMpv.Mpv_DLL.mpvSetProperty(_mpvHandle, strPropertyName, enumMpv_Format.MPV_FORMAT_NODE, new structMpv_Node(value)); }
  1033.         }
  1034.         public void DoMpvCommand(params string[] args)
  1035.         {
  1036.             IntPtr[] byteArrayPointers = new IntPtr[];
  1037.             dynamic mainPtr = moduleMpv.AllocateUtf8IntPtrArrayWithSentinel(args, ref byteArrayPointers);
  1038.             moduleMpv.Mpv_DLL.mpvCommand(_mpvHandle, mainPtr);
  1039.             foreach (IntPtr ptr in byteArrayPointers) {
  1040.                 Marshal.FreeHGlobal(ptr);
  1041.             }
  1042.             Marshal.FreeHGlobal(mainPtr);
  1043.         }
  1044.         private bool disposedValue;
  1045.         protected virtual void Dispose(bool disposing)
  1046.         {
  1047.             if (!disposedValue) {
  1048.                 if (disposing) {
  1049.                     if (_mpvHandle != IntPtr.Zero) {
  1050.                         moduleMpv.Mpv_DLL.mpvUnobserveProperty(_mpvHandle, 0);
  1051.                         moduleMpv.Mpv_DLL.mpvTerminateDestroy(_mpvHandle);
  1052.                         _mpvHandle = IntPtr.Zero;
  1053.                     }
  1054.                 }
  1055.             }
  1056.             disposedValue = true;
  1057.         }
  1058.         public void Dispose()
  1059.         {
  1060.             Dispose(true);
  1061.         }
  1062.     }
  1063. }
  1064.  
  1065. //=======================================================
  1066. //Service provided by Telerik (www.telerik.com)
  1067. //Conversion powered by NRefactory.
  1068. //Twitter: @telerik
  1069. //Facebook: facebook.com/telerik
  1070. //=======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement