VideoCapabilities.cs
Upload User: wuming6209
Upload Date: 2013-06-06
Package Size: 161k
Code Size: 4k
Category:

Video Capture

Development Platform:

Visual C++

  1. // ------------------------------------------------------------------
  2. // DirectX.Capture
  3. //
  4. // History:
  5. // 2003-Jan-24 BL - created
  6. //
  7. // Copyright (c) 2003 Brian Low
  8. // ------------------------------------------------------------------
  9. using System;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Runtime.InteropServices;
  13. using DShowNET;
  14. namespace DirectX.Capture
  15. {
  16. /// <summary>
  17. ///  Capabilities of the video device such as 
  18. ///  min/max frame size and frame rate.
  19. /// </summary>
  20. public class VideoCapabilities
  21. {
  22. // ------------------ Properties --------------------
  23. /// <summary> Native size of the incoming video signal. This is the largest signal the filter can digitize with every pixel remaining unique. Read-only. </summary>
  24. public Size InputSize;
  25. /// <summary> Minimum supported frame size. Read-only. </summary>
  26. public Size MinFrameSize;
  27. /// <summary> Maximum supported frame size. Read-only. </summary>
  28. public Size MaxFrameSize;
  29. /// <summary> Granularity of the output width. This value specifies the increments that are valid between MinFrameSize and MaxFrameSize. Read-only. </summary>
  30. public int FrameSizeGranularityX;
  31. /// <summary> Granularity of the output height. This value specifies the increments that are valid between MinFrameSize and MaxFrameSize. Read-only. </summary>
  32. public int FrameSizeGranularityY;
  33. /// <summary> Minimum supported frame rate. Read-only. </summary>
  34. public double MinFrameRate;
  35. /// <summary> Maximum supported frame rate. Read-only. </summary>
  36. public double MaxFrameRate;
  37. // ----------------- Constructor ---------------------
  38. /// <summary> Retrieve capabilities of a video device </summary>
  39. internal VideoCapabilities(IAMStreamConfig videoStreamConfig)
  40. {
  41. if ( videoStreamConfig == null ) 
  42. throw new ArgumentNullException( "videoStreamConfig" );
  43. AMMediaType mediaType = null;
  44. VideoStreamConfigCaps caps = null;
  45. IntPtr pCaps = IntPtr.Zero;
  46. IntPtr pMediaType;
  47. try
  48. {
  49. // Ensure this device reports capabilities
  50. int c, size;
  51. int hr = videoStreamConfig.GetNumberOfCapabilities( out c, out size );
  52. if ( hr != 0 ) Marshal.ThrowExceptionForHR( hr );
  53. if ( c <= 0 ) 
  54. throw new NotSupportedException( "This video device does not report capabilities." );
  55. if ( size > Marshal.SizeOf( typeof( VideoStreamConfigCaps ) ) )
  56. throw new NotSupportedException( "Unable to retrieve video device capabilities. This video device requires a larger VideoStreamConfigCaps structure." );
  57. if ( c > 1 )
  58. Debug.WriteLine("This video device supports " + c + " capability structures. Only the first structure will be used." );
  59. // Alloc memory for structure
  60. pCaps = Marshal.AllocCoTaskMem( Marshal.SizeOf( typeof( VideoStreamConfigCaps ) ) ); 
  61. // Retrieve first (and hopefully only) capabilities struct
  62. hr = videoStreamConfig.GetStreamCaps( 0, out pMediaType, pCaps );
  63. if ( hr != 0 ) Marshal.ThrowExceptionForHR( hr );
  64. // Convert pointers to managed structures
  65. mediaType = (AMMediaType) Marshal.PtrToStructure( pMediaType, typeof( AMMediaType ) );
  66. caps = (VideoStreamConfigCaps) Marshal.PtrToStructure( pCaps, typeof( VideoStreamConfigCaps ) );
  67. // Extract info
  68. InputSize = caps.InputSize;
  69. MinFrameSize = caps.MinOutputSize;
  70. MaxFrameSize = caps.MaxOutputSize;
  71. FrameSizeGranularityX = caps.OutputGranularityX;
  72. FrameSizeGranularityY = caps.OutputGranularityY;
  73. MinFrameRate = (double)10000000 / caps.MaxFrameInterval;
  74. MaxFrameRate = (double)10000000 / caps.MinFrameInterval;
  75. }
  76. finally
  77. {
  78. if ( pCaps != IntPtr.Zero )
  79. Marshal.FreeCoTaskMem( pCaps ); pCaps = IntPtr.Zero;
  80. if ( mediaType != null )
  81. DsUtils.FreeAMMediaType( mediaType ); mediaType = null;
  82. }
  83. }
  84. }
  85. }