// Set the Framerate, and video size private void SetConfigParms(DirectShowLib.ICaptureGraphBuilder2 capGraph, DirectShowLib.IBaseFilter capFilter, int iFrameRate, int iWidth, int iHeight) { int hr; object o; DirectShowLib.AMMediaType media; // Find the stream config interface hr = capGraph.FindInterface( DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, typeof(DirectShowLib.IAMStreamConfig).GUID, out o); DirectShowLib.IAMStreamConfig videoStreamConfig = o as DirectShowLib.IAMStreamConfig; if (videoStreamConfig == null) { throw new Exception("Failed to get IAMStreamConfig"); } // Get the existing format block hr = videoStreamConfig.GetFormat(out media); DsError.ThrowExceptionForHR(hr); // copy out the videoinfoheader DirectShowLib.VideoInfoHeader v = new DirectShowLib.VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, v); // if overriding the framerate, set the frame rate if (iFrameRate > 0) { v.AvgTimePerFrame = 10000000 / iFrameRate; } // if overriding the width, set the width if (iWidth > 0) { v.BmiHeader.Width = iWidth; } // if overriding the Height, set the Height if (iHeight > 0) { v.BmiHeader.Height = iHeight; } // Copy the media structure back Marshal.StructureToPtr(v, media.formatPtr, false); // Set the new format hr = videoStreamConfig.SetFormat(media); DsError.ThrowExceptionForHR(hr); DirectShowLib.DsUtils.FreeAMMediaType(media); media = null; }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, Control hControl) { int hr; DirectShowLib.ISampleGrabber sampGrabber = null; DirectShowLib.IBaseFilter capFilter = null; DirectShowLib.ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_graphBuilder = (DirectShowLib.IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_graphBuilder as DirectShowLib.IMediaControl; try { // Get the ICaptureGraphBuilder2 capGraph = (DirectShowLib.ICaptureGraphBuilder2) new DirectShowLib.CaptureGraphBuilder2(); // Get the SampleGrabber interface sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(m_graphBuilder); DsError.ThrowExceptionForHR(hr); // Add the video device hr = m_graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); DsError.ThrowExceptionForHR(hr); DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // If any of the default config items are set if (iFrameRate + iHeight + iWidth > 0) { SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight); } // ------------------------------------ if (false) { if (false) { hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt); DsError.ThrowExceptionForHR(hr); } theCompressor = CreateFilter(FilterCategory.VideoCompressorCategory, "Microsoft MPEG-4 Video Codec V2"); // Add the Video compressor filter to the graph if (theCompressor != null) { hr = m_graphBuilder.AddFilter(theCompressor, "Compressor Filter"); DsError.ThrowExceptionForHR(hr); } // Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink DirectShowLib.IBaseFilter mux; DirectShowLib.IFileSinkFilter sink; hr = capGraph.SetOutputFileName(DirectShowLib.MediaSubType.Avi, "C:\\Test.avi", out mux, out sink); DsError.ThrowExceptionForHR(hr); hr = capGraph.RenderStream(DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, theCompressor, mux); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(mux); Marshal.ReleaseComObject(sink); hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, null); DsError.ThrowExceptionForHR(hr); //ShowVideoWindow(hControl); } else { hr = capGraph.RenderStream(DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt); DsError.ThrowExceptionForHR(hr); //hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, null); //DsError.ThrowExceptionForHR(hr); //ShowVideoWindow(hControl); } // -------------------------------------- //hr = capGraph.RenderStream( DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt ); //DsError.ThrowExceptionForHR( hr ); SaveSizeInfo(sampGrabber); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (capGraph != null) { Marshal.ReleaseComObject(capGraph); capGraph = null; } } }