DeviceManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProMovieCapture
  6. {
  7. public enum DeviceType
  8. {
  9. AudioInput,
  10. }
  11. public interface IMediaApiItem
  12. {
  13. int Index { get; }
  14. string Name { get; }
  15. MediaApi MediaApi { get; }
  16. }
  17. public class Device : IMediaApiItem
  18. {
  19. private DeviceType _deviceType;
  20. private int _index;
  21. private string _name;
  22. private MediaApi _api;
  23. public DeviceType DeviceType { get { return _deviceType; } }
  24. public int Index { get { return _index; } }
  25. public string Name { get { return _name; } }
  26. public MediaApi MediaApi { get { return _api; } }
  27. internal Device(DeviceType deviceType, int index, string name, MediaApi api)
  28. {
  29. _deviceType = deviceType;
  30. _index = index;
  31. _name = name;
  32. _api = api;
  33. }
  34. }
  35. public class DeviceList : IEnumerable
  36. {
  37. internal DeviceList(Device[] devices)
  38. {
  39. _devices = devices;
  40. }
  41. public Device FindDevice(string name, MediaApi mediaApi = MediaApi.Unknown)
  42. {
  43. Device result = null;
  44. foreach (Device device in _devices)
  45. {
  46. if (device.Name == name)
  47. {
  48. if (mediaApi == MediaApi.Unknown || mediaApi == device.MediaApi)
  49. {
  50. result = device;
  51. break;
  52. }
  53. }
  54. }
  55. return result;
  56. }
  57. public Device GetFirstWithMediaApi(MediaApi api)
  58. {
  59. Device result = null;
  60. foreach (Device device in _devices)
  61. {
  62. if (device.MediaApi == api)
  63. {
  64. result = device;
  65. break;
  66. }
  67. }
  68. return result;
  69. }
  70. public IEnumerator GetEnumerator()
  71. {
  72. return _devices.GetEnumerator();
  73. }
  74. public Device[] Devices { get { return _devices; } }
  75. public int Count { get{ return _devices.Length; } }
  76. private Device[] _devices = new Device[0];
  77. }
  78. public static class DeviceManager
  79. {
  80. public static Device FindDevice(DeviceType deviceType, string name)
  81. {
  82. CheckInit();
  83. Device result = null;
  84. DeviceList devices = GetDevices(deviceType);
  85. result = devices.FindDevice(name);
  86. return result;
  87. }
  88. public static int GetDeviceCount(DeviceType deviceType)
  89. {
  90. CheckInit();
  91. return GetDevices(deviceType).Count;
  92. }
  93. private static void CheckInit()
  94. {
  95. if (!_isEnumerated)
  96. {
  97. if (NativePlugin.Init())
  98. {
  99. EnumerateDevices();
  100. }
  101. }
  102. }
  103. private static DeviceList GetDevices(DeviceType deviceType)
  104. {
  105. DeviceList result = null;
  106. switch (deviceType)
  107. {
  108. case DeviceType.AudioInput:
  109. result = _audioInputDevices;
  110. break;
  111. }
  112. return result;
  113. }
  114. private static void EnumerateDevices()
  115. {
  116. {
  117. Device[] audioInputDevices = new Device[NativePlugin.GetAudioInputDeviceCount()];
  118. for (int i = 0; i < audioInputDevices.Length; i++)
  119. {
  120. audioInputDevices[i] = new Device(DeviceType.AudioInput, i, NativePlugin.GetAudioInputDeviceName(i), NativePlugin.GetAudioInputDeviceMediaApi(i));
  121. }
  122. _audioInputDevices = new DeviceList(audioInputDevices);
  123. }
  124. _isEnumerated = true;
  125. }
  126. public static DeviceList AudioInputDevices { get { CheckInit(); return _audioInputDevices; } }
  127. private static bool _isEnumerated = false;
  128. private static DeviceList _audioInputDevices = new DeviceList(new Device[0]);
  129. }
  130. }