MediaDevices.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace RtcUtil
  4. {
  5. public enum MediaDeviceKind
  6. {
  7. AUDIOINPUT,
  8. AUDIOOUTPUT,
  9. VIDEOINPUT
  10. };
  11. public class MediaDeviceInfo
  12. {
  13. public string DeviceId { get; set; }
  14. public string Label { get; set; }
  15. public MediaDeviceKind Kind { get; set; }
  16. public string GroupId { get; set; }
  17. }
  18. [StructLayout(LayoutKind.Sequential)]
  19. public struct NativeMediaDeviceInfo
  20. {
  21. public UInt32 DeviceNumber;
  22. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  23. public string DeviceNameUTF8;
  24. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  25. public string DeviceUniqueIdUTF8;
  26. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  27. public string ProductUniqueIdUTF8;
  28. }
  29. public class MediaDevices
  30. {
  31. [DllImport("x64\\RtcUtil.Native.x64.dll", EntryPoint = "CreateVideoDeviceInfo", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  32. internal static extern int CreateVideoDeviceInfo(out IntPtr deviceInfo);
  33. [DllImport("x64\\RtcUtil.Native.x64.dll", EntryPoint = "GetVidoeDeviceInfo", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  34. internal static extern int GetVidoeDeviceInfo(IntPtr hDeviceInfo, UInt32 deviceNumber, out IntPtr meadiaDeviceInfo);
  35. public static IList<string> EnumerateVideoDevices()
  36. {
  37. var list = new List<string>();
  38. IntPtr ptrInfo;
  39. var num = CreateVideoDeviceInfo(out ptrInfo);
  40. if (ptrInfo != IntPtr.Zero)
  41. {
  42. for(UInt32 i =0;i< num; i++)
  43. {
  44. IntPtr ptrMdDeviceInfo;
  45. var retValue = GetVidoeDeviceInfo(ptrInfo, i, out ptrMdDeviceInfo);
  46. if (retValue != -1)
  47. {
  48. var info = (NativeMediaDeviceInfo)Marshal.PtrToStructure(ptrMdDeviceInfo, typeof(NativeMediaDeviceInfo));
  49. list.Add(info.DeviceNameUTF8);//TODO need other properties , such as ID ?
  50. }
  51. }
  52. }
  53. return list;
  54. }
  55. }
  56. }