/Vlc.DotNet/Vlc.DotNet.Core.Interops/LibVlcInteropsManager.cs

# · C# · 170 lines · 137 code · 20 blank · 13 comment · 28 complexity · bc6e81c81e8c62162cca2e6fcc792ee3 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text.RegularExpressions;
  5. using Vlc.DotNet.Core.Interops.Signatures.LibVlc;
  6. #if SILVERLIGHT
  7. #else
  8. using System.ComponentModel;
  9. #endif
  10. namespace Vlc.DotNet.Core.Interops
  11. {
  12. /// <summary>
  13. /// LibVlcInteropsManager class
  14. /// </summary>
  15. public sealed class LibVlcInteropsManager : IDisposable
  16. {
  17. private IntPtr myLibVlcCoreDllHandle;
  18. private IntPtr myLibVlcDllHandle;
  19. /// <summary>
  20. /// Initializes a new instance of the LibVlcInteropsManager class.
  21. /// </summary>
  22. /// <param name="libVlcDllsDirectory">The path to libvlc.dll and libvlccore.dll</param>
  23. public LibVlcInteropsManager(string libVlcDllsDirectory)
  24. {
  25. if (string.IsNullOrEmpty(libVlcDllsDirectory))
  26. throw new ArgumentNullException("libVlcDllsDirectory");
  27. InitVlcLib(libVlcDllsDirectory);
  28. }
  29. public LibVlcFunction<NewInstance> NewInstance { get; private set; }
  30. public LibVlcFunction<ReleaseInstance> ReleaseInstance { get; private set; }
  31. public LibVlcFunction<RetainInstance> RetainInstance { get; private set; }
  32. public LibVlcFunction<AddInterface> AddInterface { get; private set; }
  33. public LibVlcFunction<SetExitCallback> SetExitCallback { get; private set; }
  34. public LibVlcFunction<Wait> Wait { get; private set; }
  35. public LibVlcFunction<SetUserAgent> SetUserAgent { get; private set; }
  36. public LibVlcFunction<GetVersion> GetVersion { get; private set; }
  37. public LibVlcFunction<GetCompiler> GetCompiler { get; private set; }
  38. public LibVlcFunction<GetChangeSet> GetChangeSet { get; private set; }
  39. public LibVlcFunction<FreeMemory> FreeMemory { get; private set; }
  40. //public LibVlcFunction<GetModuleDescriptionList> GetModuleDescriptionList { get; private set; }
  41. //public LibVlcFunction<ReleaseModuleDescription> ReleaseModule { get; private set; }
  42. public LibVlcAsynchronousEvents EventInterops { get; private set; }
  43. public LibVlcLogging LoggingInterops { get; private set; }
  44. public LibVlcMediaPlayer MediaPlayerInterops { get; private set; }
  45. public LibVlcMedia MediaInterops { get; private set; }
  46. public LibVlcMediaList MediaListInterops { get; set; }
  47. public LibVlcAudio AudioInterops { get; private set; }
  48. public LibVlcVideo VideoInterops { get; private set; }
  49. public LibVlcErrorHandling ErrorHandlingInterops { get; private set; }
  50. public LibVlcMediaListPlayer MediaListPlayerInterops { get; private set; }
  51. public Version VlcVersion
  52. {
  53. get; private set;
  54. }
  55. #region IDisposable Members
  56. public void Dispose()
  57. {
  58. if (myLibVlcDllHandle != IntPtr.Zero)
  59. {
  60. Win32Interop.FreeLibrary(myLibVlcDllHandle);
  61. myLibVlcDllHandle = IntPtr.Zero;
  62. }
  63. if (myLibVlcCoreDllHandle != IntPtr.Zero)
  64. {
  65. Win32Interop.FreeLibrary(myLibVlcCoreDllHandle);
  66. myLibVlcCoreDllHandle = IntPtr.Zero;
  67. }
  68. NewInstance = null;
  69. ReleaseInstance = null;
  70. RetainInstance = null;
  71. AddInterface = null;
  72. SetExitCallback = null;
  73. Wait = null;
  74. SetUserAgent = null;
  75. GetVersion = null;
  76. GetCompiler = null;
  77. GetChangeSet = null;
  78. FreeMemory = null;
  79. //GetModuleDescriptionList = null;
  80. //ReleaseModule = null;
  81. if (EventInterops != null)
  82. EventInterops.Dispose();
  83. EventInterops = null;
  84. if (MediaPlayerInterops != null)
  85. MediaPlayerInterops.Dispose();
  86. MediaPlayerInterops = null;
  87. if (MediaInterops != null)
  88. MediaInterops.Dispose();
  89. MediaInterops = null;
  90. if (MediaListInterops != null)
  91. MediaListInterops.Dispose();
  92. MediaListInterops = null;
  93. if (AudioInterops != null)
  94. AudioInterops.Dispose();
  95. AudioInterops = null;
  96. if (VideoInterops != null)
  97. VideoInterops.Dispose();
  98. VideoInterops = null;
  99. if (LoggingInterops != null)
  100. LoggingInterops.Dispose();
  101. LoggingInterops = null;
  102. if (MediaListPlayerInterops != null)
  103. MediaListPlayerInterops.Dispose();
  104. MediaListPlayerInterops = null;
  105. }
  106. #endregion
  107. private void InitVlcLib(string libVlcDllsDirectory)
  108. {
  109. if (!Directory.Exists(libVlcDllsDirectory))
  110. throw new DirectoryNotFoundException(string.Format("The directory : {0} not found.", libVlcDllsDirectory));
  111. string libVlcFilePath = Path.Combine(libVlcDllsDirectory, "libvlc.dll");
  112. if (!File.Exists(libVlcFilePath))
  113. throw new FileNotFoundException("Libvlc library not found in directory.");
  114. string libVlcCoreFilePath = Path.Combine(libVlcDllsDirectory, "libvlccore.dll");
  115. if (!File.Exists(libVlcCoreFilePath))
  116. throw new FileNotFoundException("Libvlccore library not found in directory.");
  117. myLibVlcCoreDllHandle = Win32Interop.LoadLibrary(libVlcCoreFilePath);
  118. if (myLibVlcCoreDllHandle == IntPtr.Zero)
  119. throw new Win32Exception(Marshal.GetLastWin32Error());
  120. myLibVlcDllHandle = Win32Interop.LoadLibrary(libVlcFilePath);
  121. if (myLibVlcDllHandle == IntPtr.Zero)
  122. throw new Win32Exception(Marshal.GetLastWin32Error());
  123. GetVersion = new LibVlcFunction<GetVersion>(myLibVlcDllHandle);
  124. var reg = new Regex("^[0-9.]*");
  125. var match = reg.Match(IntPtrExtensions.ToStringAnsi(GetVersion.Invoke()));
  126. VlcVersion = new Version(match.Groups[0].Value);
  127. NewInstance = new LibVlcFunction<NewInstance>(myLibVlcDllHandle, VlcVersion);
  128. ReleaseInstance = new LibVlcFunction<ReleaseInstance>(myLibVlcDllHandle, VlcVersion);
  129. RetainInstance = new LibVlcFunction<RetainInstance>(myLibVlcDllHandle, VlcVersion);
  130. AddInterface = new LibVlcFunction<AddInterface>(myLibVlcDllHandle, VlcVersion);
  131. SetExitCallback = new LibVlcFunction<SetExitCallback>(myLibVlcDllHandle, VlcVersion);
  132. Wait = new LibVlcFunction<Wait>(myLibVlcDllHandle, VlcVersion);
  133. SetUserAgent = new LibVlcFunction<SetUserAgent>(myLibVlcDllHandle, VlcVersion);
  134. GetCompiler = new LibVlcFunction<GetCompiler>(myLibVlcDllHandle, VlcVersion);
  135. GetChangeSet = new LibVlcFunction<GetChangeSet>(myLibVlcDllHandle, VlcVersion);
  136. FreeMemory = new LibVlcFunction<FreeMemory>(myLibVlcDllHandle, VlcVersion);
  137. //GetModuleDescriptionList = new LibVlcFunction<GetModuleDescriptionList>(myLibVlcDllHandle, VlcVersion);
  138. //ReleaseModule = new LibVlcFunction<ReleaseModuleDescription>(myLibVlcDllHandle, VlcVersion);
  139. EventInterops = new LibVlcAsynchronousEvents(myLibVlcDllHandle, VlcVersion);
  140. MediaPlayerInterops = new LibVlcMediaPlayer(myLibVlcDllHandle, VlcVersion);
  141. MediaInterops = new LibVlcMedia(myLibVlcDllHandle, VlcVersion);
  142. MediaListInterops = new LibVlcMediaList(myLibVlcDllHandle, VlcVersion);
  143. AudioInterops = new LibVlcAudio(myLibVlcDllHandle, VlcVersion);
  144. VideoInterops = new LibVlcVideo(myLibVlcDllHandle, VlcVersion);
  145. LoggingInterops = new LibVlcLogging(myLibVlcDllHandle, VlcVersion);
  146. ErrorHandlingInterops = new LibVlcErrorHandling(myLibVlcDllHandle, VlcVersion);
  147. MediaListPlayerInterops = new LibVlcMediaListPlayer(myLibVlcDllHandle, VlcVersion);
  148. }
  149. }
  150. }