// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(string FileName) { int hr; // Get the graphbuilder object m_graphBuilder = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph DirectShowLib.ICaptureGraphBuilder2 icgb2 = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_graphBuilder); DsError.ThrowExceptionForHR( hr ); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_graphBuilder); #endif // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_graphBuilder.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR( hr ); // Get the SampleGrabber interface m_sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber(); DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter) m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_graphBuilder.AddFilter( baseGrabFlt, "Ds.NET Grabber" ); DsError.ThrowExceptionForHR( hr ); // System.Windows.Forms.MessageBox.Show(Width.ToString(), Height.ToString(), System.Windows.Forms.MessageBoxButtons.OK); // A Null Renderer does not display the video // But it allows the Sample Grabber to run // And it will keep proper playback timing // Unless specified otherwise. DirectShowLib.NullRenderer nullRenderer = new DirectShowLib.NullRenderer(); m_graphBuilder.AddFilter((DirectShowLib.IBaseFilter) nullRenderer, "Null Renderer"); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, (DirectShowLib.IBaseFilter) nullRenderer); DsError.ThrowExceptionForHR( hr ); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting SaveSizeInfo(m_sampGrabber); // Configure the Video Window //DirectShowLib.IVideoWindow videoWindow = m_graphBuilder as DirectShowLib.IVideoWindow; //ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_graphBuilder as DirectShowLib.IMediaEvent; m_mediaCtrl = m_graphBuilder as DirectShowLib.IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } #if DEBUG // Double check to make sure we aren't releasing something // important. GC.Collect(); GC.WaitForPendingFinalizers(); #endif }
/// <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; } } }
/// <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; } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(string FileName) { int hr; // Get the graphbuilder object m_graphBuilder = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph DirectShowLib.ICaptureGraphBuilder2 icgb2 = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_graphBuilder); DsError.ThrowExceptionForHR(hr); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_graphBuilder); #endif // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_graphBuilder.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber(); DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // System.Windows.Forms.MessageBox.Show(Width.ToString(), Height.ToString(), System.Windows.Forms.MessageBoxButtons.OK); // A Null Renderer does not display the video // But it allows the Sample Grabber to run // And it will keep proper playback timing // Unless specified otherwise. DirectShowLib.NullRenderer nullRenderer = new DirectShowLib.NullRenderer(); m_graphBuilder.AddFilter((DirectShowLib.IBaseFilter)nullRenderer, "Null Renderer"); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, (DirectShowLib.IBaseFilter)nullRenderer); DsError.ThrowExceptionForHR(hr); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting SaveSizeInfo(m_sampGrabber); // Configure the Video Window //DirectShowLib.IVideoWindow videoWindow = m_graphBuilder as DirectShowLib.IVideoWindow; //ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_graphBuilder as DirectShowLib.IMediaEvent; m_mediaCtrl = m_graphBuilder as DirectShowLib.IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } #if DEBUG // Double check to make sure we aren't releasing something // important. GC.Collect(); GC.WaitForPendingFinalizers(); #endif }