// Retrieve capabilities of a video device internal VideoCapabilities( IAMStreamConfig videoStreamConfig, int index ) { AMMediaType mediaType = null; VideoStreamConfigCaps caps = new VideoStreamConfigCaps( ); try { // retrieve capabilities struct at the specified index int hr = videoStreamConfig.GetStreamCaps( index, out mediaType, caps ); if ( hr != 0 ) Marshal.ThrowExceptionForHR( hr ); if ( mediaType.FormatType == FormatType.VideoInfo ) { VideoInfoHeader videoInfo = (VideoInfoHeader) Marshal.PtrToStructure( mediaType.FormatPtr, typeof( VideoInfoHeader ) ); FrameSize = new Size( videoInfo.BmiHeader.Width, videoInfo.BmiHeader.Height ); BitCount = videoInfo.BmiHeader.BitCount; AverageFrameRate = (int) ( 10000000 / videoInfo.AverageTimePerFrame ); MaximumFrameRate = (int) ( 10000000 / caps.MinFrameInterval ); } else if ( mediaType.FormatType == FormatType.VideoInfo2 ) { VideoInfoHeader2 videoInfo = (VideoInfoHeader2) Marshal.PtrToStructure( mediaType.FormatPtr, typeof( VideoInfoHeader2 ) ); FrameSize = new Size( videoInfo.BmiHeader.Width, videoInfo.BmiHeader.Height ); BitCount = videoInfo.BmiHeader.BitCount; AverageFrameRate = (int) ( 10000000 / videoInfo.AverageTimePerFrame ); MaximumFrameRate = (int) ( 10000000 / caps.MinFrameInterval ); } else { throw new ApplicationException( "Unsupported format found." ); } // ignore 12 bpp formats for now, since it was noticed they cause issues on Windows 8 // TODO: proper fix needs to be done so ICaptureGraphBuilder2::RenderStream() does not fail // on such formats if ( BitCount <= 12 ) { throw new ApplicationException( "Unsupported format found." ); } } finally { if ( mediaType != null ) mediaType.Dispose( ); } }
// Set resolution for the specified stream configuration private void SetResolution( IAMStreamConfig streamConfig, VideoCapabilities resolution ) { if ( resolution == null ) { return; } // iterate through device's capabilities to find mediaType for desired resolution int capabilitiesCount = 0, capabilitySize = 0; AMMediaType newMediaType = null; VideoStreamConfigCaps caps = new VideoStreamConfigCaps( ); streamConfig.GetNumberOfCapabilities( out capabilitiesCount, out capabilitySize ); for ( int i = 0; i < capabilitiesCount; i++ ) { try { VideoCapabilities vc = new VideoCapabilities( streamConfig, i ); if ( resolution == vc ) { if ( streamConfig.GetStreamCaps( i, out newMediaType, caps ) == 0 ) { break; } } } catch { } } // set the new format if ( newMediaType != null ) { streamConfig.SetFormat( newMediaType ); newMediaType.Dispose( ); } }