private void BuildGraph(string sFileName, out IFilterGraph2 graphBuilder, out IBaseFilter audioFilter) { int hr = 0; IBaseFilter sourceFilter = null; IPin pinOut = null; graphBuilder = new FilterGraph() as IFilterGraph2; audioFilter = null; try { hr = graphBuilder.AddSourceFilter(sFileName, sFileName, out sourceFilter); Marshal.ThrowExceptionForHR(hr); audioFilter = (IBaseFilter) new DSoundRender(); hr = graphBuilder.AddFilter(audioFilter, "DirectSound Renderer"); pinOut = DsFindPin.ByDirection(sourceFilter, PinDirection.Output, 0); hr = graphBuilder.RenderEx(pinOut, AMRenderExFlags.RenderToExistingRenderers, IntPtr.Zero); Marshal.ThrowExceptionForHR(hr); } catch { Marshal.ReleaseComObject(graphBuilder); throw; } finally { Marshal.ReleaseComObject(sourceFilter); Marshal.ReleaseComObject(pinOut); } }
private void Config() { int hr; IBaseFilter pFilter; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); m_fg = (IFilterGraph2) new FilterGraph(); DsROTEntry rot = new DsROTEntry(m_fg); hr = icgb.SetFiltergraph(m_fg); DsError.ThrowExceptionForHR(hr); m_mo = (IMixerOCX) new OverlayMixer(); TestAdvise(); hr = m_fg.AddFilter((IBaseFilter)m_mo, null); DsError.ThrowExceptionForHR(hr); hr = m_fg.AddSourceFilter(@"foo.avi", null, out pFilter); DsError.ThrowExceptionForHR(hr); IPin iPin = DsFindPin.ByDirection(pFilter, PinDirection.Output, 0); hr = icgb.RenderStream(null, null, iPin, null, (IBaseFilter)m_mo); DsError.ThrowExceptionForHR(hr); hr = ((IMediaControl)m_fg).Run(); DsError.ThrowExceptionForHR(hr); }
private void BuildGraph(string sFileName, out IFilterGraph2 graphBuilder, out IBaseFilter sourceFilter, out IBaseFilter vmr9Filter) { int hr = 0; graphBuilder = new FilterGraph() as IFilterGraph2; vmr9Filter = null; try { hr = graphBuilder.AddSourceFilter(sFileName, sFileName, out sourceFilter); Marshal.ThrowExceptionForHR(hr); vmr9Filter = (IBaseFilter) new VideoMixingRenderer9(); hr = graphBuilder.AddFilter(vmr9Filter, "VMR9"); Marshal.ThrowExceptionForHR(hr); } catch { Marshal.ReleaseComObject(graphBuilder); throw; } finally { } }
private void Configure2() { int hr; IBaseFilter pFilter; IBaseFilter ibf; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); IFilterGraph2 FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(FilterGraph); hr = icgb.SetFiltergraph(FilterGraph); DsError.ThrowExceptionForHR(hr); hr = FilterGraph.AddSourceFilter(FileName, "foo", out pFilter); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, null, null); DsError.ThrowExceptionForHR(hr); hr = FilterGraph.FindFilterByName("AVI Splitter", out ibf); DsError.ThrowExceptionForHR(hr); m_pPersist2 = (IPersistMediaPropertyBag)ibf; Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(icgb); }
private void Configure2() { // In order to lock a profile, you have to have at least one stream // connected to the sink. I connect a video thru the DVVideoEnc into // the StreamBufferSink. int hr; IBaseFilter pFilter; IBaseFilter ibf; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(m_FilterGraph); hr = icgb.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.AddSourceFilter(FileName, "foo", out pFilter); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, null, null); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.FindFilterByName("AVI Splitter", out ibf); DsError.ThrowExceptionForHR(hr); m_imc = (IAMMediaContent)ibf; Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(icgb); }
private void ConfigGraph(IFilterGraph2 fg) { int hr = 0; hr = fg.AddSourceFilter("foo.avi", "Source Filter", out this.sourceFilter); Marshal.ThrowExceptionForHR(hr); }
private void SetupGraph3(Control hWin, string FileName) { int hr; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); IBaseFilter pAudioRenderer = (IBaseFilter) new DSoundRender(); hr = m_FilterGraph.AddFilter(pAudioRenderer, "Audio Renderer"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, null, null); DsError.ThrowExceptionForHR(hr); hr = icgb2.RenderStream(null, MediaType.Audio, sourceFilter, null, pAudioRenderer); DsError.ThrowExceptionForHR(hr); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; m_mediaSeeking = m_FilterGraph as IMediaSeeking; m_basicAudio = m_FilterGraph as IBasicAudio; } 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 }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR(hr); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }
private void Configure() { // In order to lock a profile, you have to have at least one stream // connected to the sink. I connect a video thru the DVVideoEnc into // the StreamBufferSink. int hr; IBaseFilter pFilter; IBaseFilter mux; IFileSinkFilter ifsf; EventCode ec; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); IFilterGraph2 filterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(filterGraph); hr = icgb.SetFiltergraph(filterGraph); DsError.ThrowExceptionForHR(hr); hr = filterGraph.AddSourceFilter("foo.avi", "foo", out pFilter); DsError.ThrowExceptionForHR(hr); hr = icgb.SetOutputFileName(MediaSubType.Avi, FileName, out mux, out ifsf); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, null, mux); DsError.ThrowExceptionForHR(hr); IMediaPropertyBag impb = (IMediaPropertyBag) new MediaPropertyBag(); Load(impb, "IART", Author); Load(impb, "ICOP", Copyright); Load(impb, "INAM", Title); IPersistMediaPropertyBag pPersist = (IPersistMediaPropertyBag)mux; hr = pPersist.Load(impb, null); DsError.ThrowExceptionForHR(hr); ((IMediaControl)filterGraph).Run(); ((IMediaEvent)filterGraph).WaitForCompletion(-1, out ec); ((IMediaControl)filterGraph).Stop(); Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(mux); Marshal.ReleaseComObject(filterGraph); Marshal.ReleaseComObject(ifsf); Marshal.ReleaseComObject(impb); Marshal.ReleaseComObject(icgb); }
private void Configure() { int hr; IBaseFilter pFilter; IBaseFilter mux; IFileSinkFilter ifsf; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); IFilterGraph2 filterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(filterGraph); hr = icgb.SetFiltergraph(filterGraph); DsError.ThrowExceptionForHR(hr); hr = filterGraph.AddSourceFilter("foo.avi", "foo", out pFilter); DsError.ThrowExceptionForHR(hr); hr = icgb.SetOutputFileName(MediaSubType.Avi, FileName, out mux, out ifsf); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, null, mux); DsError.ThrowExceptionForHR(hr); m_impb = (IMediaPropertyBag) new MediaPropertyBag(); Load(m_impb, "IART", Author); Load(m_impb, "ICOP", Copyright); Load(m_impb, "INAM", Title); IPersistMediaPropertyBag pPersist = (IPersistMediaPropertyBag)mux; hr = pPersist.Load(m_impb, null); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(mux); Marshal.ReleaseComObject(filterGraph); Marshal.ReleaseComObject(ifsf); Marshal.ReleaseComObject(icgb); }
private void StartCapture() { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; if (System.IO.File.Exists(txtAviFileName.Text)) { // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the video source hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter); DsError.ThrowExceptionForHR(hr); //add AVI Decompressor IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec(); hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor"); DsError.ThrowExceptionForHR(hr); IBaseFilter ffdshow; try { // Create Decoder filter COM object (ffdshow video decoder) Type comtype = Type.GetTypeFromCLSID(new Guid("{04FE9017-F873-410E-871E-AB91661A4EF7}")); if (comtype == null) { throw new NotSupportedException("Creating ffdshow video decoder COM object fails."); } object comobj = Activator.CreateInstance(comtype); ffdshow = (IBaseFilter)comobj; // error ocurrs! raised exception comobj = null; } catch { CustomMessageBox.Show("Please install/reinstall ffdshow"); return; } hr = m_FilterGraph.AddFilter(ffdshow, "ffdshow"); DsError.ThrowExceptionForHR(hr); // IBaseFilter baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); IBaseFilter vidrender = (IBaseFilter) new VideoRenderer(); hr = m_FilterGraph.AddFilter(vidrender, "Render"); DsError.ThrowExceptionForHR(hr); IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IPin ffdpinin = DsFindPin.ByName(ffdshow, "In"); IPin ffdpinout = DsFindPin.ByName(ffdshow, "Out"); IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input"); hr = m_FilterGraph.Connect(captpin, ffdpinin); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(ffdpinout, samppin); DsError.ThrowExceptionForHR(hr); FileWriter filewritter = new FileWriter(); IFileSinkFilter filemux = (IFileSinkFilter)filewritter; //filemux.SetFileName("test.avi",); //hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender); // DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // setup buffer if (m_handle == IntPtr.Zero) { m_handle = Marshal.AllocCoTaskMem(m_stride * m_videoHeight); } // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); m_bGotOne = false; m_bRunning = false; timer1 = new Thread(timer); timer1.IsBackground = true; timer1.Start(); m_mediaextseek = m_FilterGraph as IAMExtendedSeeking; m_mediapos = m_FilterGraph as IMediaPosition; m_mediaseek = m_FilterGraph as IMediaSeeking; double length = 0; m_mediapos.get_Duration(out length); trackBar_mediapos.Minimum = 0; trackBar_mediapos.Maximum = (int)length; Start(); } else { MessageBox.Show("File does not exist"); } }
// 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_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber)new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph IBaseFilter nullrenderer = new NullRenderer() as IBaseFilter; hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, nullrenderer); DsError.ThrowExceptionForHR(hr); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting SaveSizeInfo(m_sampGrabber); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as 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 }
private void StartCapture() { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; if (System.IO.File.Exists(txtAviFileName.Text)) { // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the video source hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter); DsError.ThrowExceptionForHR(hr); //add AVI Decompressor IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec(); hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor"); DsError.ThrowExceptionForHR(hr); IBaseFilter ffdshow; try { // Create Decoder filter COM object (ffdshow video decoder) Type comtype = Type.GetTypeFromCLSID(new Guid("{04FE9017-F873-410E-871E-AB91661A4EF7}")); if (comtype == null) throw new NotSupportedException("Creating ffdshow video decoder COM object fails."); object comobj = Activator.CreateInstance(comtype); ffdshow = (IBaseFilter) comobj; // error ocurrs! raised exception comobj = null; } catch { CustomMessageBox.Show("Please install/reinstall ffdshow"); return; } hr = m_FilterGraph.AddFilter(ffdshow, "ffdshow"); DsError.ThrowExceptionForHR(hr); // IBaseFilter baseGrabFlt = (IBaseFilter) sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); IBaseFilter vidrender = (IBaseFilter) new VideoRenderer(); hr = m_FilterGraph.AddFilter(vidrender, "Render"); DsError.ThrowExceptionForHR(hr); IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IPin ffdpinin = DsFindPin.ByName(ffdshow, "In"); IPin ffdpinout = DsFindPin.ByName(ffdshow, "Out"); IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input"); hr = m_FilterGraph.Connect(captpin, ffdpinin); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(ffdpinout, samppin); DsError.ThrowExceptionForHR(hr); FileWriter filewritter = new FileWriter(); IFileSinkFilter filemux = (IFileSinkFilter) filewritter; //filemux.SetFileName("test.avi",); //hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender); // DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // setup buffer if (m_handle == IntPtr.Zero) m_handle = Marshal.AllocCoTaskMem(m_stride*m_videoHeight); // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); m_bGotOne = false; m_bRunning = false; timer1 = new Thread(timer); timer1.IsBackground = true; timer1.Start(); m_mediaextseek = m_FilterGraph as IAMExtendedSeeking; m_mediapos = m_FilterGraph as IMediaPosition; m_mediaseek = m_FilterGraph as IMediaSeeking; double length = 0; m_mediapos.get_Duration(out length); trackBar_mediapos.Minimum = 0; trackBar_mediapos.Maximum = (int) length; Start(); } else { MessageBox.Show("File does not exist"); } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName, Control hWin) { int hr; IBaseFilter ibfRenderer = null; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin iPinInFilter = null; IPin iPinOutFilter = null; IPin iPinInDest = null; IBasicAudio basicAudio = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif try { // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Hopefully this will be the video pin IPin iPinOutSource = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter); DsError.ThrowExceptionForHR(hr); // Get the default video renderer ibfRenderer = (IBaseFilter)new VideoRendererDefault(); // Add it to the graph hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault"); DsError.ThrowExceptionForHR(hr); iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0); // Connect the graph. Many other filters automatically get added here hr = m_FilterGraph.Connect(iPinOutFilter, iPinInDest); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // Set the output window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; hr = videoWindow.put_Owner(hWin.Handle); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); //TODO : Need a better way to hide the video in the parent Window Rectangle rc = hWin.ClientRectangle; if (mParentWindowDisplay) hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom); else hr = videoWindow.SetWindowPosition(0, 0, 0, 0); DsError.ThrowExceptionForHR(hr); IGraphBuilder graphBuilder = m_FilterGraph as IGraphBuilder; ICaptureGraphBuilder2 captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); hr = captureGraphBuilder.RenderStream(null, MediaType.Audio, capFilter, null, null); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (ibfRenderer != null) { Marshal.ReleaseComObject(ibfRenderer); ibfRenderer = null; } if (iPinInFilter != null) { Marshal.ReleaseComObject(iPinInFilter); iPinInFilter = null; } if (iPinOutFilter != null) { Marshal.ReleaseComObject(iPinOutFilter); iPinOutFilter = null; } if (iPinInDest != null) { Marshal.ReleaseComObject(iPinInDest); iPinInDest = null; } } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter nullrenderer = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_FilterGraph as IMediaControl; m_MediaEvent = m_FilterGraph as IMediaEvent; IMediaFilter mediaFilt = m_FilterGraph as IMediaFilter; try { #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the file filter to the sample grabber // Hopefully this will be the video pin, we could check by reading it's mediatype IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); // Get the input pin from the sample grabber IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph nullrenderer = new NullRenderer() as IBaseFilter; hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the sample grabber to the null renderer iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Turn off the clock. This causes the frames to be sent // thru the graph as fast as possible hr = mediaFilt.SetSyncSource(null); DsError.ThrowExceptionForHR(hr); // Read and cache the image sizes SaveSizeInfo(sampGrabber); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (nullrenderer != null) { Marshal.ReleaseComObject(nullrenderer); nullrenderer = null; } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR( hr ); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter) m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter( baseGrabFlt, "Ds.NET Grabber" ); DsError.ThrowExceptionForHR( hr ); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR( hr ); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }
private void BuildGraph() { int hr = 0; Guid wavesReverbDMO = new Guid("87fc0268-9a55-4360-95aa-004a1d9de26c"); IPin pinIn, pinOut; graphBuilder = (IFilterGraph2) new FilterGraph(); // Add a source filter hr = graphBuilder.AddSourceFilter(@"..\..\..\Resources\foo.avi", "foo.avi", out fileSource); DsError.ThrowExceptionForHR(hr); // Add an Avi Splitter aviSplitter = (IBaseFilter) new AviSplitter(); hr = graphBuilder.AddFilter(aviSplitter, "Splitter"); DsError.ThrowExceptionForHR(hr); // Add a DMO Wrapper Filter dmoFilter = (IBaseFilter) new DMOWrapperFilter(); dmoWrapperFilter = (IDMOWrapperFilter)dmoFilter; // IDMOWrapperFilter unique method is tested here // // Init this filter with WavesReverb DMO Object // hr = dmoWrapperFilter.Init(wavesReverbDMO, DMOCategory.AudioEffect); DsError.ThrowExceptionForHR(hr); // Add it to the Graph hr = graphBuilder.AddFilter(dmoFilter, "DMO Filter"); DsError.ThrowExceptionForHR(hr); // Add an audio renderer directSoundDevice = (IBaseFilter) new DSoundRender(); hr = graphBuilder.AddFilter(directSoundDevice, "DirectSound Device"); DsError.ThrowExceptionForHR(hr); // Connect Source filter with Avi Splitter pinOut = DsFindPin.ByDirection(fileSource, PinDirection.Output, 0); pinIn = DsFindPin.ByDirection(aviSplitter, PinDirection.Input, 0); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(pinOut); Marshal.ReleaseComObject(pinIn); // Connect Avi Splitter audio (2nd) pin with DMO Wrapper // Should add an audio codec between the 2 filters pinOut = DsFindPin.ByDirection(aviSplitter, PinDirection.Output, 1); pinIn = DsFindPin.ByDirection(dmoFilter, PinDirection.Input, 0); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(pinOut); Marshal.ReleaseComObject(pinIn); // Connect DMO Wrapper with the audio renderer pinOut = DsFindPin.ByDirection(dmoFilter, PinDirection.Output, 0); pinIn = DsFindPin.ByDirection(directSoundDevice, PinDirection.Input, 0); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(pinOut); Marshal.ReleaseComObject(pinIn); // Run this graph (just for fun) hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); System.Threading.Thread.Sleep(4000); }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_FilterGraph); #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_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); 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 IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as 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 }
private void SetupGraph(string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter nullrenderer = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_FilterGraph as IMediaControl; m_MediaEvent = m_FilterGraph as IMediaEvent; IMediaFilter mediaFilt = m_FilterGraph as IMediaFilter; try { // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the file filter to the sample grabber // Hopefully this will be the audio pin, we could check by reading it's mediatype IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); // Get the input pin from the sample grabber IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph nullrenderer = new NullRenderer() as IBaseFilter; hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the sample grabber to the null renderer iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Turn off the clock. This causes the frames to be sent // thru the graph as fast as possible hr = mediaFilt.SetSyncSource(null); DsError.ThrowExceptionForHR(hr); // Read and cache the image sizes SaveSizeInfo(sampGrabber); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (nullrenderer != null) { Marshal.ReleaseComObject(nullrenderer); nullrenderer = null; } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; IBaseFilter ibfRenderer = null; IPin iPinInFilter = null; IPin iPinOutFilter = null; IPin iPinInDest = null; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out sourceFilter); DsError.ThrowExceptionForHR(hr); // Hopefully this will be the video pin IPin iPinOutSource = DsFindPin.ByDirection(sourceFilter, PinDirection.Output, 0); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber)new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter); DsError.ThrowExceptionForHR(hr); // Get the default video renderer ibfRenderer = (IBaseFilter)new VideoRendererDefault(); // Add it to the graph hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault"); DsError.ThrowExceptionForHR(hr); iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0); // Connect the graph. Many other filters automatically get added here hr = m_FilterGraph.Connect(iPinOutFilter, iPinInDest); DsError.ThrowExceptionForHR(hr); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; m_mediaPosition = m_FilterGraph as IMediaPosition; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName, Control hWin) { int hr; IBaseFilter ibfRenderer = null; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin iPinInFilter = null; IPin iPinOutFilter = null; IPin iPinInDest = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif try { // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Hopefully this will be the video pin IPin iPinOutSource = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter); DsError.ThrowExceptionForHR(hr); // Get the default video renderer ibfRenderer = (IBaseFilter) new VideoRendererDefault(); // Add it to the graph hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault"); DsError.ThrowExceptionForHR(hr); iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0); // Connect the graph. Many other filters automatically get added here hr = m_FilterGraph.Connect(iPinOutFilter, iPinInDest); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // Set the output window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; hr = videoWindow.put_Owner(hWin.Handle); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); Rectangle rc = hWin.ClientRectangle; hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom); DsError.ThrowExceptionForHR(hr); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (ibfRenderer != null) { Marshal.ReleaseComObject(ibfRenderer); ibfRenderer = null; } if (iPinInFilter != null) { Marshal.ReleaseComObject(iPinInFilter); iPinInFilter = null; } if (iPinOutFilter != null) { Marshal.ReleaseComObject(iPinOutFilter); iPinOutFilter = null; } if (iPinInDest != null) { Marshal.ReleaseComObject(iPinInDest); iPinInDest = null; } } }
private void StartCapture() { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; if (System.IO.File.Exists(txtAviFileName.Text)) { // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the video source hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter); DsError.ThrowExceptionForHR(hr); //add AVI Decompressor IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec(); hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor"); DsError.ThrowExceptionForHR(hr); // IBaseFilter baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); IBaseFilter vidrender = (IBaseFilter) new VideoRenderer(); hr = m_FilterGraph.AddFilter(vidrender, "Render"); DsError.ThrowExceptionForHR(hr); IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input"); hr = m_FilterGraph.Connect(captpin, samppin); DsError.ThrowExceptionForHR(hr); FileWriter filewritter = new FileWriter(); IFileSinkFilter filemux = (IFileSinkFilter)filewritter; //filemux.SetFileName("test.avi",); //hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender); // DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // setup buffer if (m_handle == IntPtr.Zero) { m_handle = Marshal.AllocCoTaskMem(m_stride * m_videoHeight); } // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); m_bGotOne = false; m_bRunning = false; timer1 = new Thread(timer); timer1.IsBackground = true; timer1.Start(); m_mediaextseek = m_FilterGraph as IAMExtendedSeeking; m_mediapos = m_FilterGraph as IMediaPosition; m_mediaseek = m_FilterGraph as IMediaSeeking; double length = 0; m_mediapos.get_Duration(out length); trackBar_mediapos.Minimum = 0; trackBar_mediapos.Maximum = (int)length; Start(); } else { MessageBox.Show("File does not exist"); } }